ocomment 0.1.0

Fast, byte-preserving comment checker and remover
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
use anyhow::{Context, Result, anyhow, bail, ensure};
use globset::{Glob, GlobMatcher};
use ocomment_core::{
    CommentKind, DeclarativeProfile, Dialect, DispositionExplanation, Language, Layout, Policy,
    ScanOptions, TransformOptions, validate_profile,
};
use serde::{Deserialize, Serialize};
use std::{
    collections::BTreeMap,
    env, fs,
    path::{Component, Path, PathBuf},
};

pub const CONFIG_FILE: &str = ".ocomment.toml";

#[derive(Clone, Debug, Default, Serialize, Deserialize)]
#[serde(default, deny_unknown_fields)]
pub struct Config {
    pub version: Option<u32>,
    pub files: FilesConfig,
    pub policy: PolicyConfig,
    pub git: GitConfig,
    pub lsp: LspConfig,
    pub languages: BTreeMap<String, LanguageConfig>,
    pub profiles: BTreeMap<String, DeclarativeProfile>,
    pub plugins: PluginsConfig,
    pub overrides: Vec<PathOverride>,
}

#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(default, deny_unknown_fields)]
pub struct FilesConfig {
    pub max_size: u64,
    pub hidden: bool,
    pub follow_symlinks: bool,
    pub ignore: bool,
    pub include: Vec<String>,
    pub exclude: Vec<String>,
}

impl Default for FilesConfig {
    fn default() -> Self {
        Self {
            max_size: 32 * 1024 * 1024,
            hidden: false,
            follow_symlinks: false,
            ignore: true,
            include: Vec::new(),
            exclude: Vec::new(),
        }
    }
}

#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(default, deny_unknown_fields)]
pub struct PolicyConfig {
    pub mode: Policy,
    pub layout: Layout,
    pub keep_kind: Vec<CommentKind>,
    pub remove_kind: Vec<CommentKind>,
    pub keep_regex: Vec<String>,
    pub remove_regex: Vec<String>,
    pub force_invalid: bool,
    pub force_protected: bool,
}

impl Default for PolicyConfig {
    fn default() -> Self {
        Self {
            mode: Policy::Safe,
            layout: Layout::Lines,
            keep_kind: Vec::new(),
            remove_kind: Vec::new(),
            keep_regex: Vec::new(),
            remove_regex: Vec::new(),
            force_invalid: false,
            force_protected: false,
        }
    }
}

#[derive(Clone, Debug, Default, Serialize, Deserialize)]
#[serde(default, deny_unknown_fields)]
pub struct GitConfig {
    pub staged: bool,
    pub index_only: bool,
}

#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(default, deny_unknown_fields)]
pub struct LspConfig {
    pub on_save: bool,
    pub diagnostics: bool,
    pub code_lens: bool,
}

impl Default for LspConfig {
    fn default() -> Self {
        Self {
            on_save: false,
            diagnostics: true,
            code_lens: true,
        }
    }
}

#[derive(Clone, Debug, Default, Serialize, Deserialize)]
#[serde(default, deny_unknown_fields)]
pub struct LanguageConfig {
    pub enabled: Option<bool>,
    pub dialect: Option<Dialect>,
    pub policy: Option<Policy>,
    pub layout: Option<Layout>,
    pub keep_kind: Vec<CommentKind>,
    pub remove_kind: Vec<CommentKind>,
    pub keep_regex: Vec<String>,
    pub remove_regex: Vec<String>,
}

#[derive(Clone, Debug, Default, Serialize, Deserialize)]
#[serde(default, deny_unknown_fields)]
pub struct PluginsConfig {
    pub enabled: Vec<String>,
    /// Maps a lowercase extension (without `.`) to a locked plugin name.
    pub routes: BTreeMap<String, String>,
    pub memory_mib: Option<u32>,
    pub instances: Option<u32>,
    pub fuel_per_byte: Option<u64>,
}

#[derive(Clone, Debug, Default, Serialize, Deserialize)]
#[serde(default, deny_unknown_fields)]
pub struct PathOverride {
    pub paths: Vec<String>,
    pub language: Option<Language>,
    pub dialect: Option<Dialect>,
    pub policy: Option<Policy>,
    pub layout: Option<Layout>,
    pub keep_kind: Vec<CommentKind>,
    pub remove_kind: Vec<CommentKind>,
    pub keep_regex: Vec<String>,
    pub remove_regex: Vec<String>,
}

/// Where one effective setting came from.
///
/// The layers are the ones [`ResolvedConfig::for_path`] merges, and a source
/// names the layer a value arrived on rather than the value itself, so
/// `--explain` can send a reader to the table they have to edit.
#[derive(Clone, Debug, Default, Eq, PartialEq)]
pub enum Source {
    /// The `[policy]` table, or the built-in default when no file set it.
    #[default]
    Global,
    /// A `[languages.<name>]` table.
    Language(String),
    /// The `[[overrides]]` table at `index`, whose globs matched the path.
    Override { index: usize, paths: Vec<String> },
    /// A flag on the command line.
    Cli { flag: &'static str },
}

impl Source {
    /// How an explanation names this source, given the file a `Global` value
    /// was written in.
    ///
    /// `#N` counts an `[[overrides]]` table from zero, the way the regex
    /// indices printed beside it count the patterns they address.
    fn describe(&self, origin: Option<&Path>) -> String {
        match self {
            Self::Global => match origin {
                Some(path) => format!(
                    "[policy] in {}",
                    crate::output::sanitize_path(&path.display().to_string())
                ),
                None => "built-in defaults".to_owned(),
            },
            Self::Language(name) => format!("[languages.{name}]"),
            Self::Override { index, paths } => crate::output::sanitize_message(&format!(
                "[[overrides]] #{index}, paths = {paths:?}"
            )),
            Self::Cli { flag } => format!("{flag} on the command line"),
        }
    }
}

/// The `[policy]` keys a trace can attribute to a file, spelled as the file
/// spells them.
const POLICY_KEYS: [&str; 6] = [
    "mode",
    "layout",
    "keep_kind",
    "remove_kind",
    "keep_regex",
    "remove_regex",
];

/// Which configuration file last set each `[policy]` key. A key no file sets
/// keeps no entry, and an explanation calls it a built-in default rather than
/// sending the reader to a file that never mentions it.
type PolicyOrigins = BTreeMap<&'static str, PathBuf>;

/// What the command line overrode, recorded while it was applied so a trace
/// can say the command line rather than the file the value would otherwise
/// have been written in.
#[derive(Clone, Copy, Debug, Default)]
pub struct CliOverrides {
    pub language: Option<Language>,
    pub dialect: Option<Dialect>,
    pub policy: bool,
    pub layout: bool,
    /// Where the `--keep-kind` values start in `policy.keep_kind`: the command
    /// line appends to the configured list instead of replacing it, so only
    /// the tail of that list belongs to the command line.
    pub keep_kind_from: Option<usize>,
    /// The same boundary for `--remove-kind` in `policy.remove_kind`.
    pub remove_kind_from: Option<usize>,
}

/// Where every effective setting for one path came from.
///
/// The `*_kind` and `*_regex` vectors run parallel to the vectors in the
/// [`ScanOptions`] that [`ResolvedConfig::for_path_traced`] returned beside
/// this: entry `i` says which layer contributed entry `i` of that list.
#[derive(Clone, Debug, Default)]
pub struct PolicyTrace {
    pub policy: Source,
    /// Where the layout came from. No disposition depends on the layout, so no
    /// explanation names it yet; it is recorded because the trace answers the
    /// question for the whole `[policy]` block and `--explain` will not be its
    /// only caller.
    #[allow(dead_code)]
    pub layout: Source,
    pub keep_kind: Vec<Source>,
    pub remove_kind: Vec<Source>,
    pub keep_regex: Vec<Source>,
    pub remove_regex: Vec<Source>,
    origins: PolicyOrigins,
}

impl PolicyTrace {
    /// Where the setting that decided `explanation` came from, worded the way
    /// `--explain` prints it, or `None` when a built-in rule decided it and
    /// there is no table to point at.
    ///
    /// `options` is the one the explanation was produced from: a regex
    /// explanation carries its index into those lists, and a kind explanation
    /// is found by the kind it names.
    pub fn origin_of(
        &self,
        explanation: &DispositionExplanation,
        options: &ScanOptions,
    ) -> Option<String> {
        let position = |kinds: &[CommentKind], kind: &CommentKind| {
            kinds.iter().position(|value| value == kind)
        };
        let (source, key) = match explanation {
            DispositionExplanation::KeptByKind(kind) => (
                self.keep_kind.get(position(&options.keep_kinds, kind)?)?,
                "keep_kind",
            ),
            DispositionExplanation::RemovedByKind(kind) => (
                self.remove_kind
                    .get(position(&options.remove_kinds, kind)?)?,
                "remove_kind",
            ),
            DispositionExplanation::KeptByRegex { index, .. } => {
                (self.keep_regex.get(*index)?, "keep_regex")
            }
            DispositionExplanation::RemovedByRegex { index, .. } => {
                (self.remove_regex.get(*index)?, "remove_regex")
            }
            /* NOTE: Every one of these is the policy having the last word, whether it
             * took the comment out or protected it. */
            DispositionExplanation::RemovedByPolicy(_)
            | DispositionExplanation::RemovedByDefault(_)
            | DispositionExplanation::KeptLicense { .. } => (&self.policy, "mode"),
            // NOTE: A built-in rule, decided by no setting at all.
            DispositionExplanation::ProtectedPreamble
            | DispositionExplanation::KeptHtml
            | DispositionExplanation::KeptDirective { .. }
            | DispositionExplanation::KeptStructural { .. } => return None,
        };
        Some(source.describe(self.origins.get(key).map(PathBuf::as_path)))
    }
}

/// One layer of the policy merge, as the trace replays it.
struct TracedLayer<'a> {
    source: Source,
    policy: Option<Policy>,
    layout: Option<Layout>,
    keep_kind: &'a [CommentKind],
    remove_kind: &'a [CommentKind],
    keep_regex: &'a [String],
    remove_regex: &'a [String],
}

/// Attribute every entry of one merged list to the layer that introduced it.
///
/// [`ResolvedConfig::for_path`] starts from the global list verbatim and then
/// appends whatever a later layer adds that is not there yet, so replaying that
/// walk reproduces the merged list position for position. The tail of the
/// global list from `cli_from` on is what `flag` appended to it.
fn attribute<T: Clone + Eq>(
    global: &[T],
    cli_from: Option<usize>,
    flag: &'static str,
    layers: &[(&Source, &[T])],
) -> Vec<Source> {
    let cli_from = cli_from.unwrap_or(usize::MAX);
    let mut sources: Vec<Source> = (0..global.len())
        .map(|index| {
            if index >= cli_from {
                Source::Cli { flag }
            } else {
                Source::Global
            }
        })
        .collect();
    let mut seen = global.to_vec();
    for (source, values) in layers {
        for value in *values {
            if !seen.contains(value) {
                seen.push(value.clone());
                sources.push((*source).clone());
            }
        }
    }
    sources
}

/// Where a setting that holds a single value came from, before any language or
/// path layer has had its say.
fn scalar_source(overridden: bool, flag: &'static str) -> Source {
    if overridden {
        Source::Cli { flag }
    } else {
        Source::Global
    }
}

struct CompiledOverride {
    matchers: Vec<GlobMatcher>,
    value: PathOverride,
}

#[derive(Clone, Debug, Default)]
pub struct ConfigTrace {
    pub user: Option<PathBuf>,
    pub project: Option<PathBuf>,
    pub explicit: Option<PathBuf>,
}

pub struct ResolvedConfig {
    pub config: Config,
    pub trace: ConfigTrace,
    /// Where the project starts: the directory `.ocomment.toml` was found in,
    /// the repository above the working directory, or the working directory
    /// itself. It decides where configuration is discovered, what the file and
    /// override globs are written relative to, and where the plugin lock
    /// lives — no longer what a command with no path walks.
    pub root: PathBuf,
    /// The directory the command was run from, which is what a path typed on
    /// the command line is relative to.
    pub cwd: PathBuf,
    /// What the command line overrode, filled in after the files were merged.
    pub cli_overrides: CliOverrides,
    overrides: Vec<CompiledOverride>,
    origins: PolicyOrigins,
}

impl ResolvedConfig {
    /// Where `path` sits under the project root, spelled the way a
    /// configuration glob is written.
    ///
    /// `files.include`, `files.exclude`, and every `[[overrides]].paths`
    /// pattern is relative to the root, while a path named on the command line
    /// is relative to the working directory. The two agree only when the
    /// command is run from the root, so the path is resolved against the
    /// directory it was typed in before it is measured against the root, and
    /// the separators come out as forward slashes so one glob reads the same
    /// on every platform.
    ///
    /// A path outside the root — an explicit target above it, say — has no
    /// root-relative spelling at all, so it keeps its absolute one and only an
    /// absolute glob can match it.
    pub fn relative_to_root(&self, path: &Path) -> String {
        /* NOTE: Standard input has no place on disk. The pseudo-path is what the
         * renderers print, so it is also what the globs are shown. */
        if path.as_os_str() == crate::files::STDIN_PATH {
            return crate::files::STDIN_PATH.to_owned();
        }
        let joined = self.cwd.join(path);
        let absolute = lexical(&std::path::absolute(&joined).unwrap_or(joined));
        let relative = absolute.strip_prefix(&self.root).unwrap_or(&absolute);
        relative.to_string_lossy().replace('\\', "/")
    }

    pub fn language_is_enabled(&self, language: Language) -> bool {
        self.cli_overrides.language.is_some()
            || self
                .config
                .languages
                .get(language.as_str())
                .and_then(|item| item.enabled)
                != Some(false)
    }

    pub fn for_path(
        &self,
        path: &Path,
        language: Language,
        dialect: Dialect,
    ) -> Result<(Language, TransformOptions)> {
        let normalized = self.relative_to_root(path);
        let mut chosen_language = language;
        let mut chosen_dialect = dialect;
        /* NOTE: Language is selected before its language-specific policy is
         * applied. A language change without an accompanying dialect starts at
         * that language's standard dialect instead of carrying (for example)
         * `tsx` into a Rust scan. */
        for override_ in &self.overrides {
            if override_
                .matchers
                .iter()
                .any(|matcher| matcher.is_match(&normalized))
                && let Some(value) = override_.value.language
                && value != chosen_language
            {
                chosen_language = value;
                chosen_dialect = Dialect::Standard;
            }
        }
        if let Some(value) = self.cli_overrides.language
            && value != chosen_language
        {
            chosen_language = value;
            chosen_dialect = Dialect::Standard;
        }
        let mut policy = self.config.policy.mode;
        let mut layout = self.config.policy.layout;
        let mut keep = self.config.policy.keep_kind.clone();
        let mut remove = self.config.policy.remove_kind.clone();
        let mut keep_regex = self.config.policy.keep_regex.clone();
        let mut remove_regex = self.config.policy.remove_regex.clone();

        if let Some(language_config) = self.config.languages.get(chosen_language.as_str()) {
            if let Some(value) = language_config.dialect {
                chosen_dialect = value;
            }
            if let Some(value) = language_config.policy {
                policy = value;
            }
            if let Some(value) = language_config.layout {
                layout = value;
            }
            extend_unique(&mut keep, &language_config.keep_kind);
            extend_unique(&mut remove, &language_config.remove_kind);
            extend_unique(&mut keep_regex, &language_config.keep_regex);
            extend_unique(&mut remove_regex, &language_config.remove_regex);
        }
        for override_ in &self.overrides {
            if override_
                .matchers
                .iter()
                .any(|matcher| matcher.is_match(&normalized))
            {
                if let Some(value) = override_.value.dialect {
                    chosen_dialect = value;
                }
                if let Some(value) = override_.value.policy {
                    policy = value;
                }
                if let Some(value) = override_.value.layout {
                    layout = value;
                }
                extend_unique(&mut keep, &override_.value.keep_kind);
                extend_unique(&mut remove, &override_.value.remove_kind);
                extend_unique(&mut keep_regex, &override_.value.keep_regex);
                extend_unique(&mut remove_regex, &override_.value.remove_regex);
            }
        }
        if self.cli_overrides.policy {
            policy = self.config.policy.mode;
        }
        if self.cli_overrides.layout {
            layout = self.config.policy.layout;
        }
        if let Some(value) = self.cli_overrides.dialect {
            chosen_dialect = value;
        }
        validate_dialect(chosen_language, chosen_dialect)
            .context("invalid effective language/dialect selection")?;
        let scan = ScanOptions {
            policy,
            dialect: chosen_dialect,
            force_invalid: self.config.policy.force_invalid,
            force_protected: self.config.policy.force_protected,
            keep_kinds: keep,
            remove_kinds: remove,
            keep_regex,
            remove_regex,
        };
        Ok((chosen_language, TransformOptions { scan, layout }))
    }

    /// The same answer as [`Self::for_path`], with a record of where each
    /// setting came from.
    ///
    /// The values are [`Self::for_path`]'s own, so what a run does and what
    /// `--explain` says about it cannot disagree; only the attribution is
    /// computed here, by replaying the same merge with the layer names
    /// attached. `--explain` is the only caller, which is why the hot path is
    /// left as it was.
    pub fn for_path_traced(
        &self,
        path: &Path,
        language: Language,
        dialect: Dialect,
    ) -> Result<(Language, TransformOptions, PolicyTrace)> {
        let (chosen_language, options) = self.for_path(path, language, dialect)?;
        let normalized = self.relative_to_root(path);
        let mut layers = Vec::new();
        if let Some(config) = self.config.languages.get(chosen_language.as_str()) {
            layers.push(TracedLayer {
                source: Source::Language(chosen_language.as_str().to_owned()),
                policy: config.policy,
                layout: config.layout,
                keep_kind: &config.keep_kind,
                remove_kind: &config.remove_kind,
                keep_regex: &config.keep_regex,
                remove_regex: &config.remove_regex,
            });
        }
        for (index, compiled) in self.overrides.iter().enumerate() {
            if !compiled
                .matchers
                .iter()
                .any(|matcher| matcher.is_match(&normalized))
            {
                continue;
            }
            let value = &compiled.value;
            layers.push(TracedLayer {
                source: Source::Override {
                    index,
                    paths: value.paths.clone(),
                },
                policy: value.policy,
                layout: value.layout,
                keep_kind: &value.keep_kind,
                remove_kind: &value.remove_kind,
                keep_regex: &value.keep_regex,
                remove_regex: &value.remove_regex,
            });
        }
        let cli = self.cli_overrides;
        let keep_kinds: Vec<_> = layers
            .iter()
            .map(|layer| (&layer.source, layer.keep_kind))
            .collect();
        let remove_kinds: Vec<_> = layers
            .iter()
            .map(|layer| (&layer.source, layer.remove_kind))
            .collect();
        let keep_patterns: Vec<_> = layers
            .iter()
            .map(|layer| (&layer.source, layer.keep_regex))
            .collect();
        let remove_patterns: Vec<_> = layers
            .iter()
            .map(|layer| (&layer.source, layer.remove_regex))
            .collect();
        let mut trace = PolicyTrace {
            policy: scalar_source(cli.policy, "--policy"),
            layout: scalar_source(cli.layout, "--layout"),
            keep_kind: attribute(
                &self.config.policy.keep_kind,
                cli.keep_kind_from,
                "--keep-kind",
                &keep_kinds,
            ),
            remove_kind: attribute(
                &self.config.policy.remove_kind,
                cli.remove_kind_from,
                "--remove-kind",
                &remove_kinds,
            ),
            /* NOTE: No flag supplies a pattern, so no entry of either list can have
             * come from the command line. */
            keep_regex: attribute(&self.config.policy.keep_regex, None, "", &keep_patterns),
            remove_regex: attribute(&self.config.policy.remove_regex, None, "", &remove_patterns),
            origins: self.origins.clone(),
        };
        /* NOTE: A single-valued setting is not merged but replaced, so the last layer
         * that names it is the one that decided it. */
        for layer in &layers {
            if layer.policy.is_some() {
                trace.policy = layer.source.clone();
            }
            if layer.layout.is_some() {
                trace.layout = layer.source.clone();
            }
        }
        /* NOTE: Flags are the final layer. Starting the trace at CLI and then
         * replaying path layers would claim that a value which never won did. */
        if cli.policy {
            trace.policy = Source::Cli { flag: "--policy" };
        }
        if cli.layout {
            trace.layout = Source::Cli { flag: "--layout" };
        }
        Ok((chosen_language, options, trace))
    }
}

pub fn load(explicit: Option<&Path>) -> Result<ResolvedConfig> {
    let cwd = env::current_dir().context("cannot determine current directory")?;
    load_from(&cwd, explicit)
}

/// Resolve configuration as though the command were invoked from `cwd`,
/// without changing the process-wide current directory. LSP workspace folders
/// use this to keep discovery and glob roots independent.
pub fn load_from(cwd: &Path, explicit: Option<&Path>) -> Result<ResolvedConfig> {
    let cwd = if cwd.is_absolute() {
        lexical(cwd)
    } else {
        lexical(&env::current_dir()?.join(cwd))
    };
    let explicit_path = explicit.map(|path| {
        let joined = if path.is_absolute() {
            path.to_path_buf()
        } else {
            cwd.join(path)
        };
        lexical(&joined)
    });
    /* NOTE: `--config` is discovery's replacement, not one more layer on top
     * of whatever happens to surround the caller. Its own directory is the
     * root for globs and the plugin lock. */
    let project_path = explicit_path
        .is_none()
        .then(|| locate_project(&cwd))
        .flatten();
    let user_path = explicit_path
        .is_none()
        .then(user_config_path)
        .flatten()
        .filter(|path| path.is_file());
    let root = explicit_path
        .as_deref()
        .and_then(Path::parent)
        .map(Path::to_path_buf)
        .or_else(|| {
            project_path
                .as_deref()
                .and_then(Path::parent)
                .map(Path::to_path_buf)
        })
        .or_else(|| locate_repository(&cwd))
        .unwrap_or_else(|| cwd.clone());
    let mut merged: toml::Value = toml::from_str(&toml::to_string(&Config::default())?)?;
    let mut trace = ConfigTrace::default();
    let mut origins = PolicyOrigins::new();
    if let Some(path) = &user_path {
        merge_layer(
            &mut merged,
            &mut origins,
            parse_layer(path, false)?,
            path,
            &cwd,
        );
        trace.user = Some(path.clone());
    }
    if let Some(path) = &project_path {
        merge_layer(
            &mut merged,
            &mut origins,
            parse_layer(path, true)?,
            path,
            &cwd,
        );
        trace.project = Some(path.clone());
    }
    if let Some(path) = &explicit_path {
        merge_layer(
            &mut merged,
            &mut origins,
            parse_layer(path, true)?,
            path,
            &cwd,
        );
        trace.explicit = Some(path.to_path_buf());
    }
    let mut config: Config = merged
        .try_into()
        .context("cannot resolve merged configuration")?;
    for (name, profile) in &mut config.profiles {
        if profile.name.is_empty() {
            profile.name = name.clone();
        }
        validate_profile(profile).map_err(|error| anyhow!(error))?;
    }
    validate_languages(&config)?;
    validate_policy_regexes(&config)?;
    let overrides = compile_overrides(&config.overrides)?;
    Ok(ResolvedConfig {
        config,
        trace,
        root,
        cwd,
        cli_overrides: CliOverrides::default(),
        overrides,
        origins,
    })
}

/// Layer one configuration file over the merged document, noting every
/// `[policy]` key it sets on the way.
///
/// A later layer overwrites an earlier one exactly as `merge_value` does, so
/// what is left is the file whose value survived the merge — the one an
/// explanation is worth sending a reader to.
fn merge_layer(
    merged: &mut toml::Value,
    origins: &mut PolicyOrigins,
    layer: toml::Value,
    path: &Path,
    cwd: &Path,
) {
    if let Some(policy) = layer.get("policy") {
        for key in POLICY_KEYS {
            if policy.get(key).is_some() {
                origins.insert(key, origin_label(path, cwd));
            }
        }
    }
    merge_value(merged, layer);
}

/// How an explanation names a configuration file: relative to the directory
/// the command was run from when it sits there, and absolute otherwise.
///
/// The label is repeated on every explained line, so the short spelling is
/// worth having — but only where it still names the file the reader would open.
/// A file further up the tree, or the user file under `$HOME`, keeps its
/// absolute path.
fn origin_label(path: &Path, cwd: &Path) -> PathBuf {
    path.strip_prefix(cwd).unwrap_or(path).to_path_buf()
}

fn validate_languages(config: &Config) -> Result<()> {
    for (name, language_config) in &config.languages {
        let language: Language = name.parse().map_err(|_| {
            anyhow!("unknown language configuration key `{name}`; see `ocomment languages`")
        })?;
        if let Some(dialect) = language_config.dialect {
            validate_dialect(language, dialect)
                .with_context(|| format!("invalid dialect for [languages.{name}]"))?;
        }
    }
    for item in &config.overrides {
        if let (Some(language), Some(dialect)) = (item.language, item.dialect) {
            validate_dialect(language, dialect)
                .context("invalid language/dialect path override")?;
        }
    }
    Ok(())
}

/// Every dialect the scanner accepts for `language`, in canonical order.
pub fn supported_dialects(language: Language) -> &'static [Dialect] {
    match language {
        Language::JavaScript => &[Dialect::Standard, Dialect::Jsx],
        Language::TypeScript => &[Dialect::Standard, Dialect::Tsx],
        Language::C => &[Dialect::Standard, Dialect::ObjectiveC, Dialect::GnuC],
        Language::Cpp => &[
            Dialect::Standard,
            Dialect::ObjectiveCpp,
            Dialect::GnuCpp,
            Dialect::Cuda,
        ],
        Language::Css => &[Dialect::Standard, Dialect::Scss, Dialect::Sass],
        Language::Shell => &[
            Dialect::Standard,
            Dialect::PosixSh,
            Dialect::Bash53,
            Dialect::Zsh,
        ],
        Language::Sql => &[
            Dialect::Standard,
            Dialect::PostgreSql,
            Dialect::MySql,
            Dialect::Sqlite,
            Dialect::TSql,
            Dialect::Oracle,
        ],
        _ => &[Dialect::Standard],
    }
}

pub fn validate_dialect(language: Language, dialect: Dialect) -> Result<()> {
    let supported = supported_dialects(language);
    ensure!(
        supported.contains(&dialect),
        "unsupported dialect `{dialect}` for {language}; supported: {}",
        supported
            .iter()
            .map(|value| value.as_str())
            .collect::<Vec<_>>()
            .join(", ")
    );
    Ok(())
}

fn validate_policy_regexes(config: &Config) -> Result<()> {
    let patterns = config
        .policy
        .keep_regex
        .iter()
        .chain(&config.policy.remove_regex)
        .chain(
            config
                .languages
                .values()
                .flat_map(|language| language.keep_regex.iter().chain(&language.remove_regex)),
        )
        .chain(
            config
                .overrides
                .iter()
                .flat_map(|item| item.keep_regex.iter().chain(&item.remove_regex)),
        );
    for pattern in patterns {
        regex::bytes::Regex::new(pattern).map_err(|error| {
            /* INVARIANT: Both halves of this line came out of a file in the project: the
             * pattern the caller wrote, and a parse error that quotes that
             * same pattern back with a caret under it. Neither may reach a
             * terminal verbatim, and the line stays one line. The pattern
             * keeps the spacing it was written with, because a reader who is
             * shown something else cannot find it in the file; the parse
             * error, which `regex` spreads over four lines, is folded onto
             * this one and kept whole. */
            anyhow!(
                "invalid comment policy regex `{}`: {}",
                crate::output::sanitize_path(pattern),
                crate::output::sanitize_message(&error.to_string())
            )
        })?;
    }
    Ok(())
}

fn parse_layer(path: &Path, require_version: bool) -> Result<toml::Value> {
    let text =
        fs::read_to_string(path).with_context(|| format!("cannot read {}", path.display()))?;
    let config: Config = toml::from_str(&text).map_err(|error| {
        let message = error.to_string();
        /* INVARIANT: `toml` quotes the line it stopped on, with a caret under the byte
         * that is wrong with it, so the whole of that line — bytes a project
         * file chose, an escape sequence among them — is on its way to a
         * terminal over four lines of diagram. It is folded onto the one line
         * an error is and kept whole, the way an invalid `[policy]` regex is:
         * the caret means nothing once the lines are joined, and the sentence
         * after it is the entire answer. The hint reads the unfolded message
         * because it quotes nothing back — only a key it found in the schema. */
        anyhow!(
            "invalid configuration {}: {}{}",
            /* NOTE: The path is the project's too — a directory it named — so it is
             * held to what every other path in the report is held to: printed
             * as it was spelled, with nothing in it a terminal would act on. */
            crate::output::sanitize_path(&path.display().to_string()),
            crate::output::sanitize_message(&message),
            unknown_key_hint(&message)
        )
    })?;
    if require_version && config.version != Some(1) {
        /* NOTE: The path is repeated deliberately: the first half is the verdict on
         * a file the reader may not have opened, the second is the edit that
         * settles it, and an editor is opened on the second one. */
        let path = path.display();
        bail!("{path} must contain `version = 1` (add `version = 1` at the top of {path})");
    }
    toml::from_str(&text).with_context(|| format!("cannot parse {}", path.display()))
}

fn merge_value(base: &mut toml::Value, overlay: toml::Value) {
    match (base, overlay) {
        (toml::Value::Table(base), toml::Value::Table(overlay)) => {
            for (key, value) in overlay {
                if key == "overrides" {
                    match value {
                        toml::Value::Array(mut incoming) => {
                            if let Some(toml::Value::Array(existing)) = base.get_mut(&key) {
                                existing.append(&mut incoming);
                            } else {
                                base.insert(key, toml::Value::Array(incoming));
                            }
                        }
                        other => {
                            base.insert(key, other);
                        }
                    }
                } else if let Some(existing) = base.get_mut(&key) {
                    merge_value(existing, value);
                } else {
                    base.insert(key, value);
                }
            }
        }
        (base, overlay) => *base = overlay,
    }
}

fn compile_overrides(overrides: &[PathOverride]) -> Result<Vec<CompiledOverride>> {
    overrides
        .iter()
        .cloned()
        .map(|value| {
            if value.paths.is_empty() {
                bail!("each [[overrides]] entry needs at least one path glob");
            }
            let matchers = value
                .paths
                .iter()
                .map(|pattern| {
                    Glob::new(pattern)
                        .with_context(|| format!("invalid override glob `{pattern}`"))
                        .map(|glob| glob.compile_matcher())
                })
                .collect::<Result<Vec<_>>>()?;
            Ok(CompiledOverride { matchers, value })
        })
        .collect()
}

/// Resolve `.` and `..` without asking the file system.
///
/// A configuration glob is matched against text, so the text has to be the one
/// the reader would have written: `../sibling/main.rs`, named from `nested/`,
/// is `sibling/main.rs` under the root, and leaving the `..` in place would
/// let it match a `nested/**` override it is not under. The resolution is
/// lexical because the path need not exist and because `canonicalize` would
/// also resolve the symbolic links the root itself may be reached through,
/// which would leave the two ends of the comparison in different spellings.
pub(crate) fn lexical(path: &Path) -> PathBuf {
    let mut resolved = PathBuf::new();
    for component in path.components() {
        match component {
            Component::CurDir => {}
            Component::ParentDir
                if matches!(
                    resolved.components().next_back(),
                    Some(Component::Normal(_))
                ) =>
            {
                resolved.pop();
            }
            component => resolved.push(component),
        }
    }
    resolved
}

pub fn locate_project(start: &Path) -> Option<PathBuf> {
    let mut directory = Some(start);
    while let Some(current) = directory {
        let candidate = current.join(CONFIG_FILE);
        if candidate.is_file() {
            return Some(candidate);
        }
        directory = current.parent();
    }
    None
}

/// Locate the nearest repository without starting `git`. A `.git` directory
/// and the indirection file used by worktrees are both accepted. This keeps
/// the no-argument command fast while making its scope the current repository.
pub fn locate_repository(start: &Path) -> Option<PathBuf> {
    let start = if start.is_dir() {
        start
    } else {
        start.parent()?
    };
    start
        .ancestors()
        .find(|directory| directory.join(".git").exists())
        .map(Path::to_path_buf)
}

pub fn user_config_path() -> Option<PathBuf> {
    if let Some(base) = env::var_os("XDG_CONFIG_HOME") {
        return Some(PathBuf::from(base).join("ocomment/config.toml"));
    }
    env::var_os("HOME").map(|base| PathBuf::from(base).join(".config/ocomment/config.toml"))
}

fn extend_unique<T: Clone + Eq>(target: &mut Vec<T>, values: &[T]) {
    for value in values {
        if !target.contains(value) {
            target.push(value.clone());
        }
    }
}

fn unknown_key_hint(message: &str) -> String {
    let Some(after) = message.split("unknown field `").nth(1) else {
        return String::new();
    };
    let Some((unknown, rest)) = after.split_once('`') else {
        return String::new();
    };
    let candidates = rest
        .split("expected one of ")
        .nth(1)
        .unwrap_or("")
        .split([',', '`', '\'', ' '])
        .filter(|value| {
            !value.is_empty()
                && value
                    .chars()
                    .all(|ch| ch.is_ascii_alphanumeric() || ch == '_' || ch == '-')
        });
    let suggestion = candidates.min_by_key(|candidate| edit_distance(unknown, candidate));
    suggestion
        .filter(|candidate| edit_distance(unknown, candidate) <= 3)
        .map_or_else(String::new, |candidate| {
            format!("; did you mean `{candidate}`?")
        })
}

fn edit_distance(left: &str, right: &str) -> usize {
    let right_length = right.len();
    let mut row: Vec<usize> = (0..=right_length).collect();
    for (i, a) in left.bytes().enumerate() {
        let mut diagonal = row[0];
        row[0] = i + 1;
        for (j, b) in right.bytes().enumerate() {
            let old = row[j + 1];
            row[j + 1] = (row[j + 1] + 1)
                .min(row[j] + 1)
                .min(diagonal + usize::from(a != b));
            diagonal = old;
        }
    }
    row[right_length]
}

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

    #[test]
    fn typo_hint_uses_nearest_key() {
        let message = "unknown field `layuot`, expected one of `mode`, `layout`, `keep_kind`";
        assert!(unknown_key_hint(message).contains("layout"));
    }

    /// `for_path_traced` must not become a second copy of the merge that can
    /// drift from it: the values it returns are `for_path`'s own, and the trace
    /// beside them lines up with those values position for position.
    #[test]
    fn a_traced_lookup_returns_the_untraced_answer_and_lines_up_with_it() {
        let directory = tempfile::tempdir().unwrap();
        let root = directory.path().to_path_buf();
        let mut config = Config::default();
        config.policy.keep_regex = vec!["global".to_owned()];
        config.policy.keep_kind = vec![CommentKind::Line];
        config.languages.insert(
            "rust".to_owned(),
            LanguageConfig {
                keep_regex: vec!["language".to_owned()],
                ..LanguageConfig::default()
            },
        );
        config.overrides = vec![PathOverride {
            paths: vec!["nested/**".to_owned()],
            policy: Some(Policy::All),
            /* NOTE: The duplicate is dropped by the merge, so the trace must not
             * record a source for it either. */
            keep_regex: vec!["override".to_owned(), "global".to_owned()],
            ..PathOverride::default()
        }];
        let overrides = compile_overrides(&config.overrides).unwrap();
        let resolved = ResolvedConfig {
            config,
            trace: ConfigTrace::default(),
            root: root.clone(),
            cwd: root.clone(),
            cli_overrides: CliOverrides {
                keep_kind_from: Some(0),
                ..CliOverrides::default()
            },
            overrides,
            origins: PolicyOrigins::new(),
        };
        let path = root.join("nested/a.rs");

        let (language, options) = resolved
            .for_path(&path, Language::Rust, Dialect::Standard)
            .unwrap();
        let (traced_language, traced_options, trace) = resolved
            .for_path_traced(&path, Language::Rust, Dialect::Standard)
            .unwrap();
        assert_eq!(traced_language, language);
        assert_eq!(traced_options, options);

        let override_source = Source::Override {
            index: 0,
            paths: vec!["nested/**".to_owned()],
        };
        assert_eq!(options.scan.keep_regex, ["global", "language", "override"]);
        assert_eq!(
            trace.keep_regex,
            [
                Source::Global,
                Source::Language("rust".to_owned()),
                override_source.clone(),
            ]
        );
        assert_eq!(trace.policy, override_source);
        assert_eq!(
            trace.keep_kind,
            [Source::Cli {
                flag: "--keep-kind"
            }]
        );
    }

    #[test]
    fn repository_root_accepts_directory_and_worktree_markers() {
        for marker_is_directory in [true, false] {
            let root = tempfile::tempdir().unwrap();
            if marker_is_directory {
                fs::create_dir(root.path().join(".git")).unwrap();
            } else {
                fs::write(root.path().join(".git"), "gitdir: elsewhere\n").unwrap();
            }
            let nested = root.path().join("a/b");
            fs::create_dir_all(&nested).unwrap();
            assert_eq!(locate_repository(&nested), Some(root.path().to_path_buf()));
        }
    }
}