Skip to content

Commit ae59561

Browse files
feat(#3132): add api.node.expand and api.node.collapse (#3133)
* feat: allow passing node to collapse all * refactor: use snake case * feat: handle api legacy calls and update signature * refactor: make sure open is a boolean * doc: collapse_all * Revert "doc: collapse_all" This reverts commit d243da3. * add api.node.collapse * add api.node.expand * add api.node.expand --------- Co-authored-by: Alexander Courtis <[email protected]>
1 parent 05d8172 commit ae59561

File tree

6 files changed

+96
-26
lines changed

6 files changed

+96
-26
lines changed

doc/nvim-tree-lua.txt

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ Show the mappings: `g?`
206206
`S` Search |nvim-tree-api.tree.search_node()|
207207
`u` Rename: Full Path |nvim-tree-api.fs.rename_full()|
208208
`U` Toggle Filter: Hidden |nvim-tree-api.tree.toggle_custom_filter()|
209-
`W` Collapse |nvim-tree-api.tree.collapse_all()|
209+
`W` Collapse All |nvim-tree-api.tree.collapse_all()|
210210
`x` Cut |nvim-tree-api.fs.cut()|
211211
`y` Copy Name |nvim-tree-api.fs.copy.filename()|
212212
`Y` Copy Relative Path |nvim-tree-api.fs.copy.relative_path()|
@@ -341,7 +341,7 @@ See |nvim-tree-highlight| for details.
341341

342342
See |nvim-tree-api.tree.collapse_all()|
343343

344-
Calls: `api.tree.collapse_all(false)`
344+
Calls: `api.tree.collapse_all({ keep_buffers = false })`
345345

346346
*:NvimTreeCollapseKeepBuffers*
347347

@@ -350,7 +350,7 @@ See |nvim-tree-highlight| for details.
350350

351351
See |nvim-tree-api.tree.collapse_all()|
352352

353-
Calls: `api.tree.collapse_all(true)`
353+
Calls: `api.tree.collapse_all({ keep_buffers = true })`
354354

355355
*:NvimTreeHiTest*
356356

@@ -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.
@@ -1831,10 +1832,13 @@ tree.find_file({opts}) *nvim-tree-api.tree.find_file()*
18311832
tree.search_node() *nvim-tree-api.tree.search_node()*
18321833
Open the search dialogue as per the search_node action.
18331834

1834-
tree.collapse_all({keep_buffers}) *nvim-tree-api.tree.collapse_all()*
1835+
tree.collapse_all({opts}) *nvim-tree-api.tree.collapse_all()*
18351836
Collapse the tree.
18361837

18371838
Parameters: ~
1839+
{opts} (table) optional parameters
1840+
1841+
Options: ~
18381842
• {keep_buffers} (boolean) do not collapse nodes with open buffers.
18391843

18401844
tree.expand_all({node}) *nvim-tree-api.tree.expand_all()*
@@ -2275,6 +2279,23 @@ node.buffer.wipe({node}, {opts}) *nvim-tree-api.node.buffer.wipe()*
22752279
Options: ~
22762280
{force} (boolean) wipe even if buffer is modified, default false
22772281

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+
2289+
node.collapse({node}, {opts}) *nvim-tree-api.node.collapse()*
2290+
Collapse the tree under a directory or a file's parent directory.
2291+
2292+
Parameters: ~
2293+
{node} (Node|nil) file or folder
2294+
{opts} (table) optional parameters
2295+
2296+
Options: ~
2297+
• {keep_buffers} (boolean) do not collapse nodes with open buffers.
2298+
22782299
==============================================================================
22792300
6.4 API GIT *nvim-tree-api.git*
22802301

@@ -2529,7 +2550,7 @@ You are encouraged to copy these to your own |nvim-tree.on_attach| function. >lu
25292550
vim.keymap.set("n", "S", api.tree.search_node, opts("Search"))
25302551
vim.keymap.set("n", "u", api.fs.rename_full, opts("Rename: Full Path"))
25312552
vim.keymap.set("n", "U", api.tree.toggle_custom_filter, opts("Toggle Filter: Hidden"))
2532-
vim.keymap.set("n", "W", api.tree.collapse_all, opts("Collapse"))
2553+
vim.keymap.set("n", "W", api.tree.collapse_all, opts("Collapse All"))
25332554
vim.keymap.set("n", "x", api.fs.cut, opts("Cut"))
25342555
vim.keymap.set("n", "y", api.fs.copy.filename, opts("Copy Name"))
25352556
vim.keymap.set("n", "Y", api.fs.copy.relative_path, opts("Copy Relative Path"))
@@ -3301,6 +3322,8 @@ highlight group is not, hard linking as follows: >
33013322
|nvim-tree-api.marks.toggle()|
33023323
|nvim-tree-api.node.buffer.delete()|
33033324
|nvim-tree-api.node.buffer.wipe()|
3325+
|nvim-tree-api.node.collapse()|
3326+
|nvim-tree-api.node.expand()|
33043327
|nvim-tree-api.node.navigate.diagnostics.next()|
33053328
|nvim-tree-api.node.navigate.diagnostics.next_recursive()|
33063329
|nvim-tree-api.node.navigate.diagnostics.prev()|

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

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

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

78
local M = {}
@@ -23,26 +24,30 @@ local function buf_match()
2324
end
2425
end
2526

26-
---@param keep_buffers boolean
27-
function M.fn(keep_buffers)
27+
---Collapse a node, root if nil
28+
---@param node Node?
29+
---@param opts ApiCollapseOpts
30+
local function collapse(node, opts)
2831
local explorer = core.get_explorer()
2932
if not explorer then
3033
return
3134
end
3235

33-
local node = explorer:get_node_at_cursor()
34-
if not node then
36+
node = node or explorer
37+
38+
local node_at_cursor = explorer:get_node_at_cursor()
39+
if not node_at_cursor then
3540
return
3641
end
3742

3843
local matches = buf_match()
3944

40-
Iterator.builder(explorer.nodes)
45+
Iterator.builder({ node:is(FileNode) and node.parent or node:as(DirectoryNode) })
4146
:hidden()
4247
:applier(function(n)
4348
local dir = n:as(DirectoryNode)
4449
if dir then
45-
dir.open = keep_buffers and matches(dir.absolute_path)
50+
dir.open = opts.keep_buffers == true and matches(dir.absolute_path)
4651
end
4752
end)
4853
:recursor(function(n)
@@ -51,7 +56,26 @@ function M.fn(keep_buffers)
5156
:iterate()
5257

5358
explorer.renderer:draw()
54-
utils.focus_node_or_parent(node)
59+
utils.focus_node_or_parent(node_at_cursor)
60+
end
61+
62+
63+
---@param opts ApiCollapseOpts|boolean|nil legacy -> opts.keep_buffers
64+
function M.all(opts)
65+
-- legacy arguments
66+
if type(opts) == "boolean" then
67+
opts = {
68+
keep_buffers = opts,
69+
}
70+
end
71+
72+
collapse(nil, opts or {})
73+
end
74+
75+
---@param node Node
76+
---@param opts ApiCollapseOpts?
77+
function M.node(node, opts)
78+
collapse(node, opts or {})
5579
end
5680

5781
return M

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: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
local M = {}
22

3-
M.collapse_all = require("nvim-tree.actions.tree.modifiers.collapse-all")
4-
M.expand_all = require("nvim-tree.actions.tree.modifiers.expand-all")
3+
M.collapse = require("nvim-tree.actions.tree.modifiers.collapse")
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: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -182,8 +182,12 @@ Api.tree.get_nodes = wrap_explorer("get_nodes")
182182

183183
Api.tree.find_file = wrap(actions.tree.find_file.fn)
184184
Api.tree.search_node = wrap(actions.finders.search_node.fn)
185-
Api.tree.collapse_all = wrap(actions.tree.modifiers.collapse_all.fn)
186-
Api.tree.expand_all = wrap_node(actions.tree.modifiers.expand_all.fn)
185+
186+
---@class ApiCollapseOpts
187+
---@field keep_buffers boolean|nil default false
188+
189+
Api.tree.collapse_all = wrap(actions.tree.modifiers.collapse.all)
190+
Api.tree.expand_all = wrap_node(actions.tree.modifiers.expand.all)
187191
Api.tree.toggle_enable_filters = wrap_explorer_member("filters", "toggle")
188192
Api.tree.toggle_gitignore_filter = wrap_explorer_member_args("filters", "toggle", "git_ignored")
189193
Api.tree.toggle_git_clean_filter = wrap_explorer_member_args("filters", "toggle", "git_clean")
@@ -312,6 +316,9 @@ Api.node.navigate.diagnostics.prev_recursive = wrap_node(actions.moves.item.fn({
312316
Api.node.navigate.opened.next = wrap_node(actions.moves.item.fn({ where = "next", what = "opened" }))
313317
Api.node.navigate.opened.prev = wrap_node(actions.moves.item.fn({ where = "prev", what = "opened" }))
314318

319+
Api.node.expand = wrap_node(actions.tree.modifiers.expand.node)
320+
Api.node.collapse = wrap_node(actions.tree.modifiers.collapse.node)
321+
315322
---@class ApiNodeDeleteWipeBufferOpts
316323
---@field force boolean|nil default false
317324

lua/nvim-tree/keymap.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ function M.default_on_attach(bufnr)
9696
vim.keymap.set("n", "S", api.tree.search_node, opts("Search"))
9797
vim.keymap.set("n", "u", api.fs.rename_full, opts("Rename: Full Path"))
9898
vim.keymap.set("n", "U", api.tree.toggle_custom_filter, opts("Toggle Filter: Hidden"))
99-
vim.keymap.set("n", "W", api.tree.collapse_all, opts("Collapse"))
99+
vim.keymap.set("n", "W", api.tree.collapse_all, opts("Collapse All"))
100100
vim.keymap.set("n", "x", api.fs.cut, opts("Cut"))
101101
vim.keymap.set("n", "y", api.fs.copy.filename, opts("Copy Name"))
102102
vim.keymap.set("n", "Y", api.fs.copy.relative_path, opts("Copy Relative Path"))

0 commit comments

Comments
 (0)