from nvim-builtin keymappings. Scans for a magic string in the desc field of a keymapping and, if found, removes the magic prefix and registers the respective binding as a group, rather than an action.
This allows defining keybinding groups from outside which-key, which can be utilized in conjunction with e.g. the keys config option for the lazy.nvim plugin manager.
If you're interested in merging this I'd be happy to write documentation for this feature to make it discoverable for users. Just let me know!
Motivation
My motivation for implementing this feature is that I'd like to be able to make my lazy plugin configurations fully standalone, so I can drop individual files to a plugins folder and it picks up all my keybindings (in the keys config item for lazy loading). At the moment this either requires some "glue" in the which-key config file or some boilerplate setup in a plugin-configs init key (so it doesn't interfere with the lazy loading mechanism). With this PR one can write a plugins entire keybinding configuration (including keybinding groups) in the keys section of the lazy config.
In a second step, if this gets merged, I'd like to extend lazy to group/shorten keybindings with a common key prefix in the home screen/overview. I have a few plugins with a lot of keybindings, and since lazy currently lists all the triggers in the overview, things become a bit hard to read at times.
A proposal for this feature is available here: https://github.com/folke/lazy.nvim/pull/1332
Things to do
Using WhichKeyGroup: as a prefix was the first thing that came to mind which is reasonably easy to handle in Lua. I'm open to suggestions if this should be changed. Changed to using the "internal" key group prefix +.
The code for "creating" the group binding is pretty wild (i.e. manually injects fields into the mapping table in the code, see diff). I didn't find any functions built into the plugin that could do this in a more controlled way, except for mappings._parse() which I didn't feel like using (since it seems to be an implementation detail). Again, I'm open to suggestions.
Other considerations
While implementing this, initially I wanted to match against patters in the rhs of a keymapping. However it seems that mappings registered through lazys keys config are wrapped into lua functions, making the rhs impossible to parse/match on. I guess this makes sense so lazy knows when a keybinding was pressed and can load the associated plugin accordingly. So I switched to using the desc field instead.
My very first experiments actually consisted of appending the secret char defined in which-key.keys to a mappings desc field. While this does make the binding in question display as group, it does not display a label (just the default +prefix text). Also it didn't seem like a good idea to expose this implementation detail, so I went with a "magic" prefix instead.
from nvim-builtin keymappings. Scans for a magic string in the
desc
field of a keymapping and, if found, removes the magic prefix and registers the respective binding as a group, rather than an action.This allows defining keybinding groups from outside
which-key
, which can be utilized in conjunction with e.g. thekeys
config option for thelazy.nvim
plugin manager.In a
lazy
config snippet, it looks like this:If you're interested in merging this I'd be happy to write documentation for this feature to make it discoverable for users. Just let me know!
Motivation
My motivation for implementing this feature is that I'd like to be able to make my
lazy
plugin configurations fully standalone, so I can drop individual files to a plugins folder and it picks up all my keybindings (in thekeys
config item for lazy loading). At the moment this either requires some "glue" in thewhich-key
config file or some boilerplate setup in a plugin-configsinit
key (so it doesn't interfere with the lazy loading mechanism). With this PR one can write a plugins entire keybinding configuration (including keybinding groups) in thekeys
section of the lazy config.In a second step, if this gets merged, I'd like to extendA proposal for this feature is available here: https://github.com/folke/lazy.nvim/pull/1332lazy
to group/shorten keybindings with a common key prefix in the home screen/overview. I have a few plugins with a lot of keybindings, and since lazy currently lists all the triggers in the overview, things become a bit hard to read at times.Things to do
UsingChanged to using the "internal" key group prefixWhichKeyGroup:
as a prefix was the first thing that came to mind which is reasonably easy to handle in Lua. I'm open to suggestions if this should be changed.+
.mapping
table in the code, see diff). I didn't find any functions built into the plugin that could do this in a more controlled way, except formappings._parse()
which I didn't feel like using (since it seems to be an implementation detail). Again, I'm open to suggestions.Other considerations
While implementing this, initially I wanted to match against patters in the
rhs
of a keymapping. However it seems that mappings registered throughlazy
skeys
config are wrapped into lua functions, making therhs
impossible to parse/match on. I guess this makes sense solazy
knows when a keybinding was pressed and can load the associated plugin accordingly. So I switched to using thedesc
field instead.My very first experiments actually consisted of appending the
secret
char defined inwhich-key.keys
to a mappingsdesc
field. While this does make the binding in question display as group, it does not display a label (just the default+prefix
text). Also it didn't seem like a good idea to expose this implementation detail, so I went with a "magic" prefix instead.