use std::collections::VecDeque; use crate::tree_sitter::Node; use crate::{Layer, Syntax}; pub struct TreeCursor<'tree> { syntax: &'tree Syntax, current: Layer, cursor: tree_sitter::TreeCursor<'tree>, } impl<'tree> TreeCursor<'tree> { pub(crate) fn new(syntax: &'tree Syntax) -> Self { let cursor = syntax.tree().walk(); Self { syntax, current: syntax.root, cursor, } } pub fn node(&self) -> Node<'tree> { self.cursor.node() } pub fn goto_parent(&mut self) -> bool { if self.cursor.goto_parent() { return true; }; loop { // Ascend to the parent layer if one exists. let Some(parent) = self.syntax.layer(self.current).parent else { return false; }; self.current = parent; if let Some(tree) = self.syntax.layer(self.current).tree() { self.cursor = tree.walk(); break; } } true } pub fn goto_parent_with
(&mut self, predicate: P) -> bool
where
P: Fn(&Node) -> bool,
{
while self.goto_parent() {
if predicate(&self.node()) {
return true;
}
}
false
}
pub fn goto_first_child(&mut self) -> bool {
let range = self.cursor.node().byte_range();
let layer = self.syntax.layer(self.current);
if let Some((layer, tree)) = layer
.injection_at_byte_idx(range.start)
.filter(|injection| injection.range.end >= range.end)
.and_then(|injection| {
Some((injection.layer, self.syntax.layer(injection.layer).tree()?))
})
{
// Switch to the child layer.
self.current = layer;
self.cursor = tree.walk();
return true;
}
self.cursor.goto_first_child()
}
pub fn goto_next_sibling(&mut self) -> bool {
self.cursor.goto_next_sibling()
}
pub fn goto_previous_sibling(&mut self) -> bool {
self.cursor.goto_previous_sibling()
}
pub fn reset_to_byte_range(&mut self, start: u32, end: u32) {
let (layer, tree) = self.syntax.layer_and_tree_for_byte_range(start, end);
self.current = layer;
self.cursor = tree.walk();
loop {
let node = self.cursor.node();
if start < node.start_byte() || end > node.end_byte() {
self.cursor.goto_parent();
break;
}
if self.cursor.goto_first_child_for_byte(start).is_none() {
break;
}
}
}
/// Returns an iterator over the children of the node the TreeCursor is on
/// at the time this is called.
pub fn children<'a>(&'a mut self) -> ChildIter<'a, 'tree> {
let parent = self.node();
ChildIter {
cursor: self,
parent,
}
}
}
pub struct ChildIter<'a, 'tree> {
cursor: &'a mut TreeCursor<'tree>,
parent: Node<'tree>,
}
impl<'tree> Iterator for ChildIter<'_, 'tree> {
type Item = Node<'tree>;
fn next(&mut self) -> Option