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

Conversation

itsramiel
Copy link
Contributor

@itsramiel itsramiel commented May 16, 2025

Summary

Support passing node to collapse_all to allow collapsing all dirs under a given node instead of always root node.

Addresses #3132 by providing an api

@itsramiel itsramiel marked this pull request as ready for review May 16, 2025 17:32
@itsramiel
Copy link
Contributor Author

@alex-courtis Please review and lmk if you have any comments. Then I'll update the api docs

@alex-courtis
Copy link
Member

I saw the issue before seeing this PR.

Please use an opts parameter as per draft #3132 (comment)

Copy link
Member

@alex-courtis alex-courtis left a comment

Choose a reason for hiding this comment

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

Many thanks for the fast PR. Please:

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

@alex-courtis
Copy link
Member

alex-courtis commented May 18, 2025

Test cases:

  vim.keymap.set("n", "nk", function()
    local node = api.tree.get_node_under_cursor()
    api.tree.collapse_all(node, true)
  end, opts("Collapse Node Keep"))

  vim.keymap.set("n", "nn", function()
    local node = api.tree.get_node_under_cursor()
    api.tree.collapse_all(node, false)
  end, opts("Collapse Node No Keep"))

  vim.keymap.set("n", "ak", function()
    api.tree.collapse_all(nil, true)
  end, opts("Collapse All Keep"))

  vim.keymap.set("n", "an", function()
    api.tree.collapse_all(nil, false)
  end, opts("Collapse All No Keep"))

  -- collapse-all.lua:43: attempt to index local 'node' (a boolean value)
  vim.keymap.set("n", "lk", function()
    api.tree.collapse_all(true)
  end, opts("Collapse All Legacy Keep"))

  -- collapse-all.lua:43: attempt to index local 'node' (a boolean value)
  vim.keymap.set("n", "ln", function()
    api.tree.collapse_all(true)
  end, opts("Collapse All Legacy No Keep"))

Following addition of opts they should look something like:

  vim.keymap.set("n", "nk", function()
    local node = api.tree.get_node_under_cursor()
    api.tree.collapse_all(node, { keep_buffers = true, })
  end, opts("Collapse Node Keep"))

@itsramiel itsramiel requested a review from alex-courtis May 18, 2025 08:06
@alex-courtis
Copy link
Member

Apologies, my time is limited, I'll look at this next weekend.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants