jubarte-edit 0.20.0

The edit product of the jubarte engine: edit plans as tracked changes, text diff views, capabilities and staged publication
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
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
// SPDX-FileCopyrightText: 2026 Jandira Technologies, LLC
// SPDX-License-Identifier: Apache-2.0

//! Git/GitHub unified patches of complete document text snapshots, and
//! other review presentations.
//!
//! DOCX snapshots use the same paragraph, table, and revision-mark view
//! as `jubarte debug --check text`, without clipping or a hunk limit.
//! The patches are for review only; they do not patch the binary DOCX
//! package. They back `jubarte diff` and the Python and WASM bindings'
//! `diff_text` / `diff_documents` shapes.

use crate::markdown::Source;
use similar::{ChangeTag, DiffOp, DiffTag, TextDiff};
use std::fmt::Write as _;
use std::ops::Range;

/// Names written on the patch headers and the number of unchanged
/// context lines around each change. Git's `a/` and `b/` prefixes are
/// added automatically by every formatter that prints them.
#[derive(Clone, Debug)]
pub struct UnifiedOptions {
    /// Original document label (Git's `a/` prefix is added automatically).
    pub old_name: String,
    /// Modified document label (Git's `b/` prefix is added automatically).
    pub new_name: String,
    /// Number of unchanged lines around each change; defaults to three.
    pub context: usize,
}

impl Default for UnifiedOptions {
    fn default() -> Self {
        Self {
            old_name: "old.docx".into(),
            new_name: "new.docx".into(),
            context: 3,
        }
    }
}

/// Presentation of a document's line changes.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum TextFormat {
    /// Git headers and unified hunks, with the snapshots' tracked marks
    /// carried into the changed lines as their existing CriticMarkup.
    #[default]
    Github,
    /// Accept both inputs first, then emit fresh word-level CriticMarkup
    /// on each changed line only.
    Word,
    /// Traditional `a`, `d`, `c` line addresses, without context lines.
    Normal,
    /// Traditional two-sided context blocks (`***`, `---`).
    Context,
    /// Aligned replacement, insertion and deletion rows, without
    /// addresses; the default for [`Self::SideBySide`] adapts to a
    /// `--window` wide enough for the text.
    SideBySide,
}

/// Review presentation options for [`diff_text_view`] and
/// [`diff_documents_view`]; the raw unified patch APIs above
/// ([`diff_text`], [`diff_documents`]) stay unclipped.
#[derive(Clone, Debug)]
pub struct TextOptions {
    /// Snapshot labels and surrounding unchanged lines.
    pub unified: UnifiedOptions,
    /// Output presentation; defaults to GitHub unified hunks.
    pub format: TextFormat,
    /// Accept the inputs' tracked changes before comparison (always true
    /// when `format == TextFormat::Word`).
    pub accept_changes: bool,
    /// Per-line Unicode scalar window; `None` keeps complete lines. The
    /// window counts characters only, never delimiters, so every mark a
    /// line opens it closes.
    pub window: Option<usize>,
}

/// The default window, in Unicode scalars.
const WINDOW: usize = 70;

impl Default for TextOptions {
    fn default() -> Self {
        Self {
            unified: UnifiedOptions::default(),
            format: TextFormat::Github,
            accept_changes: false,
            window: Some(WINDOW),
        }
    }
}

/// Compares two plain-text snapshots (`old`, `new`) in the review
/// presentation `options.format` names.
///
/// Existing CriticMarkup stays inside the changed lines unless acceptance
/// is requested. Word mode always accepts both inputs and emits only the
/// changed lines with newly-generated CriticMarkup. Clipping (the
/// `window`) happens after comparison, so it cannot hide a hunk or
/// change its line ranges.
pub fn diff_text_view(old: &str, new: &str, options: &TextOptions) -> String {
    let accept = options.accept_changes || options.format == TextFormat::Word;
    let old = accepted_text(old, accept);
    let new = accepted_text(new, accept);
    render_view(&old, &new, options)
}

/// Compares two DOCX bytes or Markdown text snapshots in the review
/// presentation `options.format` names. DOCX revision acceptance runs on
/// the package before text extraction, so removed paragraphs and table
/// rows cannot contribute phantom line numbers.
///
/// # Inputs
///
/// - `old`, `new` — either two `.docx` byte slices or two Markdown strings
///   ([`crate::markdown::Source`]).
/// - `options` — review presentation, acceptance policy and `window`.
///
/// # Errors
///
/// A message when [`Source::Docx`] is given for `old` or `new` and the
/// package cannot be admitted (a budget, a duplicate or unsafe part name,
/// an encrypted `.docx`, an unsupported XML encoding, or the bytes are
/// not a `.docx`).
pub fn diff_documents_view(
    old: Source<'_>,
    new: Source<'_>,
    options: &TextOptions,
) -> Result<String, String> {
    let accept = options.accept_changes || options.format == TextFormat::Word;
    let critic = options.format != TextFormat::Word;
    let old = view_snapshot(old, accept, critic)?;
    let new = view_snapshot(new, accept, critic)?;
    Ok(render_view(&old, &new, options))
}

fn accepted_text(text: &str, accept: bool) -> std::borrow::Cow<'_, str> {
    if accept {
        crate::markdown::accepted_clauses(text).into()
    } else {
        text.into()
    }
}

fn view_snapshot(source: Source<'_>, accept: bool, critic: bool) -> Result<String, String> {
    match source {
        Source::Markdown(text) => Ok(accepted_text(text, accept).into_owned()),
        Source::Docx(bytes) => {
            crate::debug::document_text_view(bytes, critic, accept, critic_literal)
        }
    }
}

fn render_view(old: &str, new: &str, options: &TextOptions) -> String {
    // CRLF and LF end the same document line. A missing final newline is
    // marked by the formats that have a marker for it (GitHub, normal and
    // context); word and side-by-side compare the lines it ends.
    let terminate = !matches!(
        options.format,
        TextFormat::Github | TextFormat::Normal | TextFormat::Context
    );
    let (old, new) = (line_endings(old, terminate), line_endings(new, terminate));
    let (old, new) = (old.as_ref(), new.as_ref());
    if old == new {
        return String::new();
    }
    if options.format == TextFormat::Github && options.window.is_none() {
        return diff_text(old, new, &options.unified);
    }
    let diff = TextDiff::from_lines(old, new);
    let context = options
        .unified
        .context
        .min(diff.old_len().max(diff.new_len()));
    match options.format {
        TextFormat::Github => github_view(&diff, options, context),
        TextFormat::Normal => normal_view(&diff, options),
        TextFormat::Context => context_view(&diff, options, context),
        TextFormat::Word | TextFormat::SideBySide => rows_view(&diff, options, context),
    }
}

/// `text` with LF line ends, and with a final newline when `terminate`.
fn line_endings(text: &str, terminate: bool) -> std::borrow::Cow<'_, str> {
    let mut text = std::borrow::Cow::Borrowed(text);
    if text.contains("\r\n") {
        text = text.replace("\r\n", "\n").into();
    }
    if terminate && !text.is_empty() && !text.ends_with('\n') {
        text.to_mut().push('\n');
    }
    text
}

/// GNU diff's marker after a last line with no newline.
fn mark_unterminated(out: &mut String, line: Option<&str>) {
    if line.is_some_and(|line| !line.ends_with('\n')) {
        out.push_str("\\ No newline at end of file\n");
    }
}

fn line(text: &str) -> &str {
    let text = text.strip_suffix('\n').unwrap_or(text);
    text.strip_suffix('\r').unwrap_or(text)
}

/// Window offsets and widths count Unicode scalars, never UTF-8 bytes.
fn clip(text: &str, changed_at: usize, window: Option<usize>) -> String {
    let Some(width) = window else {
        return text.to_string();
    };
    let len = text.chars().count();
    if len <= width {
        return text.to_string();
    }
    let start = changed_at.saturating_sub(width / 2).min(len);
    let end = start.saturating_add(width).min(len);
    // The window's bytes, so the string is sized once (#1830).
    let byte_at = |i: usize| text.char_indices().nth(i).map_or(text.len(), |(b, _)| b);
    let window = &text[byte_at(start)..byte_at(end)];
    let ellipses = usize::from(start > 0) + usize::from(end < len);
    let mut out = String::with_capacity(window.len() + ellipses * '…'.len_utf8());
    if start > 0 {
        out.push('…');
    }
    out.push_str(window);
    if end < len {
        out.push('…');
    }
    out
}

fn first_difference(old: &str, new: &str) -> usize {
    old.chars()
        .zip(new.chars())
        .take_while(|(a, b)| a == b)
        .count()
}

#[derive(Debug, PartialEq, Eq)]
enum Row {
    Pair(usize, usize),
    Delete(usize),
    Insert(usize),
}

/// Use the upstream word diff to align similar clauses within a replacement.
/// A bounded search avoids quadratic work for large, wholly rewritten hunks.
/// Equal-sized unrelated tails still pair by position, as standard diff does.
fn replacement_rows(old: &[&str], new: &[&str]) -> Vec<Row> {
    if old.len().saturating_mul(new.len()) > 400 {
        return positional_rows(old.len(), new.len());
    }
    let old_words: Vec<_> = old.iter().map(|text| matching_words(text)).collect();
    let new_words: Vec<_> = new.iter().map(|text| matching_words(text)).collect();
    let mut rows = Vec::with_capacity(old_words.len() + new_words.len());
    let mut next = 0;
    for (i, a) in old_words.iter().enumerate() {
        let mut best = None;
        let mut score = 0.3;
        for (j, b) in new_words.iter().enumerate().skip(next) {
            let ratio = TextDiff::from_slices(a, b).ratio();
            if ratio > score {
                score = ratio;
                best = Some(j);
            }
        }
        // A later old clause may be a much stronger match for this new row.
        if let Some(j) = best {
            let stronger = old_words
                .iter()
                .skip(i + 1)
                .any(|a| TextDiff::from_slices(a, &new_words[j]).ratio() > score);
            if stronger {
                rows.push(Row::Delete(i));
                continue;
            }
        }
        let best = best
            .or_else(|| (old.len() - i == new.len() - next && next < new.len()).then_some(next));
        if let Some(j) = best {
            rows.extend((next..j).map(Row::Insert));
            rows.push(Row::Pair(i, j));
            next = j + 1;
        } else {
            rows.push(Row::Delete(i));
        }
    }
    rows.extend((next..new.len()).map(Row::Insert));
    rows
}

fn matching_words(text: &str) -> Vec<&str> {
    crate::util::word_tokens(text)
        .into_iter()
        .filter(|t| t.chars().any(char::is_alphanumeric))
        .collect()
}

fn positional_rows(old_len: usize, new_len: usize) -> Vec<Row> {
    let paired = old_len.min(new_len);
    (0..paired)
        .map(|i| Row::Pair(i, i))
        .chain((paired..old_len).map(Row::Delete))
        .chain((paired..new_len).map(Row::Insert))
        .collect()
}

fn op_lines<'a>(diff: &'a TextDiff<'_, '_, str>, op: &DiffOp) -> (Vec<&'a str>, Vec<&'a str>) {
    let old = op
        .old_range()
        .map(|i| line(&diff.old_lookup()[i]))
        .collect();
    let new = op
        .new_range()
        .map(|i| line(&diff.new_lookup()[i]))
        .collect();
    (old, new)
}

fn op_windows(
    diff: &TextDiff<'_, '_, str>,
    op: &DiffOp,
    window: Option<usize>,
) -> (Vec<String>, Vec<String>) {
    let (old, new) = op_lines(diff, op);
    let mut old_starts = vec![0; old.len()];
    let mut new_starts = vec![0; new.len()];
    if op.tag() == DiffTag::Replace {
        for row in replacement_rows(&old, &new) {
            if let Row::Pair(i, j) = row {
                let at = first_difference(old[i], new[j]);
                old_starts[i] = at;
                new_starts[j] = at;
            }
        }
    }
    let olds = old
        .iter()
        .zip(old_starts)
        .map(|(text, at)| clip(text, at, window))
        .collect();
    let news = new
        .iter()
        .zip(new_starts)
        .map(|(text, at)| clip(text, at, window))
        .collect();
    (olds, news)
}

fn github_view(diff: &TextDiff<'_, '_, str>, options: &TextOptions, context: usize) -> String {
    let old_name = git_path("a", &options.unified.old_name);
    let new_name = git_path("b", &options.unified.new_name);
    let mut out = format!("diff --git {old_name} {new_name}\n--- {old_name}\n+++ {new_name}\n");
    for ops in diff.grouped_ops(context) {
        let _ = writeln!(out, "{}", similar::udiff::UnifiedHunkHeader::new(&ops));
        for op in &ops {
            let (old, new) = op_windows(diff, op, options.window);
            for change in diff.iter_changes(op) {
                let text = match change.tag() {
                    ChangeTag::Equal | ChangeTag::Delete => {
                        &old[change.old_index().unwrap() - op.old_range().start]
                    }
                    ChangeTag::Insert => &new[change.new_index().unwrap() - op.new_range().start],
                };
                let _ = writeln!(out, "{}{text}", change.tag());
                if change.missing_newline() {
                    out.push_str("\\ No newline at end of file\n");
                }
            }
        }
    }
    out
}

/// Nonempty ranges use inclusive, one-based addresses; empty ones name the
/// preceding line (including line zero for an insertion at the beginning).
fn address(range: &Range<usize>) -> String {
    match range.len() {
        0 => range.start.to_string(),
        1 => range.end.to_string(),
        _ => format!("{},{}", range.start + 1, range.end),
    }
}

fn normal_view(diff: &TextDiff<'_, '_, str>, options: &TextOptions) -> String {
    let mut out = String::new();
    for op in diff.ops().iter().filter(|op| op.tag() != DiffTag::Equal) {
        let letter = match op.tag() {
            DiffTag::Insert => 'a',
            DiffTag::Delete => 'd',
            _ => 'c',
        };
        let _ = writeln!(
            out,
            "{}{letter}{}",
            address(&op.old_range()),
            address(&op.new_range())
        );
        let (old, new) = op_windows(diff, op, options.window);
        for (index, text) in op.old_range().zip(old) {
            let _ = writeln!(out, "< {text}");
            mark_unterminated(&mut out, diff.old_slice(index));
        }
        if op.tag() == DiffTag::Replace {
            out.push_str("---\n");
        }
        for (index, text) in op.new_range().zip(new) {
            let _ = writeln!(out, "> {text}");
            mark_unterminated(&mut out, diff.new_slice(index));
        }
    }
    out
}

fn context_view(diff: &TextDiff<'_, '_, str>, options: &TextOptions, context: usize) -> String {
    // Reuse Git quoting for labels, dropping only the synthetic side prefix.
    let label = |name: &str| git_path("", name).replacen('/', "", 1);
    let mut out = format!(
        "*** {}\n--- {}\n",
        label(&options.unified.old_name),
        label(&options.unified.new_name)
    );
    for ops in diff.grouped_ops(context) {
        let Some(first) = ops.first() else { continue };
        let last = ops.last().unwrap();
        let olds = first.old_range().start..last.old_range().end;
        let news = first.new_range().start..last.new_range().end;
        out.push_str("***************\n");
        let _ = writeln!(out, "*** {} ****", address(&olds));
        let windows: Vec<_> = ops
            .iter()
            .map(|op| op_windows(diff, op, options.window))
            .collect();
        for (op, (old, _)) in ops.iter().zip(&windows) {
            let prefix = match op.tag() {
                DiffTag::Equal => "  ",
                DiffTag::Replace => "! ",
                _ => "- ",
            };
            for (index, text) in op.old_range().zip(old) {
                let _ = writeln!(out, "{prefix}{text}");
                mark_unterminated(&mut out, diff.old_slice(index));
            }
        }
        let _ = writeln!(out, "--- {} ----", address(&news));
        for (op, (_, new)) in ops.iter().zip(&windows) {
            let prefix = match op.tag() {
                DiffTag::Equal => "  ",
                DiffTag::Replace => "! ",
                _ => "+ ",
            };
            for (index, text) in op.new_range().zip(new) {
                let _ = writeln!(out, "{prefix}{text}");
                mark_unterminated(&mut out, diff.new_slice(index));
            }
        }
    }
    out
}

/// Escape literal text before surrounding it with generated revision marks.
/// Backslashes must be doubled before the renderer escapes mark delimiters.
pub(crate) fn critic_literal(text: &str) -> String {
    let text = text.replace('\\', "\\\\");
    crate::markdown::diff_markdown(&text, &text)
}

/// One stretch of a word-level change line.
enum Piece {
    Same(String),
    Delete(String),
    Insert(String),
    Replace(String, String),
}

impl Piece {
    /// The texts the line shows, in order: one, or the old and new text.
    fn texts(&self) -> [&str; 2] {
        match self {
            Self::Same(t) | Self::Delete(t) | Self::Insert(t) => [t, ""],
            Self::Replace(a, b) => [a, b],
        }
    }

    fn len(&self) -> usize {
        self.texts().iter().map(|t| t.chars().count()).sum()
    }
}

fn word_pieces(old: &str, new: &str) -> Vec<Piece> {
    let old_tokens = crate::util::word_tokens(old);
    let new_tokens = crate::util::word_tokens(new);
    let diff = TextDiff::from_slices(&old_tokens, &new_tokens);
    diff.ops()
        .iter()
        .map(|op| {
            let a = old_tokens[op.old_range()].concat();
            let b = new_tokens[op.new_range()].concat();
            match op.tag() {
                DiffTag::Equal => Piece::Same(a),
                DiffTag::Delete => Piece::Delete(a),
                DiffTag::Insert => Piece::Insert(b),
                DiffTag::Replace => Piece::Replace(a, b),
            }
        })
        .collect()
}

#[cfg(test)]
fn word_change(old: &str, new: &str) -> String {
    critic_line(&word_pieces(old, new), None)
}

/// The pieces as CriticMarkup, keeping `window` scalars of text around the
/// first change. The window counts text, never delimiters: a mark it
/// reaches keeps its delimiters and loses only text, so every mark the
/// line opens, it closes. Text left out becomes `…`.
fn critic_line(pieces: &[Piece], window: Option<usize>) -> String {
    let len: usize = pieces.iter().map(Piece::len).sum();
    let shown = match window {
        Some(width) if len > width => {
            let at: usize = pieces
                .iter()
                .take_while(|p| matches!(p, Piece::Same(_)))
                .map(Piece::len)
                .sum();
            let start = at.saturating_sub(width / 2).min(len);
            start..start.saturating_add(width).min(len)
        }
        _ => 0..len,
    };
    // The visible part of the text at `offset`, as scalar offsets into it.
    let visible = |offset: usize, n: usize| {
        let from = shown.start.clamp(offset, offset + n) - offset;
        let to = shown.end.clamp(offset, offset + n) - offset;
        (from < to).then_some((from, to))
    };
    let slice = |text: &str, (from, to): (usize, usize)| {
        critic_literal(&text.chars().skip(from).take(to - from).collect::<String>())
    };
    let mut out = String::new();
    let mut offset = 0;
    // Text up to here is written or stands behind a `…`.
    let mut covered = 0;
    for piece in pieces {
        let end = offset + piece.len();
        let (open, close) = match piece {
            Piece::Same(text) => {
                let n = text.chars().count();
                if let Some(part) = visible(offset, n) {
                    if offset + part.0 > covered {
                        out.push('…');
                    }
                    out.push_str(&slice(text, part));
                    covered = offset + part.1;
                }
                offset = end;
                continue;
            }
            Piece::Delete(_) => ("{--", "--}"),
            Piece::Insert(_) => ("{++", "++}"),
            Piece::Replace(..) => ("{~~", "~~}"),
        };
        if shown.start >= end || offset >= shown.end {
            offset = end;
            continue;
        }
        if offset > covered {
            out.push('…');
        }
        out.push_str(open);
        let texts = piece.texts();
        let parts = if matches!(piece, Piece::Replace(..)) {
            2
        } else {
            1
        };
        for (i, text) in texts.iter().take(parts).enumerate() {
            if i == 1 {
                out.push_str("~>");
            }
            // The window starts at or before the first change, so it never
            // cuts a mark's text at its start; and a mark's text is never
            // empty.
            let n = text.chars().count();
            match visible(offset, n) {
                Some(part) => {
                    out.push_str(&slice(text, part));
                    if part.1 < n {
                        out.push('…');
                    }
                }
                None => out.push('…'),
            }
            offset += n;
        }
        out.push_str(close);
        covered = end;
    }
    if covered < len {
        out.push('…');
    }
    out
}

fn rows_view(diff: &TextDiff<'_, '_, str>, options: &TextOptions, context: usize) -> String {
    let word = options.format == TextFormat::Word;
    let mut out = String::new();
    // Side-by-side rows: the old cell, the gutter mark, the new cell.
    let mut side: Vec<(String, char, String)> = Vec::new();
    let cut = |text: &str, at: usize| clip(text, at, options.window);
    for ops in diff.grouped_ops(if word { 0 } else { context }) {
        for op in &ops {
            let (old, new) = op_lines(diff, op);
            if op.tag() == DiffTag::Equal {
                if !word {
                    for text in old {
                        let text = cut(text, 0);
                        side.push((text.clone(), ' ', text));
                    }
                }
                continue;
            }
            for row in replacement_rows(&old, &new) {
                let (a, b) = match row {
                    Row::Pair(i, j) => (old[i], new[j]),
                    Row::Delete(i) => (old[i], ""),
                    Row::Insert(j) => ("", new[j]),
                };
                if word {
                    // A blank line has no word to mark: an empty mark keeps
                    // its direction.
                    let line = match row {
                        Row::Delete(_) if a.is_empty() => "{----}".to_string(),
                        Row::Insert(_) if b.is_empty() => "{++++}".to_string(),
                        _ => critic_line(&word_pieces(a, b), options.window),
                    };
                    let _ = writeln!(out, "{line}");
                    continue;
                }
                side.push(match row {
                    Row::Pair(..) => {
                        let at = first_difference(a, b);
                        (cut(a, at), '|', cut(b, at))
                    }
                    Row::Delete(..) => (cut(a, 0), '<', String::new()),
                    Row::Insert(..) => (String::new(), '>', cut(b, 0)),
                });
            }
        }
    }
    // Pad the old column to its widest cell, in scalars like the window, so
    // every gutter mark and every new cell starts in the same column. Whole
    // lines pad no wider than a default window and its two `…`: one long
    // paragraph would otherwise pad every row to its length. A longer old
    // cell pushes only its own row's mark right.
    let width = side
        .iter()
        .map(|(a, ..)| a.chars().count())
        .max()
        .unwrap_or(0)
        .min(options.window.map_or(WINDOW + 2, |_| usize::MAX));
    for (a, mark, b) in side {
        if b.is_empty() {
            let _ = writeln!(out, "{}", format!("{a:<width$} {mark}").trim_end());
        } else {
            let _ = writeln!(out, "{a:<width$} {mark} {b}");
        }
    }
    out
}

/// Reads a complete textual snapshot of `source` — the same view
/// `jubarte debug --check text` prints, preserving the snapshots'
/// tracked marks. Kept for callers that need the raw text of one
/// document before comparing it; the review paths go through
/// [`diff_text_view`] / [`diff_documents_view`].
///
/// # Errors
///
/// A message when `source` is [`Source::Docx`] and the package cannot
/// be admitted.
pub fn document_text(source: Source<'_>) -> Result<String, String> {
    match source {
        Source::Markdown(text) => Ok(text.to_string()),
        Source::Docx(bytes) => crate::debug::document_text(bytes),
    }
}

/// Compares two plain-text snapshots (`old`, `new`) as a standard unified
/// patch (Git C-style quoted names, `@@` hunk headers, `context`-line
/// surroundings, `\ No newline at end of file` markers when the last line
/// has no newline). Backs `jubarte diff` and the bindings; CRLF line ends
/// are normalized to LF. Returns an empty string when `old == new`.
pub fn diff_text(old: &str, new: &str, options: &UnifiedOptions) -> String {
    if old == new {
        return String::new();
    }
    let old_name = git_path("a", &options.old_name);
    let new_name = git_path("b", &options.new_name);
    let diff = similar::TextDiff::from_lines(old, new);
    let hunks = diff
        .unified_diff()
        .context_radius(
            options
                .context
                .min(old.lines().count().max(new.lines().count())),
        )
        .header(&old_name, &new_name)
        .to_string();
    format!("diff --git {old_name} {new_name}\n{hunks}")
}

// Git C-style path quoting keeps control characters out of patch headers.
fn git_path(side: &str, name: &str) -> String {
    let path = format!("{side}/{name}");
    if !path
        .chars()
        .any(|c| c.is_control() || matches!(c, '"' | '\\' | ' '))
    {
        return path;
    }
    let mut out = String::from("\"");
    for byte in path.bytes() {
        match byte {
            b'"' => out.push_str("\\\""),
            b'\\' => out.push_str("\\\\"),
            b'\t' => out.push_str("\\t"),
            b'\n' => out.push_str("\\n"),
            b'\r' => out.push_str("\\r"),
            0..=31 | 127..=255 => {
                use std::fmt::Write as _;
                let _ = write!(out, "\\{byte:03o}");
            }
            _ => out.push(char::from(byte)),
        }
    }
    out.push('"');
    out
}

/// Compares two DOCX byte slices or two Markdown strings as a standard
/// unified patch without constructing a redline. Backs `jubarte diff
/// old.docx new.docx` and the bindings.
///
/// # Errors
///
/// A message when `old` or `new` is [`Source::Docx`] and the package
/// cannot be admitted.
pub fn diff_documents(
    old: Source<'_>,
    new: Source<'_>,
    options: &UnifiedOptions,
) -> Result<String, String> {
    Ok(diff_text(
        &document_text(old)?,
        &document_text(new)?,
        options,
    ))
}

#[cfg(test)]
#[cfg_attr(all(coverage_nightly, test), coverage(off))]
mod tests {
    use super::*;
    use crate::markdown::{DocxOptions, markdown_to_docx};

    fn word(text: &str) -> Vec<u8> {
        markdown_to_docx(text, &DocxOptions::default())
            .unwrap()
            .docx
    }

    fn word_view(old: &str, new: &str, window: Option<usize>) -> String {
        let options = TextOptions {
            format: TextFormat::Word,
            window,
            ..Default::default()
        };
        diff_text_view(old, new, &options)
    }

    /// Every mark a line opens, it closes, in CriticMarkup order.
    fn marks_close(line: &str) -> bool {
        let mut rest = line;
        while let Some(at) = ["{++", "{--", "{~~"]
            .iter()
            .filter_map(|open| rest.find(open))
            .min()
        {
            let close: &[&str] = match &rest[at..at + 3] {
                "{++" => &["++}"],
                "{--" => &["--}"],
                _ => &["~>", "~~}"],
            };
            rest = &rest[at + 3..];
            for delimiter in close {
                let Some(end) = rest.find(delimiter) else {
                    return false;
                };
                if ["{++", "{--", "{~~"]
                    .iter()
                    .any(|o| rest[..end].contains(o))
                {
                    return false;
                }
                rest = &rest[end + delimiter.len()..];
            }
        }
        !["++}", "--}", "~~}", "~>"].iter().any(|c| rest.contains(c))
    }

    #[test]
    fn a_blank_line_change_keeps_its_direction_in_the_word_view() {
        for window in [Some(70), None] {
            assert_eq!(word_view("a\nb\n", "a\n\nb\n", window), "{++++}\n");
            assert_eq!(word_view("a\n\nb\n", "a\nb\n", window), "{----}\n");
            // A blank line replacing text is still the text's deletion.
            assert_eq!(word_view("a\nx\nb\n", "a\n\nb\n", window), "{--x--}\n");
        }
    }

    #[test]
    fn a_word_window_keeps_every_mark_closed() {
        let tail = "and the tail of an inserted clause that runs well past the window";
        let cases = [
            (
                "Pay within thirty days.".to_string(),
                format!("Pay within thirty days {tail}."),
            ),
            (format!("Pay {tail} now."), "Pay now.".to_string()),
            ("Keep old wording here".to_string(), format!("Keep {tail}")),
            (
                format!("A {tail} B one C"),
                format!("A {tail} B two C three D four"),
            ),
            (
                format!("{} lead then old", "\\".repeat(2_000)),
                format!("{} lead then new", "\\".repeat(2_000)),
            ),
        ];
        for (old, new) in &cases {
            for width in 0..90 {
                let out = word_view(old, new, Some(width));
                assert!(marks_close(&out), "width {width}: {out}");
                // Any window wider than nothing shows the first change.
                assert_eq!(out.contains('{'), width > 0, "width {width}: {out}");
            }
            assert!(marks_close(&word_view(old, new, None)));
        }
        // Text is cut, never a delimiter: the window counts text only.
        assert_eq!(
            word_view("a b", "a b c d e f g h i j", Some(10)),
            "a b{++ c d e …++}\n"
        );
        assert_eq!(
            word_view("keep one two three four five", "keep six", Some(10)),
            "keep {~~one t…~>…~~}\n"
        );
        assert_eq!(
            word_view(
                "lead words here, then old",
                "lead words here, then new",
                Some(12)
            ),
            "… then {~~old~>new~~}\n"
        );
        assert_eq!(word_view("a b", "a b c d e f g h i j", Some(0)), "…\n");
    }

    #[test]
    fn side_by_side_columns_line_up_on_every_row() {
        let options = TextOptions {
            format: TextFormat::SideBySide,
            ..Default::default()
        };
        let view = |old: &str, new: &str| diff_text_view(old, new, &options);
        assert_eq!(
            view("same\nold\nend\n", "same\nnew\nend\n"),
            "same   same\nold  | new\nend    end\n"
        );
        assert_eq!(
            view("a\nlong line\n", "a\n"),
            "a           a\nlong line <\n"
        );
        assert_eq!(view("a\n", "a\nadded\n"), "a   a\n  > added\n");
        // Widths count scalars, as windows do.
        assert_eq!(view("é界\nx\n", "é界\ny\n"), "é界   é界\nx  | y\n");
        // An empty unchanged line leaves no trailing spaces.
        assert_eq!(view("\nold\n", "\nnew\n"), "\nold | new\n");
        // Whole lines pad to a default window at most; the long row alone
        // overflows.
        let options = TextOptions {
            window: None,
            ..options
        };
        let long = "x".repeat(200);
        let out = diff_text_view(&format!("a\n{long}\nb\n"), "a\nb\n", &options);
        let pad = " ".repeat(WINDOW + 1);
        assert_eq!(out, format!("a{pad}   a\n{long} <\nb{pad}   b\n"));
    }

    #[test]
    fn review_windows_count_scalars_and_allow_unbounded_and_zero_width() {
        assert_eq!(clip("é界xyz", 3, Some(2)), "…xy…");
        assert_eq!(clip("é界xyz", 0, Some(2)), "é界…");
        assert_eq!(clip("é界xyz", 5, Some(2)), "…z");
        assert_eq!(clip("é界xyz", 3, None), "é界xyz");
        assert_eq!(clip("short", 100, Some(70)), "short");
        assert_eq!(clip("abc", 0, Some(0)), "…");
        assert_eq!(clip("abc", 1, Some(0)), "……");
        assert_eq!(clip("", 0, Some(0)), "");
        assert_eq!(first_difference("é界", "é中"), 1);
        assert_eq!(first_difference("same", "same suffix"), 4);
    }

    /// A clipped window is sized from its UTF-8 bytes and ellipses once
    /// (#4372659578, #4372904367 on #1830).
    #[test]
    fn a_clipped_window_is_allocated_once() {
        for (text, at, width) in [
            ("é界xyz", 3, 2),
            ("界界界界界界", 3, 2),
            ("é界xyz", 0, 2),
            ("abc", 1, 0),
        ] {
            let out = clip(text, at, Some(width));
            assert_eq!(out.capacity(), out.len(), "{text:?} at {at}: {out:?}");
        }
    }

    #[test]
    fn replacement_alignment_handles_inserted_and_deleted_clauses_and_large_blocks() {
        assert_eq!(
            replacement_rows(
                &["Payment 30 days", "Security assets"],
                &["Prepayment allowed", "Payment 60 days", "Security property"]
            ),
            [Row::Insert(0), Row::Pair(0, 1), Row::Pair(1, 2)]
        );
        assert_eq!(
            replacement_rows(
                &["Payment 60 days", "Payment 30 days"],
                &["Payment 60 days"]
            ),
            [Row::Pair(0, 0), Row::Delete(1)]
        );
        assert_eq!(
            replacement_rows(&["Payment stale", "Payment 30 days"], &["Payment 60 days"]),
            [Row::Delete(0), Row::Pair(1, 0)]
        );
        assert_eq!(replacement_rows(&["a"], &["b"]), [Row::Pair(0, 0)]);
        assert_eq!(replacement_rows(&["a"], &[]), [Row::Delete(0)]);
        assert_eq!(replacement_rows(&[], &["b"]), [Row::Insert(0)]);
        let rows = replacement_rows(&vec!["old"; 21], &vec!["new"; 22]);
        assert_eq!(rows.len(), 22);
        assert_eq!(rows.last(), Some(&Row::Insert(21)));
    }

    #[test]
    fn word_changes_preserve_literal_syntax_and_changed_clause_numbers() {
        assert_eq!(word_change("1. Terms", "2. Terms"), "{~~1~>2~~}. Terms");
        assert_eq!(
            word_change("pay 30 days", "pay 60 days"),
            "pay {~~30~>60~~} days"
        );
        assert_eq!(word_change("", "new"), "{++new++}");
        assert_eq!(word_change("old", ""), "{--old--}");
        assert_eq!(word_change("same", "same"), "same");
        // Literal markup is text, escaped.
        assert_eq!(
            word_change("{++literal++} old", "{++literal++} new"),
            r"\{++literal++\} {~~old~>new~~}"
        );
        assert_eq!(
            critic_literal("{+literal+}[-literal-]"),
            "{+literal+}[-literal-]"
        );
        assert_eq!(critic_literal("{++literal++}"), r"\{++literal++\}");
    }

    #[test]
    fn review_context_and_unterminated_github_lines_use_original_coordinates() {
        let options = TextOptions {
            window: Some(70),
            ..Default::default()
        };
        assert_eq!(
            diff_text_view("a", "b", &options),
            diff_text("a", "b", &options.unified)
        );
        let options = TextOptions {
            format: TextFormat::SideBySide,
            ..options
        };
        assert_eq!(
            diff_text_view("same\nold\nend\n", "same\nnew\nend\n", &options),
            "same   same\nold  | new\nend    end\n"
        );
        let options = TextOptions {
            format: TextFormat::Context,
            unified: UnifiedOptions {
                old_name: "old\nfile".into(),
                new_name: "new file".into(),
                context: 0,
            },
            ..options
        };
        assert!(
            diff_text_view("a\n", "b\n", &options)
                .starts_with("*** \"old\\nfile\"\n--- \"new file\"\n")
        );
    }

    #[test]
    fn patch_has_git_headers_ranges_and_context() {
        let out = diff_text(
            "Intro\nThirty days\nSigned\n",
            "Intro\nSixty days\nSigned\n",
            &UnifiedOptions::default(),
        );
        assert_eq!(
            out,
            "diff --git a/old.docx b/new.docx\n--- a/old.docx\n+++ b/new.docx\n@@ -1,3 +1,3 @@\n Intro\n-Thirty days\n+Sixty days\n Signed\n"
        );
    }

    #[test]
    fn line_terminators_never_show_as_identical_changed_lines() {
        let formats = [
            TextFormat::Github,
            TextFormat::Word,
            TextFormat::Normal,
            TextFormat::Context,
            TextFormat::SideBySide,
        ];
        for format in formats {
            for window in [Some(70), None] {
                let options = TextOptions {
                    format,
                    window,
                    ..Default::default()
                };
                // CRLF against LF is the same document text.
                assert_eq!(
                    diff_text_view("one\r\ntwo\r\n", "one\ntwo\n", &options),
                    "",
                    "{format:?} {window:?}"
                );
                // A missing final newline is marked where the format has a
                // marker for it, and is no change where it has none.
                let out = diff_text_view("same", "same\n", &options);
                match format {
                    TextFormat::Github | TextFormat::Normal | TextFormat::Context => assert_eq!(
                        out.matches("\\ No newline at end of file").count(),
                        1,
                        "{format:?} {window:?}: {out}"
                    ),
                    TextFormat::Word | TextFormat::SideBySide => {
                        assert_eq!(out, "", "{format:?} {window:?}");
                    }
                }
            }
        }
    }

    #[test]
    fn empty_ranges_and_missing_final_newlines_follow_unified_convention() {
        let options = UnifiedOptions {
            context: 0,
            ..Default::default()
        };
        let inserted = diff_text("", "Hello\n", &options);
        assert!(inserted.contains("@@ -0,0 +1 @@\n+Hello\n"), "{inserted}");
        let deleted = diff_text("Hello\n", "", &options);
        assert!(deleted.contains("@@ -1 +0,0 @@\n-Hello\n"), "{deleted}");
        let unterminated = diff_text("Old", "New", &options);
        assert_eq!(
            unterminated.matches("\\ No newline at end of file").count(),
            2
        );
    }

    #[test]
    fn identical_content_has_no_patch_even_with_different_names() {
        assert_eq!(
            diff_text("same\n", "same\n", &UnifiedOptions::default()),
            ""
        );
        assert_eq!(diff_text("", "", &UnifiedOptions::default()), "");
    }

    #[test]
    fn names_with_control_characters_are_git_quoted() {
        let options = UnifiedOptions {
            old_name: "a\t\".docx".into(),
            new_name: "b\n.docx".into(),
            context: 0,
        };
        let out = diff_text("a\n", "b\n", &options);
        assert!(
            out.starts_with("diff --git \"a/a\\t\\\".docx\" \"b/b\\n.docx\"\n"),
            "{out}"
        );
        assert!(out.contains("--- \"a/a\\t\\\".docx\"\n"));
    }

    #[test]
    fn every_change_and_long_unicode_line_is_preserved() {
        let old = (0..12)
            .map(|n| format!("old {n} {}\n", "é".repeat(300)))
            .collect::<String>();
        let new = old.replace("old ", "new ");
        let out = diff_text(&old, &new, &UnifiedOptions::default());
        assert_eq!(out.lines().filter(|l| l.starts_with("-old ")).count(), 12);
        assert!(out.contains(&"é".repeat(300)));
        assert!(!out.contains('…'));
    }

    #[test]
    fn word_snapshot_preserves_existing_marks_and_compares_them() {
        let old = word("Due in {~~30~>45~~} days.\n");
        let new = word("Due in {~~30~>60~~} days.\n");
        let snapshot = document_text(Source::Docx(&old)).unwrap();
        // The marks retain document order: the substitution writes its
        // deletion before its insertion.
        assert!(snapshot.contains("[-30-]{+45+}"), "{snapshot}");
        let out = diff_documents(
            Source::Docx(&old),
            Source::Docx(&new),
            &UnifiedOptions::default(),
        )
        .unwrap();
        assert!(out.contains("[-30-]{+45+}"), "{out}");
        assert!(out.contains("[-30-]{+60+}"), "{out}");
        assert_eq!(
            diff_documents(
                Source::Docx(&old),
                Source::Docx(&old),
                &UnifiedOptions::default()
            )
            .unwrap(),
            ""
        );
    }

    #[test]
    fn invalid_docx_is_refused() {
        assert!(document_text(Source::Docx(b"not a document")).is_err());
    }

    #[test]
    fn markdown_and_mixed_sources_share_the_same_formatter() {
        let old = word("Hello\n");
        let snapshot = document_text(Source::Docx(&old)).unwrap();
        assert_eq!(
            diff_documents(
                Source::Docx(&old),
                Source::Markdown(&snapshot),
                &UnifiedOptions::default()
            )
            .unwrap(),
            ""
        );
        assert!(
            diff_documents(
                Source::Markdown("Old\n"),
                Source::Markdown("New\n"),
                &UnifiedOptions::default()
            )
            .unwrap()
            .contains("-Old\n+New\n")
        );
    }
}