Skip to content
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

Add Tree.get_node_by_id #1535

Merged
merged 6 commits into from
Jan 10, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
- Added `TreeNode.parent` -- a read-only property for accessing a node's parent https://github.com/Textualize/textual/issues/1397
- Added public `TreeNode` label access via `TreeNode.label` https://github.com/Textualize/textual/issues/1396
- Added read-only public access to the children of a `TreeNode` via `TreeNode.children` https://github.com/Textualize/textual/issues/1398
- Added `Tree.get_node_by_id` to allow getting a node by its ID https://github.com/Textualize/textual/pull/1535
- Added a `Tree.NodeHighlighted` message, giving a `on_tree_node_highlighted` event handler https://github.com/Textualize/textual/issues/1400

### Changed
Expand Down
20 changes: 20 additions & 0 deletions src/textual/widgets/_tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -515,6 +515,26 @@ def get_node_at_line(self, line_no: int) -> TreeNode[TreeDataType] | None:
else:
return line.node

class UnknownID(Exception):
davep marked this conversation as resolved.
Show resolved Hide resolved
"""Exception raised when referring to an unknown `TreeNode` ID."""

def get_node_by_id(self, node_id: NodeID) -> TreeNode[TreeDataType]:
davep marked this conversation as resolved.
Show resolved Hide resolved
"""Get a tree node by its ID.

Args:
node_id (NodeID): The ID of the node to get.

Returns:
TreeNode[TreeDataType]: The node associated with that ID.

Raises:
Tree.UnknownID: Raised if the `TreeNode` ID is unknown.
"""
try:
return self._nodes[node_id]
except KeyError:
raise self.UnknownID(f"Unknown TreeNode ID: {node_id}") from None
davep marked this conversation as resolved.
Show resolved Hide resolved

def validate_cursor_line(self, value: int) -> int:
"""Prevent cursor line from going outside of range."""
return clamp(value, 0, len(self._tree_lines) - 1)
Expand Down
16 changes: 16 additions & 0 deletions tests/tree/test_tree_get_node_by_id.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import pytest
from typing import cast
from textual.widgets import Tree
from textual.widgets._tree import NodeID


def test_get_tree_node_by_id() -> None:
"""It should be possible to get a TreeNode by its ID."""
tree = Tree[None]("Anakin")
child = tree.root.add("Leia")
grandchild = child.add("Ben")
assert tree.get_node_by_id(tree.root.id).id == tree.root.id
assert tree.get_node_by_id(child.id).id == child.id
assert tree.get_node_by_id(grandchild.id).id == grandchild.id
with pytest.raises(Tree.UnknownID):
tree.get_node_by_id(cast(NodeID, grandchild.id + 1000))