-- luacheck: globals packer_plugins local function default_attach(client, bufnr) require('completion').on_attach() local function buf_set_keymap(...) vim.api.nvim_buf_set_keymap(bufnr, ...) end local function buf_set_option(...) vim.api.nvim_buf_set_option(bufnr, ...) end buf_set_option('omnifunc', 'v:lua.vim.lsp.omnifunc') -- Mappings -- TODO: Maybe prefix all of these for easier discovery local opts = { noremap=true, silent=true } buf_set_keymap('n', 'gD', 'lua vim.lsp.buf.declaration()', opts) buf_set_keymap('n', 'gd', 'lua vim.lsp.buf.definition()', opts) buf_set_keymap('n', 'K', 'lua vim.lsp.buf.hover()', opts) buf_set_keymap('n', 'gi', 'lua vim.lsp.buf.implementation()', opts) buf_set_keymap('n', '', 'lua vim.lsp.buf.signature_help()', opts) buf_set_keymap('n', 'wa', 'lua vim.lsp.buf.add_workspace_folder()', opts) buf_set_keymap('n', 'wr', 'lua vim.lsp.buf.remove_workspace_folder()', opts) buf_set_keymap('n', 'wl', 'lua print(vim.inspect(vim.lsp.buf.list_workspace_folders()))', opts) buf_set_keymap('n', 'D', 'lua vim.lsp.buf.type_definition()', opts) buf_set_keymap('n', 'rn', 'lua vim.lsp.buf.rename()', opts) buf_set_keymap('n', 'gr', 'lua vim.lsp.buf.references()', opts) buf_set_keymap('n', 'e', 'lua vim.lsp.diagnostic.show_line_diagnostics()', opts) buf_set_keymap('n', '[d', 'lua vim.lsp.diagnostic.goto_prev()', opts) buf_set_keymap('n', ']d', 'lua vim.lsp.diagnostic.goto_next()', opts) buf_set_keymap('n', 'q', 'lua vim.lsp.diagnostic.set_loclist()', opts) -- Set some keybinds conditional on server capabilities if client.resolved_capabilities.document_formatting then buf_set_keymap("n", "f", "lua vim.lsp.buf.formatting()", opts) vim.cmd([[ augroup lsp_format autocmd! autocmd BufWritePre *.rs,*.go lua vim.lsp.buf.formatting_sync(nil, 1000) " autocmd BufWritePre lua vim.lsp.buf.formatting_sync(nil, 1000) augroup END ]]) elseif client.resolved_capabilities.document_range_formatting then buf_set_keymap("n", "f", "lua vim.lsp.buf.formatting()", opts) end -- Set autocommands conditional on server_capabilities if client.resolved_capabilities.document_highlight then vim.cmd([[ :hi LspReferenceRead cterm=bold ctermbg=red guibg=LightYellow :hi LspReferenceText cterm=bold ctermbg=red guibg=LightYellow :hi LspReferenceWrite cterm=bold ctermbg=red guibg=LightYellow augroup lsp_document_highlight autocmd! autocmd CursorHold lua vim.lsp.buf.document_highlight() autocmd CursorMoved lua vim.lsp.buf.clear_references() augroup END ]]) end -- Some override some fuzzy finder bindings to use lsp sources if packer_plugins["nvim-lspfuzzy"] then buf_set_keymap("n", "t", "lua vim.lsp.buf.document_symbol()", opts) -- buf_set_keymap("n", "ft", "lua vim.lsp.buf.workspace_symbol()", opts) elseif packer_plugins["telescope.nvim"] then buf_set_keymap("n", "t", "Telescope lsp_document_symbols", opts) buf_set_keymap("n", "ft", "Telescope lsp_dynamic_workspace_symbols", opts) end end local function config_lsp() local language_servers = { "bashls", "gopls", "pylsp", "rust_analyzer", } local lsp_config = require("lspconfig") for _, ls in ipairs(language_servers) do lsp_config[ls].setup{ on_attach=default_attach, settings={ pylsp={ -- configurationSources = {"flake8"}, configurationSources = {"black"}, formatCommand = {"black"}, }, }, } end end config_lsp()