modules/keymap: add validation

This commit is contained in:
Dmitriy Pleshevskiy 2024-04-28 01:54:38 +03:00
parent c460f49dea
commit bbf171f51b
Signed by: pleshevskiy
GPG Key ID: 17041163DA10A9A2
2 changed files with 35 additions and 3 deletions

View File

@ -25,8 +25,8 @@ let
type = mode;
default = "";
description = ''
Mode | Norm | Ins | Cmd | Vis | Sel | Opr | Term | Lang |
Command +------+-----+-----+-----+-----+-----+------+------+
Mode | Norm | Ins | Cmd | Vis | Sel | Opr | Term | Lang |
Command +------+-----+-----+-----+-----+-----+------+------+
"" | yes | - | - | yes | yes | yes | - | - |
"n" | yes | - | - | - | - | - | - | - |
"!" | - | yes | yes | - | - | - | - | - |

View File

@ -1,10 +1,42 @@
{ lib, ... }:
{ config, lib, ... }:
let cfg = config.vim.keymap; in
{
options.vim.keymap = with lib; {
_validate = mkOption {
# buffer mode lhs rhs
type = with types; attrsOf (attrsOf (attrsOf (either str attrs)));
visible = false;
internal = true;
readOnly = true;
};
set = mkOption {
type = with types; listOf keymap;
default = [ ];
};
};
config = {
vim.keymap._validate = lib.mkMerge
(map
({ mode, lhs, rhs, buffer, ... }:
let
sourceModes = if builtins.isList mode then mode else [ mode ];
unwrappedModes = lib.flatten (lib.flip map sourceModes (m:
if m == "" then [ "n" "x" "s" "o" ]
else if m == "!" then [ "i" "c" ]
else if m == "v" then [ "x" "s" ]
else if m == "l" then [ "i" "c" "l" ]
else m
));
buf = if buffer == null then "" else buffer;
in
lib.foldl lib.recursiveUpdate { }
(lib.flip map unwrappedModes (m: {
"${buf}"."${m}"."${lhs}" = rhs;
}))
)
cfg.set);
};
}