uncomment 3.9.0

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
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
use super::*;
use crate::config::{Config, ConfigManager, ResolvedConfig};
use crate::languages::config::LanguageConfig;
use tempfile::tempdir;

fn default_resolved_config() -> ResolvedConfig {
    ResolvedConfig {
        remove_todos: false,
        remove_fixme: false,
        remove_docs: false,
        preserve_patterns: Vec::new(),
        use_default_ignores: true,
        respect_gitignore: true,
        traverse_git_repos: false,
        language_config: None,
    }
}

fn process_rust(source: &str) -> String {
    let mut processor = Processor::new();
    let language_config = LanguageConfig::rust();
    let resolved_config = default_resolved_config();
    let ProcessOutcome { content: output, .. } = processor
        .process_content_with_config(source, &language_config, &resolved_config)
        .expect("processing rust source");
    output
}

#[test]
fn plan_removals_reports_removable_comments_with_ranges() {
    let source = "// remove me\nfn main() {\n    let x = 1; // trailing\n    // TODO: keep\n    // ~keep\n}\n";
    let mut processor = Processor::new();
    let removals = processor
        .plan_removals(source, std::path::Path::new("sample.rs"), &default_resolved_config())
        .expect("plan removals");
    // TODO (remove_todos=false) and ~keep are preserved; two comments remain.
    let previews: Vec<&str> = removals.iter().map(|removal| removal.preview.as_str()).collect();
    assert_eq!(previews, vec!["// remove me", "// trailing"]);
    assert_eq!(removals[0].remove_start, 0);
    assert_eq!(
        &source[removals[0].remove_start..removals[0].remove_end],
        "// remove me\n"
    );
    // A trailing comment's range reaches back over the space separating it from the code.
    assert_eq!(
        &source[removals[1].remove_start..removals[1].remove_end],
        " // trailing"
    );
}

fn process_rust_with(source: &str, remove_docs: bool) -> ProcessOutcome {
    let mut processor = Processor::new();
    let language_config = LanguageConfig::rust();
    let resolved_config = ResolvedConfig {
        remove_docs,
        ..default_resolved_config()
    };
    processor
        .process_content_with_config(source, &language_config, &resolved_config)
        .expect("processing rust source")
}

#[test]
fn above_line_marker_preserves_doc_comment_under_remove_doc() {
    let source = "// ~keep\n/// Parent element ID.\npub fn a() {}\n";
    let outcome = process_rust_with(source, true);
    assert!(
        outcome.content.contains("/// Parent element ID."),
        "above-line marker protects the doc comment: {}",
        outcome.content
    );
    assert!(outcome.content.contains("// ~keep"), "the marker itself survives");
}

#[test]
fn above_line_marker_does_not_reach_past_a_blank_line() {
    let source = "// ~keep\n\n/// Unrelated doc.\npub fn a() {}\n";
    let outcome = process_rust_with(source, true);
    assert!(
        !outcome.content.contains("/// Unrelated doc."),
        "a blank line ends the run: {}",
        outcome.content
    );
}

#[test]
fn above_line_marker_does_not_reach_past_code() {
    // The `///` node swallows its trailing newline, so row adjacency alone
    // would make the comment after `pub fn a()` look like a neighbour.
    let source = "// ~keep\n/// Protected doc.\npub fn a() {}\n// unrelated removable\npub fn b() {}\n";
    let outcome = process_rust_with(source, true);
    assert!(outcome.content.contains("/// Protected doc."), "target kept");
    assert!(
        !outcome.content.contains("// unrelated removable"),
        "code between comments ends the run: {}",
        outcome.content
    );
}

#[test]
fn redundant_keep_marker_is_stripped_from_doc_comment() {
    let source = "/// Parent element ID. ~keep Resolves elsewhere.\npub fn a() {}\n";
    let outcome = process_rust_with(source, false);
    assert!(
        outcome.content.contains("/// Parent element ID. Resolves elsewhere."),
        "marker stripped and spacing collapsed: {}",
        outcome.content
    );
    assert_eq!(outcome.redundant_markers.len(), 1, "one marker reported");
    assert_eq!(outcome.redundant_markers[0].line, 0);
}

#[test]
fn trailing_redundant_marker_takes_the_space_before_it() {
    let source = "/// Parent element ID. ~keep\npub fn a() {}\n";
    let outcome = process_rust_with(source, false);
    assert!(
        outcome.content.contains("/// Parent element ID.\n"),
        "no trailing space left behind: {}",
        outcome.content
    );
}

#[test]
fn load_bearing_keep_marker_survives_under_remove_doc() {
    let source = "/// Parent element ID. ~keep\npub fn a() {}\n";
    let outcome = process_rust_with(source, true);
    assert!(
        outcome.content.contains("~keep"),
        "the marker is the only thing preserving this doc comment: {}",
        outcome.content
    );
    assert!(outcome.redundant_markers.is_empty(), "nothing reported as redundant");
}

#[test]
fn keep_marker_on_a_line_comment_is_never_stripped() {
    let source = "// rationale ~keep\npub fn a() {}\n";
    let outcome = process_rust_with(source, false);
    assert!(
        outcome.content.contains("// rationale ~keep"),
        "line comments do not render into docs, so the marker stays: {}",
        outcome.content
    );
    assert!(outcome.redundant_markers.is_empty());
}

#[test]
fn nested_comment_nodes_are_counted_once() {
    // Rust records each `///` line twice: the outer `line_comment` and the
    // inner doc node. Both describe one comment.
    let source = "/// Doc one.\npub fn a() {}\n\n/// Doc two.\npub fn b() {}\n";
    let outcome = process_rust_with(source, true);
    assert_eq!(outcome.removed_comments.len(), 2, "two doc comments, not four nodes");
    let spans: Vec<(usize, usize)> = outcome
        .removed_comments
        .iter()
        .map(|comment| (comment.start_row, comment.end_row))
        .collect();
    assert_eq!(spans, vec![(0, 1), (3, 4)], "no duplicated ranges");
}

#[test]
fn prose_about_the_marker_is_left_alone() {
    let cases = [
        "/// Comments containing `~keep` are preserved.\npub fn a() {}\n",
        "/// Write `/// ~keep Parent element ID.` to protect it.\npub fn a() {}\n",
        "/// ```text\n/// // ~keep\n/// ```\npub fn a() {}\n",
        "/// A ~keepsake is not a marker.\npub fn a() {}\n",
    ];
    for source in cases {
        let outcome = process_rust_with(source, false);
        assert_eq!(
            outcome.content, source,
            "documentation discussing the marker must survive intact: {source}"
        );
        assert!(outcome.redundant_markers.is_empty(), "nothing reported for: {source}");
    }
}

#[test]
fn keep_marker_preserves_whole_contiguous_line_comment_block() {
    let source = "fn f() {\n    // line one\n    // line two\n    // line three ~keep\n    let x = 1;\n}\n";
    let output = process_rust(source);
    assert!(output.contains("// line one"), "first block line kept: {output}");
    assert!(output.contains("// line two"), "middle block line kept: {output}");
    assert!(output.contains("// line three ~keep"), "marked line kept: {output}");
}

#[test]
fn keep_marker_on_first_line_preserves_block() {
    let source = "fn f() {\n    // one ~keep\n    // two\n    // three\n    let x = 1;\n}\n";
    let output = process_rust(source);
    assert!(output.contains("// one ~keep"), "marked line kept: {output}");
    assert!(output.contains("// two"), "following line kept: {output}");
    assert!(output.contains("// three"), "following line kept: {output}");
}

#[test]
fn blank_line_breaks_keep_block() {
    let source =
        "fn f() {\n    // kept ~keep\n    // kept two\n\n    // dropped one\n    // dropped two\n    let x = 1;\n}\n";
    let output = process_rust(source);
    assert!(output.contains("// kept ~keep"), "marked line kept: {output}");
    assert!(output.contains("// kept two"), "same-block line kept: {output}");
    assert!(
        !output.contains("// dropped one"),
        "separate paragraph stripped: {output}"
    );
    assert!(
        !output.contains("// dropped two"),
        "separate paragraph stripped: {output}"
    );
}

#[test]
fn trailing_keep_does_not_extend_to_standalone_neighbor() {
    let source = "fn f() {\n    let x = 1; // trailing ~keep\n    // standalone removable\n    let y = 2;\n}\n";
    let output = process_rust(source);
    assert!(
        output.contains("// trailing ~keep"),
        "trailing keep preserved per-comment: {output}"
    );
    assert!(
        !output.contains("// standalone removable"),
        "a trailing keep must not anchor a block: {output}"
    );
}

#[test]
fn code_between_comments_breaks_keep_block() {
    let source = "fn f() {\n    // block a ~keep\n    let x = 1;\n    // block b removable\n    let y = 2;\n}\n";
    let output = process_rust(source);
    assert!(output.contains("// block a ~keep"), "marked line kept: {output}");
    assert!(
        !output.contains("// block b removable"),
        "code between comments ends the block: {output}"
    );
}

#[test]
fn merge_ranges_combines_touching_and_overlapping() {
    assert_eq!(merge_ranges(&[(0, 5), (5, 10)]), vec![(0, 10)], "touching ranges merge");
    assert_eq!(
        merge_ranges(&[(0, 5), (6, 10)]),
        vec![(0, 5), (6, 10)],
        "disjoint stay split"
    );
    assert_eq!(merge_ranges(&[(0, 7), (3, 10)]), vec![(0, 10)], "overlapping merge");
    assert_eq!(
        merge_ranges(&[(6, 10), (0, 5)]),
        vec![(0, 5), (6, 10)],
        "unsorted input is sorted"
    );
    assert_eq!(merge_ranges(&[]), Vec::<(usize, usize)>::new(), "empty input");
}

#[test]
fn cut_ranges_removes_only_covered_bytes() {
    let content = "abcdefghij";
    assert_eq!(cut_ranges(content, 0, 10, &[(3, 6)]), "abcghij", "range mid-window");
    assert_eq!(
        cut_ranges(content, 2, 8, &[(2, 4)]),
        "efgh",
        "range flush to window start"
    );
    assert_eq!(
        cut_ranges(content, 2, 8, &[(6, 8)]),
        "cdef",
        "range flush to window end"
    );
    assert_eq!(cut_ranges(content, 2, 8, &[(0, 10)]), "", "range covers whole window");
    assert_eq!(
        cut_ranges(content, 2, 5, &[(6, 9)]),
        "cde",
        "range outside window is ignored"
    );
    assert_eq!(
        cut_ranges(content, 0, 5, &[(3, 3)]),
        "abcde",
        "zero-length range is a no-op"
    );
}

#[test]
fn records_removed_comment_locations_and_previews() {
    let source = "// standalone\nfn main() {\n    let x = 1; // trailing\n    /* block\n       two */\n}\n";
    let mut processor = Processor::new();
    let language_config = LanguageConfig::rust();
    let outcome = processor
        .process_content_with_config(source, &language_config, &default_resolved_config())
        .expect("processing rust source");

    let spans: Vec<(usize, usize)> = outcome
        .removed_comments
        .iter()
        .map(|comment| (comment.start_row, comment.end_row))
        .collect();
    assert_eq!(spans, vec![(0, 0), (2, 2), (3, 4)]);

    let previews: Vec<&str> = outcome
        .removed_comments
        .iter()
        .map(|comment| comment.preview.as_str())
        .collect();
    assert_eq!(previews, vec!["// standalone", "// trailing", "/* block"]);

    assert!(outcome.removed_comments.iter().all(|comment| !comment.is_documentation));
    assert_eq!(outcome.removed_comments.len(), 3);
}

#[test]
fn plan_removals_preserves_python_docstrings_by_default() {
    let source = "def f():\n    \"\"\"docstring\"\"\"\n    # remove me\n    return 1\n";
    let mut processor = Processor::new();
    let removals = processor
        .plan_removals(source, std::path::Path::new("module.py"), &default_resolved_config())
        .expect("plan removals");
    let previews: Vec<&str> = removals.iter().map(|removal| removal.preview.as_str()).collect();
    assert_eq!(previews, vec!["# remove me"]);
}

#[test]
fn plan_removals_unsupported_extension_errors() {
    let mut processor = Processor::new();
    let result = processor.plan_removals(
        "noop",
        std::path::Path::new("file.unknownext"),
        &default_resolved_config(),
    );
    assert!(result.is_err());
}

fn process_go(source: &str, use_default_ignores: bool, remove_docs: bool) -> String {
    let mut processor = Processor::new();
    let language_config = LanguageConfig::go();
    let mut resolved_config = default_resolved_config();
    resolved_config.use_default_ignores = use_default_ignores;
    resolved_config.remove_docs = remove_docs;
    let ProcessOutcome { content: output, .. } = processor
        .process_content_with_config(source, &language_config, &resolved_config)
        .expect("processing go source");
    output
}

fn process_language(source: &str, language_config: LanguageConfig) -> String {
    let mut processor = Processor::new();
    let resolved_config = default_resolved_config();
    let ProcessOutcome { content: output, .. } = processor
        .process_content_with_config(source, &language_config, &resolved_config)
        .expect("processing source");
    output
}

fn process_language_with_default_ignores(
    source: &str,
    language_config: LanguageConfig,
    use_default_ignores: bool,
) -> String {
    let mut processor = Processor::new();
    let mut resolved_config = default_resolved_config();
    resolved_config.use_default_ignores = use_default_ignores;
    let ProcessOutcome { content: output, .. } = processor
        .process_content_with_config(source, &language_config, &resolved_config)
        .expect("processing source");
    output
}

#[test]
fn preserves_strings_matching_comment_text() {
    let source = r#"fn main() {
    let pattern = "// comment";
    println!("{}", pattern); // comment
}
"#;

    let processed = process_rust(source);

    assert!(processed.contains("\"// comment\""));
    assert!(!processed.contains("; // comment"));
}

#[test]
fn preserves_macro_invocations_with_comment_like_strings() {
    let source = r#"macro_rules! announce {
    ($msg:expr) => {{
        println!("{}", $msg); // keep
    }};
}

fn main() {
    announce!("// keep");
}
"#;

    let processed = process_rust(source);

    assert!(processed.contains("announce!(\"// keep\");"));
    assert!(!processed.contains("// keep\n"));
}

#[test]
fn preserves_attributes_when_removing_doc_comments() {
    let source = r#"#[derive(Subcommand, Debug)]
pub enum Commands {
    /// Create smart config
    #[command(about = "Create a template configuration file")]
    Init,
}
"#;

    let mut processor = Processor::new();
    let language_config = LanguageConfig::rust();
    let mut config = default_resolved_config();
    config.remove_docs = true;

    let ProcessOutcome { content: processed, .. } = processor
        .process_content_with_config(source, &language_config, &config)
        .expect("process doc comments");

    assert!(processed.contains("#[command(about = \"Create a template configuration file\")]"));
    assert!(!processed.contains("Create smart config"));
}

#[test]
fn respects_no_default_ignores_override() {
    let dir = tempdir().expect("create temp dir");
    let file_path = dir.path().join("sample.rs");
    let source = r#"/// #![feature(never_type)]
// NOTE: this would normally be preserved
fn main() {}
"#;

    std::fs::write(&file_path, source).expect("write test file");

    let config_manager = ConfigManager::from_single_config(dir.path(), Config::default()).expect("config manager");

    let mut processor = Processor::new();

    let overrides_with_defaults = ProcessingOptions {
        remove_todo: true,
        remove_fixme: true,
        remove_doc: true,
        custom_preserve_patterns: Vec::new(),
        use_default_ignores: true,
        dry_run: true,
        show_diff: false,
        respect_gitignore: true,
        traverse_git_repos: false,
    };

    let with_defaults = processor
        .process_file_with_config(&file_path, &config_manager, Some(&overrides_with_defaults))
        .expect("process with defaults");
    assert!(with_defaults.processed_content.contains("NOTE"));
    assert!(with_defaults.processed_content.contains("#![feature"));

    let overrides_without_defaults = ProcessingOptions {
        use_default_ignores: false,
        ..overrides_with_defaults
    };

    let without_defaults = processor
        .process_file_with_config(&file_path, &config_manager, Some(&overrides_without_defaults))
        .expect("process without defaults");
    assert!(!without_defaults.processed_content.contains("NOTE"));
    assert!(!without_defaults.processed_content.contains("#![feature"));
    assert!(without_defaults.processed_content.contains("fn main()"));
}

#[test]
fn honors_config_file_disabling_default_ignores() {
    // Regression for #106: a config with use_default_ignores = false must be honored
    // when --no-default-ignores was NOT passed. Previously the CLI default clobbered it,
    // leaving hardcoded NOTE/HACK patterns unremovable.
    let dir = tempdir().expect("create temp dir");
    let file_path = dir.path().join("sample.lua");
    std::fs::write(&file_path, "-- NOTE: remove me\n-- HACK: me too\nlocal x = 1\n").expect("write test file");

    let mut config = Config::default();
    config.global.use_default_ignores = false;
    let config_manager = ConfigManager::from_single_config(dir.path(), config).expect("config manager");

    // CLI defaults: use_default_ignores = true because --no-default-ignores absent.
    let overrides = ProcessingOptions {
        remove_todo: false,
        remove_fixme: false,
        remove_doc: false,
        custom_preserve_patterns: Vec::new(),
        use_default_ignores: true,
        dry_run: true,
        show_diff: false,
        respect_gitignore: true,
        traverse_git_repos: false,
    };

    let mut processor = Processor::new();
    let result = processor
        .process_file_with_config(&file_path, &config_manager, Some(&overrides))
        .expect("process lua file");

    assert!(!result.processed_content.contains("NOTE"));
    assert!(!result.processed_content.contains("HACK"));
    assert!(result.processed_content.contains("local x = 1"));
}

#[test]
fn preserves_go_embed_directives_even_without_default_ignores() {
    let source = r#"package main

//go:embed hello.txt
var embedded string

func main() { /* regular comment should be removed */ }
"#;

    let processed = process_go(source, false, true);
    assert!(processed.contains("//go:embed hello.txt"));
    assert!(!processed.contains("regular comment should be removed"));
}

#[test]
fn preserves_go_cgo_preamble_comments() {
    let source = r#"package htmltomarkdown

// #cgo LDFLAGS: -lhtml_to_markdown_ffi
// #include <stdlib.h>
// extern const char* html_to_markdown_version();
import "C"

func Version() string { return C.GoString(C.html_to_markdown_version()) /* regular comment should be removed */ }
"#;

    for use_default_ignores in [true, false] {
        let processed = process_go(source, use_default_ignores, true);
        assert!(
            processed.contains("// #cgo LDFLAGS: -lhtml_to_markdown_ffi"),
            "expected to preserve cgo preamble with use_default_ignores={use_default_ignores}"
        );
        assert!(
            processed.contains("// #include <stdlib.h>"),
            "expected to preserve cgo preamble with use_default_ignores={use_default_ignores}"
        );
        assert!(
            processed.contains("// extern const char* html_to_markdown_version();"),
            "expected to preserve cgo preamble with use_default_ignores={use_default_ignores}"
        );
        assert!(processed.contains("import \"C\""));
        assert!(!processed.contains("regular comment should be removed"));
    }
}

#[test]
fn removes_ruby_comments_without_touching_strings() {
    let source = r#"# remove me
puts "Hello # not a comment"
"#;

    let processed = process_language(source, LanguageConfig::ruby());
    assert!(!processed.contains("# remove me"));
    assert!(processed.contains("Hello # not a comment"));
}

#[test]
fn preserves_ruby_frozen_string_literal_magic_comment() {
    let source = r#"# frozen_string_literal: true
# remove me
puts "ok"
"#;

    let processed = process_language(source, LanguageConfig::ruby());
    assert!(processed.contains("# frozen_string_literal: true"));
    assert!(!processed.contains("# remove me"));
}

#[test]
fn preserves_shebangs_even_without_default_ignores() {
    let source = r#"#!/usr/bin/env bash
# remove me
echo "ok"
"#;

    let processed = process_language_with_default_ignores(source, LanguageConfig::shell(), false);

    assert!(processed.starts_with("#!/usr/bin/env bash\n"));
    assert!(!processed.contains("# remove me"));
    assert!(processed.contains("echo \"ok\""));
}

#[test]
fn preserves_ruby_yard_doc_comments_by_default() {
    let source = r#"# @param x [Integer]
def foo(x)
  x + 1
end
"#;

    let processed = process_language(source, LanguageConfig::ruby());
    assert!(processed.contains("# @param x [Integer]"));
}

#[test]
fn removes_php_comments_without_touching_strings() {
    let source = r#"<?php
// remove me
$s = "// not a comment";
echo $s;
"#;

    let processed = process_language(source, LanguageConfig::php());
    assert!(!processed.contains("// remove me"));
    assert!(processed.contains("\"// not a comment\""));
}

#[test]
fn preserves_c_header_guard_trailing_comments() {
    let source = r#"#ifndef HTML_TO_MARKDOWN_H
#define HTML_TO_MARKDOWN_H

// remove me
int x;

#endif  /* HTML_TO_MARKDOWN_H */
"#;

    let processed = process_language(source, LanguageConfig::c());
    assert!(processed.contains("#endif  /* HTML_TO_MARKDOWN_H */"));
    assert!(!processed.contains("remove me"));
    assert!(processed.contains("int x;"));
}

#[test]
fn removes_elixir_comments_without_touching_strings() {
    let source = r##"# remove me
IO.puts("# not a comment")
"##;

    let processed = process_language(source, LanguageConfig::elixir());
    assert!(!processed.contains("# remove me"));
    assert!(processed.contains("\"# not a comment\""));
}

#[test]
fn removes_toml_comments_without_touching_strings() {
    let source = r##"# remove me
key = "# not a comment"
"##;

    let processed = process_language(source, LanguageConfig::toml());
    assert!(!processed.contains("# remove me"));
    assert!(processed.contains("\"# not a comment\""));
}

#[test]
fn removes_csharp_comments_without_touching_strings() {
    let source = r#"// remove me
class C { void M() { var s = "// not a comment"; } }
"#;

    let processed = process_language(source, LanguageConfig::csharp());
    assert!(!processed.contains("// remove me"));
    assert!(processed.contains("\"// not a comment\""));
}

#[test]
fn removes_haskell_comments_without_touching_strings() {
    let source = r#"-- remove me
main = putStrLn "-- not a comment"
"#;

    let processed = process_language(source, LanguageConfig::haskell());
    assert!(!processed.contains("-- remove me"));
    assert!(processed.contains("\"-- not a comment\""));
}

#[test]
fn removes_html_comments_without_touching_content() {
    let source = r#"<!-- remove me -->
<div>Hello</div>
"#;

    let processed = process_language(source, LanguageConfig::html());
    assert!(!processed.contains("remove me"));
    assert!(processed.contains("<div>Hello</div>"));
}

#[test]
fn removes_css_comments_without_touching_strings() {
    let source = r#"/* remove me */
.a::before { content: "/* not a comment */"; }
"#;

    let processed = process_language(source, LanguageConfig::css());
    assert!(!processed.contains("remove me"));
    assert!(processed.contains("\"/* not a comment */\""));
}

#[test]
fn removes_xml_comments_without_touching_text() {
    let source = r#"<!-- remove me -->
<root>hello</root>
"#;

    let processed = process_language(source, LanguageConfig::xml());
    assert!(!processed.contains("remove me"));
    assert!(processed.contains("<root>hello</root>"));
}

#[test]
fn removes_sql_comments_without_touching_strings() {
    let source = r#"-- remove me
SELECT '-- not a comment' as val;
"#;

    let processed = process_language(source, LanguageConfig::sql());
    assert!(!processed.contains("-- remove me"));
    assert!(processed.contains("'-- not a comment'"));
}

#[test]
fn removes_kotlin_comments_without_touching_strings() {
    let source = r#"// remove me
fun main() { val s = "// not a comment" }
"#;

    let processed = process_language(source, LanguageConfig::kotlin());
    assert!(!processed.contains("// remove me"));
    assert!(processed.contains("\"// not a comment\""));
}

#[test]
fn removes_swift_comments_without_touching_strings() {
    let source = r#"// remove me
let s = "// not a comment"
"#;

    let processed = process_language(source, LanguageConfig::swift());
    assert!(!processed.contains("// remove me"));
    assert!(processed.contains("\"// not a comment\""));
}

#[test]
fn removes_objc_comments_without_touching_strings() {
    let source = r#"// remove me
NSString *s = @"// not a comment";
"#;

    let processed = process_language(source, LanguageConfig::objc());
    assert!(!processed.contains("// remove me"));
    assert!(processed.contains("@\"// not a comment\""));
}

#[test]
fn preserves_objc_preprocessor_trailing_comments() {
    let source = r#"#import "Local.h" // fallback
#define kTimeout 30 // seconds
// remove me
NSString *s = @"// not a comment";
"#;

    let processed = process_language(source, LanguageConfig::objc());
    assert!(processed.contains("#import \"Local.h\" // fallback"));
    assert!(processed.contains("#define kTimeout 30 // seconds"));
    assert!(!processed.contains("remove me"));
    assert!(processed.contains("@\"// not a comment\""));
}

#[test]
fn removes_lua_comments_without_touching_strings() {
    let source = r#"-- remove me
local s = "-- not a comment"
"#;

    let processed = process_language(source, LanguageConfig::lua());
    assert!(!processed.contains("-- remove me"));
    assert!(processed.contains("\"-- not a comment\""));
}

#[test]
fn removes_nix_comments_without_touching_strings() {
    let source = r##"# remove me
let s = "# not a comment"; in s
"##;

    let processed = process_language(source, LanguageConfig::nix());
    assert!(!processed.contains("# remove me"));
    assert!(processed.contains("\"# not a comment\""));
}

#[test]
fn removes_powershell_comments_without_touching_strings() {
    let source = r##"# remove me
$s = "# not a comment"
Write-Output $s
"##;

    let processed = process_language(source, LanguageConfig::powershell());
    assert!(!processed.contains("# remove me"));
    assert!(processed.contains("\"# not a comment\""));
}

#[test]
fn removes_proto_comments_without_touching_strings() {
    let source = r#"// remove me
syntax = "proto3";
message A { string s = 1 [default = "// not a comment"]; }
"#;

    let processed = process_language(source, LanguageConfig::proto());
    assert!(!processed.contains("// remove me"));
    assert!(processed.contains("\"// not a comment\""));
}

#[test]
fn removes_ini_comments_without_touching_values() {
    let source = r#"; remove me
[section]
key = # not a comment
"#;

    let processed = process_language(source, LanguageConfig::ini());
    assert!(!processed.contains("; remove me"));
    assert!(processed.contains("key = # not a comment"));
}

#[test]
fn removes_python_docstrings_when_remove_docs_enabled() {
    let source = r#""""This is a docstring"""
# TODO: regular todo
# mypy: ignore
def hello(): pass"#;

    let mut processor = Processor::new();
    let language_config = LanguageConfig::python();
    let mut resolved_config = default_resolved_config();
    resolved_config.remove_docs = true;

    let ProcessOutcome { content: output, .. } = processor
        .process_content_with_config(source, &language_config, &resolved_config)
        .expect("processing python source");

    assert!(
        !output.contains("This is a docstring"),
        "Python docstring should be removed when remove_docs=true"
    );
    assert!(output.contains("TODO: regular todo"), "TODO should be preserved");
    assert!(output.contains("mypy: ignore"), "mypy should be preserved");
}

#[test]
fn handles_utf8_multibyte_in_comments() {
    let source = "// Comment with emoji 🎉\nfn main() {}\n";

    let processed = process_rust(source);
    assert!(!processed.contains("🎉"));
    assert!(processed.contains("fn main()"));
}

#[test]
fn handles_file_with_only_comments() {
    let source = "// Only comments\n// Nothing else\n";

    let processed = process_rust(source);
    assert!(processed.trim().is_empty());
}

#[test]
fn handles_empty_file() {
    let source = "";

    let mut processor = Processor::new();
    let language_config = LanguageConfig::rust();
    let resolved_config = default_resolved_config();
    let outcome = processor
        .process_content_with_config(source, &language_config, &resolved_config)
        .expect("processing empty source");
    assert_eq!(outcome.content, "");
    assert_eq!(outcome.removed_comments.len(), 0);
}

#[test]
fn handles_comment_at_end_of_file_no_trailing_newline() {
    let source = "fn main() {} // trailing";

    let processed = process_rust(source);
    assert!(!processed.contains("// trailing"));
    assert!(processed.contains("fn main()"));
}

/// Sources covering the comment shapes that behave differently from each other:
/// Rust `///` and `//!` (recorded as nested node pairs) plus a `/** */` block,
/// Python docstrings (`string` nodes, not comments), JSDoc, and a `#`-comment
/// language with a shebang.
fn inventory_cases() -> Vec<(&'static str, LanguageConfig, &'static str)> {
    vec![
        (
            "sample.rs",
            LanguageConfig::rust(),
            "//! Crate entry point.\n\n/// Documented.\npub fn a() {}\n\n/** Block doc. */\npub fn b() {}\n\n// plain removable\npub fn c() {\n    let x = 1; // trailing removable\n    /* block\n       removable */\n}\n",
        ),
        (
            "module.py",
            LanguageConfig::python(),
            "\"\"\"Module docstring.\"\"\"\n\n\ndef f():\n    \"\"\"Function docstring.\"\"\"\n    # remove me\n    return 1  # trailing removable\n",
        ),
        (
            "app.js",
            LanguageConfig::javascript(),
            "/** JSDoc summary. */\nfunction f() {\n  // remove me\n  return 1; /* block removable */\n}\n",
        ),
        (
            "script.sh",
            LanguageConfig::shell(),
            "#!/usr/bin/env bash\n# remove me\necho \"ok\"  # trailing removable\n",
        ),
    ]
}

/// Apply a removal plan to `source` the way a host tool would, so the plan can be
/// compared against what the rewriting path actually produces.
fn apply_removals(source: &str, removals: &[Removal]) -> String {
    let merged = merge_ranges(
        &removals
            .iter()
            .map(|removal| (removal.remove_start, removal.remove_end))
            .collect::<Vec<_>>(),
    );
    let mut output = String::with_capacity(source.len());
    let mut cursor = 0;
    for (start, end) in merged {
        if cursor < start {
            output.push_str(&source[cursor..start]);
        }
        cursor = cursor.max(end);
    }
    output.push_str(&source[cursor..]);
    output
}

#[test]
fn plan_removals_is_inspect_filtered_to_remove_verdicts() {
    for (path, _, source) in inventory_cases() {
        let mut processor = Processor::new();
        let config = default_resolved_config();

        let expected: Vec<Removal> = processor
            .inspect(source, Path::new(path), &config)
            .expect("inspect")
            .into_iter()
            .filter_map(|comment| match comment.verdict {
                Verdict::Remove {
                    expanded_start,
                    expanded_end,
                } => Some(Removal {
                    comment_start: comment.start_byte,
                    comment_end: comment.end_byte,
                    remove_start: expanded_start,
                    remove_end: expanded_end,
                    start_row: comment.start_row,
                    is_documentation: comment.is_documentation,
                    preview: first_line_preview(&comment.text),
                }),
                Verdict::Preserve => None,
            })
            .collect();

        let removals = processor
            .plan_removals(source, Path::new(path), &config)
            .expect("plan removals");
        assert_eq!(removals, expected, "plan_removals diverged from inspect for {path}");
        assert!(
            removals.len() >= 2,
            "{path} must exercise several removals, got {removals:?}"
        );
    }
}

#[test]
fn inspect_removals_reproduce_the_rewritten_source() {
    for (path, language_config, source) in inventory_cases() {
        let mut processor = Processor::new();
        let removals = processor
            .plan_removals(source, Path::new(path), &default_resolved_config())
            .expect("plan removals");
        assert_eq!(
            apply_removals(source, &removals),
            process_language(source, language_config),
            "the inventory's removals disagree with the rewriting path for {path}"
        );
    }
}

#[test]
fn inspect_is_ordered_and_every_preserved_comment_names_a_reason() {
    for (path, _, source) in inventory_cases() {
        let mut processor = Processor::new();
        let inspected = processor
            .inspect(source, Path::new(path), &default_resolved_config())
            .expect("inspect");

        assert!(
            inspected
                .windows(2)
                .all(|pair| pair[0].start_byte <= pair[1].start_byte),
            "{path} is not ordered by start_byte: {inspected:?}"
        );
        assert!(
            inspected.iter().any(|comment| comment.verdict == Verdict::Preserve),
            "{path} must exercise at least one preserved comment"
        );
        for comment in &inspected {
            match comment.verdict {
                Verdict::Preserve => assert!(
                    comment.reason.is_some(),
                    "preserved comment has no reason in {path}: {comment:?}"
                ),
                Verdict::Remove { .. } => assert!(
                    comment.reason.is_none(),
                    "removed comment carries a reason in {path}: {comment:?}"
                ),
            }
            assert_eq!(
                comment.text,
                &source[comment.start_byte..comment.end_byte],
                "text does not match its byte range in {path}"
            );
        }
    }
}

#[test]
fn inspect_reports_one_entry_per_comment_for_nested_node_pairs() {
    // Rust records each `///` line twice, as the outer `line_comment` and as the
    // inner doc node; the inventory is per comment, not per node.
    let source = "/// Doc one.\npub fn a() {}\n\n/// Doc two.\npub fn b() {}\n";
    let mut processor = Processor::new();
    let inspected = processor
        .inspect(source, Path::new("sample.rs"), &default_resolved_config())
        .expect("inspect");
    assert_eq!(inspected.len(), 2, "two doc comments, not four nodes: {inspected:?}");
    assert!(
        inspected
            .iter()
            .all(|comment| comment.kind == CommentKind::Doc && comment.reason == Some(PreserveReason::Documentation))
    );
}

#[test]
fn inspect_classifies_comment_shapes() {
    let source = "//! Crate docs.\n\n/// Documented.\npub fn a() {}\n\n/** Block doc. */\npub fn b() {}\n\n// plain\npub fn c() {}\n\n/* block\n   comment */\npub fn d() {}\n";
    let mut processor = Processor::new();
    let inspected = processor
        .inspect(source, Path::new("sample.rs"), &default_resolved_config())
        .expect("inspect");
    let shapes: Vec<(CommentKind, &str)> = inspected
        .iter()
        .map(|comment| (comment.kind, comment.text.lines().next().unwrap_or_default()))
        .collect();
    assert_eq!(
        shapes,
        vec![
            (CommentKind::Doc, "//! Crate docs."),
            (CommentKind::Doc, "/// Documented."),
            (CommentKind::Doc, "/** Block doc. */"),
            (CommentKind::Line, "// plain"),
            (CommentKind::Block, "/* block"),
        ]
    );

    let python = "\"\"\"Module docstring.\"\"\"\n# plain\n";
    let inspected = processor
        .inspect(python, Path::new("module.py"), &default_resolved_config())
        .expect("inspect python");
    assert_eq!(inspected[0].kind, CommentKind::Docstring);
    assert_eq!(inspected[1].kind, CommentKind::Line);
}

#[test]
fn above_line_marker_separates_the_marker_from_what_it_extends_over() {
    let source = "// ~keep\n/// Parent element ID.\npub fn a() {}\n";
    let config = ResolvedConfig {
        remove_docs: true,
        ..default_resolved_config()
    };
    let mut processor = Processor::new();
    let inspected = processor
        .inspect(source, Path::new("sample.rs"), &config)
        .expect("inspect");

    let reasons: Vec<(&str, Option<&PreserveReason>)> = inspected
        .iter()
        .map(|comment| (comment.text.lines().next().unwrap_or_default(), comment.reason.as_ref()))
        .collect();
    assert_eq!(
        reasons,
        vec![
            ("// ~keep", Some(&PreserveReason::KeepMarker)),
            (
                "/// Parent element ID.",
                Some(&PreserveReason::ExtendedByNeighbourMarker)
            ),
        ]
    );
}

#[test]
fn keep_block_members_are_attributed_to_the_marker_they_borrow() {
    // The `// TODO` line has a reason of its own even though the marker also covers
    // it, so `keep` must not read it as marker-dependent.
    let source = "fn f() {\n    // TODO: later\n    // context line\n    // rationale ~keep\n    let x = 1;\n}\n";
    let mut processor = Processor::new();
    let inspected = processor
        .inspect(source, Path::new("sample.rs"), &default_resolved_config())
        .expect("inspect");

    let reasons: Vec<Option<&PreserveReason>> = inspected.iter().map(|comment| comment.reason.as_ref()).collect();
    assert_eq!(
        reasons,
        vec![
            Some(&PreserveReason::Pattern("TODO".to_string())),
            Some(&PreserveReason::ExtendedByNeighbourMarker),
            Some(&PreserveReason::KeepMarker),
        ]
    );
}

#[test]
fn inspect_names_shebang_and_pattern_reasons() {
    let source = "#!/usr/bin/env bash\n# NOTE: load bearing\n# remove me\necho ok\n";
    let mut processor = Processor::new();
    let inspected = processor
        .inspect(source, Path::new("script.sh"), &default_resolved_config())
        .expect("inspect");
    assert_eq!(inspected[0].reason, Some(PreserveReason::Shebang));
    assert_eq!(inspected[1].reason, Some(PreserveReason::Pattern("NOTE".to_string())));
    assert!(matches!(inspected[2].verdict, Verdict::Remove { .. }));
}

#[test]
fn grammar_forced_preservation_is_named_a_language_directive() {
    // The trailing `/* GUARD */` survives because the C handler recognises a
    // comment trailing a preprocessor line, not because any pattern matched it.
    let source = "#ifndef GUARD\n#define GUARD\n// remove me\nint x;\n#endif  /* GUARD */\n";
    let mut processor = Processor::new();
    let inspected = processor
        .inspect(source, Path::new("header.h"), &default_resolved_config())
        .expect("inspect");
    let guard = inspected
        .iter()
        .find(|comment| comment.text.contains("GUARD */"))
        .expect("guard comment inspected");
    assert_eq!(guard.reason, Some(PreserveReason::LanguageDirective));
    assert_eq!(guard.verdict, Verdict::Preserve);
}

#[test]
fn inspect_expanded_range_swallows_a_standalone_comment_line() {
    let source = "// standalone\nfn main() {\n    let x = 1; // trailing\n}\n";
    let mut processor = Processor::new();
    let inspected = processor
        .inspect(source, Path::new("sample.rs"), &default_resolved_config())
        .expect("inspect");
    let ranges: Vec<&str> = inspected
        .iter()
        .filter_map(|comment| match comment.verdict {
            Verdict::Remove {
                expanded_start,
                expanded_end,
            } => Some(&source[expanded_start..expanded_end]),
            Verdict::Preserve => None,
        })
        .collect();
    assert_eq!(ranges, vec!["// standalone\n", " // trailing"]);
}

#[test]
fn inspect_unsupported_extension_errors() {
    let mut processor = Processor::new();
    let result = processor.inspect("noop", Path::new("file.unknownext"), &default_resolved_config());
    assert!(result.is_err());
}