From 2fc7dc798b9fc1cc307591deb977605de18f5000 Mon Sep 17 00:00:00 2001 From: Ching Pei Yang <59727193+horriblename@users.noreply.github.com> Date: Tue, 13 Aug 2024 00:27:05 +0200 Subject: [PATCH] Re-add old maps API (#356) * wrapper: fix wrong import source * wrapper: fix typo * Revert "lib/binds: improve code, adjust functions to new api" This reverts commit 6cb57e1d266e8d1f40bc5af1e691a65d320f8a35. * mappings: re-add legacy vim.maps API * mappings: fix wrong submodule syntax * docs: fix missing section id --- docs/release-notes/rl-0.7.md | 4 +- lib/binds.nix | 121 ++++++++++++++-------------- modules/neovim/mappings/options.nix | 73 ++++++++++++++--- modules/wrapper/rc/config.nix | 35 +++++++- 4 files changed, 156 insertions(+), 77 deletions(-) diff --git a/docs/release-notes/rl-0.7.md b/docs/release-notes/rl-0.7.md index e0dabdf..51e05c9 100644 --- a/docs/release-notes/rl-0.7.md +++ b/docs/release-notes/rl-0.7.md @@ -4,7 +4,7 @@ Release notes for release 0.7 ## Breaking Changes and Migration Guide {#sec-breaking-changes-and-migration-guide-0-7} -### `vim.configRC` removed +### `vim.configRC` removed {#sec-vim-configrc-removed} In v0.7 we are removing `vim.configRC` in favor of making `vim.luaConfigRC` the top-level DAG, and thereby making the entire configuration Lua based. This @@ -26,7 +26,7 @@ making good use of its extensive Lua API. Additionally, Vimscript is slow and brings unnecessary performance overhead while working with different configuration formats. -### `vim.maps` rewrite +### `vim.maps` rewrite {#sec-vim-maps-rewrite} Instead of specifying map modes using submodules (eg.: `vim.maps.normal`), a new `mode` option has mode has been introduced. It can be either a string, or a list diff --git a/lib/binds.nix b/lib/binds.nix index a8fbf5f..8c9e9a6 100644 --- a/lib/binds.nix +++ b/lib/binds.nix @@ -4,68 +4,69 @@ inherit (lib.types) nullOr str; inherit (lib.attrsets) isAttrs mapAttrs; - mkLuaBinding = mode: key: action: desc: - mkIf (key != null) { - ${key} = { - inherit mode action desc; - lua = true; - silent = true; - }; - }; - - mkExprBinding = mode: key: action: desc: - mkIf (key != null) { - ${key} = { - inherit mode action desc; - lua = true; - silent = true; - expr = true; - }; - }; - - mkBinding = mode: key: action: desc: - mkIf (key != null) { - ${key} = { - inherit mode action desc; - silent = true; - }; - }; - - mkMappingOption = description: default: - mkOption { - type = nullOr str; - inherit default description; - }; - - # Utility function that takes two attrsets: - # { someKey = "some_value" } and - # { someKey = { description = "Some Description"; }; } - # and merges them into - # { someKey = { value = "some_value"; description = "Some Description"; }; } - addDescriptionsToMappings = actualMappings: mappingDefinitions: - mapAttrs (name: value: let - isNested = isAttrs value; - returnedValue = - if isNested - then addDescriptionsToMappings actualMappings.${name} mappingDefinitions.${name} - else { - inherit value; - inherit (mappingDefinitions.${name}) description; + binds = rec { + mkLuaBinding = key: action: desc: + mkIf (key != null) { + "${key}" = { + inherit action desc; + lua = true; + silent = true; }; - in - returnedValue) - actualMappings; + }; - mkSetBinding = mode: binding: action: - mkBinding mode binding.value action binding.description; + mkExprBinding = key: action: desc: + mkIf (key != null) { + "${key}" = { + inherit action desc; + lua = true; + silent = true; + expr = true; + }; + }; - mkSetExprBinding = mode: binding: action: - mkExprBinding mode binding.value action binding.description; + mkBinding = key: action: desc: + mkIf (key != null) { + "${key}" = { + inherit action desc; + silent = true; + }; + }; - mkSetLuaBinding = mode: binding: action: - mkLuaBinding mode binding.value action binding.description; + mkMappingOption = description: default: + mkOption { + type = nullOr str; + inherit default description; + }; - pushDownDefault = attr: mapAttrs (_: mkDefault) attr; -in { - inherit mkLuaBinding mkExprBinding mkBinding mkMappingOption addDescriptionsToMappings mkSetBinding mkSetExprBinding mkSetLuaBinding pushDownDefault; -} + # Utility function that takes two attrsets: + # { someKey = "some_value" } and + # { someKey = { description = "Some Description"; }; } + # and merges them into + # { someKey = { value = "some_value"; description = "Some Description"; }; } + addDescriptionsToMappings = actualMappings: mappingDefinitions: + mapAttrs (name: value: let + isNested = isAttrs value; + returnedValue = + if isNested + then addDescriptionsToMappings actualMappings."${name}" mappingDefinitions."${name}" + else { + inherit value; + inherit (mappingDefinitions."${name}") description; + }; + in + returnedValue) + actualMappings; + + mkSetBinding = binding: action: + mkBinding binding.value action binding.description; + + mkSetExprBinding = binding: action: + mkExprBinding binding.value action binding.description; + + mkSetLuaBinding = binding: action: + mkLuaBinding binding.value action binding.description; + + pushDownDefault = attr: mapAttrs (_: mkDefault) attr; + }; +in + binds diff --git a/modules/neovim/mappings/options.nix b/modules/neovim/mappings/options.nix index dcf9cf4..522079e 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,69 @@ 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 { + options = + mapConfigOptions + // { + 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. + ''; + }; + }; + }; + + # 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 7457d45..f4c100d 100644 --- a/modules/wrapper/rc/config.nix +++ b/modules/wrapper/rc/config.nix @@ -3,9 +3,8 @@ lib, ... }: let - inherit (builtins) map mapAttrs filter; - inherit (lib.options) mkOption; - inherit (lib.attrsets) mapAttrsToList filterAttrs getAttrs attrValues attrNames; + inherit (builtins) map mapAttrs filter removeAttrs attrNames; + inherit (lib.attrsets) mapAttrsToList filterAttrs attrsToList; inherit (lib.strings) concatLines concatMapStringsSep; inherit (lib.trivial) showWarnings; inherit (lib.generators) mkLuaInline; @@ -46,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 config.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 = {