Skip to content

Commit e38e061

Browse files
committed
feat(api): add api.tree.get_nodes
1 parent a65063c commit e38e061

File tree

3 files changed

+41
-0
lines changed

3 files changed

+41
-0
lines changed

doc/nvim-tree-lua.txt

+1
Original file line numberDiff line numberDiff line change
@@ -1147,6 +1147,7 @@ exists.
11471147
- change_root_to_node
11481148
- change_root_to_parent
11491149
- get_node_under_cursor
1150+
- get_nodes
11501151
- find_file `(filename: string)`
11511152
- search_node
11521153
- collapse_all `(keep_buffers?: bool)`

lua/nvim-tree/api.lua

+1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ Api.tree.change_root_to_node = inject_node(function(node)
3232
end)
3333
Api.tree.change_root_to_parent = inject_node(require("nvim-tree.actions.root.dir-up").fn)
3434
Api.tree.get_node_under_cursor = require("nvim-tree.lib").get_node_at_cursor
35+
Api.tree.get_nodes = require("nvim-tree.lib").get_nodes
3536
Api.tree.find_file = require("nvim-tree.actions.finders.find-file").fn
3637
Api.tree.search_node = require("nvim-tree.actions.finders.search-node").fn
3738
Api.tree.collapse_all = require("nvim-tree.actions.tree-modifiers.collapse-all").fn

lua/nvim-tree/lib.lua

+39
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,45 @@ function M.get_node_at_cursor()
3232
return utils.get_nodes_by_line(core.get_explorer().nodes, core.get_nodes_starting_line())[line]
3333
end
3434

35+
---Create a sanitized partial copy of a node, populating children recursively.
36+
---@param node table
37+
---@return table|nil cloned node
38+
local function clone_node(node)
39+
if not node then
40+
node = core.get_explorer()
41+
if not node then
42+
return nil
43+
end
44+
end
45+
46+
local n = {
47+
absolute_path = node.absolute_path,
48+
executable = node.executable,
49+
extension = node.extension,
50+
git_status = node.git_status,
51+
has_children = node.has_children,
52+
hidden = node.hidden,
53+
link_to = node.link_to,
54+
name = node.name,
55+
open = node.open,
56+
type = node.type,
57+
}
58+
59+
if type(node.nodes) == "table" then
60+
n.nodes = {}
61+
for _, child in ipairs(node.nodes) do
62+
table.insert(n.nodes, clone_node(child))
63+
end
64+
end
65+
66+
return n
67+
end
68+
69+
---Api.tree.get_nodes
70+
function M.get_nodes()
71+
return clone_node(core.get_explorer())
72+
end
73+
3574
-- If node is grouped, return the last node in the group. Otherwise, return the given node.
3675
function M.get_last_group_node(node)
3776
local next = node

0 commit comments

Comments
 (0)