semantic_code_edit_mcp/languages/
traits.rs

1use anyhow::Result;
2use tree_sitter::{Node, Tree};
3
4/// Default editor implementation with basic tree-sitter validation
5#[derive(Debug, Clone)]
6pub struct DefaultEditor;
7
8impl DefaultEditor {
9    pub fn new() -> Self {
10        Self
11    }
12}
13
14impl Default for DefaultEditor {
15    fn default() -> Self {
16        Self::new()
17    }
18}
19
20/// Trait for language-specific operations like validation and formatting
21pub trait LanguageEditor: Send + Sync {
22    /// Collect syntax error line numbers from a tree-sitter parse tree
23    fn collect_errors(&self, tree: &Tree, _content: &str) -> Vec<usize> {
24        collect_errors(tree)
25            .into_iter()
26            .map(|node| node.start_position().row)
27            .collect()
28    }
29
30    /// Format code according to language conventions
31    fn format_code(&self, source: &str) -> Result<String> {
32        Ok(source.to_string())
33    }
34}
35
36impl LanguageEditor for DefaultEditor {
37    // Uses all default implementations
38}
39
40pub fn collect_errors<'tree>(tree: &'tree Tree) -> Vec<Node<'tree>> {
41    let mut errors = vec![];
42    collect_errors_recursive(tree.root_node(), &mut errors);
43    errors
44}
45
46fn collect_errors_recursive<'tree>(node: Node<'tree>, errors: &mut Vec<Node<'tree>>) {
47    // Check if this node is an error
48    if node.is_error() || node.is_missing() {
49        errors.push(node);
50    }
51
52    // Recursively check all children
53    for child in node.children(&mut node.walk()) {
54        collect_errors_recursive(child, errors);
55    }
56}