karet-treesitter 0.7.1

Shared tree-sitter parse host (incremental parsing, tree cache, query running) for karet.
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
//! The compile-time registry of bundled tree-sitter grammars.
//!
//! Each entry is wired by a `lang-*` cargo feature, so [`all`] returns exactly the
//! grammars compiled in. [`LanguageId`] values are stable and must **never** be
//! renumbered once shipped.

use std::sync::OnceLock;

use crate::LanguageId;

mod outlines;
pub(crate) use outlines::outline_query;
mod programming;
mod shells;
mod structured;
mod web;

#[cfg(feature = "lang-rust")]
const RUST_SEMANTIC: &str = r#"
(function_item (block) @semantic.body) @semantic.scope
(struct_item (field_declaration_list) @semantic.body) @semantic.scope
(enum_item (enum_variant_list) @semantic.body) @semantic.scope
(union_item (field_declaration_list) @semantic.body) @semantic.scope
(trait_item (declaration_list) @semantic.body) @semantic.scope
(impl_item (declaration_list) @semantic.body) @semantic.scope
(mod_item (declaration_list) @semantic.body) @semantic.scope
"#;

#[cfg(feature = "lang-python")]
const PYTHON_SEMANTIC: &str = r#"
(class_definition body: (block) @semantic.body) @semantic.scope
(function_definition body: (block) @semantic.body) @semantic.scope
"#;

#[cfg(feature = "lang-javascript")]
const JAVASCRIPT_SEMANTIC: &str = r#"
(class_declaration body: (class_body) @semantic.body) @semantic.scope
(class body: (class_body) @semantic.body) @semantic.scope
(function_declaration body: (statement_block) @semantic.body) @semantic.scope
(generator_function_declaration body: (statement_block) @semantic.body) @semantic.scope
(method_definition body: (statement_block) @semantic.body) @semantic.scope
"#;

#[cfg(feature = "lang-typescript")]
const TYPESCRIPT_SEMANTIC: &str = r#"
(class_declaration body: (class_body) @semantic.body) @semantic.scope
(abstract_class_declaration body: (class_body) @semantic.body) @semantic.scope
(function_declaration body: (statement_block) @semantic.body) @semantic.scope
(generator_function_declaration body: (statement_block) @semantic.body) @semantic.scope
(method_definition body: (statement_block) @semantic.body) @semantic.scope
(interface_declaration body: (interface_body) @semantic.body) @semantic.scope
"#;

#[cfg(feature = "lang-json")]
const JSON_SEMANTIC: &str = r#"
(pair key: (string) @semantic.header value: [(object) (array)] @semantic.body) @semantic.scope
"#;

#[cfg(feature = "lang-go")]
const GO_SEMANTIC: &str = r#"
(function_declaration body: (block) @semantic.body) @semantic.scope
(method_declaration body: (block) @semantic.body) @semantic.scope
(type_declaration (type_spec type: [(struct_type) (interface_type)] @semantic.body)) @semantic.scope
"#;

#[cfg(feature = "lang-c")]
const C_SEMANTIC: &str = r#"
(function_definition body: (compound_statement) @semantic.body) @semantic.scope
(struct_specifier body: (field_declaration_list) @semantic.body) @semantic.scope
(union_specifier body: (field_declaration_list) @semantic.body) @semantic.scope
(enum_specifier body: (enumerator_list) @semantic.body) @semantic.scope
"#;

#[cfg(feature = "lang-cpp")]
const CPP_SEMANTIC: &str = r#"
(function_definition body: (compound_statement) @semantic.body) @semantic.scope
(class_specifier body: (field_declaration_list) @semantic.body) @semantic.scope
(struct_specifier body: (field_declaration_list) @semantic.body) @semantic.scope
(namespace_definition body: (declaration_list) @semantic.body) @semantic.scope
(enum_specifier body: (enumerator_list) @semantic.body) @semantic.scope
"#;

#[cfg(feature = "lang-csharp")]
const CSHARP_SEMANTIC: &str = r#"
(namespace_declaration body: (declaration_list) @semantic.body) @semantic.scope
(class_declaration body: (declaration_list) @semantic.body) @semantic.scope
(interface_declaration body: (declaration_list) @semantic.body) @semantic.scope
(struct_declaration body: (declaration_list) @semantic.body) @semantic.scope
(record_declaration body: (declaration_list) @semantic.body) @semantic.scope
(method_declaration body: (block) @semantic.body) @semantic.scope
(constructor_declaration body: (block) @semantic.body) @semantic.scope
"#;

#[cfg(feature = "lang-java")]
const JAVA_SEMANTIC: &str = r#"
(class_declaration body: (class_body) @semantic.body) @semantic.scope
(interface_declaration body: (interface_body) @semantic.body) @semantic.scope
(enum_declaration body: (enum_body) @semantic.body) @semantic.scope
(record_declaration body: (class_body) @semantic.body) @semantic.scope
(method_declaration body: (block) @semantic.body) @semantic.scope
(constructor_declaration body: (constructor_body) @semantic.body) @semantic.scope
"#;

#[cfg(feature = "lang-ruby")]
const RUBY_SEMANTIC: &str = r#"
(class) @semantic.scope
(singleton_class) @semantic.scope
(module) @semantic.scope
(method) @semantic.scope
(singleton_method) @semantic.scope
"#;

#[cfg(feature = "lang-php")]
const PHP_SEMANTIC: &str = r#"
(namespace_definition body: (compound_statement) @semantic.body) @semantic.scope
(class_declaration body: (declaration_list) @semantic.body) @semantic.scope
(interface_declaration body: (declaration_list) @semantic.body) @semantic.scope
(trait_declaration body: (declaration_list) @semantic.body) @semantic.scope
(function_definition body: (compound_statement) @semantic.body) @semantic.scope
(method_declaration body: (compound_statement) @semantic.body) @semantic.scope
"#;

#[cfg(feature = "lang-bash")]
const BASH_SEMANTIC: &str = r#"
(function_definition body: (_) @semantic.body) @semantic.scope
(if_statement) @semantic.scope
(for_statement) @semantic.scope
(c_style_for_statement) @semantic.scope
(while_statement) @semantic.scope
(case_statement) @semantic.scope
"#;

#[cfg(feature = "lang-toml")]
const TOML_SEMANTIC: &str = r#"
(table) @semantic.scope
(table_array_element) @semantic.scope
"#;

#[cfg(feature = "lang-html")]
const HTML_SEMANTIC: &str = r#"
(element (start_tag) @semantic.header) @semantic.scope
(script_element (start_tag) @semantic.header) @semantic.scope
(style_element (start_tag) @semantic.header) @semantic.scope
"#;

#[cfg(feature = "lang-css")]
const CSS_SEMANTIC: &str = r#"
(rule_set (selectors) @semantic.header (block) @semantic.body) @semantic.scope
(media_statement (block) @semantic.body) @semantic.scope
(supports_statement (block) @semantic.body) @semantic.scope
(keyframes_statement (keyframe_block_list) @semantic.body) @semantic.scope
"#;

#[cfg(feature = "lang-yaml")]
const YAML_SEMANTIC: &str = r#"
(block_mapping_pair
  key: (_) @semantic.header
  value: (block_node [(block_mapping) (block_sequence)] @semantic.body)) @semantic.scope
"#;

#[cfg(feature = "lang-markdown")]
const MARKDOWN_SEMANTIC: &str = r#"
(atx_heading (atx_h1_marker)) @semantic.heading.1
(atx_heading (atx_h2_marker)) @semantic.heading.2
(atx_heading (atx_h3_marker)) @semantic.heading.3
(atx_heading (atx_h4_marker)) @semantic.heading.4
(atx_heading (atx_h5_marker)) @semantic.heading.5
(atx_heading (atx_h6_marker)) @semantic.heading.6
(setext_heading (setext_h1_underline)) @semantic.heading.1
(setext_heading (setext_h2_underline)) @semantic.heading.2
"#;

pub(crate) fn semantic_query(_lang: LanguageId) -> Option<&'static str> {
    #[cfg(feature = "lang-rust")]
    if _lang == RUST {
        return Some(RUST_SEMANTIC);
    }
    #[cfg(feature = "lang-python")]
    if _lang == PYTHON {
        return Some(PYTHON_SEMANTIC);
    }
    #[cfg(feature = "lang-javascript")]
    if _lang == JAVASCRIPT {
        return Some(JAVASCRIPT_SEMANTIC);
    }
    #[cfg(feature = "lang-typescript")]
    if _lang == TYPESCRIPT || _lang == TSX {
        return Some(TYPESCRIPT_SEMANTIC);
    }
    #[cfg(feature = "lang-json")]
    if _lang == JSON {
        return Some(JSON_SEMANTIC);
    }
    #[cfg(feature = "lang-go")]
    if _lang == GO {
        return Some(GO_SEMANTIC);
    }
    #[cfg(feature = "lang-c")]
    if _lang == C {
        return Some(C_SEMANTIC);
    }
    #[cfg(feature = "lang-cpp")]
    if _lang == CPP {
        return Some(CPP_SEMANTIC);
    }
    #[cfg(feature = "lang-csharp")]
    if _lang == CSHARP {
        return Some(CSHARP_SEMANTIC);
    }
    #[cfg(feature = "lang-java")]
    if _lang == JAVA {
        return Some(JAVA_SEMANTIC);
    }
    #[cfg(feature = "lang-ruby")]
    if _lang == RUBY {
        return Some(RUBY_SEMANTIC);
    }
    #[cfg(feature = "lang-php")]
    if _lang == PHP {
        return Some(PHP_SEMANTIC);
    }
    #[cfg(feature = "lang-bash")]
    if _lang == BASH {
        return Some(BASH_SEMANTIC);
    }
    #[cfg(feature = "lang-toml")]
    if _lang == TOML {
        return Some(TOML_SEMANTIC);
    }
    #[cfg(feature = "lang-html")]
    if _lang == HTML {
        return Some(HTML_SEMANTIC);
    }
    #[cfg(feature = "lang-css")]
    if _lang == CSS {
        return Some(CSS_SEMANTIC);
    }
    #[cfg(feature = "lang-yaml")]
    if _lang == YAML {
        return Some(YAML_SEMANTIC);
    }
    #[cfg(feature = "lang-markdown")]
    if _lang == MARKDOWN {
        return Some(MARKDOWN_SEMANTIC);
    }
    #[cfg(feature = "lang-latex")]
    if _lang == LATEX {
        return Some(LATEX_SEMANTIC);
    }
    None
}

/// Static metadata for one registered grammar.
pub(crate) struct GrammarInfo {
    /// The grammar's stable identifier.
    pub id: LanguageId,
    /// A human-readable display name (e.g. `"Rust"`).
    #[cfg_attr(not(test), allow(dead_code))] // consistency-test reference data
    pub name: &'static str,
    /// File extensions this grammar expects `karet-filetype` to route to it.
    /// Reference data for the cross-registry consistency test ONLY: runtime
    /// path→language routing goes exclusively through `karet-filetype`.
    #[cfg_attr(not(test), allow(dead_code))]
    pub extensions: &'static [&'static str],
    /// Lowercase names by which an injection query — or a markdown code-fence info
    /// string — may refer to this grammar (e.g. `rust`, `rs`). Distinct from
    /// [`extensions`](Self::extensions): a fence says ` ```sh `, not ` ```bash `.
    pub names: &'static [&'static str],
    /// Returns the tree-sitter `Language` for this grammar.
    pub language: fn() -> tree_sitter::Language,
    /// The grammar's highlights query source.
    pub highlights: &'static str,
    /// The grammar's bundled injections query source, if it ships one.
    pub injections: Option<&'static str>,
    /// karet-authored injection patterns appended to [`injections`](Self::injections).
    /// Kept in a separate field so the grammar's own query stays pristine and the
    /// delta we add is auditable in one place.
    pub injections_extra: Option<&'static str>,
}

// Stable language ids — never renumber once shipped. Each is cfg-gated to its
// grammar feature so feature-subset builds don't carry unused constants.
#[cfg(feature = "lang-rust")]
pub(crate) const RUST: LanguageId = LanguageId(1);
#[cfg(feature = "lang-python")]
pub(crate) const PYTHON: LanguageId = LanguageId(2);
#[cfg(feature = "lang-javascript")]
pub(crate) const JAVASCRIPT: LanguageId = LanguageId(3);
#[cfg(feature = "lang-typescript")]
pub(crate) const TYPESCRIPT: LanguageId = LanguageId(4);
#[cfg(feature = "lang-typescript")]
pub(crate) const TSX: LanguageId = LanguageId(5);
#[cfg(feature = "lang-json")]
pub(crate) const JSON: LanguageId = LanguageId(6);
#[cfg(feature = "lang-go")]
pub(crate) const GO: LanguageId = LanguageId(7);
#[cfg(feature = "lang-c")]
pub(crate) const C: LanguageId = LanguageId(8);
#[cfg(feature = "lang-cpp")]
pub(crate) const CPP: LanguageId = LanguageId(9);
#[cfg(feature = "lang-csharp")]
pub(crate) const CSHARP: LanguageId = LanguageId(10);
#[cfg(feature = "lang-java")]
pub(crate) const JAVA: LanguageId = LanguageId(11);
#[cfg(feature = "lang-ruby")]
pub(crate) const RUBY: LanguageId = LanguageId(12);
#[cfg(feature = "lang-php")]
pub(crate) const PHP: LanguageId = LanguageId(13);
#[cfg(feature = "lang-bash")]
pub(crate) const BASH: LanguageId = LanguageId(14);
#[cfg(feature = "lang-toml")]
pub(crate) const TOML: LanguageId = LanguageId(15);
#[cfg(feature = "lang-html")]
pub(crate) const HTML: LanguageId = LanguageId(16);
#[cfg(feature = "lang-css")]
pub(crate) const CSS: LanguageId = LanguageId(17);
#[cfg(feature = "lang-yaml")]
pub(crate) const YAML: LanguageId = LanguageId(18);
#[cfg(feature = "lang-markdown")]
pub(crate) const MARKDOWN: LanguageId = LanguageId(19);
/// tree-sitter-md's companion *inline* grammar. Never resolved from a path — the
/// block grammar reaches it only through an `markdown_inline` injection.
#[cfg(feature = "lang-markdown")]
pub(crate) const MARKDOWN_INLINE: LanguageId = LanguageId(20);
#[cfg(feature = "lang-latex")]
pub(crate) const LATEX: LanguageId = LanguageId(21);
#[cfg(feature = "lang-zig")]
pub(crate) const ZIG: LanguageId = LanguageId(22);
#[cfg(feature = "lang-xml")]
pub(crate) const XML: LanguageId = LanguageId(23);
#[cfg(feature = "lang-svelte")]
pub(crate) const SVELTE: LanguageId = LanguageId(24);
#[cfg(feature = "lang-astro")]
pub(crate) const ASTRO: LanguageId = LanguageId(25);
#[cfg(feature = "lang-vue")]
pub(crate) const VUE: LanguageId = LanguageId(26);
#[cfg(feature = "lang-sql")]
pub(crate) const SQL: LanguageId = LanguageId(27);
#[cfg(feature = "lang-graphql")]
pub(crate) const GRAPHQL: LanguageId = LanguageId(28);
#[cfg(feature = "lang-protobuf")]
pub(crate) const PROTOBUF: LanguageId = LanguageId(29);
#[cfg(feature = "lang-containerfile")]
pub(crate) const CONTAINERFILE: LanguageId = LanguageId(30);
#[cfg(feature = "lang-make")]
pub(crate) const MAKE: LanguageId = LanguageId(31);
#[cfg(feature = "lang-cmake")]
pub(crate) const CMAKE: LanguageId = LanguageId(32);
#[cfg(feature = "lang-rst")]
pub(crate) const RST: LanguageId = LanguageId(33);
#[cfg(feature = "lang-asciidoc")]
pub(crate) const ASCIIDOC: LanguageId = LanguageId(34);
#[cfg(feature = "lang-zsh")]
pub(crate) const ZSH: LanguageId = LanguageId(35);
#[cfg(feature = "lang-fish")]
pub(crate) const FISH: LanguageId = LanguageId(36);
#[cfg(feature = "lang-powershell")]
pub(crate) const POWERSHELL: LanguageId = LanguageId(37);
#[cfg(feature = "lang-batch")]
pub(crate) const BATCH: LanguageId = LanguageId(38);
#[cfg(feature = "lang-graphql")]
const GRAPHQL_HIGHLIGHTS: &str = r#"
(comment) @comment
(string_value) @string
[(int_value) (float_value)] @number
[(boolean_value) (null_value)] @constant.builtin
[(object_type_definition) (interface_type_definition) (enum_type_definition)
 (input_object_type_definition) (union_type_definition) (scalar_type_definition)] @type
(operation_type) @keyword
[(field) (field_definition)] @property
(directive) @attribute
"#;

#[cfg(feature = "lang-protobuf")]
const PROTOBUF_HIGHLIGHTS: &str = r#"
[
  "syntax" "edition" "package" "option" "import" "service" "rpc" "returns"
  "message" "enum" "oneof" "repeated" "reserved" "to"
] @keyword
[(key_type) (type) (message_name) (enum_name) (service_name) (rpc_name)] @type
(string) @string
[(int_lit) (float_lit)] @number
[(true) (false)] @constant.builtin
(comment) @comment
["(" ")" "[" "]" "{" "}"] @punctuation.bracket
"#;

#[cfg(feature = "lang-latex")]
const LATEX_HIGHLIGHTS: &str = r#"
[(line_comment) (block_comment) (comment) (comment_environment)] @comment
(todo) @comment.mark
[(part) (chapter) (section) (subsection) (subsubsection) (paragraph) (subparagraph)] @markup.heading
[(inline_formula) (displayed_equation) (math_environment)] @markup.raw
[(generic_command) (begin) (end) (class_include) (package_include)] @function
[(citation) (label_definition) (label_reference) (label_reference_range)] @variable
[(hyperlink) (curly_group_uri)] @markup.link
[(curly_group_path) (curly_group_path_list) (glob_pattern)] @string
[(operator) (math_delimiter)] @operator
[(subscript) (superscript)] @punctuation.special
"#;

#[cfg(feature = "lang-latex")]
const LATEX_SEMANTIC: &str = r#"
[(part) (chapter) (section) (subsection) (subsubsection)] @semantic.scope
"#;

#[cfg(feature = "lang-rst")]
const RST_HIGHLIGHTS: &str = r#"
(title) @markup.heading
[(emphasis) (strong)] @markup.strong
[(literal) (literal_block)] @markup.raw
[(link) (standalone_hyperlink)] @markup.link
(comment) @comment
"#;

#[cfg(feature = "lang-asciidoc")]
const ASCIIDOC_HIGHLIGHTS: &str = r#"
[(document_title) (title1) (title2) (title3) (title4) (title5)] @markup.heading
[(line_comment) (block_comment)] @comment
(email) @markup.link.url
(block_macro (target) @markup.link)
[(listing_block) (literal_block) (ident_block)] @markup.raw.block
"#;

// tree-sitter-vue-next 0.1 publishes its queries under `queries/vue/`, while
// its Rust binding currently looks only in `queries/`. Keep the small Karet
// query here until the crate exposes those constants correctly.
#[cfg(feature = "lang-vue")]
const VUE_HIGHLIGHTS: &str = r#"
(comment) @comment
(doctype) @keyword
(tag_name) @tag
(attribute_name) @tag.attribute
(directive_name) @tag.attribute
(directive_value) @variable
(attribute_value) @string
(quoted_attribute_value) @string
["<" ">" "</" "/>" "="] @punctuation.delimiter
"#;

#[cfg(feature = "lang-vue")]
const VUE_INJECTIONS: &str = r#"
((style_element
  (start_tag
    (attribute
      (attribute_name) @_attr
      (quoted_attribute_value (attribute_value) @injection.language)))
  (raw_text) @injection.content)
 (#eq? @_attr "lang"))
((script_element
  (start_tag
    (attribute
      (attribute_name) @_attr
      (quoted_attribute_value (attribute_value) @injection.language)))
  (raw_text) @injection.content)
 (#eq? @_attr "lang"))
((interpolation (raw_text) @injection.content)
 (#set! injection.language "typescript"))
"#;

/// karet's own addition to Rust's injections query: a doc comment is markdown.
///
/// tree-sitter-rust ships injections only for macro token trees, so `///` and `//!`
/// bodies would otherwise render as flat comment text. The `doc:` field yields the
/// comment's *content*, with the `///` marker excluded — exactly the markdown source
/// rustdoc sees.
///
/// `injection.combined` is essential rather than an optimization: each `///` line is
/// its own `line_comment` node, so without combining them into a single markdown
/// parse a fenced ` ```rust ` block could never span the lines it always spans. Once
/// combined, markdown's own fence injection recursively lights up the doctest as Rust.
#[cfg(feature = "lang-rust")]
const RUST_DOC_COMMENT_INJECTION: &str = r#"
((line_comment doc: (doc_comment) @injection.content)
 (#set! injection.language "markdown")
 (#set! injection.combined))

((block_comment doc: (doc_comment) @injection.content)
 (#set! injection.language "markdown")
 (#set! injection.combined))
"#;

/// All grammars compiled into this build, in id order.
// The pushes are `#[cfg]`-gated per grammar, which `vec![]` cannot express.
#[allow(clippy::vec_init_then_push)]
pub(crate) fn all() -> &'static [GrammarInfo] {
    static REG: OnceLock<Vec<GrammarInfo>> = OnceLock::new();
    REG.get_or_init(|| {
        // With no `lang-*` feature enabled nothing pushes, and `mut` goes unused.
        #[allow(unused_mut)]
        let mut v: Vec<GrammarInfo> = Vec::new();

        #[cfg(feature = "lang-rust")]
        v.push(GrammarInfo {
            id: RUST,
            name: "Rust",
            extensions: &["rs"],
            names: &["rust", "rs"],
            language: || tree_sitter_rust::LANGUAGE.into(),
            highlights: tree_sitter_rust::HIGHLIGHTS_QUERY,
            injections: Some(tree_sitter_rust::INJECTIONS_QUERY),
            injections_extra: Some(RUST_DOC_COMMENT_INJECTION),
        });
        #[cfg(feature = "lang-python")]
        v.push(GrammarInfo {
            id: PYTHON,
            name: "Python",
            extensions: &["py", "pyi"],
            names: &["python", "py"],
            language: || tree_sitter_python::LANGUAGE.into(),
            highlights: tree_sitter_python::HIGHLIGHTS_QUERY,
            injections: None,
            injections_extra: None,
        });
        #[cfg(feature = "lang-javascript")]
        v.push(GrammarInfo {
            id: JAVASCRIPT,
            name: "JavaScript",
            extensions: &["js", "mjs", "cjs", "jsx"],
            names: &["javascript", "js", "jsx", "mjs", "cjs"],
            language: || tree_sitter_javascript::LANGUAGE.into(),
            highlights: tree_sitter_javascript::HIGHLIGHT_QUERY, // singular
            injections: Some(tree_sitter_javascript::INJECTIONS_QUERY),
            injections_extra: Some(web::JS_GRAPHQL_MARKER_INJECTION),
        });
        #[cfg(feature = "lang-typescript")]
        v.push(GrammarInfo {
            id: TYPESCRIPT,
            name: "TypeScript",
            extensions: &["ts", "mts", "cts"],
            names: &["typescript", "ts"],
            language: || tree_sitter_typescript::LANGUAGE_TYPESCRIPT.into(),
            highlights: tree_sitter_typescript::HIGHLIGHTS_QUERY,
            injections: None,
            injections_extra: Some(web::TS_INJECTION_EXTRA),
        });
        #[cfg(feature = "lang-typescript")]
        v.push(GrammarInfo {
            id: TSX,
            name: "TSX",
            extensions: &["tsx"],
            names: &["tsx"],
            language: || tree_sitter_typescript::LANGUAGE_TSX.into(),
            highlights: tree_sitter_typescript::HIGHLIGHTS_QUERY,
            injections: None,
            injections_extra: Some(web::TS_INJECTION_EXTRA),
        });
        #[cfg(feature = "lang-json")]
        v.push(GrammarInfo {
            id: JSON,
            name: "JSON",
            extensions: &["json", "jsonc"],
            names: &["json", "jsonc"],
            language: || tree_sitter_json::LANGUAGE.into(),
            highlights: tree_sitter_json::HIGHLIGHTS_QUERY,
            injections: None,
            injections_extra: None,
        });
        #[cfg(feature = "lang-go")]
        v.push(GrammarInfo {
            id: GO,
            name: "Go",
            extensions: &["go"],
            names: &["go", "golang"],
            language: || tree_sitter_go::LANGUAGE.into(),
            highlights: tree_sitter_go::HIGHLIGHTS_QUERY,
            injections: None,
            injections_extra: None,
        });
        #[cfg(feature = "lang-c")]
        v.push(GrammarInfo {
            id: C,
            name: "C",
            extensions: &["c", "h"],
            names: &["c"],
            language: || tree_sitter_c::LANGUAGE.into(),
            highlights: tree_sitter_c::HIGHLIGHT_QUERY, // singular
            injections: None,
            injections_extra: None,
        });
        #[cfg(feature = "lang-cpp")]
        v.push(GrammarInfo {
            id: CPP,
            name: "C++",
            extensions: &["cc", "cpp", "cxx", "hpp", "hh", "hxx"],
            names: &["cpp", "c++", "cxx", "cc"],
            language: || tree_sitter_cpp::LANGUAGE.into(),
            highlights: tree_sitter_cpp::HIGHLIGHT_QUERY, // singular
            injections: None,
            injections_extra: None,
        });
        #[cfg(feature = "lang-csharp")]
        v.push(GrammarInfo {
            id: CSHARP,
            name: "C#",
            extensions: &["cs"],
            names: &["csharp", "c_sharp", "c#", "cs"],
            language: || tree_sitter_c_sharp::LANGUAGE.into(),
            highlights: tree_sitter_c_sharp::HIGHLIGHTS_QUERY,
            // tree-sitter-c-sharp declares `INJECTIONS_QUERY` behind a build-script cfg
            // but ships no `queries/injections.scm`, so the constant is gated out.
            injections: None,
            injections_extra: None,
        });
        #[cfg(feature = "lang-java")]
        v.push(GrammarInfo {
            id: JAVA,
            name: "Java",
            extensions: &["java"],
            names: &["java"],
            language: || tree_sitter_java::LANGUAGE.into(),
            highlights: tree_sitter_java::HIGHLIGHTS_QUERY,
            injections: None,
            injections_extra: None,
        });
        #[cfg(feature = "lang-ruby")]
        v.push(GrammarInfo {
            id: RUBY,
            name: "Ruby",
            extensions: &["rb"],
            names: &["ruby", "rb"],
            language: || tree_sitter_ruby::LANGUAGE.into(),
            highlights: tree_sitter_ruby::HIGHLIGHTS_QUERY,
            injections: None,
            injections_extra: None,
        });
        #[cfg(feature = "lang-php")]
        v.push(GrammarInfo {
            id: PHP,
            name: "PHP",
            extensions: &["php"],
            names: &["php"],
            language: || tree_sitter_php::LANGUAGE_PHP.into(),
            highlights: tree_sitter_php::HIGHLIGHTS_QUERY,
            injections: Some(tree_sitter_php::INJECTIONS_QUERY),
            injections_extra: Some(web::PHP_HTML_INJECTION),
        });
        #[cfg(feature = "lang-bash")]
        v.push(GrammarInfo {
            id: BASH,
            name: "Bash",
            extensions: &["sh", "bash"],
            // NOTE: no "zsh" here — the Zsh grammar owns that name; a name must
            // belong to exactly one grammar or resolution depends on table order
            // (the consistency test in detect.rs enforces uniqueness).
            names: &["bash", "sh", "shell", "console"],
            language: || tree_sitter_bash::LANGUAGE.into(),
            highlights: tree_sitter_bash::HIGHLIGHT_QUERY, // singular
            injections: None,
            injections_extra: None,
        });
        #[cfg(feature = "lang-toml")]
        v.push(GrammarInfo {
            id: TOML,
            name: "TOML",
            extensions: &["toml"],
            names: &["toml"],
            language: || tree_sitter_toml_ng::LANGUAGE.into(),
            highlights: tree_sitter_toml_ng::HIGHLIGHTS_QUERY,
            injections: None,
            injections_extra: None,
        });
        #[cfg(feature = "lang-html")]
        v.push(GrammarInfo {
            id: HTML,
            name: "HTML",
            extensions: &["html", "htm"],
            names: &["html", "htm"],
            language: || tree_sitter_html::LANGUAGE.into(),
            highlights: tree_sitter_html::HIGHLIGHTS_QUERY,
            injections: Some(tree_sitter_html::INJECTIONS_QUERY),
            injections_extra: None,
        });
        #[cfg(feature = "lang-css")]
        v.push(GrammarInfo {
            id: CSS,
            name: "CSS",
            extensions: &["css"],
            names: &["css"],
            language: || tree_sitter_css::LANGUAGE.into(),
            highlights: tree_sitter_css::HIGHLIGHTS_QUERY,
            injections: None,
            injections_extra: None,
        });
        #[cfg(feature = "lang-yaml")]
        v.push(GrammarInfo {
            id: YAML,
            name: "YAML",
            extensions: &["yml", "yaml"],
            names: &["yaml", "yml"],
            language: || tree_sitter_yaml::LANGUAGE.into(),
            highlights: tree_sitter_yaml::HIGHLIGHTS_QUERY,
            injections: None,
            injections_extra: None,
        });
        // Markdown is two grammars. The *block* grammar is the entry point (headings,
        // fences, list markers); its injections query hands `(inline)` nodes to the
        // companion inline grammar and each fence's content to the language its info
        // string names — so emphasis, links and embedded code all arrive as layers.
        #[cfg(feature = "lang-markdown")]
        v.push(GrammarInfo {
            id: MARKDOWN,
            name: "Markdown",
            extensions: &["md", "markdown", "mdown", "mkd"],
            names: &["markdown", "md"],
            language: || tree_sitter_md::LANGUAGE.into(),
            highlights: tree_sitter_md::HIGHLIGHT_QUERY_BLOCK,
            injections: Some(tree_sitter_md::INJECTION_QUERY_BLOCK),
            injections_extra: None,
        });
        #[cfg(feature = "lang-latex")]
        v.push(GrammarInfo {
            id: LATEX,
            name: "TeX",
            extensions: &["tex", "sty", "cls"],
            names: &["tex", "latex"],
            language: || codebook_tree_sitter_latex::LANGUAGE.into(),
            highlights: LATEX_HIGHLIGHTS,
            injections: None,
            injections_extra: None,
        });
        #[cfg(feature = "lang-zig")]
        v.push(GrammarInfo {
            id: ZIG,
            name: "Zig",
            extensions: &["zig"],
            names: &["zig"],
            language: || tree_sitter_zig::LANGUAGE.into(),
            highlights: tree_sitter_zig::HIGHLIGHTS_QUERY,
            injections: Some(tree_sitter_zig::INJECTIONS_QUERY),
            injections_extra: None,
        });
        #[cfg(feature = "lang-xml")]
        v.push(GrammarInfo {
            id: XML,
            name: "XML",
            extensions: &["xml", "svg"],
            names: &["xml", "svg"],
            language: || tree_sitter_xml::LANGUAGE_XML.into(),
            highlights: tree_sitter_xml::XML_HIGHLIGHT_QUERY,
            injections: None,
            injections_extra: None,
        });
        #[cfg(feature = "lang-svelte")]
        v.push(GrammarInfo {
            id: SVELTE,
            name: "Svelte",
            extensions: &["svelte"],
            names: &["svelte"],
            language: || tree_sitter_svelte_next::LANGUAGE.into(),
            highlights: tree_sitter_svelte_next::HIGHLIGHTS_QUERY,
            injections: Some(tree_sitter_svelte_next::INJECTIONS_QUERY),
            injections_extra: None,
        });
        #[cfg(feature = "lang-astro")]
        v.push(GrammarInfo {
            id: ASTRO,
            name: "Astro",
            extensions: &["astro"],
            names: &["astro"],
            language: || tree_sitter_astro_next::LANGUAGE.into(),
            highlights: tree_sitter_astro_next::HIGHLIGHTS_QUERY,
            injections: Some(tree_sitter_astro_next::INJECTIONS_QUERY),
            injections_extra: None,
        });
        #[cfg(feature = "lang-vue")]
        v.push(GrammarInfo {
            id: VUE,
            name: "Vue",
            extensions: &["vue"],
            names: &["vue"],
            language: || tree_sitter_vue_next::LANGUAGE.into(),
            highlights: VUE_HIGHLIGHTS,
            injections: Some(VUE_INJECTIONS),
            injections_extra: None,
        });
        #[cfg(feature = "lang-sql")]
        v.push(GrammarInfo {
            id: SQL,
            name: "SQL",
            extensions: &["sql"],
            names: &["sql"],
            language: || tree_sitter_sequel::LANGUAGE.into(),
            highlights: tree_sitter_sequel::HIGHLIGHTS_QUERY,
            injections: None,
            injections_extra: None,
        });
        #[cfg(feature = "lang-graphql")]
        v.push(GrammarInfo {
            id: GRAPHQL,
            name: "GraphQL",
            extensions: &["graphql", "gql", "graphqls"],
            names: &["graphql", "gql"],
            language: || tree_sitter_graphql::LANGUAGE.into(),
            highlights: GRAPHQL_HIGHLIGHTS,
            injections: None,
            injections_extra: None,
        });
        #[cfg(feature = "lang-protobuf")]
        v.push(GrammarInfo {
            id: PROTOBUF,
            name: "Protobuf",
            extensions: &["proto"],
            names: &["protobuf", "proto"],
            language: || tree_sitter_proto::LANGUAGE.into(),
            highlights: PROTOBUF_HIGHLIGHTS,
            injections: None,
            injections_extra: None,
        });
        #[cfg(feature = "lang-containerfile")]
        v.push(GrammarInfo {
            id: CONTAINERFILE,
            name: "Dockerfile",
            extensions: &[],
            names: &["dockerfile", "containerfile", "docker"],
            language: || tree_sitter_containerfile::LANGUAGE.into(),
            highlights: tree_sitter_containerfile::HIGHLIGHTS_QUERY,
            injections: Some(tree_sitter_containerfile::INJECTIONS_QUERY),
            injections_extra: None,
        });
        #[cfg(feature = "lang-make")]
        v.push(GrammarInfo {
            id: MAKE,
            name: "Makefile",
            extensions: &["mk"],
            names: &["make", "makefile"],
            language: || tree_sitter_make::LANGUAGE.into(),
            highlights: tree_sitter_make::HIGHLIGHTS_QUERY,
            injections: None,
            injections_extra: None,
        });
        #[cfg(feature = "lang-cmake")]
        v.push(GrammarInfo {
            id: CMAKE,
            name: "CMake",
            extensions: &["cmake"],
            names: &["cmake"],
            language: || tree_sitter_cmake::LANGUAGE.into(),
            highlights: tree_sitter_cmake::HIGHLIGHTS_QUERY,
            injections: None,
            injections_extra: None,
        });
        #[cfg(feature = "lang-rst")]
        v.push(GrammarInfo {
            id: RST,
            name: "reStructuredText",
            extensions: &["rst"],
            names: &["restructuredtext", "rst"],
            language: || tree_sitter_rst::LANGUAGE.into(),
            highlights: RST_HIGHLIGHTS,
            injections: None,
            injections_extra: None,
        });
        #[cfg(feature = "lang-asciidoc")]
        v.push(GrammarInfo {
            id: ASCIIDOC,
            name: "AsciiDoc",
            extensions: &["adoc", "asciidoc"],
            names: &["asciidoc", "adoc"],
            language: tree_sitter_asciidoc::language,
            highlights: ASCIIDOC_HIGHLIGHTS,
            injections: None,
            injections_extra: None,
        });
        shells::push(&mut v);
        programming::push(&mut v);
        structured::push(&mut v);
        web::push(&mut v);
        #[cfg(feature = "lang-markdown")]
        v.push(GrammarInfo {
            id: MARKDOWN_INLINE,
            name: "Markdown (inline)",
            // Never resolved from a path: reached only via the block grammar's
            // `markdown_inline` injection.
            extensions: &[],
            names: &["markdown_inline", "markdown-inline"],
            language: || tree_sitter_md::INLINE_LANGUAGE.into(),
            highlights: tree_sitter_md::HIGHLIGHT_QUERY_INLINE,
            injections: Some(tree_sitter_md::INJECTION_QUERY_INLINE),
            injections_extra: None,
        });

        v
    })
}

/// The grammar registered under `id`, if compiled in.
pub(crate) fn grammar(id: LanguageId) -> Option<&'static GrammarInfo> {
    all().iter().find(|g| g.id == id)
}