-- luacheck: globals packer_plugins local M = {} local utils = require("utils") function M.config_lsp_ui() local border = { { "┌", "FloatBorder" }, { "─", "FloatBorder" }, { "┐", "FloatBorder" }, { "│", "FloatBorder" }, { "┘", "FloatBorder" }, { "─", "FloatBorder" }, { "└", "FloatBorder" }, { "│", "FloatBorder" }, } local orig_util_open_floating_preview = vim.lsp.util.open_floating_preview function vim.lsp.util.open_floating_preview(contents, syntax, opts, ...) opts = opts or {} opts.border = opts.border or border return orig_util_open_floating_preview(contents, syntax, opts, ...) end -- Diagnostics signs local signs = utils.diagnostic_signs for type, icon in pairs(signs) do local hl = "DiagnosticSign" .. type vim.fn.sign_define(hl, { text = icon, texthl = hl, numhl = hl }) end utils.try_require("trouble", function(trouble) trouble.setup({ fold_open = "▼", fold_closed = "▶", icons = false, use_diagnostic_signs = true, }) end) end local function get_default_attach(override_capabilities) return function(client, bufnr) -- Allow overriding capabilities to avoid duplicate lsps with capabilities if override_capabilities ~= nil then client.resolved_capabilities = vim.tbl_extend("force", client.resolved_capabilities, override_capabilities or {}) end 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 -- Set built in features to use lsp functions buf_set_option("omnifunc", "v:lua.vim.lsp.omnifunc") if client.resolved_capabilities.goto_definition then buf_set_option("tagfunc", "v:lua.vim.lsp.tagfunc") end if client.resolved_capabilities.document_formatting then buf_set_option("formatexpr", "v:lua.vim.lsp.formatexpr()") end -- Mappings local opts = { noremap = true, silent = true } local lsp_keymap = utils.keymap_group("n", "l", opts, bufnr) lsp_keymap("h", "lua vim.lsp.buf.hover()") lsp_keymap("rn", "lua vim.lsp.buf.rename()") lsp_keymap("e", "lua vim.diagnostic.open_float()") lsp_keymap("D", "lua vim.lsp.buf.declaration()") lsp_keymap("d", "lua vim.lsp.buf.definition()") lsp_keymap("t", "lua vim.lsp.buf.type_definition()") lsp_keymap("i", "lua vim.lsp.buf.implementation()") lsp_keymap("s", "lua vim.lsp.buf.signature_help()") lsp_keymap("wa", "lua vim.lsp.buf.add_workspace_folder()", opts) lsp_keymap("wr", "lua vim.lsp.buf.remove_workspace_folder()", opts) lsp_keymap("wl", "lua print(vim.inspect(vim.lsp.buf.list_workspace_folders()))", opts) lsp_keymap("r", "lua vim.lsp.buf.references()", opts) lsp_keymap("p", "lua vim.lsp.diagnostic.goto_prev()", opts) lsp_keymap("n", "lua vim.lsp.diagnostic.goto_next()", opts) -- Set insert keymap for signature help buf_set_keymap("i", "", "lua vim.lsp.buf.signature_help()", opts) -- Older keymaps 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", "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.diagnostic.open_float()", 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) -- Open diagnostic on hold if vim["diagnostic"] ~= nil then vim.cmd([[autocmd CursorHold * lua vim.diagnostic.open_float(nil, {focus=false, scope="cursor"})]]) end -- Use IncRename if available if utils.try_require("inc_rename") ~= nil then lsp_keymap("rn", "IncRename()") end -- Set some keybinds conditional on server capabilities if client.resolved_capabilities.document_formatting then buf_set_keymap("n", "lf", "lua vim.lsp.buf.formatting()", opts) vim.cmd([[ augroup lsp_format autocmd! autocmd BufWritePre *.rs,*.go,*.sh,*.lua lua vim.lsp.buf.formatting_sync(nil, 1000) " autocmd BufWritePre lua vim.lsp.buf.formatting_sync(nil, 1000) augroup END ]]) end if client.resolved_capabilities.document_range_formatting then buf_set_keymap("n", "lfr", "lua vim.lsp.buf.range_formatting()", opts) end -- Set autocommands conditional on server_capabilities if client.resolved_capabilities.document_highlight then vim.cmd([[ :highlight link LspReferenceRead MatchParen :highlight link LspReferenceText MatchParen :highlight link LspReferenceWrite MatchParen 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 utils.try_require("telescope") ~= nil then -- Replace some Telescope bindings with LSP versions if client.resolved_capabilities.goto_definition then buf_set_keymap("n", "t", "Telescope lsp_document_symbols", opts) buf_set_keymap("n", "ft", "Telescope lsp_dynamic_workspace_symbols", opts) buf_set_keymap("n", "ft", "Telescope lsp_dynamic_workspace_symbols", opts) end -- Replace some LSP bindings with Telescope ones if client.resolved_capabilities.goto_definition then lsp_keymap("d", "Telescope lsp_definitions") lsp_keymap("t", "Telescope lsp_type_definition()") end lsp_keymap("i", "Telescope lsp_implementations") lsp_keymap("r", "Telescope lsp_references") lsp_keymap("A", "Telescope lsp_code_actions") buf_set_keymap("n", "ca", "Telescope lsp_code_actions", opts) buf_set_keymap("v", "lA", "Telescope lsp_range_code_actions", opts) end end end local function merged_capabilities() -- Maybe update capabilities local capabilities = vim.lsp.protocol.make_client_capabilities() utils.try_require("cmp-nvim-lsp", function(cmp_nvim_lsp) capabilities = cmp_nvim_lsp.update_capabilities(capabilities) end) return capabilities end --[[ local function get_luadev_config() local neodev = utils.try_require("neodev") if neodev ~= nil then return neodev.setup({ -- add any options here, or leave empty to use the default settings lspconfig = { on_attach = get_default_attach(), settings = { Lua = { completion = { callSnippet = "Disable", keywordSnippet = "Disable", }, }, }, }, }) end return { settings = nil } end --]] function M.config_lsp() utils.try_require("lspconfig", function(lsp_config) local capabilities = merged_capabilities() local default_attach = get_default_attach() local default_setup = { capabilities = capabilities, on_attach = default_attach } -- Configure each server lsp_config.bashls.setup(default_setup) lsp_config.gopls.setup(default_setup) lsp_config.pyright.setup(default_setup) -- lsp_config.tsserver.setup(default_setup) lsp_config.rls.setup({ capabilities = capabilities, on_attach = default_attach, settings = { rust = { build_on_save = false, all_features = true, unstable_features = true, }, }, }) -- Configure neovim dev for when sumneko_lua is installed utils.try_require("neodev", function(neodev) neodev.setup({}) end) --[[ lsp_config.sumneko_lua.setup({ capabilities = capabilities, on_attach = default_attach, -- settings = get_luadev_config().settings, }) --]] -- Auto setup mason installed servers utils.try_require("mason-lspconfig", function(mason_lspconfig) -- Get list of servers that are installed but not set up local already_setup = lsp_config.available_servers() local needs_setup = vim.tbl_filter(function(server) return not vim.tbl_contains(already_setup, server) end, mason_lspconfig.get_installed_servers()) -- Setup each server with default config vim.tbl_map(function(server) lsp_config[server].setup(default_setup) end, needs_setup) end) -- Config null-ls after lsps so we can disable for languages that have language servers require("plugins.null-ls").configure(default_setup) end) end function M.config_lsp_saga() utils.try_require("lspsaga", function(saga) saga.init_lsp_saga({ error_sign = utils.diagnostic_signs.Error, warn_sign = utils.diagnostic_signs.Warn, hint_sign = utils.diagnostic_signs.Hint, dianostic_header_icon = " 💬 ", code_action_icon = "💡", code_action_prompt = { enable = false, sign = false, }, }) end) end function M.config_lsp_intaller() utils.try_require("mason", function(mason) mason.setup() end) utils.try_require("mason-lspconfig", function(mason_lspconfig) mason_lspconfig.setup() end) end return M