88local walk
99
1010--- @param node wk.Node
11- --- @param prefix string
11+ --- @param prefix string ?
1212--- @return MenuItem[] ?
1313local node_to_item = function (node , prefix )
1414 -- Create the name.
@@ -18,7 +18,10 @@ local node_to_item = function(node, prefix)
1818 -- If item has children, descend to them.
1919 return {
2020 name = name .. " " .. node .path [# node .path ],
21- items = walk (node , prefix ),
21+ items = function ()
22+ return walk (node , prefix ) or {}
23+ end ,
24+ hl = " Exblue" ,
2225 rtxt = node .path [# node .path ],
2326 }
2427 else
@@ -34,7 +37,7 @@ local node_to_item = function(node, prefix)
3437end
3538
3639--- @param node wk.Node
37- --- @param prefix string
40+ --- @param prefix string ?
3841--- @return MenuItem[] ?
3942walk = function (node , prefix )
4043 --- @type MenuItem[]
@@ -43,7 +46,7 @@ walk = function(node, prefix)
4346 if children then
4447 items = {}
4548 for _ , child in pairs (children ) do
46- if child and child .keys and string_startswith (child .keys , prefix ) then
49+ if child and child .keys and ( not prefix or prefix == " " or string_startswith (child .keys , prefix ) ) then
4750 table.insert (items , node_to_item (child , prefix ))
4851 end
4952 end
@@ -54,9 +57,13 @@ walk = function(node, prefix)
5457 return items
5558end
5659
57- local root = require (" which-key.buf" ).get ({ mode = " n" }).tree .root
58- -- print(vim.inspect(root))
59- local prefix = vim .g .mapleader :gsub (" " , " <Space>" )
60- local menu = walk (root , prefix )
61- -- print(vim.inspect(menu))
62- return menu and next (menu ) and menu [1 ].items or nil
60+ --- @param prefix string ?
61+ --- @param mode string ?
62+ return function (prefix , mode )
63+ local root = require (" which-key.buf" ).get ({ mode = mode or " n" }).tree .root
64+ -- print(vim.inspect(root))
65+ prefix = prefix and vim .g .mapleader :gsub (prefix , " <Space>" )
66+ local items = walk (root , prefix )
67+ -- print(vim.inspect(menu))
68+ return items and next (items ) and items or nil
69+ end
0 commit comments