Skip to content

Commit

Permalink
feat: fix hidden and empty groups
Browse files Browse the repository at this point in the history
  • Loading branch information
folke committed Jul 12, 2024
1 parent aef2e53 commit afc4aa9
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 3 deletions.
1 change: 1 addition & 0 deletions lua/which-key/buf.lua
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ function Mode:update()
end

self.tree:add(mappings --[[@as wk.Keymap[] ]])
self.tree:fix()
self:attach()
require("which-key.state").update()
end
Expand Down
7 changes: 6 additions & 1 deletion lua/which-key/mappings.lua
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,10 @@ local wkargs = {
"plugin",
"buffer",
"remap",
"hidden",
"cmd",
"name",
"hidden",
"group",
"preset",
"cond",
Expand Down Expand Up @@ -154,7 +156,7 @@ function M._parse(value, mappings, opts)
error("Incorrect mapping " .. vim.inspect(list))
end

if opts.desc or opts.group then
if opts.desc or opts.group or opts.hidden ~= nil then
if type(opts.mode) == "table" then
for _, mode in pairs(opts.mode) do
local mode_opts = vim.deepcopy(opts)
Expand Down Expand Up @@ -185,6 +187,9 @@ function M.to_mapping(mapping)

mapping.mode = mapping.mode or "n"
mapping.desc = mapping.desc or mapping.name
if mapping.desc == "which_key_ignore" then
mapping.hidden = true
end
mapping.name = nil

return mapping
Expand Down
36 changes: 34 additions & 2 deletions lua/which-key/tree.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ local Util = require("which-key.util")
---@field desc? string
---@field plugin? string
---@field keymap? wk.Keymap
---@field virtual? wk.Keymap
---@field children? table<string, wk.Node>
---@field action? fun()

Expand Down Expand Up @@ -55,7 +56,9 @@ function M:_add(keymap)
end
node.desc = keymap.desc or node.desc
node.plugin = node.plugin or keymap.plugin
if not keymap.virtual then
if keymap.virtual then
node.virtual = keymap
else
node.keymap = keymap
end
-- node.keymap = not keymap.group and keymap or nil
Expand All @@ -67,12 +70,41 @@ end
---@param keymaps wk.Keymap[]
function M:add(keymaps)
for _, keymap in ipairs(keymaps) do
if keymap.lhs:sub(1, 6) ~= "<Plug>" and keymap.desc ~= "which_key_ignore" then
if keymap.lhs:sub(1, 6) ~= "<Plug>" then
self:_add(keymap)
end
end
end

---@param node wk.Node
function M:del(node)
if node == self.root then
return self:clear()
end
local parent = node.parent
assert(parent, "node has no parent")
parent.children[node.key] = nil
if not self:keep(parent) then
self:del(parent)
end
end

---@param node wk.Node
function M:keep(node)
if node.keymap and node.keymap == "which_key_ignore" or node.virtual and node.virtual.hidden then
return false
end
return node.plugin or node.keymap or M.is_group(node) or (node.virtual and not node.virtual.group)
end

function M:fix()
self:walk(function(node)
if not self:keep(node) then
self:del(node)
end
end)
end

---@param keys string|string[]
---@return wk.Node?
function M:find(keys)
Expand Down
1 change: 1 addition & 0 deletions lua/which-key/types.lua
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
---@field plugin? string
---@field group? boolean
---@field virtual? boolean
---@field hidden? boolean

---@class MappingOptions
---@field noremap boolean
Expand Down

0 comments on commit afc4aa9

Please sign in to comment.