skelecode 1.0.0

Code structure scanner that generates project-wide context graphs for humans and AI
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
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
use std::path::Path;
use tree_sitter::{Node, Parser};

use super::LanguageParser;
use crate::ir::*;

pub struct JavaParser {
    parser: std::cell::RefCell<Parser>,
}

impl Default for JavaParser {
    fn default() -> Self {
        Self::new()
    }
}

impl JavaParser {
    pub fn new() -> Self {
        let mut parser = Parser::new();
        parser
            .set_language(&tree_sitter_java::LANGUAGE.into())
            .expect("Error loading Java grammar");
        Self {
            parser: std::cell::RefCell::new(parser),
        }
    }
}

impl LanguageParser for JavaParser {
    fn language(&self) -> Language {
        Language::Java
    }

    fn can_parse(&self, path: &Path) -> bool {
        path.extension().is_some_and(|e| e == "java")
    }

    fn parse_file(&self, path: &Path, source: &str) -> Option<Module> {
        let tree = self.parser.borrow_mut().parse(source, None)?;
        let root = tree.root_node();

        // Java path conventionally uses packages. We can extract package name from file text.
        let mut package_name = String::new();
        let mut types = Vec::new();
        let functions = Vec::new();
        let mut imports = Vec::new();

        let mut cursor = root.walk();
        for child in root.children(&mut cursor) {
            match child.kind() {
                "package_declaration" => {
                    if let Some(pkg_name) = parse_package_name(child, source) {
                        package_name = pkg_name;
                    }
                }
                "import_declaration" => {
                    if let Some(imp) = parse_java_import(child, source) {
                        imports.push(imp);
                    }
                }
                "class_declaration" => {
                    if let Some(td) = parse_class(child, source) {
                        types.push(td);
                    }
                }
                "interface_declaration" => {
                    if let Some(td) = parse_interface(child, source) {
                        types.push(td);
                    }
                }
                "enum_declaration" => {
                    if let Some(td) = parse_enum(child, source) {
                        types.push(td);
                    }
                }
                "record_declaration" => {
                    if let Some(td) = parse_record(child, source) {
                        types.push(td);
                    }
                }
                _ => {}
            }
        }

        // If package wasn't found, fallback to file path logic
        let module_path = if !package_name.is_empty() {
            package_name
        } else {
            path.file_stem()
                .and_then(|s| s.to_str())
                .unwrap_or("unknown")
                .to_string()
        };

        Some(Module {
            path: module_path,
            language: Language::Java,
            types,
            functions,
            imports,
        })
    }
}

fn node_text<'a>(node: Node<'a>, source: &'a str) -> &'a str {
    &source[node.byte_range()]
}

// ─── Import parsing ─────────────────────────────────────────────────────────────

fn parse_java_import(node: Node, source: &str) -> Option<ImportedName> {
    let raw = node_text(node, source).trim();
    let raw = raw.strip_prefix("import").unwrap_or(raw).trim();
    let raw = raw.strip_prefix("static").unwrap_or(raw).trim();
    let raw = raw.strip_suffix(';').unwrap_or(raw).trim();

    // Skip wildcards
    if raw.ends_with(".*") {
        return None;
    }

    dot_qualified(raw, None)
}

/// Split `"a.b.ClassName"` at the last dot into `module_path::ClassName`.
/// Only accepts names whose last segment starts with an uppercase letter.
fn dot_qualified(path: &str, alias_override: Option<&str>) -> Option<ImportedName> {
    let dot = path.rfind('.')?;
    let module_path = &path[..dot];
    let type_name = &path[dot + 1..];
    // Heuristic: type names start with uppercase
    if !type_name.starts_with(|c: char| c.is_uppercase()) {
        return None;
    }
    let alias = alias_override.unwrap_or(type_name);
    Some(ImportedName {
        alias: alias.to_string(),
        qualified: format!("{}::{}", module_path, type_name),
    })
}

fn parse_package_name(node: Node, source: &str) -> Option<String> {
    let mut cursor = node.walk();
    for child in node.children(&mut cursor) {
        if child.kind() == "scoped_identifier" || child.kind() == "identifier" {
            return Some(node_text(child, source).to_string());
        }
    }
    None
}

struct Modifiers {
    visibility: Visibility,
    is_static: bool,
}

fn extract_modifiers(node: Node, source: &str) -> Modifiers {
    let mut vis = Visibility::Internal; // Package-private by default in Java
    let mut is_static = false;

    let mut cursor = node.walk();
    for child in node.children(&mut cursor) {
        if child.kind() == "modifiers" {
            let mut mod_cursor = child.walk();
            for m in child.children(&mut mod_cursor) {
                match node_text(m, source) {
                    "public" => vis = Visibility::Public,
                    "private" => vis = Visibility::Private,
                    "protected" => vis = Visibility::Protected,
                    "static" => is_static = true,
                    _ => {}
                }
            }
            break;
        }
    }

    Modifiers {
        visibility: vis,
        is_static,
    }
}

fn extract_type_params(node: Node, source: &str) -> Vec<String> {
    if let Some(tp) = node.child_by_field_name("type_parameters") {
        let mut cursor = tp.walk();
        let mut params = Vec::new();
        for child in tp.children(&mut cursor) {
            if child.kind() == "type_parameter" {
                params.push(node_text(child, source).to_string());
            }
        }
        return params;
    }
    Vec::new()
}

fn parse_class(node: Node, source: &str) -> Option<TypeDef> {
    let name = node.child_by_field_name("name")?;
    let name_str = node_text(name, source).to_string();
    let mods = extract_modifiers(node, source);
    let type_params = extract_type_params(node, source);

    let mut fields = Vec::new();
    let mut methods = Vec::new();
    let mut relations = Vec::new();

    // Superclass
    if let Some(superclass) = node.child_by_field_name("superclass") {
        // superclass is usually "superclass" node with a type child
        relations.push(TypeRelation {
            kind: RelationKind::Extends,
            target: node_text(superclass, source).trim_start_matches("extends ").trim().to_string(),
        });
    }

    // Interfaces
    if let Some(interfaces) = node.child_by_field_name("interfaces") {
        let mut cursor = interfaces.walk();
        for child in interfaces.children(&mut cursor) {
            if child.kind() == "type_list" {
                let mut type_cursor = child.walk();
                for type_node in child.children(&mut type_cursor) {
                    if type_node.kind() == "type_identifier" || type_node.kind() == "generic_type" {
                        relations.push(TypeRelation {
                            kind: RelationKind::Implements,
                            target: node_text(type_node, source).to_string(),
                        });
                    }
                }
            }
        }
    }

    if let Some(body) = node.child_by_field_name("body") {
        let mut cursor = body.walk();
        for child in body.children(&mut cursor) {
            match child.kind() {
                "field_declaration" => {
                    fields.extend(parse_fields(child, source));
                }
                "method_declaration" => {
                    if let Some(m) = parse_method(child, source) {
                        methods.push(m);
                    }
                }
                "constructor_declaration" => {
                    if let Some(m) = parse_constructor(child, source) {
                        methods.push(m);
                    }
                }
                _ => {}
            }
        }
    }

    Some(TypeDef {
        name: name_str,
        kind: TypeKind::Class,
        visibility: mods.visibility,
        fields,
        methods,
        relations,
        annotations: Vec::new(),
        type_params,
        enum_variants: Vec::new(),
    })
}

fn parse_interface(node: Node, source: &str) -> Option<TypeDef> {
    let name = node.child_by_field_name("name")?;
    let name_str = node_text(name, source).to_string();
    let mods = extract_modifiers(node, source);
    let type_params = extract_type_params(node, source);

    let mut fields = Vec::new();
    let mut methods = Vec::new();
    let mut relations = Vec::new();

    // Extended interfaces
    if let Some(interfaces) = node.child_by_field_name("interfaces") {
        let mut cursor = interfaces.walk();
        for child in interfaces.children(&mut cursor) {
            if child.kind() == "type_list" {
                let mut type_cursor = child.walk();
                for type_node in child.children(&mut type_cursor) {
                    if type_node.kind() == "type_identifier" || type_node.kind() == "generic_type" {
                        relations.push(TypeRelation {
                            kind: RelationKind::Extends,
                            target: node_text(type_node, source).to_string(),
                        });
                    }
                }
            }
        }
    }

    if let Some(body) = node.child_by_field_name("body") {
        let mut cursor = body.walk();
        for child in body.children(&mut cursor) {
            match child.kind() {
                "constant_declaration" => {
                    fields.extend(parse_fields(child, source));
                }
                "method_declaration" => {
                    if let Some(m) = parse_method(child, source) {
                        methods.push(m);
                    }
                }
                _ => {}
            }
        }
    }

    Some(TypeDef {
        name: name_str,
        kind: TypeKind::Interface,
        visibility: mods.visibility,
        fields,
        methods,
        relations,
        annotations: Vec::new(),
        type_params,
        enum_variants: Vec::new(),
    })
}

fn parse_enum(node: Node, source: &str) -> Option<TypeDef> {
    let name = node.child_by_field_name("name")?;
    let name_str = node_text(name, source).to_string();
    let mods = extract_modifiers(node, source);

    let mut variants = Vec::new();
    let mut fields = Vec::new();
    let mut methods = Vec::new();

    if let Some(body) = node.child_by_field_name("body") {
        let mut cursor = body.walk();
        for child in body.children(&mut cursor) {
            match child.kind() {
                "enum_constant" => {
                    if let Some(vname) = child.child_by_field_name("name") {
                        let variant_name = node_text(vname, source).to_string();
                        // Also include arguments if present (e.g. RED(255, 0, 0))
                        let mut has_args = false;
                        let mut inner_cursor = child.walk();
                        for vc in child.children(&mut inner_cursor) {
                            if vc.kind() == "argument_list" {
                                variants.push(format!("{}{}", variant_name, node_text(vc, source)));
                                has_args = true;
                                break;
                            }
                        }
                        if !has_args {
                            variants.push(variant_name);
                        }
                    }
                }
                "field_declaration" => {
                    fields.extend(parse_fields(child, source));
                }
                "method_declaration" => {
                    if let Some(m) = parse_method(child, source) {
                        methods.push(m);
                    }
                }
                "constructor_declaration" => {
                    if let Some(m) = parse_constructor(child, source) {
                        methods.push(m);
                    }
                }
                _ => {}
            }
        }
    }

    Some(TypeDef {
        name: name_str,
        kind: TypeKind::Enum,
        visibility: mods.visibility,
        fields,
        methods,
        relations: Vec::new(),
        annotations: Vec::new(),
        type_params: Vec::new(),
        enum_variants: variants,
    })
}

fn parse_record(node: Node, source: &str) -> Option<TypeDef> {
    let name = node.child_by_field_name("name")?;
    let name_str = node_text(name, source).to_string();
    let mods = extract_modifiers(node, source);
    let type_params = extract_type_params(node, source);

    let mut fields = Vec::new();
    let mut methods = Vec::new();

    // Record components (which become fields and methods implicitly, but we capture them as fields)
    if let Some(params) = node.child_by_field_name("parameters") {
        let mut cursor = params.walk();
        for child in params.children(&mut cursor) {
            if child.kind() == "record_declaration_parameter" {
                if let (Some(type_node), Some(name_node)) = (
                    child.child_by_field_name("type"),
                    child.child_by_field_name("name"),
                ) {
                    fields.push(Field {
                        name: node_text(name_node, source).to_string(),
                        type_name: node_text(type_node, source).to_string(),
                        visibility: Visibility::Private, // record components are implicitly private final fields
                    });
                }
            }
        }
    }

    if let Some(body) = node.child_by_field_name("body") {
        let mut cursor = body.walk();
        for child in body.children(&mut cursor) {
            match child.kind() {
                // Record can have explicit fields (static), compact constructors, methods
                "field_declaration" => {
                    fields.extend(parse_fields(child, source));
                }
                "method_declaration" => {
                    if let Some(m) = parse_method(child, source) {
                        methods.push(m);
                    }
                }
                "compact_constructor_declaration" => {
                    // Similar to constructor but without params
                    if let Some(mname) = child.child_by_field_name("name") {
                        let calls = if let Some(body_node) = child.child_by_field_name("body") {
                            extract_calls(body_node, source)
                        } else {
                            Vec::new()
                        };
                        let m_mods = extract_modifiers(child, source);
                        methods.push(Method {
                            name: node_text(mname, source).to_string(),
                            params: Vec::new(),
                            return_type: None, // Constructors have no return type
                            visibility: m_mods.visibility,
                            calls,
                            callers: Vec::new(),
                            annotations: Vec::new(),
                            is_static: false,
                        });
                    }
                }
                "constructor_declaration" => {
                    if let Some(m) = parse_constructor(child, source) {
                        methods.push(m);
                    }
                }
                _ => {}
            }
        }
    }

    Some(TypeDef {
        name: name_str,
        kind: TypeKind::Record,
        visibility: mods.visibility,
        fields,
        methods,
        relations: Vec::new(), // Records can implement interfaces, could be added later
        annotations: Vec::new(),
        type_params,
        enum_variants: Vec::new(),
    })
}

fn parse_fields(node: Node, source: &str) -> Vec<Field> {
    let mods = extract_modifiers(node, source);
    let mut fields = Vec::new();
    if let Some(type_node) = node.child_by_field_name("type") {
        let type_name = node_text(type_node, source).to_string();
        
        let mut cursor = node.walk();
        for child in node.children(&mut cursor) {
            if child.kind() == "variable_declarator" {
                if let Some(name_node) = child.child_by_field_name("name") {
                    fields.push(Field {
                        name: node_text(name_node, source).to_string(),
                        type_name: type_name.clone(),
                        visibility: mods.visibility.clone(),
                    });
                }
            }
        }
    }
    fields
}

fn parse_method(node: Node, source: &str) -> Option<Method> {
    let name = node.child_by_field_name("name")?;
    let name_str = node_text(name, source).to_string();
    let mods = extract_modifiers(node, source);

    let mut return_type = None;
    if let Some(ret) = node.child_by_field_name("type") {
        return_type = Some(node_text(ret, source).split_whitespace().collect::<Vec<_>>().join(" "));
    }

    let params = parse_parameters(node, source);

    let calls = if let Some(body) = node.child_by_field_name("body") {
        extract_calls(body, source)
    } else {
        Vec::new()
    };

    Some(Method {
        name: name_str,
        params,
        return_type,
        visibility: mods.visibility,
        calls,
        callers: Vec::new(),
        annotations: Vec::new(),
        is_static: mods.is_static,
    })
}

fn parse_constructor(node: Node, source: &str) -> Option<Method> {
    let name = node.child_by_field_name("name")?;
    let name_str = node_text(name, source).to_string();
    let mods = extract_modifiers(node, source);
    
    let params = parse_parameters(node, source);

    let calls = if let Some(body) = node.child_by_field_name("body") {
        extract_calls(body, source)
    } else {
        Vec::new()
    };

    Some(Method {
        name: name_str,
        params,
        return_type: None, // Constructors don't have return types
        visibility: mods.visibility,
        calls,
        callers: Vec::new(),
        annotations: Vec::new(),
        is_static: false, // Constructors evaluate as non-static
    })
}

fn parse_parameters(node: Node, source: &str) -> Vec<Param> {
    let mut params = Vec::new();
    if let Some(params_node) = node.child_by_field_name("parameters") {
        let mut cursor = params_node.walk();
        for child in params_node.children(&mut cursor) {
            if child.kind() == "formal_parameter" || child.kind() == "spread_parameter" {
                if let (Some(type_node), Some(name_node)) = (
                    child.child_by_field_name("type"),
                    child.child_by_field_name("name"),
                ) {
                    params.push(Param {
                        name: node_text(name_node, source).to_string(),
                        type_name: node_text(type_node, source).to_string(),
                    });
                }
            }
        }
    }
    params
}

fn extract_calls(node: Node, source: &str) -> Vec<CallRef> {
    let mut calls = Vec::new();
    collect_calls(node, source, &mut calls);

    calls.sort_by(|a, b| {
        let a_str = format!("{}", a);
        let b_str = format!("{}", b);
        a_str.cmp(&b_str)
    });
    calls.dedup_by(|a, b| format!("{}", a) == format!("{}", b));

    calls
}

fn resolve_receiver(node: Node, source: &str) -> String {
    let raw = match node.kind() {
        "this" | "super" | "identifier" => node_text(node, source).to_string(),
        "field_access" => {
            if let (Some(obj), Some(field)) = (
                node.child_by_field_name("object"),
                node.child_by_field_name("field"),
            ) {
                let obj_text = node_text(obj, source);
                if obj_text == "this" {
                    return format!("this.{}", node_text(field, source).trim());
                }
                return resolve_receiver(obj, source);
            }
            node_text(node, source).to_string()
        }
        "method_invocation" => {
            // Unwind method chaining
            if let Some(obj) = node.child_by_field_name("object") {
                return resolve_receiver(obj, source);
            }
            "?".to_string()
        }
        _ => node_text(node, source).to_string(),
    };
    raw.split_whitespace().collect::<Vec<_>>().join(" ")
}

fn collect_calls(node: Node, source: &str, calls: &mut Vec<CallRef>) {
    match node.kind() {
        "method_invocation" => {
            let method_name = if let Some(name_node) = node.child_by_field_name("name") {
                node_text(name_node, source).split_whitespace().collect::<Vec<_>>().join(" ")
            } else {
                "".to_string()
            };

            if let Some(obj) = node.child_by_field_name("object") {
                // e.g. foo.bar()
                let receiver = resolve_receiver(obj, source);
                calls.push(CallRef {
                    target_type: Some(receiver),
                    target_method: method_name,
                });
            } else {
                // local method call e.g. doWork()
                calls.push(CallRef {
                    target_type: None,
                    target_method: method_name,
                });
            }
        }
        "object_creation_expression" => {
            // new Foo()
            if let Some(type_node) = node.child_by_field_name("type") {
                calls.push(CallRef {
                    target_type: Some(node_text(type_node, source).split_whitespace().collect::<Vec<_>>().join(" ")),
                    target_method: "<init>".to_string(),
                });
            }
        }
        _ => {}
    }

    let mut cursor = node.walk();
    for child in node.children(&mut cursor) {
        collect_calls(child, source, calls);
    }
}