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
This commit is contained in:
parent
d41b4ca013
commit
b652dbd0e0
@ -1,4 +1,5 @@
|
|||||||
local keymap = require "nvim-tree.keymap"
|
local keymap = require "nvim-tree.keymap"
|
||||||
|
local api = {} -- circular dependency
|
||||||
|
|
||||||
local PAT_MOUSE = "^<.*Mouse"
|
local PAT_MOUSE = "^<.*Mouse"
|
||||||
local PAT_CTRL = "^<C%-"
|
local PAT_CTRL = "^<C%-"
|
||||||
@ -79,18 +80,19 @@ local function sort_lhs(a, b)
|
|||||||
end
|
end
|
||||||
|
|
||||||
--- Compute all lines for the buffer
|
--- Compute all lines for the buffer
|
||||||
|
---@param map table keymap.get_keymap
|
||||||
---@return table strings of text
|
---@return table strings of text
|
||||||
---@return table arrays of arguments 3-6 for nvim_buf_add_highlight()
|
---@return table arrays of arguments 3-6 for nvim_buf_add_highlight()
|
||||||
---@return number maximum length of text
|
---@return number maximum length of text
|
||||||
local function compute()
|
local function compute(map)
|
||||||
local head_lhs = "nvim-tree mappings"
|
local head_lhs = "nvim-tree mappings"
|
||||||
local head_rhs1 = "exit: q"
|
local head_rhs1 = "exit: q"
|
||||||
local head_rhs2 = string.format("sort by %s: s", M.config.sort_by == "key" and "description" or "keymap")
|
local head_rhs2 = string.format("sort by %s: s", M.config.sort_by == "key" and "description" or "keymap")
|
||||||
|
|
||||||
-- formatted lhs and desc from active keymap
|
-- formatted lhs and desc from active keymap
|
||||||
local mappings = vim.tbl_map(function(map)
|
local mappings = vim.tbl_map(function(m)
|
||||||
return { lhs = tidy_lhs(map.lhs), desc = tidy_desc(map.desc) }
|
return { lhs = tidy_lhs(m.lhs), desc = tidy_desc(m.desc) }
|
||||||
end, keymap.get_keymap())
|
end, map)
|
||||||
|
|
||||||
-- sorter function for mappings
|
-- sorter function for mappings
|
||||||
local sort_fn
|
local sort_fn
|
||||||
@ -165,8 +167,11 @@ local function open()
|
|||||||
-- close existing, shouldn't be necessary
|
-- close existing, shouldn't be necessary
|
||||||
close()
|
close()
|
||||||
|
|
||||||
|
-- fetch all mappings
|
||||||
|
local map = keymap.get_keymap()
|
||||||
|
|
||||||
-- text and highlight
|
-- text and highlight
|
||||||
local lines, hl, width = compute()
|
local lines, hl, width = compute(map)
|
||||||
|
|
||||||
-- create the buffer
|
-- create the buffer
|
||||||
M.bufnr = vim.api.nvim_create_buf(false, true)
|
M.bufnr = vim.api.nvim_create_buf(false, true)
|
||||||
@ -206,12 +211,21 @@ local function open()
|
|||||||
open()
|
open()
|
||||||
end
|
end
|
||||||
|
|
||||||
local keymaps = {
|
-- hardcoded
|
||||||
|
local help_keymaps = {
|
||||||
q = { fn = close, desc = "nvim-tree: exit help" },
|
q = { fn = close, desc = "nvim-tree: exit help" },
|
||||||
|
["<Esc>"] = { fn = close, desc = "nvim-tree: exit help" }, -- hidden
|
||||||
s = { fn = toggle_sort, desc = "nvim-tree: toggle sorting method" },
|
s = { fn = toggle_sort, desc = "nvim-tree: toggle sorting method" },
|
||||||
}
|
}
|
||||||
|
|
||||||
for k, v in pairs(keymaps) do
|
-- api help binding closes
|
||||||
|
for _, m in ipairs(map) do
|
||||||
|
if m.callback == api.tree.toggle_help then
|
||||||
|
help_keymaps[m.lhs] = { fn = close, desc = "nvim-tree: exit help" }
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
for k, v in pairs(help_keymaps) do
|
||||||
vim.keymap.set("n", k, v.fn, {
|
vim.keymap.set("n", k, v.fn, {
|
||||||
desc = v.desc,
|
desc = v.desc,
|
||||||
buffer = M.bufnr,
|
buffer = M.bufnr,
|
||||||
@ -240,6 +254,8 @@ end
|
|||||||
function M.setup(opts)
|
function M.setup(opts)
|
||||||
M.config.cursorline = opts.view.cursorline
|
M.config.cursorline = opts.view.cursorline
|
||||||
M.config.sort_by = opts.help.sort_by
|
M.config.sort_by = opts.help.sort_by
|
||||||
|
|
||||||
|
api = require "nvim-tree.api"
|
||||||
end
|
end
|
||||||
|
|
||||||
return M
|
return M
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user