Skip to main content

codesynapse_core/ts_extract/
pascal.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 TsPascalExtractor;
9
10impl TsPascalExtractor {
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_pascal::LANGUAGE.into();
19
20        // procedure Foo; / function Bar: T;  →  declProc > identifier (direct child)
21        // Covers both interface declarations and implementation definitions (via defProc > declProc)
22        let proc_query = r#"
23            (declProc (identifier) @proc.name)
24        "#;
25        if let Ok(mut captures) = run_query_named(source, &lang, proc_query) {
26            for name in captures.remove("proc.name").unwrap_or_default() {
27                let id = make_id(&[&file_id, &name]);
28                fragment.nodes.push(Node {
29                    id: id.clone(),
30                    label: name,
31                    file_type: "function".to_string(),
32                    source_file: path.to_string_lossy().to_string(),
33                    source_location: None,
34                    community: None,
35                    rationale: None,
36                    docstring: None,
37                    metadata: HashMap::new(),
38                });
39                add_contains_edge(&mut fragment, &file_id, id, path);
40            }
41        }
42
43        // uses SysUtils;  →  declUses > moduleName > identifier
44        let uses_query = r#"
45            (declUses (moduleName (identifier) @import.name))
46        "#;
47        if let Ok(mut captures) = run_query_named(source, &lang, uses_query) {
48            for name in captures.remove("import.name").unwrap_or_default() {
49                let mod_id = make_id(&[&file_id, &name]);
50                let mod_node = Node {
51                    id: mod_id.clone(),
52                    label: name,
53                    file_type: "module".to_string(),
54                    source_file: path.to_string_lossy().to_string(),
55                    source_location: None,
56                    community: None,
57                    rationale: None,
58                    docstring: None,
59                    metadata: HashMap::new(),
60                };
61                add_node_if_missing(&mut fragment, mod_node);
62                fragment.edges.push(Edge {
63                    source: file_id.clone(),
64                    target: mod_id,
65                    relation: "imports".to_string(),
66                    confidence: "EXTRACTED".to_string(),
67                    source_file: Some(path.to_string_lossy().to_string()),
68                    weight: 1.0,
69                    context: None,
70                });
71            }
72        }
73
74        Ok(fragment)
75    }
76}
77
78impl LanguageExtractor for TsPascalExtractor {
79    fn file_extensions(&self) -> Vec<&'static str> {
80        vec!["pas", "pp", "inc"]
81    }
82    fn extract(&self, source: &[u8], path: &Path) -> Result<ExtractionFragment> {
83        Self::extract(source, path)
84    }
85    fn resolve_imports(&self, _imports: &[ImportNode]) -> Vec<Edge> {
86        vec![]
87    }
88    fn collect_type_refs(&self, _fragment: &mut ExtractionFragment) {}
89}