Skip to content

Commit 506d2e7

Browse files
committed
chore/fix: renderer module initializations
- put renderer into its own folder, extract the padding logic to make it reloadable. Will allow small refactorings of the rendering logic to make it easier to extend. - get the icon state before each renderer reload
1 parent db547dc commit 506d2e7

File tree

2 files changed

+51
-35
lines changed

2 files changed

+51
-35
lines changed

lua/nvim-tree/renderer.lua renamed to lua/nvim-tree/renderer/init.lua

Lines changed: 4 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
local config = require'nvim-tree.config'
22
local utils = require'nvim-tree.utils'
33
local view = require'nvim-tree.view'
4+
local _padding = require'nvim-tree.renderer.padding'
45

56
local api = vim.api
67

@@ -241,40 +242,6 @@ if icon_state.show_git_icon then
241242
end
242243
end
243244

244-
local get_padding = function(depth)
245-
return string.rep(' ', depth)
246-
end
247-
248-
if icon_state.show_folder_icon and icon_state.show_folder_arrows then
249-
get_padding = function(depth, _, _, node)
250-
if node.entries then
251-
local icon = icon_state.icons.folder_icons[node.open and 'arrow_open' or 'arrow_closed']
252-
return string.rep(' ', depth - 2)..icon..' '
253-
end
254-
return string.rep(' ', depth)
255-
end
256-
end
257-
258-
if vim.g.nvim_tree_indent_markers == 1 then
259-
get_padding = function(depth, idx, tree, _, markers)
260-
local padding = ""
261-
if depth ~= 0 then
262-
local rdepth = depth/2
263-
markers[rdepth] = idx ~= #tree.entries
264-
for i=1,rdepth do
265-
if idx == #tree.entries and i == rdepth then
266-
padding = padding..''
267-
elseif markers[i] then
268-
padding = padding..''
269-
else
270-
padding = padding..' '
271-
end
272-
end
273-
end
274-
return padding
275-
end
276-
end
277-
278245
local picture = {
279246
jpg = true,
280247
jpeg = true,
@@ -303,7 +270,7 @@ local function update_draw_data(tree, depth, markers)
303270
end
304271

305272
for idx, node in ipairs(tree.entries) do
306-
local padding = get_padding(depth, idx, tree, node, markers)
273+
local padding = _padding.get_padding(depth, idx, tree, node, markers)
307274
local offset = string.len(padding)
308275
if depth > 0 then
309276
table.insert(hl, { 'NvimTreeIndentMarker', index, 0, offset })
@@ -443,6 +410,8 @@ function M.draw(tree, reload)
443410
hl = {}
444411

445412
local show_arrows = icon_state.show_folder_icon and icon_state.show_folder_arrows
413+
_padding.reload_padding_function()
414+
icon_state = config.get_icon_state()
446415
update_draw_data(tree, show_arrows and 2 or 0, {})
447416
end
448417

lua/nvim-tree/renderer/padding.lua

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
local M = {}
2+
3+
function M.get_padding(depth)
4+
return string.rep(' ', depth)
5+
end
6+
7+
local function get_padding_arrows(icon_state)
8+
return function(depth, _, _, node)
9+
if node.entries then
10+
local icon = icon_state.icons.folder_icons[node.open and 'arrow_open' or 'arrow_closed']
11+
return string.rep(' ', depth - 2)..icon..' '
12+
end
13+
return string.rep(' ', depth)
14+
end
15+
end
16+
17+
local function get_padding_indent_markers(depth, idx, tree, _, markers)
18+
local padding = ""
19+
if depth ~= 0 then
20+
local rdepth = depth/2
21+
markers[rdepth] = idx ~= #tree.entries
22+
for i=1,rdepth do
23+
if idx == #tree.entries and i == rdepth then
24+
padding = padding..''
25+
elseif markers[i] then
26+
padding = padding..''
27+
else
28+
padding = padding..' '
29+
end
30+
end
31+
end
32+
return padding
33+
end
34+
35+
function M.reload_padding_function()
36+
local icon_state = require'nvim-tree.config'.get_icon_state()
37+
38+
if icon_state.show_folder_icon and icon_state.show_folder_arrows then
39+
M.get_padding = get_padding_arrows(icon_state)
40+
end
41+
42+
if vim.g.nvim_tree_indent_markers == 1 then
43+
M.get_padding = get_padding_indent_markers
44+
end
45+
end
46+
47+
return M

0 commit comments

Comments
 (0)