Making Lazy and Whichkkey work together?

Hello there.

This might be a bit dumb, but I have two Plugins I would like to have that just wont work together nicely.

  • The first one is lazy.nvim, a nice package manager, where one of its nicest features is that it can help you lazy-load many of your packages. To achieve that, it offers you to let it manage and set your keymappings for your plugins for you. If you do, the affected package will only load after the key combo is invoked. I’d like to have that.
  • On the other hand are whichkey and legendary, These two working together can create you pretty helpmenus and legends for all your commands and keybindings. These menus can even be automatically nested and labeled if you let whichkey manage and set your keymappings for you.

I would like to have both of these capabilities, but the obvious question then becomes: Who gets to manage my keybindings? I would like whichkey to do that, so that legendary has full access to the associated functionality, but then lazy has trouble properly loading my packages when required.

My current approach looks like this:

  1. store all my mappings stored in a big nested table that whichkey understands
  2. create a transformed copy of that in a format that lazy understands, without the functionality, just the key
  3. call require("lazy").setup , provide the stripped down mappings where applicable.
  4. pass the original table over to whichkey to register.

The tables look like this:

wktable = {
    undotree = {
        keys = {
            ["<Leader>"] = {
                u = { ":UndotreeToggle<CR>", "toggle undotree" }
            },
        },
        opts = {},
    },


   -- ... more here
}
  


lazykeys = flatten(wktable)
-- -> {undotree = { { "<Leader>u", mode = "n" } },  .... }

It seems this is not enough to convince lazy to load undotree on ‘u’ when required (I get an Error: UndotreeToggle is not an Editor command). After much confusions I found that Lazy’s section about keybindings notes that, if the rhs-part of a binding is missing, the actual binding needs to be created in the plugins setup function, but I want to register the binding in whichkey beforehand, else my menus and legends would not contains this plugins bindings until it is loaded.
Is there maybe a command to manually prompt lazy to load a plugin? Then I could shift the responsibility of loading the packages at the right time away from lazy and into the actual keybindings.

This all seems a bit extra silly since all these packages are from the same author. Am I doing something wrong? Any help is appreciated. If you need I can give you my full config, bit its a bit lengthy at this point.

1 Like

Which-key doesn’t require you to create the keymappings through it. Any keymapping with a desc field will get a label. The wk.register is only needed for labelling groups. If you register the keymaps through lazy they should show up in the which-key menu

In fact lazy.nvim doesn’t require you to create keymappings through it either.
You could also add lazy load on command then you can create the keymap wherever. Lazy loading on require module is always there so if you make the keymap such that the module is required inside the callback (not at the time of setting) then it will be lazy loaded.

1 Like

Hi. Thanks for your help. Sorry for not responding so long, I had important stuff to do and had little time to fiddle with my nvim config.
I managed to get it to work now, it is just so incredibly inconvenient. No matter what I do, I either need to have to coexisting styles for keybindings, or have some very heavyweight logic to transform and adapt all the tables between formats, conditionally removing the action from the binding depending on what is being done with them. It seems there is just not an easy way :frowning:

Again, idk what you’re talking about, which-key automatically detects ALL my leader key and operator mode mappings (anything where the prefix is registered with which-key). I have zero custom logic or transformations or any mixed styles. All of my keymaps are lazy.nvim or vim.keymap.set if it’s not a plugin . I just have a few entries in wk.register to give groups and prefixes names, no keymappings

Doing it the other way is also entirely possible, since, like I mentioned before, lazy.nvim can lazy load on commands and modules

Hi

It might very well be that what I did is a typical case of unnecessary over engineering. While it might be overkill, it works , so I will most likely leave it at that for now. Thanks again for the help, and sorry for my infrequent replies, my schedule is quite full atm.

Your approach is definitely a case of massive over-engineering. As you say, it works, so you can leave it for now, but it’s definitely worth simplifying sometime in the future.