Skip to content

Commit 86e6dc6

Browse files
committed
chore: move help rendering in its own file
1 parent 94b8604 commit 86e6dc6

File tree

3 files changed

+55
-50
lines changed

3 files changed

+55
-50
lines changed

lua/nvim-tree/lib.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ function M.get_node_at_cursor()
107107
local cursor = api.nvim_win_get_cursor(view.get_winnr())
108108
local line = cursor[1]
109109
if view.is_help_ui() then
110-
local help_lines, _ = renderer.draw_help()
110+
local help_lines = require'nvim-tree.renderer.help'.compute_lines()
111111
local help_text = get_node_at_line(line+1)(help_lines)
112112
return {name = help_text}
113113
else

lua/nvim-tree/renderer/help.lua

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
local view = require'nvim-tree.view'
2+
local M = {}
3+
4+
function M.compute_lines()
5+
local help_lines = {'HELP'}
6+
local help_hl = {{'NvimTreeRootFolder', 0, 0, #help_lines[1]}}
7+
local mappings = vim.tbl_filter(function(v)
8+
return v.cb ~= nil and v.cb ~= ""
9+
end, view.View.mappings)
10+
local processed = {}
11+
for _, b in pairs(mappings) do
12+
local cb = b.cb
13+
local key = b.key
14+
local name
15+
if cb:sub(1,35) == view.nvim_tree_callback('test'):sub(1,35) then
16+
name = cb:match("'[^']+'[^']*$")
17+
name = name:match("'[^']+'")
18+
table.insert(processed, {key, name, true})
19+
else
20+
name = (b.name ~= nil) and b.name or cb
21+
name = '"' .. name .. '"'
22+
table.insert(processed, {key, name, false})
23+
end
24+
end
25+
table.sort(processed, function(a,b)
26+
return (a[3] == b[3]
27+
and (a[2] < b[2] or (a[2] == b[2] and #a[1] < #b[1])))
28+
or (a[3] and not b[3])
29+
end)
30+
31+
local num = 0
32+
for _, val in pairs(processed) do
33+
local keys = type(val[1]) == "string" and {val[1]} or val[1]
34+
local map_name = val[2]
35+
local builtin = val[3]
36+
for _, key in pairs(keys) do
37+
num = num + 1
38+
local bind_string = string.format("%6s : %s", key, map_name)
39+
table.insert(help_lines, bind_string)
40+
41+
local hl_len = math.max(6, string.len(key)) + 2
42+
table.insert(help_hl, {'NvimTreeFolderName', num, 0, hl_len})
43+
44+
if not builtin then
45+
table.insert(help_hl, {'NvimTreeFileRenamed', num, hl_len, -1})
46+
end
47+
end
48+
end
49+
return help_lines, help_hl
50+
end
51+
52+
return M

lua/nvim-tree/renderer/init.lua

Lines changed: 2 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ local config = require'nvim-tree.config'
22
local utils = require'nvim-tree.utils'
33
local view = require'nvim-tree.view'
44
local _padding = require'nvim-tree.renderer.padding'
5+
local _help = require'nvim-tree.renderer.help'
56

67
local api = vim.api
78

@@ -359,54 +360,6 @@ end
359360

360361
local M = {}
361362

362-
function M.draw_help()
363-
local help_lines = {'HELP'}
364-
local help_hl = {{'NvimTreeRootFolder', 0, 0, #help_lines[1]}}
365-
local mappings = vim.tbl_filter(function(v)
366-
return v.cb ~= nil and v.cb ~= ""
367-
end, view.View.mappings)
368-
local processed = {}
369-
for _, b in pairs(mappings) do
370-
local cb = b.cb
371-
local key = b.key
372-
local name
373-
if cb:sub(1,35) == view.nvim_tree_callback('test'):sub(1,35) then
374-
name = cb:match("'[^']+'[^']*$")
375-
name = name:match("'[^']+'")
376-
table.insert(processed, {key, name, true})
377-
else
378-
name = (b.name ~= nil) and b.name or cb
379-
name = '"' .. name .. '"'
380-
table.insert(processed, {key, name, false})
381-
end
382-
end
383-
table.sort(processed, function(a,b)
384-
return (a[3] == b[3]
385-
and (a[2] < b[2] or (a[2] == b[2] and #a[1] < #b[1])))
386-
or (a[3] and not b[3])
387-
end)
388-
389-
local num = 0
390-
for _, val in pairs(processed) do
391-
local keys = type(val[1]) == "string" and {val[1]} or val[1]
392-
local map_name = val[2]
393-
local builtin = val[3]
394-
for _, key in pairs(keys) do
395-
num = num + 1
396-
local bind_string = string.format("%6s : %s", key, map_name)
397-
table.insert(help_lines, bind_string)
398-
399-
local hl_len = math.max(6, string.len(key)) + 2
400-
table.insert(help_hl, {'NvimTreeFolderName', num, 0, hl_len})
401-
402-
if not builtin then
403-
table.insert(help_hl, {'NvimTreeFileRenamed', num, hl_len, -1})
404-
end
405-
end
406-
end
407-
return help_lines, help_hl
408-
end
409-
410363
function M.draw(tree, reload)
411364
if not api.nvim_buf_is_loaded(view.View.bufnr) then return end
412365
local cursor
@@ -425,7 +378,7 @@ function M.draw(tree, reload)
425378
end
426379

427380
if view.is_help_ui() then
428-
lines, hl = M.draw_help()
381+
lines, hl = _help.compute_lines()
429382
end
430383
api.nvim_buf_set_option(view.View.bufnr, 'modifiable', true)
431384
api.nvim_buf_set_lines(view.View.bufnr, 0, -1, false, lines)

0 commit comments

Comments
 (0)