ast-bro 2.2.0

Fast, AST-based code-navigation: shape, public API, deps & call graphs, hybrid semantic search, structural rewrite. MCP server included.
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
//! Per-language import extraction. Wraps existing `surface::imports`
//! extractors for Rust/Python/TS/JS/Scala and adds new tree-sitter
//! passes for Java/C#/Kotlin/Go.
//!
//! All extractors emit `RawImport` records — language-agnostic units
//! the resolver consumes. Each record carries enough info that the
//! renderer can show the original statement + line.

use ast_grep_core::{Doc, Node};
use ast_grep_language::{LanguageExt, SupportLang};
use std::path::Path;

use crate::deps::graph::ImportKind;
use crate::deps::resolver::build::Lang;
use crate::surface::imports as surface_imports;

/// One extracted import. Targets are normalised slash-joined module
/// paths (e.g. `com/foo/Bar`, `crate/net/client`); resolver does the
/// final mapping to a file.
#[derive(Debug, Clone)]
pub struct RawImport {
    pub spec: String,
    pub kind: ImportKind,
    pub line: u32,
    /// Display-only: the original statement source line.
    #[allow(dead_code)]
    pub statement: String,
    pub local_name: Option<String>,
    /// Source dotted path (preserves dots, no slashes).
    pub raw_path: Option<String>,
}

/// Top-level dispatch — extract every import from `path`. Returns
/// nothing for unrecognised extensions. Caller is responsible for
/// reading the file (we do it here).
pub fn extract(path: &Path, lang: Lang) -> Vec<RawImport> {
    let Ok(src) = std::fs::read_to_string(path) else {
        return Vec::new();
    };
    match lang {
        Lang::Rust => extract_rust(&src),
        Lang::Python => extract_python(&src),
        Lang::TypeScript | Lang::Tsx | Lang::JavaScript => extract_typescript(&src, lang),
        Lang::Scala => extract_scala(&src),
        Lang::Java => extract_java(&src),
        Lang::Kotlin => extract_kotlin(&src),
        Lang::CSharp => extract_csharp(&src),
        Lang::Go => extract_go(&src),
        Lang::Cpp => extract_cpp(&src),
        Lang::Php => extract_php(&src),
        Lang::Ruby => extract_ruby(&src),
        Lang::Other => Vec::new(),
    }
}

// ---- Rust ----

fn extract_rust(src: &str) -> Vec<RawImport> {
    let imports = surface_imports::extract_rust_imports(src);
    let mut out = Vec::new();
    for u in imports.uses {
        let kind = match u.kind {
            surface_imports::UseSegmentKind::Item => ImportKind::Use,
            surface_imports::UseSegmentKind::Glob => ImportKind::Glob,
        };
        out.push(RawImport {
            spec: u.path.clone(),
            kind,
            line: u.line as u32,
            statement: u.statement,
            local_name: u.alias,
            raw_path: Some(u.path),
        });
    }
    for m in imports.mods {
        if !m.is_external_file {
            continue;
        }
        out.push(RawImport {
            spec: format!("self::{}", m.name),
            kind: ImportKind::Mod,
            line: m.line as u32,
            statement: format!("mod {};", m.name),
            local_name: None,
            raw_path: Some(m.name),
        });
    }
    out
}

// ---- Python ----

fn extract_python(src: &str) -> Vec<RawImport> {
    let mut out = Vec::new();
    let imports = surface_imports::extract_python_imports(src);
    for fi in imports.from_imports {
        let prefix: String = ".".repeat(fi.relative_dots);
        let module = if fi.module.is_empty() {
            prefix.clone()
        } else {
            format!("{}{}", prefix, fi.module)
        };
        if fi.is_glob {
            // Treat `from x import *` as one edge to the source module.
            let spec = normalise_python(&module, fi.relative_dots);
            out.push(RawImport {
                spec,
                kind: ImportKind::StarFrom,
                line: fi.line as u32,
                statement: fi.statement.clone(),
                local_name: None,
                raw_path: Some(module.clone()),
            });
            continue;
        }
        // For each name imported, emit one edge — first try resolving
        // the full `module.name` path (e.g. `pkg.sub.fn`), the resolver
        // will fall back to dropping the trailing segment.
        if fi.names.is_empty() {
            let spec = normalise_python(&module, fi.relative_dots);
            out.push(RawImport {
                spec,
                kind: ImportKind::From,
                line: fi.line as u32,
                statement: fi.statement.clone(),
                local_name: None,
                raw_path: Some(module),
            });
            continue;
        }
        for n in fi.names {
            let dotted = if fi.module.is_empty() {
                format!("{}{}", prefix, n.name)
            } else {
                format!("{}.{}", module, n.name)
            };
            let spec = normalise_python(&dotted, fi.relative_dots);
            out.push(RawImport {
                spec,
                kind: ImportKind::From,
                line: fi.line as u32,
                statement: fi.statement.clone(),
                local_name: n.alias,
                raw_path: Some(dotted),
            });
        }
    }
    // Bare `import x.y` — surface::imports doesn't extract these, so
    // we run a small ast-grep pass directly.
    out.extend(extract_python_bare(src));
    out
}

fn normalise_python(module: &str, dots: usize) -> String {
    if dots == 0 {
        // Absolute: `a.b.c` → `a/b/c`.
        return module.replace('.', "/");
    }
    // Relative: leading `.` or `..` becomes `./` or `../`.
    let body = module.trim_start_matches('.').replace('.', "/");
    let mut s = String::new();
    if dots == 1 {
        s.push_str("./");
    } else {
        for _ in 0..dots - 1 {
            s.push_str("../");
        }
    }
    s.push_str(&body);
    if s.ends_with('/') {
        s.pop();
    }
    s
}

fn extract_python_bare(src: &str) -> Vec<RawImport> {
    let mut out = Vec::new();
    let lang = SupportLang::Python;
    let ast = lang.ast_grep(src);
    let root = ast.root();
    _walk_python_bare(&root, &mut out);
    out
}

fn _walk_python_bare<'a, D: Doc>(node: &Node<'a, D>, out: &mut Vec<RawImport>) {
    for c in node.children() {
        if !c.is_named() {
            continue;
        }
        if c.kind() == "import_statement" {
            // Children include `dotted_name` or `aliased_import` for each name.
            let line = (c.start_pos().line() + 1) as u32;
            let stmt = c.text().into_owned();
            for n in c.children() {
                let k = n.kind();
                if k == "dotted_name" {
                    let mod_name = n.text().into_owned();
                    out.push(RawImport {
                        spec: mod_name.replace('.', "/"),
                        kind: ImportKind::Bare,
                        line,
                        statement: stmt.clone(),
                        local_name: None,
                        raw_path: Some(mod_name),
                    });
                } else if k == "aliased_import" {
                    let name = n.field("name").map(|f| f.text().into_owned());
                    let alias = n.field("alias").map(|f| f.text().into_owned());
                    if let Some(name) = name {
                        out.push(RawImport {
                            spec: name.replace('.', "/"),
                            kind: ImportKind::Bare,
                            line,
                            statement: stmt.clone(),
                            local_name: alias,
                            raw_path: Some(name),
                        });
                    }
                }
            }
        }
    }
}

// ---- TS / JS ----

fn extract_typescript(src: &str, lang: Lang) -> Vec<RawImport> {
    let support = match lang {
        Lang::TypeScript => SupportLang::TypeScript,
        Lang::Tsx => SupportLang::Tsx,
        Lang::JavaScript => SupportLang::JavaScript,
        _ => return Vec::new(),
    };
    let ast = support.ast_grep(src);
    let root = ast.root();
    let mut out = Vec::new();
    _walk_ts(&root, &mut out);
    out
}

fn _walk_ts<'a, D: Doc>(node: &Node<'a, D>, out: &mut Vec<RawImport>) {
    for c in node.children() {
        if !c.is_named() {
            continue;
        }
        let kind = c.kind();
        let kind = kind.as_ref();
        if kind == "import_statement" {
            consume_ts_import(&c, out);
        } else if kind == "export_statement" {
            // Re-exports are also dependency edges.
            consume_ts_export(&c, out);
        } else if kind == "expression_statement" {
            // Top-level CommonJS `require('x')` calls.
            consume_ts_require(&c, out);
        }
    }
}

fn consume_ts_import<'a, D: Doc>(node: &Node<'a, D>, out: &mut Vec<RawImport>) {
    let Some(source) = node.field("source") else {
        return;
    };
    let from = strip_quotes(&source.text());
    if from.is_empty() {
        return;
    }
    let line = (node.start_pos().line() + 1) as u32;
    let stmt = node.text().into_owned();

    // Detect what kind of import this is.
    let mut named_seen = false;
    let mut star_seen = false;
    for c in node.children() {
        let k = c.kind();
        if k == "import_clause" {
            for sub in c.children() {
                let sk = sub.kind();
                if sk == "named_imports" {
                    named_seen = true;
                } else if sk == "namespace_import" {
                    star_seen = true;
                }
            }
        }
    }
    let kind = if star_seen {
        ImportKind::StarFrom
    } else if named_seen {
        ImportKind::NamedFrom
    } else {
        ImportKind::Bare
    };
    out.push(RawImport {
        spec: from.clone(),
        kind,
        line,
        statement: stmt,
        local_name: None,
        raw_path: Some(from),
    });
}

fn consume_ts_export<'a, D: Doc>(node: &Node<'a, D>, out: &mut Vec<RawImport>) {
    let Some(source) = node.field("source") else {
        return;
    };
    let from = strip_quotes(&source.text());
    if from.is_empty() {
        return;
    }
    let line = (node.start_pos().line() + 1) as u32;
    let stmt = node.text().into_owned();
    let mut star = false;
    for c in node.children() {
        let k = c.kind();
        if k == "export_clause" {
            // `export { Foo } from 'x'`
            out.push(RawImport {
                spec: from.clone(),
                kind: ImportKind::NamedFrom,
                line,
                statement: stmt.clone(),
                local_name: None,
                raw_path: Some(from.clone()),
            });
            return;
        } else if k == "*" || k.as_ref() == "namespace_export" {
            star = true;
        }
    }
    out.push(RawImport {
        spec: from.clone(),
        kind: if star {
            ImportKind::StarFrom
        } else {
            ImportKind::NamedFrom
        },
        line,
        statement: stmt,
        local_name: None,
        raw_path: Some(from),
    });
}

fn consume_ts_require<'a, D: Doc>(node: &Node<'a, D>, out: &mut Vec<RawImport>) {
    // Look for `require('x')` or `require("x")` calls, top-level only.
    let text = node.text();
    let s = text.as_ref();
    if !s.contains("require(") {
        return;
    }
    let line = (node.start_pos().line() + 1) as u32;
    if let Some(start) = s.find("require(") {
        let after = &s[start + "require(".len()..];
        let arg = after.split(')').next().unwrap_or("").trim();
        let from = arg.trim_matches('\'').trim_matches('"');
        if !from.is_empty() && (from.starts_with('.') || !from.contains(' ')) {
            out.push(RawImport {
                spec: from.to_string(),
                kind: ImportKind::Bare,
                line,
                statement: s.to_string(),
                local_name: None,
                raw_path: Some(from.to_string()),
            });
        }
    }
}

fn strip_quotes(s: &str) -> String {
    let t = s.trim();
    // Length guard: a single `"` or `'` satisfies both starts_with and
    // ends_with against itself, but `t[1..t.len() - 1]` would panic on
    // `1..0`. Require a real pair (>= 2 bytes) before stripping.
    if t.len() >= 2
        && ((t.starts_with('"') && t.ends_with('"'))
            || (t.starts_with('\'') && t.ends_with('\'')))
    {
        t[1..t.len() - 1].to_string()
    } else {
        t.to_string()
    }
}

// ---- Scala ----

fn extract_scala(src: &str) -> Vec<RawImport> {
    let lang = SupportLang::Scala;
    let ast = lang.ast_grep(src);
    let root = ast.root();
    let mut out = Vec::new();
    _walk_scala(&root, &mut out);
    out
}

fn _walk_scala<'a, D: Doc>(node: &Node<'a, D>, out: &mut Vec<RawImport>) {
    for c in node.children() {
        if !c.is_named() {
            continue;
        }
        let kind = c.kind();
        let kind = kind.as_ref();
        if kind == "import_declaration" {
            // The import's text is `import a.b.c` or `import a.b.{c, d}`.
            let line = (c.start_pos().line() + 1) as u32;
            let stmt = c.text().into_owned();
            // Heuristic: take the part after `import `, split on `{` for selectors.
            let after_kw = stmt.trim_start_matches("import").trim_start();
            let (base, selectors) = match after_kw.split_once('{') {
                Some((a, sel)) => (a.trim().trim_end_matches('.').to_string(), Some(sel)),
                None => (after_kw.trim().to_string(), None),
            };
            if let Some(sel) = selectors {
                let inner = sel.trim_end_matches('}');
                for raw in inner.split(',') {
                    let part = raw.trim();
                    if part.is_empty() {
                        continue;
                    }
                    let (name, alias) = match part.split_once("=>") {
                        Some((n, a)) => (n.trim().to_string(), Some(a.trim().to_string())),
                        None => (part.to_string(), None),
                    };
                    let spec = if name == "_" {
                        base.clone()
                    } else {
                        format!("{}.{}", base, name)
                    };
                    out.push(RawImport {
                        spec: spec.replace('.', "/"),
                        kind: if name == "_" {
                            ImportKind::StarFrom
                        } else {
                            ImportKind::Bare
                        },
                        line,
                        statement: stmt.clone(),
                        local_name: alias,
                        raw_path: Some(spec),
                    });
                }
            } else {
                let kind = if base.ends_with("._") {
                    ImportKind::StarFrom
                } else {
                    ImportKind::Bare
                };
                let dotted = base.trim_end_matches("._").to_string();
                out.push(RawImport {
                    spec: dotted.replace('.', "/"),
                    kind,
                    line,
                    statement: stmt,
                    local_name: None,
                    raw_path: Some(dotted),
                });
            }
        } else if matches!(
            kind,
            "package_clause" | "object_definition" | "class_definition" | "trait_definition"
        ) {
            // Descend into bodies — Scala 3 allows nested imports.
            _walk_scala(&c, out);
        }
    }
}

// ---- Java ----

fn extract_java(src: &str) -> Vec<RawImport> {
    let lang = SupportLang::Java;
    let ast = lang.ast_grep(src);
    let root = ast.root();
    let mut out = Vec::new();
    for c in root.children() {
        if !c.is_named() {
            continue;
        }
        if c.kind() == "import_declaration" {
            let line = (c.start_pos().line() + 1) as u32;
            let stmt = c.text().into_owned();
            let body = stmt
                .trim_start_matches("import")
                .trim_end_matches(';')
                .trim();
            let is_static = body.starts_with("static ");
            let body = body.trim_start_matches("static ").trim();
            let is_glob = body.ends_with(".*");
            let dotted = body.trim_end_matches(".*").to_string();
            out.push(RawImport {
                spec: dotted.replace('.', "/"),
                kind: if is_static {
                    ImportKind::Static
                } else if is_glob {
                    ImportKind::Glob
                } else {
                    ImportKind::Bare
                },
                line,
                statement: stmt,
                local_name: None,
                raw_path: Some(dotted),
            });
        }
    }
    out
}

// ---- Kotlin ----

fn extract_kotlin(src: &str) -> Vec<RawImport> {
    let lang = SupportLang::Kotlin;
    let ast = lang.ast_grep(src);
    let root = ast.root();
    let mut out = Vec::new();
    _walk_kotlin(&root, &mut out);
    out
}

fn _walk_kotlin<'a, D: Doc>(node: &Node<'a, D>, out: &mut Vec<RawImport>) {
    for c in node.children() {
        if !c.is_named() {
            continue;
        }
        let kind = c.kind();
        let kind = kind.as_ref();
        if kind == "import_header" || kind == "import_directive" || kind == "import_list" {
            // Some Kotlin tree-sitter grammars name it differently;
            // handle either. The text form is `import x.y.Z [as W]`.
            if kind == "import_list" {
                _walk_kotlin(&c, out);
                continue;
            }
            let line = (c.start_pos().line() + 1) as u32;
            let stmt = c.text().into_owned();
            let body = stmt.trim_start_matches("import").trim();
            // Optional `as Quux` rename.
            let (path, alias) = match body.split_once(" as ") {
                Some((p, a)) => (p.trim().to_string(), Some(a.trim().to_string())),
                None => (body.to_string(), None),
            };
            let is_glob = path.ends_with(".*");
            let dotted = path.trim_end_matches(".*").to_string();
            out.push(RawImport {
                spec: dotted.replace('.', "/"),
                kind: if alias.is_some() {
                    ImportKind::Alias
                } else if is_glob {
                    ImportKind::Glob
                } else {
                    ImportKind::Bare
                },
                line,
                statement: stmt,
                local_name: alias,
                raw_path: Some(dotted),
            });
        }
    }
}

// ---- C# ----

fn extract_csharp(src: &str) -> Vec<RawImport> {
    let lang = SupportLang::CSharp;
    let ast = lang.ast_grep(src);
    let root = ast.root();
    let mut out = Vec::new();
    _walk_csharp(&root, &mut out);
    out
}

fn _walk_csharp<'a, D: Doc>(node: &Node<'a, D>, out: &mut Vec<RawImport>) {
    for c in node.children() {
        if !c.is_named() {
            continue;
        }
        let kind = c.kind();
        let kind = kind.as_ref();
        if kind == "using_directive" {
            let line = (c.start_pos().line() + 1) as u32;
            let stmt = c.text().into_owned();
            let body = stmt.trim_start_matches("using").trim_end_matches(';').trim().to_string();
            let is_static = body.starts_with("static ");
            let rest = body.trim_start_matches("static ").trim().to_string();

            // Alias form: `A = X.Y` (no `static`).
            if !is_static {
                if let Some((alias, target)) = rest.split_once('=') {
                    let dotted = target.trim().to_string();
                    out.push(RawImport {
                        spec: dotted.replace('.', "/"),
                        kind: ImportKind::Alias,
                        line,
                        statement: stmt,
                        local_name: Some(alias.trim().to_string()),
                        raw_path: Some(dotted),
                    });
                    continue;
                }
            }

            let dotted = rest.to_string();
            out.push(RawImport {
                spec: dotted.replace('.', "/"),
                kind: if is_static {
                    ImportKind::Static
                } else {
                    ImportKind::Bare
                },
                line,
                statement: stmt,
                local_name: None,
                raw_path: Some(dotted),
            });
        } else if matches!(kind, "namespace_declaration" | "file_scoped_namespace_declaration") {
            // Recurse into namespace bodies; usings can live inside.
            _walk_csharp(&c, out);
        }
    }
}

// ---- Go ----

fn extract_go(src: &str) -> Vec<RawImport> {
    let lang = SupportLang::Go;
    let ast = lang.ast_grep(src);
    let root = ast.root();
    let mut out = Vec::new();
    for c in root.children() {
        if !c.is_named() {
            continue;
        }
        if c.kind() == "import_declaration" {
            // Either single `import "foo"` or grouped `import (...)`.
            for spec in c.children() {
                if !spec.is_named() {
                    continue;
                }
                if spec.kind() == "import_spec" {
                    consume_go_spec(&spec, &mut out);
                } else if spec.kind() == "import_spec_list" {
                    for inner in spec.children() {
                        if inner.is_named() && inner.kind() == "import_spec" {
                            consume_go_spec(&inner, &mut out);
                        }
                    }
                }
            }
        }
    }
    out
}

fn consume_go_spec<'a, D: Doc>(node: &Node<'a, D>, out: &mut Vec<RawImport>) {
    let line = (node.start_pos().line() + 1) as u32;
    let stmt = node.text().into_owned();
    let path = node.field("path").map(|f| f.text().into_owned());
    let name = node.field("name").map(|f| f.text().into_owned());
    let Some(path) = path else { return };
    let stripped = path.trim_matches('"').to_string();
    out.push(RawImport {
        spec: stripped.clone(),
        kind: ImportKind::Bare,
        line,
        statement: stmt,
        local_name: name,
        raw_path: Some(stripped),
    });
}

// ---- C++ ----

fn extract_cpp(src: &str) -> Vec<RawImport> {
    let lang = SupportLang::Cpp;
    let ast = lang.ast_grep(src);
    let root = ast.root();
    let mut out = Vec::new();
    _walk_cpp(&root, &mut out);
    out
}

fn _walk_cpp<'a, D: Doc>(node: &Node<'a, D>, out: &mut Vec<RawImport>) {
    for c in node.children() {
        if !c.is_named() {
            continue;
        }
        let kind = c.kind();
        let kind = kind.as_ref();
        if kind == "preproc_include" {
            consume_cpp_include(&c, out);
        } else if matches!(
            kind,
            "namespace_definition"
                | "linkage_specification"
                | "translation_unit"
                | "preproc_ifdef"
                | "preproc_if"
                | "preproc_else"
                | "preproc_elif"
        ) {
            // Headers commonly wrap includes in `#ifndef … #endif` guards or
            // `extern "C" { … }`; namespaces also occasionally hold them.
            _walk_cpp(&c, out);
        }
    }
}

fn consume_cpp_include<'a, D: Doc>(node: &Node<'a, D>, out: &mut Vec<RawImport>) {
    let line = (node.start_pos().line() + 1) as u32;
    let stmt = node.text().into_owned();
    for sub in node.children() {
        if !sub.is_named() {
            continue;
        }
        let k = sub.kind();
        let k = k.as_ref();
        if k == "string_literal" {
            // `#include "local.h"` — local include, resolve relative.
            let raw = sub.text().into_owned();
            let path = raw.trim().trim_matches('"').to_string();
            if path.is_empty() {
                continue;
            }
            let spec = if path.starts_with("./") || path.starts_with("../") {
                path.clone()
            } else {
                format!("./{}", path)
            };
            out.push(RawImport {
                spec,
                kind: ImportKind::Bare,
                line,
                statement: stmt.clone(),
                local_name: None,
                raw_path: Some(path),
            });
        } else if k == "system_lib_string" {
            // `#include <vector>` — system header. Emit so it shows up in
            // external listings; the resolver won't find it inside the project.
            let raw = sub.text().into_owned();
            let inner = raw.trim().trim_start_matches('<').trim_end_matches('>').to_string();
            if inner.is_empty() {
                continue;
            }
            out.push(RawImport {
                spec: inner.clone(),
                kind: ImportKind::Bare,
                line,
                statement: stmt.clone(),
                local_name: None,
                raw_path: Some(format!("<{}>", inner)),
            });
        }
    }
}

// ---- PHP ----

fn extract_php(src: &str) -> Vec<RawImport> {
    let lang = SupportLang::Php;
    let ast = lang.ast_grep(src);
    let root = ast.root();
    let mut out = Vec::new();
    _walk_php(&root, &mut out);
    out
}

fn _walk_php<'a, D: Doc>(node: &Node<'a, D>, out: &mut Vec<RawImport>) {
    for c in node.children() {
        if !c.is_named() {
            continue;
        }
        let kind = c.kind();
        let kind = kind.as_ref();
        if kind == "namespace_use_declaration" {
            consume_php_use(&c, out);
            continue;
        }
        if matches!(
            kind,
            "require_expression"
                | "require_once_expression"
                | "include_expression"
                | "include_once_expression"
        ) {
            consume_php_require(&c, out);
            continue;
        }
        // Imports can appear inside any nesting structure (class bodies,
        // method bodies, conditionals). Recurse through every named child;
        // the cost is negligible and matching is type-driven.
        _walk_php(&c, out);
    }
}

fn consume_php_use<'a, D: Doc>(node: &Node<'a, D>, out: &mut Vec<RawImport>) {
    let line = (node.start_pos().line() + 1) as u32;
    let stmt = node.text().into_owned();
    // `use App\Core\Foo;`, `use App\Core\Foo as Bar;`,
    // `use App\Core\{Foo, Bar};` — walk children for clauses.
    for sub in node.children() {
        if !sub.is_named() {
            continue;
        }
        let k = sub.kind();
        let k = k.as_ref();
        if k == "namespace_use_clause" || k == "namespace_use_group_clause" {
            let text = sub.text().into_owned();
            let (path, alias) = match text.split_once(" as ") {
                Some((p, a)) => (p.trim().to_string(), Some(a.trim().to_string())),
                None => (text.trim().to_string(), None),
            };
            let path = path.trim_start_matches('\\').to_string();
            if path.is_empty() {
                continue;
            }
            out.push(RawImport {
                spec: path.replace('\\', "/"),
                kind: ImportKind::Use,
                line,
                statement: stmt.clone(),
                local_name: alias,
                raw_path: Some(path),
            });
        } else if k == "namespace_use_group" {
            // `use App\Core\{Foo, Bar};` — find the prefix qualified_name and
            // each member clause inside the group.
            let mut prefix: Option<String> = None;
            for inner in sub.children() {
                if !inner.is_named() {
                    continue;
                }
                let ik = inner.kind();
                let ik = ik.as_ref();
                if (ik == "qualified_name" || ik == "namespace_name") && prefix.is_none() {
                    prefix = Some(inner.text().into_owned().trim_start_matches('\\').to_string());
                } else if ik == "namespace_use_group_clause" || ik == "namespace_use_clause" {
                    let text = inner.text().into_owned();
                    let (item, alias) = match text.split_once(" as ") {
                        Some((p, a)) => (p.trim().to_string(), Some(a.trim().to_string())),
                        None => (text.trim().to_string(), None),
                    };
                    let item = item.trim_start_matches('\\').to_string();
                    let full = match &prefix {
                        Some(p) if !p.is_empty() => format!("{}\\{}", p, item),
                        _ => item,
                    };
                    if full.is_empty() {
                        continue;
                    }
                    out.push(RawImport {
                        spec: full.replace('\\', "/"),
                        kind: ImportKind::Use,
                        line,
                        statement: stmt.clone(),
                        local_name: alias,
                        raw_path: Some(full),
                    });
                }
            }
        }
    }
}

fn consume_php_require<'a, D: Doc>(node: &Node<'a, D>, out: &mut Vec<RawImport>) {
    let line = (node.start_pos().line() + 1) as u32;
    let stmt = node.text().into_owned();
    // The argument is a string child — but `require('foo.php')` wraps the
    // string in a `parenthesized_expression`, so we may need to descend one
    // level. We bail on anything that's not a literal (concatenation,
    // variable, etc.) since dynamic paths can't be resolved statically.
    for sub in node.children() {
        if !sub.is_named() {
            continue;
        }
        if let Some(path) = _php_extract_string_arg(&sub) {
            if path.is_empty() {
                continue;
            }
            // Skip dynamic paths: __DIR__ concatenation and `$var`
            // interpolation never produce a static target.
            if path.contains("__DIR__") || path.contains('$') {
                continue;
            }
            let spec = if path.starts_with("./") || path.starts_with("../") {
                path.clone()
            } else {
                format!("./{}", path)
            };
            out.push(RawImport {
                spec,
                kind: ImportKind::Bare,
                line,
                statement: stmt.clone(),
                local_name: None,
                raw_path: Some(path),
            });
            break;
        }
    }
}

/// Pull the literal path out of a require/include argument, descending
/// through one level of `parenthesized_expression` if present. Returns
/// None for any non-literal form (concatenation, variable, function call).
fn _php_extract_string_arg<'a, D: Doc>(node: &Node<'a, D>) -> Option<String> {
    let k = node.kind();
    let k = k.as_ref();
    if k == "string" || k == "encapsed_string" {
        let raw = node.text().into_owned();
        return Some(
            raw.trim()
                .trim_matches('\'')
                .trim_matches('"')
                .to_string(),
        );
    }
    if k == "parenthesized_expression" {
        for inner in node.children() {
            if !inner.is_named() {
                continue;
            }
            if let Some(s) = _php_extract_string_arg(&inner) {
                return Some(s);
            }
        }
    }
    None
}

// ---- Ruby ----

fn extract_ruby(src: &str) -> Vec<RawImport> {
    let lang = SupportLang::Ruby;
    let ast = lang.ast_grep(src);
    let root = ast.root();
    let mut out = Vec::new();
    _walk_ruby(&root, &mut out);
    out
}

fn _walk_ruby<'a, D: Doc>(node: &Node<'a, D>, out: &mut Vec<RawImport>) {
    for c in node.children() {
        if !c.is_named() {
            continue;
        }
        if c.kind().as_ref() == "call" {
            consume_ruby_call(&c, out);
            continue;
        }
        // `require_relative` calls can appear inside any block (class body,
        // method body, if/unless, begin/rescue). Walk the full tree —
        // `consume_ruby_call` filters out non-import calls cheaply.
        _walk_ruby(&c, out);
    }
}

fn consume_ruby_call<'a, D: Doc>(node: &Node<'a, D>, out: &mut Vec<RawImport>) {
    // tree-sitter-ruby exposes `method` and `arguments` fields on `call`.
    let method = match node.field("method") {
        Some(m) => m.text().into_owned(),
        None => return,
    };
    if !matches!(
        method.as_str(),
        "require" | "require_relative" | "load" | "autoload"
    ) {
        return;
    }
    let args = match node.field("arguments") {
        Some(a) => a,
        None => return,
    };
    // Collect named argument children in order. `autoload(const, file)` puts
    // the path in the SECOND positional slot; the rest put it first. Picking
    // by position handles `autoload "Foo", "path"` (where the const name is a
    // String) — naive "first string wins" would grab "Foo".
    let positional: Vec<_> = args.children().filter(|a| a.is_named()).collect();
    let path_arg = if method == "autoload" {
        positional.get(1)
    } else {
        positional.first()
    };
    let Some(path_arg) = path_arg else { return };
    if path_arg.kind().as_ref() != "string" {
        // Non-literal argument (variable, method call, interpolated string) —
        // can't resolve statically.
        return;
    }
    let raw = path_arg.text().into_owned();
    let path = raw
        .trim()
        .trim_matches('\'')
        .trim_matches('"')
        .to_string();
    if path.is_empty() {
        return;
    }
    let line = (node.start_pos().line() + 1) as u32;
    let stmt = node.text().into_owned();

    if method == "require_relative" {
        // Ruby convention is to omit the `.rb` extension; append it so the
        // resolver's `target.is_file()` check finds the actual file.
        let path_with_ext = if path.ends_with(".rb") {
            path.clone()
        } else {
            format!("{}.rb", path)
        };
        let spec = if path_with_ext.starts_with("./") || path_with_ext.starts_with("../") {
            path_with_ext.clone()
        } else {
            format!("./{}", path_with_ext)
        };
        out.push(RawImport {
            spec,
            kind: ImportKind::Bare,
            line,
            statement: stmt,
            local_name: None,
            raw_path: Some(path),
        });
    } else {
        // `require 'gem'`, `load`, `autoload` — these target $LOAD_PATH or
        // gems, which live outside the repo. Emit a non-resolvable spec so
        // they show up in the external list with the original path.
        out.push(RawImport {
            spec: path.clone(),
            kind: ImportKind::Bare,
            line,
            statement: stmt,
            local_name: None,
            raw_path: Some(path),
        });
    }
}

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

    #[test]
    fn strip_quotes_handles_lone_quote_without_panic() {
        // Regression: a single `"` or `'` satisfies both starts_with and
        // ends_with against itself; without a length guard the slice
        // `t[1..t.len()-1]` would compute `1..0` and panic at runtime.
        // Real Scala/TS sources can produce lone-quote tokens when an
        // import path is malformed or a tree-sitter pass skips a child.
        assert_eq!(strip_quotes("\""), "\"");
        assert_eq!(strip_quotes("'"), "'");
        // Whitespace-padded lone quotes still hit the same code path.
        assert_eq!(strip_quotes("  \"  "), "\"");
    }

    #[test]
    fn strip_quotes_strips_paired_quotes() {
        assert_eq!(strip_quotes("\"hello\""), "hello");
        assert_eq!(strip_quotes("'world'"), "world");
        assert_eq!(strip_quotes("  \"x\"  "), "x");
    }

    #[test]
    fn strip_quotes_leaves_unquoted_unchanged() {
        assert_eq!(strip_quotes("foo"), "foo");
        assert_eq!(strip_quotes(""), "");
        // Mismatched delimiters: only `"x'` doesn't qualify as paired.
        assert_eq!(strip_quotes("\"x'"), "\"x'");
    }
}