maps: allow same key on multiple mode (#360)

* mappings: add new keymap option

* mappings: impl keymap option

* doc: update release notes

* map: fix misinformation

* map: remove redundant variable

* fixup! mappings: impl keymap option
This commit is contained in:
Ching Pei Yang 2024-10-06 11:23:01 +02:00 committed by GitHub
parent 649f5513f9
commit b637f921d5
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 120 additions and 97 deletions

View file

@ -28,10 +28,10 @@ configuration formats.
### `vim.maps` rewrite {#sec-vim-maps-rewrite} ### `vim.maps` rewrite {#sec-vim-maps-rewrite}
Instead of specifying map modes using submodules (eg.: `vim.maps.normal`), a new Instead of specifying map modes using submodules (eg.: `vim.maps.normal`), a new `vim.keymaps`
`mode` option has mode has been introduced. It can be either a string, or a list submodule with support for a `mode` option has been introduced. It can be either a string, or a
of strings, where a string represents the short-name of the map mode(s), that list of strings, where a string represents the short-name of the map mode(s), that the mapping
the mapping should be set for. See `:help map-modes` for more information. should be set for. See `:help map-modes` for more information.
For example: For example:
@ -42,10 +42,13 @@ vim.maps.normal."<leader>m" = { ... };
has to be replaced by has to be replaced by
```nix ```nix
vim.maps."<leader>m" = { vim.keymaps = [
mode = "n"; {
key = "<leader>m";
mode = "n";
}
... ...
}; ];
``` ```
### `vim.lsp.nvimCodeActionMenu` removed in favor of `vim.ui.fastaction` {#sec-nvim-code-action-menu-deprecation} ### `vim.lsp.nvimCodeActionMenu` removed in favor of `vim.ui.fastaction` {#sec-nvim-code-action-menu-deprecation}

View file

@ -3,32 +3,75 @@
lib, lib,
... ...
}: let }: let
inherit (lib.modules) mkIf; inherit (lib.modules) mkIf mkMerge;
inherit (lib.trivial) pipe;
inherit (lib.attrsets) mapAttrsToList;
inherit (lib.lists) flatten;
legacyMapModes = {
normal = ["n"];
insert = ["i"];
select = ["s"];
visual = ["v"];
terminal = ["t"];
normalVisualOp = ["n" "v" "o"];
visualOnly = ["n" "x"];
operator = ["o"];
insertCommand = ["i" "c"];
lang = ["l"];
command = ["c"];
};
cfg = config.vim; cfg = config.vim;
in { in {
config = { config = {
vim.maps = mkIf cfg.disableArrows { vim.keymaps = mkMerge [
"<up>" = { (
mode = ["n" "i"]; mkIf cfg.disableArrows [
action = "<nop>"; {
noremap = false; key = "<up>";
}; mode = ["n" "i"];
"<down>" = { action = "<nop>";
mode = ["n" "i"]; noremap = false;
action = "<nop>"; }
noremap = false; {
}; key = "<down>";
"<left>" = { mode = ["n" "i"];
mode = ["n" "i"]; action = "<nop>";
action = "<nop>"; noremap = false;
noremap = false; }
}; {
"<right>" = { key = "<left>";
mode = ["n" "i"]; mode = ["n" "i"];
action = "<nop>"; action = "<nop>";
noremap = false; noremap = false;
}; }
}; {
key = "<right>";
mode = ["n" "i"];
action = "<nop>";
noremap = false;
}
]
)
(
pipe cfg.maps
[
(mapAttrsToList (
oldMode: keybinds:
mapAttrsToList (
key: bind:
bind
// {
inherit key;
mode = legacyMapModes.${oldMode};
}
)
keybinds
))
flatten
]
)
];
}; };
} }

View file

@ -1,6 +1,6 @@
{lib, ...}: let {lib, ...}: let
inherit (lib.options) mkOption; inherit (lib.options) mkOption;
inherit (lib.types) either str listOf attrsOf nullOr submodule bool; inherit (lib.types) either str listOf attrsOf nullOr submodule;
inherit (lib.nvim.config) mkBool; inherit (lib.nvim.config) mkBool;
mapConfigOptions = { mapConfigOptions = {
@ -31,6 +31,10 @@
options = options =
mapConfigOptions mapConfigOptions
// { // {
key = mkOption {
type = str;
description = "The key that triggers this keybind.";
};
mode = mkOption { mode = mkOption {
type = either str (listOf str); type = either str (listOf str);
description = '' description = ''
@ -38,52 +42,56 @@
See `:help map-modes` for a list of modes. See `:help map-modes` for a list of modes.
''; '';
example = ''`["n" "v" "c"]` for normal, visual and command mode'';
}; };
}; };
}; };
# legacy stuff legacyMapOption = mode:
mapOption = submodule {
options = mapConfigOptions;
};
mapOptions = mode:
mkOption { mkOption {
description = "Mappings for ${mode} mode"; description = "Mappings for ${mode} mode";
type = attrsOf mapOption; type = attrsOf (submodule {
options = mapConfigOptions;
});
default = {}; default = {};
}; };
in { in {
options.vim = { options.vim = {
maps = mkOption { keymaps = mkOption {
type = submodule { type = listOf mapType;
freeformType = attrsOf mapType;
options = {
normal = mapOptions "normal";
insert = mapOptions "insert";
select = mapOptions "select";
visual = mapOptions "visual and select";
terminal = mapOptions "terminal";
normalVisualOp = mapOptions "normal, visual, select and operator-pending (same as plain 'map')";
visualOnly = mapOptions "visual only";
operator = mapOptions "operator-pending";
insertCommand = mapOptions "insert and command-line";
lang = mapOptions "insert, command-line and lang-arg";
command = mapOptions "command-line";
};
};
default = {};
description = "Custom keybindings."; description = "Custom keybindings.";
example = '' example = ''
maps = { vim.keymaps = [
"<leader>m" = { {
key = "<leader>m";
mode = "n"; mode = "n";
silent = true; silent = true;
action = "<cmd>make<CR>"; action = ":make<CR>";
}; # Same as nnoremap <leader>m <silent> <cmd>make<CR> }
}; {
key = "<leader>l";
mode = ["n" "x"];
silent = true;
action = "<cmd>cnext<CR>";
}
];
''; '';
default = {};
};
maps = {
normal = legacyMapOption "normal";
insert = legacyMapOption "insert";
select = legacyMapOption "select";
visual = legacyMapOption "visual and select";
terminal = legacyMapOption "terminal";
normalVisualOp = legacyMapOption "normal, visual, select and operator-pending (same as plain 'map')";
visualOnly = legacyMapOption "visual only";
operator = legacyMapOption "operator-pending";
insertCommand = legacyMapOption "insert and command-line";
lang = legacyMapOption "insert, command-line and lang-arg";
command = legacyMapOption "command-line";
}; };
}; };
} }

View file

@ -3,8 +3,8 @@
lib, lib,
... ...
}: let }: let
inherit (builtins) map mapAttrs filter removeAttrs attrNames; inherit (builtins) map mapAttrs filter;
inherit (lib.attrsets) mapAttrsToList filterAttrs attrsToList; inherit (lib.attrsets) mapAttrsToList filterAttrs;
inherit (lib.strings) concatLines concatMapStringsSep; inherit (lib.strings) concatLines concatMapStringsSep;
inherit (lib.trivial) showWarnings; inherit (lib.trivial) showWarnings;
inherit (lib.generators) mkLuaInline; inherit (lib.generators) mkLuaInline;
@ -41,40 +41,9 @@ in {
inherit (keymap) desc silent nowait script expr unique noremap; inherit (keymap) desc silent nowait script expr unique noremap;
}; };
toLuaKeymap = { toLuaKeymap = bind: "vim.keymap.set(${toLuaObject bind.mode}, ${toLuaObject bind.key}, ${toLuaObject (getAction bind)}, ${toLuaObject (getOpts bind)})";
name,
value,
}: "vim.keymap.set(${toLuaObject value.mode}, ${toLuaObject name}, ${toLuaObject (getAction value)}, ${toLuaObject (getOpts value)})";
namedModes = { keymaps = concatLines (map toLuaKeymap cfg.keymaps);
"normal" = ["n"];
"insert" = ["i"];
"select" = ["s"];
"visual" = ["v"];
"terminal" = ["t"];
"normalVisualOp" = ["n" "v" "o"];
"visualOnly" = ["n" "x"];
"operator" = ["o"];
"insertCommand" = ["i" "c"];
"lang" = ["l"];
"command" = ["c"];
};
maps =
removeAttrs cfg.maps (attrNames namedModes)
// mapAttrs (_: legacyMap: legacyMap // {mode = namedModes.normal;}) cfg.maps.normal
// mapAttrs (_: legacyMap: legacyMap // {mode = namedModes.insert;}) cfg.maps.insert
// mapAttrs (_: legacyMap: legacyMap // {mode = namedModes.select;}) cfg.maps.select
// mapAttrs (_: legacyMap: legacyMap // {mode = namedModes.visual;}) cfg.maps.visual
// mapAttrs (_: legacyMap: legacyMap // {mode = namedModes.terminal;}) cfg.maps.terminal
// mapAttrs (_: legacyMap: legacyMap // {mode = namedModes.normalVisualOp;}) cfg.maps.normalVisualOp
// mapAttrs (_: legacyMap: legacyMap // {mode = namedModes.visualOnly;}) cfg.maps.visualOnly
// mapAttrs (_: legacyMap: legacyMap // {mode = namedModes.operator;}) cfg.maps.operator
// mapAttrs (_: legacyMap: legacyMap // {mode = namedModes.insertCommand;}) cfg.maps.insertCommand
// mapAttrs (_: legacyMap: legacyMap // {mode = namedModes.lang;}) cfg.maps.lang
// mapAttrs (_: legacyMap: legacyMap // {mode = namedModes.command;}) cfg.maps.command;
keymaps = concatLines (map toLuaKeymap (attrsToList (filterAttrs (_: value: value != null) maps)));
in { in {
vim = { vim = {
luaConfigRC = { luaConfigRC = {