Skip to content

Commit

Permalink
feat(mappings): added support for setting custom icons from the spec
Browse files Browse the repository at this point in the history
  • Loading branch information
folke committed Jul 12, 2024
1 parent 6f7a945 commit 951ae7a
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 5 deletions.
4 changes: 3 additions & 1 deletion lua/which-key/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ function M.setup(opts)
local wk = require("which-key")
wk.register = M.register
for _, v in ipairs(wk._queue) do
M.register(v.mappings, v.opts)
M.register(v.spec, v.opts)
end
M.register(M.options.spec)
wk._queue = {}
Expand All @@ -157,6 +157,8 @@ function M.setup(opts)
end
end

---@param mappings wk.Spec
---@param opts? wk.Mapping
function M.register(mappings, opts)
local Mappings = require("which-key.mappings")
for _, km in ipairs(Mappings.parse(mappings, opts)) do
Expand Down
6 changes: 4 additions & 2 deletions lua/which-key/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@ function M.show(opts)
require("which-key.state").start(opts)
end

---@type {mappings:table, opts?:table}
---@type {spec:wk.Spec, opts?:wk.Mapping}[]
M._queue = {}

---@param mappings wk.Spec
---@param opts? wk.Mapping
function M.register(mappings, opts)
table.insert(M._queue, { mappings = mappings, opts = opts })
table.insert(M._queue, { spec = mappings, opts = opts })
end

function M.setup(opts)
Expand Down
1 change: 1 addition & 0 deletions lua/which-key/mappings.lua
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ M.fields = {
name = {},
cmd = { transform = "rhs" },
group = {},
icon = { transitive = true },
rhs = {},
lhs = {},
}
Expand Down
2 changes: 1 addition & 1 deletion lua/which-key/tree.lua
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ function M:add(keymap, virtual)
node.desc = keymap.desc or node.desc
node.plugin = node.plugin or keymap.plugin
if virtual then
node.virtual = keymap
node.mapping = keymap
else
node.keymap = keymap
node.global = not (keymap.buffer and keymap.buffer ~= 0)
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 @@ -35,6 +35,7 @@
---@field remap? boolean
---@field hidden? boolean
---@field preset? boolean
---@field icon? wk.Icon

---@class wk.Spec: {[string]: wk.Spec, [number]: wk.Spec} , wk.Mapping
---@field [1]? string|fun()
Expand Down
5 changes: 4 additions & 1 deletion lua/which-key/view.lua
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,10 @@ function M.item(node, opts)
end
desc = M.replace("desc", desc or "")
local icon, icon_hl
if not (node.parent and node.parent.plugin) then
if node.mapping and node.mapping.icon then
icon, icon_hl = Icons.get(node.mapping.icon)
end
if not icon and not (node.parent and node.parent.plugin) then
icon, icon_hl = Icons.get({ keymap = node.keymap, desc = node.desc })
end
local parent_key = opts.parent_key and M.replace("key", opts.parent_key) or ""
Expand Down

0 comments on commit 951ae7a

Please sign in to comment.