method for reloading single node of DirectoryTree#2988
method for reloading single node of DirectoryTree#2988rodrigogiraoserrao merged 11 commits intoTextualize:mainfrom
Conversation
rodrigogiraoserrao
left a comment
There was a problem hiding this comment.
This is headed in the correct direction!
Just a couple of small changes, please!
| """Clear all nodes under the given node. | ||
|
|
||
| Returns: | ||
| The `Tree` instance (the subtree below the node). |
There was a problem hiding this comment.
You say "the subtree below the node", which doesn't sound right to me.
If you are returning self, you are returning the root tree.
If you return node, you are returning the root of the subtree that was cleared.
So, the docstring needs to be touched up.
| from textual.widgets import DirectoryTree | ||
|
|
||
|
|
||
| class MyDirectoryTree(DirectoryTree): |
There was a problem hiding this comment.
You can use DirectoryTree directly! 😃
Thanks for the comments, I'm about to push updates. |
| # loading up. | ||
| self._add_to_load_queue(self.root) | ||
|
|
||
| def clear_subtree(self, node: TreeNode[DirEntry]) -> DirectoryTree: |
There was a problem hiding this comment.
I think I'd have this typed to return Self.
|
|
||
| def reset_subtree( | ||
| self, node: TreeNode[DirEntry], label: TextType, data: DirEntry | None = None | ||
| ) -> DirectoryTree: |
There was a problem hiding this comment.
I think I'd have this typed to return Self.
|
@gergely-elias yeah some of our tests are a bit flaky; yesterday I noticed a failing test in this PR and it was completely unrelated to you. I re-ran it and it passed. |
|
Thanks @davep & @rodrigogiraoserrao. Let me know if it is fine as it is now or should be reworked. |
rodrigogiraoserrao
left a comment
There was a problem hiding this comment.
Looks good to me! 🚀
willmcgugan
left a comment
There was a problem hiding this comment.
It's probably best to add some utility methods on the base case as needed here.
| self._tree_lines_cached = None | ||
| node_label = node._label | ||
| node_data = node.data | ||
| node = TreeNode( |
There was a problem hiding this comment.
A new TreeNode is created, but doesn't this need to be added to the tree somewhere?
There was a problem hiding this comment.
you're right, the second argument should be the parent node, and not None
| # loading up. | ||
| self._add_to_load_queue(self.root) | ||
|
|
||
| def clear_subtree(self, node: TreeNode[DirEntry]) -> Self: |
There was a problem hiding this comment.
I don't think we've used the term subtree anywhere. Feels a bit out of place. Could it just be clear_node?
| Returns: | ||
| The `Tree` instance. | ||
| """ | ||
| self._line_cache.clear() |
There was a problem hiding this comment.
We're modifying a "private" variable of the base class here, which is generally inadvisable. Too easy to inadvertently break this class if the base class is modified.
It's probably best to have any neccesary methods added to the base class.
There was a problem hiding this comment.
I noticed the base class already has an invalidate_cache method, which needs just a little bit more than needed here, so in the end I did not use that one. On the other hand - as far as I can see - calling that one would also be an option here, so maybe you'd prefer to be this done with that.
Also I did not implement a utility method to increase self._updates, looked like somewhat of an overkill. But again, let me know if you'd prefer it being done like that.
| yield DirectoryTree(self._tmp_path) | ||
|
|
||
|
|
||
| async def test_directory_tree_reload_node(tmp_path) -> None: |
There was a problem hiding this comment.
Test is somewhat verbose.
Could you either A) break it in to smaller tests. or B) group some of these asserts, and add a comment that explains what is being tets
There was a problem hiding this comment.
I did both break it down into two cases and added some comments. Let me know if you still think the tests should be improved.
rodrigogiraoserrao
left a comment
There was a problem hiding this comment.
After addressing Will's comments, the PR looks good to me.
Sorry for the time you've been waiting. We'll come back to you ASAP.
|
@rodrigogiraoserrao no worries |
willmcgugan
left a comment
There was a problem hiding this comment.
Looks good. Just one super trivial request.
src/textual/widgets/_tree.py
Outdated
| label = self.render_label(node, NULL_STYLE, NULL_STYLE) | ||
| return label.cell_len | ||
|
|
||
| def clear_line_cache(self) -> None: |
There was a problem hiding this comment.
This should be private, i.e. prefixed with _. It's not something that the dev needs to know about generally.
|
@rodrigogiraoserrao @willmcgugan |
|
@gergely-elias thank you so much for all your work and patience! |
Please review the following checklist.
New method
DirectoryTree.reload_nodeallows reloading the content of a single directory.fixes #2757