Skip to content

Commit 3bc4d45

Browse files
committed
tidy group methods
1 parent daf27a8 commit 3bc4d45

File tree

4 files changed

+18
-11
lines changed

4 files changed

+18
-11
lines changed

lua/nvim-tree.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ function M.place_cursor_on_node()
125125
if not node or node.name == ".." then
126126
return
127127
end
128-
node = node:group_parent_or_node()
128+
node = node:get_parent_of_group() or node
129129

130130
local line = vim.api.nvim_get_current_line()
131131
local cursor = vim.api.nvim_win_get_cursor(0)

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ function M.fn(should_close)
2424
return
2525
end
2626

27-
local parent = node:group_parent_or_node().parent
27+
local parent = (node:get_parent_of_group() or node).parent
2828

2929
if not parent or not parent.parent then
3030
return view.set_cursor({ 1, 0 })

lua/nvim-tree/node/directory.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ end
118118

119119
---Refresh contents and git status for a single node
120120
function DirectoryNode:refresh()
121-
local node = self:group_parent_or_node()
121+
local node = self:get_parent_of_group() or self
122122
local toplevel = git.get_toplevel(self.absolute_path)
123123

124124
git.reload_project(toplevel, self.absolute_path, function()
@@ -195,7 +195,7 @@ function DirectoryNode:expand_or_collapse(toggle_group)
195195
self.explorer:expand(self)
196196
end
197197

198-
local head_node = self:group_parent_or_node()
198+
local head_node = self:get_parent_of_group() or self
199199
if toggle_group then
200200
head_node:toggle_group_folders()
201201
end

lua/nvim-tree/node/init.lua

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -116,13 +116,20 @@ function BaseNode:update_parent_statuses(project, root)
116116
end
117117
end
118118

119-
---Get the highest parent of grouped nodes or the node itself
120-
---@return Node
121-
function BaseNode:group_parent_or_node()
122-
if self.parent and self.parent.group_next then
123-
return self.parent:group_parent_or_node()
124-
else
125-
return self
119+
---Get the highest parent of grouped nodes, nil when not grouped
120+
---@return DirectoryNode?
121+
function BaseNode:get_parent_of_group()
122+
if not self.parent or not self.parent.group_next then
123+
return nil
124+
end
125+
126+
local node = self.parent
127+
while node do
128+
if node.parent and node.parent.group_next then
129+
node = node.parent
130+
else
131+
return node
132+
end
126133
end
127134
end
128135

0 commit comments

Comments
 (0)