minor change

This commit is contained in:
Solomon Laing 2024-02-25 23:20:50 +10:30
parent 3c73fa2fc8
commit 8b302b42af

View File

@ -1,73 +1,73 @@
local M = {} local M = {}
function M.on_attach(client, buffer) function M.on_attach(client, buffer)
local self = M.new(client, buffer) local self = M.new(client, buffer)
self:map("<leader>ld", vim.diagnostic.open_float, { desc = "Line Diagnostics" }) self:map("<leader>ld", vim.diagnostic.open_float, { desc = "Line Diagnostics" })
self:map("<leader>lI", "LspInfo", { desc = "Lsp Info" }) self:map("<leader>lI", "LspInfo", { desc = "Lsp Info" })
self:map("<leader>ls", "Telescope diagnostics", { desc = "Telescope Diagnostics" }) self:map("<leader>ls", "Telescope diagnostics", { desc = "Telescope Diagnostics" })
self:map("gd", "Telescope lsp_definitions", { desc = "Goto Definition" }) self:map("gd", "Telescope lsp_definitions", { desc = "Goto Definition" })
self:map("gr", "Telescope lsp_references", { desc = "References" }) self:map("gr", "Telescope lsp_references", { desc = "References" })
self:map("gD", "Telescope lsp_declarations", { desc = "Goto Declaration" }) self:map("gD", "Telescope lsp_declarations", { desc = "Goto Declaration" })
self:map("gI", "Telescope lsp_implementations", { desc = "Goto Implementation" }) self:map("gI", "Telescope lsp_implementations", { desc = "Goto Implementation" })
self:map("gt", "Telescope lsp_type_definitions", { desc = "Goto Type Definition" }) self:map("gt", "Telescope lsp_type_definitions", { desc = "Goto Type Definition" })
self:map("K", vim.lsp.buf.hover, { desc = "Hover" }) self:map("K", vim.lsp.buf.hover, { desc = "Hover" })
self:map("gK", vim.lsp.buf.signature_help, { desc = "Signature Help", has = "signatureHelp" }) self:map("gK", vim.lsp.buf.signature_help, { desc = "Signature Help", has = "signatureHelp" })
self:map("[d", M.diagnostic_goto(true), { desc = "Next Diagnostic" }) self:map("[d", M.diagnostic_goto(true), { desc = "Next Diagnostic" })
self:map("]d", M.diagnostic_goto(false), { desc = "Prev Diagnostic" }) self:map("]d", M.diagnostic_goto(false), { desc = "Prev Diagnostic" })
self:map("]e", M.diagnostic_goto(true, "ERROR"), { desc = "Next Error" }) self:map("]e", M.diagnostic_goto(true, "ERROR"), { desc = "Next Error" })
self:map("[e", M.diagnostic_goto(false, "ERROR"), { desc = "Prev Error" }) self:map("[e", M.diagnostic_goto(false, "ERROR"), { desc = "Prev Error" })
self:map("]w", M.diagnostic_goto(true, "WARNING"), { desc = "Next Warning" }) self:map("]w", M.diagnostic_goto(true, "WARNING"), { desc = "Next Warning" })
self:map("[w", M.diagnostic_goto(false, "WARNING"), { desc = "Prev Warning" }) self:map("[w", M.diagnostic_goto(false, "WARNING"), { desc = "Prev Warning" })
self:map("<leader>la", vim.lsp.buf.code_action, { desc = "Code Action", mode = { "n", "v" }, has = "codeAction" }) self:map("<leader>la", vim.lsp.buf.code_action, { desc = "Code Action", mode = { "n", "v" }, has = "codeAction" })
local format = require("lazyvim.plugins.lsp.format").format local format = require("lazyvim.plugins.lsp.format").format
self:map("<leader>lf", format, { desc = "Format Document", has = "documentFormatting" }) self:map("<leader>lf", format, { desc = "Format Document", has = "documentFormatting" })
self:map("<leader>lF", format, { desc = "Format Range", mode = "v", has = "documentRangeFormatting" }) self:map("<leader>lF", format, { desc = "Format Range", mode = "v", has = "documentRangeFormatting" })
self:map("<leader>lr", M.rename, { expr = true, desc = "Rename", has = "rename" }) self:map("<leader>lr", M.rename, { expr = true, desc = "Rename", has = "rename" })
if client.name == "tsserver" and pcall(require, "typescript") then if client.name == "tsserver" and pcall(require, "typescript") then
self:map("<leader>lo", "TypescriptOrganizeImports", { desc = "Organize Imports" }) self:map("<leader>lo", "TypescriptOrganizeImports", { desc = "Organize Imports" })
self:map("<leader>lR", "TypescriptRenameFile", { desc = "Rename File" }) self:map("<leader>lR", "TypescriptRenameFile", { desc = "Rename File" })
end end
end end
function M.new(client, buffer) function M.new(client, buffer)
return setmetatable({ client = client, buffer = buffer }, { __index = M }) return setmetatable({ client = client, buffer = buffer }, { __index = M })
end end
function M:has(cap) function M:has(cap)
return self.client.server_capabilities[cap .. "Provider"] return self.client.server_capabilities[cap .. "Provider"]
end end
function M:map(lhs, rhs, opts) function M:map(lhs, rhs, opts)
opts = opts or {} opts = opts or {}
if opts.has and not self:has(opts.has) then if opts.has and not self:has(opts.has) then
return return
end end
vim.keymap.set( vim.keymap.set(
opts.mode or "n", opts.mode or "n",
lhs, lhs,
type(rhs) == "string" and ("<cmd>%s<cr>"):format(rhs) or rhs, type(rhs) == "string" and ("<cmd>%s<cr>"):format(rhs) or rhs,
---@diagnostic disable-next-line: no-unknown ---@diagnostic disable-next-line: no-unknown
{ silent = true, buffer = self.buffer, expr = opts.expr, desc = opts.desc } { silent = true, buffer = self.buffer, expr = opts.expr, desc = opts.desc }
) )
end end
function M.rename() function M.rename()
if pcall(require, "inc_rename") then if pcall(require, "inc_rename") then
return ":IncRename " .. vim.fn.expand("<cword>") return ":IncRename " .. vim.fn.expand("<cword>")
else else
vim.lsp.buf.rename() vim.lsp.buf.rename()
end end
end end
function M.diagnostic_goto(next, severity) function M.diagnostic_goto(next, severity)
local go = next and vim.diagnostic.goto_next or vim.diagnostic.goto_prev local go = next and vim.diagnostic.goto_next or vim.diagnostic.goto_prev
severity = severity and vim.diagnostic.severity[severity] or nil severity = severity and vim.diagnostic.severity[severity] or nil
return function() return function()
go({ severity = severity }) go({ severity = severity })
end end
end end
return M return M