Skip to content

Conversation

@coolapso
Copy link
Contributor

@coolapso coolapso commented Nov 6, 2025

Description:

Fixes the following code note:

// TODO scrolling to a node should open all parents if they aren't already

This one was quite a bit out of my depth, and tried my best to find a somewhat clean way of doing it, therefore I don't have any smart reasoning as to why it was done like this other than a "this is how I managed to get it done"

let me know if you want any changes.

2025-11-06_20-11-1762456932.mp4

Checklist:

  • Tests included.
  • Lint and formatter run with no errors.
  • Tests all pass.

Where applicable:

  • Public APIs match existing style and have Since: line.
  • Any breaking changes have a deprecation path or have been discussed.
  • Check for binary size increases when importing new modules.

Implement TODO action and update ScrollTo to open branches when scrolling to item
Copy link
Member

@andydotxyz andydotxyz left a comment

Choose a reason for hiding this comment

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

Thanks for looking at this.

I think the expanding could be more efficient - instead of re-constructing the entire tree parent items you should be able to record the ids that lead to a child - then ensure that each of those ids are expanded. (though if you're using walk then onNode may need to be called after the child items in a branch scenario.

I would also factor this out into a separate method (maybe expandParents) so it's more readable and has a unit test.

@coolapso
Copy link
Contributor Author

coolapso commented Nov 6, 2025

Thanks for looking at this.

I think the expanding could be more efficient - instead of re-constructing the entire tree parent items you should be able to record the ids that lead to a child - then ensure that each of those ids are expanded. (though if you're using walk then onNode may need to be called after the child items in a branch scenario.

I would also factor this out into a separate method (maybe expandParents) so it's more readable and has a unit test.

🤔 I kinda tried that first time but miserably failed. will give it another shot and add a expandParents(...)

* add findPath internal method to find a path to a tree node
* add expandBranches internal method to expand all branches leading to a node
* use expandBranches in ScrollTo
@coolapso
Copy link
Contributor Author

@andydotxyz think I got it, hope this is more aligned with what you mentioned.

@coolapso coolapso mentioned this pull request Nov 15, 2025
6 tasks
Copy link
Member

@andydotxyz andydotxyz left a comment

Choose a reason for hiding this comment

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

Thanks, this is looking good - a few cleanups and I think that the path slice should be ordered for avoidance of confusion

t.Run("deeply nested node", func(t *testing.T) {
found, parents := tree.findPath("", "item_1_2_2")
assert.True(t, found)
assert.ElementsMatch(t, []string{"", "item_1", "item_1_2"}, parents)
Copy link
Member

Choose a reason for hiding this comment

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

Surely the order matters? The naming implies so - the word "path" implies ordering

for _, child := range childUIDs(current) {
found, path := t.findPath(child, target)
if found {
return true, append(path, current)
Copy link
Member

Choose a reason for hiding this comment

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

I think this will add the path elements in reverse order, see note in tests too

// Resize sets a new size for a widget.
// Opens the branches leading to a node
//
// Since: 2.8
Copy link
Member

Choose a reason for hiding this comment

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

There is no since required for private methods.

}

t.ensureOpenMap()
var opened []TreeNodeID
Copy link
Member

Choose a reason for hiding this comment

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

The need for this can be avoided if you call the callback function in the first loop.

Copy link
Member

Choose a reason for hiding this comment

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

(just a bool instead to indicate if refresh is required)

return
}

if len(parents) == 0 {
Copy link
Member

Choose a reason for hiding this comment

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

You could put this after an "or" for the !found to keep this shorter.

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