Skip to content

Commit 36954ff

Browse files
committed
feat(hidden_display): Simplification and better performance by not sorting and grouping virtual lines
1 parent 8ef7e48 commit 36954ff

File tree

3 files changed

+25
-30
lines changed

3 files changed

+25
-30
lines changed

lua/nvim-tree/renderer/builder.lua

Lines changed: 18 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -358,23 +358,31 @@ function Builder:build_line(node, idx, num_children)
358358
self.depth = self.depth + 1
359359
self:build_lines(node)
360360
self.depth = self.depth - 1
361-
self:add_hidden_count_string(node, idx, num_children)
362361
end
363362
end
364363

364+
---Add virtual lines for rendering hidden count information per node
365365
---@private
366366
function Builder:add_hidden_count_string(node, idx, num_children)
367+
if not node.open then
368+
return
369+
end
367370
local hidden_count_string = M.opts.renderer.hidden_display(node.hidden_count)
368371
if hidden_count_string and hidden_count_string ~= "" then
369-
local indent_markers = pad.get_indent_markers(math.max(self.depth, 0), idx or 0, num_children or 0, node, self.markers)
372+
local indent_markers = pad.get_indent_markers(self.depth, idx or 0, num_children or 0, node, self.markers, 1)
370373
local indent_width = M.opts.renderer.indent_width
371-
local indent_string = string.rep(" ", indent_width) .. (indent_markers.str or "")
372-
table.insert(self.virtual_lines, {
373-
indent_string = indent_string,
374-
depth = self.depth,
375-
line_nr = #self.lines - 1,
376-
-- Remove padding if we're in root
377-
text = (node.parent == nil and "" or string.rep(" ", indent_width)) .. hidden_count_string,
374+
375+
local indent_padding = string.rep(" ", indent_width)
376+
local indent_string = indent_padding .. indent_markers.str
377+
local line_nr = #self.lines - 1
378+
self.virtual_lines[line_nr] = self.virtual_lines[line_nr] or {}
379+
380+
-- NOTE: We are inserting in depth order because of current traversal
381+
-- if we change the traversal, we might need to sort by depth before rendering `self.virtual_lines`
382+
-- to maintain proper ordering of parent and child folder hidden count info.
383+
table.insert(self.virtual_lines[line_nr], {
384+
{ indent_string, indent_markers.hl },
385+
{ string.rep(indent_padding, (node.parent == nil and 0 or 1)) .. hidden_count_string, "NvimTreeHiddenDisplay" },
378386
})
379387
end
380388
end
@@ -408,6 +416,7 @@ function Builder:build_lines(node)
408416
idx = idx + 1
409417
end
410418
end
419+
self:add_hidden_count_string(node)
411420
end
412421

413422
---@private
@@ -458,24 +467,10 @@ end
458467
function Builder:build()
459468
self:build_header()
460469
self:build_lines()
461-
self:build_root_hidden_count()
462470
self:sanitize_lines()
463471
return self
464472
end
465473

466-
--- Add the hidden_count for root, since root dir is treated differently
467-
--- from normal directories we need to do it again for root.
468-
--- Also need to sort by depth
469-
---@private
470-
function Builder:build_root_hidden_count()
471-
local root = core.get_explorer()
472-
self:add_hidden_count_string(root)
473-
-- Now that we're done, we must sort by depth, to ensure proper rendering
474-
table.sort(self.virtual_lines, function(a, b)
475-
return a.depth < b.depth
476-
end)
477-
end
478-
479474
---@param opts table
480475
local setup_hidden_display_function = function(opts)
481476
local hidden_display = opts.renderer.hidden_display

lua/nvim-tree/renderer/components/padding.lua

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,15 @@ local function check_siblings_for_folder(node, with_arrows)
1919
return false
2020
end
2121

22-
local function get_padding_indent_markers(depth, idx, nodes_number, markers, with_arrows, inline_arrows, node)
22+
local function get_padding_indent_markers(depth, idx, nodes_number, markers, with_arrows, inline_arrows, node, early_stop)
2323
local base_padding = with_arrows and (not node.nodes or depth > 0) and " " or ""
2424
local padding = (inline_arrows or depth == 0) and base_padding or ""
2525

2626
if depth > 0 then
2727
local has_folder_sibling = check_siblings_for_folder(node, with_arrows)
2828
local indent = string.rep(" ", M.config.indent_width - 1)
2929
markers[depth] = idx ~= nodes_number
30-
for i = 1, depth do
30+
for i = 1, depth - early_stop do
3131
local glyph
3232
if idx == nodes_number and i == depth then
3333
local bottom_width = M.config.indent_width - 2 + (with_arrows and not inline_arrows and has_folder_sibling and 2 or 0)
@@ -62,7 +62,7 @@ end
6262
---@param node table
6363
---@param markers table
6464
---@return HighlightedString[]
65-
function M.get_indent_markers(depth, idx, nodes_number, node, markers)
65+
function M.get_indent_markers(depth, idx, nodes_number, node, markers, early_stop)
6666
local str = ""
6767

6868
local show_arrows = M.config.icons.show.folder_arrow
@@ -71,7 +71,7 @@ function M.get_indent_markers(depth, idx, nodes_number, node, markers)
7171
local indent_width = M.config.indent_width
7272

7373
if show_markers then
74-
str = str .. get_padding_indent_markers(depth, idx, nodes_number, markers, show_arrows, inline_arrows, node)
74+
str = str .. get_padding_indent_markers(depth, idx, nodes_number, markers, show_arrows, inline_arrows, node, early_stop or 0)
7575
else
7676
str = str .. string.rep(" ", depth * indent_width)
7777
end

lua/nvim-tree/renderer/init.lua

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,9 @@ local function _draw(bufnr, lines, hl_args, signs, extmarks, virtual_lines)
5151
end
5252
end
5353

54-
for _, vline in ipairs(virtual_lines) do
55-
vim.api.nvim_buf_set_extmark(bufnr, namespace_extmarks_id, vline.line_nr, 0, {
56-
virt_lines = { { { vline.indent_string or "", "NvimTreeIndentMarker" }, { vline.text, "NvimTreeHiddenDisplay" } } },
54+
for line_nr, vlines in pairs(virtual_lines) do
55+
vim.api.nvim_buf_set_extmark(bufnr, namespace_extmarks_id, line_nr, 0, {
56+
virt_lines = vlines,
5757
virt_lines_above = false,
5858
virt_lines_leftcol = true,
5959
})

0 commit comments

Comments
 (0)