feat(actions): expand all under folder (#1292)

This commit is contained in:
Kiyan
2022-05-29 11:15:32 +02:00
committed by GitHub
parent 0373680819
commit 3a95c5a9cf
5 changed files with 78 additions and 0 deletions

View File

@@ -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

View File

@@ -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 = "<C-k>", 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)