Compare commits

...

10 commits

Author SHA1 Message Date
Pei Yang Ching
feccaf477a cleanup unused import 2024-08-23 14:33:32 +02:00
Pei Yang Ching
aa040ab538 remove unneeded pipe 2024-08-23 14:33:16 +02:00
Pei Yang Ching
71727e5378 keymaps: update example 2024-08-23 14:26:32 +02:00
Pei Yang Ching
34b462a744 keymaps: use listOf mapOption instead 2024-08-23 14:17:24 +02:00
Pei Yang Ching
fd4ddbdd39 maps: fix bad mode names 2024-08-18 15:05:11 +02:00
Pei Yang Ching
b8c8dc2484 maps: allow same key on multiple mode 2024-08-18 14:43:38 +02:00
Ching Pei Yang
e40cce5653
fix: map descriptions (#358)
* remove unused

* maps: fix missing description
2024-08-17 13:08:47 +02:00
Ching Pei Yang
2fc7dc798b
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 6cb57e1d26.

* mappings: re-add legacy vim.maps API

* mappings: fix wrong submodule syntax

* docs: fix missing section id
2024-08-13 00:27:05 +02:00
raf
3ca7e3b841
Merge branch 'main' into v0.7 2024-08-12 00:10:03 +00:00
diniamo
b71bf75dcd
vim.maps rewrite (#352)
* feat: rewrite vim.maps

* modules/mappings: enable silent by default

* docs: add entry for vim.maps rewrite

* lib/binds: improve code, adjust functions to new api
2024-08-12 00:07:45 +00:00
4 changed files with 175 additions and 208 deletions

View file

@ -4,6 +4,8 @@ Release notes for release 0.7
## Breaking Changes and Migration Guide {#sec-breaking-changes-and-migration-guide-0-7} ## Breaking Changes and Migration Guide {#sec-breaking-changes-and-migration-guide-0-7}
### `vim.configRC` removed {#sec-vim-configrc-removed}
In v0.7 we are removing `vim.configRC` in favor of making `vim.luaConfigRC` the 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 top-level DAG, and thereby making the entire configuration Lua based. This
change introduces a few breaking changes: change introduces a few breaking changes:
@ -24,6 +26,28 @@ making good use of its extensive Lua API. Additionally, Vimscript is slow and
brings unnecessary performance overhead while working with different brings unnecessary performance overhead while working with different
configuration formats. configuration formats.
### `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
of strings, where a string represents the short-name of the map mode(s), that
the mapping should be set for. See `:help map-modes` for more information.
For example:
```nix
vim.maps.normal."<leader>m" = { ... };
```
has to be replaced by
```nix
vim.maps."<leader>m" = {
mode = "n";
...
};
```
## Changelog {#sec-release-0.7-changelog} ## Changelog {#sec-release-0.7-changelog}
[ItsSorae](https://github.com/ItsSorae): [ItsSorae](https://github.com/ItsSorae):
@ -114,6 +138,8 @@ configuration formats.
has been introduced for setting up internal plugins. See the "DAG entries in has been introduced for setting up internal plugins. See the "DAG entries in
nvf" manual page for more information. nvf" manual page for more information.
- Rewrite `vim.maps`, see the breaking changes section above.
[NotAShelf](https://github.com/notashelf): [NotAShelf](https://github.com/notashelf):
[ts-error-translator.nvim]: https://github.com/dmmulroy/ts-error-translator.nvim [ts-error-translator.nvim]: https://github.com/dmmulroy/ts-error-translator.nvim

View file

@ -3,51 +3,75 @@
lib, lib,
... ...
}: let }: let
inherit (lib.modules) mkIf; inherit (lib.modules) mkIf mkMerge;
inherit (lib.trivial) pipe;
inherit (lib.attrsets) mapAttrsToList;
inherit (lib.lists) flatten;
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; cfg = config.vim;
in { in {
config = { config = {
vim.maps = { vim.keymaps = mkMerge [
normal = mkIf cfg.disableArrows { (
"<up>" = { mkIf cfg.disableArrows [
action = "<nop>"; {
key = "<up>";
noremap = false; mode = ["n" "i"];
}; action = "<nop>";
"<down>" = { noremap = false;
action = "<nop>"; }
{
noremap = false; key = "<down>";
}; mode = ["n" "i"];
"<left>" = { action = "<nop>";
action = "<nop>"; noremap = false;
noremap = false; }
}; {
"<right>" = { key = "<left>";
action = "<nop>"; mode = ["n" "i"];
noremap = false; action = "<nop>";
}; noremap = false;
}; }
{
insert = mkIf cfg.disableArrows { key = "<right>";
"<up>" = { mode = ["n" "i"];
action = "<nop>"; action = "<nop>";
noremap = false; noremap = false;
}; }
"<down>" = { ]
action = "<nop>"; )
noremap = false; (
}; pipe cfg.maps
"<left>" = { [
action = "<nop>"; (mapAttrsToList (
noremap = false; oldMode: keybinds:
}; mapAttrsToList (
"<right>" = { key: bind:
action = "<nop>"; bind
noremap = false; // {
}; inherit key;
}; mode = legacyMapModes.${oldMode};
}; }
)
keybinds
))
flatten
]
)
];
}; };
} }

View file

@ -1,101 +1,102 @@
{lib, ...}: let {lib, ...}: let
inherit (lib.options) mkOption; inherit (lib.options) mkOption;
inherit (lib.types) bool str attrsOf nullOr submodule; inherit (lib.types) either str listOf attrsOf nullOr submodule bool;
inherit (lib.nvim.config) mkBool; inherit (lib.nvim.config) mkBool;
# Most of the keybindings code is highly inspired by pta2002/nixvim.
# Thank you!
mapConfigOptions = { mapConfigOptions = {
silent =
mkBool false
"Whether this mapping should be silent. Equivalent to adding <silent> to a map.";
nowait =
mkBool false
"Whether to wait for extra input on ambiguous mappings. Equivalent to adding <nowait> to a map.";
script =
mkBool false
"Equivalent to adding <script> to a map.";
expr =
mkBool false
"Means that the action is actually an expression. Equivalent to adding <expr> to a map.";
unique =
mkBool false
"Whether to fail if the map is already defined. Equivalent to adding <unique> 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.";
desc = mkOption { desc = mkOption {
type = nullOr str; type = nullOr str;
default = null; default = null;
description = "A description of this keybind, to be shown in which-key, if you have it enabled."; description = "A description of this keybind, to be shown in which-key, if you have it enabled.";
}; };
action = mkOption {
type = str;
description = "The command to execute.";
};
lua = mkBool false ''
If true, `action` is considered to be lua code.
Thus, it will not be wrapped in `""`.
'';
silent = mkBool true "Whether this mapping should be silent. Equivalent to adding <silent> to a map.";
nowait = mkBool false "Whether to wait for extra input on ambiguous mappings. Equivalent to adding <nowait> to a map.";
script = mkBool false "Equivalent to adding <script> to a map.";
expr = mkBool false "Means that the action is actually an expression. Equivalent to adding <expr> to a map.";
unique = mkBool false "Whether to fail if the map is already defined. Equivalent to adding <unique> 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.";
}; };
mapOption = submodule { mapType = submodule {
options = options =
mapConfigOptions mapConfigOptions
// { // {
action = mkOption { key = mkOption {
type = str; type = str;
description = "The action to execute.";
};
lua = mkOption {
type = bool;
description = '' description = ''
If true, `action` is considered to be lua code. Key that triggers this keybind.
Thus, it will not be wrapped in `""`.
''; '';
default = false; };
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.
'';
example = ''"nvc" for normal, visual and command mode'';
}; };
}; };
}; };
# legacy stuff
legacyMapOption = submodule {
options = mapConfigOptions;
};
mapOptions = mode: mapOptions = mode:
mkOption { mkOption {
description = "Mappings for ${mode} mode"; description = "Mappings for ${mode} mode";
type = attrsOf mapOption; type = attrsOf legacyMapOption;
default = {}; default = {};
}; };
in { in {
options.vim = { options.vim = {
maps = mkOption { keymaps = mkOption {
type = submodule { type = listOf mapType;
options = { description = "Custom keybindings.";
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 for any mode.
For plain maps (e.g. just 'map' or 'remap') use `maps.normalVisualOp`.
'';
example = '' example = ''
maps = { vim.keymaps = [
normal."<leader>m" = { {
key = "<leader>m";
mode = "n";
silent = true; silent = true;
action = "<cmd>make<CR>"; action = ":make<CR>";
}; # Same as nnoremap <leader>m <silent> <cmd>make<CR> }
}; {
key = "<leader>l";
mode = ["n" "x"];
silent = true;
action = "<cmd>cnext<CR>";
}
];
''; '';
default = {};
};
maps = {
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";
}; };
}; };
} }

View file

@ -4,83 +4,17 @@
... ...
}: let }: let
inherit (builtins) map mapAttrs filter; inherit (builtins) map mapAttrs filter;
inherit (lib.options) mkOption; inherit (lib.attrsets) mapAttrsToList filterAttrs;
inherit (lib.attrsets) mapAttrsToList filterAttrs getAttrs attrValues attrNames;
inherit (lib.strings) concatLines concatMapStringsSep; inherit (lib.strings) concatLines concatMapStringsSep;
inherit (lib.trivial) showWarnings; inherit (lib.trivial) showWarnings;
inherit (lib.types) str nullOr;
inherit (lib.generators) mkLuaInline; inherit (lib.generators) mkLuaInline;
inherit (lib.nvim.dag) entryAfter mkLuarcSection resolveDag entryAnywhere; inherit (lib.nvim.dag) entryAfter mkLuarcSection resolveDag entryAnywhere;
inherit (lib.nvim.lua) toLuaObject; inherit (lib.nvim.lua) toLuaObject;
inherit (lib.nvim.config) mkBool;
cfg = config.vim; cfg = config.vim;
# Most of the keybindings code is highly inspired by pta2002/nixvim.
# Thank you!
mapConfigOptions = {
silent =
mkBool false
"Whether this mapping should be silent. Equivalent to adding <silent> to a map.";
nowait =
mkBool false
"Whether to wait for extra input on ambiguous mappings. Equivalent to adding <nowait> to a map.";
script =
mkBool false
"Equivalent to adding <script> to a map.";
expr =
mkBool false
"Means that the action is actually an expression. Equivalent to adding <expr> to a map.";
unique =
mkBool false
"Whether to fail if the map is already defined. Equivalent to adding <unique> 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.";
desc = mkOption {
type = nullOr str;
default = null;
description = "A description of this keybind, to be shown in which-key, if you have it enabled.";
};
};
genMaps = mode: maps: let
/*
Take a user-defined action (string or attrs) and return the following attribute set:
{
action = (string) the actual action to map to this key
config = (attrs) the configuration options for this mapping (noremap, silent...)
}
*/
normalizeAction = action: {
# Extract the values of the config options that have been explicitly set by the user
config =
filterAttrs (_: v: v != null)
(getAttrs (attrNames mapConfigOptions) action);
action =
if action.lua
then mkLuaInline action.action
else action.action;
};
in
attrValues (mapAttrs
(key: action: let
normalizedAction = normalizeAction action;
in {
inherit (normalizedAction) action config;
inherit key;
inherit mode;
})
maps);
in { in {
config = let config = let
filterNonNull = attrs: filterAttrs (_: value: value != null) attrs; filterNonNull = filterAttrs (_: value: value != null);
globalsScript = globalsScript =
mapAttrsToList (name: value: "vim.g.${name} = ${toLuaObject value}") mapAttrsToList (name: value: "vim.g.${name} = ${toLuaObject value}")
(filterNonNull cfg.globals); (filterNonNull cfg.globals);
@ -97,38 +31,20 @@ in {
mapResult = result: concatLines (map mkLuarcSection result); mapResult = result: concatLines (map mkLuarcSection result);
}; };
toLuaBindings = mode: maps: getAction = keymap:
map (value: '' if keymap.lua
vim.keymap.set(${toLuaObject mode}, ${toLuaObject value.key}, ${toLuaObject value.action}, ${toLuaObject value.config}) then mkLuaInline keymap.action
'') (genMaps mode maps); else keymap.action;
# I'm not sure if every one of these will work. getOpts = keymap: {
allmap = toLuaBindings "" config.vim.maps.normalVisualOp; inherit (keymap) desc silent nowait script expr unique noremap;
nmap = toLuaBindings "n" config.vim.maps.normal; };
vmap = toLuaBindings "v" config.vim.maps.visual;
xmap = toLuaBindings "x" config.vim.maps.visualOnly;
smap = toLuaBindings "s" config.vim.maps.select;
imap = toLuaBindings "i" config.vim.maps.insert;
cmap = toLuaBindings "c" config.vim.maps.command;
tmap = toLuaBindings "t" config.vim.maps.terminal;
lmap = toLuaBindings "l" config.vim.maps.lang;
omap = toLuaBindings "o" config.vim.maps.operator;
icmap = toLuaBindings "ic" config.vim.maps.insertCommand;
maps = [ toLuaKeymap = bind: "vim.keymap.set(${toLuaObject bind.mode}, ${toLuaObject bind.key}, ${toLuaObject (getAction bind)}, ${toLuaObject (getOpts bind)})";
nmap
imap maps = concatLines (map toLuaKeymap cfg.keymaps);
vmap
xmap keymaps = maps;
smap
cmap
omap
tmap
lmap
icmap
allmap
];
mappings = concatLines (map concatLines maps);
in { in {
vim = { vim = {
luaConfigRC = { luaConfigRC = {
@ -136,7 +52,7 @@ in {
# basic, theme # basic, theme
pluginConfigs = entryAfter ["theme"] pluginConfigs; pluginConfigs = entryAfter ["theme"] pluginConfigs;
extraPluginConfigs = entryAfter ["pluginConfigs"] extraPluginConfigs; extraPluginConfigs = entryAfter ["pluginConfigs"] extraPluginConfigs;
mappings = entryAfter ["extraPluginConfigs"] mappings; mappings = entryAfter ["extraPluginConfigs"] keymaps;
}; };
builtLuaConfigRC = let builtLuaConfigRC = let