feat(api): add api.tree.get_nodes

This commit is contained in:
Alexander Courtis 2022-11-19 15:54:16 +11:00
parent a65063cb0a
commit e38e061710
3 changed files with 41 additions and 0 deletions

View File

@ -1147,6 +1147,7 @@ exists.
- change_root_to_node
- change_root_to_parent
- get_node_under_cursor
- get_nodes
- find_file `(filename: string)`
- search_node
- collapse_all `(keep_buffers?: bool)`

View File

@ -32,6 +32,7 @@ Api.tree.change_root_to_node = inject_node(function(node)
end)
Api.tree.change_root_to_parent = inject_node(require("nvim-tree.actions.root.dir-up").fn)
Api.tree.get_node_under_cursor = require("nvim-tree.lib").get_node_at_cursor
Api.tree.get_nodes = require("nvim-tree.lib").get_nodes
Api.tree.find_file = require("nvim-tree.actions.finders.find-file").fn
Api.tree.search_node = require("nvim-tree.actions.finders.search-node").fn
Api.tree.collapse_all = require("nvim-tree.actions.tree-modifiers.collapse-all").fn

View File

@ -32,6 +32,45 @@ function M.get_node_at_cursor()
return utils.get_nodes_by_line(core.get_explorer().nodes, core.get_nodes_starting_line())[line]
end
---Create a sanitized partial copy of a node, populating children recursively.
---@param node table
---@return table|nil cloned node
local function clone_node(node)
if not node then
node = core.get_explorer()
if not node then
return nil
end
end
local n = {
absolute_path = node.absolute_path,
executable = node.executable,
extension = node.extension,
git_status = node.git_status,
has_children = node.has_children,
hidden = node.hidden,
link_to = node.link_to,
name = node.name,
open = node.open,
type = node.type,
}
if type(node.nodes) == "table" then
n.nodes = {}
for _, child in ipairs(node.nodes) do
table.insert(n.nodes, clone_node(child))
end
end
return n
end
---Api.tree.get_nodes
function M.get_nodes()
return clone_node(core.get_explorer())
end
-- If node is grouped, return the last node in the group. Otherwise, return the given node.
function M.get_last_group_node(node)
local next = node