mirror of
https://github.com/nix-community/nixvim.git
synced 2025-06-21 00:25:42 +02:00
modules/keymaps: deprecate maps option
This commit is contained in:
parent
ae3a47674b
commit
48dd8d28cf
3 changed files with 71 additions and 247 deletions
|
@ -79,63 +79,43 @@ in rec {
|
||||||
|
|
||||||
# TODO: When `maps` will have to be deprecated (early December 2023), change this.
|
# TODO: When `maps` will have to be deprecated (early December 2023), change this.
|
||||||
# mapOptionSubmodule = {...} (no need for options... except for 'optionalAction')
|
# mapOptionSubmodule = {...} (no need for options... except for 'optionalAction')
|
||||||
mkMapOptionSubmodule = {
|
mapOptionSubmodule = with types;
|
||||||
defaultMode ? "",
|
submodule {
|
||||||
withKeyOpt ? true,
|
options = {
|
||||||
flatConfig ? false,
|
key = mkOption {
|
||||||
}:
|
type = str;
|
||||||
with types; let
|
description = "The key to map.";
|
||||||
mapOptionSubmodule = submodule {
|
example = "<C-m>";
|
||||||
options =
|
};
|
||||||
(
|
|
||||||
if withKeyOpt
|
|
||||||
then {
|
|
||||||
key = mkOption {
|
|
||||||
type = str;
|
|
||||||
description = "The key to map.";
|
|
||||||
example = "<C-m>";
|
|
||||||
};
|
|
||||||
}
|
|
||||||
else {}
|
|
||||||
)
|
|
||||||
// {
|
|
||||||
mode = mkOption {
|
|
||||||
type = either modeEnum (listOf modeEnum);
|
|
||||||
description = ''
|
|
||||||
One or several modes.
|
|
||||||
Use the short-names (`"n"`, `"v"`, ...).
|
|
||||||
See `:h map-modes` to learn more.
|
|
||||||
'';
|
|
||||||
default = defaultMode;
|
|
||||||
example = ["n" "v"];
|
|
||||||
};
|
|
||||||
|
|
||||||
action = mkOption {
|
mode = mkOption {
|
||||||
type = str;
|
type = either modeEnum (listOf modeEnum);
|
||||||
description = "The action to execute.";
|
description = ''
|
||||||
};
|
One or several modes.
|
||||||
|
Use the short-names (`"n"`, `"v"`, ...).
|
||||||
|
See `:h map-modes` to learn more.
|
||||||
|
'';
|
||||||
|
default = "";
|
||||||
|
example = ["n" "v"];
|
||||||
|
};
|
||||||
|
|
||||||
lua = mkOption {
|
action = mkOption {
|
||||||
type = bool;
|
type = str;
|
||||||
description = ''
|
description = "The action to execute.";
|
||||||
If true, `action` is considered to be lua code.
|
};
|
||||||
Thus, it will not be wrapped in `""`.
|
|
||||||
'';
|
lua = mkOption {
|
||||||
default = false;
|
type = bool;
|
||||||
};
|
description = ''
|
||||||
}
|
If true, `action` is considered to be lua code.
|
||||||
// (
|
Thus, it will not be wrapped in `""`.
|
||||||
if flatConfig
|
'';
|
||||||
then mapConfigOptions
|
default = false;
|
||||||
else {
|
};
|
||||||
options = mapConfigOptions;
|
|
||||||
}
|
options = mapConfigOptions;
|
||||||
);
|
|
||||||
};
|
};
|
||||||
in
|
};
|
||||||
if flatConfig
|
|
||||||
then either str mapOptionSubmodule
|
|
||||||
else mapOptionSubmodule;
|
|
||||||
|
|
||||||
# Correctly merge two attrs (partially) representing a mapping.
|
# Correctly merge two attrs (partially) representing a mapping.
|
||||||
mergeKeymap = defaults: keymap: let
|
mergeKeymap = defaults: keymap: let
|
||||||
|
@ -145,56 +125,6 @@ in rec {
|
||||||
# Then, merge the root attrs together and add the previously merged `options` attrs.
|
# Then, merge the root attrs together and add the previously merged `options` attrs.
|
||||||
(defaults // keymap) // {options = mergedOpts;};
|
(defaults // keymap) // {options = mergedOpts;};
|
||||||
|
|
||||||
# Given an attrs of key mappings (for a single mode), applies the defaults to each one of them.
|
|
||||||
#
|
|
||||||
# Example:
|
|
||||||
# mkModeMaps { silent = true; } {
|
|
||||||
# Y = "y$";
|
|
||||||
# "<C-c>" = { action = ":b#<CR>"; silent = false; };
|
|
||||||
# };
|
|
||||||
#
|
|
||||||
# would give:
|
|
||||||
# {
|
|
||||||
# Y = {
|
|
||||||
# action = "y$";
|
|
||||||
# silent = true;
|
|
||||||
# };
|
|
||||||
# "<C-c>" = {
|
|
||||||
# action = ":b#<CR>";
|
|
||||||
# silent = false;
|
|
||||||
# };
|
|
||||||
# };
|
|
||||||
mkModeMaps = defaults:
|
|
||||||
mapAttrs
|
|
||||||
(
|
|
||||||
key: action: let
|
|
||||||
actionAttrs =
|
|
||||||
if isString action
|
|
||||||
then {inherit action;}
|
|
||||||
else action;
|
|
||||||
in
|
|
||||||
defaults // actionAttrs
|
|
||||||
);
|
|
||||||
|
|
||||||
# Applies some default mapping options to a set of mappings
|
|
||||||
#
|
|
||||||
# Example:
|
|
||||||
# maps = mkMaps { silent = true; expr = true; } {
|
|
||||||
# normal = {
|
|
||||||
# ...
|
|
||||||
# };
|
|
||||||
# visual = {
|
|
||||||
# ...
|
|
||||||
# };
|
|
||||||
# }
|
|
||||||
mkMaps = defaults:
|
|
||||||
mapAttrs
|
|
||||||
(
|
|
||||||
name: modeMaps:
|
|
||||||
mkModeMaps defaults modeMaps
|
|
||||||
);
|
|
||||||
|
|
||||||
# TODO deprecate `mkMaps` and `mkModeMaps` and leave only this one
|
|
||||||
mkKeymaps = defaults:
|
mkKeymaps = defaults:
|
||||||
map
|
map
|
||||||
(mergeKeymap defaults);
|
(mergeKeymap defaults);
|
||||||
|
|
|
@ -5,59 +5,11 @@
|
||||||
...
|
...
|
||||||
}:
|
}:
|
||||||
with lib; {
|
with lib; {
|
||||||
options = {
|
# This warning has been added on 2023-12-02. TODO: remove it in early Feb. 2024.
|
||||||
maps =
|
imports = [
|
||||||
mapAttrs
|
(mkRemovedOptionModule
|
||||||
(
|
["maps"]
|
||||||
modeName: modeProps: let
|
|
||||||
desc = modeProps.desc or modeName;
|
|
||||||
in
|
|
||||||
mkOption {
|
|
||||||
description = "Mappings for ${desc} mode";
|
|
||||||
type = with types;
|
|
||||||
attrsOf
|
|
||||||
(
|
|
||||||
either
|
|
||||||
str
|
|
||||||
(
|
|
||||||
helpers.keymaps.mkMapOptionSubmodule
|
|
||||||
{
|
|
||||||
defaultMode = modeProps.short;
|
|
||||||
withKeyOpt = false;
|
|
||||||
flatConfig = true;
|
|
||||||
}
|
|
||||||
)
|
|
||||||
);
|
|
||||||
default = {};
|
|
||||||
}
|
|
||||||
)
|
|
||||||
helpers.keymaps.modes;
|
|
||||||
|
|
||||||
keymaps = mkOption {
|
|
||||||
type =
|
|
||||||
types.listOf
|
|
||||||
(helpers.keymaps.mkMapOptionSubmodule {});
|
|
||||||
default = [];
|
|
||||||
example = [
|
|
||||||
{
|
|
||||||
key = "<C-m>";
|
|
||||||
action = "<cmd>make<CR>";
|
|
||||||
options.silent = true;
|
|
||||||
}
|
|
||||||
];
|
|
||||||
};
|
|
||||||
};
|
|
||||||
|
|
||||||
config = {
|
|
||||||
warnings =
|
|
||||||
optional
|
|
||||||
(
|
|
||||||
any
|
|
||||||
(modeMaps: modeMaps != {})
|
|
||||||
(attrValues config.maps)
|
|
||||||
)
|
|
||||||
''
|
''
|
||||||
The `maps` option will be deprecated in the near future.
|
|
||||||
Please, use the new `keymaps` option which works as follows:
|
Please, use the new `keymaps` option which works as follows:
|
||||||
|
|
||||||
keymaps = [
|
keymaps = [
|
||||||
|
@ -79,66 +31,42 @@ with lib; {
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
];
|
];
|
||||||
'';
|
'')
|
||||||
|
];
|
||||||
|
|
||||||
|
options = {
|
||||||
|
keymaps = mkOption {
|
||||||
|
type =
|
||||||
|
types.listOf
|
||||||
|
helpers.keymaps.mapOptionSubmodule;
|
||||||
|
default = [];
|
||||||
|
example = [
|
||||||
|
{
|
||||||
|
key = "<C-m>";
|
||||||
|
action = "<cmd>make<CR>";
|
||||||
|
options.silent = true;
|
||||||
|
}
|
||||||
|
];
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
config = {
|
||||||
extraConfigLua = let
|
extraConfigLua = let
|
||||||
modeMapsAsList =
|
normalizeMapping = keyMapping: {
|
||||||
flatten
|
inherit
|
||||||
(
|
(keyMapping)
|
||||||
mapAttrsToList
|
mode
|
||||||
(
|
key
|
||||||
modeOptionName: modeProps:
|
options
|
||||||
mapAttrsToList
|
;
|
||||||
(
|
|
||||||
key: action:
|
|
||||||
(
|
|
||||||
if isString action
|
|
||||||
then {
|
|
||||||
mode = modeProps.short;
|
|
||||||
inherit action;
|
|
||||||
lua = false;
|
|
||||||
options = {};
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
inherit
|
|
||||||
(action)
|
|
||||||
action
|
|
||||||
lua
|
|
||||||
mode
|
|
||||||
;
|
|
||||||
}
|
|
||||||
// {
|
|
||||||
options =
|
|
||||||
getAttrs
|
|
||||||
(attrNames helpers.keymaps.mapConfigOptions)
|
|
||||||
action;
|
|
||||||
}
|
|
||||||
)
|
|
||||||
// {inherit key;}
|
|
||||||
)
|
|
||||||
config.maps.${modeOptionName}
|
|
||||||
)
|
|
||||||
helpers.keymaps.modes
|
|
||||||
);
|
|
||||||
|
|
||||||
mappings = let
|
action =
|
||||||
normalizeMapping = keyMapping: {
|
if keyMapping.lua
|
||||||
inherit
|
then helpers.mkRaw keyMapping.action
|
||||||
(keyMapping)
|
else keyMapping.action;
|
||||||
mode
|
};
|
||||||
key
|
|
||||||
options
|
|
||||||
;
|
|
||||||
|
|
||||||
action =
|
mappings = map normalizeMapping config.keymaps;
|
||||||
if keyMapping.lua
|
|
||||||
then helpers.mkRaw keyMapping.action
|
|
||||||
else keyMapping.action;
|
|
||||||
};
|
|
||||||
in
|
|
||||||
map normalizeMapping
|
|
||||||
(config.keymaps ++ modeMapsAsList);
|
|
||||||
in
|
in
|
||||||
optionalString (mappings != [])
|
optionalString (mappings != [])
|
||||||
''
|
''
|
||||||
|
|
|
@ -1,38 +1,4 @@
|
||||||
{helpers, ...}: {
|
{helpers, ...}: {
|
||||||
legacy = {
|
|
||||||
maps.normal."," = "<cmd>echo \"test\"<cr>";
|
|
||||||
};
|
|
||||||
|
|
||||||
legacy-mkMaps = {
|
|
||||||
maps = helpers.keymaps.mkMaps {silent = true;} {
|
|
||||||
normal."," = "<cmd>echo \"test\"<cr>";
|
|
||||||
visual = {
|
|
||||||
"<C-a>" = {
|
|
||||||
action = "function() print('toto') end";
|
|
||||||
lua = true;
|
|
||||||
silent = false;
|
|
||||||
};
|
|
||||||
"<C-z>" = {
|
|
||||||
action = "bar";
|
|
||||||
};
|
|
||||||
};
|
|
||||||
};
|
|
||||||
};
|
|
||||||
|
|
||||||
legacy-mkModeMaps = {
|
|
||||||
maps.normal = helpers.keymaps.mkModeMaps {silent = true;} {
|
|
||||||
"," = "<cmd>echo \"test\"<cr>";
|
|
||||||
"<C-a>" = {
|
|
||||||
action = "function() print('toto') end";
|
|
||||||
lua = true;
|
|
||||||
silent = false;
|
|
||||||
};
|
|
||||||
"<leader>b" = {
|
|
||||||
action = "bar";
|
|
||||||
};
|
|
||||||
};
|
|
||||||
};
|
|
||||||
|
|
||||||
example = {
|
example = {
|
||||||
keymaps = [
|
keymaps = [
|
||||||
{
|
{
|
||||||
|
@ -47,7 +13,7 @@
|
||||||
];
|
];
|
||||||
};
|
};
|
||||||
|
|
||||||
mkMaps = {
|
mkKeymaps = {
|
||||||
keymaps =
|
keymaps =
|
||||||
helpers.keymaps.mkKeymaps
|
helpers.keymaps.mkKeymaps
|
||||||
{
|
{
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue