Skip to content

Commit 0d11148

Browse files
committed
feat: help closes on <Esc> and api.tree.toggle_help mappings
1 parent cab0e7e commit 0d11148

File tree

1 file changed

+14
-10
lines changed

1 file changed

+14
-10
lines changed

lua/nvim-tree/help.lua

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -80,18 +80,19 @@ local function sort_lhs(a, b)
8080
end
8181

8282
--- Compute all lines for the buffer
83+
---@param map table keymap.get_keymap
8384
---@return table strings of text
8485
---@return table arrays of arguments 3-6 for nvim_buf_add_highlight()
8586
---@return number maximum length of text
86-
local function compute()
87+
local function compute(map)
8788
local head_lhs = "nvim-tree mappings"
8889
local head_rhs1 = "exit: q"
8990
local head_rhs2 = string.format("sort by %s: s", M.config.sort_by == "key" and "description" or "keymap")
9091

9192
-- formatted lhs and desc from active keymap
92-
local mappings = vim.tbl_map(function(map)
93-
return { lhs = tidy_lhs(map.lhs), desc = tidy_desc(map.desc) }
94-
end, keymap.get_keymap())
93+
local mappings = vim.tbl_map(function(m)
94+
return { lhs = tidy_lhs(m.lhs), desc = tidy_desc(m.desc) }
95+
end, map)
9596

9697
-- sorter function for mappings
9798
local sort_fn
@@ -166,8 +167,11 @@ local function open()
166167
-- close existing, shouldn't be necessary
167168
close()
168169

170+
-- fetch all mappings
171+
local map = keymap.get_keymap()
172+
169173
-- text and highlight
170-
local lines, hl, width = compute()
174+
local lines, hl, width = compute(map)
171175

172176
-- create the buffer
173177
M.bufnr = vim.api.nvim_create_buf(false, true)
@@ -208,20 +212,20 @@ local function open()
208212
end
209213

210214
-- hardcoded
211-
local keymaps = {
215+
local help_keymaps = {
212216
q = { fn = close, desc = "nvim-tree: exit help" },
213217
["<Esc>"] = { fn = close, desc = "nvim-tree: exit help" }, -- hidden
214218
s = { fn = toggle_sort, desc = "nvim-tree: toggle sorting method" },
215219
}
216220

217221
-- api help binding closes
218-
for _, map in ipairs(keymap.get_keymap()) do
219-
if map.callback == api.tree.toggle_help then
220-
keymaps[map.lhs] = { fn = close, desc = "nvim-tree: exit help" }
222+
for _, m in ipairs(map) do
223+
if m.callback == api.tree.toggle_help then
224+
help_keymaps[m.lhs] = { fn = close, desc = "nvim-tree: exit help" }
221225
end
222226
end
223227

224-
for k, v in pairs(keymaps) do
228+
for k, v in pairs(help_keymaps) do
225229
vim.keymap.set("n", k, v.fn, {
226230
desc = v.desc,
227231
buffer = M.bufnr,

0 commit comments

Comments
 (0)