From 302262304e55ea596218eeecb67f43510816ff02 Mon Sep 17 00:00:00 2001 From: Matt Sturgeon Date: Fri, 7 Jun 2024 08:43:41 +0100 Subject: [PATCH] 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`. --- lib/keymap-helpers.nix | 21 ++++++++++----------- plugins/bufferlines/barbar.nix | 2 -- plugins/lsp/wtf.nix | 9 +-------- 3 files changed, 11 insertions(+), 21 deletions(-) diff --git a/lib/keymap-helpers.nix b/lib/keymap-helpers.nix index 2b316600..0bc54cd1 100644 --- a/lib/keymap-helpers.nix +++ b/lib/keymap-helpers.nix @@ -60,11 +60,7 @@ rec { # ["" "n" "v" ...] (map ({ short, ... }: short) (attrValues modes)); - mapOptionSubmodule = mkMapOptionSubmodule { - hasKey = true; - hasAction = true; - rawAction = true; - }; + mapOptionSubmodule = mkMapOptionSubmodule { }; mkModeOption = default: @@ -85,9 +81,10 @@ rec { mkMapOptionSubmodule = { defaults ? { }, - hasKey ? false, - hasAction ? false, - rawAction ? true, + # key and action can be true/false to enable/disable adding the option, + # or an attrset to enable the option and add/override mkOption args. + key ? true, + action ? true, }: # TODO remove assert once `lua` option is gone # This is here to ensure no uses of `mkMapOptionSubmodule` set a `lua` default @@ -96,22 +93,24 @@ rec { with types; submodule { options = - (optionalAttrs hasKey { + (optionalAttrs (isAttrs key || key) { key = mkOption ( { type = str; description = "The key to map."; example = ""; } + // (optionalAttrs (isAttrs key) key) // (optionalAttrs (defaults ? key) { default = defaults.key; }) ); }) - // (optionalAttrs hasAction { + // (optionalAttrs (isAttrs action || action) { action = mkOption ( { - type = if rawAction then nixvimTypes.maybeRaw str else str; + type = nixvimTypes.maybeRaw str; description = "The action to execute."; } + // (optionalAttrs (isAttrs action) action) // (optionalAttrs (defaults ? action) { default = defaults.action; }) ); }) diff --git a/plugins/bufferlines/barbar.nix b/plugins/bufferlines/barbar.nix index 2d33a6ca..6a11096a 100644 --- a/plugins/bufferlines/barbar.nix +++ b/plugins/bufferlines/barbar.nix @@ -198,8 +198,6 @@ helpers.neovim-plugin.mkNeovimPlugin config { mode = "n"; action = "Buffer${funcName}"; }; - hasKey = true; - hasAction = true; }) "Keymap for function Buffer${funcName}" ) keymapsActions; }; diff --git a/plugins/lsp/wtf.nix b/plugins/lsp/wtf.nix index 290835b6..8fe671b6 100644 --- a/plugins/lsp/wtf.nix +++ b/plugins/lsp/wtf.nix @@ -36,14 +36,7 @@ in keymaps = mapAttrs ( action: defaults: helpers.mkNullOrOption ( - with types; - either str ( - helpers.keymaps.mkMapOptionSubmodule { - inherit defaults; - hasKey = true; - hasAction = true; - } - ) + with types; either str (helpers.keymaps.mkMapOptionSubmodule { inherit defaults; }) ) "Keymap for the ${action} action." ) defaultKeymaps;