-- Shared LSP utilities local M = {} -- Set up buffer-local keymaps when LSP attaches function M.on_attach(client, bufnr) local opts = { buffer = bufnr, silent = true } -- Navigation vim.keymap.set("n", "gd", vim.lsp.buf.definition, vim.tbl_extend("force", opts, { desc = "Go to definition" })) vim.keymap.set("n", "gD", vim.lsp.buf.declaration, vim.tbl_extend("force", opts, { desc = "Go to declaration" })) vim.keymap.set("n", "", vim.lsp.buf.definition, vim.tbl_extend("force", opts, { desc = "Go to definition" })) vim.keymap.set("n", "gi", vim.lsp.buf.implementation, vim.tbl_extend("force", opts, { desc = "Go to implementation" })) vim.keymap.set("n", "gr", vim.lsp.buf.references, vim.tbl_extend("force", opts, { desc = "Go to references" })) -- Documentation vim.keymap.set("n", "K", vim.lsp.buf.hover, vim.tbl_extend("force", opts, { desc = "Hover documentation" })) -- Refactoring vim.keymap.set("n", "rn", vim.lsp.buf.rename, vim.tbl_extend("force", opts, { desc = "Rename symbol" })) vim.keymap.set({ "n", "v" }, "ca", vim.lsp.buf.code_action, vim.tbl_extend("force", opts, { desc = "Code action" })) -- Formatting vim.keymap.set("n", "f", function() vim.lsp.buf.format({ async = true }) end, vim.tbl_extend("force", opts, { desc = "Format buffer" })) end -- Return default capabilities for LSP servers function M.capabilities() local capabilities = vim.lsp.protocol.make_client_capabilities() -- If nvim-cmp is available, extend capabilities local ok, cmp_nvim_lsp = pcall(require, "cmp_nvim_lsp") if ok then capabilities = vim.tbl_deep_extend("force", capabilities, cmp_nvim_lsp.default_capabilities()) end return capabilities end return M