From 5fe7b7ad4d6fa699ca9cc306a788d6485fc0ba8d Mon Sep 17 00:00:00 2001 From: Subho Banerjee Date: Tue, 5 Oct 2021 14:29:58 -0500 Subject: feat(lsp): make lsp buffer-mappings configurable (#1687) --- lua/lsp/config.lua | 17 +++++++++++++++++ lua/lsp/init.lua | 40 ++++++++++++++++++++++------------------ 2 files changed, 39 insertions(+), 18 deletions(-) (limited to 'lua') diff --git a/lua/lsp/config.lua b/lua/lsp/config.lua index 521ab50a..32185b56 100644 --- a/lua/lsp/config.lua +++ b/lua/lsp/config.lua @@ -21,6 +21,23 @@ return { on_attach_callback = nil, on_init_callback = nil, automatic_servers_installation = true, + buffer_mappings = { + normal_mode = { + ["K"] = { "lua vim.lsp.buf.hover()", "Show hover" }, + ["gd"] = { "lua vim.lsp.buf.definition()", "Goto Definition" }, + ["gD"] = { "lua vim.lsp.buf.declaration()", "Goto declaration" }, + ["gr"] = { "lua vim.lsp.buf.references()", "Goto references" }, + ["gI"] = { "lua vim.lsp.buf.implementation()", "Goto Implementation" }, + ["gs"] = { "lua vim.lsp.buf.signature_help()", "show signature help" }, + ["gp"] = { "lua require'lsp.peek'.Peek('definition')", "Peek definition" }, + ["gl"] = { + "lua require'lsp.handlers'.show_line_diagnostics()", + "Show line diagnostics", + }, + }, + insert_mode = {}, + visual_mode = {}, + }, null_ls = { setup = {}, }, diff --git a/lua/lsp/init.lua b/lua/lsp/init.lua index fae7ca8d..284b9dfe 100644 --- a/lua/lsp/init.lua +++ b/lua/lsp/init.lua @@ -22,25 +22,29 @@ local function lsp_highlight_document(client) end local function add_lsp_buffer_keybindings(bufnr) - local status_ok, wk = pcall(require, "which-key") - if not status_ok then - return - end - - local keys = { - ["K"] = { "lua vim.lsp.buf.hover()", "Show hover" }, - ["gd"] = { "lua vim.lsp.buf.definition()", "Goto Definition" }, - ["gD"] = { "lua vim.lsp.buf.declaration()", "Goto declaration" }, - ["gr"] = { "lua vim.lsp.buf.references()", "Goto references" }, - ["gI"] = { "lua vim.lsp.buf.implementation()", "Goto Implementation" }, - ["gs"] = { "lua vim.lsp.buf.signature_help()", "show signature help" }, - ["gp"] = { "lua require'lsp.peek'.Peek('definition')", "Peek definition" }, - ["gl"] = { - "lua require'lsp.handlers'.show_line_diagnostics()", - "Show line diagnostics", - }, + local mappings = { + normal_mode = "n", + insert_mode = "i", + visual_mode = "v", } - wk.register(keys, { mode = "n", buffer = bufnr }) + + if lvim.builtin.which_key.active then + -- Remap using which_key + local status_ok, wk = pcall(require, "which-key") + if not status_ok then + return + end + for mode_name, mode_char in pairs(mappings) do + wk.register(lvim.lsp.buffer_mappings[mode_name], { mode = mode_char, buffer = bufnr }) + end + else + -- Remap using nvim api + for mode_name, mode_char in pairs(mappings) do + for key, remap in pairs(lvim.lsp.buffer_mappings[mode_name]) do + vim.api.nvim_buf_set_keymap(bufnr, mode_char, key, remap[1], { noremap = true, silent = true }) + end + end + end end function M.common_capabilities() -- cgit v1.2.3