probe_code/language/
go.rs

1use super::language_trait::LanguageImpl;
2use tree_sitter::{Language as TSLanguage, Node};
3
4/// Implementation of LanguageImpl for Go
5pub struct GoLanguage;
6
7impl Default for GoLanguage {
8    fn default() -> Self {
9        Self::new()
10    }
11}
12
13impl GoLanguage {
14    pub fn new() -> Self {
15        GoLanguage
16    }
17}
18
19impl LanguageImpl for GoLanguage {
20    fn get_tree_sitter_language(&self) -> TSLanguage {
21        tree_sitter_go::LANGUAGE.into()
22    }
23
24    fn get_extension(&self) -> &'static str {
25        "go"
26    }
27
28    fn is_acceptable_parent(&self, node: &Node) -> bool {
29        matches!(
30            node.kind(),
31            "function_declaration" |
32            "method_declaration" |
33            "type_declaration" |
34            "struct_type" |
35            "interface_type" |
36            // "const_declaration" |
37            // "var_declaration" |
38            // "const_spec" |
39            // "var_spec" |
40            // "short_var_declaration" |
41            "type_spec" // Added for type definitions
42        )
43    }
44
45    fn is_test_node(&self, node: &Node, source: &[u8]) -> bool {
46        let debug_mode = std::env::var("DEBUG").unwrap_or_default() == "1";
47        let node_type = node.kind();
48
49        // Go: Check function_declaration nodes with names starting with Test
50        if node_type == "function_declaration" {
51            let mut cursor = node.walk();
52            for child in node.children(&mut cursor) {
53                if child.kind() == "identifier" {
54                    let name = child.utf8_text(source).unwrap_or("");
55                    if name.starts_with("Test") {
56                        if debug_mode {
57                            println!("DEBUG: Test node detected (Go): Test function");
58                        }
59                        return true;
60                    }
61                }
62            }
63        }
64
65        false
66    }
67
68    fn find_parent_function<'a>(&self, node: Node<'a>) -> Option<Node<'a>> {
69        let debug_mode = std::env::var("DEBUG").unwrap_or_default() == "1";
70
71        if debug_mode {
72            println!(
73                "DEBUG: Finding parent function for {node_kind}",
74                node_kind = node.kind()
75            );
76        }
77
78        let mut current = node;
79
80        while let Some(parent) = current.parent() {
81            if parent.kind() == "function_declaration" || parent.kind() == "method_declaration" {
82                if debug_mode {
83                    println!(
84                        "DEBUG: Found parent function: {parent_kind}",
85                        parent_kind = parent.kind()
86                    );
87                }
88                return Some(parent);
89            }
90            current = parent;
91        }
92
93        if debug_mode {
94            println!(
95                "DEBUG: No parent function found for {node_kind}",
96                node_kind = node.kind()
97            );
98        }
99
100        None
101    }
102}