From df3aa867137227bda9e44beab82a63443d700f18 Mon Sep 17 00:00:00 2001 From: Matt Sturgeon Date: Sat, 1 Jun 2024 16:17:23 +0100 Subject: [PATCH] lib/keymap-helpers: refactor `mkMapOptionSubmodule` Make the `key` and `action` options optional, and allow configuring whether `action` can be a raw type. --- lib/keymap-helpers.nix | 79 ++++++++++++++++++++++++------------------ plugins/lsp/wtf.nix | 9 ++++- 2 files changed, 53 insertions(+), 35 deletions(-) diff --git a/lib/keymap-helpers.nix b/lib/keymap-helpers.nix index 2f6571ec..2b316600 100644 --- a/lib/keymap-helpers.nix +++ b/lib/keymap-helpers.nix @@ -60,7 +60,11 @@ rec { # ["" "n" "v" ...] (map ({ short, ... }: short) (attrValues modes)); - mapOptionSubmodule = mkMapOptionSubmodule { }; + mapOptionSubmodule = mkMapOptionSubmodule { + hasKey = true; + hasAction = true; + rawAction = true; + }; mkModeOption = default: @@ -79,48 +83,55 @@ rec { }; mkMapOptionSubmodule = - defaults: + { + defaults ? { }, + hasKey ? false, + hasAction ? false, + rawAction ? true, + }: # TODO remove assert once `lua` option is gone # This is here to ensure no uses of `mkMapOptionSubmodule` set a `lua` default assert !(defaults ? lua); ( with types; submodule { - options = { - key = mkOption ( - { - type = str; - description = "The key to map."; - example = ""; - } - // (optionalAttrs (defaults ? key) { default = defaults.key; }) - ); + options = + (optionalAttrs hasKey { + key = mkOption ( + { + type = str; + description = "The key to map."; + example = ""; + } + // (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 ( - { - type = nixvimTypes.maybeRaw str; - description = "The action to execute."; - } - // (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; + 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; - }; } ); diff --git a/plugins/lsp/wtf.nix b/plugins/lsp/wtf.nix index d89acc34..290835b6 100644 --- a/plugins/lsp/wtf.nix +++ b/plugins/lsp/wtf.nix @@ -36,7 +36,14 @@ in keymaps = mapAttrs ( action: defaults: 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." ) defaultKeymaps;