From 9757d03205d88b05e46a0668b761bef40b8aabde Mon Sep 17 00:00:00 2001 From: Matt Sturgeon Date: Sun, 29 Sep 2024 20:46:59 +0100 Subject: [PATCH] lib/keymaps: remove `lua` attr in type's `merge` fn Rather than having each option remove `lua` in their `apply` function, we can do this in the submodule type's `merge` function. --- lib/keymap-helpers.nix | 118 +++++++++++---------- modules/keymaps.nix | 4 +- plugins/by-name/barbar/default.nix | 1 - plugins/by-name/tmux-navigator/default.nix | 1 - plugins/by-name/wtf/default.nix | 1 - plugins/lsp/default.nix | 1 - 6 files changed, 65 insertions(+), 61 deletions(-) diff --git a/lib/keymap-helpers.nix b/lib/keymap-helpers.nix index daf4570578..e9da0a9c05 100644 --- a/lib/keymap-helpers.nix +++ b/lib/keymap-helpers.nix @@ -61,11 +61,8 @@ rec { mapOptionSubmodule = mkMapOptionSubmodule { }; - # NOTE: options that have the deprecated `lua` sub-option must use `removeDeprecatedMapAttrs` - # to ensure `lua` isn't evaluated when (e.g.) generating lua code. - # Failure to do so will result in "option used but not defined" errors! + # NOTE: `lua` was deprecated 2024-05-26 deprecatedMapOptionSubmodule = mkMapOptionSubmodule { lua = true; }; - removeDeprecatedMapAttrs = v: builtins.removeAttrs v [ "lua" ]; mkModeOption = default: @@ -99,57 +96,68 @@ rec { extraOptions ? { }, extraModules ? [ ], }: - types.submodule ( - { config, options, ... }: - { - imports = extraModules; - - options = - (lib.optionalAttrs (isAttrs key || key) { - key = lib.mkOption ( - { - type = types.str; - description = "The key to map."; - example = ""; - } - // (optionalAttrs (isAttrs key) key) - // (optionalAttrs (defaults ? key) { default = defaults.key; }) - ); - }) - // (optionalAttrs (isAttrs action || action) { - action = lib.mkOption ( - { - type = types.maybeRaw types.str; - description = "The action to execute."; - apply = v: if options.lua.isDefined or false && config.lua then lib.nixvim.mkRaw v else v; - } - // (optionalAttrs (isAttrs action) action) - // (optionalAttrs (defaults ? action) { default = defaults.action; }) - ); - }) - // optionalAttrs (isAttrs lua || lua) { - lua = lib.mkOption ( - { - type = types.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 = ""`. - ''; - visible = false; - } - // optionalAttrs (isAttrs lua) lua - ); - } - // { - mode = mkModeOption defaults.mode or ""; - options = mapConfigOptions; - } - // extraOptions; - } - ); + let + type = types.submodule ( + { config, options, ... }: + { + imports = extraModules; + + options = + (lib.optionalAttrs (isAttrs key || key) { + key = lib.mkOption ( + { + type = types.str; + description = "The key to map."; + example = ""; + } + // (optionalAttrs (isAttrs key) key) + // (optionalAttrs (defaults ? key) { default = defaults.key; }) + ); + }) + // (optionalAttrs (isAttrs action || action) { + action = lib.mkOption ( + { + type = types.maybeRaw types.str; + description = "The action to execute."; + apply = v: if options.lua.isDefined or false && config.lua then lib.nixvim.mkRaw v else v; + } + // (optionalAttrs (isAttrs action) action) + // (optionalAttrs (defaults ? action) { default = defaults.action; }) + ); + }) + // optionalAttrs (isAttrs lua || lua) { + lua = lib.mkOption ( + { + type = types.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 = ""`. + ''; + visible = false; + } + // optionalAttrs (isAttrs lua) lua + ); + } + // { + mode = mkModeOption defaults.mode or ""; + options = mapConfigOptions; + } + // extraOptions; + } + ); + in + type + // { + # Remove deprecated attrs from the keymap + merge = + loc: defs: + builtins.removeAttrs (type.merge loc defs) [ + "lua" + ]; + }; # Correctly merge two attrs (partially) representing a mapping. mergeKeymap = diff --git a/modules/keymaps.nix b/modules/keymaps.nix index 1fe874de7b..780fab62f7 100644 --- a/modules/keymaps.nix +++ b/modules/keymaps.nix @@ -81,7 +81,7 @@ extraConfigLua = lib.mkIf (config.keymaps != [ ]) '' -- Set up keybinds {{{ do - local __nixvim_binds = ${helpers.toLuaObject (map helpers.keymaps.removeDeprecatedMapAttrs config.keymaps)} + local __nixvim_binds = ${helpers.toLuaObject config.keymaps} for i, map in ipairs(__nixvim_binds) do vim.keymap.set(map.mode, map.key, map.action, map.options) end @@ -99,7 +99,7 @@ callback = helpers.mkRaw '' function() do - local __nixvim_binds = ${helpers.toLuaObject (map helpers.keymaps.removeDeprecatedMapAttrs mappings)} + local __nixvim_binds = ${helpers.toLuaObject mappings} for i, map in ipairs(__nixvim_binds) do vim.keymap.set(map.mode, map.key, map.action, map.options) end diff --git a/plugins/by-name/barbar/default.nix b/plugins/by-name/barbar/default.nix index e22fdaafd8..ba5f66a725 100644 --- a/plugins/by-name/barbar/default.nix +++ b/plugins/by-name/barbar/default.nix @@ -206,7 +206,6 @@ lib.nixvim.neovim-plugin.mkNeovimPlugin { }; lua = true; }; - apply = v: if v == null then null else keymaps.removeDeprecatedMapAttrs v; description = "Keymap for function Buffer${funcName}"; } ) keymapsActions; diff --git a/plugins/by-name/tmux-navigator/default.nix b/plugins/by-name/tmux-navigator/default.nix index 07fbb87fdc..17f3bee5ff 100644 --- a/plugins/by-name/tmux-navigator/default.nix +++ b/plugins/by-name/tmux-navigator/default.nix @@ -180,7 +180,6 @@ helpers.vim-plugin.mkVimPlugin { lua = true; } ); - apply = map helpers.keymaps.removeDeprecatedMapAttrs; }; }; diff --git a/plugins/by-name/wtf/default.nix b/plugins/by-name/wtf/default.nix index 3a638771bc..724d48824a 100644 --- a/plugins/by-name/wtf/default.nix +++ b/plugins/by-name/wtf/default.nix @@ -61,7 +61,6 @@ in lua = true; } ); - apply = v: if v == null then null else helpers.keymaps.removeDeprecatedMapAttrs v; description = "Keymap for the ${action} action."; } ) defaultKeymaps; diff --git a/plugins/lsp/default.nix b/plugins/lsp/default.nix index eee4435f18..4eaa4ea44e 100644 --- a/plugins/lsp/default.nix +++ b/plugins/lsp/default.nix @@ -53,7 +53,6 @@ in extra = mkOption { type = with types; listOf helpers.keymaps.deprecatedMapOptionSubmodule; - apply = map helpers.keymaps.removeDeprecatedMapAttrs; description = '' Extra keymaps to register when an LSP is attached. This can be used to customise LSP behaviour, for example with "telescope" or the "Lspsaga" plugin, as seen in the examples.