]> git.rmz.io Git - dotfiles.git/blob - nvim/lua/plugins/lsp/keymaps.lua
72cfa51cff8554d079a7f19a21836f285818b0e7
[dotfiles.git] / nvim / lua / plugins / lsp / keymaps.lua
1 local M = {}
2
3 ---@type LazyKeysLspSpec[]|nil
4 M._keys = nil
5
6 ---@alias LazyKeysLspSpec LazyKeysSpec|{has?:string|string[], cond?:fun():boolean}
7 ---@alias LazyKeysLsp LazyKeys|{has?:string|string[], cond?:fun():boolean}
8
9 ---@return LazyKeysLspSpec[]
10 function M.get()
11 if M._keys then
12 return M._keys
13 end
14 -- stylua: ignore
15 M._keys = {
16 { "<leader>cl", "<cmd>LspInfo<cr>", desc = "Lsp Info" },
17 { "gd", vim.lsp.buf.definition, desc = "Goto Definition", has = "definition" },
18 -- { "gr", vim.lsp.buf.references, desc = "References", nowait = true },
19 { "gI", vim.lsp.buf.implementation, desc = "Goto Implementation" },
20 { "gy", vim.lsp.buf.type_definition, desc = "Goto T[y]pe Definition" },
21 { "gD", vim.lsp.buf.declaration, desc = "Goto Declaration" },
22 { "K", function() return vim.lsp.buf.hover() end, desc = "Hover" },
23 { "gK", function() return vim.lsp.buf.signature_help() end, desc = "Signature Help", has = "signatureHelp" },
24 { "<c-k>", function() return vim.lsp.buf.signature_help() end, mode = "i", desc = "Signature Help", has = "signatureHelp" },
25 { "<leader>ca", vim.lsp.buf.code_action, desc = "Code Action", mode = { "n", "v" }, has = "codeAction" },
26 { "<leader>cc", vim.lsp.codelens.run, desc = "Run Codelens", mode = { "n", "v" }, has = "codeLens" },
27 { "<leader>cC", vim.lsp.codelens.refresh, desc = "Refresh & Display Codelens", mode = { "n" }, has = "codeLens" },
28 { "<leader>cR", function() Snacks.rename.rename_file() end, desc = "Rename File", mode ={"n"}, has = { "workspace/didRenameFiles", "workspace/willRenameFiles" } },
29 { "<leader>cr", vim.lsp.buf.rename, desc = "Rename", has = "rename" },
30 { "<leader>cA", LazyVim.lsp.action.source, desc = "Source Action", has = "codeAction" },
31 { "]]", function() Snacks.words.jump(vim.v.count1) end, has = "documentHighlight",
32 desc = "Next Reference", cond = function() return Snacks.words.is_enabled() end },
33 { "[[", function() Snacks.words.jump(-vim.v.count1) end, has = "documentHighlight",
34 desc = "Prev Reference", cond = function() return Snacks.words.is_enabled() end },
35 { "<a-n>", function() Snacks.words.jump(vim.v.count1, true) end, has = "documentHighlight",
36 desc = "Next Reference", cond = function() return Snacks.words.is_enabled() end },
37 { "<a-p>", function() Snacks.words.jump(-vim.v.count1, true) end, has = "documentHighlight",
38 desc = "Prev Reference", cond = function() return Snacks.words.is_enabled() end },
39 }
40
41 return M._keys
42 end
43
44 ---@param method string|string[]
45 function M.has(buffer, method)
46 if type(method) == "table" then
47 for _, m in ipairs(method) do
48 if M.has(buffer, m) then
49 return true
50 end
51 end
52 return false
53 end
54 method = method:find("/") and method or "textDocument/" .. method
55 local clients = LazyVim.lsp.get_clients({ bufnr = buffer })
56 for _, client in ipairs(clients) do
57 if client.supports_method(method) then
58 return true
59 end
60 end
61 return false
62 end
63
64 ---@return LazyKeysLsp[]
65 function M.resolve(buffer)
66 local Keys = require("lazy.core.handler.keys")
67 if not Keys.resolve then
68 return {}
69 end
70 local spec = vim.tbl_extend("force", {}, M.get())
71 local opts = LazyVim.opts("nvim-lspconfig")
72 local clients = LazyVim.lsp.get_clients({ bufnr = buffer })
73 for _, client in ipairs(clients) do
74 local maps = opts.servers[client.name] and opts.servers[client.name].keys or {}
75 vim.list_extend(spec, maps)
76 end
77 return Keys.resolve(spec)
78 end
79
80 function M.on_attach(_, buffer)
81 local Keys = require("lazy.core.handler.keys")
82 local keymaps = M.resolve(buffer)
83
84 for _, keys in pairs(keymaps) do
85 local has = not keys.has or M.has(buffer, keys.has)
86 local cond = not (keys.cond == false or ((type(keys.cond) == "function") and not keys.cond()))
87
88 if has and cond then
89 local opts = Keys.opts(keys)
90 opts.cond = nil
91 opts.has = nil
92 opts.silent = opts.silent ~= false
93 opts.buffer = buffer
94 vim.keymap.set(keys.mode or "n", keys.lhs, keys.rhs, opts)
95 end
96 end
97 end
98
99 return M