Skip to content

feat: Allow to expand nodes until certain condition is met #3166

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 3 commits into
base: master
Choose a base branch
from

Conversation

ghostbuster91
Copy link
Contributor

@ghostbuster91 ghostbuster91 commented Jul 20, 2025

This closes #2789

It took me a while but here I am :) I decided to create a fresh one as it was easier then resolving conflicts on the old one.

For now bare minimum of the changes that makes it work. Once we agree how the api will look like I will add docs following what we did in #2790

---@param mode string
---@param node Node
---@param edit_opts NodeEditOpts?
local function edit(mode, node, edit_opts)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

thought: just moved this function higher so that I can refer to it from toggle_descend_until

Copy link
Member

Choose a reason for hiding this comment

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

Moved it up during review and the changes are indeed minimal.

Could we please leave edit where it is, and move Api.tree.toggle_descend_until below it? That will result in a cleaner git history.

I've made that change on a temporary branch for review https://github.com/nvim-tree/nvim-tree.lua/compare/expand-until-2-alex-refactor?expand=1

Yes, the API is a mess however will be cleaned up in #3088

Api.tree.expand_all = wrap_node(actions.tree.modifiers.expand.all)

Api.tree.toggle_descend_until = wrap_node(function(node, descend_until)
Copy link
Contributor Author

@ghostbuster91 ghostbuster91 Jul 20, 2025

Choose a reason for hiding this comment

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

note: this is actually what I would like to achieve in the end. The edit part is wrong as nodes are not getting collapsed after hitting enter for the second time. Can you advise? fixed

question: how should the end user api look like?

Copy link
Member

Choose a reason for hiding this comment

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

This will be very useful, however it needs a bit of thought.

Can we please do this in a future PR? The functionality for the API expands is working and we can merge that now.

What should it look like? This isn't something the user should have to do; it should be core functionality.

It could go into node.open.edit, as a new member of NodeEditOpts

@alex-courtis
Copy link
Member

Thanks for getting back to this one @ghostbuster91

My time is limited, however I will get to reviewing next weekend.

FYI the nightly CI failure has been resolved on master: #3167

@alex-courtis
Copy link
Member

The descend filter functionality working beautifully for api.tree.expand_all and api.node.expand

Small issue: When the nodes are grouped, the arrow indicates open for a directory that is not traversed e.g.

20250728_105539
#!/bin/sh

rm -rfv foo

mkdir -pv foo/bar foo/baz foo/stop foo/baz/stop

touch foo/bar/1
touch foo/bar/2

touch foo/stop/3
touch foo/stop/4

touch foo/baz/stop/5
touch foo/baz/stop/6
---@type ApiTreeExpandAllOpts
local expand_all_opts = {
  descend_until = function(i, node)
    print(i .. " " .. node.name)
    return node.name ~= "stop" and node.name ~= ".git" and node.name ~= "7"
  end
}

  vim.keymap.set("n", "A", function()
    api.tree.expand_all(nil, expand_all_opts)
  end, opts("expand"))

  vim.keymap.set("n", "N", function()
    api.node.expand(nil, expand_all_opts)
  end, opts("expand"))

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.

Looking good, let's merge the expand functionality now.

Please:

@@ -72,12 +102,13 @@ local function gen_iterator()
end

---@param node Node?
local function expand_node(node)
---@param expand_opts ApiTreeExpandAllOpts?
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
---@param expand_opts ApiTreeExpandAllOpts?
---@param expand_opts ApiTreeExpandOpts?

This will bring it into parity with #3133

---@param mode string
---@param node Node
---@param edit_opts NodeEditOpts?
local function edit(mode, node, edit_opts)
Copy link
Member

Choose a reason for hiding this comment

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

Moved it up during review and the changes are indeed minimal.

Could we please leave edit where it is, and move Api.tree.toggle_descend_until below it? That will result in a cleaner git history.

I've made that change on a temporary branch for review https://github.com/nvim-tree/nvim-tree.lua/compare/expand-until-2-alex-refactor?expand=1

Yes, the API is a mess however will be cleaned up in #3088

@@ -186,7 +225,25 @@ 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)

---@class ApiTreeExpandAllOpts
---@field descend_until (fun(expansion_count: integer, node: Node): boolean)|nil
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
---@field descend_until (fun(expansion_count: integer, node: Node): boolean)|nil
---@field expand_until (fun(expansion_count: integer, node: Node): boolean)|nil

That would match the name of the api methods: expand

Api.tree.expand_all = wrap_node(actions.tree.modifiers.expand.all)

Api.tree.toggle_descend_until = wrap_node(function(node, descend_until)
Copy link
Member

Choose a reason for hiding this comment

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

This will be very useful, however it needs a bit of thought.

Can we please do this in a future PR? The functionality for the API expands is working and we can merge that now.

What should it look like? This isn't something the user should have to do; it should be core functionality.

It could go into node.open.edit, as a new member of NodeEditOpts

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.

Allow nodes to expand until a certain condition is met.
2 participants