-- This function configures all the LSP clients that i might care about. -- It is expected to be called in the 'config' step of the 'lspconfig' plugin. -- autocommand: function, that can create autocmds return function() local integrate_into_neovim = function(client, buffer_number) print("LSP " .. client.name .. " [" .. client.id .. "] " .. "started.") -- enable lsp-powered auto-completion local cmp = require("cmp") local types = require("cmp.types") -- adds icons into the completion ui local lspkind = require("lspkind") -- TODO define snippets in some other file... local snippets = require("snippets") cmp.setup({ snippet = { expand = function(arguments) --FIXME no clue if this actually does something.. snippets.lookup_snippet(vim.o.filetype, arguments.body) end, }, completion = { autocomplete = false, }, confirmation = { default_behaviour = types.cmp.ConfirmBehavior.Insert }, mapping = { [""] = cmp.mapping.scroll_docs(-4), [""] = cmp.mapping.scroll_docs(4), [""] = cmp.mapping.complete(), [""] = cmp.mapping.select_next_item(), [""] = cmp.mapping.select_prev_item(), [""] = cmp.mapping.abort(), [""] = cmp.mapping.confirm({ select = true }), }, formatting = { format = function(_, vim_item) vim_item.kind = lspkind.presets.default[vim_item.kind] return vim_item end, }, sources = { { name = "nvim_lsp" }, { name = "nvim_lua" }, -- this source only makes sense in neovim lua files... { name = "buffer" }, { name = "path" }, { name = "emoji" }, { name = "calc" }, }, }) -- Configure diagnostics to only show up on file save vim.lsp.handlers["textDocument/publishDiagnostics"] = vim.lsp.with(vim.lsp.diagnostic.on_publish_diagnostics, { signs = true, underline = false, virtual_text = false, -- FIXME these options are not merged yet --[[ show_diagnostic_autocmds = { "BufWritePost" }, diagnostic_delay = 500, ]] -- TODO -- * clear diagnostics on enter i }) local keys = require("which-key") keys.register({ g = { name = "Goto Navigation", ["D"] = { [[lua vim.lsp.buf.declaration()]], "Goto Declaration" }, ["d"] = { [[lua vim.lsp.buf.definition()]], "Goto Definition" }, ["i"] = { [[lua vim.lsp.buf.implementation()]], "Goto Implementation" }, ["t"] = { [[lua vim.lsp.buf.type_definition()]], "Goto Type Definition" }, ["w"] = { [[lua vim.lsp.buf.document_symbol()]], "Goto Document Symbol" }, ["W"] = { [[lua vim.lsp.buf.workspace_symbol()]], "Goto Workspace Symbol" }, ["n"] = { [[lua vim.lsp.diagnostic.goto_next()]], "Goto Next Diagnostic" }, ["N"] = { [[lua vim.lsp.diagnostic.goto_prev()]], "Goto Previous Diagnostic" }, }, a = { name = "Code Operations", ["h"] = { [[lua vim.lsp.buf.hover()]], "Show Hover" }, ["d"] = { [[lua vim.lsp.diagnostic.show_line_diagnostics()]], "Show Diagnostics on Current Line", }, ["s"] = { [[lua vim.lsp.buf.signature_help()]], "Show Signature Help" }, ["f"] = { [[lua vim.lsp.buf.code_action()]], "Code Actions..." }, ["r"] = { [[lua vim.lsp.buf.rename()]], "Rename" }, ["="] = { [[lua vim.lsp.buf.formatting()]], "Format" }, ["R"] = { [[lua vim.lsp.buf.references()]], "Show References" }, ["i"] = { [[lua vim.lsp.buf.incoming_calls()]], "Show Incoming Calls" }, ["o"] = { [[lua vim.lsp.buf.outgoing_calls()]], "Show Outgoind Calls" }, }, }, { prefix = "", buffer = buffer_number, }) end local lsp = require("lspconfig") local capabilities = vim.lsp.protocol.make_client_capabilities() capabilities = require("cmp_nvim_lsp").update_capabilities(capabilities) -- Nvim lua configuration LSP local neovim_runtime_path = vim.split(package.path, ";") table.insert(neovim_runtime_path, "lua/?.lua") table.insert(neovim_runtime_path, "lua/?/init.lua") local system_name if vim.fn.has("mac") == 1 then system_name = "macOS" elseif vim.fn.has("unix") == 1 then system_name = "Linux" elseif vim.fn.has("win32") == 1 then system_name = "Windows" else print("Unsupported system for sumneko") end local neovim_lua_library = {} for _, path in pairs(vim.api.nvim_list_runtime_paths()) do local lua_path = path .. "/lua/" if vim.fn.isdirectory(lua_path) then neovim_lua_library[lua_path] = true end end local sumneko_home = "~/Workspace/lua-language-server" if system_name == "Windows" then sumneko_home = "W:/misc/lua-language-server" end -- This loads the `lua` files from nvim into the runtime. neovim_lua_library[vim.fn.expand("$VIMRUNTIME/lua")] = true --TODO: How to switch this configuration based upon if we are editing neovim configuration or just any Lua file/project? lsp.sumneko_lua.setup({ on_attach = integrate_into_neovim, capabilites = capabilities, cmd = { sumneko_home .. "/bin/" .. system_name .. "/lua-language-server", "-E", "-W", sumneko_home .. "/main.lua", }, settings = { Lua = { runtime = { version = "LuaJIT", path = neovim_runtime_path, }, diagnostics = { enable = true, disable = "trailing-space", globals = { "vim", "_G", "Dump" }, }, hint = { enable = true, }, workspace = { library = neovim_lua_library, maxPreload = 10000, preloadFileSize = 10000, }, telemetry = { enable = true, }, }, }, }) -- Markdown Notes (Zettelkasten) LSP lsp.zeta_note.setup({ on_attach = integrate_into_neovim, capabilites = capabilities, cmd = { "zeta-note" }, }) -- YAML LSP lsp.yamlls.setup({ on_attach = integrate_into_neovim, capabilites = capabilities, --FIXME: Only works on Windows cmd = { "yaml-language-server.cmd", "--stdio" }, root_dir = function() return vim.fn.getcwd() end, }) -- Zig LSP -- FIXME: ZLS is very tied to the specific version of the zig compiler one uses. -- we need to build our own version for 0.8.0 because it is not done upstream yet lsp.zls.setup({ on_attach = integrate_into_neovim, capabilites = capabilities, }) -- LLVM Clang LSP lsp.clangd.setup({ on_attach = integrate_into_neovim, capabilites = capabilities, }) -- Rust LSP -- this will automatically start and configure nvim-lsp rust-analyzer require("rust-tools").setup({ server = { on_attach = integrate_into_neovim, capabilities = capabilities, } }) -- JSON LSP lsp.jsonls.setup({ on_attach = integrate_into_neovim, capabilites = capabilities, --FIXME: Only works on Windows cmd = { "vscode-json-language-server.cmd", "--stdio" }, commands = { Format = { -- Apply the ranged formatting to the whole file function() vim.lsp.buf.range_formatting({}, { 0, 0 }, { vim.fn.line("$"), 0 }) end, }, }, }) -- GraphViz Dot LSP lsp.dotls.setup({ on_attach = integrate_into_neovim, capabilites = capabilities, }) -- Null-ls local nls = require("null-ls") nls.setup({ debug = false, capabilites = capabilities, -- Format: [CODE] MESSAGE (SOURCE) diagnostics_format = "[#{c}] #{m} (#{s})", sources = { nls.builtins.formatting.stylua, -- this is a nice linter, but wihtout configuration, it spews too much nonsense. -- one can define the stdlib for vim in a config file, but the builtin selene does not find the config file. -- this is a general issue with formatters/linters integrated into neovim. -- how should i handle this? -- write a function, that searches the path to root for a config file? (internal stylua behaviour) -- somehow make sure, that vim root folder is always correct...? -- nls.builtins.diagnostics.selene, }, on_attach = integrate_into_neovim, }) end