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 document tree iterator #251

Merged
merged 1 commit into from
Nov 11, 2023
Merged

Add document tree iterator #251

merged 1 commit into from
Nov 11, 2023

Conversation

Kiyoshika
Copy link
Member

@Kiyoshika Kiyoshika commented Nov 11, 2023

This adds a TreeIterator which takes a &DocumentHandle and constructs an iterator which walks through the tree one node at a time in tree order.

EDIT: below statement is incorrect, mutations are reflected in the iterator if it's still kept "open." I added a warning in the struct description about this.
This iterator is a snapshot of the document at the time the iterator is created and is not updated from mutations. In that case, a new iterator must be created.

At the moment, the iterator returns NodeIds (thanks for the help from @emwalker) which is good enough for now but ideally I would like this to return some type of &Node or Rc<Node> that the user agent can use directly without having to touch the document's "internals"

DocumentHandle.query() has been updated to use this new iterator as well which still passes all tests

@Kiyoshika Kiyoshika requested a review from a team November 11, 2023 01:06
@@ -185,20 +185,28 @@ impl TreeBuilder for DocumentTaskQueue {
new_id
}

fn create_text(&mut self, content: &str, parent_id: NodeId) {
fn create_text(&mut self, content: &str, parent_id: NodeId) -> NodeId {
Copy link
Member Author

Choose a reason for hiding this comment

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

Making create_text and create_comment return a node ID is not mandatory but I did it for convenience (in the tests) and also it makes the tree builder interface more consistent anyways..


while let Some(current_node_id) = node_stack.pop() {
for current_node_id in tree_iterator {
Copy link
Member Author

Choose a reason for hiding this comment

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

cool side effect of using the iterator, we get this nice Python-like iteration syntax

@Kiyoshika Kiyoshika force-pushed the feat/tree-iterator branch 2 times, most recently from 60d6aa8 to 87528b9 Compare November 11, 2023 02:07
@jaytaph jaytaph merged commit acafe70 into main Nov 11, 2023
8 checks passed
@jaytaph jaytaph deleted the feat/tree-iterator branch November 11, 2023 09:15
let mut traversed_nodes = Vec::new();
for current_node_id in tree_iterator {
traversed_nodes.push(current_node_id);
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

Also:

let traversed_nodes = tree_iterator.collect::<Vec<_>>();

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.

4 participants