Skip to content

Commit d9235f8

Browse files
committed
fix which-key menu, add which-key menu from leader and make it more lazy loaded
1 parent ad0a269 commit d9235f8

3 files changed

Lines changed: 32 additions & 15 deletions

File tree

lua/menu/init.lua

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ M.open = function(items, opts)
3333
}
3434
end
3535

36-
local items_was = type(items)
36+
local items_was = items
3737
if type(items) == "function" then
3838
items = items()
3939
end
@@ -46,8 +46,10 @@ M.open = function(items, opts)
4646
assert(
4747
type(items) == "table",
4848
"Items has to be a table."
49-
.. " items_was="
50-
.. items_was
49+
.. " type(items_was)="
50+
.. type(items_was)
51+
.. " debug.getinfo(items_was)="
52+
.. vim.inspect(type(items_was) == "function" and debug.getinfo(items_was))
5153
.. " type(items)="
5254
.. type(items)
5355
.. " vim.inspect(items)="

lua/menus/default.lua

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,10 +101,18 @@ local items = {
101101
local ok = require "which-key"
102102
if ok then
103103
insertafter(items, "Lsp Actions", {
104-
name = " Which-key",
104+
name = " Which-key from leader",
105105
hl = "Exblue",
106106
items = function()
107-
return require "menus.which-key"
107+
local subitems = require "menus.which-key"(vim.g.mapleader)[1].items
108+
return type(subitems) == "function" and subitems() or subitems
109+
end,
110+
})
111+
insertafter(items, "Lsp Actions", {
112+
name = " Which-key all keys",
113+
hl = "Exblue",
114+
items = function()
115+
return require "menus.which-key"()
108116
end,
109117
})
110118
end

lua/menus/which-key.lua

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ end
88
local walk
99

1010
---@param node wk.Node
11-
---@param prefix string
11+
---@param prefix string?
1212
---@return MenuItem[]?
1313
local 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)
3437
end
3538

3639
---@param node wk.Node
37-
---@param prefix string
40+
---@param prefix string?
3841
---@return MenuItem[]?
3942
walk = 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
5558
end
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

Comments
 (0)