arbor-core 3.1.1

AST parsing and code analysis for Arbor
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
//! Rust language parser implementation.
//!
//! Handles .rs files and extracts functions, structs, enums, traits,
//! and impl blocks.

use super::rust_receivers::{struct_field_refs, Receivers};
use crate::languages::LanguageParser;
use crate::node::{
    clean_type_name, returns_ref, typed_receiver_ref, CodeNode, NodeKind, TypeRelationKind,
    Visibility,
};
use tree_sitter::{Language, Node, Tree};

pub struct RustParser;

impl LanguageParser for RustParser {
    fn language(&self) -> Language {
        tree_sitter_rust::language()
    }

    fn extensions(&self) -> &[&str] {
        &["rs"]
    }

    fn extract_nodes(&self, tree: &Tree, source: &str, file_path: &str) -> Vec<CodeNode> {
        let mut nodes = Vec::new();
        let root = tree.root_node();

        extract_from_node(&root, source, file_path, &mut nodes, None);
        attach_rust_inheritance(&root, source, &mut nodes);

        nodes
    }
}

/// `impl Trait for Type` and `trait Child: Parent` are not class clauses.
/// Attach them onto the type or trait node extracted above.
fn attach_rust_inheritance(root: &Node, source: &str, nodes: &mut [CodeNode]) {
    walk_rust_inheritance(root, source, nodes);
}

fn walk_rust_inheritance(node: &Node, source: &str, nodes: &mut [CodeNode]) {
    match node.kind() {
        "impl_item" => {
            if let (Some(trait_node), Some(type_node)) = (
                node.child_by_field_name("trait"),
                node.child_by_field_name("type"),
            ) {
                let trait_name = clean_type_name(&node_text(&trait_node, source));
                let type_name = clean_type_name(&node_text(&type_node, source));
                if !trait_name.is_empty() && !type_name.is_empty() {
                    if let Some(target) = nodes.iter_mut().find(|n| {
                        is_rust_type(n.kind)
                            && (n.name == type_name || clean_type_name(&n.name) == type_name)
                    }) {
                        target.add_type_relation(TypeRelationKind::Implements, &trait_name);
                    }
                }
            }
        }
        "trait_item" => {
            let Some(name_node) = node.child_by_field_name("name") else {
                return;
            };
            let trait_name = node_text(&name_node, source);
            if let Some(bounds) = node.child_by_field_name("bounds") {
                let supers = super::heritage::type_names(&bounds, source);
                if let Some(target) = nodes
                    .iter_mut()
                    .find(|n| n.kind == NodeKind::Interface && n.name == trait_name)
                {
                    for parent in supers {
                        target.add_type_relation(TypeRelationKind::Extends, &parent);
                    }
                }
            }
        }
        _ => {}
    }

    for i in 0..node.child_count() {
        if let Some(child) = node.child(i) {
            walk_rust_inheritance(&child, source, nodes);
        }
    }
}

fn is_rust_type(kind: NodeKind) -> bool {
    matches!(
        kind,
        NodeKind::Struct | NodeKind::Enum | NodeKind::Interface | NodeKind::TypeAlias
    )
}

fn node_text(node: &Node, source: &str) -> String {
    let range = node.byte_range();
    if range.end <= source.len() {
        source[range].to_string()
    } else {
        String::new()
    }
}

fn extract_from_node(
    node: &Node,
    source: &str,
    file_path: &str,
    nodes: &mut Vec<CodeNode>,
    context: Option<&str>,
) {
    stacker::maybe_grow(64 * 1024, 4 * 1024 * 1024, || {
        let kind = node.kind();

        match kind {
            // Standalone functions
            "function_item" => {
                if let Some(code_node) = extract_function(node, source, file_path, context) {
                    nodes.push(code_node);
                }
            }

            // Structs
            "struct_item" => {
                if let Some(code_node) = extract_struct(node, source, file_path) {
                    nodes.push(code_node);
                }
            }

            // Enums
            "enum_item" => {
                if let Some(code_node) = extract_enum(node, source, file_path) {
                    nodes.push(code_node);
                }
            }

            // Traits (Rust's version of interfaces)
            "trait_item" => {
                if let Some(code_node) = extract_trait(node, source, file_path) {
                    let trait_name = code_node.name.clone();
                    nodes.push(code_node);

                    // Extract trait methods
                    if let Some(body) = find_child_by_kind(node, "declaration_list") {
                        for i in 0..body.child_count() {
                            if let Some(child) = body.child(i) {
                                extract_from_node(
                                    &child,
                                    source,
                                    file_path,
                                    nodes,
                                    Some(&trait_name),
                                );
                            }
                        }
                    }
                    return;
                }
            }

            // Impl blocks
            "impl_item" => {
                // Methods are keyed by the bare type (`impl<T> Foo<T>` → `Foo`)
                // so `Foo::new()` and `self.method()` can find them.
                let impl_target = get_impl_target(node, source).map(|target| {
                    let clean = clean_type_name(&target);
                    if clean.is_empty() {
                        target
                    } else {
                        clean
                    }
                });
                if let Some(body) = find_child_by_kind(node, "declaration_list") {
                    for i in 0..body.child_count() {
                        if let Some(child) = body.child(i) {
                            extract_from_node(
                                &child,
                                source,
                                file_path,
                                nodes,
                                impl_target.as_deref(),
                            );
                        }
                    }
                }
                return;
            }

            // Module declarations
            "mod_item" => {
                if let Some(code_node) = extract_module(node, source, file_path) {
                    nodes.push(code_node);
                }
            }

            // Use statements (imports)
            "use_declaration" => {
                if let Some(code_node) = extract_use(node, source, file_path) {
                    nodes.push(code_node);
                }
            }

            // Constants and statics
            "const_item" | "static_item" => {
                if let Some(code_node) = extract_const(node, source, file_path) {
                    nodes.push(code_node);
                }
            }

            // Type aliases
            "type_item" => {
                if let Some(code_node) = extract_type_alias(node, source, file_path) {
                    nodes.push(code_node);
                }
            }

            _ => {}
        }

        // Recurse into children
        for i in 0..node.child_count() {
            if let Some(child) = node.child(i) {
                extract_from_node(&child, source, file_path, nodes, context);
            }
        }
    }); // stacker::maybe_grow
}

/// Extracts a function or method.
fn extract_function(
    node: &Node,
    source: &str,
    file_path: &str,
    context: Option<&str>,
) -> Option<CodeNode> {
    let name_node = node.child_by_field_name("name")?;
    let name = get_text(&name_node, source);

    let kind = if context.is_some() {
        NodeKind::Method
    } else {
        NodeKind::Function
    };

    let qualified_name = match context {
        Some(ctx) => format!("{}.{}", ctx, name),
        None => name.clone(),
    };

    // Check visibility
    let visibility = detect_visibility(node, source);

    // Check for async
    let is_async = has_modifier(node, "async");

    // Build signature
    let signature = build_function_signature(node, source, &name);

    // Extract references. A method call on a receiver whose type the code
    // states is recorded with that type, so it resolves to one definition.
    let receivers = Receivers::of_function(node, source, context);
    let mut references = extract_call_references(node, source, &receivers);
    // What a call to this function hands back, so `let d = prepare()?;`
    // elsewhere can type `d`.
    if let Some(returned) = node
        .child_by_field_name("return_type")
        .and_then(|return_type| receivers.core_type(&return_type))
    {
        references.push(returns_ref(&returned));
    }

    Some(
        CodeNode::new(&name, &qualified_name, kind, file_path)
            .with_lines(
                node.start_position().row as u32 + 1,
                node.end_position().row as u32 + 1,
            )
            .with_bytes(node.start_byte() as u32, node.end_byte() as u32)
            .with_column(name_node.start_position().column as u32)
            .with_signature(signature)
            .with_visibility(visibility)
            .with_references(references)
            .with_async_if(is_async),
    )
}

/// Extracts a struct definition.
fn extract_struct(node: &Node, source: &str, file_path: &str) -> Option<CodeNode> {
    let name_node = node.child_by_field_name("name")?;
    let name = get_text(&name_node, source);
    let visibility = detect_visibility(node, source);

    Some(
        CodeNode::new(&name, &name, NodeKind::Struct, file_path)
            .with_lines(
                node.start_position().row as u32 + 1,
                node.end_position().row as u32 + 1,
            )
            .with_bytes(node.start_byte() as u32, node.end_byte() as u32)
            .with_column(name_node.start_position().column as u32)
            .with_visibility(visibility)
            // Field types let `state.draft.run_started()` resolve through
            // this struct from any file.
            .with_references(struct_field_refs(node, source, &name)),
    )
}

/// Extracts an enum definition.
fn extract_enum(node: &Node, source: &str, file_path: &str) -> Option<CodeNode> {
    let name_node = node.child_by_field_name("name")?;
    let name = get_text(&name_node, source);
    let visibility = detect_visibility(node, source);

    Some(
        CodeNode::new(&name, &name, NodeKind::Enum, file_path)
            .with_lines(
                node.start_position().row as u32 + 1,
                node.end_position().row as u32 + 1,
            )
            .with_bytes(node.start_byte() as u32, node.end_byte() as u32)
            .with_column(name_node.start_position().column as u32)
            .with_visibility(visibility),
    )
}

/// Extracts a trait definition.
fn extract_trait(node: &Node, source: &str, file_path: &str) -> Option<CodeNode> {
    let name_node = node.child_by_field_name("name")?;
    let name = get_text(&name_node, source);
    let visibility = detect_visibility(node, source);

    Some(
        CodeNode::new(&name, &name, NodeKind::Interface, file_path)
            .with_lines(
                node.start_position().row as u32 + 1,
                node.end_position().row as u32 + 1,
            )
            .with_bytes(node.start_byte() as u32, node.end_byte() as u32)
            .with_column(name_node.start_position().column as u32)
            .with_visibility(visibility),
    )
}

/// Extracts a module declaration.
fn extract_module(node: &Node, source: &str, file_path: &str) -> Option<CodeNode> {
    let name_node = node.child_by_field_name("name")?;
    let name = get_text(&name_node, source);
    let visibility = detect_visibility(node, source);

    Some(
        CodeNode::new(&name, &name, NodeKind::Module, file_path)
            .with_lines(
                node.start_position().row as u32 + 1,
                node.end_position().row as u32 + 1,
            )
            .with_bytes(node.start_byte() as u32, node.end_byte() as u32)
            .with_column(name_node.start_position().column as u32)
            .with_visibility(visibility),
    )
}

/// Extracts a use statement.
fn extract_use(node: &Node, source: &str, file_path: &str) -> Option<CodeNode> {
    // Get the full use path
    if let Some(arg) = node.child_by_field_name("argument") {
        let path = get_text(&arg, source);

        return Some(
            CodeNode::new(&path, &path, NodeKind::Import, file_path)
                .with_lines(
                    node.start_position().row as u32 + 1,
                    node.end_position().row as u32 + 1,
                )
                .with_bytes(node.start_byte() as u32, node.end_byte() as u32),
        );
    }
    None
}

/// Extracts a const or static item.
fn extract_const(node: &Node, source: &str, file_path: &str) -> Option<CodeNode> {
    let name_node = node.child_by_field_name("name")?;
    let name = get_text(&name_node, source);
    let visibility = detect_visibility(node, source);

    Some(
        CodeNode::new(&name, &name, NodeKind::Constant, file_path)
            .with_lines(
                node.start_position().row as u32 + 1,
                node.end_position().row as u32 + 1,
            )
            .with_bytes(node.start_byte() as u32, node.end_byte() as u32)
            .with_column(name_node.start_position().column as u32)
            .with_visibility(visibility),
    )
}

/// Extracts a type alias.
fn extract_type_alias(node: &Node, source: &str, file_path: &str) -> Option<CodeNode> {
    let name_node = node.child_by_field_name("name")?;
    let name = get_text(&name_node, source);
    let visibility = detect_visibility(node, source);

    Some(
        CodeNode::new(&name, &name, NodeKind::TypeAlias, file_path)
            .with_lines(
                node.start_position().row as u32 + 1,
                node.end_position().row as u32 + 1,
            )
            .with_bytes(node.start_byte() as u32, node.end_byte() as u32)
            .with_column(name_node.start_position().column as u32)
            .with_visibility(visibility),
    )
}

// ============================================================================
// Helper functions
// ============================================================================

/// Gets text content of a node.
fn get_text(node: &Node, source: &str) -> String {
    source[node.byte_range()].to_string()
}

/// Finds a child node by its kind.
fn find_child_by_kind<'a>(node: &'a Node, kind: &str) -> Option<Node<'a>> {
    for i in 0..node.child_count() {
        if let Some(child) = node.child(i) {
            if child.kind() == kind {
                return Some(child);
            }
        }
    }
    None
}

/// Gets the target type of an impl block (e.g., "UserService" from `impl UserService`).
fn get_impl_target(node: &Node, source: &str) -> Option<String> {
    // The type being implemented for
    if let Some(type_node) = node.child_by_field_name("type") {
        return Some(get_text(&type_node, source));
    }
    None
}

/// Detects visibility from Rust's pub/pub(crate) modifiers.
fn detect_visibility(node: &Node, source: &str) -> Visibility {
    for i in 0..node.child_count() {
        if let Some(child) = node.child(i) {
            if child.kind() == "visibility_modifier" {
                let text = get_text(&child, source);
                if text == "pub" {
                    return Visibility::Public;
                } else if text.contains("crate") || text.contains("super") {
                    return Visibility::Internal;
                }
            }
        }
    }
    Visibility::Private
}

/// Checks if a node has a specific modifier.
fn has_modifier(node: &Node, modifier: &str) -> bool {
    for i in 0..node.child_count() {
        if let Some(child) = node.child(i) {
            if child.kind() == modifier {
                return true;
            }
        }
    }
    false
}

/// Builds a function signature.
fn build_function_signature(node: &Node, source: &str, name: &str) -> String {
    let params = node
        .child_by_field_name("parameters")
        .map(|n| get_text(&n, source))
        .unwrap_or_else(|| "()".to_string());

    let return_type = node
        .child_by_field_name("return_type")
        .map(|n| get_text(&n, source))
        .unwrap_or_default();

    if return_type.is_empty() {
        format!("fn {}{}", name, params)
    } else {
        format!("fn {}{} {}", name, params, return_type)
    }
}

/// Method names so common on std types (`Option`, `Result`, `Vec`, `String`,
/// iterators, maps) that a call on a receiver of unknown type says nothing
/// about which definition is meant. Linking `.len()` to the one `len` a project
/// defines would invent callers, so these are not emitted as unknown-receiver
/// calls. A call on `self` is still emitted: its type is known.
const STD_METHODS: &[&str] = &[
    "all",
    "and",
    "and_then",
    "any",
    "as_bytes",
    "as_mut",
    "as_ref",
    "as_slice",
    "as_str",
    "borrow",
    "borrow_mut",
    "chain",
    "clone",
    "cloned",
    "cmp",
    "collect",
    "contains",
    "contains_key",
    "copied",
    "count",
    "dedup",
    "default",
    "ends_with",
    "entry",
    "enumerate",
    "eq",
    "err",
    "expect",
    "extend",
    "filter",
    "filter_map",
    "find",
    "first",
    "flat_map",
    "flatten",
    "fmt",
    "fold",
    "for_each",
    "from",
    "hash",
    "into",
    "into_iter",
    "is_empty",
    "is_err",
    "is_none",
    "is_ok",
    "is_some",
    "iter",
    "iter_mut",
    "join",
    "keys",
    "last",
    "len",
    "lock",
    "map",
    "map_err",
    "max",
    "min",
    "next",
    "ok",
    "ok_or",
    "ok_or_else",
    "or",
    "or_default",
    "or_else",
    "or_insert",
    "or_insert_with",
    "parse",
    "partial_cmp",
    "pop",
    "push",
    "push_str",
    "retain",
    "rev",
    "skip",
    "sort",
    "sort_by",
    "sort_by_key",
    "sort_unstable",
    "split",
    "starts_with",
    "sum",
    "take",
    "then",
    "to_lowercase",
    "to_owned",
    "to_string",
    "to_uppercase",
    "to_vec",
    "trim",
    "try_from",
    "try_into",
    "unwrap",
    "unwrap_or",
    "unwrap_or_default",
    "unwrap_or_else",
    "values",
    "zip",
];

/// Extracts function call references in the shapes the graph builder
/// resolves: `name`, `module::name`, `Type::name`, `self.name`, and `.name`
/// for a method on a receiver whose type isn't known.
fn extract_call_references(node: &Node, source: &str, receivers: &Receivers) -> Vec<String> {
    let mut refs = Vec::new();
    collect_calls(node, source, receivers, &mut refs);
    refs.sort();
    refs.dedup();
    refs
}

fn collect_calls(node: &Node, source: &str, receivers: &Receivers, refs: &mut Vec<String>) {
    stacker::maybe_grow(64 * 1024, 4 * 1024 * 1024, || {
        match node.kind() {
            "call_expression" => {
                if let Some(function) = node.child_by_field_name("function") {
                    refs.extend(call_reference(&function, source, receivers));
                }
            }
            // A macro's arguments are unparsed tokens, not expressions, so a
            // call inside `assert!(check(x))` or `format!("{}", f(x))` never
            // appears as a `call_expression`. Scan the tokens instead.
            "macro_invocation" => {
                for i in 0..node.child_count() {
                    if let Some(child) = node.child(i) {
                        if child.kind() == "token_tree" {
                            collect_token_calls(&child, source, refs);
                        }
                    }
                }
                return;
            }
            // A macro body is a template, not code this function runs.
            "macro_definition" => return,
            _ => {}
        }
        for i in 0..node.child_count() {
            if let Some(child) = node.child(i) {
                collect_calls(&child, source, receivers, refs);
            }
        }
    });
}

/// The reference for the `function` of a `call_expression`, if it names one.
fn call_reference(function: &Node, source: &str, receivers: &Receivers) -> Option<String> {
    match function.kind() {
        "identifier" => Some(get_text(function, source)),
        "scoped_identifier" => path_reference(&get_text(function, source)),
        "field_expression" => {
            let field = function.child_by_field_name("field")?;
            // `tuple.0(x)` calls a stored closure; there's no name to resolve.
            if field.kind() != "field_identifier" {
                return None;
            }
            let receiver = function.child_by_field_name("value")?;
            let name = get_text(&field, source);
            if receiver.kind() != "self" && !STD_METHODS.contains(&name.as_str()) {
                if let Some(receiver_type) = receivers.type_of(&receiver) {
                    return Some(typed_receiver_ref(&name, &receiver_type));
                }
            }
            method_reference(receiver.kind() == "self", &name)
        }
        // `parse::<u32>(s)` is a call to `parse`.
        "generic_function" => call_reference(
            &function.child_by_field_name("function")?,
            source,
            receivers,
        ),
        // Closures, `(self.handler)(x)`, `f()()`: no stable name.
        _ => None,
    }
}

/// `jobs::enqueue`, `Vec::<u8>::new`, `crate::db::get` → a `::` path with
/// generics removed. Qualified-type syntax (`<T as Trait>::f`) has no stable
/// owner and is skipped.
fn path_reference(text: &str) -> Option<String> {
    let text = text.trim();
    if text.starts_with('<') {
        return None;
    }
    let mut segments: Vec<&str> = Vec::new();
    for raw in text.split("::") {
        // A turbofish segment (`<u8>`) contributes nothing.
        let segment = raw.split('<').next().unwrap_or_default().trim();
        if segment.is_empty() {
            continue;
        }
        if !segment.chars().all(|c| c.is_alphanumeric() || c == '_') {
            return None;
        }
        segments.push(segment);
    }
    match segments.as_slice() {
        [] => None,
        [name] => Some((*name).to_string()),
        _ => Some(segments.join("::")),
    }
}

fn method_reference(on_self: bool, name: &str) -> Option<String> {
    if name.is_empty() {
        return None;
    }
    if on_self {
        return Some(format!("self.{name}"));
    }
    if STD_METHODS.contains(&name) {
        return None;
    }
    Some(format!(".{name}"))
}

/// Calls inside a macro's token tree: `name(`, `path::name(`, `.name(` and
/// `self.name(`. Nested groups and nested macros are scanned too.
fn collect_token_calls(tree: &Node, source: &str, refs: &mut Vec<String>) {
    stacker::maybe_grow(64 * 1024, 4 * 1024 * 1024, || {
        let tokens: Vec<Node> = (0..tree.child_count())
            .filter_map(|i| tree.child(i))
            .collect();
        for (i, token) in tokens.iter().enumerate() {
            if token.kind() != "token_tree" {
                continue;
            }
            collect_token_calls(token, source, refs);
            let opens_call = token.child(0).is_some_and(|open| open.kind() == "(");
            if !opens_call || i == 0 || tokens[i - 1].kind() != "identifier" {
                continue;
            }
            let mut start = i - 1;
            let mut path = vec![get_text(&tokens[start], source)];
            while start >= 2
                && tokens[start - 1].kind() == "::"
                && matches!(
                    tokens[start - 2].kind(),
                    "identifier" | "self" | "super" | "crate"
                )
            {
                path.insert(0, get_text(&tokens[start - 2], source));
                start -= 2;
            }
            let reference = if path.len() == 1 && start >= 1 && tokens[start - 1].kind() == "." {
                let on_self = start >= 2 && tokens[start - 2].kind() == "self";
                method_reference(on_self, &path[0])
            } else {
                path_reference(&path.join("::"))
            };
            refs.extend(reference);
        }
    });
}

// Builder pattern helpers
trait CodeNodeExt {
    fn with_async_if(self, cond: bool) -> Self;
}

impl CodeNodeExt for CodeNode {
    fn with_async_if(self, cond: bool) -> Self {
        if cond {
            self.as_async()
        } else {
            self
        }
    }
}

#[cfg(test)]
mod call_tests {
    use super::RustParser;
    use crate::languages::LanguageParser;
    use crate::node::CodeNode;

    fn parse(source: &str) -> Vec<CodeNode> {
        let mut parser = tree_sitter::Parser::new();
        parser.set_language(&RustParser.language()).unwrap();
        let tree = parser.parse(source, None).unwrap();
        assert!(!tree.root_node().has_error(), "fixture must parse cleanly");
        RustParser.extract_nodes(&tree, source, "src/billing.rs")
    }

    fn references_of<'a>(nodes: &'a [CodeNode], name: &str) -> &'a [String] {
        &nodes
            .iter()
            .find(|n| n.name == name)
            .unwrap_or_else(|| panic!("no node {name}"))
            .references
    }

    const SOURCE: &str = r#"
struct Provider;
struct Wrapper<T>(T);
impl<T> Wrapper<T> {
    fn make() {}
}
impl Provider {
    fn order_history(&self, store: Store) {
        self.owner_of(1);
        jobs::enqueue(pool, actor);
        crate::app::create_project(1);
        Provider::new();
        let v: Vec<u8> = Vec::with_capacity(3);
        store.fetch_ledger(1).unwrap();
        items.iter().map(|x| x.len()).collect::<Vec<_>>();
        parse::<u32>("1");
        assert!(verify_webhook_signature(&body, sig));
        assert_eq!(self.total(), money::round_cents(2.0));
        println!("{}", format_money(x));
        tokio::spawn(async move { worker::run().await });
        (self.handler)(1);
        <T as Default>::default();
    }
}
"#;

    #[test]
    fn calls_are_normalized_to_resolvable_shapes() {
        let nodes = parse(SOURCE);
        let refs = references_of(&nodes, "order_history");
        for expected in [
            "self.owner_of",
            "jobs::enqueue",
            "crate::app::create_project",
            "Provider::new",
            "Vec::with_capacity",
            ".fetch_ledger@Store",
            "parse",
            "tokio::spawn",
            "worker::run",
        ] {
            assert!(
                refs.iter().any(|r| r == expected),
                "missing {expected} in {refs:?}"
            );
        }
        // Whole chains and generics never leak into a reference.
        assert!(
            refs.iter().all(|r| !r.contains('(') && !r.contains('<')),
            "{refs:?}"
        );
    }

    #[test]
    fn calls_inside_macros_are_found() {
        let nodes = parse(SOURCE);
        let refs = references_of(&nodes, "order_history");
        for expected in [
            "verify_webhook_signature",
            "self.total",
            "money::round_cents",
            "format_money",
        ] {
            assert!(
                refs.iter().any(|r| r == expected),
                "missing {expected} in {refs:?}"
            );
        }
    }

    const RECEIVERS: &str = r#"
pub struct AppState {
    draft: Mutex<Draft>,
    backup: Option<Box<Draft>>,
    drafts: HashMap<u32, Draft>,
    count: u32,
}
struct Session { state: Arc<AppState> }
impl Session {
    fn tick(&mut self) {
        self.state.draft.lock().unwrap().run_started(9);
    }
}
fn typed_parameter(mut draft: &mut Draft, state: State<'_, AppState>) {
    draft.run_started(1);
    state.draft.lock().unwrap().run_started(2);
}
fn locals(state: &AppState, app: AppHandle) {
    let mut guard = state.draft.lock().unwrap();
    guard.run_started(3);
    let fresh = Draft::new()?;
    fresh.run_started(4);
    let literal = Draft { runs: 0 };
    literal.run_started(5);
    let annotated: Box<Draft> = make();
    annotated.run_started(6);
    if let Some(saved) = state.backup.as_mut() {
        saved.run_started(7);
    }
    let managed = app.state::<AppState>();
    managed.drafts.get_mut(&1).unwrap().run_started(8);
    let handler = |d: &mut Draft| d.run_started(10);
}
fn traits<T: RunObserver>(observer: &mut T, dynamic: &mut dyn RunObserver) {
    observer.run_started(11);
    dynamic.run_started(12);
}
fn uncertain<U>(value: U, client: reqwest::Client) {
    let shadowed = Draft::new();
    let shadowed = Telemetry::new();
    shadowed.run_started(13);
    value.run_started(14);
    let loaded = load_draft();
    loaded.run_started(15);
    for item in items { item.run_started(16); }
    client.send_report();
}
"#;

    #[test]
    fn method_calls_record_the_receiver_type_the_code_states() {
        let nodes = parse(RECEIVERS);
        let expect = |function: &str, wanted: &[&str]| {
            let refs = references_of(&nodes, function);
            for expected in wanted {
                assert!(
                    refs.iter().any(|r| r == expected),
                    "{function}: missing {expected} in {refs:?}"
                );
            }
        };
        // Parameters, through a reference and through Tauri's `State`.
        expect(
            "typed_parameter",
            &[".run_started@Draft", ".run_started@AppState.draft"],
        );
        // A lock guard, a constructor with `?`, a struct literal, an
        // annotation through `Box`, `if let Some`, `app.state::<T>()` with a
        // map lookup, and a typed closure parameter.
        expect(
            "locals",
            &[
                ".run_started@AppState.draft",
                ".run_started@Draft",
                ".run_started@AppState.backup",
                ".run_started@AppState.drafts",
            ],
        );
        // `self.field` goes through the impl's own type.
        expect("tick", &[".run_started@Session.state.draft"]);
        // A bounded generic and a trait object resolve to the trait.
        expect("traits", &[".run_started@RunObserver"]);
    }

    // The shapes from a real review: a draft prepared by a helper, then
    // started from a `let … else` and from a channel inside a `match`.
    const CALL_RESULTS: &str = r#"
fn prepare_draft(handle: &AppHandle) -> Option<drafts::mac::PreparedDraft> {
    None
}
struct PendingDraft {
    receiver: std::sync::mpsc::Receiver<Option<drafts::mac::PreparedDraft>>,
}
impl PendingDraft {
    fn run(self) {
        match self.receiver.recv() {
            Ok(Some(draft)) => draft.run_started(run),
            Ok(None) | Err(_) => run.finish("failed"),
        }
    }
}
fn start_draft(handle: &AppHandle) {
    let Some(run) = drafts::mac::DraftRun::begin(handle) else {
        return;
    };
    let Some(draft) = prepare_draft(handle) else {
        run.finish("failed", 0, None);
        return;
    };
    draft.run_started(run);
}
async fn fetch(client: &Client) -> Result<Draft, Error> {
    let loaded = load(client).await?;
    loaded.session().run_started(1);
}
struct Loader;
impl Loader {
    fn go() {
        Self::open().run_started(2);
    }
}
"#;

    #[test]
    fn call_results_type_their_receivers() {
        let nodes = parse(CALL_RESULTS);
        let refs = |name: &str| references_of(&nodes, name).to_vec();
        // The function records what it returns, through `Option` and a path.
        assert!(refs("prepare_draft").contains(&"returns:PreparedDraft".to_string()));
        // `Result<Draft, Error>` returns the `Draft`, not the error.
        assert!(
            refs("fetch").contains(&"returns:Draft".to_string()),
            "{:?}",
            refs("fetch")
        );
        let run = refs("start_draft");
        assert!(
            run.contains(&".run_started@prepare_draft()".to_string()),
            "{run:?}"
        );
        assert!(
            run.contains(&".finish@drafts::mac::DraftRun::begin()".to_string()),
            "{run:?}"
        );
        // A field typed by a channel of `Option<…>`, through `recv()` and
        // the nested `Ok(Some(draft))` arm.
        assert!(refs("run").contains(&".run_started@PendingDraft.receiver".to_string()));
        // `.await` passes through; a method result is followed by name.
        let fetch = refs("fetch");
        assert!(
            fetch.contains(&".run_started@load().session()".to_string()),
            "{fetch:?}"
        );
        // `Self::` in an impl names the impl's type.
        assert!(refs("go").contains(&".run_started@Loader::open()".to_string()));
    }

    #[test]
    fn uncertain_receivers_stay_unknown() {
        let nodes = parse(RECEIVERS);
        let refs = references_of(&nodes, "uncertain");
        // Shadowed with two types, an unbounded generic and a loop variable:
        // no type is claimed.
        assert!(refs.iter().any(|r| r == ".run_started"), "{refs:?}");
        // A function's result is typed by what the graph finds it returns.
        let typed: Vec<&String> = refs
            .iter()
            .filter(|r| r.starts_with(".run_started@"))
            .collect();
        assert_eq!(typed, [".run_started@load_draft()"], "{refs:?}");
        // An external type is still recorded; the graph drops it as external.
        assert!(refs.iter().any(|r| r == ".send_report@Client"), "{refs:?}");
    }

    #[test]
    fn struct_fields_record_the_type_a_method_reaches() {
        let nodes = parse(RECEIVERS);
        let state = nodes.iter().find(|n| n.name == "AppState").unwrap();
        for expected in [
            "field:draft:Draft",
            "field:backup:Draft",
            "field:drafts:Draft",
        ] {
            assert!(
                state.references.iter().any(|r| r == expected),
                "missing {expected} in {:?}",
                state.references
            );
        }
        // A primitive has no methods of the project's.
        assert!(!state
            .references
            .iter()
            .any(|r| r.starts_with("field:count:")));
    }

    #[test]
    fn std_methods_on_unknown_receivers_are_not_references() {
        let nodes = parse(SOURCE);
        let refs = references_of(&nodes, "order_history");
        for noise in [".unwrap", ".iter", ".map", ".len", ".collect", "default"] {
            assert!(!refs.iter().any(|r| r == noise), "{noise} in {refs:?}");
        }
    }

    #[test]
    fn generic_impls_key_methods_by_the_bare_type() {
        let nodes = parse(SOURCE);
        let make = nodes.iter().find(|n| n.name == "make").unwrap();
        assert_eq!(make.qualified_name, "Wrapper.make");
        let history = nodes.iter().find(|n| n.name == "order_history").unwrap();
        assert_eq!(history.qualified_name, "Provider.order_history");
    }
}