return { 'neovim/nvim-lspconfig', dependencies = { 'saghen/blink.cmp', 'ibhagwan/fzf-lua', { "nvimdev/lspsaga.nvim", event = "LspAttach", opts = { ui = { border = "shadow", }, outline = { layout = "float", }, }, }, }, -- example using `opts` for defining servers opts = { servers = { lua_ls = {}, clangd = { cmd = { "/opt/clangd/bin/clangd", --"clangd"), "--compile-commands-dir=build", "--all-scopes-completion", "--background-index", "--clang-tidy", "--completion-style=bundled", "--experimental-modules-support", "--function-arg-placeholders=1", "--header-insertion=iwyu", "--header-insertion-decorators", "--malloc-trim", "--pch-storage=memory" } } } }, config = function(_, opts) local keymap = vim.keymap local autocmd = vim.api.nvim_create_autocmd -- create autocmd local utils = require("utils") local map = utils.map local augroup = utils.augroup vim.api.nvim_create_autocmd("LspAttach", { group = vim.api.nvim_create_augroup("UserLspConfig", {}), callback = function(ev) -- Buffer local mappings. -- See `:help vim.lsp.*` for documentation on any of the below functions local opts = { buffer = ev.buf, silent = true } local bufnr = ev.buf local client = vim.lsp.get_client_by_id(ev.data.client_id) if client.server_capabilities.inlayHintProvider then vim.lsp.inlay_hint.enable(true) end if client.server_capabilities.hoverProvider then map("n", "K", function() vim.lsp.buf.hover() end, opts, "get hover info") end if client.server_capabilities.semanticTokensProvider then map("n", "us", function() utils.toggle("enable_semantic_tokens", { global = true }, nil) if vim.g["enable_semantic_tokens"] then vim.lsp.semantic_tokens.start(bufnr, client.id) else vim.lsp.semantic_tokens.stop(bufnr, client.id) end end, opts, "toggle semantic token highlighting") end if client.server_capabilities.signatureHelpProvider then map( "n", "k", vim.lsp.buf.signature_help, opts, "get fn signature help" ) end if client.server_capabilities.declarationProvider then map( "n", "gD", vim.lsp.buf.declaration, opts, "goto declaration" ) end -- if client.server_capabilities.completionProvider then -- vim.bo[bufnr].omnifunc = "v:lua.vim.lsp.omnifunc" -- end if client.server_capabilities.definitionProvider then -- vim.bo[bufnr].tagfunc = "v:lua.vim.lsp.tagfunc" map("n", "gd", function() if utils.has("lspsaga.nvim") then vim.cmd([[Lspsaga goto_definition]]) else vim.lsp.buf.definition() end end, opts, "goto definition") if utils.has("lspsaga.nvim") then map("n", "pd", function() vim.cmd([[Lspsaga peek_definition]]) end, opts, "peek definition") end end if client.server_capabilities.typeDefinitionProvider then map("n", "gt", function() if utils.has("lspsaga.nvim") then vim.cmd([[Lspsaga goto_type_definition]]) else vim.lsp.buf.type_definition() end end, opts, "goto type definition") if utils.has("lspsaga.nvim") then map("n", "pt", function() vim.cmd([[Lspsaga peek_type_definition]]) end, opts, "peek type definition") end end if client.server_capabilities.implementationProvider then map("n", "gi", function() if utils.has("lspsaga.nvim") then vim.cmd([[Lspsaga finder imp+def]]) else vim.lsp.buf.implementation() end end, opts, "goto type implementation") end if client.server_capabilities.referencesProvider then map("n", "gr", function() if utils.has("lspsaga.nvim") then vim.cmd([[Lspsaga finder ref]]) else vim.lsp.buf.references({}) end end, opts, "goto type references") end if client.server_capabilities.documentHighlightProvider then vim.api.nvim_set_hl(0, "LspReferenceRead", { link = "DiffText", }) vim.api.nvim_set_hl(0, "LspReferenceText", { link = "IncSearch", }) vim.api.nvim_set_hl(0, "LspRefDiffTexterenceWrite", { link = "WildMenu", }) local doc_highlight = augroup("lsp_document_highlight") local enable_highlight = function() autocmd({ "CursorHold", "CursorHoldI" }, { group = doc_highlight, buffer = bufnr, callback = vim.lsp.buf.document_highlight, }) autocmd("CursorMoved", { group = doc_highlight, buffer = bufnr, callback = vim.lsp.buf.clear_references, }) end local disable_highlight = function() vim.lsp.buf.clear_references() vim.api.nvim_clear_autocmds({ buffer = bufnr, group = doc_highlight, }) end map("n", "uh", function() utils.toggle( "highlight", {}, { enable_highlight, disable_highlight } ) vim.b[vim.fn.bufnr()]["highlight"]() end, opts, "toggle document highlight") end if client.server_capabilities.documentSymbolProvider then map( "n", "ds", vim.lsp.buf.document_symbol, opts, "document symbols" ) end if client.server_capabilities.codeActionProvider then map({ "n", "v" }, "ca", function() if utils.has("lspsaga.nvim") then vim.cmd([[Lspsaga code_action]]) else vim.lsp.buf.code_action() end end, opts, "code action") end if client.server_capabilities.documentFormattingProvider then map("n", "uf", function() utils.toggle("autoformat", { global = true }) autocmd("BufWritePre", { group = augroup("LspFormat"), callback = function() if vim.g["autoformat"] then vim.lsp.buf.format({ async = true }) end end, }) end, opts, "toggle autoformat") map("n", "f", function() vim.lsp.buf.format({ async = true }) end, opts, "range format buffer") end if client.server_capabilities.documentRangeFormattingProvider then map("v", "f", function() vim.lsp.buf.format({ async = true }) end, opts, "range format buffer") end if client.server_capabilities.renameProvider then map("n", "rn", function() vim.lsp.buf.rename() end, opts, "rename symbol") if utils.has("lspsaga.nvim") then map("n", "rnw", function() vim.cmd([[Lspsaga project_replace]]) end, opts, "rename across workspace") end end if client.server_capabilities.callHierarchyProvider then map("n", "ci", function() if utils.has("lspsaga.nvim") then vim.cmd([[Lspsaga incoming_calls]]) else vim.lsp.buf.incoming_calls() end end, opts, "incoming calls") map("n", "co", function() if utils.has("lspsaga.nvim") then vim.cmd([[Lspsaga outgoing_calls]]) else vim.lsp.buf.outgoing_calls() end end, opts, "outgoing calls") end if client.server_capabilities.workspaceSymbolProvider then map( "n", "ws", vim.lsp.buf.workspace_symbol, opts, "list workspace symbols" ) end map("n", "[d", function() if utils.has("lspsaga.nvim") then vim.cmd([[Lspsaga diagnostic_jump_prev]]) else vim.diagnostic.jump({count = -1, float = true}) end end, opts, "goto previous diagnostics") map("n", "]d", function() if utils.has("lspsaga.nvim") then vim.cmd([[Lspsaga diagnostic_jump_next]]) else vim.diagnostic.jump({count = 1, float = true}) end end, opts, "goto next diagnostics") if utils.has("lspsaga.nvim") then map("n", "wd", function() vim.cmd([[Lspsaga show_workspace_diagnostics]]) --can use ++normal to show in loclist end, opts, "workspace diagnostics") map("n", "bd", function() vim.cmd([[Lspsaga show_buf_diagnostics]]) end, opts, "workspace diagnostics") map("n", "wo", function() vim.cmd([[Lspsaga outline]]) end, opts, "workspace outline") end map("n", "sl", vim.diagnostic.setloclist, opts, "set loclist") map("n", "of", vim.diagnostic.open_float, opts, "open float") map("n", "wf", function() vim.print(vim.lsp.buf.list_workspace_folders()) end, opts, "list workspace folders") map("n", "rd", function() print( "Language server " .. (vim.lsp.buf.server_ready() and "is ready" or "is not ready") ) end, opts, "check if lsp is ready") -- K for hover is now a built-in default in Neovim 0.12 end, }) for server, config in pairs(opts.servers) do local capabilities = vim.lsp.protocol.make_client_capabilities() capabilities.textDocument.foldingRange = { dynamicRegistration = false, lineFoldingOnly = true, } -- passing config.capabilities to blink.cmp merges with the capabilities in your -- `opts[server].capabilities, if you've defined it config.capabilities = require('blink.cmp').get_lsp_capabilities(capabilities) vim.lsp.config(server, config) vim.lsp.enable(server) end vim.api.nvim_create_user_command("LspInfo", "checkhealth vim.lsp", { desc = "Show LSP Info", }) vim.api.nvim_create_user_command("LspLog", function(_) local log_path = vim.lsp.log.get_filename() vim.cmd(string.format("edit %s", log_path)) end, { desc = "Show LSP log", }) vim.api.nvim_create_user_command("LspRestart", "lsp restart", { desc = "Restart LSP", }) --- show LSP progress (works on Ghostty) vim.api.nvim_create_autocmd("LspProgress", { callback = function(ev) local value = ev.data.params.value vim.api.nvim_echo({ { value.message or "done" } }, false, { id = "lsp." .. ev.data.client_id, kind = "progress", source = "vim.lsp", title = value.title, status = value.kind ~= "end" and "running" or "success", percent = value.percentage, }) end, }) end }