use crate::tree::{NodeId, SyntaxTree};
impl SyntaxTree<'_> {
pub fn dump(&self, id: NodeId, with_text: bool) -> String {
let mut out = String::new();
self.dump_into(id, with_text, &mut out);
out
}
fn dump_into(&self, id: NodeId, with_text: bool, out: &mut String) {
out.push_str(&format!("{:?}", self.kind(id)));
let mut kids = self.children(id).peekable();
if kids.peek().is_some() {
out.push('(');
let mut first = true;
for c in kids {
if !first {
out.push(' ');
}
first = false;
self.dump_into(c, with_text, out);
}
out.push(')');
} else if with_text {
out.push_str(&format!("{:?}", self.text(id)));
}
}
pub fn errors(&self) -> impl Iterator<Item = NodeId> + '_ {
self.descendants(self.root()).filter(|&n| self.kind(n) == crate::SyntaxKind::Error)
}
}