From 1d0c48d02a6bed3a46e5f4f2ca7e8ec874bd1e10 Mon Sep 17 00:00:00 2001 From: Gabriel Arazas Date: Sat, 28 Oct 2023 12:13:32 +0800 Subject: [PATCH] nvim: move LSP config to the plugin --- nvim/init.lua | 1 - nvim/lua/lsp-user-config.lua | 54 ------------------------------ nvim/lua/plugins/completion.lua | 58 ++++++++++++++++++++++++++++++++- 3 files changed, 57 insertions(+), 56 deletions(-) delete mode 100644 nvim/lua/lsp-user-config.lua diff --git a/nvim/init.lua b/nvim/init.lua index 093de34..fc575e2 100644 --- a/nvim/init.lua +++ b/nvim/init.lua @@ -15,5 +15,4 @@ end vim.opt.rtp:prepend(lazypath) require("settings").setup() -require("lsp-user-config").setup() require("lazy").setup("plugins") diff --git a/nvim/lua/lsp-user-config.lua b/nvim/lua/lsp-user-config.lua deleted file mode 100644 index 1f57ffd..0000000 --- a/nvim/lua/lsp-user-config.lua +++ /dev/null @@ -1,54 +0,0 @@ --- LSP config -local nvim_lsp = require("lspconfig") - -function setup() - -- Use an on_attach function to only map the following keys - -- after the language server attaches to the current buffer - local on_attach = function(client, bufnr) - local function buf_set_option(...) - vim.api.nvim_buf_set_option(bufnr, ...) - end - - -- Enable completion triggered by - buf_set_option("omnifunc", "v:lua.vim.lsp.omnifunc") - - -- Mappings. - local opts = { noremap = true, silent = true } - - -- See `:help vim.lsp.*` for documentation on any of the below functions - vim.keymap.set("n", "gD", vim.lsp.buf.declaration, opts) - vim.keymap.set("n", "gd", vim.lsp.buf.definition, opts) - vim.keymap.set("n", "K", vim.lsp.buf.hover, opts) - vim.keymap.set("n", "gi", vim.lsp.buf.implementation, opts) - vim.keymap.set("n", "", vim.lsp.buf.signature_help, opts) - vim.keymap.set("n", "wa", vim.lsp.buf.add_workspace_folder, opts) - vim.keymap.set("n", "wr", vim.lsp.buf.remove_workspace_folder, opts) - vim.keymap.set("n", "wl", print(vim.inspect(vim.lsp.buf.list_workspace_folders, opts))) - vim.keymap.set("n", "D", vim.lsp.buf.type_definition, opts) - vim.keymap.set("n", "rn", vim.lsp.buf.rename, opts) - vim.keymap.set("n", "ca", vim.lsp.buf.code_action, opts) - vim.keymap.set("n", "gr", vim.lsp.buf.references, opts) - vim.keymap.set("n", "e", vim.lsp.diagnostic.show_line_diagnostics, opts) - vim.keymap.set("n", "[d", vim.lsp.diagnostic.goto_prev, opts) - vim.keymap.set("n", "]d", vim.lsp.diagnostic.goto_next, opts) - vim.keymap.set("n", "q", vim.lsp.diagnostic.set_loclist, opts) - vim.keymap.set("n", "f", vim.lsp.buf.formatting, opts) - end - - local capabilities = vim.lsp.protocol.make_client_capabilities() - capabilities.textDocument.completion.completionItem.snippetSupport = true - capabilities = require("cmp_nvim_lsp").default_capabilities(capabilities) - - -- Enable the following language servers - local servers = { "clangd", "rust_analyzer", "pyright", "tsserver", "rnix", "lua_ls" } - for _, lsp in ipairs(servers) do - nvim_lsp[lsp].setup({ - on_attach = on_attach, - capabilities = capabilities, - }) - end -end - -return { - setup = setup, -} diff --git a/nvim/lua/plugins/completion.lua b/nvim/lua/plugins/completion.lua index 8a5d496..8279801 100644 --- a/nvim/lua/plugins/completion.lua +++ b/nvim/lua/plugins/completion.lua @@ -59,8 +59,64 @@ return { }, -- A bunch of pre-configured LSP configurations. - "neovim/nvim-lspconfig", + { + "neovim/nvim-lspconfig", + dependencies = { "hrsh7th/cmp-nvim-lsp" }, + config = function() + local nvim_lsp = require("lspconfig") + -- Use an on_attach function to only map the following keys + -- after the language server attaches to the current buffer + local on_attach = function(client, bufnr) + local function buf_set_option(...) + vim.api.nvim_buf_set_option(bufnr, ...) + end + -- Enable completion triggered by + buf_set_option("omnifunc", "v:lua.vim.lsp.omnifunc") + -- Mappings. + local opts = { noremap = true, silent = true } + + -- See `:help vim.lsp.*` for documentation on any of the below functions + vim.keymap.set("n", "gD", vim.lsp.buf.declaration, opts) + vim.keymap.set("n", "gd", vim.lsp.buf.definition, opts) + vim.keymap.set("n", "K", vim.lsp.buf.hover, opts) + vim.keymap.set("n", "gi", vim.lsp.buf.implementation, opts) + vim.keymap.set("n", "", vim.lsp.buf.signature_help, opts) + vim.keymap.set("n", "wa", vim.lsp.buf.add_workspace_folder, opts) + vim.keymap.set("n", "wr", vim.lsp.buf.remove_workspace_folder, opts) + vim.keymap.set("n", "wl", print(vim.inspect(vim.lsp.buf.list_workspace_folders, opts))) + vim.keymap.set("n", "D", vim.lsp.buf.type_definition, opts) + vim.keymap.set("n", "rn", vim.lsp.buf.rename, opts) + vim.keymap.set("n", "ca", vim.lsp.buf.code_action, opts) + vim.keymap.set("n", "gr", vim.lsp.buf.references, opts) + vim.keymap.set("n", "e", vim.lsp.diagnostic.show_line_diagnostics, opts) + vim.keymap.set("n", "[d", vim.lsp.diagnostic.goto_prev, opts) + vim.keymap.set("n", "]d", vim.lsp.diagnostic.goto_next, opts) + vim.keymap.set("n", "q", vim.lsp.diagnostic.set_loclist, opts) + vim.keymap.set("n", "f", vim.lsp.buf.formatting, opts) + end + + local capabilities = vim.lsp.protocol.make_client_capabilities() + capabilities.textDocument.completion.completionItem.snippetSupport = true + capabilities = require("cmp_nvim_lsp").default_capabilities(capabilities) + + -- Enable the following language servers + local servers = { + "clangd", + "rust_analyzer", + "pyright", + "tsserver", + "nil_ls", + "lua_ls", + } + for _, lsp in ipairs(servers) do + nvim_lsp[lsp].setup({ + on_attach = on_attach, + capabilities = capabilities, + }) + end + end, + }, }