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
Changes from 1 commit
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
19 changes: 14 additions & 5 deletions lua/nvim-tree/actions/tree/modifiers/collapse-all.lua
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,30 @@ local function buf_match()
end
end

---@param node Node
---@param keep_buffers boolean
function M.fn(keep_buffers)
function M.fn(node, keep_buffers)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As we are changing the signature of this API, we're going to need to retain backwards compatibility, in the case of the user calling it like collapse_all(true)

See

---Toggle the tree.
---@param opts ApiTreeToggleOpts|nil|boolean legacy -> opts.find_file
---@param no_focus string|nil legacy -> opts.focus
---@param cwd boolean|nil legacy -> opts.path
---@param bang boolean|nil legacy -> opts.update_root
function M.fn(opts, no_focus, cwd, bang)
-- legacy arguments
if type(opts) == "boolean" then
opts = {
find_file = opts,
}
if type(cwd) == "string" then
opts.path = cwd
end
if type(no_focus) == "boolean" then
opts.focus = not no_focus
end
if type(bang) == "boolean" then
opts.update_root = bang
end
end
opts = opts or {}
for a complex example.

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

local node = explorer:get_node_at_cursor()
if not node then
local nodeAtCursor = explorer:get_node_at_cursor()
if not nodeAtCursor 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)
Expand All @@ -51,7 +60,7 @@ function M.fn(keep_buffers)
:iterate()

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

return M
Loading