Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

keys: Automatically detect key groups #557

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

har7an
Copy link

@har7an har7an commented Dec 21, 2023

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.

In a lazy config snippet, it looks like this:

        keys = {
            { "<leader>g",                                      desc = "+git" },
            { "<leader>gg",     "<Cmd>tab Git<CR>",             desc = "overview" },
            { "<leader>gb",                                     desc = "+branch" },
        },

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: folke/lazy.nvim#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. the `keys` config option for
the `lazy.nvim` plugin manager.
instead of inlining the string pattern where needed.
@har7an
Copy link
Author

har7an commented Feb 20, 2024

Updates the "group identifier" to use +, which is already used to mark groups in the UI.

@har7an har7an changed the title keys: Magically detect key groups keys: Automatically detect key groups Feb 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant