hewdiff 0.5.0

High-performance review-first terminal diff viewer with PR-style comments
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
//! Flatten a [`Changeset`] into lightweight rows for rendering + anchoring.

use crate::comments::model::{CommentStore, Thread};
use crate::diff::model::{Changeset, LineKind, Side};
use std::collections::{HashMap, HashSet};
use std::path::Path;
use std::time::{SystemTime, UNIX_EPOCH};
use unicode_width::UnicodeWidthChar;

/// Terminal display width of a string in cells (wide/CJK glyphs count as 2,
/// zero-width/control as 0). The whole TUI lays text out in fixed cells, so
/// column math must measure cells, not `char`s, or wide glyphs misalign.
pub fn str_width(s: &str) -> usize {
    s.chars().map(char_width).sum()
}

/// Display width of a single char (control chars treated as 0; they're stripped
/// before rendering anyway).
pub fn char_width(c: char) -> usize {
    UnicodeWidthChar::width(c).unwrap_or(0)
}

/// Take the longest prefix of `s` whose display width does not exceed `max`,
/// returning the prefix and its actual width. A wide glyph straddling the
/// boundary is dropped (so the result never overflows `max`).
pub fn take_width(s: &str, max: usize) -> (String, usize) {
    let mut out = String::new();
    let mut w = 0;
    for c in s.chars() {
        let cw = char_width(c);
        if w + cw > max {
            break;
        }
        out.push(c);
        w += cw;
    }
    (out, w)
}

/// Format a `SystemTime` as a UTC `YYYY-MM-DD HH:MM` timestamp (no external
/// date crate).
fn fmt_date(t: SystemTime) -> String {
    let secs = t.duration_since(UNIX_EPOCH).unwrap_or_default().as_secs() as i64;
    let days = secs.div_euclid(86_400);
    let tod = secs.rem_euclid(86_400);
    let (hh, mm) = (tod / 3600, (tod % 3600) / 60);
    // Howard Hinnant's civil-from-days algorithm.
    let z = days + 719_468;
    let era = if z >= 0 { z } else { z - 146_096 } / 146_097;
    let doe = z - era * 146_097;
    let yoe = (doe - doe / 1460 + doe / 36_524 - doe / 146_096) / 365;
    let y = yoe + era * 400;
    let doy = doe - (365 * yoe + yoe / 4 - yoe / 100);
    let mp = (5 * doy + 2) / 153;
    let d = doy - (153 * mp + 2) / 5 + 1;
    let m = if mp < 10 { mp + 3 } else { mp - 9 };
    let y = y + if m <= 2 { 1 } else { 0 };
    format!("{y:04}-{m:02}-{d:02} {hh:02}:{mm:02}")
}

/// One visual line of an inline-expanded comment thread.
#[derive(Debug, Clone)]
pub enum CommentKind {
    /// Top rounded border of the thread box.
    Top,
    /// Thread header: message count (resolved state lives on `CommentLine`).
    Head { replies: usize },
    /// A message author line (`@name`) with its formatted date.
    Author { name: String, date: String },
    /// A (pre-wrapped) body line.
    Body(String),
    /// Blank spacer between messages.
    Gap,
    /// The action-button row (reply / resolve / delete) above the bottom border.
    Actions,
    /// Bottom rounded border of the thread box.
    Bottom,
}

/// One visual line of a thread box, tagged with the thread's `resolved` state
/// so the renderer can dim the whole box (not just the header) when resolved,
/// plus identity so the line can be selected/focused. `comment_id` is the
/// owning message for content lines (`Author`/`Body`/`Gap`) and `None` for
/// thread chrome (`Top`/`Head`/`Bottom`) — selection keys off it, so a message
/// (author + its body lines) forms one selectable unit.
#[derive(Debug, Clone)]
pub struct CommentLine {
    pub kind: CommentKind,
    pub resolved: bool,
    pub thread_id: String,
    pub comment_id: Option<String>,
}

/// Where an open inline composer attaches in the row stream.
#[derive(Debug, Clone)]
pub enum ComposerAnchor {
    /// A new thread, anchored to the first line of the selected diff range.
    NewThread {
        file_idx: usize,
        side: Side,
        line: u32,
    },
    /// A reply, injected just below an existing thread's box.
    Reply { thread_id: String },
}

/// An open inline composer to inject into the row stream while typing.
#[derive(Debug, Clone)]
pub struct ComposerSpec {
    pub anchor: ComposerAnchor,
    pub title: String,
    pub body: String,
}

/// One visual line of the inline composer box.
#[derive(Debug, Clone)]
pub enum ComposerKind {
    /// Top rounded border, carrying the box title.
    Top { title: String },
    /// A (pre-wrapped) body line; the line at the cursor carries the caret glyph.
    Body(String),
    /// The one-line key hint above the bottom border.
    Hint,
    /// Bottom rounded border.
    Bottom,
}

#[derive(Debug, Clone)]
pub struct ComposerLine {
    pub kind: ComposerKind,
}

/// A row injected after a code line: an existing comment thread line, or a line
/// of the live composer box. Internal to row building — not part of the crate
/// API.
enum Injected {
    Comment(CommentLine),
    Composer(ComposerLine),
}

/// Expand an open composer into wrapped visual lines (a rounded box with the
/// title, the live buffer + caret, and a key hint).
fn composer_lines(spec: &ComposerSpec, width: usize) -> Vec<ComposerLine> {
    let mut out = vec![ComposerLine {
        kind: ComposerKind::Top {
            title: spec.title.clone(),
        },
    }];
    // `spec.body` already carries the caret glyph at the cursor position (see
    // `body_with_caret`); the glyph survives `sanitize_line` (not a control
    // char) and wraps with the surrounding text.
    for raw in spec.body.split('\n') {
        let s = sanitize_line(raw);
        if s.is_empty() {
            out.push(ComposerLine {
                kind: ComposerKind::Body(String::new()),
            });
        } else {
            for wl in wrap_preserve(&s, width) {
                out.push(ComposerLine {
                    kind: ComposerKind::Body(wl),
                });
            }
        }
    }
    out.push(ComposerLine {
        kind: ComposerKind::Hint,
    });
    out.push(ComposerLine {
        kind: ComposerKind::Bottom,
    });
    out
}

/// Composer lines for a *new* thread anchored exactly at `(file_idx, side,
/// line)`, emitted at most once per build (tracked via `emitted`).
fn new_thread_composer(
    composer: Option<&ComposerSpec>,
    emitted: &mut bool,
    file_idx: usize,
    side: Side,
    line: u32,
    width: usize,
) -> Vec<ComposerLine> {
    if *emitted {
        return Vec::new();
    }
    if let Some(spec) = composer {
        if let ComposerAnchor::NewThread {
            file_idx: f,
            side: s,
            line: l,
        } = spec.anchor
        {
            if f == file_idx && s == side && l == line {
                *emitted = true;
                return composer_lines(spec, width);
            }
        }
    }
    Vec::new()
}

/// Greedy word-wrap to `width` display columns, hard-splitting over-long words.
/// All measurements are in terminal cells (wide glyphs count as 2).
fn wrap_text(s: &str, width: usize) -> Vec<String> {
    let width = width.max(1);
    let mut out = Vec::new();
    let mut line = String::new();
    let mut w = 0usize;
    // Append `word` to the current line, hard-splitting it across lines when it
    // overflows. Only break before a glyph when the current line is non-empty,
    // so a single glyph wider than `width` (e.g. a CJK char at width 1) lands on
    // its own line instead of emitting a spurious empty line ahead of it.
    let push_overlong = |word: &str, out: &mut Vec<String>, line: &mut String, w: &mut usize| {
        for ch in word.chars() {
            let cw = char_width(ch);
            if *w > 0 && *w + cw > width {
                out.push(std::mem::take(line));
                *w = 0;
            }
            line.push(ch);
            *w += cw;
        }
    };
    let push_word = |word: &str, out: &mut Vec<String>, line: &mut String, w: &mut usize| {
        let ww = str_width(word);
        if *w == 0 {
            push_overlong(word, out, line, w);
        } else if *w + 1 + ww <= width {
            line.push(' ');
            line.push_str(word);
            *w += 1 + ww;
        } else {
            out.push(std::mem::take(line));
            *w = 0;
            push_overlong(word, out, line, w);
        }
    };
    for word in s.split_whitespace() {
        push_word(word, &mut out, &mut line, &mut w);
    }
    out.push(line);
    out
}

/// Width-wrap that preserves every character verbatim — including runs of
/// spaces and leading/trailing whitespace. Used by the comment composer, which
/// is a live text buffer: unlike `wrap_text` (a display formatter that collapses
/// whitespace via `split_whitespace`), the editor must render exactly what the
/// user typed. Breaks greedily at the display-width boundary, preferring the
/// last space on the line so words don't split mid-token when avoidable.
fn wrap_preserve(s: &str, width: usize) -> Vec<String> {
    let width = width.max(1);
    let mut out = Vec::new();
    let mut line = String::new();
    let mut w = 0usize;
    for ch in s.chars() {
        let cw = char_width(ch);
        if w + cw > width && !line.is_empty() {
            // Try to break at the last space so a word isn't split needlessly.
            if ch != ' ' {
                if let Some(brk) = line.rfind(' ') {
                    // Don't break on a trailing run of spaces (brk at end).
                    let tail: String = line[brk + 1..].to_string();
                    if !tail.is_empty() {
                        // Keep the break space on the first visual line so no
                        // character is dropped — this is a live edit buffer, and
                        // the rendered text (and caret position) must match the
                        // buffer verbatim.
                        line.truncate(brk + 1);
                        out.push(std::mem::take(&mut line));
                        line = tail;
                        w = str_width(&line);
                    } else {
                        out.push(std::mem::take(&mut line));
                        w = 0;
                    }
                } else {
                    out.push(std::mem::take(&mut line));
                    w = 0;
                }
            } else {
                out.push(std::mem::take(&mut line));
                w = 0;
            }
        }
        line.push(ch);
        w += cw;
    }
    out.push(line);
    out
}

/// Expand a thread into wrapped visual lines.
pub fn thread_lines(t: &Thread, width: usize) -> Vec<CommentLine> {
    // Chrome lines (Top/Head/Bottom) carry no message id; content lines carry
    // their owning message's id so author + body + trailing gap select as one.
    let chrome = |kind: CommentKind| CommentLine {
        kind,
        resolved: t.resolved,
        thread_id: t.id.clone(),
        comment_id: None,
    };
    let content = |kind: CommentKind, cid: &str| CommentLine {
        kind,
        resolved: t.resolved,
        thread_id: t.id.clone(),
        comment_id: Some(cid.to_string()),
    };
    let mut out = vec![
        chrome(CommentKind::Top),
        chrome(CommentKind::Head {
            replies: t.comments.len(),
        }),
    ];
    for (i, c) in t.comments.iter().enumerate() {
        out.push(content(
            CommentKind::Author {
                name: c.author.clone().unwrap_or_else(|| "?".into()),
                date: fmt_date(c.created_at),
            },
            &c.id,
        ));
        for raw in c.body.split('\n') {
            let s = sanitize_line(raw);
            if s.is_empty() {
                out.push(content(CommentKind::Body(String::new()), &c.id));
            } else {
                for wl in wrap_text(&s, width) {
                    out.push(content(CommentKind::Body(wl), &c.id));
                }
            }
        }
        if i + 1 < t.comments.len() {
            out.push(content(CommentKind::Gap, &c.id));
        }
    }
    out.push(chrome(CommentKind::Actions));
    out.push(chrome(CommentKind::Bottom));
    out
}

/// Index thread positions by their anchored file path, so the per-line
/// injection scans only the (usually few) threads on the current file instead
/// of every thread in the changeset. Built once per row rebuild.
type ThreadsByPath<'a> = std::collections::HashMap<&'a Path, Vec<usize>>;

fn threads_by_path(comments: &CommentStore) -> ThreadsByPath<'_> {
    let mut map: ThreadsByPath<'_> = std::collections::HashMap::new();
    for (i, t) in comments.threads.iter().enumerate() {
        map.entry(t.file.as_path()).or_default().push(i);
    }
    map
}

/// Map each thread (across the whole changeset, keyed by thread id) to the
/// *last* `(side, line)` anchor within its range that is actually present in
/// the diff. A range comment renders after this line, so its box sits below the
/// last selected line (GitHub-style) rather than the first.
fn last_anchor_lines(
    changeset: &Changeset,
    comments: &CommentStore,
    by_path: &ThreadsByPath<'_>,
) -> HashMap<String, (Side, u32)> {
    let mut m = HashMap::new();
    for file in &changeset.files {
        let Some(indices) = by_path.get(Path::new(file.display_path())) else {
            continue;
        };
        for &i in indices {
            let t = &comments.threads[i];
            let mut best: Option<u32> = None;
            for line in file.hunks.iter().flat_map(|h| h.lines.iter()) {
                let l = match t.side {
                    Side::Old => line.old_line,
                    Side::New => line.new_line,
                };
                if let Some(l) = l {
                    if t.range.contains(l) {
                        best = Some(best.map_or(l, |b| b.max(l)));
                    }
                }
            }
            if let Some(l) = best {
                m.insert(t.id.clone(), (t.side, l));
            }
        }
    }
    m
}

/// Inline-comment lines to inject after a code row, for every thread whose
/// last in-diff anchor line (see [`last_anchor_lines`]) is one of the row's
/// `(side, line)` anchors.
#[allow(clippy::too_many_arguments)]
fn comment_rows_for(
    comments: &CommentStore,
    by_path: &ThreadsByPath<'_>,
    last: &HashMap<String, (Side, u32)>,
    emitted: &mut HashSet<String>,
    path: &str,
    anchors: &[(Side, u32)],
    width: usize,
    composer: Option<&ComposerSpec>,
) -> Vec<(Side, Injected)> {
    let mut out = Vec::new();
    let Some(indices) = by_path.get(Path::new(path)) else {
        return out;
    };
    for &i in indices {
        let t = &comments.threads[i];
        // Emit each thread once, at the last line of its range present in the
        // diff. `emitted` guards against a repeated emit.
        if emitted.contains(&t.id) {
            continue;
        }
        if last
            .get(&t.id)
            .is_some_and(|&(ts, tl)| anchors.iter().any(|&(s, l)| s == ts && l == tl))
        {
            emitted.insert(t.id.clone());
            out.extend(
                thread_lines(t, width)
                    .into_iter()
                    .map(|cl| (t.side, Injected::Comment(cl))),
            );
            // A reply composer sits directly under the thread it replies to.
            if let Some(spec) = composer {
                if matches!(&spec.anchor, ComposerAnchor::Reply { thread_id } if *thread_id == t.id)
                {
                    out.extend(
                        composer_lines(spec, width)
                            .into_iter()
                            .map(|cl| (t.side, Injected::Composer(cl))),
                    );
                }
            }
        }
    }
    out
}

/// Make a line safe for a TUI cell grid. ratatui diffs cells between frames, so
/// a stray `\r`, tab, or ANSI escape corrupts the terminal and never self-heals.
/// We expand tabs (4-col stops), drop CR/LF, strip ANSI CSI/OSC sequences, and
/// drop any remaining control characters.
pub fn sanitize_line(s: &str) -> String {
    let mut out = String::with_capacity(s.len());
    let mut col = 0usize;
    let mut chars = s.chars().peekable();
    while let Some(c) = chars.next() {
        match c {
            '\t' => {
                let n = 4 - (col % 4);
                out.extend(std::iter::repeat_n(' ', n));
                col += n;
            }
            '\r' | '\n' => {}
            '\u{1b}' => match chars.peek() {
                // CSI: ESC [ ... <final 0x40..=0x7e>
                Some('[') => {
                    chars.next();
                    while let Some(&p) = chars.peek() {
                        chars.next();
                        if ('@'..='~').contains(&p) {
                            break;
                        }
                    }
                }
                // OSC: ESC ] ... (BEL | ESC \)
                Some(']') => {
                    chars.next();
                    while let Some(&p) = chars.peek() {
                        chars.next();
                        if p == '\u{7}' {
                            break;
                        }
                        if p == '\u{1b}' {
                            if chars.peek() == Some(&'\\') {
                                chars.next();
                            }
                            break;
                        }
                    }
                }
                _ => {}
            },
            c if c.is_control() => {}
            c => {
                out.push(c);
                col += 1;
            }
        }
    }
    out
}

#[derive(Debug, Clone)]
pub enum RowKind {
    FileHeader,
    HunkHeader,
    Line {
        kind: LineKind,
        old_line: Option<u32>,
        new_line: Option<u32>,
    },
    /// An inline-expanded comment-thread line (non-selectable).
    Comment(CommentLine),
    /// A line of the live inline composer box (non-selectable).
    Composer(ComposerLine),
}

#[derive(Debug, Clone)]
pub struct Row {
    pub file_idx: usize,
    pub kind: RowKind,
    pub text: String,
}

impl Row {
    /// The comment anchor (side, line) for a code line, if any.
    pub fn anchor(&self) -> Option<(Side, u32)> {
        match &self.kind {
            RowKind::Line {
                kind,
                old_line,
                new_line,
            } => match kind {
                LineKind::Deletion => old_line.map(|l| (Side::Old, l)),
                _ => new_line.map(|l| (Side::New, l)),
            },
            _ => None,
        }
    }
    pub fn is_selectable(&self) -> bool {
        matches!(self.kind, RowKind::Line { .. })
    }
}

/// One side (left=old / right=new) of a split row.
#[derive(Debug, Clone)]
pub struct SideCell {
    pub kind: LineKind,
    pub line: Option<u32>,
    pub text: String,
}

#[derive(Debug, Clone)]
pub enum SplitRowKind {
    FileHeader,
    HunkHeader,
    /// A side-by-side pair. Either side may be empty (padding).
    Pair {
        left: Option<SideCell>,
        right: Option<SideCell>,
    },
    /// An inline-expanded comment-thread line (non-selectable). `side` records
    /// which column (old=left / new=right) the thread is anchored to so split
    /// view can render it under the correct side.
    Comment {
        side: Side,
        line: CommentLine,
    },
    /// A line of the live inline composer box (non-selectable). `side` records
    /// the anchored column so split view renders it under the correct side.
    Composer {
        side: Side,
        line: ComposerLine,
    },
}

#[derive(Debug, Clone)]
pub struct SplitRow {
    pub file_idx: usize,
    pub kind: SplitRowKind,
    pub text: String, // header text
}

impl SplitRow {
    pub fn is_selectable(&self) -> bool {
        matches!(self.kind, SplitRowKind::Pair { .. })
    }

    /// Comment anchor: prefer the new (right) side, fall back to old (left).
    pub fn anchor(&self) -> Option<(Side, u32)> {
        match &self.kind {
            SplitRowKind::Pair { left, right } => right
                .as_ref()
                .and_then(|c| c.line.map(|l| (Side::New, l)))
                .or_else(|| left.as_ref().and_then(|c| c.line.map(|l| (Side::Old, l)))),
            _ => None,
        }
    }
}

fn hunk_header(hunk: &crate::diff::model::Hunk) -> String {
    format!(
        "@@ -{},{} +{},{} @@{}",
        hunk.old_start,
        hunk.old_count,
        hunk.new_start,
        hunk.new_count,
        hunk.section
            .as_ref()
            .map(|s| format!(" {s}"))
            .unwrap_or_default()
    )
}

/// Build the side-by-side (split) row list. Within a change block, consecutive
/// deletions (left) are zipped with consecutive additions (right); context
/// lines appear on both sides.
pub fn build_split_rows(
    changeset: &Changeset,
    comments: &CommentStore,
    width: usize,
    composer: Option<&ComposerSpec>,
) -> Vec<SplitRow> {
    let mut rows = Vec::new();
    let mut emitted: HashSet<String> = HashSet::new();
    let mut composer_emitted = false;
    let by_path = threads_by_path(comments);
    let last = last_anchor_lines(changeset, comments, &by_path);
    for (fi, file) in changeset.files.iter().enumerate() {
        let path = file.display_path();
        rows.push(SplitRow {
            file_idx: fi,
            kind: SplitRowKind::FileHeader,
            text: file.display_path().to_string(),
        });
        if file.is_binary {
            rows.push(SplitRow {
                file_idx: fi,
                kind: SplitRowKind::HunkHeader,
                text: "Binary file".into(),
            });
            continue;
        }
        for hunk in &file.hunks {
            rows.push(SplitRow {
                file_idx: fi,
                kind: SplitRowKind::HunkHeader,
                text: hunk_header(hunk),
            });
            let mut dels: Vec<SideCell> = Vec::new();
            let mut adds: Vec<SideCell> = Vec::new();
            for line in &hunk.lines {
                let cell = SideCell {
                    kind: line.kind,
                    line: match line.kind {
                        LineKind::Deletion => line.old_line,
                        _ => line.new_line,
                    },
                    text: sanitize_line(&line.text),
                };
                match line.kind {
                    LineKind::Deletion => dels.push(cell),
                    LineKind::Addition => adds.push(cell),
                    LineKind::Context => {
                        flush_pairs(
                            fi,
                            &mut dels,
                            &mut adds,
                            &mut rows,
                            comments,
                            &by_path,
                            &last,
                            &mut emitted,
                            path,
                            width,
                            composer,
                            &mut composer_emitted,
                        );
                        rows.push(SplitRow {
                            file_idx: fi,
                            kind: SplitRowKind::Pair {
                                left: Some(SideCell {
                                    kind: LineKind::Context,
                                    line: line.old_line,
                                    text: sanitize_line(&line.text),
                                }),
                                right: Some(cell),
                            },
                            text: String::new(),
                        });
                        let mut anchors = Vec::new();
                        if let Some(l) = line.old_line {
                            anchors.push((Side::Old, l));
                        }
                        if let Some(l) = line.new_line {
                            anchors.push((Side::New, l));
                        }
                        for (side, inj) in comment_rows_for(
                            comments,
                            &by_path,
                            &last,
                            &mut emitted,
                            path,
                            &anchors,
                            width,
                            composer,
                        ) {
                            rows.push(SplitRow {
                                file_idx: fi,
                                kind: split_injected(side, inj),
                                text: String::new(),
                            });
                        }
                        for (side, l) in &anchors {
                            for cl in new_thread_composer(
                                composer,
                                &mut composer_emitted,
                                fi,
                                *side,
                                *l,
                                width,
                            ) {
                                rows.push(SplitRow {
                                    file_idx: fi,
                                    kind: SplitRowKind::Composer {
                                        side: *side,
                                        line: cl,
                                    },
                                    text: String::new(),
                                });
                            }
                        }
                    }
                }
            }
            flush_pairs(
                fi,
                &mut dels,
                &mut adds,
                &mut rows,
                comments,
                &by_path,
                &last,
                &mut emitted,
                path,
                width,
                composer,
                &mut composer_emitted,
            );
        }
        for (side, inj) in
            orphan_thread_rows(comments, &by_path, &mut emitted, path, width, composer)
        {
            rows.push(SplitRow {
                file_idx: fi,
                kind: split_injected(side, inj),
                text: String::new(),
            });
        }
    }
    rows
}

/// Map an injected row to its split-view kind under column `side`.
fn split_injected(side: Side, inj: Injected) -> SplitRowKind {
    match inj {
        Injected::Comment(line) => SplitRowKind::Comment { side, line },
        Injected::Composer(line) => SplitRowKind::Composer { side, line },
    }
}

/// Emit the accumulated deletion/addition runs as zipped pairs.
#[allow(clippy::too_many_arguments)]
fn flush_pairs(
    file_idx: usize,
    dels: &mut Vec<SideCell>,
    adds: &mut Vec<SideCell>,
    rows: &mut Vec<SplitRow>,
    comments: &CommentStore,
    by_path: &ThreadsByPath<'_>,
    last: &HashMap<String, (Side, u32)>,
    emitted: &mut HashSet<String>,
    path: &str,
    width: usize,
    composer: Option<&ComposerSpec>,
    composer_emitted: &mut bool,
) {
    let n = dels.len().max(adds.len());
    let mut di = dels.drain(..);
    let mut ai = adds.drain(..);
    for _ in 0..n {
        let left = di.next();
        let right = ai.next();
        let mut anchors = Vec::new();
        if let Some(l) = left.as_ref().and_then(|c| c.line) {
            anchors.push((Side::Old, l));
        }
        if let Some(l) = right.as_ref().and_then(|c| c.line) {
            anchors.push((Side::New, l));
        }
        rows.push(SplitRow {
            file_idx,
            kind: SplitRowKind::Pair { left, right },
            text: String::new(),
        });
        for (side, inj) in comment_rows_for(
            comments, by_path, last, emitted, path, &anchors, width, composer,
        ) {
            rows.push(SplitRow {
                file_idx,
                kind: split_injected(side, inj),
                text: String::new(),
            });
        }
        for (side, l) in &anchors {
            for cl in new_thread_composer(composer, composer_emitted, file_idx, *side, *l, width) {
                rows.push(SplitRow {
                    file_idx,
                    kind: SplitRowKind::Composer {
                        side: *side,
                        line: cl,
                    },
                    text: String::new(),
                });
            }
        }
    }
}

/// Threads on `path` that never matched a visible diff line. Their anchor sits
/// outside every shown hunk — e.g. a review comment on an unchanged line, or one
/// GitHub repositioned to an out-of-hunk line. Such a thread is still counted by
/// the sidebar's comment dot, so without this it would be advertised yet
/// unreachable. Emitted once, after the file's hunks, so every thread the
/// sidebar promises is navigable. `emitted` dedups against inline threads.
fn orphan_thread_rows(
    comments: &CommentStore,
    by_path: &ThreadsByPath<'_>,
    emitted: &mut HashSet<String>,
    path: &str,
    width: usize,
    composer: Option<&ComposerSpec>,
) -> Vec<(Side, Injected)> {
    let mut out = Vec::new();
    let Some(indices) = by_path.get(Path::new(path)) else {
        return out;
    };
    for &i in indices {
        let t = &comments.threads[i];
        if emitted.contains(t.id.as_str()) {
            continue;
        }
        emitted.insert(t.id.clone());
        out.extend(
            thread_lines(t, width)
                .into_iter()
                .map(|cl| (t.side, Injected::Comment(cl))),
        );
        // A reply composer sits directly under the thread it replies to.
        if let Some(spec) = composer {
            if matches!(&spec.anchor, ComposerAnchor::Reply { thread_id } if *thread_id == t.id) {
                out.extend(
                    composer_lines(spec, width)
                        .into_iter()
                        .map(|cl| (t.side, Injected::Composer(cl))),
                );
            }
        }
    }
    out
}

/// Build the unified (stack) row list.
pub fn build_rows(
    changeset: &Changeset,
    comments: &CommentStore,
    width: usize,
    composer: Option<&ComposerSpec>,
) -> Vec<Row> {
    let mut rows = Vec::new();
    let mut emitted: HashSet<String> = HashSet::new();
    let mut composer_emitted = false;
    let by_path = threads_by_path(comments);
    let last = last_anchor_lines(changeset, comments, &by_path);
    for (fi, file) in changeset.files.iter().enumerate() {
        let path = file.display_path();
        rows.push(Row {
            file_idx: fi,
            kind: RowKind::FileHeader,
            text: file.display_path().to_string(),
        });
        if file.is_binary {
            rows.push(Row {
                file_idx: fi,
                kind: RowKind::HunkHeader,
                text: "Binary file".into(),
            });
            continue;
        }
        for hunk in &file.hunks {
            rows.push(Row {
                file_idx: fi,
                kind: RowKind::HunkHeader,
                text: hunk_header(hunk),
            });
            for line in &hunk.lines {
                let prefix = match line.kind {
                    LineKind::Addition => '+',
                    LineKind::Deletion => '-',
                    LineKind::Context => ' ',
                };
                rows.push(Row {
                    file_idx: fi,
                    kind: RowKind::Line {
                        kind: line.kind,
                        old_line: line.old_line,
                        new_line: line.new_line,
                    },
                    text: format!("{prefix}{}", sanitize_line(&line.text)),
                });
                let (side, ln) = match line.kind {
                    LineKind::Deletion => (Side::Old, line.old_line),
                    _ => (Side::New, line.new_line),
                };
                if let Some(ln) = ln {
                    for (_, inj) in comment_rows_for(
                        comments,
                        &by_path,
                        &last,
                        &mut emitted,
                        path,
                        &[(side, ln)],
                        width,
                        composer,
                    ) {
                        let kind = match inj {
                            Injected::Comment(cl) => RowKind::Comment(cl),
                            Injected::Composer(cl) => RowKind::Composer(cl),
                        };
                        rows.push(Row {
                            file_idx: fi,
                            kind,
                            text: String::new(),
                        });
                    }
                    for cl in
                        new_thread_composer(composer, &mut composer_emitted, fi, side, ln, width)
                    {
                        rows.push(Row {
                            file_idx: fi,
                            kind: RowKind::Composer(cl),
                            text: String::new(),
                        });
                    }
                }
            }
        }
        for (_, inj) in orphan_thread_rows(comments, &by_path, &mut emitted, path, width, composer)
        {
            let kind = match inj {
                Injected::Comment(cl) => RowKind::Comment(cl),
                Injected::Composer(cl) => RowKind::Composer(cl),
            };
            rows.push(Row {
                file_idx: fi,
                kind,
                text: String::new(),
            });
        }
    }
    rows
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::comments::model::{CommentStore, LineRange};
    use crate::diff::parse::parse_report;
    use crate::loader::{load_comments, load_patch};
    use std::path::{Path, PathBuf};

    // A two-line change: old line 2 (`b`) deleted, new line 2 (`B`) added.
    const SIMPLE_DIFF: &str = "\
--- a/foo.txt
+++ b/foo.txt
@@ -1,3 +1,3 @@ fn main
 a
-b
+B
 c
";

    fn store_with(side: Side, line: u32) -> CommentStore {
        let mut store = CommentStore::default();
        store.add_thread(
            PathBuf::from("foo.txt"),
            side,
            LineRange {
                start: line,
                end: line,
            },
            Some("me".into()),
            "a comment".into(),
        );
        store
    }

    #[test]
    fn split_comment_renders_under_anchored_side() {
        let cs = parse_report(SIMPLE_DIFF).0;

        // Old-side thread (anchored to the deleted line 2) is tagged Old.
        let old = store_with(Side::Old, 2);
        let rows = build_split_rows(&cs, &old, 80, None);
        assert!(
            rows.iter().any(|r| matches!(
                r.kind,
                SplitRowKind::Comment {
                    side: Side::Old,
                    ..
                }
            )),
            "old-side comment should be tagged Side::Old"
        );
        assert!(
            !rows.iter().any(|r| matches!(
                r.kind,
                SplitRowKind::Comment {
                    side: Side::New,
                    ..
                }
            )),
            "old-side comment must not be tagged Side::New"
        );

        // New-side thread (anchored to the added line 2) is tagged New.
        let new = store_with(Side::New, 2);
        let rows = build_split_rows(&cs, &new, 80, None);
        assert!(
            rows.iter().any(|r| matches!(
                r.kind,
                SplitRowKind::Comment {
                    side: Side::New,
                    ..
                }
            )),
            "new-side comment should be tagged Side::New"
        );
    }

    #[test]
    fn thread_anchored_outside_any_hunk_is_still_emitted() {
        // A comment on a line the diff never shows (new-side line 99, far past
        // the only hunk) used to be counted by the sidebar yet never rendered.
        // It must still appear, appended after the file's hunks, in both views.
        let cs = parse_report(SIMPLE_DIFF).0;
        let orphan = store_with(Side::New, 99);

        let unified = build_rows(&cs, &orphan, 80, None);
        assert!(
            unified
                .iter()
                .any(|r| matches!(r.kind, RowKind::Comment(_))),
            "an out-of-hunk thread must still render in the unified view"
        );

        let split = build_split_rows(&cs, &orphan, 80, None);
        assert!(
            split
                .iter()
                .any(|r| matches!(r.kind, SplitRowKind::Comment { .. })),
            "an out-of-hunk thread must still render in the split view"
        );
    }

    #[test]
    fn in_hunk_thread_is_not_double_emitted_by_orphan_pass() {
        // Dedup: a thread shown inline must not be re-emitted as an orphan.
        let cs = parse_report(SIMPLE_DIFF).0;
        let inhunk = store_with(Side::New, 2); // anchored to the added line 2
        let rows = build_rows(&cs, &inhunk, 80, None);
        let box_tops = rows
            .iter()
            .filter(
                |r| matches!(&r.kind, RowKind::Comment(cl) if matches!(cl.kind, CommentKind::Top)),
            )
            .count();
        assert_eq!(box_tops, 1, "thread emitted exactly once");
    }

    #[test]
    fn expands_tabs_and_strips_controls() {
        assert_eq!(sanitize_line("\tx"), "    x");
        assert_eq!(sanitize_line("a\tb"), "a   b"); // tab to next 4-col stop
        assert_eq!(sanitize_line("end\r"), "end");
        assert_eq!(sanitize_line("a\u{0}b"), "ab");
        // ANSI CSI color sequence is removed, payload kept.
        assert_eq!(sanitize_line("\u{1b}[31mred\u{1b}[0m"), "red");
    }

    #[test]
    fn display_width_counts_wide_glyphs() {
        assert_eq!(str_width("abc"), 3);
        assert_eq!(str_width("日本語"), 6); // 3 wide CJK glyphs = 6 cells
        assert_eq!(str_width("aæ—¥b"), 4);
    }

    #[test]
    fn take_width_never_overflows_on_wide_glyphs() {
        // Budget 3 over "a日本": fits 'a'(1)+'日'(2)=3; '本' would overflow.
        let (s, w) = take_width("a日本", 3);
        assert_eq!(s, "aæ—¥");
        assert_eq!(w, 3);
        // Odd budget straddling a wide glyph drops it (no half-cell).
        let (s, w) = take_width("日本", 1);
        assert_eq!(s, "");
        assert_eq!(w, 0);
    }

    #[test]
    fn wrap_text_wraps_on_display_width() {
        // Three wide glyphs (6 cells) wrap at width 4 (2 glyphs per line).
        let lines = wrap_text("日本語", 4);
        assert!(lines.iter().all(|l| str_width(l) <= 4));
        assert_eq!(lines.concat(), "日本語");
    }

    #[test]
    fn wrap_text_no_empty_lines_for_unsplittable_glyphs() {
        // A glyph wider than the width can't be split; it must land on its own
        // line with no spurious empty line ahead of it.
        let lines = wrap_text("日本", 1);
        assert_eq!(lines, vec!["日".to_string(), "本".to_string()]);
        assert!(lines.iter().all(|l| !l.is_empty()));
    }

    #[test]
    fn wrap_preserve_keeps_runs_of_spaces() {
        // Regression: the composer is a live buffer, so consecutive spaces must
        // survive verbatim (wrap_text collapses them via split_whitespace).
        let lines = wrap_preserve("a    b", 80);
        assert_eq!(lines, vec!["a    b".to_string()]);
    }

    #[test]
    fn wrap_preserve_breaks_at_word_boundary() {
        // Greedy break prefers the last space so a word isn't split mid-token.
        // The break space stays on the first line so the buffer is preserved
        // verbatim (concatenating the visual lines reproduces the input).
        let lines = wrap_preserve("hello world", 7);
        assert_eq!(lines, vec!["hello ".to_string(), "world".to_string()]);
        assert_eq!(lines.concat(), "hello world");
    }

    #[test]
    fn wrap_preserve_hard_splits_an_overlong_token() {
        // A token longer than the width has no space to break on, so it is split.
        let lines = wrap_preserve("abcdef", 3);
        assert_eq!(lines, vec!["abc".to_string(), "def".to_string()]);
    }

    #[test]
    fn injects_inline_thread_rows() {
        let cs = load_patch(Some(Path::new("examples/rust-long-en.patch"))).unwrap();
        let comments = load_comments(Path::new("examples/rust-long-en.comments.json")).unwrap();
        let base = build_rows(&cs, &CommentStore::default(), 80, None);
        let rows = build_rows(&cs, &comments, 80, None);
        // Threads inject extra (comment) rows over a no-comment baseline.
        assert!(rows.len() > base.len());
        // Those rows are comment rows: non-selectable and anchorless.
        let comment_rows = rows
            .iter()
            .filter(|r| matches!(r.kind, RowKind::Comment(_)))
            .count();
        assert!(comment_rows > 0);
        // Each thread renders exactly one header, regardless of multi-line
        // anchor ranges (no per-line duplication).
        let heads = rows
            .iter()
            .filter(|r| {
                matches!(
                    &r.kind,
                    RowKind::Comment(CommentLine {
                        kind: CommentKind::Head { .. },
                        ..
                    })
                )
            })
            .count();
        assert_eq!(heads, comments.threads.len());
        assert!(rows.iter().all(|r| !matches!(r.kind, RowKind::Comment(_))
            || (!r.is_selectable() && r.anchor().is_none())));
    }

    #[test]
    fn new_thread_composer_injects_inline_box() {
        let cs = parse_report(SIMPLE_DIFF).0;
        let store = CommentStore::default();
        let spec = ComposerSpec {
            anchor: ComposerAnchor::NewThread {
                file_idx: 0,
                side: Side::New,
                line: 2,
            },
            title: " new comment ".into(),
            body: "hi".into(),
        };
        // Without a composer, no composer rows; with one, a box appears.
        assert!(!build_rows(&cs, &store, 80, None)
            .iter()
            .any(|r| matches!(r.kind, RowKind::Composer(_))));
        let rows = build_rows(&cs, &store, 80, Some(&spec));
        // Exactly one top + one bottom border (a single box), and it carries
        // the live body text. Composer rows are never selectable.
        assert_eq!(
            rows.iter()
                .filter(|r| matches!(
                    r.kind,
                    RowKind::Composer(ComposerLine {
                        kind: ComposerKind::Top { .. }
                    })
                ))
                .count(),
            1
        );
        assert!(rows
            .iter()
            .any(|r| matches!(&r.kind, RowKind::Composer(ComposerLine { kind: ComposerKind::Body(b) }) if b.contains("hi"))));
        assert!(rows
            .iter()
            .all(|r| !matches!(r.kind, RowKind::Composer(_)) || !r.is_selectable()));
    }

    #[test]
    fn reply_composer_injects_under_its_thread() {
        let cs = parse_report(SIMPLE_DIFF).0;
        let store = store_with(Side::New, 2);
        let thread_id = store.threads[0].id.clone();
        let spec = ComposerSpec {
            anchor: ComposerAnchor::Reply { thread_id },
            title: " reply ".into(),
            body: "ok".into(),
        };
        let rows = build_rows(&cs, &store, 80, Some(&spec));
        // The reply box renders after the thread's bottom border.
        let bottom = rows.iter().position(|r| {
            matches!(
                &r.kind,
                RowKind::Comment(CommentLine {
                    kind: CommentKind::Bottom,
                    ..
                })
            )
        });
        let comp_top = rows.iter().position(|r| {
            matches!(
                r.kind,
                RowKind::Composer(ComposerLine {
                    kind: ComposerKind::Top { .. }
                })
            )
        });
        assert!(bottom.is_some() && comp_top.is_some());
        assert!(
            comp_top > bottom,
            "reply composer must sit below its thread"
        );
    }
}