diff --git a/lua/plugins/editor.lua b/lua/plugins/editor.lua index 4ebc88c9..e10cddf1 100644 --- a/lua/plugins/editor.lua +++ b/lua/plugins/editor.lua @@ -53,10 +53,15 @@ return { key_labels = { [""] = "SPC" }, }) wk.register({ - f = { name = "+file" }, - g = { name = "+git" }, - b = { name = "+buffer" }, - }, { prefix = "" }) + ["g"] = { name = "+goto" }, + ["]"] = { name = "+next" }, + ["["] = { name = "+prev" }, + ["b"] = { name = "+buffer" }, + ["c"] = { name = "+code" }, + ["f"] = { name = "+file" }, + ["g"] = { name = "+git" }, + ["x"] = { name = "+diagnostics" }, + }) end, }, diff --git a/lua/plugins/lsp/keymaps.lua b/lua/plugins/lsp/keymaps.lua index 5645c705..e90e0266 100644 --- a/lua/plugins/lsp/keymaps.lua +++ b/lua/plugins/lsp/keymaps.lua @@ -1,70 +1,62 @@ local M = {} +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 + function M.on_attach(client, buffer) local cap = client.server_capabilities - require("which-key").register({ - buffer = buffer, - [""] = { - c = { - name = "+code", - { - cond = client.name == "tsserver", - o = { ":TypescriptOrganizeImports", "Organize Imports" }, - R = { ":TypescriptRenameFile", "Rename File" }, - }, - r = { "lua vim.lsp.buf.rename()", "Rename", cond = cap.renameProvider }, - a = { - { vim.lsp.buf.code_action, "Code Action" }, - { "lua vim.lsp.buf.code_action()", "Code Action", mode = "v" }, - }, - f = { - { - require("plugins.lsp.format").format, - "Format Document", - cond = cap.documentFormatting, - }, - { - require("plugins.lsp.format").format, - "Format Range", - cond = cap.documentRangeFormatting, - mode = "v", - }, - }, - d = { vim.diagnostic.open_float, "Line Diagnostics" }, - l = { - name = "+lsp", - i = { "LspInfo", "Lsp Info" }, - }, - }, - x = { - d = { "Telescope diagnostics", "Search Diagnostics" }, - }, - }, - g = { - name = "+goto", - d = { "Telescope lsp_definitions", "Goto Definition" }, - r = { "Telescope lsp_references", "References" }, - R = { "Trouble lsp_references", "Trouble References" }, - D = { "Telescope lsp_declarations", "Goto Declaration" }, - I = { "Telescope lsp_implementations", "Goto Implementation" }, - t = { "Telescope lsp_type_definitions", "Goto Type Definition" }, - }, - [""] = { "lua vim.lsp.buf.signature_help()", "Signature Help", mode = { "n", "i" } }, - ["K"] = { "lua vim.lsp.buf.hover()", "Hover" }, - ["[d"] = { "lua vim.diagnostic.goto_prev()", "Next Diagnostic" }, - ["]d"] = { "lua vim.diagnostic.goto_next()", "Prev Diagnostic" }, - ["[e"] = { "lua vim.diagnostic.goto_prev({severity = vim.diagnostic.severity.ERROR})", "Next Error" }, - ["]e"] = { "lua vim.diagnostic.goto_next({severity = vim.diagnostic.severity.ERROR})", "Prev Error" }, - ["[w"] = { - "lua vim.diagnostic.goto_prev({severity = vim.diagnostic.severity.WARNING})", - "Next Warning", - }, - ["]w"] = { - "lua vim.diagnostic.goto_next({severity = vim.diagnostic.severity.WARNING})", - "Prev Warning", - }, - }) + local function map(lhs, rhs, opts) + opts = opts or {} + vim.keymap.set(opts.mode or "n", lhs, rhs, { silent = true, buffer = buffer, expr = opts.expr, desc = opts.desc }) + end + + map("ca", vim.lsp.buf.code_action, { desc = "Code Action", mode = { "n", "v" } }) + map("cd", vim.diagnostic.open_float, { desc = "Line Diagnostics" }) + map("cl", "LspInfo", { desc = "Lsp Info" }) + map("xd", "Telescope diagnostics", { desc = "Telescope Diagnostics" }) + map("gd", "Telescope lsp_definitions", { desc = "Goto Definition" }) + map("gr", "Telescope lsp_references", { desc = "References" }) + map("gR", "Trouble lsp_references", { desc = "Trouble References" }) + map("gD", "Telescope lsp_declarations", { desc = "Goto Declaration" }) + map("gI", "Telescope lsp_implementations", { desc = "Goto Implementation" }) + map("gt", "Telescope lsp_type_definitions", { desc = "Goto Type Definition" }) + map("", "lua vim.lsp.buf.signature_help()", { desc = "Signature Help", mode = { "i", "n" } }) + map("K", "lua vim.lsp.buf.hover()", { desc = "Hover" }) + map("[d", M.diagnostic_goto(true), { desc = "Next Diagnostic" }) + map("]d", M.diagnostic_goto(false), { desc = "Prev Diagnostic" }) + map("]e", M.diagnostic_goto(true, "ERROR"), { desc = "Next Error" }) + map("[e", M.diagnostic_goto(false, "ERROR"), { desc = "Prev Error" }) + map("]w", M.diagnostic_goto(true, "WARNING"), { desc = "Next Warning" }) + map("[w", M.diagnostic_goto(false, "WARNING"), { desc = "Prev Warning" }) + + if cap.documentFormatting then + map("cf", require("plugins.lsp.format").format, { desc = "Format Document" }) + end + + if cap.documentRangeFormatting then + map("cf", require("plugins.lsp.format").format, { desc = "Format Range", mode = "v" }) + end + + if cap.renameProvider then + map("cr", function() + if pcall(require, "inc_rename") then + return ":IncRename " .. vim.fn.expand("") + else + vim.lsp.buf.rename() + end + end, { expr = true, desc = "Rename" }) + end + + if client.name == "tsserver" and pcall(require, "typescript") then + map("co", "TypescriptOrganizeImports", { desc = "Organize Imports" }) + map("cR", "TypescriptRenameFile", { desc = "Rename File" }) + end end return M