Skip to content

Commit 447efc9

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

File tree

5 files changed

+77
-38
lines changed

5 files changed

+77
-38
lines changed

lua/nvim-tree/lib.lua

Lines changed: 3 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -42,46 +42,11 @@ function M.get_node_at_cursor()
4242
return utils.get_nodes_by_line(core.get_explorer().nodes, core.get_nodes_starting_line())[cursor[1]]
4343
end
4444

45-
---TODO move to node
46-
---Create a sanitized partial copy of a node, populating children recursively.
47-
---@param node Node?
48-
---@return Node|nil cloned node
49-
local function clone_node(node)
50-
if not node then
51-
node = core.get_explorer()
52-
if not node then
53-
return nil
54-
end
55-
end
56-
57-
local n = {
58-
absolute_path = node.absolute_path,
59-
executable = node.executable,
60-
extension = node.extension,
61-
git_status = node.git_status,
62-
has_children = node.has_children,
63-
hidden = node.hidden,
64-
link_to = node.link_to,
65-
name = node.name,
66-
open = node.open,
67-
type = node.type,
68-
fs_stat = node.fs_stat,
69-
}
70-
71-
if type(node.nodes) == "table" then
72-
n.nodes = {}
73-
for _, child in ipairs(node.nodes) do
74-
table.insert(n.nodes, clone_node(child))
75-
end
76-
end
77-
78-
return n
79-
end
80-
8145
---Api.tree.get_nodes
82-
---@return Node[]|nil
46+
---@return Node[]?
8347
function M.get_nodes()
84-
return clone_node(core.get_explorer())
48+
local explorer = core.get_explorer()
49+
return explorer and explorer:clone()
8550
end
8651

8752
---TODO move to node

lua/nvim-tree/node/directory.lua

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,4 +58,22 @@ function DirectoryNode:destroy()
5858
end
5959
end
6060

61+
---Create a sanitized partial copy of a node, populating children recursively.
62+
---@return DirectoryNode cloned
63+
function DirectoryNode:clone()
64+
local clone = BaseNode.clone(self) --[[@as DirectoryNode]]
65+
66+
clone.has_children = self.has_children
67+
clone.group_next = nil
68+
clone.nodes = {}
69+
clone.open = self.open
70+
clone.hidden_stats = nil
71+
72+
for _, child in ipairs(self.nodes) do
73+
table.insert(clone.nodes, child:clone())
74+
end
75+
76+
return clone
77+
end
78+
6179
return DirectoryNode

lua/nvim-tree/node/file.lua

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,14 @@ function FileNode:create(explorer, parent, absolute_path, name, fs_stat)
3636
return o
3737
end
3838

39+
---Create a sanitized partial copy of a node, populating children recursively.
40+
---@return FileNode cloned
41+
function FileNode:clone()
42+
local clone = BaseNode.clone(self) --[[@as FileNode]]
43+
44+
clone.extension = self.extension
45+
46+
return clone
47+
end
48+
3949
return FileNode

lua/nvim-tree/node/init.lua

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,4 +231,30 @@ function BaseNode:refresh()
231231
end)
232232
end
233233

234+
---Create a sanitized partial copy of a node, populating children recursively.
235+
---@return BaseNode cloned
236+
function BaseNode:clone()
237+
238+
---@type Explorer
239+
local placeholder
240+
241+
---@type BaseNode
242+
local clone = {
243+
type = self.type,
244+
explorer = placeholder,
245+
absolute_path = self.absolute_path,
246+
executable = self.executable,
247+
fs_stat = self.fs_stat,
248+
git_status = self.git_status,
249+
hidden = self.hidden,
250+
is_dot = self.is_dot,
251+
name = self.name,
252+
parent = nil,
253+
watcher = nil,
254+
diag_status = nil,
255+
}
256+
257+
return clone
258+
end
259+
234260
return BaseNode

lua/nvim-tree/node/link.lua

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,4 +66,24 @@ function LinkNode:create(explorer, parent, absolute_path, name, fs_stat)
6666
return o
6767
end
6868

69+
---Create a sanitized partial copy of a node, populating children recursively.
70+
---@return LinkNode cloned
71+
function LinkNode:clone()
72+
local clone = BaseNode.clone(self) --[[@as LinkNode]]
73+
74+
clone.has_children = self.has_children
75+
clone.group_next = nil
76+
clone.link_to = self.link_to
77+
clone.nodes = {}
78+
clone.open = self.open
79+
80+
if self.nodes then
81+
for _, child in ipairs(self.nodes) do
82+
table.insert(clone.nodes, child:clone())
83+
end
84+
end
85+
86+
return clone
87+
end
88+
6989
return LinkNode

0 commit comments

Comments
 (0)