neovim-confs/lua/plex/plugins/lsp/keymaps.lua

103 lines
3.3 KiB
Lua
Raw Normal View History

2023-09-06 00:26:45 +02:00
local M = {}
2024-01-11 11:44:26 +01:00
---@type LazyKeysLspSpec[]|nil
2023-09-06 00:26:45 +02:00
M._keys = nil
2024-01-11 11:44:26 +01:00
---@alias LazyKeysLspSpec LazyKeysSpec|{has?:string}
---@alias LazyKeysLsp LazyKeys|{has?:string}
2023-09-06 00:26:45 +02:00
2024-01-11 11:44:26 +01:00
---@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", function() require("telescope.builtin").lsp_definitions({ reuse_win = true }) end, desc = "Goto Definition", has = "definition" },
{ "gr", "<cmd>Telescope lsp_references<cr>", desc = "References" },
{ "gD", vim.lsp.buf.declaration, desc = "Goto Declaration" },
{ "gI", function() require("telescope.builtin").lsp_implementations({ reuse_win = true }) end, desc = "Goto Implementation" },
{ "gy", function() require("telescope.builtin").lsp_type_definitions({ reuse_win = true }) end, desc = "Goto T[y]pe Definition" },
2024-01-11 13:20:26 +01:00
{ "<leader>cK", vim.lsp.buf.hover, desc = "Hover" },
2024-01-11 11:44:26 +01:00
{ "gK", vim.lsp.buf.signature_help, desc = "Signature Help", has = "signatureHelp" },
{ "<c-k>", vim.lsp.buf.signature_help, mode = "i", desc = "Signature Help", has = "signatureHelp" },
{ "<leader>ca", vim.lsp.buf.code_action, desc = "Code Action", mode = { "n", "v" }, has = "codeAction" },
{
"<leader>cA",
function()
vim.lsp.buf.code_action({
context = {
only = {
"source",
},
diagnostics = {},
},
})
end,
desc = "Source Action",
has = "codeAction",
}
}
if require("lazyvim.util").has("inc-rename.nvim") then
M._keys[#M._keys + 1] = {
"<leader>cr",
function()
local inc_rename = require("inc_rename")
return ":" .. inc_rename.config.cmd_name .. " " .. vim.fn.expand("<cword>")
end,
expr = true,
desc = "Rename",
has = "rename",
}
else
M._keys[#M._keys + 1] = { "<leader>cr", vim.lsp.buf.rename, desc = "Rename", has = "rename" }
end
return M._keys
2023-09-06 00:26:45 +02:00
end
---@param method string
function M.has(buffer, method)
2024-01-11 11:44:26 +01:00
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
2023-09-06 00:26:45 +02:00
end
2024-01-11 11:44:26 +01:00
---@return (LazyKeys|{has?:string})[]
2023-09-06 00:26:45 +02:00
function M.resolve(buffer)
2024-01-11 11:44:26 +01:00
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)
2023-09-06 00:26:45 +02:00
end
2024-01-11 11:44:26 +01:00
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
2023-09-06 00:26:45 +02:00
end
return M