Skip to main content

codesynapse_core/ts_extract/
lua.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 TsLuaExtractor;
9
10impl TsLuaExtractor {
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_lua::LANGUAGE.into();
19
20        let func_query = r#"
21            (function_declaration
22                name: (identifier) @func.name
23            )
24        "#;
25        if let Ok(mut captures) = run_query_named(source, &lang, func_query) {
26            for name in captures.remove("func.name").unwrap_or_default() {
27                let label = format!("{}()", name);
28                let id = make_id(&[&file_id, &name]);
29                fragment.nodes.push(Node {
30                    id: id.clone(),
31                    label,
32                    file_type: "function".to_string(),
33                    source_file: path.to_string_lossy().to_string(),
34                    source_location: None,
35                    community: None,
36                    rationale: None,
37                    docstring: None,
38                    metadata: HashMap::new(),
39                });
40                add_contains_edge(&mut fragment, &file_id, id, path);
41            }
42        }
43
44        let require_query = r#"
45            ((function_call
46                name: (identifier) @func.name
47                arguments: (arguments
48                    (string) @require.path))
49             (#eq? @func.name "require"))
50        "#;
51        if let Ok(mut captures) = run_query_named(source, &lang, require_query) {
52            for path_str in captures.remove("require.path").unwrap_or_default() {
53                let module = path_str.trim_matches('"').trim().trim_matches('\'');
54                let mod_id = make_id(&[&file_id, module]);
55                let mod_node = Node {
56                    id: mod_id.clone(),
57                    label: module.to_string(),
58                    file_type: "module".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_node_if_missing(&mut fragment, mod_node);
67                fragment.edges.push(Edge {
68                    source: file_id.clone(),
69                    target: mod_id,
70                    relation: "imports".to_string(),
71                    confidence: "EXTRACTED".to_string(),
72                    source_file: Some(path.to_string_lossy().to_string()),
73                    weight: 1.0,
74                    context: None,
75                });
76            }
77        }
78
79        Ok(fragment)
80    }
81}
82
83impl LanguageExtractor for TsLuaExtractor {
84    fn file_extensions(&self) -> Vec<&'static str> {
85        vec!["lua"]
86    }
87    fn extract(&self, source: &[u8], path: &Path) -> Result<ExtractionFragment> {
88        Self::extract(source, path)
89    }
90    fn resolve_imports(&self, _imports: &[ImportNode]) -> Vec<Edge> {
91        vec![]
92    }
93    fn collect_type_refs(&self, _fragment: &mut ExtractionFragment) {}
94}