uncomment 3.8.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
//! `uncomment scan`: a machine-readable inventory of every comment in a tree.
//!
//! The inventory is read-only — no source file is opened for writing — and it is produced by the
//! same engine a real run uses ([`Processor::inspect`], of which the removal planner is a filter),
//! so a `remove` verdict here is a comment the equivalent `uncomment` invocation would strip. The
//! comment-selection flags are shared with that run for the same reason: `--remove-doc`,
//! `--ignore`, `-c` and the rest change verdicts exactly as they change behaviour.
//!
//! # Output
//!
//! `--format jsonl` (the default) emits one object per line:
//!
//! ```json
//! {"id":"a3f9c1d2ab","path":"dev_tools/acli/cli.py","line":42,"end_line":42,
//!  "start_byte":1180,"end_byte":1203,"kind":"line","verdict":"remove","text":"# legacy shim"}
//! ```
//!
//! `line` and `end_line` are 1-based; `start_byte`/`end_byte` are 0-based offsets into the file as
//! it was read. `path` is repo-relative with `/` separators (falling back to a path relative to the
//! working directory outside a repository), so a decisions file is portable across checkouts.
//!
//! A preserved record carries why it survived. [`PreserveReason`] is encoded externally tagged with
//! snake-case names: every reason but one is a bare string — `"reason":"documentation"`,
//! `"file_header"`, `"shebang"`, `"keep_marker"`, `"extended_by_neighbour_marker"`,
//! `"language_directive"` — and the pattern variant carries its pattern as
//! `"reason":{"pattern":"TODO"}`.
//!
//! `--format json` wraps the same objects in an array, `--format text` prints
//! `path:line  [verdict]  text`.
//!
//! # Identifiers
//!
//! Ids come from [`super::id`], which deliberately hashes path, exact bytes and occurrence index
//! and not location, so an id survives edits above the comment. Truncation to 10 hex characters is
//! fallible at scale, so collisions are resolved rather than ignored:
//!
//! - Within one file, if two comments share a short id, *every* record carrying it is widened to
//!   the 32-hex [`full_id`]. `keep` re-derives ids per file, so an intra-file collision is the one
//!   that could make it mark the wrong comment.
//! - Across files, the first claimant keeps the short id and every later one is widened. Each id in
//!   a report is therefore unique, which is what a consumer needs, without buffering the whole tree
//!   to discover it. Both cases are noted on stderr.
//!
//! # Scale
//!
//! Files are parsed in parallel in bounded batches and records are written as each batch completes,
//! so `jsonl`, `json` and `text` hold one batch of comments at a time rather than the whole tree.
//! `--group-identical` is the exception: collapsing byte-different but equivalent comments onto one
//! decision is inherently whole-tree, and accumulates one entry per distinct normalized text. The
//! one thing every format retains is the set of ids already emitted, at ten bytes a comment, which
//! is what makes the cross-file collision check possible without holding the comments themselves.

use std::collections::{BTreeMap, HashSet};
use std::fs::File;
use std::io::{BufWriter, Write};
use std::path::{Path, PathBuf};

use anyhow::{Context, Result, bail};
use rayon::prelude::*;
use serde::Serialize;

use crate::config::{Config, ConfigManager, ResolvedConfig};
use crate::languages::LanguageRegistry;
use crate::languages::registry::warn_languages_without_a_grammar;
use crate::paths::{absolute_normalized, find_repo_root, repo_relative, to_slash};
use crate::processor::{CommentKind, PreserveReason, ProcessingOptions, Processor, Verdict};
use crate::ui;

use super::id::{ambiguous_ids, assign_occurrence_indices, comment_id, full_id, group_id, normalize_comment_text};

/// Sites listed per group before the list is capped and marked `truncated`.
const SITE_CAP: usize = 50;

/// Files parsed per batch. Bounds how many comments are held in memory at once while keeping
/// batches large enough that the thread pool stays busy.
const BATCH_FILES: usize = 256;

#[derive(Copy, Clone, Debug, PartialEq, Eq, clap::ValueEnum)]
pub enum ScanFormat {
    /// One JSON object per line.
    Jsonl,
    /// A single JSON array.
    Json,
    /// `path:line  [verdict]  text`, for humans.
    Text,
}

#[derive(Copy, Clone, Debug, PartialEq, Eq, clap::ValueEnum)]
pub enum ScanSelection {
    /// Only comments a real run would strip.
    Removable,
    /// Only comments a real run would keep.
    Preserved,
    All,
}

#[derive(clap::Args, Debug)]
pub struct ScanArgs {
    #[command(flatten)]
    pub process: crate::cli::ProcessArgs,

    /// Output format
    #[arg(
        long,
        value_name = "FORMAT",
        value_enum,
        default_value_t = ScanFormat::Jsonl,
        help = "Report format: jsonl (default), json, or text",
        help_heading = "Inventory"
    )]
    pub format: ScanFormat,

    /// Which verdicts to report
    #[arg(
        long,
        value_name = "WHICH",
        value_enum,
        default_value_t = ScanSelection::All,
        help = "Report only removable comments, only preserved ones, or all",
        help_heading = "Inventory"
    )]
    pub only: ScanSelection,

    /// Collapse comments whose normalized text is identical
    #[arg(
        long = "group-identical",
        help = "Collapse equivalent comments into one record with a site list",
        help_heading = "Inventory"
    )]
    pub group_identical: bool,

    /// Write the report to a file instead of stdout
    #[arg(
        short = 'o',
        long = "output",
        value_name = "FILE",
        help = "Write the report to FILE instead of stdout",
        help_heading = "Inventory"
    )]
    pub output: Option<PathBuf>,
}

/// Produce the inventory for `args`, writing it to stdout or to `--output`.
///
/// # Errors
///
/// Fails when no paths were given, when configuration cannot be loaded, when the thread pool
/// cannot be built, or when the report cannot be written. A file that cannot be read, decoded or
/// parsed is reported on stderr and skipped, exactly as in a real run.
pub fn run(args: &ScanArgs) -> Result<()> {
    if args.process.paths.is_empty() {
        bail!("No input paths specified. Pass a file, directory or glob to scan.");
    }

    let options = args.process.processing_options();
    let current_dir = std::env::current_dir().context("Failed to get current directory")?;
    let mut config_manager = build_config_manager(args, &current_dir)?;

    // One config-aware registry for both collection and inspection, matching a real run: the
    // extensions a `[languages]` section declares decide what is collected, not only how it is read.
    config_manager.discover_language_sources(&args.process.paths, options.respect_gitignore);
    let mut registry = LanguageRegistry::new();
    warn_languages_without_a_grammar(&registry.register_configured_languages(&config_manager.get_all_languages()));

    let files = collect_files(&args.process.paths, &options, &registry)?;

    let num_threads = if args.process.threads == 0 {
        num_cpus::get()
    } else {
        args.process.threads
    };
    rayon::ThreadPoolBuilder::new()
        .num_threads(num_threads)
        .build_global()
        .context("Failed to initialize thread pool")?;

    let repo_root = find_repo_root(&current_dir);
    let context = ScanContext {
        config_manager: &config_manager,
        registry: &registry,
        options: &options,
        current_dir: &current_dir,
        repo_root: repo_root.as_deref(),
        selection: args.only,
        grouping: args.group_identical,
    };

    let writer: Box<dyn Write> = match &args.output {
        Some(path) => {
            Box::new(BufWriter::new(File::create(path).with_context(|| {
                format!("Failed to create report file: {}", path.display())
            })?))
        }
        None => Box::new(BufWriter::new(std::io::stdout())),
    };
    let mut emitter = Emitter::new(writer, args.format);

    let mut totals = Totals::default();
    let mut seen_ids: HashSet<String> = HashSet::new();
    let mut groups: BTreeMap<String, Group> = BTreeMap::new();

    for batch in files.chunks(BATCH_FILES) {
        let scanned: Vec<FileScan> = if num_threads == 1 {
            batch.iter().map(|file| context.scan_file(file)).collect()
        } else {
            batch.par_iter().map(|file| context.scan_file(file)).collect()
        };

        for mut scan in scanned {
            if let Some(message) = scan.skipped {
                totals.skipped += 1;
                anstream::eprintln!("{} skipping {}: {message}", ui::warn("warning:"), ui::path(&scan.file));
                continue;
            }
            totals.files += 1;

            for widened in resolve_collisions(&mut scan.records, &mut seen_ids) {
                anstream::eprintln!(
                    "{} id {} is claimed by more than one comment; {}:{} is reported under its full identifier",
                    ui::warn("warning:"),
                    ui::accent(&widened.short),
                    widened.path,
                    widened.line
                );
            }

            for record in scan.records {
                totals.count(record.verdict);
                if context.grouping {
                    accumulate(&mut groups, record);
                } else {
                    emitter.emit(&record, || record.text_line())?;
                }
            }
        }
    }

    if context.grouping {
        emit_groups(&mut emitter, groups, &mut totals)?;
    }

    emitter.finish()?;

    if !args.process.quiet {
        report_totals(&totals, context.grouping);
    }

    Ok(())
}

/// Counts for the closing summary. Written to stderr so stdout carries only the report.
#[derive(Debug, Default)]
struct Totals {
    files: usize,
    skipped: usize,
    removable: usize,
    preserved: usize,
    groups: usize,
}

impl Totals {
    fn count(&mut self, verdict: RecordVerdict) {
        match verdict {
            RecordVerdict::Remove => self.removable += 1,
            RecordVerdict::Preserve => self.preserved += 1,
        }
    }
}

/// One record reported under its full identifier because another already claimed the short one.
#[derive(Debug, PartialEq, Eq)]
struct Widened {
    short: String,
    path: String,
    line: usize,
}

/// Leave every record in `records` under an identifier no other record in the report claims.
///
/// `seen` accumulates the short ids of the records already emitted. The first claimant of a short id
/// keeps it and each later one is widened, so a consumer can resolve any id in the report to exactly
/// one comment. A record already widened by the intra-file check is left alone and does not reserve
/// its short form, which no record is using.
fn resolve_collisions(records: &mut [Record], seen: &mut HashSet<String>) -> Vec<Widened> {
    let mut widened = Vec::new();

    for record in records {
        if record.id.len() == FULL_ID_HEX {
            continue;
        }
        if !seen.insert(record.id.clone()) {
            widened.push(Widened {
                short: std::mem::replace(&mut record.id, record.full_id.clone()),
                path: record.path.clone(),
                line: record.line,
            });
        }
    }

    widened
}

fn report_totals(totals: &Totals, grouping: bool) {
    let reported = totals.removable + totals.preserved;
    anstream::eprintln!(
        "{} {}",
        ui::dim(ui::BULLET),
        ui::dim(format!(
            "Scanned {} files, {reported} comments reported ({} removable, {} preserved){}{}",
            totals.files,
            totals.removable,
            totals.preserved,
            if grouping {
                format!(", {} groups", totals.groups)
            } else {
                String::new()
            },
            if totals.skipped > 0 {
                format!("; {} file(s) skipped", totals.skipped)
            } else {
                String::new()
            },
        ))
    );
}

/// Length of a [`full_id`] in hex characters, used to recognise an already-widened id.
const FULL_ID_HEX: usize = super::id::FULL_ID_BYTES * 2;

#[derive(Copy, Clone, Debug, PartialEq, Eq)]
enum RecordVerdict {
    Remove,
    Preserve,
}

impl RecordVerdict {
    fn as_str(self) -> &'static str {
        match self {
            RecordVerdict::Remove => "remove",
            RecordVerdict::Preserve => "preserve",
        }
    }
}

impl Serialize for RecordVerdict {
    fn serialize<S: serde::Serializer>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error> {
        serializer.serialize_str(self.as_str())
    }
}

/// Why a preserved comment survived, in the JSON encoding documented at the module level.
#[derive(Debug, Serialize)]
#[serde(rename_all = "snake_case")]
enum Reason {
    Pattern(String),
    Documentation,
    FileHeader,
    Shebang,
    KeepMarker,
    ExtendedByNeighbourMarker,
    LanguageDirective,
}

impl From<&PreserveReason> for Reason {
    fn from(reason: &PreserveReason) -> Self {
        match reason {
            PreserveReason::Pattern(pattern) => Reason::Pattern(pattern.clone()),
            PreserveReason::Documentation => Reason::Documentation,
            PreserveReason::FileHeader => Reason::FileHeader,
            PreserveReason::Shebang => Reason::Shebang,
            PreserveReason::KeepMarker => Reason::KeepMarker,
            PreserveReason::ExtendedByNeighbourMarker => Reason::ExtendedByNeighbourMarker,
            PreserveReason::LanguageDirective => Reason::LanguageDirective,
        }
    }
}

/// One comment in the report.
#[derive(Debug, Serialize)]
struct Record {
    id: String,
    path: String,
    line: usize,
    end_line: usize,
    start_byte: usize,
    end_byte: usize,
    kind: &'static str,
    verdict: RecordVerdict,
    text: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    reason: Option<Reason>,

    /// The widened form, carried so a collision found later needs no re-derivation. Not part of the
    /// report.
    #[serde(skip)]
    full_id: String,
}

impl Record {
    fn text_line(&self) -> String {
        format!(
            "{}:{}  [{}]  {}",
            self.path,
            self.line,
            self.verdict.as_str(),
            single_line(&self.text)
        )
    }
}

/// The outcome of one file: either records, or the reason it was skipped.
struct FileScan {
    file: PathBuf,
    records: Vec<Record>,
    skipped: Option<String>,
}

struct ScanContext<'a> {
    config_manager: &'a ConfigManager,
    registry: &'a LanguageRegistry,
    options: &'a ProcessingOptions,
    current_dir: &'a Path,
    repo_root: Option<&'a Path>,
    selection: ScanSelection,
    grouping: bool,
}

impl ScanContext<'_> {
    fn scan_file(&self, file: &Path) -> FileScan {
        let skip = |message: String| FileScan {
            file: file.to_path_buf(),
            records: Vec::new(),
            skipped: Some(message),
        };

        let content = match std::fs::read_to_string(file) {
            Ok(content) => content,
            Err(error) => return skip(error.to_string()),
        };

        let language = self
            .registry
            .detect_language(file)
            .map(|language| language.name.to_lowercase());
        let config = self.resolved_config(file, language.as_deref());

        let mut processor = Processor::new_with_config(self.config_manager);
        let inspected = match processor.inspect(&content, file, &config) {
            Ok(inspected) => inspected,
            Err(error) => return skip(format!("{error:#}")),
        };

        let path = self.display_path(file);
        let texts: Vec<&str> = inspected.iter().map(|comment| comment.text.as_str()).collect();
        let occurrences = assign_occurrence_indices(&texts);
        let ids: Vec<String> = texts
            .iter()
            .zip(&occurrences)
            .map(|(text, &occurrence)| comment_id(&path, text, occurrence))
            .collect();
        // Every comment in the file participates, not just the reported ones: `keep` re-derives ids
        // over the whole file, so a short id shared with a filtered-out comment is still ambiguous.
        let ambiguous = ambiguous_ids(&ids);

        let mut records = Vec::new();
        for ((comment, id), occurrence) in inspected.iter().zip(ids).zip(occurrences) {
            let verdict = match comment.verdict {
                Verdict::Remove { .. } => RecordVerdict::Remove,
                Verdict::Preserve => RecordVerdict::Preserve,
            };
            if !self.selects(verdict) {
                continue;
            }

            let wide = full_id(&path, &comment.text, occurrence);
            let id = if ambiguous.contains(&id) { wide.clone() } else { id };
            records.push(Record {
                id,
                path: path.clone(),
                line: comment.start_row + 1,
                end_line: comment.end_row + 1,
                start_byte: comment.start_byte,
                end_byte: comment.end_byte,
                kind: kind_name(comment.kind),
                verdict,
                text: comment.text.clone(),
                reason: comment.reason.as_ref().map(Reason::from),
                full_id: wide,
            });
        }

        FileScan {
            file: file.to_path_buf(),
            records,
            skipped: None,
        }
    }

    fn selects(&self, verdict: RecordVerdict) -> bool {
        match self.selection {
            ScanSelection::All => true,
            ScanSelection::Removable => verdict == RecordVerdict::Remove,
            ScanSelection::Preserved => verdict == RecordVerdict::Preserve,
        }
    }

    /// Repo-relative with `/` separators, so an id and a site survive being carried to another
    /// checkout. Outside a repository the working directory stands in for the root; a file under
    /// neither is reported by its absolute path rather than silently losing components.
    fn display_path(&self, file: &Path) -> String {
        let absolute = absolute_normalized(self.current_dir, file);
        let relative = self
            .repo_root
            .and_then(|root| repo_relative(root, &absolute))
            .or_else(|| repo_relative(self.current_dir, &absolute));
        to_slash(relative.as_deref().unwrap_or(&absolute))
    }

    /// Resolve `file`'s configuration the way a real run does.
    ///
    /// [`Processor::inspect`] takes an already-resolved config and performs no discovery, so the
    /// CLI overrides are applied here instead. They are applied one-directionally, mirroring
    /// `Processor::process_file_with_config`: an unset flag must never clobber a config-file value.
    fn resolved_config(&self, file: &Path, language: Option<&str>) -> ResolvedConfig {
        let mut config = match language {
            Some(language) => self.config_manager.get_config_for_file_with_language(file, language),
            None => self.config_manager.get_config_for_file(file),
        };

        if self.options.remove_doc {
            config.remove_docs = true;
        }
        if !self.options.use_default_ignores {
            config.use_default_ignores = false;
        }
        if self.options.remove_todo {
            config.remove_todos = true;
        }
        if self.options.remove_fixme {
            config.remove_fixme = true;
        }
        if !self.options.custom_preserve_patterns.is_empty() {
            config
                .preserve_patterns
                .extend(self.options.custom_preserve_patterns.iter().cloned());
        }
        if !self.options.respect_gitignore {
            config.respect_gitignore = false;
        }
        if self.options.traverse_git_repos {
            config.traverse_git_repos = true;
        }

        config
    }
}

fn kind_name(kind: CommentKind) -> &'static str {
    match kind {
        CommentKind::Line => "line",
        CommentKind::Block => "block",
        CommentKind::Doc => "doc",
        CommentKind::Docstring => "docstring",
    }
}

/// One decision covering every site whose comment normalizes to the same text.
#[derive(Debug)]
struct Group {
    id: String,
    text: String,
    count: usize,
    removable: usize,
    preserved: usize,
    sites: Vec<String>,
    truncated: bool,
}

#[derive(Debug, Serialize)]
struct GroupRecord<'a> {
    id: &'a str,
    text: &'a str,
    count: usize,
    sites: &'a [String],
    #[serde(skip_serializing_if = "is_false")]
    truncated: bool,
    verdict: &'static str,
    /// Present only for a `mixed` group, where one decision cannot speak for every site.
    #[serde(skip_serializing_if = "Option::is_none")]
    removable: Option<usize>,
    #[serde(skip_serializing_if = "Option::is_none")]
    preserved: Option<usize>,
    /// Two different normalized texts produced the same group id; a consumer must not treat this id
    /// as naming one decision.
    #[serde(skip_serializing_if = "is_false")]
    ambiguous: bool,
}

fn is_false(value: &bool) -> bool {
    !*value
}

fn accumulate(groups: &mut BTreeMap<String, Group>, record: Record) {
    let normalized = normalize_comment_text(&record.text);
    let group = groups.entry(normalized).or_insert_with_key(|normalized| Group {
        id: group_id(normalized),
        text: record.text.clone(),
        count: 0,
        removable: 0,
        preserved: 0,
        sites: Vec::new(),
        truncated: false,
    });

    group.count += 1;
    match record.verdict {
        RecordVerdict::Remove => group.removable += 1,
        RecordVerdict::Preserve => group.preserved += 1,
    }
    if group.sites.len() < SITE_CAP {
        group.sites.push(format!("{}:{}", record.path, record.line));
    } else {
        group.truncated = true;
    }
}

fn emit_groups(emitter: &mut Emitter, groups: BTreeMap<String, Group>, totals: &mut Totals) -> Result<()> {
    // Collected from a BTreeMap and then totally ordered, so the sequence is the same on every run:
    // the count and id decide, and the normalized text breaks a tie between two colliding ids.
    let mut ordered: Vec<(String, Group)> = groups.into_iter().collect();
    ordered.sort_by(|(left_key, left), (right_key, right)| {
        right
            .count
            .cmp(&left.count)
            .then_with(|| left.id.cmp(&right.id))
            .then_with(|| left_key.cmp(right_key))
    });
    totals.groups = ordered.len();

    let ids: Vec<&str> = ordered.iter().map(|(_, group)| group.id.as_str()).collect();
    let ambiguous = ambiguous_ids(&ids);
    for id in &ambiguous {
        anstream::eprintln!(
            "{} group id {} covers more than one distinct comment text",
            ui::warn("warning:"),
            ui::accent(id)
        );
    }

    for (_, group) in &ordered {
        let mixed = group.removable > 0 && group.preserved > 0;
        let verdict = if mixed {
            "mixed"
        } else if group.removable > 0 {
            "remove"
        } else {
            "preserve"
        };
        let record = GroupRecord {
            id: &group.id,
            text: &group.text,
            count: group.count,
            sites: &group.sites,
            truncated: group.truncated,
            verdict,
            removable: mixed.then_some(group.removable),
            preserved: mixed.then_some(group.preserved),
            ambiguous: ambiguous.iter().any(|id| id == &group.id),
        };
        emitter.emit(&record, || {
            format!("{:>7}x  [{verdict}]  {}", group.count, single_line(&group.text))
        })?;
    }

    Ok(())
}

/// Writes records in the chosen format, one at a time.
struct Emitter {
    writer: Box<dyn Write>,
    format: ScanFormat,
    written: usize,
}

impl Emitter {
    fn new(writer: Box<dyn Write>, format: ScanFormat) -> Self {
        Self {
            writer,
            format,
            written: 0,
        }
    }

    fn emit(&mut self, value: &impl Serialize, text_line: impl FnOnce() -> String) -> Result<()> {
        match self.format {
            ScanFormat::Text => writeln!(self.writer, "{}", text_line())?,
            ScanFormat::Jsonl => {
                serde_json::to_writer(&mut self.writer, value).context("Failed to serialize record")?;
                writeln!(self.writer)?;
            }
            ScanFormat::Json => {
                if self.written == 0 {
                    writeln!(self.writer, "[")?;
                } else {
                    writeln!(self.writer, ",")?;
                }
                serde_json::to_writer(&mut self.writer, value).context("Failed to serialize record")?;
            }
        }
        self.written += 1;
        Ok(())
    }

    fn finish(&mut self) -> Result<()> {
        if self.format == ScanFormat::Json {
            if self.written == 0 {
                writeln!(self.writer, "[]")?;
            } else {
                writeln!(self.writer)?;
                writeln!(self.writer, "]")?;
            }
        }
        self.writer.flush().context("Failed to write the report")
    }
}

/// Collapse a comment onto one line for the human-facing format, which promises one record per
/// line and must not be broken by a block comment.
fn single_line(text: &str) -> String {
    let mut out = String::with_capacity(text.len());
    for (index, line) in text.lines().enumerate() {
        if index > 0 {
            out.push(' ');
        }
        out.push_str(line.trim_end());
    }
    out
}

fn build_config_manager(args: &ScanArgs, current_dir: &Path) -> Result<ConfigManager> {
    match &args.process.config {
        Some(path) => {
            let config =
                Config::from_file(path).with_context(|| format!("Failed to load config file: {}", path.display()))?;
            ConfigManager::from_single_config(current_dir, config)
        }
        None => ConfigManager::new(current_dir).context("Failed to initialize configuration manager"),
    }
}

/// The supported files named by `paths`, sorted and deduplicated.
///
/// Mirrors the default run's collection — gitignore-aware directory walks, globs otherwise — so
/// the inventory covers exactly the files that run would have processed.
fn collect_files(paths: &[String], options: &ProcessingOptions, registry: &LanguageRegistry) -> Result<Vec<PathBuf>> {
    let mut files = Vec::new();

    for pattern in paths {
        let path = Path::new(pattern);

        if path.is_file() {
            if registry.detect_language(path).is_some() {
                files.push(path.to_path_buf());
            }
        } else if path.is_dir() {
            collect_from_pattern(&format!("{}/**/*", path.display()), &mut files, options, registry)?;
        } else {
            collect_from_pattern(pattern, &mut files, options, registry)?;
        }
    }

    files.sort();
    files.dedup();
    Ok(files)
}

fn collect_from_pattern(
    pattern: &str,
    files: &mut Vec<PathBuf>,
    options: &ProcessingOptions,
    registry: &LanguageRegistry,
) -> Result<()> {
    if !options.respect_gitignore {
        for entry in glob::glob(pattern).context("Failed to parse glob pattern")? {
            match entry {
                Ok(path) if path.is_file() && registry.detect_language(&path).is_some() => files.push(path),
                Ok(_) => {}
                Err(error) => anstream::eprintln!("{} reading path: {error}", ui::danger("error")),
            }
        }
        return Ok(());
    }

    let root = pattern.strip_suffix("/**/*").unwrap_or(pattern);
    let absolute = absolute_normalized(
        &std::env::current_dir().unwrap_or_else(|_| PathBuf::from(".")),
        Path::new(root),
    );

    // Walk from the git root when there is one, so ignore rules recorded above the requested
    // directory still apply, then keep only what lies under that directory.
    let git_root = absolute.ancestors().skip(1).find(|dir| dir.join(".git").exists());
    let (walk_root, prefix) = match git_root {
        Some(root) if absolute.starts_with(root) => (root.to_path_buf(), Some(absolute.clone())),
        _ => (absolute, None),
    };

    let walker = ignore::WalkBuilder::new(walk_root)
        .hidden(false)
        .git_ignore(true)
        .git_global(true)
        .git_exclude(true)
        .parents(true)
        .require_git(false)
        .build();

    for entry in walker {
        match entry {
            Ok(entry) => {
                let path = entry.path();
                if let Some(prefix) = &prefix
                    && !path.starts_with(prefix)
                {
                    continue;
                }
                if path.is_file() && registry.detect_language(path).is_some() {
                    files.push(path.to_path_buf());
                }
            }
            Err(error) => anstream::eprintln!("{} reading path: {error}", ui::danger("error")),
        }
    }

    Ok(())
}

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

    /// A record with the fields the unit under test reads, and defaults elsewhere.
    fn record(id: &str, path: &str, line: usize, text: &str, verdict: RecordVerdict) -> Record {
        Record {
            id: id.to_string(),
            path: path.to_string(),
            line,
            end_line: line,
            start_byte: 0,
            end_byte: 0,
            kind: "line",
            verdict,
            text: text.to_string(),
            reason: None,
            full_id: format!("{id}{}", "f".repeat(FULL_ID_HEX - id.len())),
        }
    }

    #[test]
    fn the_first_claimant_of_an_id_keeps_it_and_later_ones_are_widened() {
        let mut seen = HashSet::new();
        let mut first = [record("a3f9c1d2ab", "src/a.rs", 4, "// x", RecordVerdict::Remove)];
        assert!(resolve_collisions(&mut first, &mut seen).is_empty());
        assert_eq!(first[0].id, "a3f9c1d2ab");

        // The same short id turning up in another file: that record moves to its full form, so the
        // short id still names exactly one comment in the report.
        let mut later = [
            record("a3f9c1d2ab", "src/b.rs", 9, "// y", RecordVerdict::Remove),
            record("0000000001", "src/b.rs", 11, "// z", RecordVerdict::Preserve),
        ];
        let widened = resolve_collisions(&mut later, &mut seen);

        assert_eq!(
            widened,
            vec![Widened {
                short: "a3f9c1d2ab".to_string(),
                path: "src/b.rs".to_string(),
                line: 9,
            }]
        );
        assert_eq!(later[0].id.len(), FULL_ID_HEX);
        assert!(later[0].id.starts_with("a3f9c1d2ab"));
        assert_eq!(later[1].id, "0000000001");
    }

    #[test]
    fn an_already_widened_record_is_left_alone() {
        let mut seen = HashSet::new();
        let wide = "a".repeat(FULL_ID_HEX);
        let mut records = [
            record(&wide, "src/a.rs", 1, "// x", RecordVerdict::Remove),
            record(&wide, "src/b.rs", 1, "// y", RecordVerdict::Remove),
        ];

        assert!(resolve_collisions(&mut records, &mut seen).is_empty());
        assert!(records.iter().all(|record| record.id == wide));
        assert!(seen.is_empty(), "a full id must not reserve a short one");
    }

    #[test]
    fn a_preserve_reason_serializes_as_a_string_except_for_a_pattern() {
        let cases = [
            (PreserveReason::Documentation, "\"documentation\""),
            (PreserveReason::FileHeader, "\"file_header\""),
            (PreserveReason::Shebang, "\"shebang\""),
            (PreserveReason::KeepMarker, "\"keep_marker\""),
            (
                PreserveReason::ExtendedByNeighbourMarker,
                "\"extended_by_neighbour_marker\"",
            ),
            (PreserveReason::LanguageDirective, "\"language_directive\""),
            (PreserveReason::Pattern("TODO".to_string()), "{\"pattern\":\"TODO\"}"),
        ];

        for (reason, expected) in cases {
            let encoded = serde_json::to_string(&Reason::from(&reason)).expect("reason serializes");
            assert_eq!(encoded, expected, "encoding {reason:?}");
        }
    }

    #[test]
    fn a_block_comment_stays_on_one_line_in_the_text_format() {
        assert_eq!(single_line("/*\n * one\n * two\n */"), "/*  * one  * two  */");
        assert_eq!(single_line("// only"), "// only");
    }

    #[test]
    fn grouping_counts_every_site_but_caps_the_list() {
        let mut groups = BTreeMap::new();
        for index in 0..SITE_CAP + 5 {
            let verdict = if index == 0 {
                RecordVerdict::Preserve
            } else {
                RecordVerdict::Remove
            };
            accumulate(
                &mut groups,
                record(
                    &format!("{index:010x}"),
                    &format!("src/file{index}.rs"),
                    index + 1,
                    "//   Legacy  Shim",
                    verdict,
                ),
            );
        }

        assert_eq!(groups.len(), 1);
        let group = groups.values().next().expect("one group");
        assert_eq!(group.count, SITE_CAP + 5);
        assert_eq!(group.sites.len(), SITE_CAP);
        assert!(group.truncated);
        assert_eq!(group.removable, SITE_CAP + 4);
        assert_eq!(group.preserved, 1);
        assert_eq!(group.id, group_id(&normalize_comment_text("// legacy shim")));
    }

    #[test]
    fn differently_written_but_equivalent_comments_land_in_one_group() {
        let mut groups = BTreeMap::new();
        for text in ["// Legacy shim", "//   legacy   SHIM", "/* legacy shim */"] {
            accumulate(
                &mut groups,
                record("0000000000", "src/a.rs", 1, text, RecordVerdict::Remove),
            );
        }

        assert_eq!(groups.len(), 1);
        assert_eq!(groups.values().next().map(|group| group.count), Some(3));
    }
}