Skip to content

method for reloading single node of DirectoryTree#2988

Merged
rodrigogiraoserrao merged 11 commits intoTextualize:mainfrom
gergely-elias:directorytree_reload_node
Aug 9, 2023
Merged

method for reloading single node of DirectoryTree#2988
rodrigogiraoserrao merged 11 commits intoTextualize:mainfrom
gergely-elias:directorytree_reload_node

Conversation

@gergely-elias
Copy link
Copy Markdown
Contributor

@gergely-elias gergely-elias commented Jul 22, 2023

Please review the following checklist.

  • Docstrings on all new or modified functions / classes
  • Updated documentation
  • Updated CHANGELOG.md (where appropriate)

New method DirectoryTree.reload_node allows reloading the content of a single directory.

fixes #2757

Copy link
Copy Markdown
Contributor

@rodrigogiraoserrao rodrigogiraoserrao left a comment

Choose a reason for hiding this comment

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

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).
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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):
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

You can use DirectoryTree directly! 😃

@gergely-elias
Copy link
Copy Markdown
Contributor Author

This is headed in the correct direction! Just a couple of small changes, please!

Thanks for the comments, I'm about to push updates.
One thing I noticed is that there is a failing test case affecting tabs on windows/python3.8 setup.
I was wondering how stable the tests are and if I should look into that specific case.

# loading up.
self._add_to_load_queue(self.root)

def clear_subtree(self, node: TreeNode[DirEntry]) -> DirectoryTree:
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I think I'd have this typed to return Self.


def reset_subtree(
self, node: TreeNode[DirEntry], label: TextType, data: DirEntry | None = None
) -> DirectoryTree:
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I think I'd have this typed to return Self.

@rodrigogiraoserrao
Copy link
Copy Markdown
Contributor

@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.
I'll let you take a look at @davep's comments and then I'll run CI again so we can merge your PR! 🚀

@gergely-elias
Copy link
Copy Markdown
Contributor Author

gergely-elias commented Jul 23, 2023

Thanks @davep & @rodrigogiraoserrao.
I've pushed another commit, in which the import of Self is behind an if - just like the way it is done in the case of the parent class Tree.
But I saw that in other files you import Self without such condition, and it wasn't clear if there is a reason for this.

Let me know if it is fine as it is now or should be reworked.

Copy link
Copy Markdown
Contributor

@rodrigogiraoserrao rodrigogiraoserrao left a comment

Choose a reason for hiding this comment

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

Looks good to me! 🚀

Copy link
Copy Markdown
Member

@willmcgugan willmcgugan left a comment

Choose a reason for hiding this comment

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

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(
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

A new TreeNode is created, but doesn't this need to be added to the tree somewhere?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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:
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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()
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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:
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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.

Copy link
Copy Markdown
Contributor

@rodrigogiraoserrao rodrigogiraoserrao left a comment

Choose a reason for hiding this comment

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

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.

@gergely-elias
Copy link
Copy Markdown
Contributor Author

@rodrigogiraoserrao no worries
I think functionality wise this PR is complete, but am not sure if I really follow all your design conventions - so if reorganizing the parts regarding cache clearing is desired, please don't hesitate to tell

Copy link
Copy Markdown
Member

@willmcgugan willmcgugan left a comment

Choose a reason for hiding this comment

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

Looks good. Just one super trivial request.

label = self.render_label(node, NULL_STYLE, NULL_STYLE)
return label.cell_len

def clear_line_cache(self) -> None:
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This should be private, i.e. prefixed with _. It's not something that the dev needs to know about generally.

@gergely-elias
Copy link
Copy Markdown
Contributor Author

@rodrigogiraoserrao @willmcgugan
all review requests have been addressed, please approve

@rodrigogiraoserrao
Copy link
Copy Markdown
Contributor

@gergely-elias thank you so much for all your work and patience!
I'm waiting for the tests to pass (relevant xkcd) and I'll merge it :)

Copy link
Copy Markdown
Member

@willmcgugan willmcgugan left a comment

Choose a reason for hiding this comment

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

LGTM!

@rodrigogiraoserrao rodrigogiraoserrao merged commit b7dc046 into Textualize:main Aug 9, 2023
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.

Add a method to DirectoryTree that allows for reloading an individual node

4 participants