feat: Help UI for keybindings (#470)

This commit is contained in:
Luke Kershaw 2021-06-28 18:38:05 +01:00 committed by GitHub
parent ce66b688ff
commit a06f949607
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 87 additions and 12 deletions

View File

@ -68,6 +68,7 @@ local keypress_funcs = {
parent_node = lib.parent_node,
toggle_ignored = lib.toggle_ignored,
toggle_dotfiles = lib.toggle_dotfiles,
toggle_help = lib.toggle_help,
refresh = lib.refresh_tree,
first_sibling = function(node) lib.sibling(node, -math.huge) end,
last_sibling = function(node) lib.sibling(node, math.huge) end,
@ -84,6 +85,7 @@ local keypress_funcs = {
}
function M.on_keypress(mode)
if view.is_help_ui() and mode ~= 'toggle_help' then return end
local node = lib.get_node_at_cursor()
if not node then return end

View File

@ -95,14 +95,20 @@ end
function M.get_node_at_cursor()
local cursor = api.nvim_win_get_cursor(view.get_winnr())
local line = cursor[1]
if line == 1 and M.Tree.cwd ~= "/" then
return { name = ".." }
end
if view.is_help_ui() then
local help_lines, _ = renderer.draw_help()
local help_text = get_node_at_line(line+1)(help_lines)
return {name = help_text}
else
if line == 1 and M.Tree.cwd ~= "/" then
return { name = ".." }
end
if M.Tree.cwd == "/" then
line = line + 1
if M.Tree.cwd == "/" then
line = line + 1
end
return get_node_at_line(line)(M.Tree.entries)
end
return get_node_at_line(line)(M.Tree.entries)
end
-- If node is grouped, return the last node in the group. Otherwise, return the given node.
@ -223,7 +229,7 @@ end
function M.pick_window()
local tabpage = api.nvim_get_current_tabpage()
local win_ids = api.nvim_tabpage_list_wins(tabpage)
local tree_winid = view.View.tabpages[tabpage]
local tree_winid = view.get_winnr(tabpage)
local exclude = config.window_picker_exclude()
local selectable = vim.tbl_filter(function (id)
@ -423,7 +429,7 @@ end
function M.set_target_win()
local id = api.nvim_get_current_win()
local tree_id = view.View.tabpages[api.nvim_get_current_tabpage()]
local tree_id = view.get_winnr()
if tree_id and id == tree_id then
M.Tree.target_winid = 0
return
@ -517,6 +523,11 @@ function M.toggle_dotfiles()
return M.refresh_tree()
end
function M.toggle_help()
view.toggle_help()
return M.refresh_tree()
end
function M.dir_up(node)
if not node or node.name == ".." then
return M.change_dir('..')

View File

@ -366,6 +366,40 @@ end
local M = {}
function M.draw_help()
local help_lines = {'HELP'}
local help_hl = {{'NvimTreeRootFolder', 0, 0, string.len('HELP')}}
local bindings = view.View.bindings
local processed = {}
for i, v in pairs(bindings) do
if v:sub(1,35) == view.nvim_tree_callback('test'):sub(1,35) then
v = v:match("'[^']+'[^']*$")
v = v:match("'[^']+'")
table.insert(processed,{i,v,true})
else
v = '"' .. v .. '"'
table.insert(processed,{i,v,false})
end
end
table.sort(processed,function(a,b)
return (a[3]==b[3] and (a[2]<b[2] or (a[2]==b[2] and #a[1]<#b[1]))) or (a[3] and not b[3])
end)
local i, v, builtin
for num, val in pairs(processed) do
i = val[1]
v = val[2]
builtin = val[3]
local bind_string = string.format("%6s : %s",i,v)
table.insert(help_lines,bind_string)
local hl_len = math.max(6,#i)+2
table.insert(help_hl,{'NvimTreeFolderName', num, 0, hl_len})
if not builtin then
table.insert(help_hl,{'NvimTreeFileRenamed', num, hl_len, -1})
end
end
return help_lines, help_hl
end
function M.draw(tree, reload)
if not api.nvim_buf_is_loaded(view.View.bufnr) then return end
local cursor
@ -381,6 +415,9 @@ function M.draw(tree, reload)
update_draw_data(tree, show_arrows and 2 or 0, {})
end
if view.is_help_ui() then
lines, hl = M.draw_help()
end
api.nvim_buf_set_option(view.View.bufnr, 'modifiable', true)
api.nvim_buf_set_lines(view.View.bufnr, 0, -1, false, lines)
M.render_hl(view.View.bufnr)

View File

@ -75,6 +75,7 @@ M.View = {
["]c"] = M.nvim_tree_callback("next_git_item"),
["-"] = M.nvim_tree_callback("dir_up"),
["q"] = M.nvim_tree_callback("close"),
["?"] = M.nvim_tree_callback("toggle_help")
}
}
@ -171,7 +172,7 @@ end
function M.win_open(opts)
if opts and opts.any_tabpage then
for _, v in pairs(M.View.tabpages) do
if a.nvim_win_is_valid(v) then
if a.nvim_win_is_valid(v.winnr) then
return true
end
end
@ -226,7 +227,8 @@ function M.open()
a.nvim_command("wincmd "..move_to)
a.nvim_command("vertical resize "..M.View.width)
local winnr = a.nvim_get_current_win()
M.View.tabpages[a.nvim_get_current_tabpage()] = winnr
local tabpage = a.nvim_get_current_tabpage()
M.View.tabpages[tabpage] = vim.tbl_extend("force", M.View.tabpages[tabpage] or {help = false}, {winnr = winnr})
for k, v in pairs(M.View.winopts) do
vim.wo[winnr][k] = v
end
@ -249,8 +251,31 @@ function M.close()
a.nvim_win_hide(M.get_winnr())
end
function M.get_winnr()
return M.View.tabpages[a.nvim_get_current_tabpage()]
--- Returns the window number for nvim-tree within the tabpage specified
---@param tabpage number: (optional) the number of the chosen tabpage. Defaults to current tabpage.
---@return number
function M.get_winnr(tabpage)
tabpage = tabpage or a.nvim_get_current_tabpage()
local tabinfo = M.View.tabpages[tabpage]
if tabinfo ~= nil then
return tabinfo.winnr
end
end
--- Checks if nvim-tree is displaying the help ui within the tabpage specified
---@param tabpage number: (optional) the number of the chosen tabpage. Defaults to current tabpage.
---@return number
function M.is_help_ui(tabpage)
tabpage = tabpage or a.nvim_get_current_tabpage()
local tabinfo = M.View.tabpages[tabpage]
if tabinfo ~= nil then
return tabinfo.help
end
end
function M.toggle_help(tabpage)
tabpage = tabpage or a.nvim_get_current_tabpage()
M.View.tabpages[tabpage].help = not M.View.tabpages[tabpage].help
end
return M