Skip to content

Commit 1b876db

Browse files
feat(#2789): add optional function expand_until to api.tree.expand_all and api.node.expand (#3166)
* feat: Allow to expand nodes until certain condition is met * Fix warnings * Restore original position of edit function * Rename field to match the api method name * Rename ApiTreeExpandAllOpts to ApiTreeExpandOpts * Remove toggle_descend_until * Remove redundant empty line * Update :help for changed methods * Fix partial expansion of grouped nodes * Fix lint error * Fix linting error * Fix incorrect open/close indicator state * Update docs * Rename descend_until option to expand_until * Always check directory expansion limit * Fix linter errors * Ignore unused param warning * Apply suggestions from code review * simplify MAX_FOLDER_DISCOVERY warning * fix bad comment whitespace --------- Co-authored-by: ghostbuster91 <ghostbuster91@users.noreply.github.com> Co-authored-by: Alexander Courtis <alex@courtis.org>
1 parent 0a52012 commit 1b876db

File tree

3 files changed

+93
-22
lines changed

3 files changed

+93
-22
lines changed

doc/nvim-tree-lua.txt

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1841,11 +1841,17 @@ tree.collapse_all({opts}) *nvim-tree-api.tree.collapse_all()*
18411841
Options: ~
18421842
• {keep_buffers} (boolean) do not collapse nodes with open buffers.
18431843

1844-
tree.expand_all({node}) *nvim-tree-api.tree.expand_all()*
1844+
tree.expand_all({node}, {opts}) *nvim-tree-api.tree.expand_all()*
18451845
Recursively expand all nodes under the tree root or specified folder.
18461846

18471847
Parameters: ~
18481848
{node} (Node|nil) folder
1849+
{opts} (ApiTreeExpandOpts) optional parameters
1850+
1851+
Options: ~
1852+
• {expand_until} ((fun(expansion_count: integer, node: Node?): boolean)?)
1853+
Return true if {node} should be expanded.
1854+
{expansion_count} is the total number of folders expanded.
18491855

18501856
*nvim-tree-api.tree.toggle_enable_filters()*
18511857
tree.toggle_enable_filters()
@@ -2279,12 +2285,18 @@ node.buffer.wipe({node}, {opts}) *nvim-tree-api.node.buffer.wipe()*
22792285
Options: ~
22802286
{force} (boolean) wipe even if buffer is modified, default false
22812287

2282-
node.expand({node}) *nvim-tree-api.node.expand()*
2288+
node.expand({node}, {opts}) *nvim-tree-api.node.expand()*
22832289
Recursively expand all nodes under a directory or a file's parent
22842290
directory.
22852291

22862292
Parameters: ~
22872293
{node} (Node|nil) file or folder
2294+
{opts} (ApiTreeExpandOpts) optional parameters
2295+
2296+
Options: ~
2297+
• {expand_until} ((fun(expansion_count: integer, node: Node?): boolean)?)
2298+
Return true if {node} should be expanded.
2299+
{expansion_count} is the total number of folders expanded.
22882300

22892301
node.collapse({node}, {opts}) *nvim-tree-api.node.collapse()*
22902302
Collapse the tree under a directory or a file's parent directory.

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

Lines changed: 75 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -27,20 +27,66 @@ local function expand(node)
2727
end
2828
end
2929

30-
---@param expansion_count integer
30+
---@param should_descend fun(expansion_count: integer, node: Node): boolean
31+
---@return fun(expansion_count: integer, node: Node): boolean
32+
local function limit_folder_discovery(should_descend)
33+
return function(expansion_count, node)
34+
local should_halt = expansion_count >= M.MAX_FOLDER_DISCOVERY
35+
if should_halt then
36+
notify.warn("expansion iteration was halted after " .. M.MAX_FOLDER_DISCOVERY .. " discovered folders")
37+
return false
38+
end
39+
40+
return should_descend(expansion_count, node)
41+
end
42+
end
43+
44+
---@param _ integer expansion_count
3145
---@param node Node
3246
---@return boolean
33-
local function should_expand(expansion_count, node)
47+
local function descend_until_empty(_, node)
48+
3449
local dir = node:as(DirectoryNode)
3550
if not dir then
3651
return false
3752
end
38-
local should_halt = expansion_count >= M.MAX_FOLDER_DISCOVERY
53+
3954
local should_exclude = M.EXCLUDE[dir.name]
40-
return not should_halt and not dir.open and not should_exclude
55+
return not should_exclude
4156
end
4257

43-
local function gen_iterator()
58+
---@param expansion_count integer
59+
---@param node Node
60+
---@param should_descend fun(expansion_count: integer, node: Node): boolean
61+
---@return boolean
62+
local function should_expand(expansion_count, node, should_descend)
63+
local dir = node:as(DirectoryNode)
64+
if not dir then
65+
return false
66+
end
67+
68+
if not dir.open and should_descend(expansion_count, node) then
69+
if #node.nodes == 0 then
70+
core.get_explorer():expand(dir) -- populate node.group_next
71+
end
72+
73+
if dir.group_next then
74+
local expand_next = should_expand(expansion_count, dir.group_next, should_descend)
75+
if expand_next then
76+
dir.open = true
77+
end
78+
return expand_next
79+
else
80+
return true
81+
end
82+
end
83+
return false
84+
end
85+
86+
87+
---@param should_descend fun(expansion_count: integer, node: Node): boolean
88+
---@return fun(node): any
89+
local function gen_iterator(should_descend)
4490
local expansion_count = 0
4591

4692
return function(parent)
@@ -52,7 +98,7 @@ local function gen_iterator()
5298
Iterator.builder(parent.nodes)
5399
:hidden()
54100
:applier(function(node)
55-
if should_expand(expansion_count, node) then
101+
if should_expand(expansion_count, node, should_descend) then
56102
expansion_count = expansion_count + 1
57103
node = node:as(DirectoryNode)
58104
if node then
@@ -61,25 +107,32 @@ local function gen_iterator()
61107
end
62108
end)
63109
:recursor(function(node)
64-
return expansion_count < M.MAX_FOLDER_DISCOVERY and (node.group_next and { node.group_next } or (node.open and node.nodes))
110+
if not should_descend(expansion_count, node) then
111+
return nil
112+
end
113+
114+
if node.group_next then
115+
return { node.group_next }
116+
end
117+
118+
if node.open and node.nodes then
119+
return node.nodes
120+
end
121+
122+
return nil
65123
end)
66124
:iterate()
67-
68-
if expansion_count >= M.MAX_FOLDER_DISCOVERY then
69-
return true
70-
end
71125
end
72126
end
73127

74128
---@param node Node?
75-
local function expand_node(node)
129+
---@param expand_opts ApiTreeExpandOpts?
130+
local function expand_node(node, expand_opts)
76131
if not node then
77132
return
78133
end
79-
80-
if gen_iterator()(node) then
81-
notify.warn("expansion iteration was halted after " .. M.MAX_FOLDER_DISCOVERY .. " discovered folders")
82-
end
134+
local descend_until = limit_folder_discovery((expand_opts and expand_opts.expand_until) or descend_until_empty)
135+
gen_iterator(descend_until)(node)
83136

84137
local explorer = core.get_explorer()
85138
if explorer then
@@ -89,18 +142,20 @@ end
89142

90143
---Expand the directory node or the root
91144
---@param node Node
92-
function M.all(node)
93-
expand_node(node and node:as(DirectoryNode) or core.get_explorer())
145+
---@param expand_opts ApiTreeExpandOpts?
146+
function M.all(node, expand_opts)
147+
expand_node(node and node:as(DirectoryNode) or core.get_explorer(), expand_opts)
94148
end
95149

96150
---Expand the directory node or parent node
97151
---@param node Node
98-
function M.node(node)
152+
---@param expand_opts ApiTreeExpandOpts?
153+
function M.node(node, expand_opts)
99154
if not node then
100155
return
101156
end
102157

103-
expand_node(node:is(FileNode) and node.parent or node:as(DirectoryNode))
158+
expand_node(node:is(FileNode) and node.parent or node:as(DirectoryNode), expand_opts)
104159
end
105160

106161
function M.setup(opts)

lua/nvim-tree/api.lua

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,10 @@ 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+
191+
---@class ApiTreeExpandOpts
192+
---@field expand_until (fun(expansion_count: integer, node: Node): boolean)|nil
193+
190194
Api.tree.expand_all = wrap_node(actions.tree.modifiers.expand.all)
191195
Api.tree.toggle_enable_filters = wrap_explorer_member("filters", "toggle")
192196
Api.tree.toggle_gitignore_filter = wrap_explorer_member_args("filters", "toggle", "git_ignored")

0 commit comments

Comments
 (0)