diff --git a/doc/nvim-tree-lua.txt b/doc/nvim-tree-lua.txt index 0961941e..a20560eb 100644 --- a/doc/nvim-tree-lua.txt +++ b/doc/nvim-tree-lua.txt @@ -1470,7 +1470,8 @@ vim |current-directory| behaviour. Type: `boolean`, Default: `false` *nvim-tree.actions.expand_all* -Configuration for expand_all behaviour. +Configuration for *nvim-tree-api.tree.expand_all()* and +*nvim-tree-api.node.expand()* *nvim-tree.actions.expand_all.max_folder_discovery* Limit the number of folders being explored when expanding every folders. @@ -2278,8 +2279,15 @@ node.buffer.wipe({node}, {opts}) *nvim-tree-api.node.buffer.wipe()* Options: ~ • {force} (boolean) wipe even if buffer is modified, default false +node.expand({node}) *nvim-tree-api.node.expand()* + Recursively expand all nodes under a directory or a file's parent + directory. + + Parameters: ~ + • {node} (Node|nil) file or folder + node.collapse({node}, {opts}) *nvim-tree-api.node.collapse()* - Collapse the tree underneath the node. + Collapse the tree under a directory or a file's parent directory. Parameters: ~ • {node} (Node|nil) file or folder diff --git a/lua/nvim-tree/actions/tree/modifiers/collapse.lua b/lua/nvim-tree/actions/tree/modifiers/collapse.lua index be796a76..62da5f9a 100644 --- a/lua/nvim-tree/actions/tree/modifiers/collapse.lua +++ b/lua/nvim-tree/actions/tree/modifiers/collapse.lua @@ -68,9 +68,8 @@ function M.all(opts) keep_buffers = opts, } end - opts = opts or {} - collapse(nil, opts) + collapse(nil, opts or {}) end ---@param node Node diff --git a/lua/nvim-tree/actions/tree/modifiers/expand-all.lua b/lua/nvim-tree/actions/tree/modifiers/expand.lua similarity index 83% rename from lua/nvim-tree/actions/tree/modifiers/expand-all.lua rename to lua/nvim-tree/actions/tree/modifiers/expand.lua index 6032c686..385ff72b 100644 --- a/lua/nvim-tree/actions/tree/modifiers/expand-all.lua +++ b/lua/nvim-tree/actions/tree/modifiers/expand.lua @@ -2,6 +2,7 @@ local core = require("nvim-tree.core") local Iterator = require("nvim-tree.iterators.node-iterator") local notify = require("nvim-tree.notify") +local FileNode = require("nvim-tree.node.file") local DirectoryNode = require("nvim-tree.node.directory") local M = {} @@ -70,23 +71,38 @@ local function gen_iterator() end end ----Expand the directory node or the root ----@param node Node -function M.fn(node) - local explorer = core.get_explorer() - local parent = node:as(DirectoryNode) or explorer - if not parent then +---@param node Node? +local function expand_node(node) + if not node then return end - if gen_iterator()(parent) then + if gen_iterator()(node) then notify.warn("expansion iteration was halted after " .. M.MAX_FOLDER_DISCOVERY .. " discovered folders") end + + local explorer = core.get_explorer() if explorer then explorer.renderer:draw() end end +---Expand the directory node or the root +---@param node Node +function M.all(node) + expand_node(node and node:as(DirectoryNode) or core.get_explorer()) +end + +---Expand the directory node or parent node +---@param node Node +function M.node(node) + if not node then + return + end + + expand_node(node:is(FileNode) and node.parent or node:as(DirectoryNode)) +end + function M.setup(opts) M.MAX_FOLDER_DISCOVERY = opts.actions.expand_all.max_folder_discovery M.EXCLUDE = to_lookup_table(opts.actions.expand_all.exclude) diff --git a/lua/nvim-tree/actions/tree/modifiers/init.lua b/lua/nvim-tree/actions/tree/modifiers/init.lua index 8b66dd61..cf1ae40b 100644 --- a/lua/nvim-tree/actions/tree/modifiers/init.lua +++ b/lua/nvim-tree/actions/tree/modifiers/init.lua @@ -1,10 +1,10 @@ local M = {} M.collapse = require("nvim-tree.actions.tree.modifiers.collapse") -M.expand_all = require("nvim-tree.actions.tree.modifiers.expand-all") +M.expand = require("nvim-tree.actions.tree.modifiers.expand") function M.setup(opts) - M.expand_all.setup(opts) + M.expand.setup(opts) end return M diff --git a/lua/nvim-tree/api.lua b/lua/nvim-tree/api.lua index aeccd43e..39fba07d 100644 --- a/lua/nvim-tree/api.lua +++ b/lua/nvim-tree/api.lua @@ -187,7 +187,7 @@ Api.tree.search_node = wrap(actions.finders.search_node.fn) ---@field keep_buffers boolean|nil default false Api.tree.collapse_all = wrap(actions.tree.modifiers.collapse.all) -Api.tree.expand_all = wrap_node(actions.tree.modifiers.expand_all.fn) +Api.tree.expand_all = wrap_node(actions.tree.modifiers.expand.all) Api.tree.toggle_enable_filters = wrap_explorer_member("filters", "toggle") Api.tree.toggle_gitignore_filter = wrap_explorer_member_args("filters", "toggle", "git_ignored") Api.tree.toggle_git_clean_filter = wrap_explorer_member_args("filters", "toggle", "git_clean") @@ -316,6 +316,7 @@ Api.node.navigate.diagnostics.prev_recursive = wrap_node(actions.moves.item.fn({ Api.node.navigate.opened.next = wrap_node(actions.moves.item.fn({ where = "next", what = "opened" })) Api.node.navigate.opened.prev = wrap_node(actions.moves.item.fn({ where = "prev", what = "opened" })) +Api.node.expand = wrap_node(actions.tree.modifiers.expand.node) Api.node.collapse = wrap_node(actions.tree.modifiers.collapse.node) ---@class ApiNodeDeleteWipeBufferOpts