sinter-extract 0.53.0

Language-agnostic tree-sitter extraction: languages are data, the engine never forks
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
use tree_sitter::Language;

/// A language is data: a grammar, a capture query, and comment node kinds.
/// The engine consumes only this struct — adding a language adds a row here
/// and a `.scm` file, never engine code.
///
/// Query capture contract (the universal primitives):
/// - `@def.<kind>`   whole definition node; `<kind>` must parse via
///   `SymbolKind::from_str_opt`. A definition also scopes what it contains.
/// - `@name`         the definition's (or scope's) name node, same match.
/// - `@qualifier`    optional extra scope prefix from the same match
///   (e.g. a Go method receiver type).
/// - `@scope`        a node that scopes names but is not itself a symbol
///   (e.g. a Rust `impl` block); pairs with `@name`.
/// - `@ref.<rel>`    a reference site; `<rel>` in {call, use} maps to the
///   relation an eventual binding would carry.
/// - `@import`       an imported path; quotes are stripped.
/// - `@import.module` + `@import.name` — from-style imports
///   (`from util import helper`, `import { helper } from "./util"`): the
///   engine joins them with the language's first path separator so the
///   import binds the item, not just the module.
/// - `@import.alias`   local rebinding (`as` clauses, Go dot imports).
/// - `@import.star`    glob semantics: with `@import.module` (Python `*`)
///   or alongside a plain `@import` (bash `source`), every top-level name
///   of the module binds.
/// - `@local` (+ `@local.type`) — shadowing bindings, optionally typed.
/// - `@embed`          embedded/promoted type members (Go).
/// - `@trait` + `@trait.impl` — an impl block (`@trait.impl`) naming the
///   trait it implements (`@trait`): dynamic-dispatch pairing input.
/// - `@doc`            a node whose text IS the definition's doc (Python
///   docstrings): attached to the smallest containing definition,
///   overriding any sibling-comment doc.
///
/// Standard tree-sitter text predicates (`#eq?`, `#any-of?`, ...) are
/// evaluated by the tree-sitter crate itself and may be used freely
/// (bash isolates `source` from ordinary commands this way).
/// A package manifest declares the mapping between a package's *name*
/// (what imports say) and its *directory* (what module paths say) — a
/// naming root the file tree alone cannot reveal. Reading it is
/// evidence, exactly like reading an import statement. Pure data; the
/// engine never branches on language.
pub struct ManifestSpec {
    /// Manifest file basename ("Cargo.toml", "go.mod").
    pub filename: &'static str,
    /// Key whose value is the package name ("name" for `name = "x"`,
    /// "module" for `module x`).
    pub name_key: &'static str,
    /// Path-head aliases meaning "this package's root" ("crate").
    pub self_names: &'static [&'static str],
    /// Normalizes the declared name to reference form (dashes to
    /// underscores for Rust).
    pub normalize: fn(&str) -> String,
}

/// A discovered package root: files under `dir` belong to package `name`
/// for language `language`.
#[derive(Debug, Clone)]
pub struct ModuleRoot {
    pub name: String,
    /// Repo-relative directory of the manifest ("" for repo root).
    pub dir: String,
    pub language: &'static str,
}

/// Parse one candidate file into a module root, if its basename matches
/// a language's manifest spec. `rel_path` is repo-relative.
pub fn manifest_root(rel_path: &str, content: &str) -> Option<ModuleRoot> {
    let base = rel_path.rsplit('/').next()?;
    let spec = LANGUAGES
        .iter()
        .find(|l| l.manifest.is_some_and(|m| m.filename == base))?;
    let m = spec.manifest?;
    let name = content.lines().find_map(|line| {
        let rest = line.trim().strip_prefix(m.name_key)?;
        let rest = rest.trim_start();
        let rest = rest.strip_prefix('=').unwrap_or(rest).trim();
        let name = rest.trim_matches('"').trim();
        (!name.is_empty() && !name.contains(' ')).then(|| name.to_string())
    })?;
    let dir = rel_path
        .rsplit_once('/')
        .map(|(d, _)| d.to_string())
        .unwrap_or_default();
    Some(ModuleRoot {
        name: (m.normalize)(&name),
        dir,
        language: spec.name,
    })
}

/// A secondary grammar for languages whose spec splits container and
/// content parses (tree-sitter markdown's block/inline split). The engine
/// re-parses the byte ranges of the named container nodes with this
/// grammar and runs its query through the same capture contract; spans
/// stay file-absolute via included-range parsing. Pure data.
pub struct InlineSpec {
    pub grammar: fn() -> Language,
    pub query_source: &'static str,
    /// Node kinds in the primary tree whose ranges the inline grammar
    /// parses.
    pub container_kinds: &'static [&'static str],
}

pub struct LanguageSpec {
    pub name: &'static str,
    pub extensions: &'static [&'static str],
    pub grammar: fn() -> Language,
    pub query_source: &'static str,
    pub comment_kinds: &'static [&'static str],
    /// Maps a repo-relative file path to its module/package path segments —
    /// what import statements are matched against. Pure data transform;
    /// the resolver stays language-blind.
    pub module_path: fn(&str) -> Vec<String>,
    /// Splits an import/reference path into segments (`::` vs `/` vs `.`).
    pub path_separators: &'static [&'static str],
    /// Turns a raw import/reference path into absolute module segments,
    /// resolving language-relative forms (`super::`, leading dots, `./`)
    /// against the path's own file. Data transform; resolver stays blind.
    pub absolutize: fn(path: &str, file: &str) -> Vec<String>,
    /// Receiver keywords (`self`, `this`): a qualified reference through one
    /// binds within the reference's enclosing type.
    pub receivers: &'static [&'static str],
    /// Node kinds the doc-comment walk may step over (max 2) between a
    /// definition and its doc — e.g. Unreal's `UCLASS(...)` line, which
    /// parses as an expression_statement between comment and class.
    pub doc_skip_kinds: &'static [&'static str],
    /// Package manifest shape, when the language has one that names
    /// module roots (see ManifestSpec).
    pub manifest: Option<&'static ManifestSpec>,
    /// Secondary inline grammar applied to designated container-node
    /// ranges, whose captures merge into the file's facts (markdown's
    /// block/inline split).
    pub inline: Option<&'static InlineSpec>,
    /// References in this language are document paths naming corpus
    /// files (`[text](docs/guide.md#setup)`), not symbol paths: the
    /// resolver binds them by exact file path — with or without the
    /// language's extensions, `#fragment` to the target file's unique
    /// def whose name slugifies to the fragment — and never through the
    /// symbol tiers. Evidence or nothing: a dead link stays unresolved.
    pub file_refs: bool,
    /// Interface satisfaction is implicit (Go): no syntax names the
    /// interface at the implementing type, so the resolver runs a
    /// structural method-set pass instead of `@trait`/`@trait.impl`
    /// pairing. Pure data; the engine never branches on language name.
    pub implicit_interfaces: bool,
}

fn rust_normalize(name: &str) -> String {
    name.replace('-', "_")
}

static RUST_MANIFEST: ManifestSpec = ManifestSpec {
    filename: "Cargo.toml",
    name_key: "name",
    self_names: &["crate"],
    normalize: rust_normalize,
};

fn identity_normalize(name: &str) -> String {
    name.to_string()
}

/// `module example.com/proj` — the declared module path anchors full-path
/// imports to the repo directory holding go.mod. No self-alias: Go import
/// paths are always absolute.
static GO_MANIFEST: ManifestSpec = ManifestSpec {
    filename: "go.mod",
    name_key: "module",
    self_names: &[],
    normalize: identity_normalize,
};

fn split_all(path: &str, separators: &[&str]) -> Vec<String> {
    let mut segments = vec![path.to_string()];
    for sep in separators {
        segments = segments
            .iter()
            .flat_map(|s| s.split(sep).map(str::to_string))
            .collect();
    }
    segments.into_iter().filter(|s| !s.is_empty()).collect()
}

fn dirname_segments(file: &str) -> Vec<String> {
    let mut segments: Vec<String> = file.split('/').map(str::to_string).collect();
    segments.pop();
    segments
}

/// `crate::x` stays absolute; `super::x`/`self::x` resolve against the
/// file's own module path.
fn rust_absolutize(path: &str, file: &str) -> Vec<String> {
    let mut module = rust_module_path(file);
    let mut rest = path;
    if let Some(r) = rest.strip_prefix("self::") {
        rest = r;
    } else {
        while let Some(r) = rest.strip_prefix("super::") {
            module.pop();
            rest = r;
        }
        if rest.len() == path.len() {
            if path.starts_with("crate::") {
                return split_all(path, &["::", "."]);
            }
            // Bare paths are relative to the file's own module
            // (`internal::helper` inside lib.rs means crate::internal::...).
            module.extend(split_all(path, &["::", "."]));
            return module;
        }
    }
    module.extend(split_all(rest, &["::", "."]));
    module
}

fn go_absolutize(path: &str, _file: &str) -> Vec<String> {
    split_all(path, &["/", "."])
}

/// `acme/core/v1/action_event.proto` -> ["acme","core","v1","action_event"]:
/// the file extension must not become a module segment, or the import key
/// never suffix-matches the file's own module path and same-package bare
/// references stay unresolved.
fn proto_absolutize(path: &str, _file: &str) -> Vec<String> {
    split_all(path.strip_suffix(".proto").unwrap_or(path), &["/", "."])
}

/// Leading dots are package-relative: one dot is the file's own package,
/// each further dot one package up.
fn python_absolutize(path: &str, file: &str) -> Vec<String> {
    let dots = path.len() - path.trim_start_matches('.').len();
    if dots == 0 {
        return split_all(path, &["."]);
    }
    let mut base = dirname_segments(file);
    for _ in 1..dots {
        base.pop();
    }
    base.extend(split_all(&path[dots..], &["."]));
    base
}

/// `./x` and `../x` resolve against the file's directory.
fn typescript_absolutize(path: &str, file: &str) -> Vec<String> {
    if !path.starts_with('.') {
        return split_all(path, &["/", "."]);
    }
    let mut base = dirname_segments(file);
    let mut rest = path;
    // Leading `/` is the repo-root convention (GitHub-style), not a
    // relative segment: resolve against the root, not the linking file.
    if let Some(r) = rest.strip_prefix('/') {
        base.clear();
        rest = r;
    }
    loop {
        if let Some(r) = rest.strip_prefix("./") {
            rest = r;
        } else if let Some(r) = rest.strip_prefix("../") {
            base.pop();
            rest = r;
        } else {
            break;
        }
    }
    base.extend(split_all(rest, &["/"]));
    base
}

fn rust_grammar() -> Language {
    tree_sitter_rust::LANGUAGE.into()
}

fn go_grammar() -> Language {
    tree_sitter_go::LANGUAGE.into()
}

/// `src/util.rs` -> ["crate", "util"]; `src/foo/mod.rs` -> ["crate", "foo"].
/// Single-crate view by design: the resolver's manifest-aware `key_of`
/// replaces the "crate" head with the declared package name in workspaces.
fn rust_module_path(file: &str) -> Vec<String> {
    let trimmed = file.strip_suffix(".rs").unwrap_or(file);
    let after_src = trimmed.rsplit_once("src/").map_or(trimmed, |(_, r)| r);
    let mut segments = vec!["crate".to_string()];
    for seg in after_src.split('/') {
        if !matches!(seg, "lib" | "main" | "mod" | "") {
            segments.push(seg.to_string());
        }
    }
    segments
}

/// `pkg/util/util.go` -> ["pkg", "util"]; root files -> [].
fn go_module_path(file: &str) -> Vec<String> {
    let mut segments: Vec<String> = file.split('/').map(str::to_string).collect();
    segments.pop(); // file name; Go packages are directories
    segments
}

fn python_grammar() -> Language {
    tree_sitter_python::LANGUAGE.into()
}

fn bash_grammar() -> Language {
    tree_sitter_bash::LANGUAGE.into()
}

/// Bash has no module system: a file is its path. `lib/util.sh` ->
/// ["lib", "util"].
fn bash_module_path(file: &str) -> Vec<String> {
    let trimmed = file
        .strip_suffix(".sh")
        .or_else(|| file.strip_suffix(".bash"))
        .unwrap_or(file);
    trimmed
        .split('/')
        .filter(|s| !s.is_empty())
        .map(str::to_string)
        .collect()
}

/// `source` paths: strip `$(dirname "$0")/` and `./` style prefixes and
/// resolve against the sourcing file's directory; bare paths pass through.
fn bash_absolutize(path: &str, file: &str) -> Vec<String> {
    let trimmed = path.trim();
    let dir_relative = [
        "$(dirname \"$0\")/",
        "$(dirname $0)/",
        "${BASH_SOURCE%/*}/",
        "./",
    ]
    .iter()
    .find_map(|p| trimmed.strip_prefix(p));
    let stripped = |s: &str| {
        s.strip_suffix(".sh")
            .or_else(|| s.strip_suffix(".bash"))
            .unwrap_or(s)
            .to_string()
    };
    match dir_relative {
        Some(rest) => {
            let mut base = dirname_segments(file);
            base.extend(rest.split('/').filter(|s| !s.is_empty()).map(stripped));
            base
        }
        None => trimmed
            .split('/')
            .filter(|s| !s.is_empty() && *s != ".")
            .map(stripped)
            .collect(),
    }
}

fn cpp_grammar() -> Language {
    tree_sitter_cpp::LANGUAGE.into()
}

/// `player/character.h` and `player/character.cpp` share the module
/// ["player", "character"] — header/impl pairs resolve into one another.
fn cpp_module_path(file: &str) -> Vec<String> {
    let trimmed = file.rsplit_once('.').map_or(file, |(stem, _)| stem);
    trimmed
        .split('/')
        .filter(|s| !s.is_empty())
        .map(str::to_string)
        .collect()
}

/// Quoted include paths, extension-stripped, `./` resolved against the
/// including file's directory; `<system>` includes pass through (and stay
/// external unless a matching module exists in the corpus).
fn cpp_absolutize(path: &str, file: &str) -> Vec<String> {
    let trimmed = path.trim().trim_matches(['<', '>']);
    let no_ext = trimmed.rsplit_once('.').map_or(trimmed, |(stem, ext)| {
        if matches!(
            ext,
            "h" | "hh" | "hpp" | "hxx" | "cpp" | "cc" | "cxx" | "inl"
        ) {
            stem
        } else {
            trimmed
        }
    });
    if let Some(rest) = no_ext.strip_prefix("./") {
        let mut base = dirname_segments(file);
        base.extend(
            rest.split('/')
                .filter(|s| !s.is_empty())
                .map(str::to_string),
        );
        return base;
    }
    // Member access (`c.jump`, `this->jump`) must split so the resolver's
    // receiver/typed-local tiers see a prefix (fixture: cpp-header-impl).
    no_ext
        .replace("->", ".")
        .split(['/', ':', '.'])
        .filter(|s| !s.is_empty())
        .map(str::to_string)
        .collect()
}

fn proto_grammar() -> Language {
    tree_sitter_proto::LANGUAGE.into()
}

/// `contracts/payments.proto` -> ["contracts", "payments"]. Generated-stub
/// imports name proto symbols through package paths; module keys are the
/// file path, packages resolve via the same suffix matching as Go.
fn proto_module_path(file: &str) -> Vec<String> {
    let trimmed = file.strip_suffix(".proto").unwrap_or(file);
    trimmed
        .split('/')
        .filter(|s| !s.is_empty())
        .map(str::to_string)
        .collect()
}

fn typescript_grammar() -> Language {
    tree_sitter_typescript::LANGUAGE_TYPESCRIPT.into()
}

/// `pkg/mod.py` -> ["pkg", "mod"]; `pkg/__init__.py` -> ["pkg"].
fn python_module_path(file: &str) -> Vec<String> {
    let trimmed = file.strip_suffix(".py").unwrap_or(file);
    trimmed
        .split('/')
        .filter(|s| !matches!(*s, "__init__" | ""))
        .map(str::to_string)
        .collect()
}

/// `src/util.ts` -> ["src", "util"]; `src/index.ts` -> ["src"].
fn typescript_module_path(file: &str) -> Vec<String> {
    let trimmed = file
        .strip_suffix(".tsx")
        .or_else(|| file.strip_suffix(".ts"))
        .unwrap_or(file);
    trimmed
        .split('/')
        .filter(|s| !matches!(*s, "index" | ""))
        .map(str::to_string)
        .collect()
}

fn javascript_grammar() -> Language {
    tree_sitter_javascript::LANGUAGE.into()
}

fn c_grammar() -> Language {
    tree_sitter_c::LANGUAGE.into()
}

fn java_grammar() -> Language {
    tree_sitter_java::LANGUAGE.into()
}

fn csharp_grammar() -> Language {
    tree_sitter_c_sharp::LANGUAGE.into()
}

/// C# module identity is the file's directory, Go-style: `using Acme.Util;`
/// imports a namespace (never a type), and namespaces conventionally mirror
/// directories. `Acme/Util/TextHelper.cs` -> ["Acme", "Util"], so the using
/// directive's segments suffix-match the directory key. Path-derived only:
/// namespace declarations that diverge from layout are out of scope
/// (stated boundary of the csharp pack, see queries/csharp.scm).
fn csharp_module_path(file: &str) -> Vec<String> {
    let mut segments: Vec<String> = file.split('/').map(str::to_string).collect();
    segments.pop(); // file name; namespaces are directories
    segments
}

fn markdown_grammar() -> Language {
    tree_sitter_md::LANGUAGE.into()
}

fn markdown_inline_grammar() -> Language {
    tree_sitter_md::INLINE_LANGUAGE.into()
}

/// Inline content (`[text](target)`) is a second grammar in tree-sitter's
/// markdown split; the engine parses the block tree's `inline` ranges
/// with it (see InlineSpec).
static MARKDOWN_INLINE: InlineSpec = InlineSpec {
    grammar: markdown_inline_grammar,
    query_source: include_str!("../queries/markdown-inline.scm"),
    container_kinds: &["inline"],
};

/// Link destinations resolve against the linking file's directory —
/// `./x`, `../x`, and bare `x` alike (doc-link convention); the `.md`
/// extension is not a module segment.
fn markdown_absolutize(path: &str, file: &str) -> Vec<String> {
    let mut base = dirname_segments(file);
    let mut rest = path;
    // Leading `/` is the repo-root convention (GitHub-style), not a
    // relative segment: resolve against the root, not the linking file.
    if let Some(r) = rest.strip_prefix('/') {
        base.clear();
        rest = r;
    }
    loop {
        if let Some(r) = rest.strip_prefix("./") {
            rest = r;
        } else if let Some(r) = rest.strip_prefix("../") {
            base.pop();
            rest = r;
        } else {
            break;
        }
    }
    let rest = rest
        .strip_suffix(".md")
        .or_else(|| rest.strip_suffix(".markdown"))
        .unwrap_or(rest);
    base.extend(
        rest.split('/')
            .filter(|s| !s.is_empty())
            .map(str::to_string),
    );
    base
}

/// A document is its path: `docs/team.md` -> ["docs", "team"].
fn markdown_module_path(file: &str) -> Vec<String> {
    let trimmed = file
        .strip_suffix(".md")
        .or_else(|| file.strip_suffix(".markdown"))
        .unwrap_or(file);
    trimmed
        .split('/')
        .filter(|s| !s.is_empty())
        .map(str::to_string)
        .collect()
}

fn sql_grammar() -> Language {
    tree_sitter_sequel::LANGUAGE.into()
}

fn javascript_module_path(file: &str) -> Vec<String> {
    let trimmed = file
        .strip_suffix(".jsx")
        .or_else(|| file.strip_suffix(".mjs"))
        .or_else(|| file.strip_suffix(".cjs"))
        .or_else(|| file.strip_suffix(".js"))
        .unwrap_or(file);
    trimmed
        .split('/')
        .filter(|s| !matches!(*s, "index" | ""))
        .map(str::to_string)
        .collect()
}

fn javascript_absolutize(path: &str, file: &str) -> Vec<String> {
    typescript_absolutize(path, file)
}

fn c_module_path(file: &str) -> Vec<String> {
    cpp_module_path(file)
}

fn c_absolutize(path: &str, file: &str) -> Vec<String> {
    cpp_absolutize(path, file)
}

/// Java packages are directories: `com/acme/util/Text.java` ->
/// ["com", "acme", "util"], matching a path-aligned `package com.acme.util;`.
/// The class contributes its own segment as a definition, so the FQN
/// `com.acme.util.Text` resolves as module + top-level def.
// ponytail: assumes package declarations align with directories; parse the
// package_declaration into module identity if misaligned repos matter.
fn java_module_path(file: &str) -> Vec<String> {
    dirname_segments(file)
        .into_iter()
        .filter(|s| !s.is_empty())
        .collect()
}

/// Java qualified references arrive as full invocation text
/// (`Text.trim(s)`): everything from the first `(` on is arguments, not
/// path. Imports are already-absolute dotted FQNs.
fn java_absolutize(path: &str, _file: &str) -> Vec<String> {
    let head = path.split('(').next().unwrap_or(path);
    head.split('.')
        .map(str::trim)
        .filter(|s| !s.is_empty())
        .map(str::to_string)
        .collect()
}

/// All .sql files in a directory share one namespace (a database schema
/// has no per-file scoping), so the module key is the directory alone:
/// `db/schema.sql` and `db/queries.sql` both map to ["db"].
fn sql_module_path(file: &str) -> Vec<String> {
    let dir = file.rsplit_once('/').map_or("", |(dir, _)| dir);
    dir.split('/')
        .filter(|s| !s.is_empty())
        .map(str::to_string)
        .collect()
}

/// Dotted absolute imports (C#/SQL style): already-absolute FQNs split
/// on dots; no relative forms exist.
fn dotted_absolutize(path: &str, _file: &str) -> Vec<String> {
    path.trim()
        .split('.')
        .filter(|s| !s.is_empty())
        .map(str::to_string)
        .collect()
}

pub static LANGUAGES: &[LanguageSpec] = &[
    LanguageSpec {
        name: "rust",
        extensions: &["rs"],
        grammar: rust_grammar,
        query_source: include_str!("../queries/rust.scm"),
        comment_kinds: &["line_comment", "block_comment"],
        module_path: rust_module_path,
        path_separators: &["::", "."],
        absolutize: rust_absolutize,
        receivers: &["self", "Self"],
        doc_skip_kinds: &[],
        manifest: Some(&RUST_MANIFEST),
        inline: None,
        file_refs: false,
        implicit_interfaces: false,
    },
    LanguageSpec {
        name: "go",
        extensions: &["go"],
        grammar: go_grammar,
        query_source: include_str!("../queries/go.scm"),
        comment_kinds: &["comment"],
        module_path: go_module_path,
        path_separators: &["/", "."],
        absolutize: go_absolutize,
        receivers: &[],
        doc_skip_kinds: &[],
        manifest: Some(&GO_MANIFEST),
        inline: None,
        file_refs: false,
        implicit_interfaces: true,
    },
    LanguageSpec {
        name: "python",
        extensions: &["py"],
        grammar: python_grammar,
        query_source: include_str!("../queries/python.scm"),
        comment_kinds: &["comment"],
        module_path: python_module_path,
        path_separators: &["."],
        absolutize: python_absolutize,
        receivers: &["self", "cls"],
        doc_skip_kinds: &[],
        manifest: None,
        inline: None,
        file_refs: false,
        implicit_interfaces: false,
    },
    LanguageSpec {
        name: "typescript",
        extensions: &["ts", "tsx"],
        grammar: typescript_grammar,
        query_source: include_str!("../queries/typescript.scm"),
        comment_kinds: &["comment"],
        module_path: typescript_module_path,
        path_separators: &["/", "."],
        absolutize: typescript_absolutize,
        receivers: &["this"],
        doc_skip_kinds: &[],
        manifest: None,
        inline: None,
        file_refs: false,
        implicit_interfaces: false,
    },
    LanguageSpec {
        name: "bash",
        extensions: &["sh", "bash"],
        grammar: bash_grammar,
        query_source: include_str!("../queries/bash.scm"),
        comment_kinds: &["comment"],
        module_path: bash_module_path,
        path_separators: &["/"],
        absolutize: bash_absolutize,
        receivers: &[],
        doc_skip_kinds: &[],
        manifest: None,
        inline: None,
        file_refs: false,
        implicit_interfaces: false,
    },
    LanguageSpec {
        name: "proto",
        extensions: &["proto"],
        grammar: proto_grammar,
        query_source: include_str!("../queries/proto.scm"),
        comment_kinds: &["comment"],
        module_path: proto_module_path,
        path_separators: &["/", "."],
        absolutize: proto_absolutize,
        receivers: &[],
        doc_skip_kinds: &[],
        manifest: None,
        inline: None,
        file_refs: false,
        implicit_interfaces: false,
    },
    LanguageSpec {
        name: "cpp",
        extensions: &["cpp", "cc", "cxx", "hpp", "hh", "hxx", "h"],
        grammar: cpp_grammar,
        query_source: include_str!("../queries/cpp.scm"),
        comment_kinds: &["comment"],
        module_path: cpp_module_path,
        path_separators: &["/", "::"],
        absolutize: cpp_absolutize,
        receivers: &["this"],
        doc_skip_kinds: &["expression_statement"],
        manifest: None,
        inline: None,
        file_refs: false,
        implicit_interfaces: false,
    },
    LanguageSpec {
        name: "javascript",
        extensions: &["js", "jsx", "mjs", "cjs"],
        grammar: javascript_grammar,
        query_source: include_str!("../queries/javascript.scm"),
        comment_kinds: &["comment"],
        module_path: javascript_module_path,
        path_separators: &["/", "."],
        absolutize: javascript_absolutize,
        receivers: &["this"],
        doc_skip_kinds: &[],
        manifest: None,
        inline: None,
        file_refs: false,
        implicit_interfaces: false,
    },
    LanguageSpec {
        name: "c",
        extensions: &["c"],
        grammar: c_grammar,
        query_source: include_str!("../queries/c.scm"),
        comment_kinds: &["comment"],
        module_path: c_module_path,
        path_separators: &["/"],
        absolutize: c_absolutize,
        receivers: &[],
        doc_skip_kinds: &[],
        manifest: None,
        inline: None,
        file_refs: false,
        implicit_interfaces: false,
    },
    LanguageSpec {
        name: "java",
        extensions: &["java"],
        grammar: java_grammar,
        query_source: include_str!("../queries/java.scm"),
        comment_kinds: &["line_comment", "block_comment"],
        module_path: java_module_path,
        path_separators: &["."],
        absolutize: java_absolutize,
        receivers: &["this"],
        doc_skip_kinds: &[],
        manifest: None,
        inline: None,
        file_refs: false,
        implicit_interfaces: false,
    },
    LanguageSpec {
        name: "csharp",
        extensions: &["cs"],
        grammar: csharp_grammar,
        query_source: include_str!("../queries/csharp.scm"),
        comment_kinds: &["comment"],
        module_path: csharp_module_path,
        path_separators: &["."],
        absolutize: dotted_absolutize,
        receivers: &["this", "base"],
        doc_skip_kinds: &[],
        manifest: None,
        inline: None,
        file_refs: false,
        implicit_interfaces: false,
    },
    LanguageSpec {
        name: "sql",
        extensions: &["sql"],
        grammar: sql_grammar,
        query_source: include_str!("../queries/sql.scm"),
        comment_kinds: &["comment", "marginalia"],
        module_path: sql_module_path,
        path_separators: &["."],
        absolutize: dotted_absolutize,
        receivers: &[],
        doc_skip_kinds: &[],
        manifest: None,
        inline: None,
        file_refs: false,
        implicit_interfaces: false,
    },
    LanguageSpec {
        name: "markdown",
        extensions: &["md", "markdown"],
        grammar: markdown_grammar,
        query_source: include_str!("../queries/markdown.scm"),
        comment_kinds: &[],
        module_path: markdown_module_path,
        path_separators: &["/"],
        absolutize: markdown_absolutize,
        receivers: &[],
        doc_skip_kinds: &[],
        manifest: None,
        inline: Some(&MARKDOWN_INLINE),
        file_refs: true,
        implicit_interfaces: false,
    },
];

/// Spec for a file, by extension.
pub fn spec_for_path(path: &str) -> Option<&'static LanguageSpec> {
    let ext = path.rsplit('.').next()?;
    LANGUAGES.iter().find(|spec| spec.extensions.contains(&ext))
}