Skip to content

Commit b9704a4

Browse files
committed
extract methods from lib
1 parent 447efc9 commit b9704a4

File tree

5 files changed

+26
-30
lines changed

5 files changed

+26
-30
lines changed

lua/nvim-tree.lua

+1-1
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 = utils.get_parent_of_group(node)
128+
node = node:get_parent_of_group()
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

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ function M.fn(should_close)
2020
return
2121
end
2222

23-
local parent = utils.get_parent_of_group(node).parent
23+
local parent = node:get_parent_of_group().parent
2424

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

lua/nvim-tree/lib.lua

+2-15
Original file line numberDiff line numberDiff line change
@@ -79,19 +79,6 @@ function M.ungroup_empty_folders(node)
7979
end
8080
end
8181

82-
---TODO move to node
83-
---@param node Node
84-
---@return Node[]
85-
function M.get_all_nodes_in_group(node)
86-
local next_node = utils.get_parent_of_group(node)
87-
local nodes = {}
88-
while next_node do
89-
table.insert(nodes, next_node)
90-
next_node = next_node.group_next
91-
end
92-
return nodes
93-
end
94-
9582
---TODO move to node
9683
-- Toggle group empty folders
9784
---@param head_node Node
@@ -119,7 +106,7 @@ function M.expand_or_collapse(node, toggle_group)
119106
explorer:expand(node)
120107
end
121108

122-
local head_node = utils.get_parent_of_group(node)
109+
local head_node = node:get_parent_of_group()
123110
if toggle_group then
124111
toggle_group_folders(head_node)
125112
end
@@ -131,7 +118,7 @@ function M.expand_or_collapse(node, toggle_group)
131118
else
132119
next_open = not open
133120
end
134-
for _, n in ipairs(M.get_all_nodes_in_group(head_node)) do
121+
for _, n in ipairs(head_node:get_all_nodes_in_group()) do
135122
n.open = next_open
136123
end
137124

lua/nvim-tree/node/init.lua

+22-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
local git = require("nvim-tree.git")
2-
local utils = require("nvim-tree.utils")
32

43
---Abstract Node class.
54
---Uses the abstract factory pattern to instantiate child instances.
@@ -217,7 +216,7 @@ end
217216

218217
---Refresh contents and git status for a single node
219218
function BaseNode:refresh()
220-
local parent_node = utils.get_parent_of_group(self)
219+
local parent_node = self:get_parent_of_group()
221220
local toplevel = git.get_toplevel(self.absolute_path)
222221

223222
git.reload_project(toplevel, self.absolute_path, function()
@@ -231,10 +230,30 @@ function BaseNode:refresh()
231230
end)
232231
end
233232

233+
---Get the highest parent of grouped nodes
234+
---@return Node node or parent
235+
function BaseNode:get_parent_of_group()
236+
local node = self
237+
while node and node.parent and node.parent.group_next do
238+
node = node.parent or node
239+
end
240+
return node
241+
end
242+
243+
---@return Node[]
244+
function BaseNode:get_all_nodes_in_group()
245+
local next_node = self:get_parent_of_group()
246+
local nodes = {}
247+
while next_node do
248+
table.insert(nodes, next_node)
249+
next_node = next_node.group_next
250+
end
251+
return nodes
252+
end
253+
234254
---Create a sanitized partial copy of a node, populating children recursively.
235255
---@return BaseNode cloned
236256
function BaseNode:clone()
237-
238257
---@type Explorer
239258
local placeholder
240259

lua/nvim-tree/utils.lua

-10
Original file line numberDiff line numberDiff line change
@@ -173,16 +173,6 @@ function M.get_node_from_path(path)
173173
:iterate()
174174
end
175175

176-
---Get the highest parent of grouped nodes
177-
---@param node Node
178-
---@return Node node or parent
179-
function M.get_parent_of_group(node)
180-
while node and node.parent and node.parent.group_next do
181-
node = node.parent or node
182-
end
183-
return node
184-
end
185-
186176
M.default_format_hidden_count = function(hidden_count, simple)
187177
local parts = {}
188178
local total_count = 0

0 commit comments

Comments
 (0)