diff --git a/plugins/lsp/default.nix b/plugins/lsp/default.nix index fda8d21d..681fb4ab 100644 --- a/plugins/lsp/default.nix +++ b/plugins/lsp/default.nix @@ -134,6 +134,35 @@ in { }; default = {}; }; + + extra = mkOption { + type = with types; listOf helpers.keymaps.mapOptionSubmodule; + description = "Extra keymaps to register on 'LspAttach'."; + example = [ + { + key = "lx"; + action = "LspStop"; + } + { + key = "ls"; + action = "LspStart"; + } + { + key = "lr"; + action = "LspRestart"; + } + { + key = "gd"; + action = "require('telescope.builtin').lsp_definitions()"; + lua = true; + } + { + key = "K"; + action = "Lspsaga hover_doc"; + } + ]; + default = []; + }; }; enabledServers = mkOption { @@ -231,34 +260,37 @@ in { mkIf cfg.enable { extraPlugins = [pkgs.vimPlugins.nvim-lspconfig]; - keymaps = let - mkMaps = prefix: - mapAttrsToList - ( - key: action: let - actionStr = - if isString action - then action - else action.action; - actionProps = - if isString action - then {} - else filterAttrs (n: v: n != "action") action; - in { - mode = "n"; - inherit key; - action.__raw = prefix + actionStr; + keymapsOnEvents = { + "LspAttach" = let + mkMaps = prefix: + mapAttrsToList + ( + key: action: let + actionStr = + if isString action + then action + else action.action; + actionProps = + if isString action + then {} + else filterAttrs (n: v: n != "action") action; + in { + mode = "n"; + inherit key; + action.__raw = prefix + actionStr; - options = - { - inherit (cfg.keymaps) silent; - } - // actionProps; - } - ); - in - (mkMaps "vim.diagnostic." cfg.keymaps.diagnostic) - ++ (mkMaps "vim.lsp.buf." cfg.keymaps.lspBuf); + options = + { + inherit (cfg.keymaps) silent; + } + // actionProps; + } + ); + in + (mkMaps "vim.diagnostic." cfg.keymaps.diagnostic) + ++ (mkMaps "vim.lsp.buf." cfg.keymaps.lspBuf) + ++ cfg.keymaps.extra; + }; # Enable all LSP servers extraConfigLua = '' diff --git a/tests/test-sources/plugins/lsp/_lsp.nix b/tests/test-sources/plugins/lsp/_lsp.nix index c475496c..3a84d664 100644 --- a/tests/test-sources/plugins/lsp/_lsp.nix +++ b/tests/test-sources/plugins/lsp/_lsp.nix @@ -27,6 +27,29 @@ desc = "Hover"; }; }; + + extra = [ + { + key = "li"; + action = "LspInfo"; + } + { + key = "lx"; + action = "LspStop"; + } + { + key = "ls"; + action = "LspStart"; + } + { + key = "lr"; + action = "LspRestart"; + } + { + key = "ll"; + action = "LspLog"; + } + ]; }; servers = {