keys: Automatically detect key groups #557
Open
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 extendlazy
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: folke/lazy.nvim#1332
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.