chore: move help rendering in its own file

This commit is contained in:
kiyan 2021-10-10 11:48:02 +02:00
parent 94b8604e86
commit 86e6dc627e
3 changed files with 55 additions and 50 deletions

View File

@ -107,7 +107,7 @@ function M.get_node_at_cursor()
local cursor = api.nvim_win_get_cursor(view.get_winnr())
local line = cursor[1]
if view.is_help_ui() then
local help_lines, _ = renderer.draw_help()
local help_lines = require'nvim-tree.renderer.help'.compute_lines()
local help_text = get_node_at_line(line+1)(help_lines)
return {name = help_text}
else

View File

@ -0,0 +1,52 @@
local view = require'nvim-tree.view'
local M = {}
function M.compute_lines()
local help_lines = {'HELP'}
local help_hl = {{'NvimTreeRootFolder', 0, 0, #help_lines[1]}}
local mappings = vim.tbl_filter(function(v)
return v.cb ~= nil and v.cb ~= ""
end, view.View.mappings)
local processed = {}
for _, b in pairs(mappings) do
local cb = b.cb
local key = b.key
local name
if cb:sub(1,35) == view.nvim_tree_callback('test'):sub(1,35) then
name = cb:match("'[^']+'[^']*$")
name = name:match("'[^']+'")
table.insert(processed, {key, name, true})
else
name = (b.name ~= nil) and b.name or cb
name = '"' .. name .. '"'
table.insert(processed, {key, name, 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 num = 0
for _, val in pairs(processed) do
local keys = type(val[1]) == "string" and {val[1]} or val[1]
local map_name = val[2]
local builtin = val[3]
for _, key in pairs(keys) do
num = num + 1
local bind_string = string.format("%6s : %s", key, map_name)
table.insert(help_lines, bind_string)
local hl_len = math.max(6, string.len(key)) + 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
end
return help_lines, help_hl
end
return M

View File

@ -2,6 +2,7 @@ local config = require'nvim-tree.config'
local utils = require'nvim-tree.utils'
local view = require'nvim-tree.view'
local _padding = require'nvim-tree.renderer.padding'
local _help = require'nvim-tree.renderer.help'
local api = vim.api
@ -359,54 +360,6 @@ end
local M = {}
function M.draw_help()
local help_lines = {'HELP'}
local help_hl = {{'NvimTreeRootFolder', 0, 0, #help_lines[1]}}
local mappings = vim.tbl_filter(function(v)
return v.cb ~= nil and v.cb ~= ""
end, view.View.mappings)
local processed = {}
for _, b in pairs(mappings) do
local cb = b.cb
local key = b.key
local name
if cb:sub(1,35) == view.nvim_tree_callback('test'):sub(1,35) then
name = cb:match("'[^']+'[^']*$")
name = name:match("'[^']+'")
table.insert(processed, {key, name, true})
else
name = (b.name ~= nil) and b.name or cb
name = '"' .. name .. '"'
table.insert(processed, {key, name, 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 num = 0
for _, val in pairs(processed) do
local keys = type(val[1]) == "string" and {val[1]} or val[1]
local map_name = val[2]
local builtin = val[3]
for _, key in pairs(keys) do
num = num + 1
local bind_string = string.format("%6s : %s", key, map_name)
table.insert(help_lines, bind_string)
local hl_len = math.max(6, string.len(key)) + 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
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
@ -425,7 +378,7 @@ function M.draw(tree, reload)
end
if view.is_help_ui() then
lines, hl = M.draw_help()
lines, hl = _help.compute_lines()
end
api.nvim_buf_set_option(view.View.bufnr, 'modifiable', true)
api.nvim_buf_set_lines(view.View.bufnr, 0, -1, false, lines)