Skip to content

Commit

Permalink
perf: no need to create triggers for all levels. first level that is …
Browse files Browse the repository at this point in the history
…not a cmd is enough
  • Loading branch information
folke committed Apr 30, 2021
1 parent b44fc09 commit 3cc0424
Showing 1 changed file with 17 additions and 13 deletions.
30 changes: 17 additions & 13 deletions lua/which-key/keys.lua
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ function M.hook_del(prefix, mode, buf)
end
end

function M.hook_add(prefix, mode, buf)
function M.hook_add(prefix, mode, buf, secret_only)
-- Check if we need to create the hook
if type(Config.options.triggers) == "string" and Config.options.triggers ~= "auto" then
if Util.t(prefix) ~= Util.t(Config.options.triggers) then return end
Expand Down Expand Up @@ -265,10 +265,10 @@ function M.hook_add(prefix, mode, buf)
-- map group triggers and nops
-- nops are needed, so that WhichKey always respects timeoutlen
if buf then
vim.api.nvim_buf_set_keymap(buf, mode, prefix, cmd, opts)
if secret_only ~= true then vim.api.nvim_buf_set_keymap(buf, mode, prefix, cmd, opts) end
vim.api.nvim_buf_set_keymap(buf, mode, prefix .. secret, "<nop>", opts)
else
vim.api.nvim_set_keymap(mode, prefix, cmd, opts)
if secret_only ~= true then vim.api.nvim_set_keymap(mode, prefix, cmd, opts) end
vim.api.nvim_set_keymap(mode, prefix .. secret, "<nop>", opts)
end
M.hooked[id] = true
Expand All @@ -286,20 +286,24 @@ function M.update(buf)
-- 2. this is a global node
-- 3. this is a local buffer node for the passed buffer
M.update_keymaps(tree.mode, tree.buf)
tree.tree:walk( ---@param node Node
function(node)
-- create group mapping if needed
if not node.mapping then
node.mapping = { prefix = node.prefix, group = true, keys = Util.parse_keys(node.prefix) }
end
if node.prefix ~= "" and node.mapping.group == true and not node.mapping.cmd then
M.hook_add(node.prefix, tree.mode, tree.buf)
end
end)
M.add_hooks(tree.mode, tree.buf, tree.tree.root)
end
end
end

---@param node Node
function M.add_hooks(mode, buf, node, secret_only)
if not node.mapping then
node.mapping = { prefix = node.prefix, group = true, keys = Util.parse_keys(node.prefix) }
end
if node.prefix ~= "" and node.mapping.group == true and not node.mapping.cmd then
-- first non-cmd level, so create hook and make all decendents secret only
M.hook_add(node.prefix, mode, buf, secret_only)
secret_only = true
end
for _, child in pairs(node.children) do M.add_hooks(mode, buf, child, secret_only) end
end

function M.dump()
local ok = {}
local todo = {}
Expand Down

0 comments on commit 3cc0424

Please sign in to comment.