|
1 | 1 | local keymap = require "nvim-tree.keymap"
|
| 2 | +local api = {} -- circular dependency |
2 | 3 |
|
3 | 4 | local PAT_MOUSE = "^<.*Mouse"
|
4 | 5 | local PAT_CTRL = "^<C%-"
|
@@ -79,18 +80,19 @@ local function sort_lhs(a, b)
|
79 | 80 | end
|
80 | 81 |
|
81 | 82 | --- Compute all lines for the buffer
|
| 83 | +---@param map table keymap.get_keymap |
82 | 84 | ---@return table strings of text
|
83 | 85 | ---@return table arrays of arguments 3-6 for nvim_buf_add_highlight()
|
84 | 86 | ---@return number maximum length of text
|
85 |
| -local function compute() |
| 87 | +local function compute(map) |
86 | 88 | local head_lhs = "nvim-tree mappings"
|
87 | 89 | local head_rhs1 = "exit: q"
|
88 | 90 | local head_rhs2 = string.format("sort by %s: s", M.config.sort_by == "key" and "description" or "keymap")
|
89 | 91 |
|
90 | 92 | -- 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) |
94 | 96 |
|
95 | 97 | -- sorter function for mappings
|
96 | 98 | local sort_fn
|
@@ -165,8 +167,11 @@ local function open()
|
165 | 167 | -- close existing, shouldn't be necessary
|
166 | 168 | close()
|
167 | 169 |
|
| 170 | + -- fetch all mappings |
| 171 | + local map = keymap.get_keymap() |
| 172 | + |
168 | 173 | -- text and highlight
|
169 |
| - local lines, hl, width = compute() |
| 174 | + local lines, hl, width = compute(map) |
170 | 175 |
|
171 | 176 | -- create the buffer
|
172 | 177 | M.bufnr = vim.api.nvim_create_buf(false, true)
|
@@ -206,12 +211,21 @@ local function open()
|
206 | 211 | open()
|
207 | 212 | end
|
208 | 213 |
|
209 |
| - local keymaps = { |
| 214 | + -- hardcoded |
| 215 | + local help_keymaps = { |
210 | 216 | q = { fn = close, desc = "nvim-tree: exit help" },
|
| 217 | + ["<Esc>"] = { fn = close, desc = "nvim-tree: exit help" }, -- hidden |
211 | 218 | s = { fn = toggle_sort, desc = "nvim-tree: toggle sorting method" },
|
212 | 219 | }
|
213 | 220 |
|
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 |
215 | 229 | vim.keymap.set("n", k, v.fn, {
|
216 | 230 | desc = v.desc,
|
217 | 231 | buffer = M.bufnr,
|
|
240 | 254 | function M.setup(opts)
|
241 | 255 | M.config.cursorline = opts.view.cursorline
|
242 | 256 | M.config.sort_by = opts.help.sort_by
|
| 257 | + |
| 258 | + api = require "nvim-tree.api" |
243 | 259 | end
|
244 | 260 |
|
245 | 261 | return M
|
0 commit comments