lib/keymaps: refactor mkMapOptionSubmodule again

Allow passing in `key` and `action` as either bool or an attrset.

If `false`, the option is omitted. If `true` or an attrset, the option is included.
If an attrset is used, it will update the default `mkOption` args.
This is useful for overriding `type` or adding an `example`.
This commit is contained in:
Matt Sturgeon 2024-06-07 08:43:41 +01:00
parent c16533b3f7
commit 302262304e
No known key found for this signature in database
GPG key ID: 4F91844CED1A8299
3 changed files with 11 additions and 21 deletions

View file

@ -60,11 +60,7 @@ 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:
@ -85,9 +81,10 @@ rec {
mkMapOptionSubmodule = mkMapOptionSubmodule =
{ {
defaults ? { }, defaults ? { },
hasKey ? false, # key and action can be true/false to enable/disable adding the option,
hasAction ? false, # or an attrset to enable the option and add/override mkOption args.
rawAction ? true, key ? true,
action ? 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
@ -96,22 +93,24 @@ rec {
with types; with types;
submodule { submodule {
options = options =
(optionalAttrs hasKey { (optionalAttrs (isAttrs key || key) {
key = mkOption ( key = mkOption (
{ {
type = str; type = str;
description = "The key to map."; description = "The key to map.";
example = "<C-m>"; example = "<C-m>";
} }
// (optionalAttrs (isAttrs key) key)
// (optionalAttrs (defaults ? key) { default = defaults.key; }) // (optionalAttrs (defaults ? key) { default = defaults.key; })
); );
}) })
// (optionalAttrs hasAction { // (optionalAttrs (isAttrs action || action) {
action = mkOption ( action = mkOption (
{ {
type = if rawAction then nixvimTypes.maybeRaw str else str; type = nixvimTypes.maybeRaw str;
description = "The action to execute."; description = "The action to execute.";
} }
// (optionalAttrs (isAttrs action) action)
// (optionalAttrs (defaults ? action) { default = defaults.action; }) // (optionalAttrs (defaults ? action) { default = defaults.action; })
); );
}) })

View file

@ -198,8 +198,6 @@ helpers.neovim-plugin.mkNeovimPlugin config {
mode = "n"; mode = "n";
action = "<Cmd>Buffer${funcName}<CR>"; action = "<Cmd>Buffer${funcName}<CR>";
}; };
hasKey = true;
hasAction = true;
}) "Keymap for function Buffer${funcName}" }) "Keymap for function Buffer${funcName}"
) keymapsActions; ) keymapsActions;
}; };

View file

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