sid-isnt-done 0.2.0

sid is a UNIX-inspired coding agent for Anthropic-compatible APIs
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
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
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
//! Semantic diff renderer with syntax-aware annotations.
//!
//! Parses git-style or plain unified diffs into an intermediate representation
//! ([`Diff`], [`DiffFile`], [`Hunk`], [`DiffLine`]), optionally annotates
//! hunks with tree-sitter role information, and renders the result with ANSI
//! truecolor highlighting.

use std::cmp::{max, min};
use std::collections::{BTreeMap, HashMap, HashSet};
use std::env;
use std::ffi::{OsStr, OsString};
use std::io::{self, Write};
use std::process::{Command, Stdio};
use tree_sitter::{Language, Node, Parser};

const DEFAULT_PAGER: &str = "less -R";
const MOVE_MIN_LINES: usize = 3;
const MAX_ROLE_GROUPS: usize = 8;
const ROLE_GLYPHS: [&str; MAX_ROLE_GROUPS] = [
    "\u{2460}", "\u{2461}", "\u{2462}", "\u{2463}", "\u{2464}", "\u{2465}", "\u{2466}", "\u{2467}",
];

const RESET: &str = "\x1b[0m";

/// Controls how trailing whitespace changes are displayed.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum WhitespaceMode {
    /// Suppress whitespace-only diff lines entirely.
    Hide,
    /// Render whitespace changes in a subdued color.
    Muted,
    /// Render whitespace changes like any other change.
    Normal,
}

/// Controls the background-tint intensity for add/remove line highlighting.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum TintIntensity {
    /// Subtle background tint.
    Low,
    /// Moderate background tint (default).
    Medium,
    /// Pronounced background tint.
    High,
}

impl TintIntensity {
    fn alpha(self) -> f32 {
        match self {
            Self::Low => 0.18,
            Self::Medium => 0.30,
            Self::High => 0.44,
        }
    }
}

/// Rendering options for [`render_diff`] and [`run_pager`].
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct SidiffOptions {
    /// Enable ANSI truecolor output.
    pub use_color: bool,
    /// Whitespace display policy.
    pub whitespace_mode: WhitespaceMode,
    /// Background-tint strength for added/removed lines.
    pub tint_intensity: TintIntensity,
}

impl Default for SidiffOptions {
    fn default() -> Self {
        Self {
            use_color: true,
            whitespace_mode: WhitespaceMode::Muted,
            tint_intensity: TintIntensity::Medium,
        }
    }
}

/// Top-level unified diff, potentially covering multiple files.
#[derive(Clone, Debug, Default, Eq, PartialEq)]
pub struct Diff {
    /// Lines that appear before any file header (e.g. `diff --stat` output).
    pub preamble: Vec<String>,
    /// Per-file diff sections.
    pub files: Vec<DiffFile>,
}

/// A single file's portion of a unified diff.
#[derive(Clone, Debug, Default, Eq, PartialEq)]
pub struct DiffFile {
    /// Path from the `---` header line, if present.
    pub old_path: Option<String>,
    /// Path from the `+++` header line, if present.
    pub new_path: Option<String>,
    /// Raw header lines (e.g. `diff --git`, `index`, `---`, `+++`).
    pub header: Vec<String>,
    /// Hunks within this file's diff.
    pub hunks: Vec<Hunk>,
}

/// A contiguous region of changed lines within a file diff.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Hunk {
    /// Raw `@@ ... @@` header line.
    pub header: String,
    /// Starting line number in the old file.
    pub old_start: usize,
    /// Number of lines from the old file in this hunk.
    pub old_count: usize,
    /// Starting line number in the new file.
    pub new_start: usize,
    /// Number of lines from the new file in this hunk.
    pub new_count: usize,
    /// Optional section name extracted from the hunk header (e.g. function name).
    pub section: String,
    /// Individual diff lines within the hunk.
    pub lines: Vec<DiffLine>,
}

/// A single line within a [`Hunk`].
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct DiffLine {
    /// Whether this line is context, added, removed, or a note.
    pub op: DiffOp,
    /// The line's text content (without the leading `+`/`-`/` ` marker).
    pub content: String,
    /// Line number in the old file, if applicable.
    pub old_lineno: Option<usize>,
    /// Line number in the new file, if applicable.
    pub new_lineno: Option<usize>,
}

/// The operation a [`DiffLine`] represents.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum DiffOp {
    /// Unchanged context line present in both old and new files.
    Context,
    /// Line added in the new file.
    Add,
    /// Line removed from the old file.
    Remove,
    /// Informational annotation (e.g. `\ No newline at end of file`).
    Note,
}

#[derive(Clone, Copy, Debug, Default, Eq, PartialEq, Hash, PartialOrd, Ord)]
struct LineId {
    file: usize,
    hunk: usize,
    line: usize,
}

#[derive(Clone, Debug)]
struct Analysis {
    lines: HashMap<LineId, LineAnalysis>,
    moves: Vec<MoveBlock>,
    roles: Vec<RoleGroup>,
    syntax: HashMap<LineId, Vec<SyntaxRange>>,
}

#[derive(Clone, Debug, Default)]
struct LineAnalysis {
    novelty: LineNovelty,
    ranges: Vec<NoveltyRange>,
    paired_with: Option<LineId>,
    move_id: Option<usize>,
    role_ranges: Vec<RoleRange>,
}

#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
enum LineNovelty {
    #[default]
    None,
    Unpaired,
    Content,
    Mixed,
    WhitespaceOnly,
}

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
struct NoveltyRange {
    start: usize,
    end: usize,
    weight: NoveltyWeight,
}

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
enum NoveltyWeight {
    Unchanged,
    Muted,
    Full,
}

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
struct RoleRange {
    start: usize,
    end: usize,
    role: usize,
}

#[derive(Clone, Debug, Eq, PartialEq)]
struct MoveBlock {
    remove_file: usize,
    remove_hunk: usize,
    remove_start: usize,
    add_file: usize,
    add_hunk: usize,
    add_start: usize,
    len: usize,
}

#[derive(Clone, Debug, Eq, PartialEq)]
struct RoleGroup {
    removed: String,
    added: String,
    count: usize,
    color: Rgb,
}

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
struct SyntaxRange {
    start: usize,
    end: usize,
    class: SyntaxClass,
}

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
enum SyntaxClass {
    Comment,
    String,
    Number,
    Keyword,
    Type,
    Identifier,
    Operator,
}

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
enum Side {
    Pre,
    Post,
}

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
struct Rgb {
    r: u8,
    g: u8,
    b: u8,
}

impl Rgb {
    const fn new(r: u8, g: u8, b: u8) -> Self {
        Self { r, g, b }
    }

    fn mix_with(self, base: Rgb, alpha: f32) -> Self {
        let mix = |fg: u8, bg: u8| -> u8 {
            ((fg as f32 * alpha) + (bg as f32 * (1.0 - alpha)))
                .round()
                .clamp(0.0, 255.0) as u8
        };
        Self::new(
            mix(self.r, base.r),
            mix(self.g, base.g),
            mix(self.b, base.b),
        )
    }
}

const ROLE_PALETTE: [Rgb; MAX_ROLE_GROUPS] = [
    Rgb::new(230, 159, 0),
    Rgb::new(86, 180, 233),
    Rgb::new(0, 158, 115),
    Rgb::new(240, 228, 66),
    Rgb::new(0, 114, 178),
    Rgb::new(213, 94, 0),
    Rgb::new(204, 121, 167),
    Rgb::new(128, 128, 128),
];

#[derive(Clone, Debug)]
struct RenderedLine {
    text: String,
}

#[derive(Clone, Debug)]
struct LineRef {
    id: LineId,
    content: String,
}

#[derive(Clone, Debug)]
struct ChangePair {
    remove: LineRef,
    add: LineRef,
}

#[derive(Clone, Debug)]
struct RunRef {
    file: usize,
    hunk: usize,
    start: usize,
    len: usize,
    normalized: Vec<String>,
    op: DiffOp,
}

#[derive(Clone, Debug)]
struct ReconLine {
    id: LineId,
    start: usize,
    end: usize,
}

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
enum LanguageKind {
    Rust,
    Python,
    Bash,
}

/// Parse a git-style or plain unified diff into sidiff's line-oriented IR.
pub fn parse_unified_diff(input: &str) -> Diff {
    let mut diff = Diff::default();
    let mut current_file: Option<DiffFile> = None;
    let mut current_hunk: Option<Hunk> = None;

    for raw in input.lines() {
        let line = raw.strip_suffix('\r').unwrap_or(raw).to_string();

        if line.starts_with("diff --git ") {
            flush_hunk(&mut current_file, &mut current_hunk);
            flush_file(&mut diff, &mut current_file);
            let (old_path, new_path) = parse_diff_git_paths(&line);
            current_file = Some(DiffFile {
                old_path,
                new_path,
                header: vec![line],
                hunks: vec![],
            });
            continue;
        }

        if line.starts_with("--- ") && should_start_plain_file(&current_file, &current_hunk) {
            flush_hunk(&mut current_file, &mut current_hunk);
            flush_file(&mut diff, &mut current_file);
            current_file = Some(DiffFile {
                old_path: parse_header_path(&line, "--- "),
                new_path: None,
                header: vec![line],
                hunks: vec![],
            });
            continue;
        }

        if line.starts_with("--- ") {
            ensure_file(&mut current_file);
            if let Some(file) = current_file.as_mut() {
                file.old_path = parse_header_path(&line, "--- ");
                file.header.push(line);
            }
            continue;
        }

        if line.starts_with("+++ ") {
            ensure_file(&mut current_file);
            if let Some(file) = current_file.as_mut() {
                file.new_path = parse_header_path(&line, "+++ ");
                file.header.push(line);
            }
            continue;
        }

        if line.starts_with("@@ ") {
            ensure_file(&mut current_file);
            flush_hunk(&mut current_file, &mut current_hunk);
            current_hunk = Some(parse_hunk_header(&line));
            continue;
        }

        if let Some(hunk) = current_hunk.as_mut() {
            append_hunk_line(hunk, &line);
            continue;
        }

        if let Some(file) = current_file.as_mut() {
            file.header.push(line);
        } else {
            diff.preamble.push(line);
        }
    }

    flush_hunk(&mut current_file, &mut current_hunk);
    flush_file(&mut diff, &mut current_file);
    diff
}

/// Render a diff as annotated text. This is used directly for piped stdout and
/// as the input fed to an external pager when stdout is a terminal.
pub fn render_diff(input: &str, options: SidiffOptions) -> String {
    let diff = parse_unified_diff(input);
    let analysis = analyze_diff(&diff);
    let lines = build_rendered_lines(&diff, &analysis, options);
    let mut out = String::new();
    for line in lines {
        out.push_str(&line.text);
        out.push('\n');
    }
    out
}

/// Render a unified diff and feed it to the external pager named by `PAGER`.
///
/// An empty `PAGER` disables paging and writes directly to stdout. When `PAGER`
/// is unset, sidiff defaults to `less -R` so ANSI truecolor survives the pager.
pub fn run_pager(input: &str, options: SidiffOptions) -> io::Result<()> {
    let rendered = render_diff(input, options);
    match pager_command_from_env() {
        PagerCommand::Disabled => write_rendered_stdout(&rendered),
        PagerCommand::Shell(command) => run_shell_pager(&command, &rendered),
    }
}

/// Render a unified diff for a confirmation preview.
///
/// When `DIFF` is unset, sidiff's built-in renderer is used. When `DIFF` is
/// set to a shell command, the raw unified diff is piped to that command and
/// its stdout becomes the preview. An empty `DIFF` disables styling and
/// returns the raw unified diff text.
pub fn render_diff_preview(input: &str) -> io::Result<String> {
    render_diff_preview_with_value(input, env::var_os("DIFF"))
}

/// Render a unified diff preview using an explicit `DIFF` environment override.
///
/// When `value` is `None`, sidiff's built-in renderer is used.  An empty
/// `OsString` disables styling and returns the raw diff.  Any other value is
/// interpreted as a shell command to pipe the diff through.
///
/// # Errors
///
/// Returns an I/O error when the external filter command fails.
pub fn render_diff_preview_with_value(input: &str, value: Option<OsString>) -> io::Result<String> {
    match diff_preview_command_from_value(value) {
        DiffPreviewCommand::Raw => Ok(input.to_string()),
        DiffPreviewCommand::Shell(command) => run_shell_filter(&command, input),
        DiffPreviewCommand::Sidiff => Ok(render_diff(
            input,
            SidiffOptions {
                use_color: env::var_os("NO_COLOR").is_none(),
                ..SidiffOptions::default()
            },
        )),
    }
}

#[derive(Clone, Debug, Eq, PartialEq)]
enum PagerCommand {
    Disabled,
    Shell(OsString),
}

#[derive(Clone, Debug, Eq, PartialEq)]
enum DiffPreviewCommand {
    Raw,
    Shell(OsString),
    Sidiff,
}

fn pager_command_from_env() -> PagerCommand {
    pager_command_from_value(env::var_os("PAGER"))
}

fn pager_command_from_value(value: Option<OsString>) -> PagerCommand {
    match value {
        Some(value) if os_str_is_blank(&value) => PagerCommand::Disabled,
        Some(value) => PagerCommand::Shell(value),
        None => PagerCommand::Shell(OsString::from(DEFAULT_PAGER)),
    }
}

fn diff_preview_command_from_value(value: Option<OsString>) -> DiffPreviewCommand {
    match value {
        Some(value) if os_str_is_blank(&value) => DiffPreviewCommand::Raw,
        Some(value) => DiffPreviewCommand::Shell(value),
        None => DiffPreviewCommand::Sidiff,
    }
}

fn os_str_is_blank(value: &OsStr) -> bool {
    value.to_string_lossy().trim().is_empty()
}

fn run_shell_pager(command: &OsStr, rendered: &str) -> io::Result<()> {
    let mut child = shell_command(command)
        .stdin(Stdio::piped())
        .stdout(Stdio::inherit())
        .stderr(Stdio::inherit())
        .spawn()
        .map_err(|err| {
            io::Error::new(
                err.kind(),
                format!(
                    "failed to launch pager '{}': {err}",
                    command.to_string_lossy()
                ),
            )
        })?;

    if let Some(mut stdin) = child.stdin.take() {
        match stdin.write_all(rendered.as_bytes()) {
            Ok(()) => {}
            Err(err) if err.kind() == io::ErrorKind::BrokenPipe => {}
            Err(err) => return Err(err),
        }
    }

    let status = child.wait()?;
    if status.success() {
        Ok(())
    } else {
        Err(io::Error::other(format!(
            "pager '{}' exited with status {status}",
            command.to_string_lossy()
        )))
    }
}

fn run_shell_filter(command: &OsStr, input: &str) -> io::Result<String> {
    let mut child = shell_command(command)
        .stdin(Stdio::piped())
        .stdout(Stdio::piped())
        .stderr(Stdio::piped())
        .spawn()
        .map_err(|err| {
            io::Error::new(
                err.kind(),
                format!(
                    "failed to launch diff preview command '{}': {err}",
                    command.to_string_lossy()
                ),
            )
        })?;

    if let Some(mut stdin) = child.stdin.take() {
        match stdin.write_all(input.as_bytes()) {
            Ok(()) => {}
            Err(err) if err.kind() == io::ErrorKind::BrokenPipe => {}
            Err(err) => return Err(err),
        }
    }

    let output = child.wait_with_output()?;
    if output.status.success() {
        Ok(String::from_utf8_lossy(&output.stdout).into_owned())
    } else {
        let stderr = String::from_utf8_lossy(&output.stderr).trim().to_string();
        let detail = if stderr.is_empty() {
            output.status.to_string()
        } else {
            format!("{}: {stderr}", output.status)
        };
        Err(io::Error::other(format!(
            "diff preview command '{}' failed: {detail}",
            command.to_string_lossy()
        )))
    }
}

#[cfg(unix)]
fn shell_command(command: &OsStr) -> Command {
    let mut shell = Command::new("sh");
    shell.arg("-c").arg(command);
    shell
}

#[cfg(windows)]
fn shell_command(command: &OsStr) -> Command {
    let mut shell = Command::new("cmd");
    shell.arg("/C").arg(command);
    shell
}

fn write_rendered_stdout(rendered: &str) -> io::Result<()> {
    let mut stdout = io::stdout();
    stdout.write_all(rendered.as_bytes())?;
    stdout.flush()
}

fn flush_file(diff: &mut Diff, current_file: &mut Option<DiffFile>) {
    if let Some(file) = current_file.take() {
        diff.files.push(file);
    }
}

fn flush_hunk(current_file: &mut Option<DiffFile>, current_hunk: &mut Option<Hunk>) {
    if let Some(hunk) = current_hunk.take() {
        ensure_file(current_file);
        if let Some(file) = current_file.as_mut() {
            file.hunks.push(hunk);
        }
    }
}

fn ensure_file(current_file: &mut Option<DiffFile>) {
    if current_file.is_none() {
        *current_file = Some(DiffFile::default());
    }
}

fn should_start_plain_file(current_file: &Option<DiffFile>, current_hunk: &Option<Hunk>) -> bool {
    current_file.is_none()
        || current_hunk.is_some()
        || current_file
            .as_ref()
            .is_some_and(|file| !file.hunks.is_empty())
}

fn parse_diff_git_paths(line: &str) -> (Option<String>, Option<String>) {
    let rest = line.strip_prefix("diff --git ").unwrap_or(line);
    let mut parts = rest.split_whitespace();
    let old = parts.next().map(clean_diff_path);
    let new = parts.next().map(clean_diff_path);
    (old, new)
}

fn parse_header_path(line: &str, prefix: &str) -> Option<String> {
    let path = line.strip_prefix(prefix)?.split_whitespace().next()?;
    if path == "/dev/null" {
        None
    } else {
        Some(clean_diff_path(path))
    }
}

fn clean_diff_path(path: &str) -> String {
    path.strip_prefix("a/")
        .or_else(|| path.strip_prefix("b/"))
        .unwrap_or(path)
        .to_string()
}

fn parse_hunk_header(line: &str) -> Hunk {
    let mut parts = line.splitn(4, ' ');
    let _open = parts.next();
    let old_part = parts.next().unwrap_or("-0,0");
    let new_part = parts.next().unwrap_or("+0,0");
    let section = parts
        .next()
        .and_then(|part| part.strip_prefix("@@"))
        .unwrap_or("")
        .trim()
        .to_string();
    let (old_start, old_count) = parse_range_part(old_part, '-');
    let (new_start, new_count) = parse_range_part(new_part, '+');
    Hunk {
        header: line.to_string(),
        old_start,
        old_count,
        new_start,
        new_count,
        section,
        lines: vec![],
    }
}

fn parse_range_part(part: &str, prefix: char) -> (usize, usize) {
    let rest = part.strip_prefix(prefix).unwrap_or(part);
    let mut split = rest.splitn(2, ',');
    let start = split
        .next()
        .and_then(|value| value.parse().ok())
        .unwrap_or(0);
    let count = split
        .next()
        .and_then(|value| value.parse().ok())
        .unwrap_or(1);
    (start, count)
}

fn append_hunk_line(hunk: &mut Hunk, line: &str) {
    let old_next = hunk
        .lines
        .iter()
        .filter(|line| matches!(line.op, DiffOp::Context | DiffOp::Remove))
        .count();
    let new_next = hunk
        .lines
        .iter()
        .filter(|line| matches!(line.op, DiffOp::Context | DiffOp::Add))
        .count();

    if line.starts_with("\\ ") {
        hunk.lines.push(DiffLine {
            op: DiffOp::Note,
            content: line.to_string(),
            old_lineno: None,
            new_lineno: None,
        });
        return;
    }

    let (op, content) = match line.as_bytes().first().copied() {
        Some(b' ') => (DiffOp::Context, &line[1..]),
        Some(b'+') => (DiffOp::Add, &line[1..]),
        Some(b'-') => (DiffOp::Remove, &line[1..]),
        _ => (DiffOp::Context, line),
    };
    hunk.lines.push(DiffLine {
        op,
        content: content.to_string(),
        old_lineno: match op {
            DiffOp::Context | DiffOp::Remove => Some(hunk.old_start + old_next),
            DiffOp::Add | DiffOp::Note => None,
        },
        new_lineno: match op {
            DiffOp::Context | DiffOp::Add => Some(hunk.new_start + new_next),
            DiffOp::Remove | DiffOp::Note => None,
        },
    });
}

fn analyze_diff(diff: &Diff) -> Analysis {
    let mut analysis = Analysis {
        lines: HashMap::new(),
        moves: vec![],
        roles: vec![],
        syntax: syntax_maps(diff),
    };
    let pairs = analyze_change_pairs(diff, &mut analysis.lines);
    detect_moves(diff, &mut analysis);
    analysis.roles = detect_roles(&pairs);
    apply_roles(diff, &mut analysis);
    analysis
}

fn analyze_change_pairs(diff: &Diff, lines: &mut HashMap<LineId, LineAnalysis>) -> Vec<ChangePair> {
    let mut pairs = vec![];
    for (file_idx, file) in diff.files.iter().enumerate() {
        for (hunk_idx, hunk) in file.hunks.iter().enumerate() {
            let mut index = 0usize;
            while index < hunk.lines.len() {
                if matches!(hunk.lines[index].op, DiffOp::Context | DiffOp::Note) {
                    index += 1;
                    continue;
                }
                let start = index;
                while index < hunk.lines.len()
                    && matches!(hunk.lines[index].op, DiffOp::Add | DiffOp::Remove)
                {
                    index += 1;
                }
                let mut removes = vec![];
                let mut adds = vec![];
                for line_idx in start..index {
                    let line = &hunk.lines[line_idx];
                    let id = LineId {
                        file: file_idx,
                        hunk: hunk_idx,
                        line: line_idx,
                    };
                    match line.op {
                        DiffOp::Remove => removes.push(LineRef {
                            id,
                            content: line.content.clone(),
                        }),
                        DiffOp::Add => adds.push(LineRef {
                            id,
                            content: line.content.clone(),
                        }),
                        DiffOp::Context | DiffOp::Note => {}
                    }
                }
                for (remove, add) in pair_change_lines(removes, adds) {
                    let old_meta = analyze_paired_line(&remove.content, &add.content, Side::Pre);
                    let new_meta = analyze_paired_line(&remove.content, &add.content, Side::Post);
                    lines.insert(
                        remove.id,
                        LineAnalysis {
                            paired_with: Some(add.id),
                            ..old_meta
                        },
                    );
                    lines.insert(
                        add.id,
                        LineAnalysis {
                            paired_with: Some(remove.id),
                            ..new_meta
                        },
                    );
                    pairs.push(ChangePair { remove, add });
                }
                for line_idx in start..index {
                    let id = LineId {
                        file: file_idx,
                        hunk: hunk_idx,
                        line: line_idx,
                    };
                    if lines.contains_key(&id) {
                        continue;
                    }
                    let line = &hunk.lines[line_idx];
                    if matches!(line.op, DiffOp::Add | DiffOp::Remove) {
                        lines.insert(id, analyze_unpaired_line(&line.content));
                    }
                }
            }
        }
    }
    pairs
}

fn pair_change_lines(removes: Vec<LineRef>, adds: Vec<LineRef>) -> Vec<(LineRef, LineRef)> {
    let mut pairs = vec![];
    let mut used_adds = HashSet::new();
    for (remove_idx, remove) in removes.iter().enumerate() {
        let mut best: Option<(usize, f32)> = None;
        for (add_idx, add) in adds.iter().enumerate() {
            if used_adds.contains(&add_idx) {
                continue;
            }
            let score = line_similarity(&remove.content, &add.content);
            if best.is_none_or(|(_, best_score)| score > best_score) {
                best = Some((add_idx, score));
            }
        }
        let Some((add_idx, score)) = best else {
            continue;
        };
        let positional = remove_idx < adds.len() && !used_adds.contains(&remove_idx);
        if score >= 0.18 || positional {
            let chosen = if positional && score < 0.18 {
                remove_idx
            } else {
                add_idx
            };
            if !used_adds.insert(chosen) {
                continue;
            }
            pairs.push((remove.clone(), adds[chosen].clone()));
        }
    }
    pairs
}

fn line_similarity(old: &str, new: &str) -> f32 {
    if old == new {
        return 1.0;
    }
    let old_tokens = tokenize(old);
    let new_tokens = tokenize(new);
    if old_tokens.is_empty() && new_tokens.is_empty() {
        return 1.0;
    }
    let old_words: Vec<&str> = old_tokens
        .iter()
        .filter(|token| token.kind != TokenKind::Whitespace)
        .map(|token| token.text.as_str())
        .collect();
    let new_words: Vec<&str> = new_tokens
        .iter()
        .filter(|token| token.kind != TokenKind::Whitespace)
        .map(|token| token.text.as_str())
        .collect();
    if old_words.is_empty() && new_words.is_empty() {
        return if normalize_whitespace(old) == normalize_whitespace(new) {
            1.0
        } else {
            0.0
        };
    }
    lcs_len(&old_words, &new_words) as f32 / max(old_words.len(), new_words.len()) as f32
}

fn analyze_paired_line(old: &str, new: &str, side: Side) -> LineAnalysis {
    if strip_all_whitespace(old) == strip_all_whitespace(new) {
        let content = match side {
            Side::Pre => old,
            Side::Post => new,
        };
        return LineAnalysis {
            novelty: LineNovelty::WhitespaceOnly,
            ranges: whole_ranges(content, NoveltyWeight::Muted),
            ..LineAnalysis::default()
        };
    }

    let old_tokens = tokenize(old);
    let new_tokens = tokenize(new);
    let (old_marks, new_marks) = diff_token_marks(&old_tokens, &new_tokens);
    let marks = match side {
        Side::Pre => old_marks,
        Side::Post => new_marks,
    };
    let tokens = match side {
        Side::Pre => old_tokens,
        Side::Post => new_tokens,
    };
    let has_muted = marks.contains(&NoveltyWeight::Muted);
    let has_full = marks.contains(&NoveltyWeight::Full);
    let novelty = if has_muted && has_full {
        LineNovelty::Mixed
    } else if has_full {
        LineNovelty::Content
    } else {
        LineNovelty::None
    };
    LineAnalysis {
        novelty,
        ranges: tokens
            .iter()
            .zip(marks)
            .map(|(token, weight)| NoveltyRange {
                start: token.start,
                end: token.end,
                weight,
            })
            .collect(),
        ..LineAnalysis::default()
    }
}

fn analyze_unpaired_line(content: &str) -> LineAnalysis {
    LineAnalysis {
        novelty: LineNovelty::Unpaired,
        ranges: whole_ranges(content, NoveltyWeight::Full),
        ..LineAnalysis::default()
    }
}

fn whole_ranges(content: &str, weight: NoveltyWeight) -> Vec<NoveltyRange> {
    if content.is_empty() {
        vec![]
    } else {
        vec![NoveltyRange {
            start: 0,
            end: content.len(),
            weight,
        }]
    }
}

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
enum TokenKind {
    Identifier,
    Whitespace,
    Other,
}

#[derive(Clone, Debug, Eq, PartialEq)]
struct Token {
    start: usize,
    end: usize,
    kind: TokenKind,
    text: String,
}

fn tokenize(input: &str) -> Vec<Token> {
    let mut tokens = vec![];
    let mut iter = input.char_indices().peekable();
    while let Some((start, ch)) = iter.next() {
        let kind = if is_ident_start(ch) {
            TokenKind::Identifier
        } else if ch.is_whitespace() {
            TokenKind::Whitespace
        } else {
            TokenKind::Other
        };
        let mut end = start + ch.len_utf8();
        while let Some(&(idx, next)) = iter.peek() {
            let same = match kind {
                TokenKind::Identifier => is_ident_continue(next),
                TokenKind::Whitespace => next.is_whitespace(),
                TokenKind::Other => {
                    !next.is_whitespace() && !is_ident_start(next) && next.is_ascii_punctuation()
                }
            };
            if !same {
                break;
            }
            iter.next();
            end = idx + next.len_utf8();
        }
        tokens.push(Token {
            start,
            end,
            kind,
            text: input[start..end].to_string(),
        });
    }
    tokens
}

fn diff_token_marks(old: &[Token], new: &[Token]) -> (Vec<NoveltyWeight>, Vec<NoveltyWeight>) {
    let pairs = lcs_pairs_by(old, new, |left, right| {
        left.kind == right.kind && left.text == right.text
    });
    let mut old_marks = vec![NoveltyWeight::Full; old.len()];
    let mut new_marks = vec![NoveltyWeight::Full; new.len()];
    let mut old_cursor = 0usize;
    let mut new_cursor = 0usize;
    for (old_match, new_match) in pairs {
        mark_changed_segment(
            old,
            new,
            &mut old_marks,
            &mut new_marks,
            old_cursor,
            old_match,
            new_cursor,
            new_match,
        );
        old_marks[old_match] = NoveltyWeight::Unchanged;
        new_marks[new_match] = NoveltyWeight::Unchanged;
        old_cursor = old_match + 1;
        new_cursor = new_match + 1;
    }
    mark_changed_segment(
        old,
        new,
        &mut old_marks,
        &mut new_marks,
        old_cursor,
        old.len(),
        new_cursor,
        new.len(),
    );
    (old_marks, new_marks)
}

#[allow(clippy::too_many_arguments)]
fn mark_changed_segment(
    old: &[Token],
    new: &[Token],
    old_marks: &mut [NoveltyWeight],
    new_marks: &mut [NoveltyWeight],
    old_start: usize,
    old_end: usize,
    new_start: usize,
    new_end: usize,
) {
    let all_whitespace = old[old_start..old_end]
        .iter()
        .chain(new[new_start..new_end].iter())
        .all(|token| token.kind == TokenKind::Whitespace);
    for idx in old_start..old_end {
        old_marks[idx] = if all_whitespace || old[idx].kind == TokenKind::Whitespace {
            NoveltyWeight::Muted
        } else {
            NoveltyWeight::Full
        };
    }
    for idx in new_start..new_end {
        new_marks[idx] = if all_whitespace || new[idx].kind == TokenKind::Whitespace {
            NoveltyWeight::Muted
        } else {
            NoveltyWeight::Full
        };
    }
}

fn lcs_len<T: Eq>(left: &[T], right: &[T]) -> usize {
    let mut row = vec![0usize; right.len() + 1];
    for left_item in left {
        let mut prev = 0usize;
        for (j, right_item) in right.iter().enumerate() {
            let saved = row[j + 1];
            if left_item == right_item {
                row[j + 1] = prev + 1;
            } else {
                row[j + 1] = max(row[j + 1], row[j]);
            }
            prev = saved;
        }
    }
    row[right.len()]
}

fn lcs_pairs_by<T>(left: &[T], right: &[T], eq: impl Fn(&T, &T) -> bool) -> Vec<(usize, usize)> {
    let mut table = vec![vec![0usize; right.len() + 1]; left.len() + 1];
    for i in (0..left.len()).rev() {
        for j in (0..right.len()).rev() {
            table[i][j] = if eq(&left[i], &right[j]) {
                table[i + 1][j + 1] + 1
            } else {
                max(table[i + 1][j], table[i][j + 1])
            };
        }
    }
    let mut i = 0usize;
    let mut j = 0usize;
    let mut pairs = vec![];
    while i < left.len() && j < right.len() {
        if eq(&left[i], &right[j]) {
            pairs.push((i, j));
            i += 1;
            j += 1;
        } else if table[i + 1][j] >= table[i][j + 1] {
            i += 1;
        } else {
            j += 1;
        }
    }
    pairs
}

fn strip_all_whitespace(input: &str) -> String {
    input.chars().filter(|ch| !ch.is_whitespace()).collect()
}

fn normalize_whitespace(input: &str) -> String {
    input.split_whitespace().collect::<Vec<_>>().join(" ")
}

fn is_ident_start(ch: char) -> bool {
    ch == '_' || ch.is_ascii_alphabetic()
}

fn is_ident_continue(ch: char) -> bool {
    ch == '_' || ch.is_ascii_alphanumeric()
}

fn detect_moves(diff: &Diff, analysis: &mut Analysis) {
    let runs = collect_runs(diff);
    let removes: Vec<&RunRef> = runs
        .iter()
        .filter(|run| run.op == DiffOp::Remove && run.len >= MOVE_MIN_LINES)
        .collect();
    let adds: Vec<&RunRef> = runs
        .iter()
        .filter(|run| run.op == DiffOp::Add && run.len >= MOVE_MIN_LINES)
        .collect();
    let mut used_adds = HashSet::new();

    for remove in removes {
        let mut best: Option<(&RunRef, f32)> = None;
        for add in &adds {
            let add_key = (add.file, add.hunk, add.start);
            if used_adds.contains(&add_key) {
                continue;
            }
            let score = run_similarity(&remove.normalized, &add.normalized);
            if score >= 0.68 && best.is_none_or(|(_, best_score)| score > best_score) {
                best = Some((*add, score));
            }
        }
        let Some((add, _score)) = best else {
            continue;
        };
        used_adds.insert((add.file, add.hunk, add.start));
        let len = min(remove.len, add.len);
        let move_id = analysis.moves.len();
        analysis.moves.push(MoveBlock {
            remove_file: remove.file,
            remove_hunk: remove.hunk,
            remove_start: remove.start,
            add_file: add.file,
            add_hunk: add.hunk,
            add_start: add.start,
            len,
        });
        for offset in 0..len {
            for id in [
                LineId {
                    file: remove.file,
                    hunk: remove.hunk,
                    line: remove.start + offset,
                },
                LineId {
                    file: add.file,
                    hunk: add.hunk,
                    line: add.start + offset,
                },
            ] {
                analysis.lines.entry(id).or_default().move_id = Some(move_id);
            }
        }
    }
}

fn collect_runs(diff: &Diff) -> Vec<RunRef> {
    let mut runs = vec![];
    for (file_idx, file) in diff.files.iter().enumerate() {
        for (hunk_idx, hunk) in file.hunks.iter().enumerate() {
            let mut index = 0usize;
            while index < hunk.lines.len() {
                let op = hunk.lines[index].op;
                if !matches!(op, DiffOp::Add | DiffOp::Remove) {
                    index += 1;
                    continue;
                }
                let start = index;
                while index < hunk.lines.len() && hunk.lines[index].op == op {
                    index += 1;
                }
                let lines = &hunk.lines[start..index];
                runs.push(RunRef {
                    file: file_idx,
                    hunk: hunk_idx,
                    start,
                    len: lines.len(),
                    normalized: lines
                        .iter()
                        .map(|line| normalize_whitespace(&line.content))
                        .collect(),
                    op,
                });
            }
        }
    }
    runs
}

fn run_similarity(remove: &[String], add: &[String]) -> f32 {
    if remove.is_empty() || add.is_empty() {
        return 0.0;
    }
    let common = lcs_len(remove, add);
    if common < MOVE_MIN_LINES {
        return 0.0;
    }
    common as f32 / max(remove.len(), add.len()) as f32
}

fn detect_roles(pairs: &[ChangePair]) -> Vec<RoleGroup> {
    let mut counts: BTreeMap<(String, String), usize> = BTreeMap::new();
    for pair in pairs {
        for (removed, added) in identifier_pairs(&pair.remove.content, &pair.add.content) {
            if removed != added {
                *counts.entry((removed, added)).or_insert(0) += 1;
            }
        }
    }
    let mut ordered: Vec<((String, String), usize)> = counts
        .into_iter()
        .filter(|((removed, added), count)| removed != added && *count >= 3)
        .collect();
    ordered.sort_by(|a, b| {
        b.1.cmp(&a.1)
            .then_with(|| a.0.0.cmp(&b.0.0))
            .then_with(|| a.0.1.cmp(&b.0.1))
    });
    ordered
        .into_iter()
        .take(MAX_ROLE_GROUPS)
        .enumerate()
        .map(|(idx, ((removed, added), count))| RoleGroup {
            removed,
            added,
            count,
            color: ROLE_PALETTE[idx],
        })
        .collect()
}

fn identifier_pairs(old: &str, new: &str) -> Vec<(String, String)> {
    let old_tokens: Vec<Token> = tokenize(old)
        .into_iter()
        .filter(|token| token.kind == TokenKind::Identifier)
        .collect();
    let new_tokens: Vec<Token> = tokenize(new)
        .into_iter()
        .filter(|token| token.kind == TokenKind::Identifier)
        .collect();
    let pairs = lcs_pairs_by(&old_tokens, &new_tokens, |left, right| {
        left.text == right.text
    });
    let mut result = vec![];
    let mut old_cursor = 0usize;
    let mut new_cursor = 0usize;
    for (old_match, new_match) in pairs {
        pair_identifier_segment(
            &old_tokens,
            &new_tokens,
            old_cursor,
            old_match,
            new_cursor,
            new_match,
            &mut result,
        );
        old_cursor = old_match + 1;
        new_cursor = new_match + 1;
    }
    pair_identifier_segment(
        &old_tokens,
        &new_tokens,
        old_cursor,
        old_tokens.len(),
        new_cursor,
        new_tokens.len(),
        &mut result,
    );
    result
}

#[allow(clippy::too_many_arguments)]
fn pair_identifier_segment(
    old: &[Token],
    new: &[Token],
    old_start: usize,
    old_end: usize,
    new_start: usize,
    new_end: usize,
    out: &mut Vec<(String, String)>,
) {
    let len = min(
        old_end.saturating_sub(old_start),
        new_end.saturating_sub(new_start),
    );
    for offset in 0..len {
        out.push((
            old[old_start + offset].text.clone(),
            new[new_start + offset].text.clone(),
        ));
    }
}

fn apply_roles(diff: &Diff, analysis: &mut Analysis) {
    if analysis.roles.is_empty() {
        return;
    }
    for (file_idx, file) in diff.files.iter().enumerate() {
        for (hunk_idx, hunk) in file.hunks.iter().enumerate() {
            for (line_idx, line) in hunk.lines.iter().enumerate() {
                let side = match line.op {
                    DiffOp::Remove => Side::Pre,
                    DiffOp::Add => Side::Post,
                    DiffOp::Context | DiffOp::Note => continue,
                };
                let mut ranges = vec![];
                for token in tokenize(&line.content) {
                    if token.kind != TokenKind::Identifier {
                        continue;
                    }
                    for (role_idx, role) in analysis.roles.iter().enumerate() {
                        let target = match side {
                            Side::Pre => &role.removed,
                            Side::Post => &role.added,
                        };
                        if token.text == *target {
                            ranges.push(RoleRange {
                                start: token.start,
                                end: token.end,
                                role: role_idx,
                            });
                        }
                    }
                }
                if !ranges.is_empty() {
                    let id = LineId {
                        file: file_idx,
                        hunk: hunk_idx,
                        line: line_idx,
                    };
                    analysis.lines.entry(id).or_default().role_ranges = ranges;
                }
            }
        }
    }
}

fn syntax_maps(diff: &Diff) -> HashMap<LineId, Vec<SyntaxRange>> {
    let mut maps = HashMap::new();
    for (file_idx, file) in diff.files.iter().enumerate() {
        for side in [Side::Pre, Side::Post] {
            let path = match side {
                Side::Pre => display_old_path(file),
                Side::Post => display_new_path(file),
            };
            let Some(language) = language_for_path(&path) else {
                continue;
            };
            let (source, recon) = reconstruct_side(file_idx, file, side);
            if source.is_empty() {
                continue;
            }
            let spans = syntax_spans(language, &source);
            distribute_syntax_spans(&mut maps, &recon, &spans);
        }
    }
    maps
}

fn reconstruct_side(file_idx: usize, file: &DiffFile, side: Side) -> (String, Vec<ReconLine>) {
    let mut source = String::new();
    let mut recon = vec![];
    for (hunk_idx, hunk) in file.hunks.iter().enumerate() {
        for (line_idx, line) in hunk.lines.iter().enumerate() {
            let include = matches!(
                (side, line.op),
                (Side::Pre, DiffOp::Context | DiffOp::Remove)
                    | (Side::Post, DiffOp::Context | DiffOp::Add)
            );
            if !include {
                continue;
            }
            let start = source.len();
            source.push_str(&line.content);
            let end = source.len();
            source.push('\n');
            recon.push(ReconLine {
                id: LineId {
                    file: file_idx,
                    hunk: hunk_idx,
                    line: line_idx,
                },
                start,
                end,
            });
        }
    }
    (source, recon)
}

fn syntax_spans(language: LanguageKind, source: &str) -> Vec<SyntaxRange> {
    let mut spans = lexical_syntax_spans(language, source);
    let mut parser = Parser::new();
    let tree_language: Language = match language {
        LanguageKind::Rust => tree_sitter_rust::LANGUAGE.into(),
        LanguageKind::Python => tree_sitter_python::LANGUAGE.into(),
        LanguageKind::Bash => tree_sitter_bash::LANGUAGE.into(),
    };
    if parser.set_language(&tree_language).is_err() {
        return spans;
    }
    let Some(tree) = parser.parse(source, None) else {
        return spans;
    };
    collect_tree_syntax(tree.root_node(), source, &mut spans);
    spans.sort_by(|left, right| {
        left.start
            .cmp(&right.start)
            .then_with(|| right.end.cmp(&left.end))
    });
    spans
}

fn collect_tree_syntax(node: Node<'_>, source: &str, spans: &mut Vec<SyntaxRange>) {
    if let Some(class) = classify_tree_node(node, source) {
        spans.push(SyntaxRange {
            start: node.start_byte(),
            end: node.end_byte(),
            class,
        });
        if matches!(class, SyntaxClass::Comment | SyntaxClass::String) {
            return;
        }
    }
    for idx in 0..node.child_count() {
        if let Some(child) = node.child(idx) {
            collect_tree_syntax(child, source, spans);
        }
    }
}

fn classify_tree_node(node: Node<'_>, source: &str) -> Option<SyntaxClass> {
    let kind = node.kind();
    if node.end_byte() <= node.start_byte() {
        return None;
    }
    if kind.contains("comment") {
        return Some(SyntaxClass::Comment);
    }
    if kind.contains("string") || kind.contains("char_literal") {
        return Some(SyntaxClass::String);
    }
    if kind.contains("integer") || kind.contains("float") || kind.contains("number") {
        return Some(SyntaxClass::Number);
    }
    if matches!(
        kind,
        "type_identifier" | "primitive_type" | "scoped_type_identifier"
    ) {
        return Some(SyntaxClass::Type);
    }
    if matches!(
        kind,
        "identifier" | "field_identifier" | "shorthand_field_identifier"
    ) {
        return Some(SyntaxClass::Identifier);
    }
    let text = source
        .get(node.start_byte()..node.end_byte())
        .unwrap_or_default();
    if is_keyword(text) {
        return Some(SyntaxClass::Keyword);
    }
    if is_operator(text) {
        return Some(SyntaxClass::Operator);
    }
    None
}

fn lexical_syntax_spans(language: LanguageKind, source: &str) -> Vec<SyntaxRange> {
    let mut spans = vec![];
    let bytes = source.as_bytes();
    let mut idx = 0usize;
    while idx < bytes.len() {
        let ch = source[idx..].chars().next().unwrap_or('\0');
        if ch == '#' && language != LanguageKind::Rust {
            let end = source[idx..]
                .find('\n')
                .map(|offset| idx + offset)
                .unwrap_or(source.len());
            spans.push(SyntaxRange {
                start: idx,
                end,
                class: SyntaxClass::Comment,
            });
            idx = end;
            continue;
        }
        if ch == '/' && source[idx..].starts_with("//") && language == LanguageKind::Rust {
            let end = source[idx..]
                .find('\n')
                .map(|offset| idx + offset)
                .unwrap_or(source.len());
            spans.push(SyntaxRange {
                start: idx,
                end,
                class: SyntaxClass::Comment,
            });
            idx = end;
            continue;
        }
        if ch == '"' || ch == '\'' {
            let quote = ch;
            let start = idx;
            idx += ch.len_utf8();
            let mut escaped = false;
            while idx < source.len() {
                let next = source[idx..].chars().next().unwrap_or('\0');
                idx += next.len_utf8();
                if escaped {
                    escaped = false;
                } else if next == '\\' {
                    escaped = true;
                } else if next == quote {
                    break;
                }
            }
            spans.push(SyntaxRange {
                start,
                end: idx,
                class: SyntaxClass::String,
            });
            continue;
        }
        if ch.is_ascii_digit() {
            let start = idx;
            idx += ch.len_utf8();
            while idx < source.len() {
                let next = source[idx..].chars().next().unwrap_or('\0');
                if !(next.is_ascii_alphanumeric() || next == '_' || next == '.') {
                    break;
                }
                idx += next.len_utf8();
            }
            spans.push(SyntaxRange {
                start,
                end: idx,
                class: SyntaxClass::Number,
            });
            continue;
        }
        if is_ident_start(ch) {
            let start = idx;
            idx += ch.len_utf8();
            while idx < source.len() {
                let next = source[idx..].chars().next().unwrap_or('\0');
                if !is_ident_continue(next) {
                    break;
                }
                idx += next.len_utf8();
            }
            let word = &source[start..idx];
            spans.push(SyntaxRange {
                start,
                end: idx,
                class: if is_keyword(word) {
                    SyntaxClass::Keyword
                } else {
                    SyntaxClass::Identifier
                },
            });
            continue;
        }
        idx += ch.len_utf8();
    }
    spans
}

fn distribute_syntax_spans(
    maps: &mut HashMap<LineId, Vec<SyntaxRange>>,
    recon: &[ReconLine],
    spans: &[SyntaxRange],
) {
    for span in spans {
        if span.start >= span.end {
            continue;
        }
        for line in recon {
            let start = max(span.start, line.start);
            let end = min(span.end, line.end);
            if start >= end {
                continue;
            }
            maps.entry(line.id).or_default().push(SyntaxRange {
                start: start - line.start,
                end: end - line.start,
                class: span.class,
            });
        }
    }
}

fn language_for_path(path: &str) -> Option<LanguageKind> {
    let lower = path.to_ascii_lowercase();
    if lower.ends_with(".rs") {
        Some(LanguageKind::Rust)
    } else if lower.ends_with(".py") || lower.ends_with(".pyw") {
        Some(LanguageKind::Python)
    } else if lower.ends_with(".sh")
        || lower.ends_with(".bash")
        || lower.ends_with(".zsh")
        || lower.ends_with("/bash")
        || lower.ends_with("/sh")
    {
        Some(LanguageKind::Bash)
    } else {
        None
    }
}

fn is_keyword(text: &str) -> bool {
    matches!(
        text,
        "as" | "async"
            | "await"
            | "break"
            | "case"
            | "class"
            | "const"
            | "continue"
            | "def"
            | "do"
            | "done"
            | "dyn"
            | "elif"
            | "else"
            | "enum"
            | "export"
            | "false"
            | "False"
            | "fi"
            | "fn"
            | "for"
            | "from"
            | "if"
            | "impl"
            | "import"
            | "in"
            | "let"
            | "loop"
            | "match"
            | "mod"
            | "move"
            | "mut"
            | "pub"
            | "return"
            | "self"
            | "Self"
            | "struct"
            | "then"
            | "trait"
            | "true"
            | "True"
            | "type"
            | "use"
            | "where"
            | "while"
    )
}

fn is_operator(text: &str) -> bool {
    matches!(
        text,
        "=" | "=="
            | "!="
            | "=>"
            | "->"
            | "::"
            | "+"
            | "-"
            | "*"
            | "/"
            | "%"
            | "&&"
            | "||"
            | "!"
            | "<"
            | ">"
            | "<="
            | ">="
            | "|"
            | "&"
            | "^"
            | "$"
    )
}

fn build_rendered_lines(
    diff: &Diff,
    analysis: &Analysis,
    options: SidiffOptions,
) -> Vec<RenderedLine> {
    let mut out = vec![];
    if !diff.preamble.is_empty() {
        for line in &diff.preamble {
            out.push(RenderedLine {
                text: paint_fg(line, Rgb::new(150, 150, 150), options.use_color),
            });
        }
    }

    for (file_idx, file) in diff.files.iter().enumerate() {
        let file_label = format!(
            "file {} -> {}",
            file.old_path.as_deref().unwrap_or("/dev/null"),
            file.new_path.as_deref().unwrap_or("/dev/null")
        );
        out.push(RenderedLine {
            text: paint_fg(&file_label, Rgb::new(120, 200, 220), options.use_color),
        });
        for header in &file.header {
            out.push(RenderedLine {
                text: paint_fg(header, Rgb::new(120, 120, 120), options.use_color),
            });
        }
        for (hunk_idx, hunk) in file.hunks.iter().enumerate() {
            out.push(RenderedLine {
                text: paint_fg(&hunk.header, Rgb::new(105, 180, 255), options.use_color),
            });
            let mut last_move = None;
            for (line_idx, line) in hunk.lines.iter().enumerate() {
                let id = LineId {
                    file: file_idx,
                    hunk: hunk_idx,
                    line: line_idx,
                };
                let meta = analysis.lines.get(&id);
                if should_hide_line(meta, line, options.whitespace_mode) {
                    continue;
                }
                if meta.and_then(|meta| meta.move_id) != last_move {
                    last_move = meta.and_then(|meta| meta.move_id);
                    if let Some(move_id) = last_move {
                        out.push(RenderedLine {
                            text: render_move_label(diff, analysis, move_id, id, options),
                        });
                    }
                }
                out.push(RenderedLine {
                    text: render_diff_line(diff, analysis, id, line, options),
                });
            }
        }
    }

    if !analysis.roles.is_empty() {
        out.push(RenderedLine {
            text: render_role_summary(&analysis.roles, options),
        });
    }
    out
}

fn should_hide_line(
    meta: Option<&LineAnalysis>,
    line: &DiffLine,
    whitespace_mode: WhitespaceMode,
) -> bool {
    whitespace_mode == WhitespaceMode::Hide
        && matches!(line.op, DiffOp::Add | DiffOp::Remove)
        && meta.is_some_and(|meta| meta.novelty == LineNovelty::WhitespaceOnly)
}

fn render_move_label(
    diff: &Diff,
    analysis: &Analysis,
    move_id: usize,
    current: LineId,
    options: SidiffOptions,
) -> String {
    let Some(block) = analysis.moves.get(move_id) else {
        return String::new();
    };
    let (label, color) = if current.file == block.add_file
        && current.hunk == block.add_hunk
        && current.line >= block.add_start
    {
        let line =
            &diff.files[block.remove_file].hunks[block.remove_hunk].lines[block.remove_start];
        (
            format!(
                "  M moved from {}:L{}",
                display_old_path(&diff.files[block.remove_file]),
                line.old_lineno.unwrap_or(0)
            ),
            Rgb::new(175, 145, 255),
        )
    } else {
        let line = &diff.files[block.add_file].hunks[block.add_hunk].lines[block.add_start];
        (
            format!(
                "  M moved to {}:L{}",
                display_new_path(&diff.files[block.add_file]),
                line.new_lineno.unwrap_or(0)
            ),
            Rgb::new(175, 145, 255),
        )
    };
    paint_fg(&label, color, options.use_color)
}

fn render_diff_line(
    diff: &Diff,
    analysis: &Analysis,
    id: LineId,
    line: &DiffLine,
    options: SidiffOptions,
) -> String {
    let meta = analysis.lines.get(&id);
    let old = line
        .old_lineno
        .map(|num| format!("{num:>4}"))
        .unwrap_or_else(|| "    ".to_string());
    let new = line
        .new_lineno
        .map(|num| format!("{num:>4}"))
        .unwrap_or_else(|| "    ".to_string());
    let op = match line.op {
        DiffOp::Context => " ",
        DiffOp::Add => "+",
        DiffOp::Remove => "-",
        DiffOp::Note => "\\",
    };
    let move_marker = if meta.and_then(|meta| meta.move_id).is_some() {
        "M"
    } else {
        " "
    };
    let role_glyph = if options.use_color {
        " "
    } else {
        meta.and_then(|meta| meta.role_ranges.first())
            .map(|range| ROLE_GLYPHS[range.role])
            .unwrap_or(" ")
    };
    let mut line_out = format!("{role_glyph}{move_marker} {old} {new} {op} ");
    if line.op == DiffOp::Note {
        line_out.push_str(&paint_fg(
            &line.content,
            Rgb::new(150, 150, 150),
            options.use_color,
        ));
        return line_out;
    }
    let syntax = analysis.syntax.get(&id).map(Vec::as_slice).unwrap_or(&[]);
    line_out.push_str(&render_content(
        &line.content,
        line.op,
        meta,
        syntax,
        &analysis.roles,
        options,
    ));
    if meta.is_some_and(|meta| meta.paired_with.is_none())
        && matches!(line.op, DiffOp::Add | DiffOp::Remove)
    {
        let path = match line.op {
            DiffOp::Add => display_new_path(&diff.files[id.file]),
            DiffOp::Remove => display_old_path(&diff.files[id.file]),
            DiffOp::Context | DiffOp::Note => display_path(&diff.files[id.file]),
        };
        line_out.push_str(&paint_fg(
            &format!("  [{path}]"),
            Rgb::new(110, 110, 110),
            options.use_color,
        ));
    }
    line_out
}

fn render_content(
    content: &str,
    op: DiffOp,
    meta: Option<&LineAnalysis>,
    syntax: &[SyntaxRange],
    roles: &[RoleGroup],
    options: SidiffOptions,
) -> String {
    if content.is_empty() {
        return String::new();
    }
    let boundaries = span_boundaries(content, meta, syntax);
    let mut out = String::new();
    for pair in boundaries.windows(2) {
        let start = pair[0];
        let end = pair[1];
        if start == end {
            continue;
        }
        let text = &content[start..end];
        let novelty = meta
            .and_then(|meta| novelty_at(meta, start, end))
            .unwrap_or(match op {
                DiffOp::Context => NoveltyWeight::Unchanged,
                DiffOp::Add | DiffOp::Remove => NoveltyWeight::Full,
                DiffOp::Note => NoveltyWeight::Unchanged,
            });
        let syntax_class = syntax_at(syntax, start, end);
        let role = meta.and_then(|meta| role_at(meta, start, end));
        out.push_str(&style_span(
            text,
            op,
            novelty,
            syntax_class,
            role.and_then(|idx| roles.get(idx)),
            options,
        ));
    }
    out
}

fn span_boundaries(
    content: &str,
    meta: Option<&LineAnalysis>,
    syntax: &[SyntaxRange],
) -> Vec<usize> {
    let mut boundaries = vec![0, content.len()];
    if let Some(meta) = meta {
        for range in &meta.ranges {
            boundaries.push(range.start);
            boundaries.push(range.end);
        }
        for range in &meta.role_ranges {
            boundaries.push(range.start);
            boundaries.push(range.end);
        }
    }
    for range in syntax {
        boundaries.push(range.start);
        boundaries.push(range.end);
    }
    boundaries.retain(|idx| *idx <= content.len() && content.is_char_boundary(*idx));
    boundaries.sort_unstable();
    boundaries.dedup();
    boundaries
}

fn novelty_at(meta: &LineAnalysis, start: usize, end: usize) -> Option<NoveltyWeight> {
    meta.ranges
        .iter()
        .find(|range| range.start <= start && range.end >= end)
        .map(|range| range.weight)
}

fn syntax_at(syntax: &[SyntaxRange], start: usize, end: usize) -> Option<SyntaxClass> {
    syntax
        .iter()
        .rev()
        .find(|range| range.start <= start && range.end >= end)
        .map(|range| range.class)
}

fn role_at(meta: &LineAnalysis, start: usize, end: usize) -> Option<usize> {
    meta.role_ranges
        .iter()
        .find(|range| range.start <= start && range.end >= end)
        .map(|range| range.role)
}

fn style_span(
    text: &str,
    op: DiffOp,
    novelty: NoveltyWeight,
    syntax: Option<SyntaxClass>,
    role: Option<&RoleGroup>,
    options: SidiffOptions,
) -> String {
    if !options.use_color {
        return text.to_string();
    }
    let mut fg = match (op, novelty) {
        (DiffOp::Add, NoveltyWeight::Full) => Rgb::new(80, 235, 150),
        (DiffOp::Remove, NoveltyWeight::Full) => Rgb::new(255, 105, 115),
        (DiffOp::Add, NoveltyWeight::Muted) | (DiffOp::Remove, NoveltyWeight::Muted) => {
            Rgb::new(125, 125, 125)
        }
        (DiffOp::Add, NoveltyWeight::Unchanged) => Rgb::new(100, 155, 120),
        (DiffOp::Remove, NoveltyWeight::Unchanged) => Rgb::new(165, 95, 100),
        (DiffOp::Context, _) => syntax_color(syntax).unwrap_or(Rgb::new(205, 205, 205)),
        (DiffOp::Note, _) => Rgb::new(150, 150, 150),
    };
    if matches!(op, DiffOp::Add | DiffOp::Remove)
        && novelty == NoveltyWeight::Unchanged
        && let Some(syntax_color) = syntax_color(syntax)
    {
        fg = syntax_color.mix_with(fg, 0.42);
    }
    let bg = role.map(|role| {
        role.color
            .mix_with(Rgb::new(18, 20, 24), options.tint_intensity.alpha())
    });
    paint(text, fg, bg, options.use_color)
}

fn syntax_color(class: Option<SyntaxClass>) -> Option<Rgb> {
    Some(match class? {
        SyntaxClass::Comment => Rgb::new(110, 145, 115),
        SyntaxClass::String => Rgb::new(225, 190, 120),
        SyntaxClass::Number => Rgb::new(185, 160, 255),
        SyntaxClass::Keyword => Rgb::new(115, 175, 255),
        SyntaxClass::Type => Rgb::new(105, 215, 210),
        SyntaxClass::Identifier => Rgb::new(220, 220, 220),
        SyntaxClass::Operator => Rgb::new(180, 180, 180),
    })
}

fn paint_fg(text: &str, fg: Rgb, use_color: bool) -> String {
    paint(text, fg, None, use_color)
}

fn paint(text: &str, fg: Rgb, bg: Option<Rgb>, use_color: bool) -> String {
    if !use_color || text.is_empty() {
        return text.to_string();
    }
    let mut out = format!("\x1b[38;2;{};{};{}m", fg.r, fg.g, fg.b);
    if let Some(bg) = bg {
        out.push_str(&format!("\x1b[48;2;{};{};{}m", bg.r, bg.g, bg.b));
    }
    out.push_str(text);
    out.push_str(RESET);
    out
}

fn render_role_summary(roles: &[RoleGroup], options: SidiffOptions) -> String {
    let mut parts = vec![];
    for (idx, role) in roles.iter().enumerate() {
        let label = format!(
            "{} {}->{} x{}",
            ROLE_GLYPHS[idx], role.removed, role.added, role.count
        );
        if options.use_color {
            parts.push(paint(
                &label,
                Rgb::new(235, 235, 235),
                Some(
                    role.color
                        .mix_with(Rgb::new(18, 20, 24), options.tint_intensity.alpha()),
                ),
                true,
            ));
        } else {
            parts.push(label);
        }
    }
    format!("roles: {}", parts.join("  "))
}

fn display_path(file: &DiffFile) -> String {
    file.new_path
        .as_ref()
        .or(file.old_path.as_ref())
        .cloned()
        .unwrap_or_else(|| "(unknown)".to_string())
}

fn display_old_path(file: &DiffFile) -> String {
    file.old_path
        .clone()
        .unwrap_or_else(|| "/dev/null".to_string())
}

fn display_new_path(file: &DiffFile) -> String {
    file.new_path
        .clone()
        .unwrap_or_else(|| "/dev/null".to_string())
}

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

    const SAMPLE: &str = "\
diff --git a/src/main.rs b/src/main.rs
index 1111111..2222222 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -1,6 +1,6 @@
 fn main() {
-    let old_name = compute(1);
-    println!(\"{}\", old_name);
+    let new_name = compute(2);
+    println!(\"{}\", new_name);
 }
";

    #[test]
    fn parser_reads_git_unified_diff() {
        let diff = parse_unified_diff(SAMPLE);
        assert_eq!(diff.files.len(), 1);
        assert_eq!(diff.files[0].old_path.as_deref(), Some("src/main.rs"));
        assert_eq!(diff.files[0].new_path.as_deref(), Some("src/main.rs"));
        assert_eq!(diff.files[0].hunks.len(), 1);
        assert_eq!(diff.files[0].hunks[0].old_start, 1);
        assert_eq!(diff.files[0].hunks[0].new_start, 1);
        assert_eq!(diff.files[0].hunks[0].lines[1].old_lineno, Some(2));
        assert_eq!(diff.files[0].hunks[0].lines[3].new_lineno, Some(2));
    }

    #[test]
    fn whitespace_only_changes_are_detected() {
        let old = "let value = 1;";
        let new = "    let value = 1;";
        let meta = analyze_paired_line(old, new, Side::Post);
        assert_eq!(meta.novelty, LineNovelty::WhitespaceOnly);
        assert!(
            meta.ranges
                .iter()
                .all(|range| range.weight == NoveltyWeight::Muted)
        );
    }

    #[test]
    fn word_delta_keeps_unchanged_tokens_dim() {
        let meta = analyze_paired_line("let old_name = 1;", "let new_name = 1;", Side::Post);
        assert_eq!(meta.novelty, LineNovelty::Content);
        let tokens = tokenize("let new_name = 1;");
        let changed: Vec<&str> = tokens
            .iter()
            .filter(|token| {
                meta.ranges
                    .iter()
                    .any(|range| range.start == token.start && range.weight == NoveltyWeight::Full)
            })
            .map(|token| token.text.as_str())
            .collect();
        assert_eq!(changed, vec!["new_name"]);
    }

    #[test]
    fn recurring_identifier_pairs_become_roles() {
        let pairs = vec![
            pair("old_name", "new_name"),
            pair("old_name", "new_name"),
            pair("old_name", "new_name"),
        ];
        let roles = detect_roles(&pairs);
        assert_eq!(roles.len(), 1);
        assert_eq!(roles[0].removed, "old_name");
        assert_eq!(roles[0].added, "new_name");
    }

    #[test]
    fn moved_blocks_are_marked() {
        let input = "\
diff --git a/a.rs b/a.rs
--- a/a.rs
+++ b/a.rs
@@ -1,5 +1,2 @@
-fn alpha() {}
-fn beta() {}
-fn gamma() {}
 fn stay() {}
@@ -20,2 +17,5 @@
 fn other() {}
+fn alpha() {}
+fn beta() {}
+fn gamma() {}
";
        let diff = parse_unified_diff(input);
        let analysis = analyze_diff(&diff);
        assert_eq!(analysis.moves.len(), 1);
        assert!(
            analysis
                .lines
                .values()
                .filter(|line| line.move_id == Some(0))
                .count()
                >= 6
        );
    }

    #[test]
    fn no_color_output_has_role_glyphs_and_no_ansi() {
        let input = "\
diff --git a/lib.rs b/lib.rs
--- a/lib.rs
+++ b/lib.rs
@@ -1,4 +1,4 @@
-let old_name = old_name + old_name;
+let new_name = new_name + new_name;
-call(old_name);
+call(new_name);
";
        let rendered = render_diff(
            input,
            SidiffOptions {
                use_color: false,
                ..SidiffOptions::default()
            },
        );
        assert!(!rendered.contains("\x1b["));
        assert!(rendered.contains("\u{2460}"));
        assert!(rendered.contains("old_name->new_name"));
    }

    #[test]
    fn pager_command_honors_pager_env_value() {
        assert_eq!(
            pager_command_from_value(Some(std::ffi::OsString::from("cat"))),
            PagerCommand::Shell(std::ffi::OsString::from("cat"))
        );
        assert_eq!(
            pager_command_from_value(Some(std::ffi::OsString::from(""))),
            PagerCommand::Disabled
        );
        assert_eq!(
            pager_command_from_value(Some(std::ffi::OsString::from("   "))),
            PagerCommand::Disabled
        );
        assert_eq!(
            pager_command_from_value(None),
            PagerCommand::Shell(std::ffi::OsString::from(DEFAULT_PAGER))
        );
    }

    #[test]
    fn diff_preview_command_honors_diff_env_value() {
        assert_eq!(
            diff_preview_command_from_value(Some(std::ffi::OsString::from("cat"))),
            DiffPreviewCommand::Shell(std::ffi::OsString::from("cat"))
        );
        assert_eq!(
            diff_preview_command_from_value(Some(std::ffi::OsString::from(""))),
            DiffPreviewCommand::Raw
        );
        assert_eq!(
            diff_preview_command_from_value(Some(std::ffi::OsString::from("   "))),
            DiffPreviewCommand::Raw
        );
        assert_eq!(
            diff_preview_command_from_value(None),
            DiffPreviewCommand::Sidiff
        );
    }

    #[test]
    fn blank_diff_preview_command_returns_raw_unified_diff() {
        let rendered =
            render_diff_preview_with_value(SAMPLE, Some(std::ffi::OsString::from(""))).unwrap();
        assert_eq!(rendered, SAMPLE);
    }

    #[test]
    fn shell_diff_preview_command_filters_raw_unified_diff() {
        let rendered =
            render_diff_preview_with_value(SAMPLE, Some(std::ffi::OsString::from("cat"))).unwrap();
        assert_eq!(rendered, SAMPLE);
    }

    #[cfg(unix)]
    #[test]
    fn shell_diff_preview_command_allows_early_exit_filters() {
        let rendered =
            render_diff_preview_with_value(SAMPLE, Some(std::ffi::OsString::from("head -n 3")))
                .unwrap();
        assert_eq!(
            rendered,
            "diff --git a/src/main.rs b/src/main.rs\nindex 1111111..2222222 100644\n--- a/src/main.rs\n"
        );
    }

    #[test]
    fn tree_sitter_rust_highlight_produces_keyword_span() {
        let spans = syntax_spans(LanguageKind::Rust, "fn main() {\n    let x = 1;\n}\n");
        assert!(
            spans
                .iter()
                .any(|span| span.class == SyntaxClass::Keyword && span.start == 0)
        );
    }

    #[test]
    fn cvd_palette_keeps_pairs_distinct() {
        for matrix in [PROTANOPIA, DEUTERANOPIA, TRITANOPIA] {
            for (left, left_color) in ROLE_PALETTE.iter().enumerate() {
                for (right, right_color) in ROLE_PALETTE.iter().enumerate().skip(left + 1) {
                    let a = simulate_cvd(*left_color, matrix);
                    let b = simulate_cvd(*right_color, matrix);
                    assert!(
                        rgb_distance(a, b) >= 20.0,
                        "palette entries {left} and {right} are too close after simulation"
                    );
                }
            }
        }
    }

    fn pair(removed: &str, added: &str) -> ChangePair {
        ChangePair {
            remove: LineRef {
                id: LineId::default(),
                content: removed.to_string(),
            },
            add: LineRef {
                id: LineId::default(),
                content: added.to_string(),
            },
        }
    }

    const PROTANOPIA: [[f32; 3]; 3] = [
        [0.152286, 1.052583, -0.204868],
        [0.114503, 0.786281, 0.099216],
        [-0.003882, -0.048116, 1.051998],
    ];
    const DEUTERANOPIA: [[f32; 3]; 3] = [
        [0.367322, 0.860646, -0.227968],
        [0.280085, 0.672501, 0.047413],
        [-0.011820, 0.042940, 0.968881],
    ];
    const TRITANOPIA: [[f32; 3]; 3] = [
        [1.255528, -0.076749, -0.178779],
        [-0.078411, 0.930809, 0.147602],
        [0.004733, 0.691367, 0.303900],
    ];

    fn simulate_cvd(color: Rgb, matrix: [[f32; 3]; 3]) -> Rgb {
        let input = [color.r as f32, color.g as f32, color.b as f32];
        let channel = |row: [f32; 3]| -> u8 {
            row.iter()
                .zip(input)
                .map(|(a, b)| a * b)
                .sum::<f32>()
                .round()
                .clamp(0.0, 255.0) as u8
        };
        Rgb::new(channel(matrix[0]), channel(matrix[1]), channel(matrix[2]))
    }

    fn rgb_distance(left: Rgb, right: Rgb) -> f32 {
        let dr = left.r as f32 - right.r as f32;
        let dg = left.g as f32 - right.g as f32;
        let db = left.b as f32 - right.b as f32;
        (dr * dr + dg * dg + db * db).sqrt()
    }
}