From 34b462a744fe4705bde099948c9b27189792944e Mon Sep 17 00:00:00 2001 From: Pei Yang Ching <59727193+horriblename@users.noreply.github.com> Date: Fri, 23 Aug 2024 14:16:07 +0200 Subject: [PATCH] keymaps: use listOf mapOption instead --- modules/neovim/mappings/config.nix | 65 +++++++++++++++++++---------- modules/neovim/mappings/options.nix | 14 ++++--- modules/wrapper/rc/config.nix | 7 ++-- 3 files changed, 54 insertions(+), 32 deletions(-) diff --git a/modules/neovim/mappings/config.nix b/modules/neovim/mappings/config.nix index a620e37..4d7f241 100644 --- a/modules/neovim/mappings/config.nix +++ b/modules/neovim/mappings/config.nix @@ -4,55 +4,74 @@ ... }: let inherit (lib.modules) mkIf mkMerge; - inherit (builtins) mapAttrs; + inherit (lib.trivial) pipe; + inherit (lib.attrsets) mapAttrsToList; + inherit (lib.lists) flatten; - processLegacyMap = modes: legacyMap: [(legacyMap // {mode = modes;})]; + legacyMapModes = { + normal = ["n"]; + insert = ["i"]; + select = ["s"]; + visual = ["v"]; + terminal = ["t"]; + normalVisualOp = ["n" "v" "o"]; + visualOnly = ["n" "x"]; + operator = ["o"]; + insertCommand = ["i" "c"]; + lang = ["l"]; + command = ["c"]; + }; cfg = config.vim; in { config = { vim.keymaps = mkMerge [ - (mkIf cfg.disableArrows { - "" = [ + ( + mkIf cfg.disableArrows [ { + key = ""; mode = ["n" "i"]; action = ""; noremap = false; } - ]; - "" = [ { + key = ""; mode = ["n" "i"]; action = ""; noremap = false; } - ]; - "" = [ { + key = ""; mode = ["n" "i"]; action = ""; noremap = false; } - ]; - "" = [ { + key = ""; mode = ["n" "i"]; action = ""; noremap = false; } - ]; - }) - (mapAttrs (_key: processLegacyMap ["n"]) cfg.maps.normal) - (mapAttrs (_key: processLegacyMap ["i"]) cfg.maps.insert) - (mapAttrs (_key: processLegacyMap ["s"]) cfg.maps.select) - (mapAttrs (_key: processLegacyMap ["v"]) cfg.maps.visual) - (mapAttrs (_key: processLegacyMap ["t"]) cfg.maps.terminal) - (mapAttrs (_key: processLegacyMap ["n" "v" "o"]) cfg.maps.normalVisualOp) - (mapAttrs (_key: processLegacyMap ["n" "x"]) cfg.maps.visualOnly) - (mapAttrs (_key: processLegacyMap ["o"]) cfg.maps.operator) - (mapAttrs (_key: processLegacyMap ["i" "c"]) cfg.maps.insertCommand) - (mapAttrs (_key: processLegacyMap ["l"]) cfg.maps.lang) - (mapAttrs (_key: processLegacyMap ["c"]) cfg.maps.command) + ] + ) + ( + pipe cfg.maps + [ + (mapAttrsToList ( + oldMode: keybinds: + mapAttrsToList ( + key: bind: + bind + // { + inherit key; + mode = legacyMapModes.${oldMode}; + } + ) + keybinds + )) + flatten + ] + ) ]; }; } diff --git a/modules/neovim/mappings/options.nix b/modules/neovim/mappings/options.nix index e16b787..5538d98 100644 --- a/modules/neovim/mappings/options.nix +++ b/modules/neovim/mappings/options.nix @@ -31,6 +31,12 @@ options = mapConfigOptions // { + key = mkOption { + type = str; + description = '' + Key that triggers this keybind. + ''; + }; mode = mkOption { type = either str (listOf str); description = '' @@ -44,22 +50,20 @@ }; # legacy stuff - mapOption = submodule { + legacyMapOption = submodule { options = mapConfigOptions; }; mapOptions = mode: mkOption { description = "Mappings for ${mode} mode"; - type = attrsOf mapOption; + type = attrsOf legacyMapOption; default = {}; }; in { options.vim = { keymaps = mkOption { - type = submodule { - freeformType = attrsOf (listOf mapType); - }; + type = listOf mapType; description = "Custom keybindings."; example = '' maps = { diff --git a/modules/wrapper/rc/config.nix b/modules/wrapper/rc/config.nix index 549c3fd..41db014 100644 --- a/modules/wrapper/rc/config.nix +++ b/modules/wrapper/rc/config.nix @@ -40,15 +40,14 @@ in { inherit (keymap) desc silent nowait script expr unique noremap; }; - toLuaKeymap = key: bind: "vim.keymap.set(${toLuaObject bind.mode}, ${toLuaObject key}, ${toLuaObject (getAction bind)}, ${toLuaObject (getOpts bind)})"; + toLuaKeymap = bind: "vim.keymap.set(${toLuaObject bind.mode}, ${toLuaObject bind.key}, ${toLuaObject (getAction bind)}, ${toLuaObject (getOpts bind)})"; maps = pipe - # attrsOf (listOf mapOption) + # listOf mapOption cfg.keymaps [ - (mapAttrsToList (key: binds: - concatLines (map (toLuaKeymap key) binds))) + (map toLuaKeymap) concatLines ];