codesynapse-core 0.1.2

Core graph extraction and analysis engine for codesynapse — AST parsing, semantic embeddings, BM25 index
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
use super::{add_contains_edge, add_node_if_missing, make_file_node};
use crate::error::Result;
use crate::extract::{make_id, ImportNode, LanguageExtractor};
use crate::types::{Edge, ExtractionFragment, Node};
use std::collections::HashMap;
use std::path::Path;

pub struct TsKotlinExtractor;

fn kt_node(id: String, label: String, file_type: &str, path: &Path) -> Node {
    Node {
        id,
        label,
        file_type: file_type.to_string(),
        source_file: path.to_string_lossy().to_string(),
        source_location: None,
        community: None,
        rationale: None,
        docstring: None,
        metadata: HashMap::new(),
    }
}

/// Parse Kotlin delegation specifiers string (after the `:`) into (name, is_class) pairs.
/// "BaseProcessor(), Loggable" → [("BaseProcessor", true), ("Loggable", false)]
fn parse_delegation_specs(specs_str: &str) -> Vec<(String, bool)> {
    let mut result = Vec::new();
    let mut depth = 0i32;
    let mut angle = 0i32;
    let mut current = String::new();

    for ch in specs_str.chars() {
        match ch {
            '<' => {
                angle += 1;
                current.push(ch);
            }
            '>' => {
                angle -= 1;
                if angle < 0 {
                    angle = 0;
                }
                current.push(ch);
            }
            '(' if angle == 0 => {
                depth += 1;
                current.push(ch);
            }
            ')' if angle == 0 => {
                depth -= 1;
                if depth < 0 {
                    depth = 0;
                }
                current.push(ch);
            }
            ',' if depth == 0 && angle == 0 => {
                let spec = current.trim().to_string();
                if !spec.is_empty() {
                    let is_class = spec.contains('(');
                    let name = spec
                        .split('<')
                        .next()
                        .unwrap_or(&spec)
                        .split('(')
                        .next()
                        .unwrap_or(&spec)
                        .trim()
                        .to_string();
                    if !name.is_empty() {
                        result.push((name, is_class));
                    }
                }
                current = String::new();
            }
            _ => {
                current.push(ch);
            }
        }
    }
    if !current.trim().is_empty() {
        let spec = current.trim().to_string();
        let is_class = spec.contains('(');
        let name = spec
            .split('<')
            .next()
            .unwrap_or(&spec)
            .split('(')
            .next()
            .unwrap_or(&spec)
            .trim()
            .to_string();
        if !name.is_empty() {
            result.push((name, is_class));
        }
    }
    result
}

/// Extract a Kotlin type from a type annotation string like "Result<DataProcessor>".
/// Returns (bare_type, vec_of_generic_args)
fn parse_kotlin_type(type_str: &str) -> (String, Vec<String>) {
    let trimmed = type_str.trim().trim_end_matches('?');
    if let Some(lt) = trimmed.find('<') {
        let base = trimmed[..lt].trim().to_string();
        let args_str = trimmed[lt + 1..].trim_end_matches('>');
        let args: Vec<String> = args_str
            .split(',')
            .map(|s| s.trim().trim_end_matches('?').to_string())
            .filter(|s| !s.is_empty())
            .collect();
        (base, args)
    } else {
        (trimmed.to_string(), vec![])
    }
}

impl TsKotlinExtractor {
    pub fn extract(source: &[u8], path: &Path) -> Result<ExtractionFragment> {
        let (file_id, _, file_node) = make_file_node(path);
        let mut fragment = ExtractionFragment {
            nodes: vec![file_node],
            edges: vec![],
        };

        let content = std::str::from_utf8(source).unwrap_or("");
        let stem = file_id.clone();

        let mut current_class: Option<String> = None;

        for line in content.lines() {
            let trimmed = line.trim();

            // import header
            if let Some(rest) = trimmed.strip_prefix("import ") {
                let full = rest.split_whitespace().next().unwrap_or("").trim();
                let leaf = full.split('.').next_back().unwrap_or(full).to_string();
                if !leaf.is_empty() {
                    let id = make_id(&[&leaf]);
                    add_node_if_missing(&mut fragment, kt_node(id.clone(), leaf, "module", path));
                    fragment.edges.push(Edge {
                        source: file_id.clone(),
                        target: id,
                        relation: "imports".to_string(),
                        confidence: "EXTRACTED".to_string(),
                        source_file: Some(path.to_string_lossy().to_string()),
                        weight: 1.0,
                        context: Some("import".to_string()),
                    });
                }
                continue;
            }

            // class / interface / object / data class declaration
            let is_class_line = {
                let mut kw = None;
                for prefix in &[
                    "class ",
                    "data class ",
                    "open class ",
                    "abstract class ",
                    "sealed class ",
                    "interface ",
                    "object ",
                    "enum class ",
                ] {
                    if let Some(rest) = trimmed.strip_prefix(prefix) {
                        kw = Some((*prefix, rest));
                        break;
                    }
                }
                kw
            };

            if let Some((kw, rest)) = is_class_line {
                // Extract name (up to ':', '(', '<', '{', ' ')
                let class_name = rest
                    .split([':', '(', '<', '{'])
                    .next()
                    .unwrap_or("")
                    .trim()
                    .to_string();

                let class_file_type = match kw {
                    "interface " => "interface",
                    "enum class " => "enum",
                    _ => "class",
                };

                if !class_name.is_empty() {
                    let class_id = make_id(&[&stem, &class_name]);
                    add_node_if_missing(
                        &mut fragment,
                        kt_node(class_id.clone(), class_name.clone(), class_file_type, path),
                    );
                    add_contains_edge(&mut fragment, &file_id, class_id.clone(), path);
                    current_class = Some(class_id.clone());

                    // Parse delegation specifiers (after ':')
                    if let Some(colon_pos) = trimmed.find(':') {
                        let after_colon = &trimmed[colon_pos + 1..];
                        // Strip trailing '{', '{'
                        let specs_str = after_colon.split('{').next().unwrap_or(after_colon).trim();

                        for (base_name, is_class_base) in parse_delegation_specs(specs_str) {
                            let base_id = make_id(&[&stem, &base_name]);
                            add_node_if_missing(
                                &mut fragment,
                                kt_node(base_id.clone(), base_name, "code", path),
                            );
                            let relation = if is_class_base {
                                "inherits"
                            } else {
                                "implements"
                            };
                            fragment.edges.push(Edge {
                                source: class_id.clone(),
                                target: base_id,
                                relation: relation.to_string(),
                                confidence: "EXTRACTED".to_string(),
                                source_file: Some(path.to_string_lossy().to_string()),
                                weight: 1.0,
                                context: None,
                            });
                        }
                    }
                }
                continue;
            }

            // fun declaration inside or outside class
            if let Some(rest) = trimmed.strip_prefix("fun ").or_else(|| {
                // modifiers before fun
                let stripped = trimmed
                    .trim_start_matches("override ")
                    .trim_start_matches("private ")
                    .trim_start_matches("public ")
                    .trim_start_matches("protected ")
                    .trim_start_matches("internal ")
                    .trim_start_matches("suspend ")
                    .trim_start_matches("inline ")
                    .trim_start_matches("abstract ");
                if stripped.starts_with("fun ") {
                    stripped.strip_prefix("fun ")
                } else {
                    None
                }
            }) {
                // Extract function name and type info
                let func_name = rest
                    .split(['(', '<', ':', ' '])
                    .next()
                    .unwrap_or("")
                    .trim()
                    .to_string();

                if func_name.is_empty() {
                    continue;
                }

                let func_id = make_id(&[&stem, &func_name, "()"]);
                let label = format!(".{}()", func_name);
                let fn_file_type = if current_class.is_some() {
                    "method"
                } else {
                    "function"
                };
                add_node_if_missing(
                    &mut fragment,
                    kt_node(func_id.clone(), label, fn_file_type, path),
                );

                let owner = current_class.as_deref().unwrap_or(&file_id);
                fragment.edges.push(Edge {
                    source: owner.to_string(),
                    target: func_id.clone(),
                    relation: "method".to_string(),
                    confidence: "EXTRACTED".to_string(),
                    source_file: Some(path.to_string_lossy().to_string()),
                    weight: 1.0,
                    context: None,
                });
                fragment.edges.push(Edge {
                    source: file_id.clone(),
                    target: func_id.clone(),
                    relation: "contains".to_string(),
                    confidence: "EXTRACTED".to_string(),
                    source_file: Some(path.to_string_lossy().to_string()),
                    weight: 1.0,
                    context: None,
                });

                // Extract return type (after last `)`): `): Type`
                if let Some(paren_end) = rest.rfind(')') {
                    let after_paren = rest[paren_end + 1..].trim();
                    if let Some(ret_str) = after_paren.strip_prefix(':') {
                        let ret_type_str = ret_str
                            .split('{')
                            .next()
                            .unwrap_or(ret_str)
                            .split('=')
                            .next()
                            .unwrap_or(ret_str)
                            .trim();
                        let (base_type, generic_args) = parse_kotlin_type(ret_type_str);
                        if !base_type.is_empty() && base_type != "Unit" {
                            let id = make_id(&[&base_type]);
                            add_node_if_missing(
                                &mut fragment,
                                kt_node(id.clone(), base_type, "code", path),
                            );
                            fragment.edges.push(Edge {
                                source: func_id.clone(),
                                target: id,
                                relation: "references".to_string(),
                                confidence: "EXTRACTED".to_string(),
                                source_file: Some(path.to_string_lossy().to_string()),
                                weight: 1.0,
                                context: Some("return_type".to_string()),
                            });
                        }
                        for arg in generic_args {
                            if !arg.is_empty() {
                                let id = make_id(&[&arg]);
                                add_node_if_missing(
                                    &mut fragment,
                                    kt_node(id.clone(), arg, "code", path),
                                );
                                fragment.edges.push(Edge {
                                    source: func_id.clone(),
                                    target: id,
                                    relation: "references".to_string(),
                                    confidence: "EXTRACTED".to_string(),
                                    source_file: Some(path.to_string_lossy().to_string()),
                                    weight: 1.0,
                                    context: Some("generic_arg".to_string()),
                                });
                            }
                        }
                    }
                }

                // Extract parameter types from `(param: Type, ...)`
                if let Some(paren_start) = rest.find('(') {
                    let paren_end = rest.rfind(')').unwrap_or(rest.len());
                    if paren_end > paren_start {
                        let params_str = &rest[paren_start + 1..paren_end];
                        for param in params_str.split(',') {
                            let param = param.trim();
                            if let Some(colon_pos) = param.find(':') {
                                let type_str = param[colon_pos + 1..].trim();
                                let (base_type, _generic_args) = parse_kotlin_type(type_str);
                                if !base_type.is_empty()
                                    && !matches!(
                                        base_type.as_str(),
                                        "String"
                                            | "Int"
                                            | "Boolean"
                                            | "Long"
                                            | "Double"
                                            | "Float"
                                            | "Any"
                                    )
                                {
                                    let id = make_id(&[&base_type]);
                                    add_node_if_missing(
                                        &mut fragment,
                                        kt_node(id.clone(), base_type.clone(), "code", path),
                                    );
                                    fragment.edges.push(Edge {
                                        source: func_id.clone(),
                                        target: id,
                                        relation: "references".to_string(),
                                        confidence: "EXTRACTED".to_string(),
                                        source_file: Some(path.to_string_lossy().to_string()),
                                        weight: 1.0,
                                        context: Some("parameter_type".to_string()),
                                    });
                                }
                            }
                        }
                    }
                }
                continue;
            }

            // Property/field declaration: `var name: Type` or `val name: Type`
            if let Some(rest) = trimmed
                .strip_prefix("var ")
                .or_else(|| trimmed.strip_prefix("val "))
            {
                if let Some(owner) = &current_class {
                    if let Some(colon_pos) = rest.find(':') {
                        let type_str = rest[colon_pos + 1..].split('=').next().unwrap_or("").trim();
                        let (base_type, _) = parse_kotlin_type(type_str);
                        if !base_type.is_empty()
                            && !matches!(
                                base_type.as_str(),
                                "String" | "Int" | "Boolean" | "Long" | "Double" | "Float"
                            )
                        {
                            let id = make_id(&[&base_type]);
                            add_node_if_missing(
                                &mut fragment,
                                kt_node(id.clone(), base_type, "code", path),
                            );
                            fragment.edges.push(Edge {
                                source: owner.clone(),
                                target: id,
                                relation: "references".to_string(),
                                confidence: "EXTRACTED".to_string(),
                                source_file: Some(path.to_string_lossy().to_string()),
                                weight: 1.0,
                                context: Some("field".to_string()),
                            });
                        }
                    }
                }
                continue;
            }

            // Track closing of class body (simple heuristic: line with just "}")
            if trimmed == "}" {
                // Don't pop current_class on every '}' - keep it for the whole file
                // since Kotlin functions can be nested inside class body
            }
        }

        Ok(fragment)
    }
}

impl LanguageExtractor for TsKotlinExtractor {
    fn file_extensions(&self) -> Vec<&'static str> {
        vec!["kt", "kts"]
    }
    fn extract(&self, source: &[u8], path: &Path) -> Result<ExtractionFragment> {
        Self::extract(source, path)
    }
    fn resolve_imports(&self, _imports: &[ImportNode]) -> Vec<Edge> {
        vec![]
    }
    fn collect_type_refs(&self, _fragment: &mut ExtractionFragment) {}
}