codingest 0.2.11

codingest — standalone extraction of KGLite's code-tree component: parse polyglot codebases into kglite knowledge graphs (tree-sitter parsers, call/type/route edges, docs pass, multi-rev merge)
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
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
//! Julia language parser.
//!
//! Backed by the official `tree-sitter/tree-sitter-julia` grammar (0.23).
//! Julia has no per-file namespace: module coordinates come from
//! `module X … end` blocks, and files are stitched together *textually* via
//! `include("relative/path.jl")`. That split drives the import model:
//!
//!   - `include("…")` is a **file path** — recorded verbatim in
//!     `FileInfo.imports` and resolved by the builder's path-import route
//!     (`registry::uses_path_imports("julia")`), which checks candidates
//!     against the real file set and so cannot invent a target.
//!   - `using Foo` / `import Foo.Bar` are **module references** — recorded as
//!     their dotted text so the dependency is visible on the File node, but
//!     they are namespace-shaped: julia is deliberately NOT in the
//!     file-anchored allowlist of `other_edges.rs`'s raw prefix walk, so a
//!     `using` of an external package whose name collides with a project file
//!     can never manufacture a File→File edge.
//!
//! Coverage:
//!   - `function f() … end`, short-form `f(x) = …` (an `assignment` whose LHS
//!     is a call), `function foo end` stubs, typed (`f(x)::T = …`) and
//!     `where`-clause signatures → FunctionInfo. Multiple dispatch (same name,
//!     different signatures) is preserved by the builder's overload pass,
//!     which decorates colliding qualified names with a signature hash.
//!   - Qualified extension methods (`function Base.show(io, x) … end`) keep
//!     the leaf name (`show`), so same-file calls resolve to them.
//!   - `struct` / `mutable struct` → ClassInfo kind="struct" (`Struct` nodes)
//!     with fields (typed, untyped, defaulted) as AttributeInfo;
//!     `abstract type` → ClassInfo kind="abstract_type"; `<:` supertypes →
//!     `extends` TypeRelationships (EXTENDS edges).
//!   - `module X … end` → members qualified as `<file-module>.X.<name>`;
//!     top-level modules are recorded as submodule declarations
//!     (Module HAS_SUBMODULE Module).
//!   - `const NAME = …` → ConstantInfo; `export a, b` → FileInfo.exports.
//!   - Call sites (identifier and `receiver.method` field calls) →
//!     FunctionInfo.calls; cyclomatic branch metrics; docstrings from the
//!     immediately preceding string literal; TODO/FIXME comment annotations.
//!   - One level of macro unwrapping: a definition inside a top-level macro
//!     call (`@inline f(x) = …`, `Base.@kwdef struct …`) is extracted as if
//!     unwrapped.
//!
//! Deliberately out of scope (recorded here so absence reads as a decision,
//! not an accident): `macro` definitions get no nodes; anonymous functions
//! (`x -> …`) and `do`-blocks get no nodes (calls inside them belong to the
//! enclosing function, mirroring the Rust-closure/Go-func-literal rule);
//! nested/local named functions get no nodes and their bodies' calls are
//! excluded from the parent; non-`const` global assignments, `primitive type`,
//! selective-import symbols (`using Foo: bar` records only `Foo`), import
//! aliases (`import JSON as J` records `JSON`), and inner constructors are
//! not extracted; `&&`/`||` do not count toward branch metrics.

use std::path::Path;
use tree_sitter::{Node, Parser, Tree};

use super::shared::{
    compute_complexity, count_lines, extract_comment_annotations, extract_procedure_annotations,
    file_to_module_path, is_generated_or_minified, is_test_path, make_qualified, node_text,
    BRANCH_KINDS_JULIA, DEFAULT_COMMENT_TYPES,
};
use super::LanguageParser;
use crate::models::{
    AttributeInfo, ClassInfo, ConstantInfo, FileInfo, FunctionInfo, MetadataMap, ParameterInfo,
    ParameterKind, ParseResult, TypeRelationship,
};

/// Base builtins that would otherwise dominate CALLS noise. Deliberately
/// omits `show`, `print`, `string` and `parse`: extending `Base.show` /
/// `Base.print` is the standard user pattern, and suppressing the bare name
/// would orphan calls to those user-defined methods.
pub const JULIA_NOISE_NAMES: &[&str] = &[
    "include",
    "println",
    "error",
    "throw",
    "length",
    "size",
    "push!",
    "pop!",
    "sqrt",
    "abs",
    "min",
    "max",
    "clamp",
    "typeof",
    "convert",
    "collect",
    "isempty",
    "isnothing",
    "zeros",
    "ones",
    "rand",
    "get",
    "haskey",
    "keys",
    "values",
    "getindex",
    "setindex!",
];

/// Scopes whose interior belongs to a *different* (unextracted) callable —
/// both the call walk and the complexity walk stop at these. Arrow functions
/// and `do`-blocks are intentionally absent from the call list: like Rust
/// closures and Go `func` literals they get no node of their own, so calls
/// inside them belong to the enclosing function.
const CALL_NESTED_SCOPES: &[&str] = &["function_definition", "macro_definition"];

/// Complexity additionally skips anonymous callables — their branches would
/// inflate the enclosing function's metrics.
const COMPLEXITY_NESTED_SCOPES: &[&str] = &[
    "function_definition",
    "macro_definition",
    "arrow_function_expression",
    "do_clause",
];

pub struct JuliaParser;

thread_local! {
    static JULIA_PARSER: std::cell::RefCell<Parser> = {
        let mut p = Parser::new();
        p.set_language(&tree_sitter_julia::LANGUAGE.into())
            .expect("loading tree-sitter-julia grammar");
        std::cell::RefCell::new(p)
    };
}

/// What a signature expression unwraps to.
struct SignatureParts<'a> {
    name: String,
    call: Option<Node<'a>>,
    return_type: Option<String>,
    type_parameters: Option<String>,
}

impl JuliaParser {
    #[allow(clippy::new_without_default)]
    pub fn new() -> Self {
        JuliaParser
    }

    fn parse_tree(&self, source: &[u8]) -> Option<Tree> {
        JULIA_PARSER.with(|p| p.borrow_mut().parse(source, None))
    }

    /// Julia has no access modifiers; the community convention is a leading
    /// underscore for internal names.
    fn get_visibility(name: &str) -> &'static str {
        if name.starts_with('_') {
            "private"
        } else {
            "public"
        }
    }

    /// The immediately preceding bare string literal is Julia's docstring
    /// mechanism (`"doc" ↵ function f() … end`). Adjacency is required so an
    /// unrelated string constant further up is not claimed.
    fn get_docstring(node: Node, source: &[u8]) -> Option<String> {
        let sib = node.prev_named_sibling()?;
        if sib.kind() != "string_literal" {
            return None;
        }
        if sib.end_position().row + 1 != node.start_position().row {
            return None;
        }
        let text = node_text(sib, source).trim_matches('"').trim();
        (!text.is_empty()).then(|| text.to_string())
    }

    /// Peel `where` clauses and a `::T` return annotation off a signature
    /// expression down to the underlying call (or bare identifier for a
    /// `function foo end` stub).
    fn unwrap_signature<'a>(sig_expr: Node<'a>, source: &'a [u8]) -> Option<SignatureParts<'a>> {
        let mut expr = sig_expr;
        let mut return_type = None;
        let mut type_parameters = None;
        loop {
            match expr.kind() {
                "where_expression" => {
                    if let Some(clause) = expr.named_child(1) {
                        type_parameters = Some(node_text(clause, source).to_string());
                    }
                    expr = expr.named_child(0)?;
                }
                "typed_expression" => {
                    if let Some(ty) = expr.named_child(1) {
                        return_type = Some(node_text(ty, source).to_string());
                    }
                    expr = expr.named_child(0)?;
                }
                _ => break,
            }
        }
        let (name, call) = match expr.kind() {
            "call_expression" => {
                let callee = expr.named_child(0)?;
                let name = match callee.kind() {
                    "identifier" => node_text(callee, source).to_string(),
                    // `function Base.show(io, x)` — keep the leaf name so
                    // same-file calls to `show` resolve to the extension.
                    "field_expression" => {
                        let leaf = Self::last_named_child(callee)?;
                        node_text(leaf, source).to_string()
                    }
                    _ => return None,
                };
                (name, Some(expr))
            }
            // `function foo end` dispatch stub.
            "identifier" => (node_text(expr, source).to_string(), None),
            _ => return None,
        };
        Some(SignatureParts {
            name,
            call,
            return_type,
            type_parameters,
        })
    }

    /// True when an `assignment` is Julia's short-form method definition:
    /// its LHS is a call (optionally `where`/`::T`-wrapped).
    fn is_short_form_function(node: Node) -> bool {
        debug_assert!(node.kind() == "assignment");
        let Some(mut lhs) = node.named_child(0) else {
            return false;
        };
        loop {
            match lhs.kind() {
                "call_expression" => return true,
                "where_expression" | "typed_expression" => {
                    let Some(inner) = lhs.named_child(0) else {
                        return false;
                    };
                    lhs = inner;
                }
                _ => return false,
            }
        }
    }

    fn extract_parameters(call: Node, source: &[u8]) -> Vec<ParameterInfo> {
        let mut out = Vec::new();
        let Some(args) = Self::find_child(call, "argument_list") else {
            return out;
        };
        let mut cursor = args.walk();
        for child in args.named_children(&mut cursor) {
            match child.kind() {
                "identifier" => out.push(ParameterInfo {
                    name: node_text(child, source).to_string(),
                    type_annotation: None,
                    default: None,
                    kind: ParameterKind::Positional,
                }),
                "typed_expression" => {
                    let name = child
                        .named_child(0)
                        .map(|n| node_text(n, source).to_string())
                        .unwrap_or_default();
                    let ty = child
                        .named_child(1)
                        .map(|n| node_text(n, source).to_string());
                    out.push(ParameterInfo {
                        name,
                        type_annotation: ty,
                        default: None,
                        kind: ParameterKind::Positional,
                    });
                }
                // Optional/keyword argument with a default (`b=1`, `c::Int=2`).
                "named_argument" => {
                    let mut name = String::new();
                    let mut ty = None;
                    if let Some(lhs) = child.named_child(0) {
                        match lhs.kind() {
                            "typed_expression" => {
                                name = lhs
                                    .named_child(0)
                                    .map(|n| node_text(n, source).to_string())
                                    .unwrap_or_default();
                                ty = lhs.named_child(1).map(|n| node_text(n, source).to_string());
                            }
                            _ => name = node_text(lhs, source).to_string(),
                        }
                    }
                    let default = Self::last_named_child(child)
                        .filter(|n| {
                            n.kind() != "operator"
                                && n.id() != child.named_child(0).map_or(0, |c| c.id())
                        })
                        .map(|n| node_text(n, source).to_string());
                    out.push(ParameterInfo {
                        name,
                        type_annotation: ty,
                        default,
                        kind: ParameterKind::Positional,
                    });
                }
                // `xs...` varargs (positional or keyword — not distinguished).
                "splat_expression" => {
                    let (name, ty) = match child.named_child(0) {
                        Some(inner) if inner.kind() == "typed_expression" => (
                            inner
                                .named_child(0)
                                .map(|n| node_text(n, source).to_string())
                                .unwrap_or_default(),
                            inner
                                .named_child(1)
                                .map(|n| node_text(n, source).to_string()),
                        ),
                        Some(inner) => (node_text(inner, source).to_string(), None),
                        None => continue,
                    };
                    out.push(ParameterInfo {
                        name,
                        type_annotation: ty,
                        default: None,
                        kind: ParameterKind::Variadic,
                    });
                }
                _ => {}
            }
        }
        out
    }

    fn extract_calls(body: Node, source: &[u8], out: &mut Vec<(String, u32)>) {
        if body.kind() == "call_expression" {
            let line = body.start_position().row as u32 + 1;
            if let Some(callee) = body.child(0) {
                match callee.kind() {
                    "identifier" => out.push((node_text(callee, source).to_string(), line)),
                    "field_expression" => {
                        // `obj.method(x)` / `Mod.func(x)` — keep the go-style
                        // `hint.method` form so the resolver can use the last
                        // qualifier segment as a hint.
                        let count = callee.named_child_count() as u32;
                        if count >= 2 {
                            if let (Some(operand), Some(field)) =
                                (callee.named_child(count - 2), callee.named_child(count - 1))
                            {
                                let op_text = node_text(operand, source);
                                let hint = op_text.rsplit('.').next().unwrap_or(op_text);
                                out.push((format!("{}.{}", hint, node_text(field, source)), line));
                            }
                        }
                    }
                    _ => {}
                }
            }
        }
        let mut cursor = body.walk();
        for child in body.named_children(&mut cursor) {
            if CALL_NESTED_SCOPES.contains(&child.kind()) {
                continue;
            }
            // A nested short-form definition (`g(x) = …` inside a function)
            // defines a local method; its body's calls are not the parent's.
            if child.kind() == "assignment" && Self::is_short_form_function(child) {
                continue;
            }
            Self::extract_calls(child, source, out);
        }
    }

    /// Sum branch metrics over the statement nodes of a body (Julia
    /// definitions have no body wrapper node).
    fn body_metrics<'a>(nodes: impl Iterator<Item = Node<'a>>) -> (u32, u32) {
        let mut branches = 0;
        let mut nesting = 0;
        for node in nodes {
            if COMPLEXITY_NESTED_SCOPES.contains(&node.kind()) {
                continue;
            }
            let (b, n) = compute_complexity(node, BRANCH_KINDS_JULIA, COMPLEXITY_NESTED_SCOPES);
            branches += b;
            nesting = nesting.max(n);
        }
        (branches, nesting)
    }

    fn find_child<'a>(node: Node<'a>, kind: &str) -> Option<Node<'a>> {
        let mut cursor = node.walk();
        let found = node.named_children(&mut cursor).find(|c| c.kind() == kind);
        found
    }

    fn last_named_child(node: Node) -> Option<Node> {
        let count = u32::try_from(node.named_child_count()).ok()?;
        count.checked_sub(1).and_then(|i| node.named_child(i))
    }

    /// Named children strictly after `marker` in source order.
    fn children_after<'a>(node: Node<'a>, marker: Node) -> Vec<Node<'a>> {
        let mut cursor = node.walk();
        node.named_children(&mut cursor)
            .filter(|c| c.start_byte() >= marker.end_byte())
            .collect()
    }

    /// Parse a long-form `function_definition` or a short-form definition
    /// `assignment`. Returns `None` for shapes that are not functions
    /// (e.g. a signature that unwraps to nothing callable).
    fn parse_function(
        node: Node,
        source: &[u8],
        module_path: &str,
        rel_path: &str,
    ) -> Option<FunctionInfo> {
        let long_form = node.kind() == "function_definition";
        let (parts, signature_text, body_nodes) = if long_form {
            let sig_node = Self::find_child(node, "signature")?;
            let parts = Self::unwrap_signature(sig_node.named_child(0)?, source)?;
            let signature_text = format!("function {}", node_text(sig_node, source));
            (parts, signature_text, Self::children_after(node, sig_node))
        } else {
            let lhs = node.named_child(0)?;
            let parts = Self::unwrap_signature(lhs, source)?;
            // A bare identifier LHS is a plain assignment, not a function.
            parts.call?;
            let operator = Self::find_child(node, "operator")?;
            let signature_text = node_text(lhs, source).to_string();
            (parts, signature_text, Self::children_after(node, operator))
        };

        let name = parts.name;
        let qualified_name = make_qualified(module_path, "", &name, '.');
        let parameters = parts
            .call
            .map(|call| Self::extract_parameters(call, source))
            .unwrap_or_default();
        let param_count = Some(parameters.len() as u32);

        let mut calls: Vec<(String, u32)> = Vec::new();
        for body in &body_nodes {
            if CALL_NESTED_SCOPES.contains(&body.kind()) {
                continue;
            }
            if body.kind() == "assignment" && Self::is_short_form_function(*body) {
                continue;
            }
            Self::extract_calls(*body, source, &mut calls);
        }
        let (branch_count, max_nesting) = Self::body_metrics(body_nodes.into_iter());
        let is_recursive = Some(calls.iter().any(|(n, _)| n == &name));
        let docstring = Self::get_docstring(node, source);
        let procedure_names = extract_procedure_annotations(docstring.as_deref());

        Some(FunctionInfo {
            visibility: Self::get_visibility(&name).to_string(),
            is_async: false,
            is_method: false,
            signature: signature_text,
            file_path: rel_path.to_string(),
            line_number: node.start_position().row as u32 + 1,
            end_line: Some(node.end_position().row as u32 + 1),
            docstring,
            return_type: parts.return_type,
            calls,
            references: Vec::new(),
            function_refs: Vec::new(),
            type_parameters: parts.type_parameters,
            decorators: Vec::new(),
            parameters,
            branch_count: Some(branch_count),
            param_count,
            max_nesting: Some(max_nesting),
            is_recursive,
            procedure_names,
            metadata: MetadataMap::new(),
            qualified_name,
            name,
        })
    }

    /// `type_head` payload → (name, type_parameters, supertype).
    /// Handles `Circle`, `Pair{T}`, `Square <: Shape`, `Pair{T} <: Shape`.
    fn type_head_parts(
        head: Node,
        source: &[u8],
    ) -> Option<(String, Option<String>, Option<String>)> {
        let mut expr = head.named_child(0)?;
        let mut supertype = None;
        if expr.kind() == "binary_expression" {
            let rhs = Self::last_named_child(expr)?;
            let raw = node_text(rhs, source);
            let bare = raw[..raw.find('{').unwrap_or(raw.len())].trim();
            supertype = (!bare.is_empty()).then(|| bare.to_string());
            expr = expr.named_child(0)?;
        }
        let (name, type_parameters) = match expr.kind() {
            "parametrized_type_expression" => {
                let name = node_text(expr.named_child(0)?, source).to_string();
                let params = expr
                    .named_child(1)
                    .map(|c| node_text(c, source).to_string());
                (name, params)
            }
            "identifier" => (node_text(expr, source).to_string(), None),
            _ => return None,
        };
        Some((name, type_parameters, supertype))
    }

    fn parse_struct(
        node: Node,
        source: &[u8],
        module_path: &str,
        rel_path: &str,
        result: &mut ParseResult,
    ) {
        let Some(head) = Self::find_child(node, "type_head") else {
            return;
        };
        let Some((name, type_parameters, supertype)) = Self::type_head_parts(head, source) else {
            return;
        };
        let qname = make_qualified(module_path, "", &name, '.');
        let is_mutable = {
            let mut cursor = node.walk();
            let found = node
                .children(&mut cursor)
                .any(|c| !c.is_named() && c.kind() == "mutable");
            found
        };
        let mut metadata = MetadataMap::new();
        if is_mutable {
            metadata.insert("is_mutable".into(), serde_json::Value::Bool(true));
        }

        // Fields: statement nodes after the head. `x::T`, bare `x`, and
        // `x::T = default` (@kwdef style). An assignment whose LHS is a call
        // is an inner constructor, not a field — skipped, as are nested
        // long-form constructors.
        for member in Self::children_after(node, head) {
            let (field_node, default_value) = match member.kind() {
                "typed_expression" | "identifier" => (member, None),
                "assignment" if !Self::is_short_form_function(member) => {
                    let Some(lhs) = member.named_child(0) else {
                        continue;
                    };
                    let default = Self::last_named_child(member)
                        .filter(|n| n.kind() != "operator" && n.id() != lhs.id())
                        .map(|n| truncate_chars(node_text(n, source), 100));
                    (lhs, default)
                }
                _ => continue,
            };
            let (field_name, type_ann) = match field_node.kind() {
                "typed_expression" => (
                    field_node
                        .named_child(0)
                        .map(|n| node_text(n, source).to_string())
                        .unwrap_or_default(),
                    field_node
                        .named_child(1)
                        .map(|n| node_text(n, source).to_string()),
                ),
                "identifier" => (node_text(field_node, source).to_string(), None),
                _ => continue,
            };
            if field_name.is_empty() {
                continue;
            }
            result.attributes.push(AttributeInfo {
                qualified_name: format!("{}.{}", qname, field_name),
                owner_qualified_name: qname.clone(),
                type_annotation: type_ann,
                visibility: Self::get_visibility(&field_name).to_string(),
                file_path: rel_path.to_string(),
                line_number: member.start_position().row as u32 + 1,
                default_value,
                name: field_name,
            });
        }

        result.classes.push(ClassInfo {
            qualified_name: qname,
            kind: "struct".into(),
            visibility: Self::get_visibility(&name).to_string(),
            file_path: rel_path.to_string(),
            line_number: node.start_position().row as u32 + 1,
            end_line: Some(node.end_position().row as u32 + 1),
            docstring: Self::get_docstring(node, source),
            bases: supertype.iter().cloned().collect(),
            type_parameters,
            metadata,
            name: name.clone(),
        });
        if let Some(target) = supertype {
            result.type_relationships.push(TypeRelationship {
                source_type: name,
                target_type: Some(target),
                relationship: "extends".into(),
                methods: Vec::new(),
            });
        }
    }

    fn parse_abstract_type(
        node: Node,
        source: &[u8],
        module_path: &str,
        rel_path: &str,
        result: &mut ParseResult,
    ) {
        let Some(head) = Self::find_child(node, "type_head") else {
            return;
        };
        let Some((name, type_parameters, supertype)) = Self::type_head_parts(head, source) else {
            return;
        };
        result.classes.push(ClassInfo {
            qualified_name: make_qualified(module_path, "", &name, '.'),
            kind: "abstract_type".into(),
            visibility: Self::get_visibility(&name).to_string(),
            file_path: rel_path.to_string(),
            line_number: node.start_position().row as u32 + 1,
            end_line: Some(node.end_position().row as u32 + 1),
            docstring: Self::get_docstring(node, source),
            bases: supertype.iter().cloned().collect(),
            type_parameters,
            metadata: MetadataMap::new(),
            name: name.clone(),
        });
        if let Some(target) = supertype {
            result.type_relationships.push(TypeRelationship {
                source_type: name,
                target_type: Some(target),
                relationship: "extends".into(),
                methods: Vec::new(),
            });
        }
    }

    fn parse_const(
        node: Node,
        source: &[u8],
        module_path: &str,
        rel_path: &str,
        result: &mut ParseResult,
    ) {
        let Some(assignment) = Self::find_child(node, "assignment") else {
            return;
        };
        let Some(lhs) = assignment.named_child(0) else {
            return;
        };
        let (name, type_ann) = match lhs.kind() {
            "identifier" => (node_text(lhs, source).to_string(), None),
            "typed_expression" => (
                lhs.named_child(0)
                    .map(|n| node_text(n, source).to_string())
                    .unwrap_or_default(),
                lhs.named_child(1).map(|n| node_text(n, source).to_string()),
            ),
            _ => return,
        };
        if name.is_empty() {
            return;
        }
        let value_preview = Self::last_named_child(assignment)
            .filter(|n| n.kind() != "operator" && n.id() != lhs.id())
            .map(|n| truncate_chars(node_text(n, source), 100));
        result.constants.push(ConstantInfo {
            qualified_name: make_qualified(module_path, "", &name, '.'),
            kind: "constant".into(),
            type_annotation: type_ann,
            value_preview,
            visibility: Self::get_visibility(&name).to_string(),
            file_path: rel_path.to_string(),
            line_number: node.start_position().row as u32 + 1,
            name,
        });
    }

    /// Module references from a `using_statement` / `import_statement`,
    /// recorded as dotted text. Relative forms (`using ..Sibling`) keep their
    /// leading dots; selective imports and aliases record the module only.
    fn collect_import_refs(node: Node, source: &[u8], out: &mut Vec<String>) {
        let mut cursor = node.walk();
        for child in node.named_children(&mut cursor) {
            match child.kind() {
                "identifier" | "scoped_identifier" | "import_path" => {
                    out.push(node_text(child, source).to_string());
                }
                "selected_import" | "import_alias" => {
                    if let Some(module) = child.named_child(0) {
                        out.push(node_text(module, source).to_string());
                    }
                }
                _ => {}
            }
        }
    }

    /// `include("relative/path.jl")` at statement level → the file path,
    /// verbatim, for the builder's path-import route.
    fn include_target(node: Node, source: &[u8]) -> Option<String> {
        debug_assert!(node.kind() == "call_expression");
        let callee = node.named_child(0)?;
        if callee.kind() != "identifier" || node_text(callee, source) != "include" {
            return None;
        }
        let args = Self::find_child(node, "argument_list")?;
        let literal = args.named_child(0)?;
        if literal.kind() != "string_literal" {
            return None;
        }
        let path = node_text(literal, source).trim_matches('"').to_string();
        (!path.is_empty()).then_some(path)
    }

    /// Statement-level dispatch, shared by the file top level, `module`
    /// bodies, and unwrapped top-level macro calls. `at_file_top_level` gates
    /// submodule declarations to depth 0 so the HAS_SUBMODULE parent
    /// (`FileInfo.module_path`) stays truthful.
    #[allow(clippy::too_many_arguments)]
    fn parse_items(
        container: Node,
        source: &[u8],
        module_path: &str,
        rel_path: &str,
        file_info: &mut FileInfo,
        result: &mut ParseResult,
        at_file_top_level: bool,
    ) {
        let mut cursor = container.walk();
        for child in container.named_children(&mut cursor) {
            match child.kind() {
                "module_definition" => {
                    let Some(name_node) = Self::find_child(child, "identifier") else {
                        continue;
                    };
                    let name = node_text(name_node, source).to_string();
                    if at_file_top_level {
                        file_info.submodule_declarations.push(name.clone());
                    }
                    let inner_path = make_qualified(module_path, "", &name, '.');
                    Self::parse_items(
                        child,
                        source,
                        &inner_path,
                        rel_path,
                        file_info,
                        result,
                        false,
                    );
                }
                "function_definition" => {
                    if let Some(f) = Self::parse_function(child, source, module_path, rel_path) {
                        result.functions.push(f);
                    }
                }
                "assignment" => {
                    if Self::is_short_form_function(child) {
                        if let Some(f) = Self::parse_function(child, source, module_path, rel_path)
                        {
                            result.functions.push(f);
                        }
                    }
                    // Non-const global assignments are deliberately skipped.
                }
                "struct_definition" => {
                    Self::parse_struct(child, source, module_path, rel_path, result);
                }
                "abstract_definition" => {
                    Self::parse_abstract_type(child, source, module_path, rel_path, result);
                }
                "const_statement" => {
                    Self::parse_const(child, source, module_path, rel_path, result);
                }
                "using_statement" | "import_statement" => {
                    Self::collect_import_refs(child, source, &mut file_info.imports);
                }
                "export_statement" => {
                    let mut ec = child.walk();
                    for item in child.named_children(&mut ec) {
                        if item.kind() == "identifier" {
                            file_info.exports.push(node_text(item, source).to_string());
                        }
                    }
                }
                "call_expression" => {
                    if let Some(target) = Self::include_target(child, source) {
                        file_info.imports.push(target);
                    }
                }
                // One level of macro unwrapping: `@inline f(x) = …`,
                // `Base.@kwdef struct …`, `@doc`-less annotated definitions.
                "macrocall_expression" => {
                    if let Some(args) = Self::find_child(child, "macro_argument_list") {
                        Self::parse_items(
                            args,
                            source,
                            module_path,
                            rel_path,
                            file_info,
                            result,
                            at_file_top_level,
                        );
                    }
                }
                _ => {}
            }
        }
    }
}

/// First `limit` chars of an expression, for value previews.
fn truncate_chars(text: &str, limit: usize) -> String {
    let take = text
        .char_indices()
        .nth(limit)
        .map(|(i, _)| i)
        .unwrap_or(text.len());
    text[..take].to_string()
}

impl LanguageParser for JuliaParser {
    fn language_name(&self) -> &'static str {
        "julia"
    }
    fn file_extensions(&self) -> &'static [&'static str] {
        &["jl"]
    }
    fn parse_file(&self, filepath: &Path, src_root: &Path) -> ParseResult {
        let Ok(source) = std::fs::read(filepath) else {
            return ParseResult::new();
        };

        let rel_path = filepath
            .strip_prefix(src_root)
            .unwrap_or(filepath)
            .to_string_lossy()
            .replace('\\', "/");
        let loc = count_lines(&source);
        let filename = filepath
            .file_name()
            .and_then(|o| o.to_str())
            .unwrap_or("")
            .to_string();
        let module_path = file_to_module_path(filepath, src_root, '.');
        let is_test =
            is_test_path(&rel_path, &filename, &["_test.jl"]) || filename == "runtests.jl";

        let mut file_info = FileInfo {
            path: rel_path.clone(),
            filename,
            loc,
            module_path: module_path.clone(),
            language: "julia".to_string(),
            submodule_declarations: Vec::new(),
            imports: Vec::new(),
            exports: Vec::new(),
            annotations: None,
            is_test,
            skip_reason: None,
        };

        let mut result = ParseResult::new();
        if let Some(reason) = is_generated_or_minified(&source) {
            file_info.skip_reason = Some(reason.to_string());
            result.files.push(file_info);
            return result;
        }

        let Some(tree) = self.parse_tree(&source) else {
            result.files.push(file_info);
            return result;
        };
        let root = tree.root_node();
        Self::parse_items(
            root,
            &source,
            &module_path,
            &rel_path,
            &mut file_info,
            &mut result,
            true,
        );

        file_info.annotations = extract_comment_annotations(root, &source, DEFAULT_COMMENT_TYPES);
        result.files.push(file_info);
        result
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::fs;

    /// Parse one source string as `<root>/src/app.jl` under a root named
    /// `pkg`, so module paths land at `pkg.src.app.*`.
    fn parse(source: &str) -> ParseResult {
        let temp = tempfile::tempdir().unwrap();
        let root = temp.path().join("pkg");
        fs::create_dir_all(root.join("src")).unwrap();
        let file = root.join("src/app.jl");
        fs::write(&file, source).unwrap();
        JuliaParser::new().parse_files(&[file], &root)
    }

    #[test]
    fn long_and_short_form_functions_are_extracted() {
        let parsed = parse(concat!(
            "function area(c)\n    return c\nend\n",
            "scaled(c, k) = k * area(c)\n",
            "x = 1\n",          // plain assignment: not a function
            "f = y -> y + 1\n", // arrow function binding: not extracted
        ));
        let names: Vec<&str> = parsed.functions.iter().map(|f| f.name.as_str()).collect();
        assert_eq!(names, ["area", "scaled"]);
        assert_eq!(parsed.functions[0].qualified_name, "pkg.src.app.area");
        assert_eq!(parsed.functions[0].signature, "function area(c)");
        assert_eq!(parsed.functions[1].qualified_name, "pkg.src.app.scaled");
        assert_eq!(parsed.functions[1].signature, "scaled(c, k)");
        // The short form's RHS is its body: the call to `area` is attributed.
        assert_eq!(parsed.functions[1].calls.len(), 1);
        assert_eq!(parsed.functions[1].calls[0].0, "area");
    }

    /// Multiple dispatch: the parser emits BOTH methods under one qualified
    /// name with distinct signatures — the builder's overload pass relies on
    /// exactly that to decorate the colliding IDs.
    #[test]
    fn multi_dispatch_pair_keeps_one_qname_and_two_signatures() {
        let parsed = parse(concat!(
            "function area(c::Circle)\n    return 1\nend\n",
            "function area(c::Circle, scale::Float64)\n    return scale\nend\n",
        ));
        assert_eq!(parsed.functions.len(), 2);
        assert_eq!(
            parsed.functions[0].qualified_name,
            parsed.functions[1].qualified_name
        );
        assert_ne!(parsed.functions[0].signature, parsed.functions[1].signature);
        assert_eq!(parsed.functions[0].param_count, Some(1));
        assert_eq!(parsed.functions[1].param_count, Some(2));
        assert_eq!(
            parsed.functions[1].parameters[1].type_annotation.as_deref(),
            Some("Float64")
        );
    }

    #[test]
    fn module_members_are_qualified_and_only_top_level_modules_declared() {
        let parsed = parse(concat!(
            "module Outer\n",
            "helper(x) = x\n",
            "module Inner\n",
            "deep(y) = y\n",
            "end\n",
            "end\n",
        ));
        let qnames: Vec<&str> = parsed
            .functions
            .iter()
            .map(|f| f.qualified_name.as_str())
            .collect();
        assert_eq!(
            qnames,
            ["pkg.src.app.Outer.helper", "pkg.src.app.Outer.Inner.deep"]
        );
        // Only the FILE-top-level module is declared: HAS_SUBMODULE's parent
        // is the file's module path, so a deeper chain would mis-parent.
        assert_eq!(parsed.files[0].submodule_declarations, ["Outer"]);
    }

    #[test]
    fn structs_carry_fields_mutability_supertype_and_type_params() {
        let parsed = parse(concat!(
            "abstract type Shape end\n",
            "struct Pair{T} <: Shape\n    a::T\n    b\nend\n",
            "mutable struct Counter\n    count::Int\nend\n",
        ));
        assert_eq!(parsed.classes.len(), 3);
        let shape = &parsed.classes[0];
        assert_eq!(shape.kind, "abstract_type");
        assert_eq!(shape.qualified_name, "pkg.src.app.Shape");
        let pair = &parsed.classes[1];
        assert_eq!(pair.kind, "struct");
        assert_eq!(pair.type_parameters.as_deref(), Some("{T}"));
        assert_eq!(pair.bases, ["Shape"]);
        assert!(!pair.metadata.contains_key("is_mutable"));
        let counter = &parsed.classes[2];
        assert_eq!(counter.metadata["is_mutable"], serde_json::json!(true));

        let fields: Vec<(&str, Option<&str>)> = parsed
            .attributes
            .iter()
            .map(|a| (a.name.as_str(), a.type_annotation.as_deref()))
            .collect();
        assert_eq!(
            fields,
            [("a", Some("T")), ("b", None), ("count", Some("Int"))]
        );
        let rels: Vec<(&str, Option<&str>)> = parsed
            .type_relationships
            .iter()
            .map(|r| (r.source_type.as_str(), r.target_type.as_deref()))
            .collect();
        assert_eq!(rels, [("Pair", Some("Shape"))]);
    }

    /// The import split that the whole Julia model hangs on: `include` paths
    /// are recorded verbatim as FILE PATHS; `using`/`import` are recorded as
    /// dotted MODULE references (relative forms keep their leading dots), and
    /// selective/aliased imports record the module only.
    #[test]
    fn includes_and_module_references_are_recorded_distinctly() {
        let parsed = parse(concat!(
            "include(\"shapes/circle.jl\")\n",
            "using Downloads\n",
            "using .Geometry\n",
            "using JSON: parse\n",
            "import Base.show\n",
            "import TOML as T\n",
            "export area, Circle\n",
        ));
        assert_eq!(
            parsed.files[0].imports,
            [
                "shapes/circle.jl",
                "Downloads",
                ".Geometry",
                "JSON",
                "Base.show",
                "TOML",
            ]
        );
        assert_eq!(parsed.files[0].exports, ["area", "Circle"]);
    }

    #[test]
    fn dispatch_stub_and_qualified_extension_take_the_leaf_name() {
        let parsed = parse(concat!(
            "function foo end\n",
            "Base.show(io, c) = 1\n",
            "function Base.print(io, c)\n    return 2\nend\n",
        ));
        let names: Vec<&str> = parsed.functions.iter().map(|f| f.name.as_str()).collect();
        assert_eq!(names, ["foo", "show", "print"]);
        assert_eq!(parsed.functions[0].param_count, Some(0));
        assert_eq!(parsed.functions[1].qualified_name, "pkg.src.app.show");
    }

    /// Calls inside a nested named function (long or short form) belong to
    /// the nested definition, which gets no node — never to the parent.
    /// Calls inside an anonymous arrow function DO belong to the parent
    /// (the Rust-closure/Go-func-literal rule).
    #[test]
    fn nested_named_scopes_are_excluded_from_parent_calls() {
        let parsed = parse(concat!(
            "function outer(xs)\n",
            "    function inner(a)\n        hidden(a)\n    end\n",
            "    local_short(b) = also_hidden(b)\n",
            "    mapped = map(x -> visible(x), xs)\n",
            "    return inner(mapped)\n",
            "end\n",
        ));
        assert_eq!(parsed.functions.len(), 1);
        let calls: Vec<&str> = parsed.functions[0]
            .calls
            .iter()
            .map(|(n, _)| n.as_str())
            .collect();
        assert_eq!(calls, ["map", "visible", "inner"]);
    }

    #[test]
    fn const_recursion_docstring_and_macro_unwrap() {
        let parsed = parse(concat!(
            "const MAX = 100\n",
            "\"\"\"\nCount down to zero.\n\"\"\"\n",
            "function countdown(n)\n",
            "    n <= 0 && return 0\n",
            "    return countdown(n - 1)\n",
            "end\n",
            "@inline shortcut(x) = 2 * x\n",
        ));
        assert_eq!(parsed.constants.len(), 1);
        assert_eq!(parsed.constants[0].qualified_name, "pkg.src.app.MAX");
        assert_eq!(parsed.constants[0].value_preview.as_deref(), Some("100"));
        let countdown = &parsed.functions[0];
        assert_eq!(countdown.name, "countdown");
        assert_eq!(countdown.docstring.as_deref(), Some("Count down to zero."));
        assert_eq!(countdown.is_recursive, Some(true));
        // One level of macro unwrapping extracts the wrapped definition.
        assert_eq!(parsed.functions[1].name, "shortcut");
    }
}