Skip to content

Commit 8f4ab9a

Browse files
committed
move last_group_node to DirectoryNode
1 parent fb2070d commit 8f4ab9a

File tree

9 files changed

+71
-36
lines changed

9 files changed

+71
-36
lines changed

lua/nvim-tree/actions/fs/clipboard.lua

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ local notify = require("nvim-tree.notify")
77

88
local find_file = require("nvim-tree.actions.finders.find-file").fn
99

10+
local DirectoryNode = require("nvim-tree.node.directory")
11+
1012
---@enum ACTION
1113
local ACTION = {
1214
copy = "copy",
@@ -219,7 +221,7 @@ end
219221
function Clipboard:do_paste(node, action, action_fn)
220222
if node.name == ".." then
221223
node = self.explorer
222-
else
224+
elseif node:is(DirectoryNode) then
223225
node = node:last_group_node()
224226
end
225227
local clip = self.data[action]

lua/nvim-tree/actions/fs/create-file.lua

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ local notify = require("nvim-tree.notify")
55

66
local find_file = require("nvim-tree.actions.finders.find-file").fn
77

8+
local DirectoryNode = require("nvim-tree.node.directory")
9+
810
local M = {}
911

1012
---@param file string
@@ -53,7 +55,7 @@ function M.fn(node)
5355
nodes = core.get_explorer().nodes,
5456
open = true,
5557
}
56-
else
58+
elseif node:is(DirectoryNode) then
5759
node = node:last_group_node()
5860
end
5961

lua/nvim-tree/actions/fs/rename-file.lua

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ local notify = require("nvim-tree.notify")
66

77
local find_file = require("nvim-tree.actions.finders.find-file").fn
88

9+
local DirectoryNode = require("nvim-tree.node.directory")
10+
911
local M = {
1012
config = {},
1113
}
@@ -120,7 +122,9 @@ function M.fn(default_modifier)
120122
return
121123
end
122124

123-
node = node:last_group_node()
125+
if node:is(DirectoryNode) then
126+
node = node:last_group_node()
127+
end
124128
if node.name == ".." then
125129
return
126130
end

lua/nvim-tree/actions/moves/parent.lua

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ local view = require("nvim-tree.view")
22
local utils = require("nvim-tree.utils")
33
local core = require("nvim-tree.core")
44

5+
local DirectoryNode = require("nvim-tree.node.directory")
6+
57
local M = {}
68

79
---@param should_close boolean|nil
@@ -11,7 +13,9 @@ function M.fn(should_close)
1113

1214
return function(node)
1315
local explorer = core.get_explorer()
14-
node = node:last_group_node()
16+
if node:is(DirectoryNode) then
17+
node = node:last_group_node()
18+
end
1519
if should_close and node.open then
1620
node.open = false
1721
if explorer then

lua/nvim-tree/actions/tree/modifiers/expand-all.lua

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ local core = require("nvim-tree.core")
22
local Iterator = require("nvim-tree.iterators.node-iterator")
33
local notify = require("nvim-tree.notify")
44

5+
local DirectoryNode = require("nvim-tree.node.directory")
6+
57
local M = {}
68

79
---@param list string[]
@@ -17,7 +19,9 @@ end
1719

1820
---@param node Node
1921
local function expand(node)
20-
node = node:last_group_node()
22+
if node:is(DirectoryNode) then
23+
node = node:last_group_node()
24+
end
2125
node.open = true
2226
if #node.nodes == 0 then
2327
core.get_explorer():expand(node)

lua/nvim-tree/node/directory.lua

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ local BaseNode = require("nvim-tree.node")
55

66
---@class (exact) DirectoryNode: BaseNode
77
---@field has_children boolean
8-
---@field group_next Node? -- If node is grouped, this points to the next child dir/link node
8+
---@field group_next DirectoryNode? -- If node is grouped, this points to the next child dir/link node
99
---@field nodes Node[]
1010
---@field open boolean
1111
---@field hidden_stats table? -- Each field of this table is a key for source and value for count
@@ -116,6 +116,18 @@ function DirectoryNode:get_git_status()
116116
end
117117
end
118118

119+
-- If node is grouped, return the last node in the group. Otherwise, return the given node.
120+
---@return Node
121+
function DirectoryNode:last_group_node()
122+
local node = self
123+
124+
while node.group_next do
125+
node = node.group_next or node
126+
end
127+
128+
return node
129+
end
130+
119131
function DirectoryNode:expand_or_collapse(toggle_group)
120132
toggle_group = toggle_group or false
121133
if self.has_children then

lua/nvim-tree/node/init.lua

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
local git = require("nvim-tree.git")
22

3+
---TODO #2886
34
---TODO remove all @cast
45
---TODO remove all references to directory fields:
56

@@ -106,17 +107,12 @@ function BaseNode:is_dotfile()
106107
return false
107108
end
108109

109-
-- If node is grouped, return the last node in the group. Otherwise, return the given node.
110+
---Return self, should only be called on a DirectoryNode
111+
---TODO #2886 remove method or leave in place, warn if practical and non too intrusive
110112
---@return Node
111113
function BaseNode:last_group_node()
112-
local node = self
113-
--- @cast node BaseNode
114-
115-
while node.group_next do
116-
node = node.group_next
117-
end
118-
119-
return node
114+
error(string.format("\nBaseNode:last_group_node called for '%s'", self.absolute_path))
115+
return self
120116
end
121117

122118
---@param project table?

lua/nvim-tree/renderer/builder.lua

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@ local notify = require("nvim-tree.notify")
22
local utils = require("nvim-tree.utils")
33
local view = require("nvim-tree.view")
44

5+
local DirectoryLinkNode = require("nvim-tree.node.directory-link")
6+
local DirectoryNode = require("nvim-tree.node.directory")
7+
local FileLinkNode = require("nvim-tree.node.file-link")
8+
59
local DecoratorBookmarks = require("nvim-tree.renderer.decorator.bookmarks")
610
local DecoratorCopied = require("nvim-tree.renderer.decorator.copied")
711
local DecoratorCut = require("nvim-tree.renderer.decorator.cut")
@@ -341,19 +345,21 @@ function Builder:add_highlights(node)
341345
return icon_hl_group, name_hl_group
342346
end
343347

348+
---Insert node line into self.lines, calling Builder:build_lines for each directory
344349
---@private
345-
function Builder:build_line(node, idx, num_children)
350+
---@param node Node
351+
---@param idx integer line number starting at 1
352+
---@param siblings integer of node
353+
function Builder:build_line(node, idx, siblings)
346354
-- various components
347-
local indent_markers = pad.get_indent_markers(self.depth, idx, num_children, node, self.markers)
355+
local indent_markers = pad.get_indent_markers(self.depth, idx, siblings, node, self.markers)
348356
local arrows = pad.get_arrows(node)
349357

350358
-- main components
351-
local is_folder = node.nodes ~= nil
352-
local is_symlink = node.link_to ~= nil
353359
local icon, name
354-
if is_folder then
360+
if node:is(DirectoryNode) then
355361
icon, name = self:build_folder(node)
356-
elseif is_symlink then
362+
elseif node:is(DirectoryLinkNode) or node:is(FileLinkNode) then
357363
icon, name = self:build_symlink(node)
358364
else
359365
icon, name = self:build_file(node)
@@ -369,11 +375,13 @@ function Builder:build_line(node, idx, num_children)
369375

370376
self.index = self.index + 1
371377

372-
node = node:last_group_node()
373-
if node.open then
374-
self.depth = self.depth + 1
375-
self:build_lines(node)
376-
self.depth = self.depth - 1
378+
if node:is(DirectoryNode) then
379+
node = node:last_group_node()
380+
if node.open then
381+
self.depth = self.depth + 1
382+
self:build_lines(node)
383+
self.depth = self.depth - 1
384+
end
377385
end
378386
end
379387

@@ -403,8 +411,11 @@ function Builder:add_hidden_count_string(node, idx, num_children)
403411
end
404412
end
405413

414+
---Number of visible nodes
406415
---@private
407-
function Builder:get_nodes_number(nodes)
416+
---@param nodes Node[]
417+
---@return integer
418+
function Builder:num_visible(nodes)
408419
if not self.explorer.live_filter.filter then
409420
return #nodes
410421
end
@@ -423,12 +434,12 @@ function Builder:build_lines(node)
423434
if not node then
424435
node = self.explorer
425436
end
426-
local num_children = self:get_nodes_number(node.nodes)
437+
local siblings = self:num_visible(node.nodes)
427438
local idx = 1
428439
for _, n in ipairs(node.nodes) do
429440
if not n.hidden then
430441
self:build_signs(n)
431-
self:build_line(n, idx, num_children)
442+
self:build_line(n, idx, siblings)
432443
idx = idx + 1
433444
end
434445
end

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,17 @@ 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, early_stop)
22+
local function get_padding_indent_markers(depth, idx, siblings, 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)
29-
markers[depth] = idx ~= nodes_number
29+
markers[depth] = idx ~= siblings
3030
for i = 1, depth - early_stop do
3131
local glyph
32-
if idx == nodes_number and i == depth then
32+
if idx == siblings 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)
3434
glyph = M.config.indent_markers.icons.corner
3535
.. string.rep(M.config.indent_markers.icons.bottom, bottom_width)
@@ -46,7 +46,7 @@ local function get_padding_indent_markers(depth, idx, nodes_number, markers, wit
4646
padding = padding .. glyph
4747
elseif inline_arrows then
4848
padding = padding
49-
elseif idx ~= nodes_number and depth == i and not node.nodes and has_folder_sibling then
49+
elseif idx ~= siblings and depth == i and not node.nodes and has_folder_sibling then
5050
padding = padding .. base_padding .. glyph .. base_padding
5151
else
5252
padding = padding .. base_padding .. glyph
@@ -58,11 +58,11 @@ end
5858

5959
---@param depth integer
6060
---@param idx integer
61-
---@param nodes_number integer
61+
---@param siblings integer
6262
---@param node Node
6363
---@param markers table
6464
---@return HighlightedString[]
65-
function M.get_indent_markers(depth, idx, nodes_number, node, markers, early_stop)
65+
function M.get_indent_markers(depth, idx, siblings, 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, early_sto
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, early_stop or 0)
74+
str = str .. get_padding_indent_markers(depth, idx, siblings, markers, show_arrows, inline_arrows, node, early_stop or 0)
7575
else
7676
str = str .. string.rep(" ", depth * indent_width)
7777
end

0 commit comments

Comments
 (0)