add api.node.expand

This commit is contained in:
Alexander Courtis 2025-06-14 17:19:11 +10:00
parent 93ed7949eb
commit 2d86cd3edf
5 changed files with 38 additions and 14 deletions

View File

@ -1470,7 +1470,8 @@ vim |current-directory| behaviour.
Type: `boolean`, Default: `false`
*nvim-tree.actions.expand_all*
Configuration for expand_all behaviour.
Configuration for *nvim-tree-api.tree.expand_all()* and
*nvim-tree-api.node.expand()*
*nvim-tree.actions.expand_all.max_folder_discovery*
Limit the number of folders being explored when expanding every folders.
@ -2278,8 +2279,15 @@ node.buffer.wipe({node}, {opts}) *nvim-tree-api.node.buffer.wipe()*
Options: ~
• {force} (boolean) wipe even if buffer is modified, default false
node.expand({node}) *nvim-tree-api.node.expand()*
Recursively expand all nodes under a directory or a file's parent
directory.
Parameters: ~
• {node} (Node|nil) file or folder
node.collapse({node}, {opts}) *nvim-tree-api.node.collapse()*
Collapse the tree underneath the node.
Collapse the tree under a directory or a file's parent directory.
Parameters: ~
• {node} (Node|nil) file or folder

View File

@ -68,9 +68,8 @@ function M.all(opts)
keep_buffers = opts,
}
end
opts = opts or {}
collapse(nil, opts)
collapse(nil, opts or {})
end
---@param node Node

View File

@ -2,6 +2,7 @@ local core = require("nvim-tree.core")
local Iterator = require("nvim-tree.iterators.node-iterator")
local notify = require("nvim-tree.notify")
local FileNode = require("nvim-tree.node.file")
local DirectoryNode = require("nvim-tree.node.directory")
local M = {}
@ -70,23 +71,38 @@ local function gen_iterator()
end
end
---Expand the directory node or the root
---@param node Node
function M.fn(node)
local explorer = core.get_explorer()
local parent = node:as(DirectoryNode) or explorer
if not parent then
---@param node Node?
local function expand_node(node)
if not node then
return
end
if gen_iterator()(parent) then
if gen_iterator()(node) then
notify.warn("expansion iteration was halted after " .. M.MAX_FOLDER_DISCOVERY .. " discovered folders")
end
local explorer = core.get_explorer()
if explorer then
explorer.renderer:draw()
end
end
---Expand the directory node or the root
---@param node Node
function M.all(node)
expand_node(node and node:as(DirectoryNode) or core.get_explorer())
end
---Expand the directory node or parent node
---@param node Node
function M.node(node)
if not node then
return
end
expand_node(node:is(FileNode) and node.parent or node:as(DirectoryNode))
end
function M.setup(opts)
M.MAX_FOLDER_DISCOVERY = opts.actions.expand_all.max_folder_discovery
M.EXCLUDE = to_lookup_table(opts.actions.expand_all.exclude)

View File

@ -1,10 +1,10 @@
local M = {}
M.collapse = require("nvim-tree.actions.tree.modifiers.collapse")
M.expand_all = require("nvim-tree.actions.tree.modifiers.expand-all")
M.expand = require("nvim-tree.actions.tree.modifiers.expand")
function M.setup(opts)
M.expand_all.setup(opts)
M.expand.setup(opts)
end
return M

View File

@ -187,7 +187,7 @@ Api.tree.search_node = wrap(actions.finders.search_node.fn)
---@field keep_buffers boolean|nil default false
Api.tree.collapse_all = wrap(actions.tree.modifiers.collapse.all)
Api.tree.expand_all = wrap_node(actions.tree.modifiers.expand_all.fn)
Api.tree.expand_all = wrap_node(actions.tree.modifiers.expand.all)
Api.tree.toggle_enable_filters = wrap_explorer_member("filters", "toggle")
Api.tree.toggle_gitignore_filter = wrap_explorer_member_args("filters", "toggle", "git_ignored")
Api.tree.toggle_git_clean_filter = wrap_explorer_member_args("filters", "toggle", "git_clean")
@ -316,6 +316,7 @@ Api.node.navigate.diagnostics.prev_recursive = wrap_node(actions.moves.item.fn({
Api.node.navigate.opened.next = wrap_node(actions.moves.item.fn({ where = "next", what = "opened" }))
Api.node.navigate.opened.prev = wrap_node(actions.moves.item.fn({ where = "prev", what = "opened" }))
Api.node.expand = wrap_node(actions.tree.modifiers.expand.node)
Api.node.collapse = wrap_node(actions.tree.modifiers.collapse.node)
---@class ApiNodeDeleteWipeBufferOpts