Nested keymaps with v3 #717
-
Just discovered the changes in v3. One thing I am unable to find (or does not exist) is the way to nest and group keyamps, require('which-key').register {
['<leader>n'] = {
name = 'Noice',
n = { vim.cmd.Noice, 'Noice window' },
e = { vim.cmd.NoiceErrors, 'Open error list' },
m = { vim.cmd.messages, 'Open messages' },
c = {
function()
vim.cmd.messages 'clear'
end,
'Clear all messaages',
},
},
} Is this no longer possible with which-key v3? Do I need to perpend |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 7 replies
-
no longer possible |
Beta Was this translation helpful? Give feedback.
-
Thanks for the quick response. Thanks for all your hard work! |
Beta Was this translation helpful? Give feedback.
-
Seeing as its already part of 'legacy' code and given people's interest, would you consider supporting this method? |
Beta Was this translation helpful? Give feedback.
-
My setup also uses this form extensively. I'm working around it with this helper function local function expand(arg)
result = {}
queue = { arg }
item = table.remove(queue)
while item do
local children = item["children"]
if children then
for i, v in ipairs(children) do
v["prefix"] = (item["prefix"] or "") .. (v["prefix"] or "")
table.insert(queue, v)
end
local name = item["name"]
if name then
table.insert(result, { item["prefix"], group = name })
end
else
item[1] = (item["prefix"] or "") .. item[1]
item["prefix"] = nil
table.insert(result, item)
end
item = table.remove(queue)
end
return result
end It takes a tree structure and flattens it into the format that which-key now expect. For example, your structure would be represented as: {
prefix = "<leader>n",
name = "Noice",
children = {
{ "n", vim.cmd.Noice, desc = "Noice window" },
{ "e", vim.cmd.NoiceErrors, desc = "Open error list" },
{ "m", vim.cmd.messages, desc = "Open messages" },
{
"c",
function()
vim.cmd.messages("clear")
end,
desc = "Clear all messaages",
},
},
} You can nest as much as you want and it only creates a group for any level you give a name to. For example you could also represent your structure as: {
prefix = "<leader>",
children = {
{
prefix = "n",
name = "Noice",
children = {
{ "n", vim.cmd.Noice, desc = "Noice window" },
{ "e", vim.cmd.NoiceErrors, desc = "Open error list" },
{ "m", vim.cmd.messages, desc = "Open messages" },
{
"c",
function()
vim.cmd.messages("clear")
end,
desc = "Clear all messaages",
},
},
},
},
} Its kinda noisy with all the key names but it did what I needed it to do... |
Beta Was this translation helpful? Give feedback.
My setup also uses this form extensively. I'm working around it with this helper function