uncomment 3.10.1

A CLI tool to remove comments from code using tree-sitter for accurate parsing
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
use ahash::AHashSet;

/// A language's literal comment delimiters, as opposed to the tree-sitter node kind
/// names in [`LanguageConfig::comment_types`].
///
/// `line` is always the *plain* line-comment token, never a documentation form:
/// [`crate::rules::preservation::PreservationRule::Documentation`] classifies `///`,
/// `//!`, `/**` and `##` as documentation, and a marker comment written with one of
/// those is preserved as documentation instead of being read as a marker — it does
/// nothing, silently.
///
/// `block` is likewise the plain pair — `("/*", "*/")`, never `("/**", "*/")` — and is
/// only populated when the pair can be written at a comment's own indentation. A form
/// anchored to column 0, such as Ruby's `=begin`/`=end` or Perl's POD, is left `None`:
/// wrapping an indented marker in it would not parse as a comment.
///
/// Either form may be absent. CSS and OCaml have only a block pair, Python and YAML
/// only a line token, and plain JSON has neither.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct CommentSyntax {
    pub line: Option<&'static str>,
    pub block: Option<(&'static str, &'static str)>,
}

impl CommentSyntax {
    /// No comment syntax at all.
    pub const NONE: Self = Self {
        line: None,
        block: None,
    };
    /// `//` plus `/* */` — the C family and the languages modelled on it.
    pub const C_STYLE: Self = Self::both("//", "/*", "*/");
    /// `//` with no block form, as in Zig.
    pub const SLASH: Self = Self::line_only("//");
    /// `#` with no block form.
    pub const HASH: Self = Self::line_only("#");
    /// `#` plus the C block pair, as in Nix and HCL.
    pub const HASH_C_BLOCK: Self = Self::both("#", "/*", "*/");
    /// `<!-- -->` only — markup languages.
    pub const MARKUP: Self = Self::block_only("<!--", "-->");
    /// `/* */` only — CSS.
    pub const C_BLOCK: Self = Self::block_only("/*", "*/");
    /// `--` plus `{- -}` — the Haskell family.
    pub const DASH_BRACE: Self = Self::both("--", "{-", "-}");

    #[must_use]
    pub const fn line_only(line: &'static str) -> Self {
        Self {
            line: Some(line),
            block: None,
        }
    }

    #[must_use]
    pub const fn block_only(open: &'static str, close: &'static str) -> Self {
        Self {
            line: None,
            block: Some((open, close)),
        }
    }

    #[must_use]
    pub const fn both(line: &'static str, open: &'static str, close: &'static str) -> Self {
        Self {
            line: Some(line),
            block: Some((open, close)),
        }
    }

    /// Syntax shared by every language that parses with the given tree-sitter grammar,
    /// used as a fallback for languages registered at runtime without their own entry.
    ///
    /// Returns `None` where no single answer is correct — `json` backs both plain JSON
    /// (no comments) and JSONC (`//` and `/* */`), so guessing either would be wrong.
    #[must_use]
    pub fn for_tree_sitter_language(tslp_name: &str) -> Option<Self> {
        let syntax = match tslp_name {
            "c" | "cpp" | "csharp" | "dart" | "go" | "groovy" | "java" | "javascript" | "kotlin" | "objc" | "php"
            | "proto" | "rust" | "scala" | "scss" | "swift" | "tsx" | "typescript" => Self::C_STYLE,
            "bash" | "dockerfile" | "elixir" | "fish" | "make" | "perl" | "properties" | "python" | "r"
            | "starlark" | "toml" | "yaml" => Self::HASH,
            "hcl" | "nix" => Self::HASH_C_BLOCK,
            "html" | "markdown" | "svelte" | "vue" | "xml" => Self::MARKUP,
            "elm" | "haskell" => Self::DASH_BRACE,
            "css" => Self::C_BLOCK,
            "zig" => Self::SLASH,
            "clojure" | "ini" => Self::line_only(";"),
            "erlang" | "latex" => Self::line_only("%"),
            "fortran" => Self::line_only("!"),
            // Ruby's `=begin`/`=end` is recognised only at column 0, so it cannot wrap a
            // marker at a comment's own indentation — `#` is the only usable form.
            "ruby" => Self::HASH,
            "sql" => Self::both("--", "/*", "*/"),
            "lua" => Self::both("--", "--[[", "]]"),
            "powershell" => Self::both("#", "<#", "#>"),
            "julia" => Self::both("#", "#=", "=#"),
            "ocaml" => Self::block_only("(*", "*)"),
            // A Go template comment is only a comment inside an action, so the delimiters are part
            // of the pair: a bare `/* … */` in a template is literal output text, not a comment.
            "gotmpl" => Self::block_only("{{/*", "*/}}"),
            _ => return None,
        };
        Some(syntax)
    }
}

/// Outcome of asking a [`LanguageConfig`] for its literal comment syntax.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum CommentSyntaxResolution {
    Resolved(CommentSyntax),
    /// Nothing is known about this language's delimiters. Callers must skip and report,
    /// never fall back to a guess: a wrong marker is worse than no marker.
    Unknown,
}

#[derive(Debug, Clone)]
pub struct LanguageConfig {
    pub name: String,
    pub extensions: Vec<String>,
    /// Whole filenames this language claims, matched case-sensitively and before any extension rule.
    ///
    /// An entry ending in `.*` claims every name starting with the part before the `*`, which is how
    /// `Dockerfile.prod` is reached without naming the language in detection code.
    pub filenames: Vec<String>,
    pub comment_types: Vec<String>,
    pub doc_comment_types: Vec<String>,
    pub tslp_name: String,
    pub comment_syntax: Option<CommentSyntax>,
}

impl LanguageConfig {
    pub fn new(
        name: &str,
        extensions: Vec<&str>,
        comment_types: Vec<&str>,
        doc_comment_types: Vec<&str>,
        tslp_name: &str,
    ) -> Self {
        Self {
            name: name.to_string(),
            extensions: extensions.iter().map(|&s| s.to_string()).collect(),
            filenames: Vec::new(),
            comment_types: comment_types.iter().map(|&s| s.to_string()).collect(),
            doc_comment_types: doc_comment_types.iter().map(|&s| s.to_string()).collect(),
            tslp_name: tslp_name.to_string(),
            comment_syntax: None,
        }
    }

    #[must_use]
    pub const fn with_comment_syntax(mut self, syntax: CommentSyntax) -> Self {
        self.comment_syntax = Some(syntax);
        self
    }

    #[must_use]
    pub fn with_filenames(mut self, filenames: Vec<&str>) -> Self {
        self.filenames = filenames.iter().map(|&s| s.to_string()).collect();
        self
    }

    /// This language's literal delimiters, falling back to the grammar family when the
    /// config carries no entry of its own.
    #[must_use]
    pub fn resolve_comment_syntax(&self) -> CommentSyntaxResolution {
        if let Some(syntax) = self.comment_syntax {
            return CommentSyntaxResolution::Resolved(syntax);
        }

        match CommentSyntax::for_tree_sitter_language(&self.tslp_name) {
            Some(syntax) => CommentSyntaxResolution::Resolved(syntax),
            None => CommentSyntaxResolution::Unknown,
        }
    }

    /// The plain line-comment token to prefix a marker line with, if the language has one.
    #[must_use]
    pub fn line_comment_token(&self) -> Option<&'static str> {
        match self.resolve_comment_syntax() {
            CommentSyntaxResolution::Resolved(syntax) => syntax.line,
            CommentSyntaxResolution::Unknown => None,
        }
    }

    /// The plain block-comment delimiters to wrap a marker line in, for a language that has no line
    /// form — CSS and HTML carry every comment they have this way.
    #[must_use]
    pub fn block_comment_delimiters(&self) -> Option<(&'static str, &'static str)> {
        match self.resolve_comment_syntax() {
            CommentSyntaxResolution::Resolved(syntax) => syntax.block,
            CommentSyntaxResolution::Unknown => None,
        }
    }

    /// What to put inside the backticks of a "keep a comment" tip's second clause, in this
    /// language's own comment syntax: the plain line token when it has one (`#`, `--`, ...), else
    /// its block pair with the marker already inside it (`/* ~keep */`), since a bare open
    /// delimiter alone would not read as a complete line.
    #[must_use]
    pub fn keep_marker_line_hint(&self) -> String {
        match self.line_comment_token() {
            Some(token) => token.to_string(),
            None => match self.block_comment_delimiters() {
                Some((open, close)) => format!("{open} ~keep {close}"),
                None => "comment".to_string(),
            },
        }
    }

    pub fn supports_extension(&self, extension: &str) -> bool {
        self.extensions
            .iter()
            .any(|configured| configured.eq_ignore_ascii_case(extension))
    }

    pub fn is_comment_type(&self, node_type: &str) -> bool {
        self.comment_types.iter().any(|configured| configured == node_type)
    }

    pub fn is_doc_comment_type(&self, node_type: &str) -> bool {
        self.doc_comment_types.iter().any(|configured| configured == node_type)
    }

    pub fn get_comment_types(&self) -> &[String] {
        &self.comment_types
    }

    pub fn get_doc_comment_types(&self) -> &[String] {
        &self.doc_comment_types
    }

    pub fn get_all_comment_types(&self) -> AHashSet<String> {
        let mut types = AHashSet::new();
        types.extend(self.comment_types.iter().cloned());
        types.extend(self.doc_comment_types.iter().cloned());
        types
    }
}

impl LanguageConfig {
    pub fn rust() -> Self {
        Self::new(
            "rust",
            vec!["rs"],
            vec!["line_comment", "block_comment"],
            vec!["doc_comment", "inner_doc_comment", "outer_doc_comment"],
            "rust",
        )
        .with_comment_syntax(CommentSyntax::C_STYLE)
    }

    pub fn python() -> Self {
        Self::new(
            "python",
            vec!["py", "pyw", "pyi", "pyx", "pxd"],
            vec!["comment"],
            vec!["string"],
            "python",
        )
        .with_comment_syntax(CommentSyntax::HASH)
    }

    pub fn javascript() -> Self {
        Self::new(
            "javascript",
            vec!["js", "jsx", "mjs", "cjs"],
            vec!["comment"],
            vec!["comment"],
            "javascript",
        )
        .with_comment_syntax(CommentSyntax::C_STYLE)
    }

    pub fn typescript() -> Self {
        Self::new(
            "typescript",
            vec!["ts", "mts", "cts", "d.ts", "d.mts", "d.cts"],
            vec!["comment"],
            vec!["comment"],
            "typescript",
        )
        .with_comment_syntax(CommentSyntax::C_STYLE)
    }

    pub fn tsx() -> Self {
        Self::new("tsx", vec!["tsx"], vec!["comment"], vec!["comment"], "tsx")
            .with_comment_syntax(CommentSyntax::C_STYLE)
    }

    pub fn go() -> Self {
        Self::new("go", vec!["go"], vec!["comment"], vec!["comment"], "go").with_comment_syntax(CommentSyntax::C_STYLE)
    }

    /// Go's `text/template` and `html/template` syntax, which is what a Helm chart's `.tpl` helper
    /// files are written in.
    ///
    /// `.tpl` is the Helm convention and the reason this exists; `.tmpl` and `.gotmpl` are Go's own,
    /// and `.gohtml` is the HTML flavour. There is no documentation-comment convention in templates,
    /// so `doc_comment_types` is deliberately empty — `{{/** … */}}` is an ordinary comment.
    pub fn gotmpl() -> Self {
        Self::new(
            "gotmpl",
            vec!["tpl", "tmpl", "gotmpl", "gohtml"],
            vec!["comment"],
            vec![],
            "gotmpl",
        )
        .with_comment_syntax(CommentSyntax::block_only("{{/*", "*/}}"))
    }

    pub fn ruby() -> Self {
        Self::new(
            "ruby",
            vec!["rb", "rbw", "gemspec", "rake"],
            vec!["comment"],
            vec![],
            "ruby",
        )
        .with_comment_syntax(CommentSyntax::HASH)
    }

    pub fn php() -> Self {
        Self::new("php", vec!["php", "phtml"], vec!["comment"], vec![], "php")
            .with_comment_syntax(CommentSyntax::C_STYLE)
    }

    pub fn elixir() -> Self {
        Self::new("elixir", vec!["ex", "exs"], vec!["comment"], vec![], "elixir")
            .with_comment_syntax(CommentSyntax::HASH)
    }

    pub fn toml() -> Self {
        Self::new("toml", vec!["toml"], vec!["comment"], vec![], "toml").with_comment_syntax(CommentSyntax::HASH)
    }

    pub fn csharp() -> Self {
        Self::new("csharp", vec!["cs"], vec!["comment"], vec![], "csharp").with_comment_syntax(CommentSyntax::C_STYLE)
    }

    pub fn java() -> Self {
        Self::new(
            "java",
            vec!["java"],
            vec!["line_comment", "block_comment"],
            vec!["block_comment"],
            "java",
        )
        .with_comment_syntax(CommentSyntax::C_STYLE)
    }

    pub fn c() -> Self {
        Self::new("c", vec!["c", "h"], vec!["comment"], vec!["comment"], "c")
            .with_comment_syntax(CommentSyntax::C_STYLE)
    }

    pub fn cpp() -> Self {
        Self::new(
            "cpp",
            vec!["cpp", "cxx", "cc", "c++", "hpp", "hxx", "hh", "h++"],
            vec!["comment"],
            vec!["comment"],
            "cpp",
        )
        .with_comment_syntax(CommentSyntax::C_STYLE)
    }

    pub fn json() -> Self {
        Self::new("json", vec!["json"], vec![], vec![], "json").with_comment_syntax(CommentSyntax::NONE)
    }

    pub fn jsonc() -> Self {
        Self::new("jsonc", vec!["jsonc"], vec!["comment"], vec![], "json").with_comment_syntax(CommentSyntax::C_STYLE)
    }

    pub fn yaml() -> Self {
        Self::new("yaml", vec!["yaml", "yml"], vec!["comment"], vec![], "yaml").with_comment_syntax(CommentSyntax::HASH)
    }

    pub fn hcl() -> Self {
        Self::new("hcl", vec!["hcl", "tf", "tfvars"], vec!["comment"], vec![], "hcl")
            .with_comment_syntax(CommentSyntax::HASH_C_BLOCK)
    }

    pub fn make() -> Self {
        Self::new("make", vec!["mk"], vec!["comment"], vec![], "make")
            .with_filenames(vec!["Makefile", "makefile", "GNUmakefile"])
            .with_comment_syntax(CommentSyntax::HASH)
    }

    pub fn shell() -> Self {
        Self::new(
            "shell",
            vec!["sh", "bash", "zsh"],
            vec!["comment"],
            vec!["comment"],
            "bash",
        )
        .with_filenames(vec!["bashrc", ".bashrc", "zshrc", ".zshrc", "zshenv", ".zshenv"])
        .with_comment_syntax(CommentSyntax::HASH)
    }

    pub fn haskell() -> Self {
        Self::new("haskell", vec!["hs", "lhs"], vec!["comment"], vec![], "haskell")
            .with_comment_syntax(CommentSyntax::DASH_BRACE)
    }

    pub fn html() -> Self {
        Self::new("html", vec!["html", "htm", "xhtml"], vec!["comment"], vec![], "html")
            .with_comment_syntax(CommentSyntax::MARKUP)
    }

    pub fn css() -> Self {
        Self::new("css", vec!["css"], vec!["comment"], vec![], "css").with_comment_syntax(CommentSyntax::C_BLOCK)
    }

    pub fn xml() -> Self {
        Self::new(
            "xml",
            vec!["xml", "xsd", "xsl", "xslt", "svg"],
            vec!["Comment"],
            vec![],
            "xml",
        )
        .with_comment_syntax(CommentSyntax::MARKUP)
    }

    pub fn sql() -> Self {
        Self::new("sql", vec!["sql"], vec!["comment"], vec![], "sql")
            .with_comment_syntax(CommentSyntax::both("--", "/*", "*/"))
    }

    pub fn kotlin() -> Self {
        Self::new(
            "kotlin",
            vec!["kt", "kts"],
            vec!["line_comment", "block_comment"],
            vec![],
            "kotlin",
        )
        .with_comment_syntax(CommentSyntax::C_STYLE)
    }

    pub fn objc() -> Self {
        Self::new("objc", vec!["m"], vec!["comment"], vec!["comment"], "objc")
            .with_comment_syntax(CommentSyntax::C_STYLE)
    }

    pub fn swift() -> Self {
        Self::new(
            "swift",
            vec!["swift"],
            vec!["comment", "multiline_comment"],
            vec![],
            "swift",
        )
        .with_comment_syntax(CommentSyntax::C_STYLE)
    }

    pub fn lua() -> Self {
        Self::new("lua", vec!["lua"], vec!["comment"], vec![], "lua")
            .with_comment_syntax(CommentSyntax::both("--", "--[[", "]]"))
    }

    pub fn nix() -> Self {
        Self::new("nix", vec!["nix"], vec!["comment"], vec![], "nix").with_comment_syntax(CommentSyntax::HASH_C_BLOCK)
    }

    pub fn powershell() -> Self {
        Self::new(
            "powershell",
            vec!["ps1", "psm1", "psd1"],
            vec!["comment"],
            vec![],
            "powershell",
        )
        .with_comment_syntax(CommentSyntax::both("#", "<#", "#>"))
    }

    pub fn proto() -> Self {
        Self::new("proto", vec!["proto"], vec!["comment"], vec![], "proto").with_comment_syntax(CommentSyntax::C_STYLE)
    }

    pub fn ini() -> Self {
        Self::new("ini", vec!["ini", "cfg", "conf"], vec!["comment"], vec![], "ini")
            .with_comment_syntax(CommentSyntax::line_only(";"))
    }

    pub fn dockerfile() -> Self {
        Self::new("dockerfile", vec![], vec!["comment"], vec![], "dockerfile")
            .with_filenames(vec!["Dockerfile", "dockerfile", "Dockerfile.*", "dockerfile.*"])
            .with_comment_syntax(CommentSyntax::HASH)
    }

    /// Bazel's build language. `BUILD` and `WORKSPACE` carry no extension at all, so the filename
    /// list — not the extension list — is what makes most of a Bazel repository visible.
    ///
    /// Starlark has Python's docstrings rather than a doc-comment form, so `string` is the
    /// doc-comment kind — a `.bzl` module or macro documents itself with a leading string expression,
    /// and Stardoc publishes it. The kind only pays its way alongside the handler:
    /// [`crate::languages::handlers::PythonHandler`] is what tells a docstring `string` node from any
    /// other string literal, and without it every string in a `BUILD` file would be handed to the
    /// doc-comment machinery.
    pub fn starlark() -> Self {
        Self::new(
            "starlark",
            vec!["bzl", "bazel", "star"],
            vec!["comment"],
            vec!["string"],
            "starlark",
        )
        .with_filenames(vec![
            "BUILD",
            "BUILD.bazel",
            "WORKSPACE",
            "WORKSPACE.bazel",
            "WORKSPACE.bzlmod",
            "MODULE.bazel",
        ])
        .with_comment_syntax(CommentSyntax::HASH)
    }

    /// Java `.properties`. The grammar reports both comment forms — `#` and `!` — as `comment`.
    pub fn properties() -> Self {
        Self::new("properties", vec!["properties"], vec!["comment"], vec![], "properties")
            .with_comment_syntax(CommentSyntax::HASH)
    }

    pub fn scala() -> Self {
        Self::new(
            "scala",
            vec!["scala", "sc"],
            vec!["comment", "block_comment"],
            vec!["block_comment"],
            "scala",
        )
        .with_comment_syntax(CommentSyntax::C_STYLE)
    }

    pub fn dart() -> Self {
        Self::new(
            "dart",
            vec!["dart"],
            vec!["comment"],
            vec!["documentation_comment"],
            "dart",
        )
        .with_comment_syntax(CommentSyntax::C_STYLE)
    }

    pub fn r() -> Self {
        Self::new("r", vec!["r", "R"], vec!["comment"], vec![], "r").with_comment_syntax(CommentSyntax::HASH)
    }

    pub fn julia() -> Self {
        Self::new("julia", vec!["jl"], vec!["line_comment"], vec![], "julia")
            .with_comment_syntax(CommentSyntax::both("#", "#=", "=#"))
    }

    pub fn zig() -> Self {
        Self::new("zig", vec!["zig"], vec!["line_comment"], vec![], "zig").with_comment_syntax(CommentSyntax::SLASH)
    }

    pub fn clojure() -> Self {
        Self::new(
            "clojure",
            vec!["clj", "cljs", "cljc", "edn"],
            vec!["comment"],
            vec![],
            "clojure",
        )
        .with_comment_syntax(CommentSyntax::line_only(";"))
    }

    pub fn elm() -> Self {
        Self::new("elm", vec!["elm"], vec!["line_comment", "block_comment"], vec![], "elm")
            .with_comment_syntax(CommentSyntax::DASH_BRACE)
    }

    pub fn erlang() -> Self {
        Self::new("erlang", vec!["erl", "hrl"], vec!["comment"], vec![], "erlang")
            .with_comment_syntax(CommentSyntax::line_only("%"))
    }

    pub fn vue() -> Self {
        Self::new("vue", vec!["vue"], vec!["comment"], vec![], "vue").with_comment_syntax(CommentSyntax::MARKUP)
    }

    pub fn svelte() -> Self {
        Self::new("svelte", vec!["svelte"], vec!["comment"], vec![], "svelte")
            .with_comment_syntax(CommentSyntax::MARKUP)
    }

    pub fn scss() -> Self {
        Self::new("scss", vec!["scss"], vec!["comment", "js_comment"], vec![], "scss")
            .with_comment_syntax(CommentSyntax::C_STYLE)
    }

    pub fn latex() -> Self {
        Self::new(
            "latex",
            vec!["tex", "sty", "cls"],
            vec!["line_comment"],
            vec![],
            "latex",
        )
        .with_comment_syntax(CommentSyntax::line_only("%"))
    }

    pub fn fish() -> Self {
        Self::new("fish", vec!["fish"], vec!["comment"], vec![], "fish").with_comment_syntax(CommentSyntax::HASH)
    }

    pub fn perl() -> Self {
        Self::new("perl", vec!["pl", "pm"], vec!["comment"], vec![], "perl").with_comment_syntax(CommentSyntax::HASH)
    }

    pub fn groovy() -> Self {
        Self::new(
            "groovy",
            vec!["groovy", "gradle"],
            vec!["line_comment", "block_comment"],
            vec!["block_comment"],
            "groovy",
        )
        .with_comment_syntax(CommentSyntax::C_STYLE)
    }

    pub fn ocaml() -> Self {
        Self::new("ocaml", vec!["ml", "mli"], vec!["comment"], vec![], "ocaml")
            .with_comment_syntax(CommentSyntax::block_only("(*", "*)"))
    }

    /// CommonMark. The grammar emits no `comment` node whatsoever — an HTML comment is raw HTML to
    /// CommonMark, so `<!-- … -->` arrives as an `html_block`, the same kind that carries a
    /// `<div align="center">` badge row or a `<details>` block.
    ///
    /// `html_block` is therefore declared here *only* because
    /// [`crate::languages::handlers::MarkdownHandler`] can tell the two apart from the block's text.
    /// Declaring it for a runtime-registered language, which gets no handler, would delete embedded
    /// HTML.
    ///
    /// `doc_comment_types` is empty: markdown has no documentation-comment form.
    pub fn markdown() -> Self {
        Self::new(
            "markdown",
            vec!["md", "markdown", "mdown", "mkd"],
            vec!["html_block"],
            vec![],
            "markdown",
        )
        .with_comment_syntax(CommentSyntax::MARKUP)
    }

    pub fn fortran() -> Self {
        Self::new(
            "fortran",
            vec!["f90", "f95", "f03", "f08"],
            vec!["comment"],
            vec![],
            "fortran",
        )
        .with_comment_syntax(CommentSyntax::line_only("!"))
    }
}

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

    /// Prefixes `PreservationRule::Documentation` treats as documentation. Duplicated
    /// here so a unit test can guard the table without reaching into the rules module;
    /// `tests/language_syntax_test.rs` runs the real classifier.
    const DOC_PREFIXES: &[&str] = &["/**", "///", "//!", "##", "\"\"\"", "'''"];

    #[test]
    fn test_language_config_creation() {
        let config = LanguageConfig::rust();
        assert_eq!(config.name, "rust");
        assert!(config.supports_extension("rs"));
        assert!(!config.supports_extension("py"));
        assert!(config.is_comment_type("line_comment"));
        assert!(config.is_doc_comment_type("doc_comment"));
    }

    #[test]
    fn test_extension_support() {
        let rust_config = LanguageConfig::rust();
        assert!(rust_config.supports_extension("rs"));
        assert!(rust_config.supports_extension("RS"));

        let python_config = LanguageConfig::python();
        assert!(python_config.supports_extension("py"));
        assert!(python_config.supports_extension("pyw"));
        assert!(python_config.supports_extension("pyi"));
    }

    #[test]
    fn test_comment_type_detection() {
        let rust_config = LanguageConfig::rust();
        assert!(rust_config.is_comment_type("line_comment"));
        assert!(rust_config.is_comment_type("block_comment"));
        assert!(!rust_config.is_comment_type("function"));

        assert!(rust_config.is_doc_comment_type("doc_comment"));
        assert!(!rust_config.is_doc_comment_type("line_comment"));
    }

    #[test]
    fn test_all_comment_types() {
        let rust_config = LanguageConfig::rust();
        let all_types = rust_config.get_all_comment_types();
        assert!(all_types.contains("line_comment"));
        assert!(all_types.contains("block_comment"));
        assert!(all_types.contains("doc_comment"));
        assert!(all_types.contains("inner_doc_comment"));
        assert!(all_types.contains("outer_doc_comment"));
    }

    #[test]
    fn test_language_specific_configs() {
        let languages = vec![
            LanguageConfig::rust(),
            LanguageConfig::python(),
            LanguageConfig::javascript(),
            LanguageConfig::typescript(),
            LanguageConfig::go(),
            LanguageConfig::java(),
            LanguageConfig::c(),
            LanguageConfig::cpp(),
            LanguageConfig::shell(),
            LanguageConfig::haskell(),
            LanguageConfig::html(),
            LanguageConfig::css(),
            LanguageConfig::xml(),
            LanguageConfig::sql(),
            LanguageConfig::kotlin(),
            LanguageConfig::swift(),
            LanguageConfig::objc(),
            LanguageConfig::lua(),
            LanguageConfig::nix(),
            LanguageConfig::powershell(),
            LanguageConfig::proto(),
            LanguageConfig::ini(),
        ];

        for lang in languages {
            assert!(!lang.name.is_empty());
            assert!(!lang.extensions.is_empty());
            assert!(!lang.comment_types.is_empty());
        }
    }

    #[test]
    fn new_leaves_comment_syntax_unset() {
        let config = LanguageConfig::new("custom", vec!["cst"], vec!["comment"], vec![], "unheard-of");
        assert_eq!(config.comment_syntax, None);
        assert_eq!(config.resolve_comment_syntax(), CommentSyntaxResolution::Unknown);
        assert_eq!(config.line_comment_token(), None);
    }

    #[test]
    fn builtin_configs_carry_their_own_syntax() {
        assert_eq!(LanguageConfig::rust().comment_syntax, Some(CommentSyntax::C_STYLE));
        assert_eq!(LanguageConfig::python().line_comment_token(), Some("#"));
        assert_eq!(LanguageConfig::json().comment_syntax, Some(CommentSyntax::NONE));
        assert_eq!(LanguageConfig::json().line_comment_token(), None);
    }

    #[test]
    fn family_fallback_resolves_by_tree_sitter_name() {
        let config = LanguageConfig::new("bash-ish", vec!["bsh"], vec!["comment"], vec![], "bash");
        assert_eq!(
            config.resolve_comment_syntax(),
            CommentSyntaxResolution::Resolved(CommentSyntax::HASH)
        );
    }

    #[test]
    fn ambiguous_json_family_does_not_guess() {
        assert_eq!(CommentSyntax::for_tree_sitter_language("json"), None);
    }

    #[test]
    fn no_builtin_line_token_is_a_documentation_prefix() {
        for syntax in [
            CommentSyntax::C_STYLE,
            CommentSyntax::SLASH,
            CommentSyntax::HASH,
            CommentSyntax::HASH_C_BLOCK,
            CommentSyntax::DASH_BRACE,
            CommentSyntax::both("--", "/*", "*/"),
            CommentSyntax::both("--", "--[[", "]]"),
            CommentSyntax::both("#", "<#", "#>"),
            CommentSyntax::both("#", "#=", "=#"),
            CommentSyntax::line_only(";"),
            CommentSyntax::line_only("%"),
            CommentSyntax::line_only("!"),
        ] {
            let line = syntax.line.expect("every syntax in this list has a line token");
            let marker = format!("{line} ~keep");
            for prefix in DOC_PREFIXES {
                assert!(
                    !marker.starts_with(prefix),
                    "`{marker}` starts with doc prefix `{prefix}`"
                );
            }
        }
    }
}