local M = {} ---@type PluginLspKeys M._keys = nil ---@return (LazyKeys|{has?:string})[] function M.get() local format = function() require("lazyvim.util").format({ force = true }) end if not M._keys then ---@class PluginLspKeys M._keys = { { "cld", vim.diagnostic.open_float, desc = "Line Diagnostics" }, { "cl", "LspInfo", desc = "Lsp Info" }, { "ca", "Telescope lsp_definitions", desc = "Goto Definition", has = "definition" }, { "cs", "Telescope lsp_references", desc = "References" }, { "cA", vim.lsp.buf.declaration, desc = "Goto Declaration" }, { "cf", "Telescope lsp_implementations", desc = "Goto Implementation" }, { "cd", "Telescope lsp_type_definitions", desc = "Goto Type Definition" }, { "ce", vim.lsp.buf.hover, desc = "Hover" }, { "cw", vim.lsp.buf.signature_help, desc = "Signature Help", has = "signatureHelp" }, { "", vim.lsp.buf.signature_help, mode = "i", desc = "Signature Help", has = "signatureHelp" }, { "]d", M.diagnostic_goto(true), desc = "Next Diagnostic" }, { "[d", M.diagnostic_goto(false), desc = "Prev Diagnostic" }, { "]e", M.diagnostic_goto(true, "ERROR"), desc = "Next Error" }, { "[e", M.diagnostic_goto(false, "ERROR"), desc = "Prev Error" }, { "]w", M.diagnostic_goto(true, "WARN"), desc = "Next Warning" }, { "[w", M.diagnostic_goto(false, "WARN"), desc = "Prev Warning" }, { "", format, desc = "Format Range", mode = "v", has = "documentRangeFormatting" }, { "cr", ":IncRename ", desc = "FUCK", has = "rename" }, { "cq", function() vim.lsp.buf.code_action({ context = { only = { "quickfix", "quickfix.ltex", "source", "source.fixAll", "source.organizeImports", "", }, }, }) end, desc = "Fix", mode = { "n", "v" }, has = "codeAction", }, { "cQ", function() vim.lsp.buf.code_action({ context = { only = { "refactor", "refactor.inline", "refactor.extract", "refactor.rewrite", }, }, }) end, desc = "Refactor", mode = { "n", "v" }, has = "codeAction", }, } end return M._keys end ---@param method string function M.has(buffer, method) method = method:find("/") and method or "textDocument/" .. method local clients = require("lazyvim.util").lsp.get_clients({ bufnr = buffer }) for _, client in ipairs(clients) do if client.supports_method(method) then return true end end return false end ---@return (LazyKeys|{has?:string})[] function M.resolve(buffer) local Keys = require("lazy.core.handler.keys") if not Keys.resolve then return {} end local spec = M.get() local opts = require("lazyvim.util").opts("nvim-lspconfig") local clients = require("lazyvim.util").lsp.get_clients({ bufnr = buffer }) for _, client in ipairs(clients) do local maps = opts.servers[client.name] and opts.servers[client.name].keys or {} vim.list_extend(spec, maps) end return Keys.resolve(spec) end function M.on_attach(_, buffer) local Keys = require("lazy.core.handler.keys") local keymaps = M.resolve(buffer) for _, keys in pairs(keymaps) do if not keys.has or M.has(buffer, keys.has) then local opts = Keys.opts(keys) opts.has = nil opts.silent = opts.silent ~= false opts.buffer = buffer vim.keymap.set(keys.mode or "n", keys.lhs, keys.rhs, opts) end end end function M.diagnostic_goto(next, severity) local go = next and vim.diagnostic.goto_next or vim.diagnostic.goto_prev severity = severity and vim.diagnostic.severity[severity] or nil return function() go({ severity = severity }) end end return M