Skip to content

Commit d5e018a

Browse files
committed
tidy group methods
1 parent 4f51269 commit d5e018a

File tree

6 files changed

+61
-76
lines changed

6 files changed

+61
-76
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:get_parent_of_group()
128+
node = node:group_parent_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:get_parent_of_group().parent
27+
local parent = node:group_parent_or_node().parent
2828

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

lua/nvim-tree/explorer/init.lua

Lines changed: 18 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ local utils = require("nvim-tree.utils")
55
local view = require("nvim-tree.view")
66
local node_factory = require("nvim-tree.node.factory")
77

8+
local DirectoryNode = require("nvim-tree.node.directory")
89
local RootNode = require("nvim-tree.node.root")
910
local Watcher = require("nvim-tree.watcher")
1011

@@ -72,12 +73,12 @@ function Explorer:create(path)
7273
return o
7374
end
7475

75-
---@param node Node
76+
---@param node DirectoryNode
7677
function Explorer:expand(node)
7778
self:_load(node)
7879
end
7980

80-
---@param node Node
81+
---@param node DirectoryNode
8182
---@param git_status table|nil
8283
function Explorer:reload(node, git_status)
8384
local cwd = node.link_to or node.absolute_path
@@ -169,17 +170,10 @@ function Explorer:reload(node, git_status)
169170
end, node.nodes)
170171
)
171172

172-
local is_root = not node.parent
173-
---
174-
--- @cast node DirectoryNode
175-
---
176-
local child_folder_only = node:has_one_child_folder() and node.nodes[1]
177-
---
178-
--- @cast child_folder_only DirectoryNode
179-
---
180-
if config.renderer.group_empty and not is_root and child_folder_only then
181-
node.group_next = child_folder_only
182-
local ns = self:reload(child_folder_only, git_status)
173+
local single_child = node:single_child_directory()
174+
if config.renderer.group_empty and node.parent and single_child then
175+
node.group_next = single_child
176+
local ns = self:reload(single_child, git_status)
183177
node.nodes = ns or {}
184178
log.profile_end(profile)
185179
return ns
@@ -225,7 +219,7 @@ function Explorer:refresh_parent_nodes_for_path(path)
225219
end
226220

227221
---@private
228-
---@param node Node
222+
---@param node DirectoryNode
229223
function Explorer:_load(node)
230224
local cwd = node.link_to or node.absolute_path
231225
local git_status = git.load_project_status(cwd)
@@ -301,7 +295,7 @@ function Explorer:populate_children(handle, cwd, node, git_status, parent)
301295
end
302296

303297
---@private
304-
---@param node Node
298+
---@param node DirectoryNode
305299
---@param status table
306300
---@param parent Explorer
307301
---@return Node[]|nil
@@ -317,18 +311,12 @@ function Explorer:explore(node, status, parent)
317311
self:populate_children(handle, cwd, node, status, parent)
318312

319313
local is_root = not node.parent
320-
---
321-
--- @cast node DirectoryNode
322-
---
323-
local child_folder_only = node:has_one_child_folder() and node.nodes[1]
324-
---
325-
--- @cast child_folder_only DirectoryNode
326-
---
327-
if config.renderer.group_empty and not is_root and child_folder_only then
328-
local child_cwd = child_folder_only.link_to or child_folder_only.absolute_path
314+
local single_child = node:single_child_directory()
315+
if config.renderer.group_empty and not is_root and single_child then
316+
local child_cwd = single_child.link_to or single_child.absolute_path
329317
local child_status = git.load_project_status(child_cwd)
330-
node.group_next = child_folder_only
331-
local ns = self:explore(child_folder_only, child_status, parent)
318+
node.group_next = single_child
319+
local ns = self:explore(single_child, child_status, parent)
332320
node.nodes = ns or {}
333321

334322
log.profile_end(profile)
@@ -347,9 +335,10 @@ end
347335
function Explorer:refresh_nodes(projects)
348336
Iterator.builder({ self })
349337
:applier(function(n)
350-
if n.nodes then
351-
local toplevel = git.get_toplevel(n.cwd or n.link_to or n.absolute_path)
352-
self:reload(n, projects[toplevel] or {})
338+
local dir = n:as(DirectoryNode)
339+
if dir then
340+
local toplevel = git.get_toplevel(dir.cwd or dir.link_to or dir.absolute_path)
341+
self:reload(dir, projects[toplevel] or {})
353342
end
354343
end)
355344
:recursor(function(n)

lua/nvim-tree/explorer/watch.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ local function is_folder_ignored(path)
5353
return false
5454
end
5555

56-
---@param node Node
56+
---@param node DirectoryNode
5757
---@return Watcher|nil
5858
function M.create_watcher(node)
5959
if not M.config.filesystem_watchers.enable or type(node) ~= "table" then

lua/nvim-tree/node/directory.lua

Lines changed: 32 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,22 @@ function DirectoryNode:get_git_status()
116116
end
117117
end
118118

119+
---Refresh contents and git status for a single node
120+
function DirectoryNode:refresh()
121+
local node = self:group_parent_or_node()
122+
local toplevel = git.get_toplevel(self.absolute_path)
123+
124+
git.reload_project(toplevel, self.absolute_path, function()
125+
local project = git.get_project(toplevel) or {}
126+
127+
self.explorer:reload(node, project)
128+
129+
node:update_parent_statuses(project, toplevel)
130+
131+
self.explorer.renderer:draw()
132+
end)
133+
end
134+
119135
-- If node is grouped, return the last node in the group. Otherwise, return the given node.
120136
---@return Node
121137
function DirectoryNode:last_group_node()
@@ -128,15 +144,18 @@ function DirectoryNode:last_group_node()
128144
return node
129145
end
130146

131-
---@return boolean
132-
function DirectoryNode:has_one_child_folder()
133-
return #self.nodes == 1 and self.nodes[1].nodes and vim.loop.fs_access(self.nodes[1].absolute_path, "R") or false
147+
---Return the one and only one child directory
148+
---@return DirectoryNode?
149+
function DirectoryNode:single_child_directory()
150+
if #self.nodes == 1 then
151+
return self.nodes[1]:as(DirectoryNode)
152+
end
134153
end
135154

136155
---@private
137156
---@return Node[]
138157
function DirectoryNode:get_all_nodes_in_group()
139-
local next_node = self:get_parent_of_group()
158+
local next_node = self:group_parent_or_node()
140159
local nodes = {}
141160
while next_node do
142161
table.insert(nodes, next_node)
@@ -162,14 +181,10 @@ end
162181
---@private
163182
---@return Node[]
164183
function DirectoryNode:group_empty_folders()
165-
local is_root = not self.parent
166-
local child_folder_only = self:has_one_child_folder() and self.nodes[1]
167-
---
168-
--- @cast child_folder_only DirectoryNode
169-
---
170-
if self.explorer.opts.renderer.group_empty and not is_root and child_folder_only then
171-
self.group_next = child_folder_only
172-
local ns = child_folder_only:group_empty_folders()
184+
local single_child = self:single_child_directory()
185+
if self.explorer.opts.renderer.group_empty and self.parent and single_child then
186+
self.group_next = single_child
187+
local ns = single_child:group_empty_folders()
173188
self.nodes = ns or {}
174189
return ns
175190
end
@@ -180,11 +195,10 @@ end
180195
-- If a node is grouped, ungroup it: put node.group_next to the node.nodes and set node.group_next to nil
181196
---@private
182197
function DirectoryNode:ungroup_empty_folders()
183-
local cur = self
184-
while cur and cur.group_next do
185-
cur.nodes = { cur.group_next }
186-
cur.group_next = nil
187-
cur = cur.nodes[1]
198+
if self.group_next then
199+
self.group_next:ungroup_empty_folders()
200+
self.nodes = { self.group_next }
201+
self.group_next = nil
188202
end
189203
end
190204

@@ -199,10 +213,7 @@ function DirectoryNode:expand_or_collapse(toggle_group)
199213
self.explorer:expand(self)
200214
end
201215

202-
local head_node = self:get_parent_of_group()
203-
---
204-
--- @cast head_node DirectoryNode
205-
---
216+
local head_node = self:group_parent_or_node()
206217
if toggle_group then
207218
head_node:toggle_group_folders()
208219
end

lua/nvim-tree/node/init.lua

Lines changed: 8 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ end
3838
---@param status table?
3939
function BaseNode:update_git_status(parent_ignored, status) ---@diagnostic disable-line: unused-local
4040
end
41+
4142
--luacheck: pop
4243

4344
---@return GitStatus?
@@ -115,30 +116,14 @@ function BaseNode:update_parent_statuses(project, root)
115116
end
116117
end
117118

118-
---Refresh contents and git status for a single node
119-
function BaseNode:refresh()
120-
local parent_node = self:get_parent_of_group()
121-
local toplevel = git.get_toplevel(self.absolute_path)
122-
123-
git.reload_project(toplevel, self.absolute_path, function()
124-
local project = git.get_project(toplevel) or {}
125-
126-
self.explorer:reload(parent_node, project)
127-
128-
parent_node:update_parent_statuses(project, toplevel)
129-
130-
self.explorer.renderer:draw()
131-
end)
132-
end
133-
134-
---Get the highest parent of grouped nodes
135-
---@return Node node or parent
136-
function BaseNode:get_parent_of_group()
137-
local node = self
138-
while node and node.parent and node.parent.group_next do
139-
node = node.parent or node
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
140126
end
141-
return node
142127
end
143128

144129
---Create a sanitized partial copy of a node, populating children recursively.

0 commit comments

Comments
 (0)