semantic_code_edit_mcp/languages/
traits.rs1use anyhow::Result;
2use tree_sitter::{Node, Tree};
3
4#[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
20pub trait LanguageEditor: Send + Sync {
22 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 fn format_code(&self, source: &str) -> Result<String> {
32 Ok(source.to_string())
33 }
34}
35
36impl LanguageEditor for DefaultEditor {
37 }
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 if node.is_error() || node.is_missing() {
49 errors.push(node);
50 }
51
52 for child in node.children(&mut node.walk()) {
54 collect_errors_recursive(child, errors);
55 }
56}