folke / which-key.nvim

💥 Create key bindings that stick. WhichKey helps you remember your Neovim keymaps, by showing available keybindings in a popup as you type.
Apache License 2.0
4.75k stars 154 forks source link

keys: Automatically detect key groups #557

Closed har7an closed 1 month ago

har7an commented 6 months ago

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: https://github.com/folke/lazy.nvim/pull/1332

Things to do

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.

har7an commented 4 months ago

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

folke commented 1 month ago

Closing in favor of https://github.com/folke/which-key.nvim/pull/522