Skip to content

Commit 2d86cd3

Browse files
committed
add api.node.expand
1 parent 93ed794 commit 2d86cd3

File tree

5 files changed

+38
-14
lines changed

5 files changed

+38
-14
lines changed

doc/nvim-tree-lua.txt

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1470,7 +1470,8 @@ vim |current-directory| behaviour.
14701470
Type: `boolean`, Default: `false`
14711471

14721472
*nvim-tree.actions.expand_all*
1473-
Configuration for expand_all behaviour.
1473+
Configuration for *nvim-tree-api.tree.expand_all()* and
1474+
*nvim-tree-api.node.expand()*
14741475

14751476
*nvim-tree.actions.expand_all.max_folder_discovery*
14761477
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()*
22782279
Options: ~
22792280
{force} (boolean) wipe even if buffer is modified, default false
22802281

2282+
node.expand({node}) *nvim-tree-api.node.expand()*
2283+
Recursively expand all nodes under a directory or a file's parent
2284+
directory.
2285+
2286+
Parameters: ~
2287+
{node} (Node|nil) file or folder
2288+
22812289
node.collapse({node}, {opts}) *nvim-tree-api.node.collapse()*
2282-
Collapse the tree underneath the node.
2290+
Collapse the tree under a directory or a file's parent directory.
22832291

22842292
Parameters: ~
22852293
{node} (Node|nil) file or folder

lua/nvim-tree/actions/tree/modifiers/collapse.lua

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,8 @@ function M.all(opts)
6868
keep_buffers = opts,
6969
}
7070
end
71-
opts = opts or {}
7271

73-
collapse(nil, opts)
72+
collapse(nil, opts or {})
7473
end
7574

7675
---@param node Node

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

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ local core = require("nvim-tree.core")
22
local Iterator = require("nvim-tree.iterators.node-iterator")
33
local notify = require("nvim-tree.notify")
44

5+
local FileNode = require("nvim-tree.node.file")
56
local DirectoryNode = require("nvim-tree.node.directory")
67

78
local M = {}
@@ -70,23 +71,38 @@ local function gen_iterator()
7071
end
7172
end
7273

73-
---Expand the directory node or the root
74-
---@param node Node
75-
function M.fn(node)
76-
local explorer = core.get_explorer()
77-
local parent = node:as(DirectoryNode) or explorer
78-
if not parent then
74+
---@param node Node?
75+
local function expand_node(node)
76+
if not node then
7977
return
8078
end
8179

82-
if gen_iterator()(parent) then
80+
if gen_iterator()(node) then
8381
notify.warn("expansion iteration was halted after " .. M.MAX_FOLDER_DISCOVERY .. " discovered folders")
8482
end
83+
84+
local explorer = core.get_explorer()
8585
if explorer then
8686
explorer.renderer:draw()
8787
end
8888
end
8989

90+
---Expand the directory node or the root
91+
---@param node Node
92+
function M.all(node)
93+
expand_node(node and node:as(DirectoryNode) or core.get_explorer())
94+
end
95+
96+
---Expand the directory node or parent node
97+
---@param node Node
98+
function M.node(node)
99+
if not node then
100+
return
101+
end
102+
103+
expand_node(node:is(FileNode) and node.parent or node:as(DirectoryNode))
104+
end
105+
90106
function M.setup(opts)
91107
M.MAX_FOLDER_DISCOVERY = opts.actions.expand_all.max_folder_discovery
92108
M.EXCLUDE = to_lookup_table(opts.actions.expand_all.exclude)
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
local M = {}
22

33
M.collapse = require("nvim-tree.actions.tree.modifiers.collapse")
4-
M.expand_all = require("nvim-tree.actions.tree.modifiers.expand-all")
4+
M.expand = require("nvim-tree.actions.tree.modifiers.expand")
55

66
function M.setup(opts)
7-
M.expand_all.setup(opts)
7+
M.expand.setup(opts)
88
end
99

1010
return M

lua/nvim-tree/api.lua

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ Api.tree.search_node = wrap(actions.finders.search_node.fn)
187187
---@field keep_buffers boolean|nil default false
188188

189189
Api.tree.collapse_all = wrap(actions.tree.modifiers.collapse.all)
190-
Api.tree.expand_all = wrap_node(actions.tree.modifiers.expand_all.fn)
190+
Api.tree.expand_all = wrap_node(actions.tree.modifiers.expand.all)
191191
Api.tree.toggle_enable_filters = wrap_explorer_member("filters", "toggle")
192192
Api.tree.toggle_gitignore_filter = wrap_explorer_member_args("filters", "toggle", "git_ignored")
193193
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({
316316
Api.node.navigate.opened.next = wrap_node(actions.moves.item.fn({ where = "next", what = "opened" }))
317317
Api.node.navigate.opened.prev = wrap_node(actions.moves.item.fn({ where = "prev", what = "opened" }))
318318

319+
Api.node.expand = wrap_node(actions.tree.modifiers.expand.node)
319320
Api.node.collapse = wrap_node(actions.tree.modifiers.collapse.node)
320321

321322
---@class ApiNodeDeleteWipeBufferOpts

0 commit comments

Comments
 (0)