codesynapse_core/ts_extract/
c.rs1use 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 TsCExtractor;
9impl TsCExtractor {
10 pub fn extract(source: &[u8], path: &Path) -> Result<ExtractionFragment> {
11 let (file_id, _, file_node) = make_file_node(path);
12 let mut fragment = ExtractionFragment {
13 nodes: vec![file_node],
14 edges: vec![],
15 };
16
17 let lang = tree_sitter_c::LANGUAGE.into();
18
19 let fn_query = r#"
21 (function_definition
22 declarator: (function_declarator
23 declarator: (identifier) @func.name
24 )
25 )
26 "#;
27 if let Ok(mut captures) = run_query_named(source, &lang, fn_query) {
28 let names = captures.remove("func.name").unwrap_or_default();
29 for name in &names {
30 let fn_id = make_id(&[&file_id, name, "()"]);
31 fragment.nodes.push(Node {
32 id: fn_id.clone(),
33 label: format!("{}()", name),
34 file_type: "function".to_string(),
35 source_file: path.to_string_lossy().to_string(),
36 source_location: None,
37 community: None,
38 rationale: None,
39 docstring: None,
40 metadata: HashMap::new(),
41 });
42 add_contains_edge(&mut fragment, &file_id, fn_id, path);
43 }
44 }
45
46 let include_query = r#"
48 [
49 (preproc_include path: (string_literal) @include.path)
50 (preproc_include path: (system_lib_string) @include.path)
51 ]
52 "#;
53 if let Ok(mut captures) = run_query_named(source, &lang, include_query) {
54 let paths = captures.remove("include.path").unwrap_or_default();
55 for p in &paths {
56 let inc = p
57 .trim()
58 .trim_matches('"')
59 .trim_matches('<')
60 .trim_matches('>');
61 let inc_id = make_id(&[&file_id, inc]);
62 add_node_if_missing(
63 &mut fragment,
64 Node {
65 id: inc_id.clone(),
66 label: inc.to_string(),
67 file_type: "code".to_string(),
68 source_file: path.to_string_lossy().to_string(),
69 source_location: None,
70 community: None,
71 rationale: None,
72 docstring: None,
73 metadata: HashMap::new(),
74 },
75 );
76 fragment.edges.push(Edge {
77 source: file_id.clone(),
78 target: inc_id,
79 relation: "imports".to_string(),
80 confidence: "EXTRACTED".to_string(),
81 source_file: Some(path.to_string_lossy().to_string()),
82 weight: 1.0,
83 context: Some("import".to_string()),
84 });
85 }
86 }
87
88 let call_query = r#"
90 (call_expression
91 function: (identifier) @call.name
92 )
93 "#;
94 if let Ok(mut captures) = run_query_named(source, &lang, call_query) {
95 for name in captures.remove("call.name").unwrap_or_default() {
96 let callee_id = make_id(&[&file_id, &name, "()"]);
97 if fragment.nodes.iter().any(|n| n.id == callee_id) {
99 let already = fragment.edges.iter().any(|e| {
100 e.relation == "calls" && e.source == file_id && e.target == callee_id
101 });
102 if !already {
103 fragment.edges.push(Edge {
104 source: file_id.clone(),
105 target: callee_id,
106 relation: "calls".to_string(),
107 confidence: "EXTRACTED".to_string(),
108 source_file: Some(path.to_string_lossy().to_string()),
109 weight: 1.0,
110 context: Some("call".to_string()),
111 });
112 }
113 }
114 }
115 }
116
117 Ok(fragment)
118 }
119}
120
121impl LanguageExtractor for TsCExtractor {
122 fn file_extensions(&self) -> Vec<&'static str> {
123 vec!["c", "h"]
124 }
125 fn extract(&self, source: &[u8], path: &Path) -> Result<ExtractionFragment> {
126 Self::extract(source, path)
127 }
128 fn resolve_imports(&self, _imports: &[ImportNode]) -> Vec<Edge> {
129 vec![]
130 }
131 fn collect_type_refs(&self, _fragment: &mut ExtractionFragment) {}
132}