
## Description
I found an issue where if I'm editing files of different types, say for
example `.cs` and `.py` files, they will naturally load the
corresponding LSPs for each language. However, if one of those LSPs has
keys defined in their `server` config section, then those key maps will
leak into the other, so in this case, the `gd` (go to definition)
mapping intended for `.cs` buffers is now present in `.py` buffers,
causing it not to work. This is currently the case with the `omnisharp`
LSP, as it defines a `gd` key map, see:
63150fa4c5/lua/lazyvim/plugins/extras/lang/omnisharp.lua (L53-L61)
The fix here is to shallow clone the "global" LSP keymaps before adding
the LSP server-specific keymaps so the LSP keymaps aren't added to the
global ones.
## Related Issue(s)
## Screenshots
## Checklist
- [x] I've read the
[CONTRIBUTING](https://github.com/LazyVim/LazyVim/blob/main/CONTRIBUTING.md)
guidelines.
100 lines
4.0 KiB
Lua
100 lines
4.0 KiB
Lua
local M = {}
|
|
|
|
---@type LazyKeysLspSpec[]|nil
|
|
M._keys = nil
|
|
|
|
---@alias LazyKeysLspSpec LazyKeysSpec|{has?:string|string[], cond?:fun():boolean}
|
|
---@alias LazyKeysLsp LazyKeys|{has?:string|string[], cond?:fun():boolean}
|
|
|
|
---@return LazyKeysLspSpec[]
|
|
function M.get()
|
|
if M._keys then
|
|
return M._keys
|
|
end
|
|
-- stylua: ignore
|
|
M._keys = {
|
|
{ "<leader>cl", "<cmd>LspInfo<cr>", desc = "Lsp Info" },
|
|
{ "gd", vim.lsp.buf.definition, desc = "Goto Definition", has = "definition" },
|
|
{ "gr", vim.lsp.buf.references, desc = "References", nowait = true },
|
|
{ "gI", vim.lsp.buf.implementation, desc = "Goto Implementation" },
|
|
{ "gy", vim.lsp.buf.type_definition, desc = "Goto T[y]pe Definition" },
|
|
{ "gD", vim.lsp.buf.declaration, desc = "Goto Declaration" },
|
|
{ "K", function() return vim.lsp.buf.hover() end, desc = "Hover" },
|
|
{ "gK", function() return vim.lsp.buf.signature_help() end, desc = "Signature Help", has = "signatureHelp" },
|
|
{ "<c-k>", function() return vim.lsp.buf.signature_help() end, mode = "i", desc = "Signature Help", has = "signatureHelp" },
|
|
{ "<leader>ca", vim.lsp.buf.code_action, desc = "Code Action", mode = { "n", "v" }, has = "codeAction" },
|
|
{ "<leader>cc", vim.lsp.codelens.run, desc = "Run Codelens", mode = { "n", "v" }, has = "codeLens" },
|
|
{ "<leader>cC", vim.lsp.codelens.refresh, desc = "Refresh & Display Codelens", mode = { "n" }, has = "codeLens" },
|
|
{ "<leader>cR", function() Snacks.rename.rename_file() end, desc = "Rename File", mode ={"n"}, has = { "workspace/didRenameFiles", "workspace/willRenameFiles" } },
|
|
{ "<leader>cr", vim.lsp.buf.rename, desc = "Rename", has = "rename" },
|
|
{ "<leader>cA", LazyVim.lsp.action.source, desc = "Source Action", has = "codeAction" },
|
|
{ "]]", function() Snacks.words.jump(vim.v.count1) end, has = "documentHighlight",
|
|
desc = "Next Reference", cond = function() return Snacks.words.is_enabled() end },
|
|
{ "[[", function() Snacks.words.jump(-vim.v.count1) end, has = "documentHighlight",
|
|
desc = "Prev Reference", cond = function() return Snacks.words.is_enabled() end },
|
|
{ "<a-n>", function() Snacks.words.jump(vim.v.count1, true) end, has = "documentHighlight",
|
|
desc = "Next Reference", cond = function() return Snacks.words.is_enabled() end },
|
|
{ "<a-p>", function() Snacks.words.jump(-vim.v.count1, true) end, has = "documentHighlight",
|
|
desc = "Prev Reference", cond = function() return Snacks.words.is_enabled() end },
|
|
}
|
|
|
|
return M._keys
|
|
end
|
|
|
|
---@param method string|string[]
|
|
function M.has(buffer, method)
|
|
if type(method) == "table" then
|
|
for _, m in ipairs(method) do
|
|
if M.has(buffer, m) then
|
|
return true
|
|
end
|
|
end
|
|
return false
|
|
end
|
|
method = method:find("/") and method or "textDocument/" .. method
|
|
local clients = LazyVim.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 LazyKeysLsp[]
|
|
function M.resolve(buffer)
|
|
local Keys = require("lazy.core.handler.keys")
|
|
if not Keys.resolve then
|
|
return {}
|
|
end
|
|
local spec = vim.tbl_extend("force", {}, M.get())
|
|
local opts = LazyVim.opts("nvim-lspconfig")
|
|
local clients = LazyVim.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
|
|
local has = not keys.has or M.has(buffer, keys.has)
|
|
local cond = not (keys.cond == false or ((type(keys.cond) == "function") and not keys.cond()))
|
|
|
|
if has and cond then
|
|
local opts = Keys.opts(keys)
|
|
opts.cond = nil
|
|
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
|
|
|
|
return M
|