Skip to content

Commit b652dbd

Browse files
authored
feat: help closes on <Esc> and api.tree.toggle_help mappings (#2909)
* feat: help closes on <Esc> and api.tree.toggle_help mappings * feat: help closes on <Esc> and api.tree.toggle_help mappings
1 parent d41b4ca commit b652dbd

File tree

1 file changed

+23
-7
lines changed

1 file changed

+23
-7
lines changed

lua/nvim-tree/help.lua

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
local keymap = require "nvim-tree.keymap"
2+
local api = {} -- circular dependency
23

34
local PAT_MOUSE = "^<.*Mouse"
45
local PAT_CTRL = "^<C%-"
@@ -79,18 +80,19 @@ local function sort_lhs(a, b)
7980
end
8081

8182
--- Compute all lines for the buffer
83+
---@param map table keymap.get_keymap
8284
---@return table strings of text
8385
---@return table arrays of arguments 3-6 for nvim_buf_add_highlight()
8486
---@return number maximum length of text
85-
local function compute()
87+
local function compute(map)
8688
local head_lhs = "nvim-tree mappings"
8789
local head_rhs1 = "exit: q"
8890
local head_rhs2 = string.format("sort by %s: s", M.config.sort_by == "key" and "description" or "keymap")
8991

9092
-- formatted lhs and desc from active keymap
91-
local mappings = vim.tbl_map(function(map)
92-
return { lhs = tidy_lhs(map.lhs), desc = tidy_desc(map.desc) }
93-
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)
9496

9597
-- sorter function for mappings
9698
local sort_fn
@@ -165,8 +167,11 @@ local function open()
165167
-- close existing, shouldn't be necessary
166168
close()
167169

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

171176
-- create the buffer
172177
M.bufnr = vim.api.nvim_create_buf(false, true)
@@ -206,12 +211,21 @@ local function open()
206211
open()
207212
end
208213

209-
local keymaps = {
214+
-- hardcoded
215+
local help_keymaps = {
210216
q = { fn = close, desc = "nvim-tree: exit help" },
217+
["<Esc>"] = { fn = close, desc = "nvim-tree: exit help" }, -- hidden
211218
s = { fn = toggle_sort, desc = "nvim-tree: toggle sorting method" },
212219
}
213220

214-
for k, v in pairs(keymaps) do
221+
-- api help binding closes
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" }
225+
end
226+
end
227+
228+
for k, v in pairs(help_keymaps) do
215229
vim.keymap.set("n", k, v.fn, {
216230
desc = v.desc,
217231
buffer = M.bufnr,
@@ -240,6 +254,8 @@ end
240254
function M.setup(opts)
241255
M.config.cursorline = opts.view.cursorline
242256
M.config.sort_by = opts.help.sort_by
257+
258+
api = require "nvim-tree.api"
243259
end
244260

245261
return M

0 commit comments

Comments
 (0)