From 40839ad70b50a77ccf0555e0c4ce8eda48455490 Mon Sep 17 00:00:00 2001 From: David JULIEN Date: Wed, 27 Apr 2022 20:12:47 +0200 Subject: [PATCH] [nvim] feat: configure LSPs through lsp-installer !!!BREAK: do not use lsp-config (used under the hood by nvim-lsp-installer) --- .config/nvim/lua/lsp/c.lua | 20 ---- .config/nvim/lua/lsp/init.lua | 5 +- .config/nvim/lua/lsp/lua.lua | 40 -------- .config/nvim/lua/lsp/python.lua | 8 -- .config/nvim/lua/lsp/tex.lua | 13 --- .config/nvim/lua/plugin/lsp_installer.lua | 106 +++++++++++++++++++--- .config/nvim/lua/plugin/packer.lua | 14 +-- 7 files changed, 97 insertions(+), 109 deletions(-) delete mode 100644 .config/nvim/lua/lsp/c.lua delete mode 100644 .config/nvim/lua/lsp/lua.lua delete mode 100644 .config/nvim/lua/lsp/python.lua delete mode 100644 .config/nvim/lua/lsp/tex.lua diff --git a/.config/nvim/lua/lsp/c.lua b/.config/nvim/lua/lsp/c.lua deleted file mode 100644 index 986eca3..0000000 --- a/.config/nvim/lua/lsp/c.lua +++ /dev/null @@ -1,20 +0,0 @@ --- Author : swytch --- Created : Tuesday May 18, 2021 12:08:51 CET --- License : GPLv3 --- Description : clangd config file for lsp - --- The nvim-cmp almost supports LSP's capabilities so You should advertise it to LSP servers.. -local capabilities = vim.lsp.protocol.make_client_capabilities() -capabilities = require('cmp_nvim_lsp').update_capabilities(capabilities) - - -require("lspconfig").clangd.setup { - cmd = { "clangd", - "--background-index", - "--suggest-missing-includes", - "--clang-tidy", - "--header-insertion=iwyu" - }, - -- The following example advertise capabilities to `clangd`. - capabilities = capabilities, -} diff --git a/.config/nvim/lua/lsp/init.lua b/.config/nvim/lua/lsp/init.lua index 4fbc92a..1cbd609 100644 --- a/.config/nvim/lua/lsp/init.lua +++ b/.config/nvim/lua/lsp/init.lua @@ -66,15 +66,12 @@ M.icons = { Variable = "  ", } + function M.setup() local kinds = vim.lsp.protocol.CompletionItemKind for i, kind in ipairs(kinds) do kinds[i] = M.icons[kind] or kind end - - local ft = vim.bo.filetype - if ft == "cpp" then ft = "c" end - require("lsp." .. ft) -- ./.lua end return M diff --git a/.config/nvim/lua/lsp/lua.lua b/.config/nvim/lua/lsp/lua.lua deleted file mode 100644 index 76caeb6..0000000 --- a/.config/nvim/lua/lsp/lua.lua +++ /dev/null @@ -1,40 +0,0 @@ --- Author : swytch --- Created : Tuesday May 18, 2021 12:08:51 CET --- License : GPLv3 --- Description : sumneko (lua) config file for lsp - - --- set the path to the sumneko installation; if you previously installed via the now deprecated :LspInstall, use -local sumneko_root_path = vim.fn.stdpath("data") .. - "/lsp_servers/sumneko_lua/extension/server" -local sumneko_binary = sumneko_root_path .. "/bin/" .. "/lua-language-server" - -local runtime_path = vim.split(package.path, ';') -table.insert(runtime_path, "lua/?.lua") -table.insert(runtime_path, "lua/?/init.lua") - -require'lspconfig'.sumneko_lua.setup { - cmd = {sumneko_binary, "-E", sumneko_root_path .. "/main.lua"}; - settings = { - Lua = { - runtime = { - -- Tell the language server which version of Lua you're using (most likely LuaJIT in the case of Neovim) - version = 'LuaJIT', - -- Setup your lua path - path = runtime_path, - }, - diagnostics = { - -- Get the language server to recognize the `vim` global - globals = {"vim", "use", "globals", "utils"}, - }, - workspace = { - -- Make the server aware of Neovim runtime files - library = vim.api.nvim_get_runtime_file("", true), - }, - -- Do not send telemetry data containing a randomized but unique identifier - telemetry = { - enable = false, - }, - }, - }, -} diff --git a/.config/nvim/lua/lsp/python.lua b/.config/nvim/lua/lsp/python.lua deleted file mode 100644 index 466a94a..0000000 --- a/.config/nvim/lua/lsp/python.lua +++ /dev/null @@ -1,8 +0,0 @@ --- Author : swytch --- Created : Tuesday May 18, 2021 15:03:43 CET --- License : GPLv3 --- Description : jedi (python) config file for lsp - -require("lspconfig").jedi_language_server.setup{ - cmd = { vim.fn.stdpath("data") .. "/lsp_servers/jedi_language_server/venv/bin/jedi-language-server" } -} diff --git a/.config/nvim/lua/lsp/tex.lua b/.config/nvim/lua/lsp/tex.lua deleted file mode 100644 index a715ed9..0000000 --- a/.config/nvim/lua/lsp/tex.lua +++ /dev/null @@ -1,13 +0,0 @@ --- Author : swytch --- Created : Saturday May 23, 2021 10:34:53 CET --- License : GPLv3 --- Description : LaTeX config file for lsp - -require("lspconfig").texlab.setup { - cmd = { vim.fn.stdpath("data") .. "/lsp_servers/latex/texlab" }, - settings = { - build = { - executable = "pdflatex" - } - } -} diff --git a/.config/nvim/lua/plugin/lsp_installer.lua b/.config/nvim/lua/plugin/lsp_installer.lua index 70e929f..d296e04 100644 --- a/.config/nvim/lua/plugin/lsp_installer.lua +++ b/.config/nvim/lua/plugin/lsp_installer.lua @@ -9,20 +9,18 @@ lsp_installer.settings { ui = { icons = { server_installed = "✓", - server_pending = "➜", - server_uninstalled = "✗" + server_pending = "o", + server_uninstalled = "x" } } } local servers = { - "bashls", - "jedi_language_server", - "texlab", + "clangd", "sumneko_lua", } -for _, name in pairs(servers) do +for _, name in ipairs(servers) do local ok, server = lsp_installer.get_server(name) -- Check that the server is supported in nvim-lsp-installer if ok then @@ -33,15 +31,95 @@ for _, name in pairs(servers) do end end +local lsp_root_path = vim.fn.stdpath("data") .. "/lsp_servers/" +local runtime_path = vim.split(package.path, ';') + + +local clangd_binary = lsp_root_path .. "clangd/clangd/bin/clangd" + +table.insert(runtime_path, "lua/?.lua") +table.insert(runtime_path, "lua/?/init.lua") + +local enhance_server_opts = { + ["clangd"] = function(opts) + opts.settings = { + cmd = { clangd_binary, + "--background-index", + "--suggest-missing-includes", + "--clang-tidy", + "--header-insertion=iwyu" + }, + } + end, + ["sumneko_lua"] = function(opts) + opts.settings = { + Lua = { + runtime = { + -- Tell the language server which version of Lua you're using (most likely LuaJIT in the case of Neovim) + version = 'LuaJIT', + -- Setup your lua path + path = runtime_path, + }, + diagnostics = { + -- Get the language server to recognize the `vim` global + globals = { + "vim", + "globals", + "utils" + }, + }, + workspace = { + -- Make the server aware of Neovim runtime files + library = + vim.api.nvim_get_runtime_file("", true), + }, + -- Do not send telemetry data containing a randomized but unique identifier + telemetry = { + enable = false, + }, + }, + } + end, +} + +local on_attach = function(_, bufnr) + local opts = { buffer = bufnr } + 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', function() + vim.inspect(vim.lsp.buf.list_workspace_folders()) + end, 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', 'gr', vim.lsp.buf.references, opts) + vim.keymap.set('n', 'ca', vim.lsp.buf.code_action, opts) + vim.keymap.set('n', 'so', + require('telescope.builtin').lsp_document_symbols, opts) + vim.api.nvim_create_user_command("Format", vim.lsp.buf.formatting, {}) +end + +-- nvim-cmp supports additional completion capabilities +local capabilities = vim.lsp.protocol.make_client_capabilities() +capabilities = require("cmp_nvim_lsp").update_capabilities(capabilities) + lsp_installer.on_server_ready(function(server) - local opts = {} + -- Specify the default options which we'll use to setup all servers + local opts = { + on_attach = on_attach, + capabilities = capabilities, + } - -- (optional) Customize the options passed to the server - -- if server.name == "tsserver" then - -- opts.root_dir = function() ... end - -- end + if enhance_server_opts[server.name] then + -- Enhance the default opts with the server-specific ones + enhance_server_opts[server.name](opts) + end - -- This setup() function is exactly the same as lspconfig's setup function (:help lspconfig-quickstart) - server:setup(opts) - vim.cmd [[ do User LspAttachBuffers ]] + server:setup(opts) end) diff --git a/.config/nvim/lua/plugin/packer.lua b/.config/nvim/lua/plugin/packer.lua index 501008c..970e546 100644 --- a/.config/nvim/lua/plugin/packer.lua +++ b/.config/nvim/lua/plugin/packer.lua @@ -54,17 +54,11 @@ return require("packer").startup(function() -- LSP use { "williamboman/nvim-lsp-installer", - cmd = { - "LspInstall", - "LspInstallInfo" + requires = { + "neovim/nvim-lspconfig", + config = function() require("lsp").setup() end }, - config = function() require("plugin.lsp_installer").setup() end - } - - use { - "neovim/nvim-lspconfig", - ft = { "c", "cpp", "lua", "python", "tex"}, - config = function() require("lsp").setup() end + config = function() require("plugin.lsp_installer") end } -- auto completion