From dffaaaa6009ada2b5de77bf7577e5a4f0e79b399 Mon Sep 17 00:00:00 2001 From: Ching Pei Yang <59727193+horriblename@users.noreply.github.com> Date: Sat, 10 Aug 2024 22:34:44 +0200 Subject: [PATCH] mappings: re-add legacy vim.maps API --- modules/neovim/mappings/options.nix | 70 ++++++++++++++++++++++++----- modules/wrapper/rc/config.nix | 32 ++++++++++++- 2 files changed, 89 insertions(+), 13 deletions(-) diff --git a/modules/neovim/mappings/options.nix b/modules/neovim/mappings/options.nix index dcf9cf4d..7b785b5d 100644 --- a/modules/neovim/mappings/options.nix +++ b/modules/neovim/mappings/options.nix @@ -1,17 +1,9 @@ {lib, ...}: let inherit (lib.options) mkOption; - inherit (lib.types) either str listOf attrsOf nullOr submodule; + inherit (lib.types) either str listOf attrsOf nullOr submodule bool; inherit (lib.nvim.config) mkBool; - mapType = submodule { - mode = mkOption { - type = either str (listOf str); - description = '' - The short-name of the mode to set the keymapping for. Passing an empty string is the equivalent of `:map`. - - See `:help map-modes` for a list of modes. - ''; - }; + mapConfigOptions = { desc = mkOption { type = nullOr str; default = null; @@ -34,10 +26,66 @@ unique = mkBool false "Whether to fail if the map is already defined. Equivalent to adding to a map."; noremap = mkBool true "Whether to use the 'noremap' variant of the command, ignoring any custom mappings on the defined action. It is highly advised to keep this on, which is the default."; }; + + mapType = submodule { + mode = mkOption { + type = either str (listOf str); + description = '' + The short-name of the mode to set the keymapping for. Passing an empty string is the equivalent of `:map`. + + See `:help map-modes` for a list of modes. + ''; + }; + inherit (mapConfigOptions) desc action lua silent nowait script expr unique noremap; + }; + + # legacy stuff + mapOption = submodule { + options = + mapConfigOptions + // { + action = mkOption { + type = str; + description = "The action to execute."; + }; + + lua = mkOption { + type = bool; + description = '' + If true, `action` is considered to be lua code. + Thus, it will not be wrapped in `""`. + ''; + default = false; + }; + }; + }; + + mapOptions = mode: + mkOption { + description = "Mappings for ${mode} mode"; + type = attrsOf mapOption; + default = {}; + }; in { options.vim = { maps = mkOption { - type = attrsOf mapType; + type = submodule { + freeformType = attrsOf mapType; + options = { + normal = mapOptions "normal"; + insert = mapOptions "insert"; + select = mapOptions "select"; + visual = mapOptions "visual and select"; + terminal = mapOptions "terminal"; + normalVisualOp = mapOptions "normal, visual, select and operator-pending (same as plain 'map')"; + + visualOnly = mapOptions "visual only"; + operator = mapOptions "operator-pending"; + insertCommand = mapOptions "insert and command-line"; + lang = mapOptions "insert, command-line and lang-arg"; + command = mapOptions "command-line"; + }; + }; default = {}; description = "Custom keybindings."; example = '' diff --git a/modules/wrapper/rc/config.nix b/modules/wrapper/rc/config.nix index 3ec2b3e6..f4c100da 100644 --- a/modules/wrapper/rc/config.nix +++ b/modules/wrapper/rc/config.nix @@ -3,7 +3,7 @@ lib, ... }: let - inherit (builtins) map mapAttrs filter; + inherit (builtins) map mapAttrs filter removeAttrs attrNames; inherit (lib.attrsets) mapAttrsToList filterAttrs attrsToList; inherit (lib.strings) concatLines concatMapStringsSep; inherit (lib.trivial) showWarnings; @@ -45,7 +45,35 @@ in { value, }: "vim.keymap.set(${toLuaObject value.mode}, ${toLuaObject name}, ${toLuaObject (getAction value)}, ${toLuaObject (getOpts value)})"; - keymaps = concatLines (map toLuaKeymap (attrsToList (filterNonNull cfg.maps))); + namedModes = { + "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"]; + }; + + maps = + removeAttrs cfg.maps (attrNames namedModes) + // mapAttrs (_: legacyMap: legacyMap // {mode = namedModes.normal;}) cfg.maps.normal + // mapAttrs (_: legacyMap: legacyMap // {mode = namedModes.insert;}) cfg.maps.insert + // mapAttrs (_: legacyMap: legacyMap // {mode = namedModes.select;}) cfg.maps.select + // mapAttrs (_: legacyMap: legacyMap // {mode = namedModes.visual;}) cfg.maps.visual + // mapAttrs (_: legacyMap: legacyMap // {mode = namedModes.terminal;}) cfg.maps.terminal + // mapAttrs (_: legacyMap: legacyMap // {mode = namedModes.normalVisualOp;}) cfg.maps.normalVisualOp + // mapAttrs (_: legacyMap: legacyMap // {mode = namedModes.visualOnly;}) cfg.maps.visualOnly + // mapAttrs (_: legacyMap: legacyMap // {mode = namedModes.operator;}) cfg.maps.operator + // mapAttrs (_: legacyMap: legacyMap // {mode = namedModes.insertCommand;}) cfg.maps.insertCommand + // mapAttrs (_: legacyMap: legacyMap // {mode = namedModes.lang;}) cfg.maps.lang + // mapAttrs (_: legacyMap: legacyMap // {mode = namedModes.command;}) cfg.maps.command; + + keymaps = concatLines (map toLuaKeymap (attrsToList (filterNonNull maps))); in { vim = { luaConfigRC = {