-- 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() -- enable lsp-powered auto-completion local cmp = require 'cmp' local types = require 'cmp.types' cmp.setup { snippet = { expand = function(arguments) require('snippy').expand_snippet(arguments.body) end, }, completion = { autocomplete = false, }, confirmation = { default_behaviour = types.cmp.ConfirmBehavior.Insert }, mapping = { [''] = cmp.mapping(cmp.mapping.scroll_docs(-4), { 'i', 'c' }), [''] = cmp.mapping(cmp.mapping.scroll_docs(4), { 'i', 'c' }), [''] = cmp.mapping(cmp.mapping.complete(), { 'i', 'c' }), [''] = cmp.config.disable, [''] = cmp.mapping { i = cmp.mapping.abort(), c = cmp.mapping.close(), }, [''] = cmp.mapping.confirm { select = true }, }, formatting = { format = function(_, vim_item) -- adds icons into the completion ui vim_item.kind = require('lspkind').presets.default[vim_item.kind] return vim_item end, }, sources = { { name = 'nvim_lsp' }, { name = 'snippy' }, { name = 'nvim_lua' }, -- FIXME: this source only makes sense in neovim lua files... { name = 'buffer' }, { name = 'path' }, { name = 'emoji' }, { name = 'calc' }, }, } cmp.setup.cmdline('/', { sources = { { name = 'buffer' }, }, }) cmp.setup.cmdline(':', { sources = cmp.config.sources { { { name = 'path' } }, { { name = 'cmdline' } }, }, }) local keybinds_in_lsp_context = { 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' }, }, } local integrate_into_neovim = function(client, buffer_number) print('LSP ' .. client.name .. ' [' .. client.id .. '] ' .. 'started.') -- 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(keybinds_in_lsp_context, { 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 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 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 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, } -- overwrite default action handlers with cuter ones vim.lsp.handlers['textDocument/codeAction'] = require('lsputil.codeAction').code_action_handler vim.lsp.handlers['textDocument/references'] = require('lsputil.locations').references_handler vim.lsp.handlers['textDocument/definition'] = require('lsputil.locations').definition_handler vim.lsp.handlers['textDocument/declaration'] = require('lsputil.locations').declaration_handler vim.lsp.handlers['textDocument/typeDefinition'] = require('lsputil.locations').typeDefinition_handler vim.lsp.handlers['textDocument/implementation'] = require('lsputil.locations').implementation_handler vim.lsp.handlers['textDocument/documentSymbol'] = require('lsputil.symbols').document_handler vim.lsp.handlers['workspace/symbol'] = require('lsputil.symbols').workspace_handler end