which-key.nvim icon indicating copy to clipboard operation
which-key.nvim copied to clipboard

Show groups between mappings

Open jandamm opened this issue 4 years ago • 10 comments

Is there a way to inline the group names between the mappings?

a -> mapping
b -> +group
c -> mapping 2

instead of

a -> mapping
c -> mapping 2
b -> +group

jandamm avatar Nov 05 '21 09:11 jandamm

I was looking for similar functionality and came across https://github.com/folke/which-key.nvim/issues/131. Probably these 2 issues are duplicates, or at least related. It'd be nice if there was a way to control the order in which the mappings are displayed. For me, just having the ability to disable the auto-ordering and let the mappings be displayed as they have been declared would be enough.

HendrikThePendric avatar Dec 13 '21 12:12 HendrikThePendric

If the sorting function would be configurable function() return false end should keep manual sorting.

jandamm avatar Dec 13 '21 12:12 jandamm

I forked the repo and was able to suppress the sorting without too much trouble. Then added a configuration option to toggle sorting on and off and that worked as well. But when trying out this solution and actually inspecting the order of the keys, I found they were different every time.

I think the issue is the fact that in Lua the sequence of keys is only guaranteed for tables with integer keys. My source is this SO post where it says:

In Lua, only tables with 1-based consecutive integer keys (a.k.a. array tables) can be parsed in order.

But until a few days ago I hadn't written a line of Lua in my life, so I might be way off.

In any case, the fix seems to be less trivial than I initially thought and I'm in over my head.

HendrikThePendric avatar Dec 13 '21 13:12 HendrikThePendric

I'm not a 100% sure but if it works ipairs would have to be used instead of pairs.

jandamm avatar Dec 13 '21 16:12 jandamm

I don’t see how that would help since the keys are still strings and not integers.

HendrikThePendric avatar Dec 13 '21 18:12 HendrikThePendric

Right, doesn't work with ipairs. Providing a custom sorting function would however work.

jandamm avatar Dec 13 '21 19:12 jandamm

I can't think of a solution which is based on how the keys are inserted (i.e. the lexical sequence in which the mappings were written). A Lua table is passed to the register function, and the table data type does not respect insertion order. So the order is lost straight away.

I can see how a "custom sorting" function could help, but only if the mappings had some explicit property to sort on. For example if a mapping would accept a order_nr property. Personally, if I would have to choose between the two evils of managing the order of my key mappings this way, or having them auto-sorted alphabetically, I'd probably go with the latter option.

I'm deleting my cloned repo and will not be pursuing this any further.

HendrikThePendric avatar Dec 13 '21 19:12 HendrikThePendric

Well, this ticket was about sorting groups alongside mappings. This can be done by a custom sort function.

Preserving the order could only work with a order_id or by using register multiple times (where the order of the calls would be preserved). You could also use a sort function where you store an array table and compare the indices. But you'd again have to have second list - should be reasonably easy to generate this list with sed though.

jandamm avatar Dec 13 '21 21:12 jandamm

should be reasonably easy to generate this list with sed though

That sounds like an interesting approach! Good luck.

HendrikThePendric avatar Dec 14 '21 08:12 HendrikThePendric

This would be great to have -- having two aA-zZ lists (one for X mappings + another for the +X group names) instead of just one is the one thing that's always looked strange when using this plugin.

ooloth avatar Feb 18 '23 20:02 ooloth