From 9bbe0077104e5e0dab8b1a8f197f7ce9bb240be4 Mon Sep 17 00:00:00 2001 From: Thomas Voss Date: Tue, 7 Nov 2023 13:39:23 +0100 Subject: nvim: Make LSP config more minimal --- .config/nvim/after/plugin/lsp.lua | 84 ++++++++++++++++++---------------- .config/nvim/after/plugin/nvim-cmp.lua | 31 +++++++++++++ .config/nvim/lua/mango/packer.lua | 39 +++++++++++----- 3 files changed, 104 insertions(+), 50 deletions(-) create mode 100644 .config/nvim/after/plugin/nvim-cmp.lua diff --git a/.config/nvim/after/plugin/lsp.lua b/.config/nvim/after/plugin/lsp.lua index dcf98f1..12e1025 100644 --- a/.config/nvim/after/plugin/lsp.lua +++ b/.config/nvim/after/plugin/lsp.lua @@ -1,40 +1,46 @@ -local lsp = require('lsp-zero') -local lib = require('mango.lib') - -lsp.preset('recommended') -lsp.ensure_installed({ - 'clangd', - 'gopls', - 'rust_analyzer', -}) - -lsp.nvim_workspace() - -local cmp = require('cmp') -local cmp_select = { behavior = cmp.SelectBehavior.Select } -local cmp_mappings = lsp.defaults.cmp_mappings({ - [''] = cmp.mapping.select_prev_item(cmp_select), - [''] = cmp.mapping.select_next_item(cmp_select), - [''] = cmp.mapping.confirm({ select = true }), - [''] = cmp.mapping.complete(), -}) - -lsp.set_preferences({ - suggest_lsp_servers = false, - sign_icons = {} +local conf = require('lspconfig') + +conf.clangd.setup {} +conf.gopls.setup {} +conf.rust_analyzer.setup {} +conf.lua_ls.setup { + settings = { + Lua = { + runtime = { + version = 'LuaJIT', + }, + diagnostics = { + globals = { + 'vim', + 'require', + }, + }, + workspace = { + library = vim.api.nvim_get_runtime_file('', true), + }, + telemetry = { + enable = false, + }, + }, + }, +} + +vim.api.nvim_create_autocmd('LspAttach', { + group = vim.api.nvim_create_augroup('MangoLspConfig', {}), + callback = function(ev) + local function remap(mode, map, fn) + vim.keymap.set(mode, map, fn, { buffer = ev.buf }) + end + + vim.bo[ev.buf].omnifunc = 'v:lua.vim.lsp.omnifunc' + remap('n', 'K', vim.lsp.buf.hover) + remap('n', 'gd', vim.lsp.buf.definition) + remap('n', 'gi', vim.lsp.buf.implementation) + remap('n', 'gr', vim.lsp.buf.rename) + remap('n', 'gt', vim.lsp.buf.type_definition) + remap('n', 'g=', function() vim.lsp.buf.format { async = true } end) + remap('n', ']d', vim.diagnostic.goto_prev) + remap('n', '[d', vim.diagnostic.goto_next) + remap('n', 'la', vim.lsp.buf.code_action) + end, }) - -lsp.setup_nvim_cmp({ - mapping = cmp_mappings -}) - -lsp.on_attach(function(_, bufnr) - local opts = { buffer = bufnr, remap = false } - - lib.remap('n', 'gd', vim.lsp.buf.definition, opts) - lib.remap('n', 'la', vim.lsp.buf.code_action, opts) - lib.remap('n', 'lr', vim.lsp.buf.rename, opts) - lib.remap('n', 'l=', vim.lsp.buf.format, opts) -end) - -lsp.setup() diff --git a/.config/nvim/after/plugin/nvim-cmp.lua b/.config/nvim/after/plugin/nvim-cmp.lua new file mode 100644 index 0000000..4bc95c5 --- /dev/null +++ b/.config/nvim/after/plugin/nvim-cmp.lua @@ -0,0 +1,31 @@ +local conf = require('lspconfig') +local capabilities = require('cmp_nvim_lsp').default_capabilities() +conf['clangd'].setup { capabilities = capabilities } +conf['gopls'].setup { capabilities = capabilities } +conf['lua_ls'].setup { capabilities = capabilities } +conf['rust_analyzer'].setup { capabilities = capabilities } + +local cmp = require('cmp') +cmp.setup { + snippet = { + expand = function(args) + require('luasnip').lsp_expand(args.body) + end, + }, + mapping = cmp.mapping.preset.insert({ + [''] = cmp.mapping.select_prev_item(), + [''] = cmp.mapping.select_next_item(), + [''] = cmp.mapping.abort(), + [''] = cmp.mapping.confirm({ select = true }), + }), + sources = cmp.config.sources({ + { name = 'nvim_lsp' }, + { name = 'path' }, + { name = 'luasnip' }, + }, { + { name = 'buffer', length = 5 }, + }), + experimental = { + ghost_text = true, + } +} diff --git a/.config/nvim/lua/mango/packer.lua b/.config/nvim/lua/mango/packer.lua index 02aa11a..7f6beb1 100644 --- a/.config/nvim/lua/mango/packer.lua +++ b/.config/nvim/lua/mango/packer.lua @@ -36,20 +36,37 @@ return require('packer').startup(function(use) use 'Glench/Vim-Jinja2-Syntax' use 'luckasRanarison/tree-sitter-hypr' - -- LSP support + -- LSP support and completions + use 'neovim/nvim-lspconfig' use { - 'VonHeikemen/lsp-zero.nvim', - branch = 'v2.x', + 'hrsh7th/nvim-cmp', requires = { - -- LSP Support - { 'neovim/nvim-lspconfig' }, - { 'williamboman/mason.nvim' }, - { 'williamboman/mason-lspconfig.nvim' }, - - -- Autocompletion - { 'hrsh7th/nvim-cmp' }, + -- Completion backends + { 'hrsh7th/cmp-buffer' }, + { 'hrsh7th/cmp-nvim-lua' }, { 'hrsh7th/cmp-nvim-lsp' }, + { 'hrsh7th/cmp-path' }, + + -- Snippets { 'L3MON4D3/LuaSnip' }, - } + { 'saadparwaiz1/cmp_luasnip' }, + }, } + + -- LSP support + -- use { + -- 'VonHeikemen/lsp-zero.nvim', + -- branch = 'v2.x', + -- requires = { + -- -- LSP Support + -- { 'neovim/nvim-lspconfig' }, + -- { 'williamboman/mason.nvim' }, + -- { 'williamboman/mason-lspconfig.nvim' }, + + -- -- Autocompletion + -- { 'hrsh7th/nvim-cmp' }, + -- { 'hrsh7th/cmp-nvim-lsp' }, + -- { 'L3MON4D3/LuaSnip' }, + -- } + -- } end) -- cgit v1.2.3