diff --git a/README.md b/README.md index 45c2507b..0f92acb8 100644 --- a/README.md +++ b/README.md @@ -190,6 +190,9 @@ require'nvim-tree'.setup { -- BEGIN_DEFAULT_OPTS global = false, restrict_above_cwd = false, }, + expand_all = { + max_folder_discovery = 300, + }, open_file = { quit_on_open = false, resize_window = true, @@ -261,6 +264,7 @@ require'nvim-tree'.setup { -- BEGIN_DEFAULT_OPTS - Double left click acts like `` - Double right click acts like `` - `W` will collapse the whole tree +- `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`). - `S` will prompt the user to enter a path and then expands the tree to match the path - `.` will enter vim command mode with the file the cursor is on - `C-k` will toggle a popup with file infos about the file under the cursor @@ -331,6 +335,7 @@ local list = { { key = "q", action = "close" }, { key = "g?", action = "toggle_help" }, { key = "W", action = "collapse_all" }, + { key = "E", action = "expand_all" }, { key = "S", action = "search_node" }, { key = "", action = "toggle_file_info" }, { key = ".", action = "run_file_command" } diff --git a/doc/nvim-tree-lua.txt b/doc/nvim-tree-lua.txt index 89569baa..219add77 100644 --- a/doc/nvim-tree-lua.txt +++ b/doc/nvim-tree-lua.txt @@ -208,6 +208,9 @@ Values may be functions. Warning: this may result in unexpected behaviour. global = false, restrict_above_cwd = false, }, + expand_all = { + max_folder_discovery = 300, + }, open_file = { quit_on_open = false, resize_window = true, @@ -633,6 +636,11 @@ Configuration for various actions. Restrict changing to a directory above the global current working directory. Type: `boolean`, Default: `false` + *nvim-tree.actions.expand_all.max_folder_discovery* + Limit the number of folders being explored when expanding every folders. + Avoids hanging neovim when running this action on very large folders. + Type: `number`, Default: `300` + *nvim-tree.actions.open_file.quit_on_open* Closes the explorer when opening a file. It will also disable preventing a buffer overriding the tree. @@ -762,6 +770,8 @@ INFORMATIONS *nvim-tree-info* - Double left click acts like - Double right click acts like - `W` will collapse the whole tree +- `E` will expand the whole tree. Be aware this might hang neovim for a while + if running on a big folder (such as home dir or root dir). - `S` will prompt the user to enter a path and then expands the tree to match the path - `.` will enter vim command mode with the file the cursor is on - `C-k` will toggle a popup with file infos about the file under the cursor @@ -807,6 +817,7 @@ Defaults to: { key = "q", action = "close" }, { key = "g?", action = "toggle_help" }, { key = 'W', action = "collapse_all" }, + { key = 'E', action = "expand_all" }, { key = "S", action = "search_node" }, { key = ".", action = "run_file_command" }, { key = "", action = "toggle_file_info" } diff --git a/lua/nvim-tree.lua b/lua/nvim-tree.lua index 04371c76..cbffbdac 100644 --- a/lua/nvim-tree.lua +++ b/lua/nvim-tree.lua @@ -465,6 +465,9 @@ local DEFAULT_OPTS = { -- BEGIN_DEFAULT_OPTS global = false, restrict_above_cwd = false, }, + expand_all = { + max_folder_discovery = 300, + }, open_file = { quit_on_open = false, resize_window = true, diff --git a/lua/nvim-tree/actions/expand-all.lua b/lua/nvim-tree/actions/expand-all.lua new file mode 100644 index 00000000..2b5db1b2 --- /dev/null +++ b/lua/nvim-tree/actions/expand-all.lua @@ -0,0 +1,56 @@ +local core = require "nvim-tree.core" +local renderer = require "nvim-tree.renderer" +local utils = require "nvim-tree.utils" + +local M = {} + +local function expand(node) + node.open = true + if #node.nodes == 0 then + core.get_explorer():expand(node) + end +end + +local function gen_iterator() + local expansion_count = 0 + + local function iterate(parent) + if expansion_count >= M.MAX_FOLDER_DISCOVERY then + return true + end + + if parent.parent and parent.nodes and not parent.open then + expansion_count = expansion_count + 1 + expand(parent) + end + + for _, node in pairs(parent.nodes) do + if node.nodes and not node.open then + expansion_count = expansion_count + 1 + expand(node) + end + + if node.open then + if iterate(node) then + return true + end + end + end + end + + return iterate +end + +function M.fn(base_node) + local node = base_node.nodes and base_node or core.get_explorer() + if gen_iterator()(node) then + utils.warn("expansion iteration was halted after " .. M.MAX_FOLDER_DISCOVERY .. " discovered folders") + end + renderer.draw() +end + +function M.setup(opts) + M.MAX_FOLDER_DISCOVERY = opts.actions.expand_all.max_folder_discovery +end + +return M diff --git a/lua/nvim-tree/actions/init.lua b/lua/nvim-tree/actions/init.lua index 1a535d54..62d15c4e 100644 --- a/lua/nvim-tree/actions/init.lua +++ b/lua/nvim-tree/actions/init.lua @@ -45,6 +45,7 @@ local M = { { key = "q", action = "close" }, { key = "g?", action = "toggle_help" }, { key = "W", action = "collapse_all" }, + { key = "E", action = "expand_all" }, { key = "S", action = "search_node" }, { key = ".", action = "run_file_command" }, { key = "", action = "toggle_file_info" }, @@ -57,6 +58,7 @@ local keypress_funcs = { close = view.close, close_node = require("nvim-tree.actions.movements").parent_node(true), collapse_all = require("nvim-tree.actions.collapse-all").fn, + expand_all = require("nvim-tree.actions.expand-all").fn, copy_absolute_path = require("nvim-tree.actions.copy-paste").copy_absolute_path, copy_name = require("nvim-tree.actions.copy-paste").copy_filename, copy_path = require("nvim-tree.actions.copy-paste").copy_path, @@ -237,6 +239,7 @@ function M.setup(opts) require("nvim-tree.actions.change-dir").setup(opts) require("nvim-tree.actions.copy-paste").setup(opts) require("nvim-tree.actions.create-file").setup(opts) + require("nvim-tree.actions.expand-all").setup(opts) local user_map_config = (opts.view or {}).mappings or {} local options = vim.tbl_deep_extend("force", DEFAULT_MAPPING_CONFIG, user_map_config)