Skip to main content

codesynapse_core/ts_extract/
cmake.rs

1use super::{add_contains_edge, add_node_if_missing, make_file_node, run_query_named};
2use crate::error::Result;
3use crate::extract::{make_id, ImportNode, LanguageExtractor};
4use crate::types::{Edge, ExtractionFragment, Node};
5use std::collections::HashMap;
6use std::path::Path;
7
8pub struct TsCmakeExtractor;
9
10impl TsCmakeExtractor {
11    pub fn extract(source: &[u8], path: &Path) -> Result<ExtractionFragment> {
12        let (file_id, _, file_node) = make_file_node(path);
13        let mut fragment = ExtractionFragment {
14            nodes: vec![file_node],
15            edges: vec![],
16        };
17
18        let lang = tree_sitter_cmake::LANGUAGE.into();
19
20        let func_query = r#"
21            (function_command
22                (argument_list
23                    (argument
24                        (unquoted_argument) @func.name)))
25        "#;
26        if let Ok(mut captures) = run_query_named(source, &lang, func_query) {
27            for name in captures.remove("func.name").unwrap_or_default() {
28                let label = format!("{}()", name);
29                let id = make_id(&[&file_id, &name]);
30                fragment.nodes.push(Node {
31                    id: id.clone(),
32                    label,
33                    file_type: "function".to_string(),
34                    source_file: path.to_string_lossy().to_string(),
35                    source_location: None,
36                    community: None,
37                    rationale: None,
38                    docstring: None,
39                    metadata: HashMap::new(),
40                });
41                add_contains_edge(&mut fragment, &file_id, id, path);
42            }
43        }
44
45        let macro_query = r#"
46            (macro_command
47                (argument_list
48                    (argument
49                        (unquoted_argument) @macro.name)))
50        "#;
51        if let Ok(mut captures) = run_query_named(source, &lang, macro_query) {
52            for name in captures.remove("macro.name").unwrap_or_default() {
53                let label = format!("{}()", name);
54                let id = make_id(&[&file_id, &name]);
55                fragment.nodes.push(Node {
56                    id: id.clone(),
57                    label,
58                    file_type: "macro".to_string(),
59                    source_file: path.to_string_lossy().to_string(),
60                    source_location: None,
61                    community: None,
62                    rationale: None,
63                    docstring: None,
64                    metadata: HashMap::new(),
65                });
66                add_contains_edge(&mut fragment, &file_id, id, path);
67            }
68        }
69
70        let include_query = r#"
71            ((normal_command
72                (identifier) @cmd.name
73                (argument_list
74                    (argument
75                        (quoted_argument) @include.path)))
76             (#eq? @cmd.name "include"))
77        "#;
78        if let Ok(mut captures) = run_query_named(source, &lang, include_query) {
79            for path_str in captures.remove("include.path").unwrap_or_default() {
80                let module = path_str.trim_matches('"').trim();
81                let mod_id = make_id(&[&file_id, module]);
82                let mod_node = Node {
83                    id: mod_id.clone(),
84                    label: module.to_string(),
85                    file_type: "module".to_string(),
86                    source_file: path.to_string_lossy().to_string(),
87                    source_location: None,
88                    community: None,
89                    rationale: None,
90                    docstring: None,
91                    metadata: HashMap::new(),
92                };
93                add_node_if_missing(&mut fragment, mod_node);
94                fragment.edges.push(Edge {
95                    source: file_id.clone(),
96                    target: mod_id,
97                    relation: "imports".to_string(),
98                    confidence: "EXTRACTED".to_string(),
99                    source_file: Some(path.to_string_lossy().to_string()),
100                    weight: 1.0,
101                    context: None,
102                });
103            }
104        }
105
106        Ok(fragment)
107    }
108}
109
110impl LanguageExtractor for TsCmakeExtractor {
111    fn file_extensions(&self) -> Vec<&'static str> {
112        vec!["cmake", "CMakeLists.txt"]
113    }
114    fn extract(&self, source: &[u8], path: &Path) -> Result<ExtractionFragment> {
115        Self::extract(source, path)
116    }
117    fn resolve_imports(&self, _imports: &[ImportNode]) -> Vec<Edge> {
118        vec![]
119    }
120    fn collect_type_refs(&self, _fragment: &mut ExtractionFragment) {}
121}