lib/keymap-helpers: refactor mkMapOptionSubmodule

Make the `key` and `action` options optional, and allow configuring
whether `action` can be a raw type.
This commit is contained in:
Matt Sturgeon 2024-06-01 16:17:23 +01:00
parent 87d6654a9f
commit df3aa86713
No known key found for this signature in database
GPG key ID: 4F91844CED1A8299
2 changed files with 53 additions and 35 deletions

View file

@ -60,7 +60,11 @@ rec {
# ["" "n" "v" ...] # ["" "n" "v" ...]
(map ({ short, ... }: short) (attrValues modes)); (map ({ short, ... }: short) (attrValues modes));
mapOptionSubmodule = mkMapOptionSubmodule { }; mapOptionSubmodule = mkMapOptionSubmodule {
hasKey = true;
hasAction = true;
rawAction = true;
};
mkModeOption = mkModeOption =
default: default:
@ -79,48 +83,55 @@ rec {
}; };
mkMapOptionSubmodule = mkMapOptionSubmodule =
defaults: {
defaults ? { },
hasKey ? false,
hasAction ? false,
rawAction ? true,
}:
# TODO remove assert once `lua` option is gone # TODO remove assert once `lua` option is gone
# This is here to ensure no uses of `mkMapOptionSubmodule` set a `lua` default # This is here to ensure no uses of `mkMapOptionSubmodule` set a `lua` default
assert !(defaults ? lua); assert !(defaults ? lua);
( (
with types; with types;
submodule { submodule {
options = { options =
key = mkOption ( (optionalAttrs hasKey {
{ key = mkOption (
type = str; {
description = "The key to map."; type = str;
example = "<C-m>"; description = "The key to map.";
} example = "<C-m>";
// (optionalAttrs (defaults ? key) { default = defaults.key; }) }
); // (optionalAttrs (defaults ? key) { default = defaults.key; })
);
})
// (optionalAttrs hasAction {
action = mkOption (
{
type = if rawAction then nixvimTypes.maybeRaw str else str;
description = "The action to execute.";
}
// (optionalAttrs (defaults ? action) { default = defaults.action; })
);
})
// {
mode = mkModeOption defaults.mode or "";
options = mapConfigOptions;
mode = mkModeOption defaults.mode or ""; lua = mkOption {
type = nullOr bool;
description = ''
If true, `action` is considered to be lua code.
Thus, it will not be wrapped in `""`.
action = mkOption ( This option is deprecated and will be removed in 24.11.
{ You should use a "raw" action instead, e.g. `action.__raw = ""`.
type = nixvimTypes.maybeRaw str; '';
description = "The action to execute."; default = null;
} visible = false;
// (optionalAttrs (defaults ? action) { default = defaults.action; }) };
);
lua = mkOption {
type = nullOr bool;
description = ''
If true, `action` is considered to be lua code.
Thus, it will not be wrapped in `""`.
This option is deprecated and will be removed in 24.11.
You should use a "raw" action instead, e.g. `action.__raw = ""`.
'';
default = null;
visible = false;
}; };
options = mapConfigOptions;
};
} }
); );

View file

@ -36,7 +36,14 @@ in
keymaps = mapAttrs ( keymaps = mapAttrs (
action: defaults: action: defaults:
helpers.mkNullOrOption ( helpers.mkNullOrOption (
with types; either str (helpers.keymaps.mkMapOptionSubmodule defaults) with types;
either str (
helpers.keymaps.mkMapOptionSubmodule {
inherit defaults;
hasKey = true;
hasAction = true;
}
)
) "Keymap for the ${action} action." ) "Keymap for the ${action} action."
) defaultKeymaps; ) defaultKeymaps;