fix(win): use correct keys for displaying help. Closes #1364

This commit is contained in:
Folke Lemaitre 2025-02-22 08:04:01 +01:00
parent 13ea94d561
commit b100c93717
No known key found for this signature in database
GPG key ID: 41F8B1FBACAE2040

View file

@ -376,15 +376,29 @@ function M:toggle_help(opts)
win:close() win:close()
end, { buf = true }) end, { buf = true })
local dim = win:dim() local dim = win:dim()
local cols = math.floor((dim.width - 1) / col_width)
local rows = math.ceil(#self.keys / cols) -- NOTE: we use the actual buffer keymaps instead of self.keys,
win.opts.height = rows -- since we want to show all keymaps, not just the ones we've defined on the window
local keys = {} ---@type vim.api.keyset.get_keymap[] local keys = {} ---@type vim.api.keyset.get_keymap[]
vim.list_extend(keys, vim.api.nvim_buf_get_keymap(self.buf, "n")) vim.list_extend(keys, vim.api.nvim_buf_get_keymap(self.buf, "n"))
vim.list_extend(keys, vim.api.nvim_buf_get_keymap(self.buf, "i")) vim.list_extend(keys, vim.api.nvim_buf_get_keymap(self.buf, "i"))
table.sort(keys, function(a, b) table.sort(keys, function(a, b)
return (a.desc or a.lhs or "") < (b.desc or b.lhs or "") return (a.desc or a.lhs or "") < (b.desc or b.lhs or "")
end) end)
local done = {} ---@type table<string, boolean>
keys = vim.tbl_filter(function(keymap)
local key = Snacks.util.normkey(keymap.lhs or "")
if done[key] or (keymap.desc and keymap.desc:find("which%-key")) then
return false
end
done[key] = true
return true
end, keys)
local cols = math.floor((dim.width - 1) / col_width)
local rows = math.ceil(#keys / cols)
win.opts.height = rows
local help = {} ---@type {[1]:string, [2]:string}[][] local help = {} ---@type {[1]:string, [2]:string}[][]
local row, col = 0, 1 local row, col = 0, 1
@ -399,24 +413,20 @@ function M:toggle_help(opts)
return align == "right" and (string.rep(" ", len - w) .. str) or (str .. string.rep(" ", len - w)) return align == "right" and (string.rep(" ", len - w) .. str) or (str .. string.rep(" ", len - w))
end end
local done = {} ---@type table<string, boolean>
for _, keymap in ipairs(keys) do for _, keymap in ipairs(keys) do
local key = Snacks.util.normkey(keymap.lhs or "") local key = Snacks.util.normkey(keymap.lhs or "")
if not done[key] and not (keymap.desc and keymap.desc:find("which%-key")) then row = row + 1
done[key] = true if row > rows then
row = row + 1 row, col = 1, col + 1
if row > rows then
row, col = 1, col + 1
end
help[row] = help[row] or {}
vim.list_extend(help[row], {
{ trunc(key, key_width, "right"), "SnacksWinKey" },
{ " " },
{ "", "SnacksWinKeySep" },
{ " " },
{ trunc(keymap.desc or "", col_width - key_width - 3), "SnacksWinKeyDesc" },
})
end end
help[row] = help[row] or {}
vim.list_extend(help[row], {
{ trunc(key, key_width, "right"), "SnacksWinKey" },
{ " " },
{ "", "SnacksWinKeySep" },
{ " " },
{ trunc(keymap.desc or "", col_width - key_width - 3), "SnacksWinKeyDesc" },
})
end end
win:show() win:show()
for l, line in ipairs(help) do for l, line in ipairs(help) do