Skip to content

feat: allow passing node to collapse all #3133

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 25 additions & 7 deletions lua/nvim-tree/actions/tree/modifiers/collapse-all.lua
Original file line number Diff line number Diff line change
Expand Up @@ -23,26 +23,44 @@ local function buf_match()
end
end

---@param keep_buffers boolean
function M.fn(keep_buffers)
---@param node Node|boolean|nil legacy -> opts.keep_buffers
---@param opts ApiTreeCollapseAllOpts|nil
function M.fn(node, opts)
-- legacy arguments
if type(node) == "boolean" then
opts = {
keep_buffers = node,
}
node = nil
end
opts = opts or {}

local explorer = core.get_explorer()
if not explorer then
return
end

local node = explorer:get_node_at_cursor()
if not node then
local node_at_cursor = explorer:get_node_at_cursor()
if not node_at_cursor then
return
end

local matches = buf_match()

Iterator.builder(explorer.nodes)
local nodesToIterate = explorer.nodes
if node then
local dir = node:as(DirectoryNode)
if dir then
nodesToIterate = { dir }
end
end

Iterator.builder(nodesToIterate)
:hidden()
:applier(function(n)
local dir = n:as(DirectoryNode)
if dir then
dir.open = keep_buffers and matches(dir.absolute_path)
dir.open = opts.keep_buffers == true and matches(dir.absolute_path)
end
end)
:recursor(function(n)
Expand All @@ -51,7 +69,7 @@ function M.fn(keep_buffers)
:iterate()

explorer.renderer:draw()
utils.focus_node_or_parent(node)
utils.focus_node_or_parent(node_at_cursor)
end

return M
4 changes: 4 additions & 0 deletions lua/nvim-tree/api.lua
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,10 @@ Api.tree.get_nodes = wrap_explorer("get_nodes")

Api.tree.find_file = wrap(actions.tree.find_file.fn)
Api.tree.search_node = wrap(actions.finders.search_node.fn)

---@class ApiTreeCollapseAllOpts
---@field keep_buffers boolean|nil default false

Api.tree.collapse_all = wrap(actions.tree.modifiers.collapse_all.fn)
Api.tree.expand_all = wrap_node(actions.tree.modifiers.expand_all.fn)
Api.tree.toggle_enable_filters = wrap_explorer_member("filters", "toggle")
Expand Down
Loading