Skip to content

Commit 3a95c5a

Browse files
authored
feat(actions): expand all under folder (#1292)
1 parent 0373680 commit 3a95c5a

File tree

5 files changed

+78
-0
lines changed

5 files changed

+78
-0
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,9 @@ require'nvim-tree'.setup { -- BEGIN_DEFAULT_OPTS
190190
global = false,
191191
restrict_above_cwd = false,
192192
},
193+
expand_all = {
194+
max_folder_discovery = 300,
195+
},
193196
open_file = {
194197
quit_on_open = false,
195198
resize_window = true,
@@ -261,6 +264,7 @@ require'nvim-tree'.setup { -- BEGIN_DEFAULT_OPTS
261264
- Double left click acts like `<CR>`
262265
- Double right click acts like `<C-]>`
263266
- `W` will collapse the whole tree
267+
- `E` will expand the whole tree. Be aware this might hang neovim for a while if running on a big folder (see `:help nvim-tree.actions.expand_all.max_folder_discovery`).
264268
- `S` will prompt the user to enter a path and then expands the tree to match the path
265269
- `.` will enter vim command mode with the file the cursor is on
266270
- `C-k` will toggle a popup with file infos about the file under the cursor
@@ -331,6 +335,7 @@ local list = {
331335
{ key = "q", action = "close" },
332336
{ key = "g?", action = "toggle_help" },
333337
{ key = "W", action = "collapse_all" },
338+
{ key = "E", action = "expand_all" },
334339
{ key = "S", action = "search_node" },
335340
{ key = "<C-k>", action = "toggle_file_info" },
336341
{ key = ".", action = "run_file_command" }

doc/nvim-tree-lua.txt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,9 @@ Values may be functions. Warning: this may result in unexpected behaviour.
208208
global = false,
209209
restrict_above_cwd = false,
210210
},
211+
expand_all = {
212+
max_folder_discovery = 300,
213+
},
211214
open_file = {
212215
quit_on_open = false,
213216
resize_window = true,
@@ -633,6 +636,11 @@ Configuration for various actions.
633636
Restrict changing to a directory above the global current working directory.
634637
Type: `boolean`, Default: `false`
635638

639+
*nvim-tree.actions.expand_all.max_folder_discovery*
640+
Limit the number of folders being explored when expanding every folders.
641+
Avoids hanging neovim when running this action on very large folders.
642+
Type: `number`, Default: `300`
643+
636644
*nvim-tree.actions.open_file.quit_on_open*
637645
Closes the explorer when opening a file.
638646
It will also disable preventing a buffer overriding the tree.
@@ -762,6 +770,8 @@ INFORMATIONS *nvim-tree-info*
762770
- Double left click acts like <CR>
763771
- Double right click acts like <C-]>
764772
- `W` will collapse the whole tree
773+
- `E` will expand the whole tree. Be aware this might hang neovim for a while
774+
if running on a big folder (such as home dir or root dir).
765775
- `S` will prompt the user to enter a path and then expands the tree to match the path
766776
- `.` will enter vim command mode with the file the cursor is on
767777
- `C-k` will toggle a popup with file infos about the file under the cursor
@@ -807,6 +817,7 @@ Defaults to:
807817
{ key = "q", action = "close" },
808818
{ key = "g?", action = "toggle_help" },
809819
{ key = 'W', action = "collapse_all" },
820+
{ key = 'E', action = "expand_all" },
810821
{ key = "S", action = "search_node" },
811822
{ key = ".", action = "run_file_command" },
812823
{ key = "<C-k>", action = "toggle_file_info" }

lua/nvim-tree.lua

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -465,6 +465,9 @@ local DEFAULT_OPTS = { -- BEGIN_DEFAULT_OPTS
465465
global = false,
466466
restrict_above_cwd = false,
467467
},
468+
expand_all = {
469+
max_folder_discovery = 300,
470+
},
468471
open_file = {
469472
quit_on_open = false,
470473
resize_window = true,

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

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
local core = require "nvim-tree.core"
2+
local renderer = require "nvim-tree.renderer"
3+
local utils = require "nvim-tree.utils"
4+
5+
local M = {}
6+
7+
local function expand(node)
8+
node.open = true
9+
if #node.nodes == 0 then
10+
core.get_explorer():expand(node)
11+
end
12+
end
13+
14+
local function gen_iterator()
15+
local expansion_count = 0
16+
17+
local function iterate(parent)
18+
if expansion_count >= M.MAX_FOLDER_DISCOVERY then
19+
return true
20+
end
21+
22+
if parent.parent and parent.nodes and not parent.open then
23+
expansion_count = expansion_count + 1
24+
expand(parent)
25+
end
26+
27+
for _, node in pairs(parent.nodes) do
28+
if node.nodes and not node.open then
29+
expansion_count = expansion_count + 1
30+
expand(node)
31+
end
32+
33+
if node.open then
34+
if iterate(node) then
35+
return true
36+
end
37+
end
38+
end
39+
end
40+
41+
return iterate
42+
end
43+
44+
function M.fn(base_node)
45+
local node = base_node.nodes and base_node or core.get_explorer()
46+
if gen_iterator()(node) then
47+
utils.warn("expansion iteration was halted after " .. M.MAX_FOLDER_DISCOVERY .. " discovered folders")
48+
end
49+
renderer.draw()
50+
end
51+
52+
function M.setup(opts)
53+
M.MAX_FOLDER_DISCOVERY = opts.actions.expand_all.max_folder_discovery
54+
end
55+
56+
return M

lua/nvim-tree/actions/init.lua

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ local M = {
4545
{ key = "q", action = "close" },
4646
{ key = "g?", action = "toggle_help" },
4747
{ key = "W", action = "collapse_all" },
48+
{ key = "E", action = "expand_all" },
4849
{ key = "S", action = "search_node" },
4950
{ key = ".", action = "run_file_command" },
5051
{ key = "<C-k>", action = "toggle_file_info" },
@@ -57,6 +58,7 @@ local keypress_funcs = {
5758
close = view.close,
5859
close_node = require("nvim-tree.actions.movements").parent_node(true),
5960
collapse_all = require("nvim-tree.actions.collapse-all").fn,
61+
expand_all = require("nvim-tree.actions.expand-all").fn,
6062
copy_absolute_path = require("nvim-tree.actions.copy-paste").copy_absolute_path,
6163
copy_name = require("nvim-tree.actions.copy-paste").copy_filename,
6264
copy_path = require("nvim-tree.actions.copy-paste").copy_path,
@@ -237,6 +239,7 @@ function M.setup(opts)
237239
require("nvim-tree.actions.change-dir").setup(opts)
238240
require("nvim-tree.actions.copy-paste").setup(opts)
239241
require("nvim-tree.actions.create-file").setup(opts)
242+
require("nvim-tree.actions.expand-all").setup(opts)
240243

241244
local user_map_config = (opts.view or {}).mappings or {}
242245
local options = vim.tbl_deep_extend("force", DEFAULT_MAPPING_CONFIG, user_map_config)

0 commit comments

Comments
 (0)