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
//! Step-through navigation for diffs
use crate::change::{Change, ChangeKind, ChangeSpan};
use crate::diff::DiffResult;
use serde::{Deserialize, Serialize};
/// Direction of the last step action
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Serialize, Deserialize)]
pub enum StepDirection {
#[default]
None,
Forward,
Backward,
}
/// Animation frame for phase-aware rendering
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum AnimationFrame {
#[default]
Idle,
FadeOut,
FadeIn,
}
/// The current state of stepping through a diff
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct StepState {
/// Current step index (0 = initial state, 1 = after first change applied, etc.)
pub current_step: usize,
/// Total number of steps (number of significant changes + 1 for initial state)
pub total_steps: usize,
/// IDs of changes that have been applied up to current step
pub applied_changes: Vec<usize>,
/// ID of the change being highlighted/animated at current step
pub active_change: Option<usize>,
/// Cursor change used for non-stepping navigation (does not imply animation)
#[serde(default, skip_serializing_if = "Option::is_none")]
pub cursor_change: Option<usize>,
/// Hunk currently being animated (distinct from cursor position in current_hunk)
#[serde(default, skip_serializing_if = "Option::is_none")]
pub animating_hunk: Option<usize>,
/// Direction of the last step action
pub step_direction: StepDirection,
/// Current hunk index (0-based)
pub current_hunk: usize,
/// Total number of hunks
pub total_hunks: usize,
/// True if the last navigation was a hunk navigation (for extent marker display)
#[serde(default)]
pub last_nav_was_hunk: bool,
/// True after hunkdown (full preview mode), cleared on first step
#[serde(default)]
pub hunk_preview_mode: bool,
/// True if preview was entered via hunkup (backward navigation)
#[serde(default)]
pub preview_from_backward: bool,
/// Show hunk extent markers while stepping (set by UI)
#[serde(default)]
pub show_hunk_extent_while_stepping: bool,
}
impl StepState {
pub fn new(total_changes: usize, total_hunks: usize) -> Self {
Self {
current_step: 0,
total_steps: total_changes + 1, // +1 for initial state
applied_changes: Vec::new(),
active_change: None,
cursor_change: None,
animating_hunk: None,
step_direction: StepDirection::None,
current_hunk: 0,
total_hunks,
last_nav_was_hunk: false,
hunk_preview_mode: false,
preview_from_backward: false,
show_hunk_extent_while_stepping: false,
}
}
/// Check if we're at the initial state (no changes applied)
pub fn is_at_start(&self) -> bool {
self.current_step == 0
}
/// Check if we're at the final state (all changes applied)
pub fn is_at_end(&self) -> bool {
self.current_step >= self.total_steps - 1
}
/// Get progress as a percentage
pub fn progress(&self) -> f64 {
if self.total_steps <= 1 {
return 100.0;
}
(self.current_step as f64 / (self.total_steps - 1) as f64) * 100.0
}
}
/// Navigator for stepping through diff changes
pub struct DiffNavigator {
/// The diff result we're navigating
diff: DiffResult,
/// Current step state
state: StepState,
/// Original content (for reconstructing views)
old_content: String,
/// New content (for reconstructing views)
new_content: String,
/// Mapping from change ID to hunk index
change_to_hunk: std::collections::HashMap<usize, usize>,
}
impl DiffNavigator {
pub fn new(diff: DiffResult, old_content: String, new_content: String) -> Self {
let total_changes = diff.significant_changes.len();
let total_hunks = diff.hunks.len();
// Build change ID to hunk index mapping
let mut change_to_hunk = std::collections::HashMap::new();
for (hunk_idx, hunk) in diff.hunks.iter().enumerate() {
for &change_id in &hunk.change_ids {
change_to_hunk.insert(change_id, hunk_idx);
}
}
Self {
diff,
state: StepState::new(total_changes, total_hunks),
old_content,
new_content,
change_to_hunk,
}
}
/// Get the current step state
pub fn state(&self) -> &StepState {
&self.state
}
/// Get mutable access to step state (test-only)
#[cfg(test)]
pub fn state_mut(&mut self) -> &mut StepState {
&mut self.state
}
/// Replace the current step state (used to restore stepping mode)
pub fn set_state(&mut self, state: StepState) -> bool {
if state.total_steps != self.state.total_steps
|| state.total_hunks != self.state.total_hunks
{
return false;
}
self.state = state;
true
}
/// Get the diff result
pub fn diff(&self) -> &DiffResult {
&self.diff
}
/// Set a non-animated cursor for classic (no-step) navigation.
pub fn set_cursor_hunk(&mut self, hunk_idx: usize, change_id: Option<usize>) {
if self.state.total_hunks > 0 {
self.state.current_hunk = hunk_idx.min(self.state.total_hunks - 1);
}
self.state.cursor_change = change_id;
}
/// Override the active cursor without changing applied steps.
pub fn set_cursor_override(&mut self, change_id: Option<usize>) {
self.state.cursor_change = change_id;
self.state.active_change = None;
self.state.step_direction = StepDirection::None;
self.state.animating_hunk = None;
}
/// Set cursor change without altering current hunk.
pub fn set_cursor_change(&mut self, change_id: Option<usize>) {
self.state.cursor_change = change_id;
}
/// Clear the non-animated cursor.
pub fn clear_cursor_change(&mut self) {
self.state.cursor_change = None;
}
/// Control hunk scope markers (extent indicators).
pub fn set_hunk_scope(&mut self, enabled: bool) {
self.state.last_nav_was_hunk = enabled;
}
/// Move to the next step
#[allow(clippy::should_implement_trait)]
pub fn next(&mut self) -> bool {
// Handle preview mode dissolution on first step
if self.state.hunk_preview_mode {
return self.dissolve_preview_for_step_down();
}
if self.state.is_at_end() {
return false;
}
let prev_hunk = self.state.current_hunk;
self.state.step_direction = StepDirection::Forward;
self.state.animating_hunk = None; // Clear hunk animation for single-step
// Get the next change to apply
let change_idx = self.state.current_step;
if change_idx < self.diff.significant_changes.len() {
let change_id = self.diff.significant_changes[change_idx];
self.state.applied_changes.push(change_id);
self.state.active_change = Some(change_id);
// Update current hunk
if let Some(hunk) = self.diff.hunk_for_change(change_id) {
self.state.current_hunk = hunk.id;
}
}
self.state.current_step += 1;
// Clear extent markers only when leaving hunk
if self.state.current_hunk != prev_hunk {
self.state.last_nav_was_hunk = false;
}
true
}
/// Dissolve preview mode on step down: keep first change, apply second
fn dissolve_preview_for_step_down(&mut self) -> bool {
if self.state.preview_from_backward {
self.state.hunk_preview_mode = false;
self.state.preview_from_backward = false;
return self.next();
}
let hunk = &self.diff.hunks[self.state.current_hunk];
// If hunk has only one change, stepping down exits the hunk
if hunk.change_ids.len() <= 1 {
self.state.hunk_preview_mode = false;
// Let normal next() handle moving to next change/hunk
return self.next();
}
// Keep only first change, unapply the rest
let first_change = hunk.change_ids[0];
let second_change = hunk.change_ids[1];
// Remove all changes in this hunk except the first
self.state
.applied_changes
.retain(|&id| !hunk.change_ids.contains(&id) || id == first_change);
// Apply second change
self.state.applied_changes.push(second_change);
// Update current_step to reflect actual applied changes
self.state.current_step = self.state.applied_changes.len();
self.state.active_change = Some(second_change);
self.state.step_direction = StepDirection::Forward;
self.state.hunk_preview_mode = false;
self.state.preview_from_backward = false;
self.state.animating_hunk = None;
// Preserve extent markers - we're still in the hunk scope
self.state.last_nav_was_hunk = true;
true
}
/// Move to the previous step
pub fn prev(&mut self) -> bool {
// Handle preview mode dissolution on first step up: exit hunk entirely
if self.state.hunk_preview_mode {
return self.dissolve_preview_for_step_up();
}
if self.state.is_at_start() {
return false;
}
let prev_hunk = self.state.current_hunk;
self.state.step_direction = StepDirection::Backward;
self.state.animating_hunk = None; // Clear hunk animation for single-step
self.state.current_step -= 1;
// Pop the change and set it as active for backward animation
if let Some(unapplied_change_id) = self.state.applied_changes.pop() {
self.state.active_change = Some(unapplied_change_id);
// Update current hunk based on last applied change
if let Some(&last_applied) = self.state.applied_changes.last() {
if let Some(hunk) = self.diff.hunk_for_change(last_applied) {
self.state.current_hunk = hunk.id;
}
} else {
self.state.current_hunk = 0;
}
} else {
self.state.active_change = None;
}
// Reset hunk cursor when at start; animation state cleared by CLI after animation completes
if self.state.is_at_start() {
self.state.current_hunk = 0;
}
// Clear extent markers when leaving hunk or at step 0
if self.state.is_at_start() || self.state.current_hunk != prev_hunk {
self.state.last_nav_was_hunk = false;
}
true
}
/// Dissolve preview mode on step up: unapply all changes in hunk and exit
fn dissolve_preview_for_step_up(&mut self) -> bool {
if self.state.preview_from_backward {
self.state.hunk_preview_mode = false;
self.state.preview_from_backward = false;
return self.prev();
}
let current_hunk_idx = self.state.current_hunk;
let hunk = &self.diff.hunks[current_hunk_idx];
// Set animating hunk for backward fade animation
self.state.animating_hunk = Some(current_hunk_idx);
// Unapply all changes in this hunk
for &change_id in &hunk.change_ids {
self.state.applied_changes.retain(|&id| id != change_id);
}
// Update current_step to reflect actual applied changes
self.state.current_step = self.state.applied_changes.len();
// Move to previous hunk if possible
if current_hunk_idx > 0 {
self.state.current_hunk = current_hunk_idx - 1;
}
self.state.step_direction = StepDirection::Backward;
// Keep cursor logic aligned with the destination hunk (bottom-most applied change)
self.state.active_change = self.state.applied_changes.last().copied();
self.state.hunk_preview_mode = false;
self.state.preview_from_backward = false;
self.state.last_nav_was_hunk = false; // Exiting hunk
true
}
/// Clear animation state (called after animation completes or one-frame render)
/// For backward steps, keeps cursor on last applied change (destination)
pub fn clear_active_change(&mut self) {
if self.state.step_direction == StepDirection::Backward {
self.state.active_change = self.state.applied_changes.last().copied();
} else {
self.state.active_change = None;
}
self.state.animating_hunk = None;
self.state.step_direction = StepDirection::None;
}
/// Jump to a specific step
pub fn goto(&mut self, step: usize) {
let target_step = step.min(self.state.total_steps - 1);
// Reset to start
self.state.current_step = 0;
self.state.applied_changes.clear();
self.state.active_change = None;
self.state.cursor_change = None;
self.state.animating_hunk = None;
self.state.current_hunk = 0;
self.state.last_nav_was_hunk = false; // Clear hunk nav flag on goto
self.state.hunk_preview_mode = false; // Clear preview mode on goto
self.state.preview_from_backward = false;
// Apply changes up to target step
for _ in 0..target_step {
self.next();
}
// Update which hunk we're in
self.update_current_hunk();
}
/// Go to the start
pub fn goto_start(&mut self) {
self.goto(0);
}
/// Go to the end
pub fn goto_end(&mut self) {
self.goto(self.state.total_steps - 1);
}
// ==================== Hunk Navigation ====================
/// Move to the next hunk, applying ALL changes (full preview mode).
/// If current hunk is not started, applies all its changes with cursor at top.
/// If current hunk is partially/fully applied, completes it and moves to next hunk.
/// Returns true if moved, false if no movement possible
pub fn next_hunk(&mut self) -> bool {
if self.diff.hunks.is_empty() {
return false;
}
// Preserve preview mode in case we return false without doing anything
let was_in_preview = self.state.hunk_preview_mode;
self.state.step_direction = StepDirection::Forward;
self.state.hunk_preview_mode = false; // Will be set to true after applying
self.state.preview_from_backward = false;
let current_hunk = &self.diff.hunks[self.state.current_hunk];
let has_applied_in_current = current_hunk
.change_ids
.iter()
.any(|id| self.state.applied_changes.contains(id));
// If current hunk has no applied changes, apply ALL changes (full preview)
if !has_applied_in_current {
let mut moved = false;
for &change_id in ¤t_hunk.change_ids {
if !self.state.applied_changes.contains(&change_id) {
self.state.applied_changes.push(change_id);
self.state.current_step += 1;
moved = true;
}
}
self.state.animating_hunk = Some(self.state.current_hunk);
self.state.active_change = current_hunk.change_ids.first().copied();
if moved {
self.state.last_nav_was_hunk = true;
self.state.hunk_preview_mode = true;
self.state.preview_from_backward = false;
}
return moved;
}
// Current hunk has applied changes - complete it first, exit preview mode
self.state.hunk_preview_mode = false;
self.state.preview_from_backward = false;
let mut completed_any = false;
for &change_id in ¤t_hunk.change_ids {
if !self.state.applied_changes.contains(&change_id) {
self.state.applied_changes.push(change_id);
self.state.current_step += 1;
completed_any = true;
}
}
// Move to next hunk
let next_hunk_idx = self.state.current_hunk + 1;
if next_hunk_idx >= self.diff.hunks.len() {
// No next hunk - if we completed current hunk, update state; otherwise return false
if completed_any {
self.state.animating_hunk = Some(self.state.current_hunk);
self.state.active_change = current_hunk.change_ids.last().copied();
self.state.last_nav_was_hunk = true;
return true;
}
// No movement, restore preview mode
self.state.hunk_preview_mode = was_in_preview;
return false;
}
let hunk = &self.diff.hunks[next_hunk_idx];
// Apply ALL changes of next hunk (full preview)
let mut moved = false;
for &change_id in &hunk.change_ids {
if !self.state.applied_changes.contains(&change_id) {
self.state.applied_changes.push(change_id);
self.state.current_step += 1;
moved = true;
}
}
self.state.animating_hunk = Some(next_hunk_idx);
self.state.active_change = hunk.change_ids.first().copied();
self.state.current_hunk = next_hunk_idx;
if moved {
self.state.last_nav_was_hunk = true;
self.state.hunk_preview_mode = true;
self.state.preview_from_backward = false;
}
moved
}
/// Move to the previous hunk, unapplying changes
/// Returns true if moved, false if nothing to unapply
pub fn prev_hunk(&mut self) -> bool {
if self.diff.hunks.is_empty() {
return false;
}
// Preserve preview mode in case we return false without doing anything
let was_in_preview = self.state.hunk_preview_mode;
// Clear preview mode
self.state.hunk_preview_mode = false;
self.state.preview_from_backward = false;
// On hunk 0, only proceed if there are applied changes to unapply
if self.state.current_hunk == 0 {
let hunk = &self.diff.hunks[0];
let has_applied = hunk
.change_ids
.iter()
.any(|id| self.state.applied_changes.contains(id));
if !has_applied {
// No movement, restore preview mode
self.state.hunk_preview_mode = was_in_preview;
return false;
}
}
self.state.step_direction = StepDirection::Backward;
// If we have applied changes in current hunk, unapply them
let current_hunk_idx = self.state.current_hunk;
let current_hunk = &self.diff.hunks[current_hunk_idx];
let mut moved = false;
// Unapply changes from current hunk that are applied
for &change_id in current_hunk.change_ids.iter().rev() {
if let Some(pos) = self
.state
.applied_changes
.iter()
.position(|&id| id == change_id)
{
self.state.applied_changes.remove(pos);
self.state.current_step = self.state.current_step.saturating_sub(1);
moved = true;
}
}
// Set animating hunk for whole-hunk animation (keep pointing at the hunk
// being removed so is_change_in_animating_hunk returns true during fade)
self.state.animating_hunk = Some(current_hunk_idx);
self.state.active_change = current_hunk.change_ids.first().copied();
// Move to previous hunk if current is now empty of applied changes
// (current_hunk tracks cursor position, animating_hunk tracks animation)
if moved {
// Check if we should move to previous hunk
let still_has_applied = current_hunk
.change_ids
.iter()
.any(|id| self.state.applied_changes.contains(id));
if !still_has_applied && self.state.current_hunk > 0 {
self.state.current_hunk -= 1;
}
} else if self.state.current_hunk > 0 {
// Nothing in current hunk was applied, try previous
self.state.current_hunk -= 1;
return self.prev_hunk();
}
// Don't overwrite animating_hunk here - let animation complete first.
// current_hunk already tracks cursor position for status display.
// Animation state cleared by CLI after animation completes
// Enter preview mode when we land in a previous hunk
if moved {
let entered_prev_hunk = self.state.current_hunk != current_hunk_idx;
if entered_prev_hunk {
self.state.hunk_preview_mode = true;
self.state.preview_from_backward = true;
self.state.last_nav_was_hunk = true;
} else {
// Set or clear extent markers based on whether we landed at step 0
self.state.last_nav_was_hunk = !self.state.is_at_start();
}
}
moved
}
/// Go to a specific hunk (0-indexed)
/// Applies all changes through target hunk (full preview mode).
/// Cursor lands at top of target hunk.
pub fn goto_hunk(&mut self, hunk_idx: usize) {
if hunk_idx >= self.diff.hunks.len() {
return;
}
// Reset to start
self.goto_start();
// Apply all changes for hunks before target
for idx in 0..hunk_idx {
let hunk = &self.diff.hunks[idx];
for &change_id in &hunk.change_ids {
self.state.applied_changes.push(change_id);
self.state.current_step += 1;
}
}
// Apply ALL changes of target hunk (full preview)
let hunk = &self.diff.hunks[hunk_idx];
for &change_id in &hunk.change_ids {
self.state.applied_changes.push(change_id);
self.state.current_step += 1;
}
self.state.current_hunk = hunk_idx;
self.state.animating_hunk = Some(hunk_idx);
self.state.active_change = hunk.change_ids.first().copied();
self.state.step_direction = StepDirection::Forward;
self.state.last_nav_was_hunk = true;
self.state.hunk_preview_mode = true;
self.state.preview_from_backward = false;
}
/// Jump to first change of current hunk, unapplying all but first
/// Returns true if moved, false if not inside a hunk or already at start
pub fn goto_hunk_start(&mut self) -> bool {
if self.diff.hunks.is_empty() {
return false;
}
let hunk = &self.diff.hunks[self.state.current_hunk];
let first_change = match hunk.change_ids.first() {
Some(&id) => id,
None => return false,
};
// Must have at least first change applied to be "inside" hunk
if !self.state.applied_changes.contains(&first_change) {
return false;
}
// Unapply all changes in this hunk except the first
let mut unapplied_any = false;
for &change_id in &hunk.change_ids[1..] {
if let Some(pos) = self
.state
.applied_changes
.iter()
.position(|&id| id == change_id)
{
self.state.applied_changes.remove(pos);
self.state.current_step = self.state.current_step.saturating_sub(1);
unapplied_any = true;
}
}
// No-op if already at start (nothing unapplied and cursor on first)
if !unapplied_any && self.state.active_change == Some(first_change) {
return false;
}
self.state.active_change = Some(first_change);
self.state.hunk_preview_mode = false;
self.state.preview_from_backward = false;
self.state.last_nav_was_hunk = true;
true
}
/// Jump to last change of current hunk, applying all changes in hunk
/// Returns true if moved, false if not inside a hunk or already at end
pub fn goto_hunk_end(&mut self) -> bool {
if self.diff.hunks.is_empty() {
return false;
}
let hunk = &self.diff.hunks[self.state.current_hunk];
let has_applied = hunk
.change_ids
.iter()
.any(|id| self.state.applied_changes.contains(id));
if !has_applied {
return false;
}
let last_change = hunk.change_ids.last().copied();
// Apply all unapplied changes in this hunk
for &change_id in &hunk.change_ids {
if !self.state.applied_changes.contains(&change_id) {
self.state.applied_changes.push(change_id);
self.state.current_step += 1;
}
}
// No-op if already at end (cursor on last)
if self.state.active_change == last_change {
return false;
}
self.state.active_change = last_change;
self.state.hunk_preview_mode = false;
self.state.preview_from_backward = false;
self.state.last_nav_was_hunk = true;
true
}
/// Update current hunk based on applied changes
pub fn update_current_hunk(&mut self) {
if self.diff.hunks.is_empty() {
return;
}
// Find which hunk contains the most recently applied change
if let Some(&last_applied) = self.state.applied_changes.last() {
for (idx, hunk) in self.diff.hunks.iter().enumerate() {
if hunk.change_ids.contains(&last_applied) {
self.state.current_hunk = idx;
return;
}
}
}
// If no changes applied, we're at hunk 0
self.state.current_hunk = 0;
}
/// Get the current hunk
pub fn current_hunk(&self) -> Option<&crate::diff::Hunk> {
self.diff.hunks.get(self.state.current_hunk)
}
/// Get all hunks
pub fn hunks(&self) -> &[crate::diff::Hunk] {
&self.diff.hunks
}
pub fn set_show_hunk_extent_while_stepping(&mut self, enabled: bool) {
self.state.show_hunk_extent_while_stepping = enabled;
}
// ==================== End Hunk Navigation ====================
/// Check if a change belongs to the hunk currently being animated
fn is_change_in_animating_hunk(&self, change_id: usize) -> bool {
if let Some(hunk_idx) = self.state.animating_hunk {
if let Some(hunk) = self.diff.hunks.get(hunk_idx) {
return hunk.change_ids.contains(&change_id);
}
}
false
}
/// Check if a change belongs to the current hunk (for persistent extent markers)
fn is_change_in_current_hunk(&self, change_id: usize) -> bool {
self.diff
.hunks
.get(self.state.current_hunk)
.map(|hunk| hunk.change_ids.contains(&change_id))
.unwrap_or(false)
}
/// Get the currently active change
pub fn active_change(&self) -> Option<&Change> {
self.state
.active_change
.and_then(|id| self.diff.changes.iter().find(|c| c.id == id))
}
/// Get all changes with their application status
pub fn changes_with_status(&self) -> Vec<(&Change, bool, bool)> {
self.diff
.changes
.iter()
.filter(|c| c.has_changes())
.map(|c| {
let applied = self.state.applied_changes.contains(&c.id);
let active = self.state.active_change == Some(c.id);
(c, applied, active)
})
.collect()
}
/// Reconstruct the content at the current step
/// Returns lines with their change status (uses Idle frame for backwards compatibility)
pub fn current_view(&self) -> Vec<ViewLine> {
self.current_view_with_frame(AnimationFrame::Idle)
}
/// Phase-aware view for word-level animation
/// CLI should pass its current animation phase for proper fade animations
pub fn current_view_with_frame(&self, frame: AnimationFrame) -> Vec<ViewLine> {
let mut lines = Vec::new();
// Primary cursor destination: last applied change on backward, active_change on forward
// Fallback to active_change at step 0 so cursor stays on fading line
let primary_change_id = if self.state.cursor_change.is_some()
&& self.state.active_change.is_none()
&& self.state.step_direction == StepDirection::None
{
self.state.cursor_change
} else if self.state.step_direction == StepDirection::Backward {
self.state
.applied_changes
.last()
.copied()
.or(self.state.active_change)
} else {
self.state.active_change
};
// Track if we've assigned a primary active line (for fallback when primary_change_id is None)
let mut primary_assigned = false;
for change in &self.diff.changes {
let is_applied = self.state.applied_changes.contains(&change.id);
// Primary active: cursor destination (decoupled from animation target on backward)
let is_primary_active = primary_change_id == Some(change.id);
// Active: part of the animating hunk (for animation styling)
let is_in_hunk = self.is_change_in_animating_hunk(change.id);
let is_active_change = self.state.active_change == Some(change.id);
// Active if: (1) the active_change, or (2) in animating hunk (lights up whole hunk during animation)
let is_active = is_active_change || is_in_hunk;
// Show extent marker if animating hunk OR (last nav was hunk AND change in current hunk)
let show_hunk_extent = is_in_hunk
|| (self.is_change_in_current_hunk(change.id)
&& (self.state.last_nav_was_hunk
|| self.state.show_hunk_extent_while_stepping));
// Fallback: if primary_change_id is None but we're in an animating hunk,
// first active line becomes primary
let is_primary_active = is_primary_active
|| (!primary_assigned && is_in_hunk && primary_change_id.is_none());
if is_primary_active {
primary_assigned = true;
}
// Check if this is a word-level diff (multiple spans in one change that represents a line)
let is_word_level = change.spans.len() > 1;
if is_word_level {
// Combine all spans into a single line
let line = self.build_word_level_line(
change,
is_applied,
is_active,
is_active_change,
is_primary_active,
show_hunk_extent,
frame,
);
if let Some(l) = line {
lines.push(l);
}
} else {
// Single span - handle as before
if let Some(span) = change.spans.first() {
if let Some(line) = self.build_single_span_line(
span,
change.id,
is_applied,
is_active,
is_active_change,
is_primary_active,
show_hunk_extent,
frame,
) {
lines.push(line);
}
}
}
}
lines
}
/// Compute whether to show new content based on animation frame and direction.
/// Used by both word-level and single-span line builders for consistent animation.
fn compute_show_new(&self, is_applied: bool, frame: AnimationFrame) -> bool {
// Guard: if direction is None, fall back to final state
if self.state.step_direction == StepDirection::None {
return is_applied;
}
match frame {
AnimationFrame::Idle => is_applied,
// Forward + FadeOut = show old (false), Backward + FadeOut = show new (true)
AnimationFrame::FadeOut => self.state.step_direction == StepDirection::Backward,
// Forward + FadeIn = show new (true), Backward + FadeIn = show old (false)
AnimationFrame::FadeIn => self.state.step_direction != StepDirection::Backward,
}
}
#[allow(clippy::too_many_arguments)]
fn build_word_level_line(
&self,
change: &Change,
is_applied: bool,
is_active: bool,
is_active_change: bool,
is_primary_active: bool,
show_hunk_extent: bool,
frame: AnimationFrame,
) -> Option<ViewLine> {
let first_span = change.spans.first()?;
let old_line = first_span.old_line;
let new_line = first_span.new_line;
// Pre-scan to classify: does this change have old content, new content, or both?
// Replace counts as both since it has old text and new_text.
let has_old = change
.spans
.iter()
.any(|s| matches!(s.kind, ChangeKind::Delete | ChangeKind::Replace));
let has_new = change
.spans
.iter()
.any(|s| matches!(s.kind, ChangeKind::Insert | ChangeKind::Replace));
// Build spans for the view line
let mut view_spans = Vec::new();
let mut content = String::new();
for span in &change.spans {
// Phase-aware content and styling for active changes
let (span_kind, text) = if is_active {
// Determine show_new based on frame and change type:
// - Idle: always snap to real applied state (no "phantom" content)
// - FadeOut/FadeIn:
// - Mixed (has_old && has_new): phase-swap old/new
// - Insert-only: always show new (visible both phases)
// - Delete-only: always show old (visible both phases)
let show_new = match frame {
AnimationFrame::Idle => self.compute_show_new(is_applied, frame),
_ => {
if has_old && has_new {
self.compute_show_new(is_applied, frame)
} else if has_new {
true // Insert-only: visible during animation
} else {
false // Delete-only: visible during animation
}
}
};
match span.kind {
ChangeKind::Equal => (ViewSpanKind::Equal, span.text.clone()),
ChangeKind::Delete => {
if show_new {
continue; // Hide deletions when showing new state
} else {
(ViewSpanKind::PendingDelete, span.text.clone())
}
}
ChangeKind::Insert => {
if show_new {
(ViewSpanKind::PendingInsert, span.text.clone())
} else {
continue; // Hide insertions when showing old state
}
}
ChangeKind::Replace => {
if show_new {
(
ViewSpanKind::PendingInsert,
span.new_text.clone().unwrap_or_else(|| span.text.clone()),
)
} else {
(ViewSpanKind::PendingDelete, span.text.clone())
}
}
}
} else if is_applied {
// Applied but not active - show final state
let kind = match span.kind {
ChangeKind::Equal => ViewSpanKind::Equal,
ChangeKind::Delete => ViewSpanKind::Deleted,
ChangeKind::Insert => ViewSpanKind::Inserted,
ChangeKind::Replace => ViewSpanKind::Inserted,
};
let text = match span.kind {
ChangeKind::Delete => {
continue; // Don't include deleted text in the final content
}
ChangeKind::Replace => {
span.new_text.clone().unwrap_or_else(|| span.text.clone())
}
_ => span.text.clone(),
};
(kind, text)
} else {
// Not applied, not active - show original state
match span.kind {
ChangeKind::Insert => {
continue; // Don't show pending inserts
}
_ => (ViewSpanKind::Equal, span.text.clone()),
}
};
content.push_str(&text);
view_spans.push(ViewSpan {
text,
kind: span_kind,
});
}
// Defensive guard: don't emit blank lines if all spans were filtered
if view_spans.is_empty() {
return None;
}
// Line kind - keep PendingModify for active word-level lines
// (evolution view filters on LineKind::PendingDelete, so this prevents drops)
let line_kind = if is_active {
LineKind::PendingModify
} else if is_applied {
LineKind::Modified
} else {
LineKind::Context
};
// Populate hunk metadata
let hunk_index = self.change_to_hunk.get(&change.id).copied();
let has_changes = change.has_changes();
Some(ViewLine {
content,
spans: view_spans,
kind: line_kind,
old_line,
new_line,
is_active,
is_active_change,
is_primary_active,
show_hunk_extent,
change_id: change.id,
hunk_index,
has_changes,
})
}
#[allow(clippy::too_many_arguments)]
fn build_single_span_line(
&self,
span: &ChangeSpan,
change_id: usize,
is_applied: bool,
is_active: bool,
is_active_change: bool,
is_primary_active: bool,
show_hunk_extent: bool,
frame: AnimationFrame,
) -> Option<ViewLine> {
let view_span_kind;
let line_kind;
let content;
match span.kind {
ChangeKind::Equal => {
view_span_kind = ViewSpanKind::Equal;
line_kind = LineKind::Context;
content = span.text.clone();
}
ChangeKind::Delete => {
// IMPORTANT: Check is_active FIRST so deletions animate before disappearing
if is_active {
view_span_kind = ViewSpanKind::PendingDelete;
line_kind = LineKind::PendingDelete;
content = span.text.clone();
} else if is_applied {
view_span_kind = ViewSpanKind::Deleted;
line_kind = LineKind::Deleted;
content = span.text.clone();
} else {
view_span_kind = ViewSpanKind::Equal;
line_kind = LineKind::Context;
content = span.text.clone();
}
}
ChangeKind::Insert => {
// Check is_active first for animation
if is_active {
view_span_kind = ViewSpanKind::PendingInsert;
line_kind = LineKind::PendingInsert;
content = span.text.clone();
} else if is_applied {
view_span_kind = ViewSpanKind::Inserted;
line_kind = LineKind::Inserted;
content = span.text.clone();
} else {
return None; // Don't show unapplied inserts
}
}
ChangeKind::Replace => {
// Phase-aware Replace: show old during FadeOut, new during FadeIn
if is_active {
let show_new = self.compute_show_new(is_applied, frame);
if show_new {
view_span_kind = ViewSpanKind::PendingInsert;
content = span.new_text.clone().unwrap_or_else(|| span.text.clone());
} else {
view_span_kind = ViewSpanKind::PendingDelete;
content = span.text.clone();
}
line_kind = LineKind::PendingModify;
} else if is_applied {
view_span_kind = ViewSpanKind::Inserted;
line_kind = LineKind::Modified;
content = span.new_text.clone().unwrap_or_else(|| span.text.clone());
} else {
view_span_kind = ViewSpanKind::Equal;
line_kind = LineKind::Context;
content = span.text.clone();
}
}
}
// Populate hunk metadata
let hunk_index = self.change_to_hunk.get(&change_id).copied();
let has_changes = !matches!(span.kind, ChangeKind::Equal);
Some(ViewLine {
content: content.clone(),
spans: vec![ViewSpan {
text: content,
kind: view_span_kind,
}],
kind: line_kind,
old_line: span.old_line,
new_line: span.new_line,
is_active,
is_active_change,
is_primary_active,
show_hunk_extent,
change_id,
hunk_index,
has_changes,
})
}
/// Get old content
pub fn old_content(&self) -> &str {
&self.old_content
}
/// Get new content
pub fn new_content(&self) -> &str {
&self.new_content
}
}
/// A styled span within a view line
#[derive(Debug, Clone)]
pub struct ViewSpan {
pub text: String,
pub kind: ViewSpanKind,
}
/// The kind of span styling
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ViewSpanKind {
Equal,
Inserted,
Deleted,
PendingInsert,
PendingDelete,
}
/// A line in the current view with its status
#[derive(Debug, Clone)]
pub struct ViewLine {
/// Full content of the line
pub content: String,
/// Individual styled spans (for word-level highlighting)
pub spans: Vec<ViewSpan>,
/// Overall line kind
pub kind: LineKind,
pub old_line: Option<usize>,
pub new_line: Option<usize>,
/// Part of the active hunk (for animation styling)
pub is_active: bool,
/// The active change itself (not just part of a hunk preview)
pub is_active_change: bool,
/// The primary focus line within the hunk (for gutter marker)
pub is_primary_active: bool,
/// Show extent marker (true only during hunk navigation)
pub show_hunk_extent: bool,
/// ID of the change this line belongs to
pub change_id: usize,
/// Index of the hunk this line belongs to
pub hunk_index: Option<usize>,
/// True if the underlying change contains any non-equal spans
pub has_changes: bool,
}
/// The kind of line in the view
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum LineKind {
/// Unchanged context line
Context,
/// Line was inserted
Inserted,
/// Line was deleted
Deleted,
/// Line was modified
Modified,
/// Line is about to be deleted (active animation)
PendingDelete,
/// Line is about to be inserted (active animation)
PendingInsert,
/// Line is about to be modified (active animation)
PendingModify,
}
#[cfg(test)]
mod tests {
use super::*;
use crate::diff::DiffEngine;
#[test]
fn test_navigation() {
let old = "foo\nbar\nbaz";
let new = "foo\nqux\nbaz";
let engine = DiffEngine::new();
let diff = engine.diff_strings(old, new);
let mut nav = DiffNavigator::new(diff, old.to_string(), new.to_string());
assert!(nav.state().is_at_start());
assert!(!nav.state().is_at_end());
nav.next();
assert!(!nav.state().is_at_start());
nav.goto_end();
assert!(nav.state().is_at_end());
nav.prev();
assert!(!nav.state().is_at_end());
nav.goto_start();
assert!(nav.state().is_at_start());
}
#[test]
fn test_progress() {
let old = "a\nb\nc\nd";
let new = "a\nB\nC\nd";
let engine = DiffEngine::new();
let diff = engine.diff_strings(old, new);
let mut nav = DiffNavigator::new(diff, old.to_string(), new.to_string());
assert_eq!(nav.state().progress(), 0.0);
nav.goto_end();
assert_eq!(nav.state().progress(), 100.0);
}
#[test]
fn test_word_level_view() {
let old = "const foo = 4";
let new = "const bar = 5";
let engine = DiffEngine::new().with_word_level(true);
let diff = engine.diff_strings(old, new);
let mut nav = DiffNavigator::new(diff, old.to_string(), new.to_string());
// At start, should show original line
let view = nav.current_view();
assert_eq!(view.len(), 1);
assert_eq!(view[0].content, "const foo = 4");
// After applying change, should show new line
nav.next();
let view = nav.current_view();
assert_eq!(view.len(), 1);
assert_eq!(view[0].content, "const bar = 5");
}
#[test]
fn test_prev_hunk_animation_state() {
// Setup: file with 2 hunks (changes separated by >3 unchanged lines)
// Hunk proximity threshold is 3, so we need at least 4 unchanged lines between changes
let old = "a\nb\nc\nd\ne\nf\ng\nh\ni\nj\nk\nl";
let new = "a\nB\nc\nd\ne\nf\ng\nh\ni\nj\nK\nl";
// ^ hunk 0 (line 2) ^ hunk 1 (line 11)
let engine = DiffEngine::new();
let diff = engine.diff_strings(old, new);
// Verify we have 2 hunks
assert!(
diff.hunks.len() >= 2,
"Expected at least 2 hunks, got {}. Adjust fixture gap.",
diff.hunks.len()
);
let mut nav = DiffNavigator::new(diff, old.to_string(), new.to_string());
// Apply both hunks
nav.next_hunk();
nav.next_hunk();
assert_eq!(nav.state().current_hunk, 1);
// Step back one hunk
nav.prev_hunk();
// animating_hunk should point to hunk 1 (the one being removed)
assert_eq!(
nav.state().animating_hunk,
Some(1),
"animating_hunk should stay on the hunk being removed for fade animation"
);
// current_hunk should have moved to 0 (cursor position)
assert_eq!(
nav.state().current_hunk,
0,
"current_hunk should move to destination for status display"
);
// View should mark hunk 1 changes as active
let view = nav.current_view();
let active_lines: Vec<_> = view.iter().filter(|l| l.is_active).collect();
assert!(
!active_lines.is_empty(),
"Hunk changes should be marked active during fade"
);
// After clearing, animating_hunk should be None
nav.clear_active_change();
assert_eq!(
nav.state().animating_hunk,
None,
"animating_hunk should be cleared after animation completes"
);
}
#[test]
fn test_prev_to_start_preserves_animation_state() {
// Single hunk - stepping back lands on step 0
// Animation state persists for fade-out, cleared by CLI after animation completes
let old = "a\nb\nc";
let new = "a\nB\nc";
let engine = DiffEngine::new();
let diff = engine.diff_strings(old, new);
let mut nav = DiffNavigator::new(diff, old.to_string(), new.to_string());
// Apply first change (no hunk preview)
nav.next();
assert_eq!(nav.state().current_step, 1);
// Step back to start
nav.prev();
assert!(nav.state().is_at_start());
// Animation state preserved for fade-out rendering
assert!(
nav.state().active_change.is_some(),
"active_change preserved for fade-out"
);
assert_eq!(
nav.state().step_direction,
StepDirection::Backward,
"step_direction should be Backward"
);
// CLI calls clear_active_change() after animation completes
nav.clear_active_change();
assert_eq!(nav.state().active_change, None);
assert_eq!(nav.state().animating_hunk, None);
assert_eq!(nav.state().step_direction, StepDirection::None);
}
#[test]
fn test_prev_hunk_from_hunk_0_unapplies_changes() {
// prev_hunk should unapply hunk 0 when it has applied changes
let old = "a\nb\nc";
let new = "a\nB\nc";
let engine = DiffEngine::new();
let diff = engine.diff_strings(old, new);
let mut nav = DiffNavigator::new(diff, old.to_string(), new.to_string());
// Apply first hunk
nav.next_hunk();
assert_eq!(nav.state().current_step, 1);
assert_eq!(nav.state().current_hunk, 0);
// prev_hunk from hunk 0 should work (unapply the hunk)
let moved = nav.prev_hunk();
assert!(
moved,
"prev_hunk should succeed when hunk 0 has applied changes"
);
assert!(nav.state().is_at_start());
assert_eq!(nav.state().current_step, 0);
// animating_hunk should be set for extent markers
assert_eq!(
nav.state().animating_hunk,
Some(0),
"animating_hunk should point to hunk 0 for fade animation"
);
assert_eq!(nav.state().step_direction, StepDirection::Backward);
// Calling prev_hunk again should return false (nothing to unapply)
let moved_again = nav.prev_hunk();
assert!(
!moved_again,
"prev_hunk should fail when hunk 0 has no applied changes"
);
}
#[test]
fn test_backward_primary_marker_on_destination() {
// Two hunks with >3 lines separation to ensure distinct hunks
// Stepping back from hunk 1 should put primary on hunk 0's change (destination)
let old = "line1\nline2\nline3\nline4\nline5\nline6\nline7\nline8";
let new = "line1\nLINE2\nline3\nline4\nline5\nline6\nLINE7\nline8";
let engine = DiffEngine::new();
let diff = engine.diff_strings(old, new);
assert!(diff.hunks.len() >= 2, "Fixture must produce 2 hunks");
let mut nav = DiffNavigator::new(diff, old.to_string(), new.to_string());
// Apply both hunks
nav.next_hunk(); // hunk 0 (LINE2)
nav.next_hunk(); // hunk 1 (LINE7)
assert_eq!(nav.state().current_step, 2);
// Step back from hunk 1
nav.prev_hunk();
let view = nav.current_view_with_frame(AnimationFrame::FadeOut);
// Find the lines
let primary_lines: Vec<_> = view.iter().filter(|l| l.is_primary_active).collect();
let active_lines: Vec<_> = view.iter().filter(|l| l.is_active).collect();
// Exactly one primary line
assert_eq!(primary_lines.len(), 1, "exactly one primary line");
// Fading hunk should have is_active lines
assert!(active_lines.len() >= 1, "fading line should be active");
// Primary is on destination (hunk 0 = LINE2), not fading line (hunk 1 = LINE7)
let primary = primary_lines[0];
assert!(
primary.content.contains("LINE2"),
"primary marker should be on destination (hunk 0)"
);
assert!(
!primary.content.contains("LINE7"),
"primary marker should not be on fading line (hunk 1)"
);
}
#[test]
fn test_forward_primary_marker_on_first_change() {
// Multi-change hunk: next_hunk should put cursor on first change (top of hunk)
// Hunk 0 has 2 consecutive changes so cursor position matters
let old = "line1\nline2\nline3\nline4\nline5\nline6\nline7\nline8";
let new = "line1\nLINE2\nLINE3\nline4\nline5\nline6\nline7\nline8";
// ^ first change ^ second change (same hunk due to proximity)
let engine = DiffEngine::new();
let diff = engine.diff_strings(old, new);
assert!(
!diff.hunks.is_empty(),
"Fixture must produce at least 1 hunk"
);
assert!(
diff.hunks[0].change_ids.len() >= 2,
"Hunk 0 must have at least 2 changes for this test"
);
let mut nav = DiffNavigator::new(diff, old.to_string(), new.to_string());
// Apply hunk 0
nav.next_hunk();
// Use FadeIn to see new content (LINE2/LINE3)
let view = nav.current_view_with_frame(AnimationFrame::FadeIn);
let primary_lines: Vec<_> = view.iter().filter(|l| l.is_primary_active).collect();
assert_eq!(primary_lines.len(), 1, "exactly one primary line");
// Primary should be on LINE2 (first change), not LINE3 (last change)
let primary = primary_lines[0];
assert!(
primary.content.contains("LINE2"),
"primary marker should be on first change (LINE2), got: {}",
primary.content
);
}
#[test]
fn test_step_down_after_next_hunk() {
// After next_hunk lands at first change, step down should move to second change
let old = "line1\nline2\nline3\nline4\nline5\nline6\nline7\nline8";
let new = "line1\nLINE2\nLINE3\nline4\nline5\nline6\nline7\nline8";
let engine = DiffEngine::new();
let diff = engine.diff_strings(old, new);
assert!(
diff.hunks[0].change_ids.len() >= 2,
"Hunk 0 must have at least 2 changes"
);
let mut nav = DiffNavigator::new(diff, old.to_string(), new.to_string());
// next_hunk applies ALL changes (full preview), cursor at first
nav.next_hunk();
assert_eq!(
nav.state().current_step,
2,
"Should apply all 2 changes after next_hunk"
);
assert!(nav.state().hunk_preview_mode, "Should be in preview mode");
// Step down dissolves preview: keeps first, applies second, cursor on second
let moved = nav.next();
assert!(moved, "next() should succeed");
assert_eq!(
nav.state().current_step,
2,
"Should still be at step 2 after dissolve"
);
assert!(!nav.state().hunk_preview_mode, "Should exit preview mode");
// Verify cursor is now on LINE3 (second change)
let view = nav.current_view_with_frame(AnimationFrame::FadeIn);
let primary = view.iter().find(|l| l.is_primary_active);
assert!(primary.is_some(), "Should have primary line");
assert!(
primary.unwrap().content.contains("LINE3"),
"Primary should be on LINE3 after stepping"
);
}
#[test]
fn test_next_hunk_completes_current_then_lands_on_next() {
// Two hunks separated by >3 lines. next_hunk on hunk 0 applies all,
// then next_hunk moves to hunk 1 with full preview.
let old = "line1\nline2\nline3\nline4\nline5\nline6\nline7\nline8";
let new = "line1\nLINE2\nLINE3\nline4\nline5\nline6\nLINE7\nLINE8";
// hunk 0: LINE2, LINE3 hunk 1: LINE7, LINE8
let engine = DiffEngine::new();
let diff = engine.diff_strings(old, new);
assert!(diff.hunks.len() >= 2, "Must have 2 hunks");
let mut nav = DiffNavigator::new(diff, old.to_string(), new.to_string());
// First next_hunk: apply all changes in hunk 0 (full preview)
nav.next_hunk();
assert_eq!(nav.state().current_hunk, 0);
assert_eq!(
nav.state().current_step,
2,
"Should apply all 2 changes in hunk 0"
);
assert!(nav.state().hunk_preview_mode);
// Second next_hunk: move to hunk 1 with full preview
nav.next_hunk();
assert_eq!(nav.state().current_hunk, 1, "Should be in hunk 1");
// All of hunk 0 (2 changes) + all of hunk 1 (2 changes) = 4 total
assert_eq!(nav.state().current_step, 4, "Should have applied 4 changes");
assert!(nav.state().hunk_preview_mode);
// Cursor should be on LINE7 (first change of hunk 1)
let view = nav.current_view_with_frame(AnimationFrame::FadeIn);
let primary = view.iter().find(|l| l.is_primary_active);
assert!(primary.is_some());
assert!(
primary.unwrap().content.contains("LINE7"),
"Cursor should be on LINE7"
);
}
#[test]
fn test_next_hunk_on_last_hunk_stays_at_end() {
// Single hunk with 2 changes. Calling next_hunk applies all changes.
// Calling next_hunk again returns false (no next hunk).
let old = "line1\nline2\nline3";
let new = "line1\nLINE2\nLINE3";
let engine = DiffEngine::new();
let diff = engine.diff_strings(old, new);
assert_eq!(diff.hunks.len(), 1, "Must have exactly 1 hunk");
assert_eq!(
diff.hunks[0].change_ids.len(),
2,
"Hunk must have 2 changes"
);
let mut nav = DiffNavigator::new(diff, old.to_string(), new.to_string());
// First next_hunk: apply all changes (full preview)
let moved1 = nav.next_hunk();
assert!(moved1);
assert_eq!(nav.state().current_step, 2, "Should apply all 2 changes");
assert!(nav.state().hunk_preview_mode);
// Second next_hunk: no next hunk, returns false
let moved2 = nav.next_hunk();
assert!(!moved2, "Should return false when no next hunk");
assert_eq!(nav.state().current_step, 2, "Should still be at step 2");
}
#[test]
fn test_markers_persist_within_hunk() {
// Stepping within a hunk after next_hunk should preserve extent markers
let old = "line1\nline2\nline3\nline4\nline5\nline6\nline7\nline8";
let new = "line1\nLINE2\nLINE3\nline4\nline5\nline6\nline7\nline8";
let engine = DiffEngine::new();
let diff = engine.diff_strings(old, new);
let mut nav = DiffNavigator::new(diff, old.to_string(), new.to_string());
nav.next_hunk();
assert!(
nav.state().last_nav_was_hunk,
"last_nav_was_hunk should be true after next_hunk"
);
// Step within hunk (still in hunk 0)
nav.next();
assert!(
nav.state().last_nav_was_hunk,
"last_nav_was_hunk should persist within hunk"
);
}
#[test]
fn test_markers_clear_on_hunk_exit() {
// Stepping into a different hunk should clear extent markers
let old = "line1\nline2\nline3\nline4\nline5\nline6\nline7\nline8";
let new = "line1\nLINE2\nline3\nline4\nline5\nline6\nLINE7\nline8";
// hunk 0: LINE2 hunk 1: LINE7
let engine = DiffEngine::new();
let diff = engine.diff_strings(old, new);
assert!(diff.hunks.len() >= 2, "Must have 2 hunks");
let mut nav = DiffNavigator::new(diff, old.to_string(), new.to_string());
// Apply hunk 0 via next_hunk
nav.next_hunk();
assert!(nav.state().last_nav_was_hunk);
// Step into hunk 1 via next() - should clear markers
nav.next();
assert!(
!nav.state().last_nav_was_hunk,
"Markers should clear when stepping into different hunk"
);
}
#[test]
fn test_active_change_flag_in_hunk_preview() {
// Single hunk with 2 changes: hunk preview should mark all lines active,
// but only the first change is the active change.
let old = "line1\nline2\nline3\n";
let new = "LINE1\nLINE2\nline3\n";
let engine = DiffEngine::new();
let diff = engine.diff_strings(old, new);
assert_eq!(diff.hunks.len(), 1, "Expected a single hunk");
assert!(
diff.hunks[0].change_ids.len() >= 2,
"Expected 2 changes in the hunk"
);
let mut nav = DiffNavigator::new(diff, old.to_string(), new.to_string());
nav.next_hunk();
let view = nav.current_view_with_frame(AnimationFrame::FadeIn);
let active_changes: Vec<_> = view.iter().filter(|l| l.is_active_change).collect();
let active_lines: Vec<_> = view.iter().filter(|l| l.is_active).collect();
assert_eq!(active_changes.len(), 1, "Only one active change expected");
assert!(
active_lines.len() >= 2,
"All hunk lines should be active during preview"
);
assert!(
active_changes[0].is_primary_active,
"Active change should be primary"
);
assert!(
active_changes[0].content.contains("LINE1"),
"Active change should be the first change in the hunk"
);
}
#[test]
fn test_word_level_phase_aware_mixed_change() {
// Mixed change: has both old (foo, 4) and new (bar, 5) content
// Should swap old/new at phase boundary
let old = "const foo = 4";
let new = "const bar = 5";
let engine = DiffEngine::new().with_word_level(true);
let diff = engine.diff_strings(old, new);
let mut nav = DiffNavigator::new(diff, old.to_string(), new.to_string());
// Apply the change (makes it active)
nav.next();
assert!(nav.state().active_change.is_some());
// FadeOut (forward): should show OLD content
let view = nav.current_view_with_frame(AnimationFrame::FadeOut);
assert_eq!(view.len(), 1);
assert_eq!(
view[0].content, "const foo = 4",
"FadeOut should show old content for mixed word-level change"
);
// FadeIn (forward): should show NEW content
let view = nav.current_view_with_frame(AnimationFrame::FadeIn);
assert_eq!(view.len(), 1);
assert_eq!(
view[0].content, "const bar = 5",
"FadeIn should show new content for mixed word-level change"
);
// Idle: should show applied (new) content
let view = nav.current_view_with_frame(AnimationFrame::Idle);
assert_eq!(view.len(), 1);
assert_eq!(
view[0].content, "const bar = 5",
"Idle should show applied (new) content"
);
}
#[test]
fn test_word_level_insert_only_visible_both_phases() {
// Insert-only change: should be visible across both FadeOut and FadeIn
let old = "hello";
let new = "hello world";
let engine = DiffEngine::new().with_word_level(true);
let diff = engine.diff_strings(old, new);
let mut nav = DiffNavigator::new(diff, old.to_string(), new.to_string());
// Apply the change
nav.next();
// FadeOut: insert-only should still be visible (not hidden)
let view = nav.current_view_with_frame(AnimationFrame::FadeOut);
assert_eq!(view.len(), 1);
assert!(
view[0].content.contains("world"),
"Insert-only change should be visible during FadeOut, got: {}",
view[0].content
);
// FadeIn: should also be visible
let view = nav.current_view_with_frame(AnimationFrame::FadeIn);
assert_eq!(view.len(), 1);
assert!(
view[0].content.contains("world"),
"Insert-only change should be visible during FadeIn, got: {}",
view[0].content
);
}
#[test]
fn test_word_level_delete_only_visible_both_phases() {
// Delete-only change: should be visible across both FadeOut and FadeIn
let old = "hello world";
let new = "hello";
let engine = DiffEngine::new().with_word_level(true);
let diff = engine.diff_strings(old, new);
let mut nav = DiffNavigator::new(diff, old.to_string(), new.to_string());
// Apply the change
nav.next();
// FadeOut: delete-only should still be visible (showing old content being deleted)
let view = nav.current_view_with_frame(AnimationFrame::FadeOut);
assert_eq!(view.len(), 1);
assert!(
view[0].content.contains("world"),
"Delete-only change should show deleted content during FadeOut, got: {}",
view[0].content
);
// FadeIn: should also show the deletion
let view = nav.current_view_with_frame(AnimationFrame::FadeIn);
assert_eq!(view.len(), 1);
assert!(
view[0].content.contains("world"),
"Delete-only change should show deleted content during FadeIn, got: {}",
view[0].content
);
}
#[test]
fn test_word_level_insert_only_idle_respects_applied_state() {
// Insert-only change: Idle frame should snap to real applied state
// (no "phantom" inserts when animations are disabled)
let old = "hello";
let new = "hello world";
let engine = DiffEngine::new().with_word_level(true);
let diff = engine.diff_strings(old, new);
let mut nav = DiffNavigator::new(diff, old.to_string(), new.to_string());
// Apply then unapply
nav.next();
nav.prev();
// Idle should NOT contain "world" (it's unapplied)
let view = nav.current_view_with_frame(AnimationFrame::Idle);
assert_eq!(view.len(), 1);
assert!(
!view[0].content.contains("world"),
"Idle should not show unapplied insert, got: {}",
view[0].content
);
}
#[test]
fn test_word_level_phase_aware_backward_with_multiple_changes() {
// To properly test backward animation, we need multiple changes
// so stepping back doesn't land on step 0 (which clears animation state)
let old = "aaa\nconst foo = 4\nccc\nddd\neee\nfff\nggg\nconst bar = 8\niii\njjj";
let new = "aaa\nconst bbb = 5\nccc\nddd\neee\nfff\nggg\nconst qux = 9\niii\njjj";
// ^ change 1 (word-level) ^ change 2 (word-level)
let engine = DiffEngine::new().with_word_level(true);
let diff = engine.diff_strings(old, new);
// Verify we have 2 changes
assert!(
diff.significant_changes.len() >= 2,
"Expected at least 2 changes, got {}",
diff.significant_changes.len()
);
let mut nav = DiffNavigator::new(diff, old.to_string(), new.to_string());
// Apply both changes
nav.next(); // step 1: first change applied
nav.next(); // step 2: second change applied
assert_eq!(nav.state().current_step, 2);
// Step back from step 2 to step 1 (second change is now active for backward animation)
nav.prev();
assert_eq!(nav.state().current_step, 1);
assert_eq!(nav.state().step_direction, StepDirection::Backward);
assert!(nav.state().active_change.is_some());
// Find the line that's active (should be line 8, word-level change being un-applied)
let view = nav.current_view_with_frame(AnimationFrame::FadeOut);
let active_line = view.iter().find(|l| l.is_active);
assert!(active_line.is_some(), "Should have an active line");
// Backward + FadeOut: should show NEW content (the content being removed)
// For word-level, this means "const qux = 9"
let active = active_line.unwrap();
assert_eq!(
active.content, "const qux = 9",
"Backward FadeOut should show new content (being removed)"
);
// Backward + FadeIn: should show OLD content (the content being restored)
// For word-level, this means "const bar = 8"
let view = nav.current_view_with_frame(AnimationFrame::FadeIn);
let active_line = view.iter().find(|l| l.is_active);
assert!(active_line.is_some(), "Should have an active line");
let active = active_line.unwrap();
assert_eq!(
active.content, "const bar = 8",
"Backward FadeIn should show old content (being restored)"
);
}
#[test]
fn test_word_level_insert_only_backward_visible_both_phases() {
// Insert-only word-level change should stay visible during backward animation
// Test: "hello" -> "hello world" is insert-only (only adds " world")
// We need 2 changes to avoid landing on step 0
let old = "aaa\nhello\nccc\nddd\neee\nfff\nggg\nfoo\niii\njjj";
let new = "aaa\nhello world\nccc\nddd\neee\nfff\nggg\nfoo bar\niii\njjj";
// ^ insert-only (add " world") ^ insert-only (add " bar")
let engine = DiffEngine::new().with_word_level(true);
let diff = engine.diff_strings(old, new);
assert!(
diff.significant_changes.len() >= 2,
"Need 2+ changes to avoid landing on step 0, got {}",
diff.significant_changes.len()
);
let mut nav = DiffNavigator::new(diff, old.to_string(), new.to_string());
// Apply both changes, then step back
nav.next();
nav.next();
nav.prev();
assert_eq!(nav.state().step_direction, StepDirection::Backward);
assert!(nav.state().active_change.is_some());
// FadeOut: insert-only should still be visible (shows the inserted content)
let view = nav.current_view_with_frame(AnimationFrame::FadeOut);
let active_line = view.iter().find(|l| l.is_active);
assert!(
active_line.is_some(),
"Should have an active line during FadeOut"
);
let active = active_line.unwrap();
assert!(
active.content.contains("bar"),
"Backward FadeOut should show insert-only content, got: {}",
active.content
);
// FadeIn: should also show the inserted content (visible both phases)
let view = nav.current_view_with_frame(AnimationFrame::FadeIn);
let active_line = view.iter().find(|l| l.is_active);
assert!(
active_line.is_some(),
"Should have an active line during FadeIn"
);
let active = active_line.unwrap();
assert!(
active.content.contains("bar"),
"Backward FadeIn should show insert-only content, got: {}",
active.content
);
}
#[test]
fn test_word_level_active_line_kind_pending_modify() {
// Active word-level lines should have LineKind::PendingModify
// (evolution view filters on LineKind::PendingDelete, so this prevents drops)
let old = "const foo = 4";
let new = "const bar = 5";
let engine = DiffEngine::new().with_word_level(true);
let diff = engine.diff_strings(old, new);
let mut nav = DiffNavigator::new(diff, old.to_string(), new.to_string());
nav.next(); // active change
let view = nav.current_view_with_frame(AnimationFrame::FadeOut);
assert_eq!(view.len(), 1);
assert_eq!(
view[0].kind,
LineKind::PendingModify,
"Active word-level line should have PendingModify kind during FadeOut"
);
let view = nav.current_view_with_frame(AnimationFrame::FadeIn);
assert_eq!(view.len(), 1);
assert_eq!(
view[0].kind,
LineKind::PendingModify,
"Active word-level line should have PendingModify kind during FadeIn"
);
}
#[test]
fn test_primary_active_unique_when_active_change_set() {
// When active_change is set, exactly one line should be is_primary_active
// and that line must also be is_active
let old = "a\nb\nc\n";
let new = "a\nB\nc\n";
let diff = DiffEngine::new().diff_strings(old, new);
let mut nav = DiffNavigator::new(diff, old.to_string(), new.to_string());
nav.next();
let view = nav.current_view_with_frame(AnimationFrame::FadeIn);
let primary: Vec<_> = view.iter().filter(|l| l.is_primary_active).collect();
assert_eq!(
primary.len(),
1,
"Exactly one line should be primary active"
);
assert!(
primary[0].is_active,
"Primary active line must also be is_active"
);
}
#[test]
fn test_hunk_extent_not_primary() {
// Multi-line hunk: all lines should be active during animation,
// but only one should be is_primary_active (for gutter marker)
let old = "a\nb\nc\nd\n";
let new = "A\nb\nC\nd\n"; // A and C form one hunk (b is unchanged but within proximity)
let diff = DiffEngine::new().diff_strings(old, new);
assert_eq!(
diff.hunks.len(),
1,
"Fixture should produce a single hunk; adjust the unchanged gap if this fails"
);
let mut nav = DiffNavigator::new(diff, old.to_string(), new.to_string());
nav.next_hunk();
let view = nav.current_view_with_frame(AnimationFrame::FadeOut);
// During animation, whole hunk lights up as active
let active = view.iter().filter(|l| l.is_active).count();
let extent = view.iter().filter(|l| l.show_hunk_extent).count();
let primary = view.iter().filter(|l| l.is_primary_active).count();
assert!(
active > 1,
"Multiple lines should be active during animation, got {}",
active
);
assert!(
extent > 1,
"Multiple lines should show extent markers, got {}",
extent
);
assert_eq!(primary, 1, "Exactly one line should be primary active");
}
#[test]
fn test_hunk_extent_while_stepping() {
// When stepping (not hunk-nav), extent markers should still show
// if explicitly enabled by the UI.
let old = "one\ntwo\nthree\nfour\n";
let new = "ONE\nTWO\nthree\nfour\n";
let diff = DiffEngine::new().diff_strings(old, new);
let mut nav = DiffNavigator::new(diff, old.to_string(), new.to_string());
nav.next();
nav.set_show_hunk_extent_while_stepping(true);
let view = nav.current_view_with_frame(AnimationFrame::Idle);
let extent = view.iter().filter(|l| l.show_hunk_extent).count();
assert!(
extent > 0,
"Extent markers should show while stepping when enabled"
);
}
#[test]
fn test_primary_active_fallback_when_active_change_none() {
// When active_change is None but animating_hunk is set,
// the first line in the hunk should become primary and be active
let old = "a\nb\nc\n";
let new = "A\nb\nC\n"; // two changes in same hunk
let diff = DiffEngine::new().diff_strings(old, new);
let mut nav = DiffNavigator::new(diff, old.to_string(), new.to_string());
// Force animating hunk without active_change
nav.state_mut().animating_hunk = Some(0);
nav.state_mut().active_change = None;
nav.state_mut().step_direction = StepDirection::Forward;
let view = nav.current_view_with_frame(AnimationFrame::FadeOut);
let primary: Vec<_> = view.iter().filter(|l| l.is_primary_active).collect();
assert_eq!(
primary.len(),
1,
"Exactly one line should be primary active"
);
assert!(
primary[0].is_active,
"Primary active line must also be is_active"
);
// Verify it's the first active line in the view
let first_active_idx = view.iter().position(|l| l.is_active).unwrap();
let first_primary_idx = view.iter().position(|l| l.is_primary_active).unwrap();
assert_eq!(
first_active_idx, first_primary_idx,
"First active line should be the primary line"
);
}
}