probe_code/language/
cpp.rs

1use super::language_trait::LanguageImpl;
2use tree_sitter::{Language as TSLanguage, Node};
3
4/// Implementation of LanguageImpl for C++
5pub struct CppLanguage;
6
7impl Default for CppLanguage {
8    fn default() -> Self {
9        Self::new()
10    }
11}
12
13impl CppLanguage {
14    pub fn new() -> Self {
15        CppLanguage
16    }
17}
18
19impl LanguageImpl for CppLanguage {
20    fn get_tree_sitter_language(&self) -> TSLanguage {
21        tree_sitter_cpp::LANGUAGE.into()
22    }
23
24    fn get_extension(&self) -> &'static str {
25        "cpp"
26    }
27
28    fn is_acceptable_parent(&self, node: &Node) -> bool {
29        matches!(
30            node.kind(),
31            "function_definition"
32                | "declaration"
33                | "struct_specifier"
34                | "class_specifier"
35                | "enum_specifier"
36                | "namespace_definition"
37        )
38    }
39
40    fn is_test_node(&self, node: &Node, source: &[u8]) -> bool {
41        let debug_mode = std::env::var("DEBUG").unwrap_or_default() == "1";
42        let node_type = node.kind();
43
44        // C++: Check function_definition nodes with test in the name
45        if node_type == "function_definition" {
46            let mut cursor = node.walk();
47            for child in node.children(&mut cursor) {
48                if child.kind() == "function_declarator" {
49                    let mut subcursor = child.walk();
50                    for subchild in child.children(&mut subcursor) {
51                        if subchild.kind() == "identifier" {
52                            let name = subchild.utf8_text(source).unwrap_or("");
53                            if name.contains("test") || name.contains("Test") {
54                                if debug_mode {
55                                    println!("DEBUG: Test node detected (C++): test function");
56                                }
57                                return true;
58                            }
59                        }
60                    }
61                }
62            }
63        }
64
65        false
66    }
67}