use anyhow::Result;
use tree_sitter::{Node, Tree};
#[derive(Debug, Clone)]
pub struct DefaultEditor;
impl DefaultEditor {
pub fn new() -> Self {
Self
}
}
impl Default for DefaultEditor {
fn default() -> Self {
Self::new()
}
}
pub trait LanguageEditor: Send + Sync {
fn collect_errors(&self, tree: &Tree, _content: &str) -> Vec<usize> {
collect_errors(tree)
.into_iter()
.map(|node| node.start_position().row)
.collect()
}
fn format_code(&self, source: &str) -> Result<String> {
Ok(source.to_string())
}
}
impl LanguageEditor for DefaultEditor {
}
pub fn collect_errors<'tree>(tree: &'tree Tree) -> Vec<Node<'tree>> {
let mut errors = vec![];
collect_errors_recursive(tree.root_node(), &mut errors);
errors
}
fn collect_errors_recursive<'tree>(node: Node<'tree>, errors: &mut Vec<Node<'tree>>) {
if node.is_error() || node.is_missing() {
errors.push(node);
}
for child in node.children(&mut node.walk()) {
collect_errors_recursive(child, errors);
}
}