Skip to content

Commit ab558ce

Browse files
committed
extract methods from lib
1 parent 4b30785 commit ab558ce

File tree

9 files changed

+22
-24
lines changed

9 files changed

+22
-24
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ end
217217
---@param action ACTION
218218
---@param action_fn fun(source: string, dest: string)
219219
function Clipboard:do_paste(node, action, action_fn)
220-
node = lib.get_last_group_node(node)
220+
node = node:last_group_node()
221221
local explorer = core.get_explorer()
222222
if node.name == ".." and explorer then
223223
node = explorer

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
local utils = require("nvim-tree.utils")
22
local events = require("nvim-tree.events")
3-
local lib = require("nvim-tree.lib")
43
local core = require("nvim-tree.core")
54
local notify = require("nvim-tree.notify")
65

@@ -49,7 +48,7 @@ function M.fn(node)
4948
return
5049
end
5150

52-
node = node and lib.get_last_group_node(node)
51+
node = node and node:last_group_node()
5352

5453
local containing_folder
5554
if not node or node.name == ".." then

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ function M.fn(default_modifier)
120120
return
121121
end
122122

123-
node = lib.get_last_group_node(node)
123+
node = node:last_group_node()
124124
if node.name == ".." then
125125
return
126126
end

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
local view = require("nvim-tree.view")
22
local utils = require("nvim-tree.utils")
33
local core = require("nvim-tree.core")
4-
local lib = require("nvim-tree.lib")
54

65
local M = {}
76

@@ -12,7 +11,7 @@ function M.fn(should_close)
1211

1312
return function(node)
1413
local explorer = core.get_explorer()
15-
node = lib.get_last_group_node(node)
14+
node = node:last_group_node()
1615
if should_close and node.open then
1716
node.open = false
1817
if explorer then

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
local core = require("nvim-tree.core")
22
local Iterator = require("nvim-tree.iterators.node-iterator")
33
local notify = require("nvim-tree.notify")
4-
local lib = require("nvim-tree.lib")
54

65
local M = {}
76

@@ -18,7 +17,7 @@ end
1817

1918
---@param node Node
2019
local function expand(node)
21-
node = lib.get_last_group_node(node)
20+
node = node:last_group_node()
2221
node.open = true
2322
if #node.nodes == 0 then
2423
core.get_explorer():expand(node)

lua/nvim-tree/api.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ Api.tree.change_root_to_node = wrap_node(function(node)
138138
if node.name == ".." then
139139
actions.root.change_dir.fn("..")
140140
elseif node.nodes ~= nil then
141-
actions.root.change_dir.fn(lib.get_last_group_node(node).absolute_path)
141+
actions.root.change_dir.fn(node:last_group_node().absolute_path)
142142
end
143143
end)
144144

lua/nvim-tree/lib.lua

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -83,17 +83,6 @@ function M.get_nodes()
8383
return clone_node(core.get_explorer())
8484
end
8585

86-
-- If node is grouped, return the last node in the group. Otherwise, return the given node.
87-
---@param node Node
88-
---@return Node
89-
function M.get_last_group_node(node)
90-
while node and node.group_next do
91-
node = node.group_next
92-
end
93-
94-
return node ---@diagnostic disable-line: return-type-mismatch -- it can't be nil
95-
end
96-
9786
---Group empty folders
9887
-- Recursively group nodes
9988
---@param node Node
@@ -164,7 +153,7 @@ function M.expand_or_collapse(node, toggle_group)
164153
toggle_group_folders(head_node)
165154
end
166155

167-
local open = M.get_last_group_node(node).open
156+
local open = node:last_group_node().open
168157
local next_open
169158
if toggle_group then
170159
next_open = open

lua/nvim-tree/node/init.lua

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ function BaseNode:get_git_status()
7878
end
7979

8080
local status = {}
81-
if not require("nvim-tree.lib").get_last_group_node(self).open or self.explorer.opts.git.show_on_open_dirs then
81+
if not self:last_group_node().open or self.explorer.opts.git.show_on_open_dirs then
8282
-- dir is closed or we should show on open_dirs
8383
if self.git_status.file ~= nil then
8484
table.insert(status, self.git_status.file)
@@ -151,4 +151,16 @@ function BaseNode:is_dotfile()
151151
return false
152152
end
153153

154+
-- If node is grouped, return the last node in the group. Otherwise, return the given node.
155+
---@return Node
156+
function BaseNode:last_group_node()
157+
local node = self
158+
159+
while node.group_next do
160+
node = node.group_next
161+
end
162+
163+
return node
164+
end
165+
154166
return BaseNode

lua/nvim-tree/renderer/builder.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ function Builder:unwrap_highlighted_strings(highlighted_strings)
137137
end
138138

139139
---@private
140-
---@param node table
140+
---@param node Node
141141
---@return HighlightedString icon
142142
---@return HighlightedString name
143143
function Builder:build_folder(node)
@@ -369,7 +369,7 @@ function Builder:build_line(node, idx, num_children)
369369

370370
self.index = self.index + 1
371371

372-
node = require("nvim-tree.lib").get_last_group_node(node)
372+
node = node:last_group_node()
373373
if node.open then
374374
self.depth = self.depth + 1
375375
self:build_lines(node)

0 commit comments

Comments
 (0)