mathypad 0.1.17

A smart TUI calculator that understands units and makes complex calculations simple.
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
//! Application state and core logic

use crate::Mode;
use mathypad_core::core::MathypadCore;
use mathypad_core::expression::update_line_references_in_text;
use std::path::PathBuf;
use std::time::Instant;

/// Animation state for a result line
#[derive(Clone, Debug)]
pub struct ResultAnimation {
    pub start_time: Instant,
    pub duration_ms: u64,
    pub animation_type: AnimationType,
}

#[derive(Clone, Debug)]
pub enum AnimationType {
    FadeIn,
    CopyFlash,
}

impl ResultAnimation {
    pub fn new_fade_in() -> Self {
        Self {
            start_time: Instant::now(),
            duration_ms: 250, // 250ms fade-in
            animation_type: AnimationType::FadeIn,
        }
    }

    pub fn new_copy_flash() -> Self {
        Self {
            start_time: Instant::now(),
            duration_ms: 150, // 150ms flash animation - faster and snappier
            animation_type: AnimationType::CopyFlash,
        }
    }

    /// Get the animation progress (0.0 to 1.0)
    pub fn progress(&self) -> f32 {
        let elapsed = self.start_time.elapsed().as_millis() as f32;
        let progress = elapsed / self.duration_ms as f32;
        progress.min(1.0)
    }

    /// Check if animation is complete
    pub fn is_complete(&self) -> bool {
        self.progress() >= 1.0
    }

    /// Get the opacity for fade-in animation (0.0 to 1.0)
    pub fn opacity(&self) -> f32 {
        match self.animation_type {
            AnimationType::FadeIn => {
                let progress = self.progress();
                // Smooth ease-out animation
                1.0 - (1.0 - progress).powi(3)
            }
            AnimationType::CopyFlash => {
                let progress = self.progress();
                // Flash effect: bright at start, fade to normal
                1.0 - progress
            }
        }
    }
}

/// Main application state for the mathematical notepad
pub struct App {
    /// Core calculation and text state (shared with web UI)
    pub core: MathypadCore,
    pub scroll_offset: usize,
    pub mode: Mode,
    pub result_animations: Vec<Option<ResultAnimation>>, // Animation state for each result
    pub file_path: Option<PathBuf>,                      // Path to the currently opened file
    pub has_unsaved_changes: bool,                       // Track if there are unsaved changes
    pub show_unsaved_dialog: bool,                       // Show the unsaved changes dialog
    pub show_save_as_dialog: bool,                       // Show the save as dialog
    pub save_as_input: String,                           // Current input for save as filename
    pub save_as_and_quit: bool, // Whether to quit after saving in save as dialog
    pub separator_position: u16, // Position of the separator between text and results (percentage)
    pub is_dragging_separator: bool, // Whether the user is currently dragging the separator
    pub is_hovering_separator: bool, // Whether the mouse is hovering over the separator
    pub copy_flash_animations: Vec<Option<ResultAnimation>>, // Flash animations for copied lines
    pub copy_flash_is_result: Vec<bool>, // Track which panel was flashed (true = results, false = text)
    pub last_click_time: Option<Instant>, // For double-click detection
    pub last_click_position: Option<(u16, u16)>, // Last click position for double-click detection
    pub show_welcome_dialog: bool,       // Show the welcome screen for new versions
    pub welcome_scroll_offset: usize,    // Scroll position for welcome screen changelog
    pub pending_normal_command: Option<char>, // For multi-character vim commands like 'dd'
    pub command_line: String,            // Current command line input (starts with ':')
    pub command_cursor: usize,           // Cursor position in command line
}

impl Default for App {
    fn default() -> App {
        App {
            core: MathypadCore::new(),
            scroll_offset: 0,
            mode: Mode::Insert,                // Start in insert mode
            result_animations: vec![None],     // Start with no animations
            file_path: None,                   // No file loaded initially
            has_unsaved_changes: false,        // Start with no changes
            show_unsaved_dialog: false,        // Start without showing dialog
            show_save_as_dialog: false,        // Start without showing save as dialog
            save_as_input: String::new(),      // Start with empty filename input
            save_as_and_quit: false,           // Start without quit flag
            separator_position: 80,            // Default to 80% for text, 20% for results
            is_dragging_separator: false,      // Start without dragging
            is_hovering_separator: false,      // Start without hovering
            copy_flash_animations: vec![None], // Start with no copy animations
            copy_flash_is_result: vec![false], // Start with no copy panel tracking
            last_click_time: None,             // No previous clicks
            last_click_position: None,         // No previous click position
            show_welcome_dialog: false,        // Start without showing welcome dialog
            welcome_scroll_offset: 0,          // Start at top of welcome content
            pending_normal_command: None,      // No pending vim command
            command_line: String::new(),       // Start with empty command line
            command_cursor: 0,                 // Start cursor at beginning of command line
        }
    }
}

impl App {
    #[cfg(test)]
    pub fn test_scenario_line_splitting(&mut self) -> (String, String) {
        // Set up the scenario: "5" on line 1, "line1 + 1" on line 2
        self.core.text_lines = vec!["5".to_string(), "line1 + 1".to_string()];
        self.core.results = vec![None, None];
        self.core.cursor_line = 0;
        self.core.cursor_col = 0; // Position cursor OVER the "5" (at beginning)

        // Record the original state
        let before = format!(
            "Line 1: '{}', Line 2: '{}'",
            self.core.text_lines[0], self.core.text_lines[1]
        );

        // Hit enter when cursor is over the 5 (actually after it)
        self.new_line();

        // Record the new state
        let after = format!(
            "Line 1: '{}', Line 2: '{}', Line 3: '{}'",
            self.core.text_lines.first().unwrap_or(&"".to_string()),
            self.core.text_lines.get(1).unwrap_or(&"".to_string()),
            self.core.text_lines.get(2).unwrap_or(&"".to_string())
        );

        (before, after)
    }
    /// Insert a character at the current cursor position
    pub fn insert_char(&mut self, c: char) {
        self.core.insert_char(c);
        self.has_unsaved_changes = true;
    }

    /// Delete the character before the cursor
    pub fn delete_char(&mut self) {
        if self.core.cursor_line < self.core.text_lines.len() {
            if self.core.cursor_col > 0 {
                // Delete character within the current line
                let line = &mut self.core.text_lines[self.core.cursor_line];

                // Find the byte index of the character to delete
                let char_indices: Vec<_> = line.char_indices().collect();
                if self.core.cursor_col > 0 && self.core.cursor_col <= char_indices.len() {
                    // We want to delete the character before the cursor (at cursor_col - 1)
                    let char_to_delete_idx = self.core.cursor_col - 1;

                    // Get the byte range of this character
                    let start_byte = char_indices[char_to_delete_idx].0;
                    let end_byte = if char_to_delete_idx + 1 < char_indices.len() {
                        char_indices[char_to_delete_idx + 1].0
                    } else {
                        line.len()
                    };

                    // Remove the character using drain
                    line.drain(start_byte..end_byte);
                    self.core.cursor_col -= 1;
                    self.update_result(self.core.cursor_line);
                    self.has_unsaved_changes = true;
                }
            } else if self.core.cursor_line > 0 {
                // Cursor is at beginning of line - merge with previous line
                let current_line = self.core.text_lines.remove(self.core.cursor_line);
                self.core.results.remove(self.core.cursor_line);

                // Remove corresponding animation if it exists
                if self.core.cursor_line < self.result_animations.len() {
                    self.result_animations.remove(self.core.cursor_line);
                }

                // Remove corresponding copy flash animation if it exists
                if self.core.cursor_line < self.copy_flash_animations.len() {
                    self.copy_flash_animations.remove(self.core.cursor_line);
                }
                if self.core.cursor_line < self.copy_flash_is_result.len() {
                    self.copy_flash_is_result.remove(self.core.cursor_line);
                }

                // Check if the previous line is empty - if so, we conceptually want to delete
                // the previous line rather than the current line
                let prev_line_empty = self.core.text_lines[self.core.cursor_line - 1].is_empty();

                if prev_line_empty && !current_line.is_empty() {
                    // Previous line is empty, current line has content
                    // Delete the previous line (conceptually what the user wants)
                    self.core.text_lines[self.core.cursor_line - 1] = current_line;
                    self.update_line_references_for_deletion(self.core.cursor_line - 1);
                    self.core.cursor_line -= 1;
                    self.core.cursor_col = 0;
                } else {
                    // Normal case: merge current line into previous line
                    self.update_line_references_for_deletion(self.core.cursor_line);
                    self.core.cursor_line -= 1;
                    self.core.cursor_col =
                        self.core.text_lines[self.core.cursor_line].chars().count();
                    self.core.text_lines[self.core.cursor_line].push_str(&current_line);
                }

                // Re-evaluate all lines after deletion to ensure line references resolve correctly
                self.update_result(self.core.cursor_line);
                for i in (self.core.cursor_line + 1)..self.core.text_lines.len() {
                    self.update_result(i);
                }
                self.has_unsaved_changes = true;
            }
        }
    }

    /// Delete the word before the cursor (Ctrl+W behavior)
    pub fn delete_word(&mut self) {
        if self.core.cursor_line < self.core.text_lines.len() && self.core.cursor_col > 0 {
            let line = &self.core.text_lines[self.core.cursor_line];
            let mut new_col = self.core.cursor_col;

            // Skip trailing whitespace
            while new_col > 0 && line.chars().nth(new_col - 1).unwrap_or(' ').is_whitespace() {
                new_col -= 1;
            }

            // Delete word characters (alphanumeric and underscore)
            while new_col > 0 {
                let ch = line.chars().nth(new_col - 1).unwrap_or(' ');
                if ch.is_alphanumeric() || ch == '_' {
                    new_col -= 1;
                } else {
                    break;
                }
            }

            // If no word was found, delete non-word characters until whitespace
            if new_col == self.core.cursor_col {
                while new_col > 0 {
                    let ch = line.chars().nth(new_col - 1).unwrap_or(' ');
                    if ch.is_whitespace() || ch.is_alphanumeric() || ch == '_' {
                        break;
                    }
                    new_col -= 1;
                }
            }

            // Delete the characters from new_col to cursor_col
            if new_col < self.core.cursor_col {
                let line = &self.core.text_lines[self.core.cursor_line];
                let char_indices: Vec<_> = line.char_indices().collect();

                if !char_indices.is_empty() {
                    let start_byte = if new_col < char_indices.len() {
                        char_indices[new_col].0
                    } else {
                        line.len()
                    };

                    let end_byte = if self.core.cursor_col < char_indices.len() {
                        char_indices[self.core.cursor_col].0
                    } else {
                        line.len()
                    };

                    if start_byte < end_byte {
                        self.core.text_lines[self.core.cursor_line].drain(start_byte..end_byte);
                    }
                }

                self.core.cursor_col = new_col;
                self.update_result(self.core.cursor_line);
                self.has_unsaved_changes = true;
            }
        }
    }

    /// Insert a new line at the cursor position
    pub fn new_line(&mut self) {
        if self.core.cursor_line < self.core.text_lines.len() {
            let current_line = self.core.text_lines[self.core.cursor_line].clone();
            let char_count = current_line.chars().count();
            let safe_cursor_col = self.core.cursor_col.min(char_count);

            // Split the line at character boundary, not byte boundary
            let (left, right) = if safe_cursor_col == 0 {
                ("".to_string(), current_line)
            } else if safe_cursor_col >= char_count {
                (current_line, "".to_string())
            } else {
                let split_byte_idx = current_line
                    .char_indices()
                    .nth(safe_cursor_col)
                    .map(|(i, _)| i)
                    .unwrap_or(current_line.len());
                let left = current_line[..split_byte_idx].to_string();
                let right = current_line[split_byte_idx..].to_string();
                (left, right)
            };

            // Check emptiness before moving the strings
            let left_empty = left.trim().is_empty();
            let right_empty = right.trim().is_empty();

            self.core.text_lines[self.core.cursor_line] = left;
            self.core
                .text_lines
                .insert(self.core.cursor_line + 1, right);
            self.core.results.insert(self.core.cursor_line + 1, None);

            // Insert corresponding empty animation slot
            if self.core.cursor_line + 1 < self.result_animations.len() {
                self.result_animations
                    .insert(self.core.cursor_line + 1, None);
            } else {
                // Ensure animations vector is large enough
                while self.result_animations.len() <= self.core.cursor_line + 1 {
                    self.result_animations.push(None);
                }
            }

            // Also ensure copy flash animations vector is large enough
            if self.core.cursor_line + 1 < self.copy_flash_animations.len() {
                self.copy_flash_animations
                    .insert(self.core.cursor_line + 1, None);
                self.copy_flash_is_result
                    .insert(self.core.cursor_line + 1, false);
            } else {
                while self.copy_flash_animations.len() <= self.core.cursor_line + 1 {
                    self.copy_flash_animations.push(None);
                    self.copy_flash_is_result.push(false);
                }
            }

            // Handle line reference updates for insertion
            let insertion_point = self.core.cursor_line + 1; // 0-based index of newly inserted line

            if left_empty && !right_empty {
                // Content moved from cursor_line to insertion_point
                // Use combined update that handles both content move and position shifts
                self.update_line_references_for_line_split_with_content_move(
                    self.core.cursor_line,
                    insertion_point,
                );
            } else {
                // Standard insertion: just shift references
                self.update_line_references_for_standard_insertion(insertion_point);
            }

            self.core.cursor_line += 1;
            self.core.cursor_col = 0;

            // Make sure to evaluate all lines in the correct order
            // First evaluate the lines that were directly affected by the split
            self.update_result(self.core.cursor_line - 1); // Line 0
            self.update_result(self.core.cursor_line); // Line 1

            // Then re-evaluate any lines that had their references updated
            // This ensures line references can resolve correctly
            for i in (self.core.cursor_line + 1)..self.core.text_lines.len() {
                self.update_result(i);
            }
            self.has_unsaved_changes = true;
        }
    }

    /// Move cursor up one line
    pub fn move_cursor_up(&mut self) {
        if self.core.cursor_line > 0 {
            self.core.cursor_line -= 1;
            self.core.cursor_col = self
                .core
                .cursor_col
                .min(self.core.text_lines[self.core.cursor_line].len());
        }
    }

    /// Move cursor down one line
    pub fn move_cursor_down(&mut self) {
        if self.core.cursor_line + 1 < self.core.text_lines.len() {
            self.core.cursor_line += 1;
            self.core.cursor_col = self
                .core
                .cursor_col
                .min(self.core.text_lines[self.core.cursor_line].len());
        }
    }

    /// Move cursor left one character
    pub fn move_cursor_left(&mut self) {
        if self.core.cursor_col > 0 {
            self.core.cursor_col -= 1;
        }
    }

    /// Move cursor right one character
    pub fn move_cursor_right(&mut self) {
        if self.core.cursor_line < self.core.text_lines.len() {
            self.core.cursor_col =
                (self.core.cursor_col + 1).min(self.core.text_lines[self.core.cursor_line].len());
        }
    }

    /// Delete the entire current line (vim 'dd' command)
    pub fn delete_line(&mut self) {
        if self.core.text_lines.len() > 1 {
            // Update line references before deletion
            self.update_line_references_for_deletion(self.core.cursor_line);

            // Remove the line
            self.core.text_lines.remove(self.core.cursor_line);
            self.core.results.remove(self.core.cursor_line);

            // Remove animation states
            if self.core.cursor_line < self.result_animations.len() {
                self.result_animations.remove(self.core.cursor_line);
            }
            if self.core.cursor_line < self.copy_flash_animations.len() {
                self.copy_flash_animations.remove(self.core.cursor_line);
                self.copy_flash_is_result.remove(self.core.cursor_line);
            }

            // Adjust cursor position
            if self.core.cursor_line >= self.core.text_lines.len() && self.core.cursor_line > 0 {
                self.core.cursor_line -= 1;
            }
            self.core.cursor_col = 0;

            // Re-evaluate all lines after deletion
            for i in self.core.cursor_line..self.core.text_lines.len() {
                self.update_result(i);
            }

            self.has_unsaved_changes = true;
        } else if self.core.text_lines.len() == 1 {
            // If only one line, just clear it instead of deleting
            self.core.text_lines[0].clear();
            self.core.results[0] = None;
            self.core.cursor_col = 0;
            self.update_result(0);
            self.has_unsaved_changes = true;
        }
    }

    /// Delete character at cursor position (vim 'x' command)
    pub fn delete_char_at_cursor(&mut self) {
        if self.core.cursor_line < self.core.text_lines.len() {
            let line = &self.core.text_lines[self.core.cursor_line];
            let char_count = line.chars().count();

            if self.core.cursor_col < char_count {
                let char_indices: Vec<_> = line.char_indices().collect();
                if self.core.cursor_col < char_indices.len() {
                    let byte_start = char_indices[self.core.cursor_col].0;
                    let byte_end = if self.core.cursor_col + 1 < char_indices.len() {
                        char_indices[self.core.cursor_col + 1].0
                    } else {
                        line.len()
                    };

                    self.core.text_lines[self.core.cursor_line].drain(byte_start..byte_end);

                    // Adjust cursor if at end of line after deletion
                    let new_char_count =
                        self.core.text_lines[self.core.cursor_line].chars().count();
                    if self.core.cursor_col >= new_char_count && self.core.cursor_col > 0 {
                        self.core.cursor_col = new_char_count;
                    }
                    self.update_result(self.core.cursor_line);
                    self.has_unsaved_changes = true;
                }
            }
        }
    }

    /// Move cursor forward by one word (vim 'w' command)
    /// A word is a sequence of alphanumeric characters or underscores
    pub fn move_word_forward(&mut self) {
        if self.core.cursor_line >= self.core.text_lines.len() {
            return;
        }

        let line = &self.core.text_lines[self.core.cursor_line];
        let chars: Vec<char> = line.chars().collect();
        let mut new_col = self.core.cursor_col;

        // Skip current word if we're in one
        while new_col < chars.len() && (chars[new_col].is_alphanumeric() || chars[new_col] == '_') {
            new_col += 1;
        }

        // Skip non-word characters
        while new_col < chars.len() && !(chars[new_col].is_alphanumeric() || chars[new_col] == '_')
        {
            new_col += 1;
        }

        // If we've reached the end of the line, move to the next line
        if new_col >= chars.len() && self.core.cursor_line + 1 < self.core.text_lines.len() {
            self.core.cursor_line += 1;
            self.core.cursor_col = 0;
        } else {
            self.core.cursor_col = new_col;
        }
    }

    /// Move cursor backward by one word (vim 'b' command)
    pub fn move_word_backward(&mut self) {
        if self.core.cursor_line >= self.core.text_lines.len() {
            return;
        }

        let line = &self.core.text_lines[self.core.cursor_line];
        let chars: Vec<char> = line.chars().collect();

        if self.core.cursor_col == 0 {
            // If at start of line, move to end of previous line
            if self.core.cursor_line > 0 {
                self.core.cursor_line -= 1;
                self.core.cursor_col = self.core.text_lines[self.core.cursor_line].chars().count();
            }
            return;
        }

        let mut new_col = self.core.cursor_col;
        new_col = new_col.saturating_sub(1);

        // Skip non-word characters backwards
        while new_col > 0 && !(chars[new_col].is_alphanumeric() || chars[new_col] == '_') {
            new_col -= 1;
        }

        // Skip word characters backwards to find start of word
        while new_col > 0 && (chars[new_col - 1].is_alphanumeric() || chars[new_col - 1] == '_') {
            new_col -= 1;
        }

        self.core.cursor_col = new_col;
    }

    /// Move cursor forward by one WORD (vim 'W' command)
    /// A WORD is a sequence of non-whitespace characters
    pub fn move_word_forward_big(&mut self) {
        if self.core.cursor_line >= self.core.text_lines.len() {
            return;
        }

        let line = &self.core.text_lines[self.core.cursor_line];
        let chars: Vec<char> = line.chars().collect();
        let mut new_col = self.core.cursor_col;

        // Skip current WORD if we're in one
        while new_col < chars.len() && !chars[new_col].is_whitespace() {
            new_col += 1;
        }

        // Skip whitespace
        while new_col < chars.len() && chars[new_col].is_whitespace() {
            new_col += 1;
        }

        // If we've reached the end of the line, move to the next line
        if new_col >= chars.len() && self.core.cursor_line + 1 < self.core.text_lines.len() {
            self.core.cursor_line += 1;
            self.core.cursor_col = 0;
        } else {
            self.core.cursor_col = new_col;
        }
    }

    /// Move cursor backward by one WORD (vim 'B' command)
    pub fn move_word_backward_big(&mut self) {
        if self.core.cursor_line >= self.core.text_lines.len() {
            return;
        }

        let line = &self.core.text_lines[self.core.cursor_line];
        let chars: Vec<char> = line.chars().collect();

        if self.core.cursor_col == 0 {
            // If at start of line, move to end of previous line
            if self.core.cursor_line > 0 {
                self.core.cursor_line -= 1;
                self.core.cursor_col = self.core.text_lines[self.core.cursor_line].chars().count();
            }
            return;
        }

        let mut new_col = self.core.cursor_col;
        new_col = new_col.saturating_sub(1);

        // Skip whitespace backwards
        while new_col > 0 && chars[new_col].is_whitespace() {
            new_col -= 1;
        }

        // Skip non-whitespace backwards to find start of WORD
        while new_col > 0 && !chars[new_col - 1].is_whitespace() {
            new_col -= 1;
        }

        self.core.cursor_col = new_col;
    }

    /// Delete from cursor to the end of the current word (vim 'dw' command)
    pub fn delete_word_forward(&mut self) {
        if self.core.cursor_line >= self.core.text_lines.len() {
            return;
        }

        let line = &self.core.text_lines[self.core.cursor_line];
        let chars: Vec<char> = line.chars().collect();
        let start_col = self.core.cursor_col;
        let mut end_col = self.core.cursor_col;

        // Skip current word if we're in one
        while end_col < chars.len() && (chars[end_col].is_alphanumeric() || chars[end_col] == '_') {
            end_col += 1;
        }

        // Also skip trailing non-word characters (spaces, punctuation) to next word
        while end_col < chars.len() && !(chars[end_col].is_alphanumeric() || chars[end_col] == '_')
        {
            end_col += 1;
        }

        // Delete the range
        if end_col > start_col {
            let line = &self.core.text_lines[self.core.cursor_line];
            let char_indices: Vec<_> = line.char_indices().collect();

            if !char_indices.is_empty() {
                let start_byte = if start_col < char_indices.len() {
                    char_indices[start_col].0
                } else {
                    line.len()
                };

                let end_byte = if end_col < char_indices.len() {
                    char_indices[end_col].0
                } else {
                    line.len()
                };

                if start_byte < end_byte {
                    self.core.text_lines[self.core.cursor_line].drain(start_byte..end_byte);
                }
            }

            self.update_result(self.core.cursor_line);
            self.has_unsaved_changes = true;
        }
    }

    /// Delete from cursor to the beginning of the previous word (vim 'db' command)
    pub fn delete_word_backward(&mut self) {
        if self.core.cursor_line >= self.core.text_lines.len() || self.core.cursor_col == 0 {
            return;
        }

        let line = &self.core.text_lines[self.core.cursor_line];
        let chars: Vec<char> = line.chars().collect();
        let end_col = self.core.cursor_col;
        let mut start_col = self.core.cursor_col;

        start_col = start_col.saturating_sub(1);

        // Skip non-word characters backwards
        while start_col > 0 && !(chars[start_col].is_alphanumeric() || chars[start_col] == '_') {
            start_col -= 1;
        }

        // Skip word characters backwards to find start of word
        while start_col > 0
            && (chars[start_col - 1].is_alphanumeric() || chars[start_col - 1] == '_')
        {
            start_col -= 1;
        }

        // Delete the range
        if end_col > start_col {
            let line = &self.core.text_lines[self.core.cursor_line];
            let char_indices: Vec<_> = line.char_indices().collect();

            if !char_indices.is_empty() {
                let start_byte = if start_col < char_indices.len() {
                    char_indices[start_col].0
                } else {
                    line.len()
                };

                let end_byte = if end_col < char_indices.len() {
                    char_indices[end_col].0
                } else {
                    line.len()
                };

                if start_byte < end_byte {
                    self.core.text_lines[self.core.cursor_line].drain(start_byte..end_byte);
                }
            }

            self.core.cursor_col = start_col;
            self.update_result(self.core.cursor_line);
            self.has_unsaved_changes = true;
        }
    }

    /// Delete from cursor to the end of the current WORD (vim 'dW' command)
    pub fn delete_word_forward_big(&mut self) {
        if self.core.cursor_line >= self.core.text_lines.len() {
            return;
        }

        let line = &self.core.text_lines[self.core.cursor_line];
        let chars: Vec<char> = line.chars().collect();
        let start_col = self.core.cursor_col;
        let mut end_col = self.core.cursor_col;

        // Skip current WORD if we're in one
        while end_col < chars.len() && !chars[end_col].is_whitespace() {
            end_col += 1;
        }

        // Also skip trailing whitespace to next WORD
        while end_col < chars.len() && chars[end_col].is_whitespace() {
            end_col += 1;
        }

        // Delete the range
        if end_col > start_col {
            let line = &self.core.text_lines[self.core.cursor_line];
            let char_indices: Vec<_> = line.char_indices().collect();

            if !char_indices.is_empty() {
                let start_byte = if start_col < char_indices.len() {
                    char_indices[start_col].0
                } else {
                    line.len()
                };

                let end_byte = if end_col < char_indices.len() {
                    char_indices[end_col].0
                } else {
                    line.len()
                };

                if start_byte < end_byte {
                    self.core.text_lines[self.core.cursor_line].drain(start_byte..end_byte);
                }
            }

            self.update_result(self.core.cursor_line);
            self.has_unsaved_changes = true;
        }
    }

    /// Delete from cursor to the beginning of the previous WORD (vim 'dB' command)
    pub fn delete_word_backward_big(&mut self) {
        if self.core.cursor_line >= self.core.text_lines.len() || self.core.cursor_col == 0 {
            return;
        }

        let line = &self.core.text_lines[self.core.cursor_line];
        let chars: Vec<char> = line.chars().collect();
        let end_col = self.core.cursor_col;
        let mut start_col = self.core.cursor_col;

        start_col = start_col.saturating_sub(1);

        // Skip whitespace backwards
        while start_col > 0 && chars[start_col].is_whitespace() {
            start_col -= 1;
        }

        // Skip non-whitespace backwards to find start of WORD
        while start_col > 0 && !chars[start_col - 1].is_whitespace() {
            start_col -= 1;
        }

        // Delete the range
        if end_col > start_col {
            let line = &self.core.text_lines[self.core.cursor_line];
            let char_indices: Vec<_> = line.char_indices().collect();

            if !char_indices.is_empty() {
                let start_byte = if start_col < char_indices.len() {
                    char_indices[start_col].0
                } else {
                    line.len()
                };

                let end_byte = if end_col < char_indices.len() {
                    char_indices[end_col].0
                } else {
                    line.len()
                };

                if start_byte < end_byte {
                    self.core.text_lines[self.core.cursor_line].drain(start_byte..end_byte);
                }
            }

            self.core.cursor_col = start_col;
            self.update_result(self.core.cursor_line);
            self.has_unsaved_changes = true;
        }
    }

    /// Update the calculation result for a given line
    pub fn update_result(&mut self, line_index: usize) {
        self.core.update_result(line_index);

        // Check if we need to start animation for the updated result
        if line_index < self.core.results.len() && self.core.results[line_index].is_some() {
            self.start_result_animation(line_index);
        }
    }

    /// Update line references in all lines when a line is deleted
    /// All references > deleted_line need to be decremented by 1
    /// References to the deleted line become invalid
    fn update_line_references_for_deletion(&mut self, deleted_line: usize) {
        for i in 0..self.core.text_lines.len() {
            let updated_text =
                update_line_references_in_text(&self.core.text_lines[i], deleted_line, -1);
            if updated_text != self.core.text_lines[i] {
                self.core.text_lines[i] = updated_text;
                // Re-evaluate this line since its content changed
                self.update_result(i);
            }
        }
    }

    /// Update line references in all lines when a new line is inserted
    /// All references >= insertion_point need to be incremented by 1
    fn update_line_references_for_standard_insertion(&mut self, insertion_point: usize) {
        for i in 0..self.core.text_lines.len() {
            let updated_text =
                update_line_references_in_text(&self.core.text_lines[i], insertion_point, 1);
            if updated_text != self.core.text_lines[i] {
                self.core.text_lines[i] = updated_text;
                // Re-evaluate this line since its content changed
                self.update_result(i);
            }
        }
    }

    /// Combined update for line splitting with content movement
    /// Handles both content following and position-based shifts in one pass
    fn update_line_references_for_line_split_with_content_move(
        &mut self,
        content_from: usize,
        insertion_point: usize,
    ) {
        use crate::expression::extract_line_references;

        for i in 0..self.core.text_lines.len() {
            let references = extract_line_references(&self.core.text_lines[i]);
            let mut updated_text = self.core.text_lines[i].clone();

            // Process references in reverse order to maintain correct string positions
            for (start_pos, end_pos, line_num) in references.into_iter().rev() {
                let new_line_num = if line_num == content_from {
                    // Content moved from content_from to insertion_point
                    insertion_point
                } else if line_num >= insertion_point {
                    // Standard position shift for lines >= insertion_point
                    line_num + 1
                } else {
                    // Lines before insertion_point stay unchanged
                    line_num
                };

                if new_line_num != line_num {
                    let new_ref = format!("line{}", new_line_num + 1); // +1 for 1-based display
                    updated_text.replace_range(start_pos..end_pos, &new_ref);
                }
            }

            if updated_text != self.core.text_lines[i] {
                self.core.text_lines[i] = updated_text;
                // Re-evaluate this line since its content changed
                self.update_result(i);
            }
        }
    }

    /// Recalculate all lines in the notebook
    pub fn recalculate_all(&mut self) {
        // Clear variables to ensure fresh calculation
        self.core.variables.clear();

        // Recalculate each line in order
        for i in 0..self.core.text_lines.len() {
            self.update_result(i);
        }
    }

    /// Start a fade-in animation for a result
    fn start_result_animation(&mut self, line_index: usize) {
        // Ensure the animations vector is large enough
        while self.result_animations.len() <= line_index {
            self.result_animations.push(None);
        }

        // Only start animation if there's actually a result
        if line_index < self.core.results.len() && self.core.results[line_index].is_some() {
            self.result_animations[line_index] = Some(ResultAnimation::new_fade_in());
        }
    }

    /// Update all animations and remove completed ones
    pub fn update_animations(&mut self) {
        for animation in &mut self.result_animations {
            if let Some(anim) = animation {
                if anim.is_complete() {
                    *animation = None;
                }
            }
        }

        // Update copy flash animations
        for animation in &mut self.copy_flash_animations {
            if let Some(anim) = animation {
                if anim.is_complete() {
                    *animation = None;
                }
            }
        }
    }

    /// Get the animation for a specific line
    pub fn get_result_animation(&self, line_index: usize) -> Option<&ResultAnimation> {
        self.result_animations.get(line_index)?.as_ref()
    }

    /// Save the current content to the file
    pub fn save(&mut self) -> Result<(), std::io::Error> {
        if let Some(ref path) = self.file_path {
            use std::fs;
            let content = self.core.text_lines.join("\n");
            fs::write(path, content)?;
            self.has_unsaved_changes = false;
            Ok(())
        } else {
            Err(std::io::Error::new(
                std::io::ErrorKind::NotFound,
                "No file path set",
            ))
        }
    }

    /// Save the current content to a new file
    pub fn save_as(&mut self, path: PathBuf) -> Result<(), std::io::Error> {
        use std::fs;
        let content = self.core.text_lines.join("\n");
        fs::write(&path, content)?;
        self.file_path = Some(path);
        self.has_unsaved_changes = false;
        Ok(())
    }

    /// Set the file path (used when loading a file)
    pub fn set_file_path(&mut self, path: Option<PathBuf>) {
        self.file_path = path;
        self.has_unsaved_changes = false;
    }

    /// Show the save as dialog
    pub fn show_save_as_dialog(&mut self, quit_after_save: bool) {
        self.show_save_as_dialog = true;
        self.save_as_and_quit = quit_after_save;
        self.save_as_input = ".pad".to_string();
    }

    /// Try to save with the current save-as filename
    pub fn save_as_from_dialog(&mut self) -> Result<bool, std::io::Error> {
        if !self.save_as_input.trim().is_empty() {
            let path = PathBuf::from(self.save_as_input.trim());
            self.save_as(path)?;
            self.show_save_as_dialog = false;

            let should_quit = self.save_as_and_quit;
            self.save_as_and_quit = false;
            Ok(should_quit)
        } else {
            // Empty filename, don't save
            Ok(false)
        }
    }

    /// Update separator position based on mouse column position
    pub fn update_separator_position(&mut self, mouse_x: u16, terminal_width: u16) {
        // Calculate percentage based on mouse position
        let percentage = ((mouse_x as f32 / terminal_width as f32) * 100.0) as u16;
        // Clamp between 20% and 80% to ensure both panels remain usable
        self.separator_position = percentage.clamp(20, 80);
    }

    /// Check if mouse position is over the separator (within a few columns for easier dragging)
    pub fn is_mouse_over_separator(&self, mouse_x: u16, terminal_width: u16) -> bool {
        let separator_x = (self.separator_position as f32 / 100.0 * terminal_width as f32) as u16;
        // Allow dragging within 2 columns of the separator
        mouse_x.abs_diff(separator_x) <= 2
    }

    /// Start dragging the separator
    pub fn start_dragging_separator(&mut self) {
        self.is_dragging_separator = true;
    }

    /// Stop dragging the separator
    pub fn stop_dragging_separator(&mut self) {
        self.is_dragging_separator = false;
    }

    /// Set hover state for the separator
    pub fn set_separator_hover(&mut self, hovering: bool) {
        self.is_hovering_separator = hovering;
    }

    /// Copy text to clipboard and start flash animation
    pub fn copy_to_clipboard(
        &mut self,
        text: &str,
        line_index: usize,
        is_result: bool,
    ) -> Result<(), String> {
        // Copy to clipboard using arboard (only available on non-WASM platforms)
        #[cfg(not(target_arch = "wasm32"))]
        {
            let mut clipboard = arboard::Clipboard::new()
                .map_err(|e| format!("Failed to access clipboard: {}", e))?;
            clipboard
                .set_text(text)
                .map_err(|e| format!("Failed to copy to clipboard: {}", e))?;
        }

        #[cfg(target_arch = "wasm32")]
        {
            // On WASM, we can't use arboard but we still want to show the animation
            // Web clipboard access would need to be implemented using web-sys if needed
            let _ = text; // Suppress unused variable warning
        }

        // Start flash animation for the copied line
        self.start_copy_flash_animation(line_index, is_result);

        Ok(())
    }

    /// Start a copy flash animation for a specific line
    fn start_copy_flash_animation(&mut self, line_index: usize, is_result: bool) {
        // Ensure the copy flash animations vector is large enough
        while self.copy_flash_animations.len() <= line_index {
            self.copy_flash_animations.push(None);
            self.copy_flash_is_result.push(false);
        }

        self.copy_flash_animations[line_index] = Some(ResultAnimation::new_copy_flash());
        // Ensure the tracking vector is large enough too
        while self.copy_flash_is_result.len() <= line_index {
            self.copy_flash_is_result.push(false);
        }
        self.copy_flash_is_result[line_index] = is_result;
    }

    /// Check if a click is a double-click
    pub fn is_double_click(&mut self, mouse_x: u16, mouse_y: u16) -> bool {
        let now = Instant::now();
        let double_click_threshold = std::time::Duration::from_millis(500); // 500ms double-click window
        let position_threshold = 2; // Allow 2 pixels of movement

        if let (Some(last_time), Some((last_x, last_y))) =
            (self.last_click_time, self.last_click_position)
        {
            let time_diff = now.duration_since(last_time);
            let position_diff = ((mouse_x as i32 - last_x as i32).abs()
                + (mouse_y as i32 - last_y as i32).abs()) as u16;

            if time_diff <= double_click_threshold && position_diff <= position_threshold {
                // Reset click tracking after successful double-click
                self.last_click_time = None;
                self.last_click_position = None;
                return true;
            }
        }

        // Update click tracking
        self.last_click_time = Some(now);
        self.last_click_position = Some((mouse_x, mouse_y));
        false
    }

    /// Get the copy flash animation for a specific line
    pub fn get_copy_flash_animation(&self, line_index: usize) -> Option<&ResultAnimation> {
        self.copy_flash_animations.get(line_index)?.as_ref()
    }
}

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

    #[test]
    fn test_line_splitting_with_line_references() {
        let mut app = App::default();
        let (before, after) = app.test_scenario_line_splitting();

        println!("Before: {}", before);
        println!("After: {}", after);

        // Expected behavior analysis:
        // Original: Line 1: "5", Line 2: "line1 + 1"
        // After hitting enter at position 1 in "5":
        // Line 1: "5", Line 2: "", Line 3: "line1 + 1" (shifted down from line 2)
        //
        // The question is: should "line1" in "line1 + 1" stay as "line1" or become "line2"?
        // User's expectation: it should become "line2" because the line that was originally
        // at position 2 is now at position 3, so references to lines >= 2 should be incremented.

        // Debug the actual state
        println!("App state after split:");
        for (i, line) in app.core.text_lines.iter().enumerate() {
            println!("  Line {}: '{}'", i + 1, line);
        }

        // The real issue: we're inserting at position 1, so lines at position 1 and after
        // should get their references incremented. "line1 + 1" was originally at position 1,
        // is now at position 2, and "line1" in it should become "line2" because ALL lines
        // shift down after insertion point.
        assert!(
            app.core.text_lines[2].contains("line2"),
            "Expected 'line1' to be updated to 'line2' but got: '{}'",
            app.core.text_lines[2]
        );
    }

    #[test]
    fn test_line_splitting_at_beginning() {
        let mut app = App::default();
        app.core.text_lines = vec!["5".to_string(), "line1 + 1".to_string()];
        app.core.results = vec![None, None];
        app.core.cursor_line = 0;
        app.core.cursor_col = 0; // Position cursor at beginning of "5"

        app.new_line();

        // When hitting enter at beginning:
        // Line 1: "" (empty), Line 2: "5" (content moved down), Line 3: "line2 + 1"
        assert_eq!(app.core.text_lines[0], "");
        assert_eq!(app.core.text_lines[1], "5");
        assert!(
            app.core.text_lines[2].contains("line2"),
            "Expected 'line1' to be updated to 'line2' but got: '{}'",
            app.core.text_lines[2]
        );
    }

    #[test]
    fn test_user_reported_scenario() {
        let mut app = App::default();
        app.core.text_lines = vec!["5".to_string(), "line1 + 1".to_string()];
        app.core.results = vec![None, None];

        // Simulate exactly what the user described:
        // "i have the following notebook: 5, line1 + 1"
        app.update_result(0); // This should make line 1 result in "5"
        app.update_result(1); // This should make line 2 result in "6" (5 + 1)

        // Verify initial results work correctly
        assert_eq!(app.core.results[0], Some("5".to_string()));
        assert_eq!(app.core.results[1], Some("6".to_string()));

        // Now simulate "hit enter when the cursor is over the 5"
        app.core.cursor_line = 0;
        app.core.cursor_col = 0; // Position cursor at the beginning of "5"
        app.new_line();

        // Verify the results:
        // Line 1: "" (empty)
        // Line 2: "5"
        // Line 3: "line2 + 1" (updated reference)
        assert_eq!(app.core.text_lines[0], "");
        assert_eq!(app.core.text_lines[1], "5");
        assert!(
            app.core.text_lines[2].contains("line2"),
            "Expected line reference to be updated to 'line2', got: '{}'",
            app.core.text_lines[2]
        );

        // The key test: verify that the expression evaluates correctly automatically
        // line2 should now refer to "5" on line 2, so "line2 + 1" should be 6
        assert_eq!(
            app.core.results[2],
            Some("6".to_string()),
            "Expected 'line2 + 1' to evaluate to 6 automatically, got: {:?}",
            app.core.results[2]
        );
    }

    #[test]
    fn test_deletion_with_line_references() {
        let mut app = App::default();
        app.core.text_lines = vec!["".to_string(), "5".to_string(), "line2 + 1".to_string()];
        app.core.results = vec![None, None, None];

        // Set up: after line splitting we have ["", "5", "line2 + 1"]
        app.update_result(0);
        app.update_result(1);
        app.update_result(2);

        // Verify initial state works
        assert_eq!(app.core.results[1], Some("5".to_string()));
        assert_eq!(app.core.results[2], Some("6".to_string()));

        // Now delete the empty first line by positioning cursor at beginning of line 2 and hitting backspace
        app.core.cursor_line = 1;
        app.core.cursor_col = 0;
        app.delete_char(); // This should merge lines and update references

        // Expected result: ["5", "line1 + 1"] (reference should go back to line1)
        assert_eq!(app.core.text_lines.len(), 2);
        assert_eq!(app.core.text_lines[0], "5");
        assert!(
            app.core.text_lines[1].contains("line1"),
            "Expected 'line2' to be updated back to 'line1', got: '{}'",
            app.core.text_lines[1]
        );

        // The critical test: expression should still evaluate correctly
        assert_eq!(
            app.core.results[1],
            Some("6".to_string()),
            "Expected 'line1 + 1' to evaluate to 6 after deletion, got: {:?}",
            app.core.results[1]
        );
    }

    #[test]
    fn test_full_user_workflow_add_then_remove_lines() {
        let mut app = App::default();
        app.core.text_lines = vec!["5".to_string(), "line1 + 1".to_string()];
        app.core.results = vec![None, None];

        // Start with the user's original notebook
        app.update_result(0);
        app.update_result(1);

        // Verify initial state: 5, line1 + 1 = 6
        assert_eq!(app.core.results[0], Some("5".to_string()));
        assert_eq!(app.core.results[1], Some("6".to_string()));

        // Step 1: Hit enter at beginning of "5" (add lines)
        app.core.cursor_line = 0;
        app.core.cursor_col = 0;
        app.new_line();

        // Should have: ["", "5", "line2 + 1"] with line2 + 1 = 6
        assert_eq!(app.core.text_lines.len(), 3);
        assert_eq!(app.core.text_lines[0], "");
        assert_eq!(app.core.text_lines[1], "5");
        assert!(app.core.text_lines[2].contains("line2"));
        assert_eq!(app.core.results[2], Some("6".to_string()));

        // Step 2: Remove the empty line (delete lines)
        app.core.cursor_line = 1;
        app.core.cursor_col = 0;
        app.delete_char();

        // Should be back to: ["5", "line1 + 1"] with line1 + 1 = 6
        assert_eq!(app.core.text_lines.len(), 2);
        assert_eq!(app.core.text_lines[0], "5");
        assert!(app.core.text_lines[1].contains("line1"));
        assert_eq!(
            app.core.results[1],
            Some("6".to_string()),
            "Full workflow failed: expected line1 + 1 = 6 after add/remove cycle"
        );
    }

    #[test]
    fn test_separator_position_updates() {
        let mut app = App::default();

        // Test default position
        assert_eq!(app.separator_position, 80);

        // Test updating separator position
        app.update_separator_position(400, 1000); // 40% position
        assert_eq!(app.separator_position, 40);

        // Test clamping to minimum
        app.update_separator_position(100, 1000); // 10% position - should be clamped to 20%
        assert_eq!(app.separator_position, 20);

        // Test clamping to maximum
        app.update_separator_position(900, 1000); // 90% position - should be clamped to 80%
        assert_eq!(app.separator_position, 80);
    }

    #[test]
    fn test_mouse_over_separator_detection() {
        let app = App::default(); // 80% separator position
        let terminal_width = 1000;
        let separator_x = 800; // 80% of 1000

        // Test exact position
        assert!(app.is_mouse_over_separator(separator_x, terminal_width));

        // Test within range (±2 columns)
        assert!(app.is_mouse_over_separator(separator_x - 2, terminal_width));
        assert!(app.is_mouse_over_separator(separator_x + 2, terminal_width));

        // Test outside range
        assert!(!app.is_mouse_over_separator(separator_x - 3, terminal_width));
        assert!(!app.is_mouse_over_separator(separator_x + 3, terminal_width));
    }

    #[test]
    fn test_separator_dragging_state() {
        let mut app = App::default();

        // Test initial state
        assert!(!app.is_dragging_separator);

        // Test starting drag
        app.start_dragging_separator();
        assert!(app.is_dragging_separator);

        // Test stopping drag
        app.stop_dragging_separator();
        assert!(!app.is_dragging_separator);
    }

    #[test]
    fn test_separator_hover_state() {
        let mut app = App::default();

        // Test initial state
        assert!(!app.is_hovering_separator);

        // Test starting hover
        app.set_separator_hover(true);
        assert!(app.is_hovering_separator);

        // Test stopping hover
        app.set_separator_hover(false);
        assert!(!app.is_hovering_separator);
    }

    #[test]
    fn test_double_click_detection() {
        let mut app = App::default();

        // First click should not be a double-click
        assert!(!app.is_double_click(100, 100));

        // Immediate second click should be a double-click
        assert!(app.is_double_click(100, 100));

        // After double-click, state should be reset
        assert!(!app.is_double_click(100, 100));
    }

    #[test]
    fn test_double_click_position_threshold() {
        let mut app = App::default();

        // First click
        assert!(!app.is_double_click(100, 100));

        // Click within threshold should be double-click
        assert!(app.is_double_click(101, 101));

        // Reset state
        app.last_click_time = None;
        app.last_click_position = None;

        // First click
        assert!(!app.is_double_click(100, 100));

        // Click outside threshold should not be double-click
        assert!(!app.is_double_click(110, 110));
    }

    #[test]
    fn test_copy_flash_animation() {
        let mut app = App::default();
        app.core.text_lines = vec!["test".to_string(), "test2".to_string()];
        app.copy_flash_animations = vec![None, None];

        // Ensure we have enough lines

        // Test copy to clipboard (we can't actually test clipboard, but we can test the animation)
        app.start_copy_flash_animation(0, false);

        // Should have flash animation
        assert!(app.get_copy_flash_animation(0).is_some());
        assert!(app.get_copy_flash_animation(1).is_none());

        // Animation should be of correct type
        let animation = app.get_copy_flash_animation(0).unwrap();
        assert!(matches!(
            animation.animation_type,
            crate::app::AnimationType::CopyFlash
        ));
    }

    #[test]
    fn test_delete_line() {
        let mut app = App::default();
        app.core.text_lines = vec![
            "first".to_string(),
            "second".to_string(),
            "third".to_string(),
        ];
        app.core.results = vec![None, None, None];
        app.result_animations = vec![None, None, None];
        app.copy_flash_animations = vec![None, None, None];
        app.copy_flash_is_result = vec![false, false, false];
        app.core.cursor_line = 1;

        // Delete middle line
        app.delete_line();
        assert_eq!(app.core.text_lines, vec!["first", "third"]);
        assert_eq!(app.core.cursor_line, 1);
        assert_eq!(app.core.cursor_col, 0);

        // Delete last line
        app.delete_line();
        assert_eq!(app.core.text_lines, vec!["first"]);
        assert_eq!(app.core.cursor_line, 0);

        // Try to delete only line - should just clear it
        app.delete_line();
        assert_eq!(app.core.text_lines, vec![""]);
        assert_eq!(app.core.cursor_line, 0);
    }

    #[test]
    fn test_delete_char_at_cursor() {
        let mut app = App::default();
        app.core.text_lines = vec!["hello world".to_string()];
        app.core.results = vec![None];
        app.core.cursor_line = 0;
        app.core.cursor_col = 6; // at 'w'

        app.delete_char_at_cursor();
        assert_eq!(app.core.text_lines[0], "hello orld");
        assert_eq!(app.core.cursor_col, 6);

        // Delete at end of line should do nothing
        app.core.cursor_col = app.core.text_lines[0].len();
        app.delete_char_at_cursor();
        assert_eq!(app.core.text_lines[0], "hello orld");
    }

    #[test]
    fn test_word_movement_forward() {
        let mut app = App::default();
        app.core.text_lines = vec!["hello world test_var 123".to_string()];
        app.core.cursor_line = 0;
        app.core.cursor_col = 0;

        // Move from start to 'world'
        app.move_word_forward();
        assert_eq!(app.core.cursor_col, 6);

        // Move to 'test_var'
        app.move_word_forward();
        assert_eq!(app.core.cursor_col, 12);

        // Move to '123'
        app.move_word_forward();
        assert_eq!(app.core.cursor_col, 21);

        // Try to move past end - should go to end of line
        app.move_word_forward();
        assert_eq!(app.core.cursor_col, 24); // Should be at end of line
    }

    #[test]
    fn test_word_movement_backward() {
        let mut app = App::default();
        app.core.text_lines = vec!["hello world test_var".to_string()];
        app.core.cursor_line = 0;
        app.core.cursor_col = 20; // at end

        // Move to start of 'test_var'
        app.move_word_backward();
        assert_eq!(app.core.cursor_col, 12);

        // Move to start of 'world'
        app.move_word_backward();
        assert_eq!(app.core.cursor_col, 6);

        // Move to start of 'hello'
        app.move_word_backward();
        assert_eq!(app.core.cursor_col, 0);
    }

    #[test]
    fn test_word_movement_big() {
        let mut app = App::default();
        app.core.text_lines = vec!["hello-world test::func()".to_string()];
        app.core.cursor_line = 0;
        app.core.cursor_col = 0;

        // 'hello-world' is one WORD
        app.move_word_forward_big();
        assert_eq!(app.core.cursor_col, 12);

        // Move backward
        app.core.cursor_col = 24;
        app.move_word_backward_big();
        assert_eq!(app.core.cursor_col, 12);

        app.move_word_backward_big();
        assert_eq!(app.core.cursor_col, 0);
    }

    #[test]
    fn test_delete_word_forward() {
        let mut app = App::default();
        app.core.text_lines = vec!["hello world test".to_string()];
        app.core.results = vec![None];
        app.core.cursor_line = 0;
        app.core.cursor_col = 0;

        // Delete 'hello ' (word + trailing space)
        app.delete_word_forward();
        assert_eq!(app.core.text_lines[0], "world test");
        assert_eq!(app.core.cursor_col, 0);

        // Delete from middle of word
        app.core.cursor_col = 2; // in 'world'
        app.delete_word_forward();
        assert_eq!(app.core.text_lines[0], "wotest");
    }

    #[test]
    fn test_delete_word_backward() {
        let mut app = App::default();
        app.core.text_lines = vec!["hello world test".to_string()];
        app.core.results = vec![None];
        app.core.cursor_line = 0;
        app.core.cursor_col = 11; // at space after 'world'

        // Delete 'world'
        app.delete_word_backward();
        assert_eq!(app.core.text_lines[0], "hello  test");
        assert_eq!(app.core.cursor_col, 6);
    }

    #[test]
    fn test_delete_word_forward_big() {
        let mut app = App::default();
        app.core.text_lines = vec!["hello-world test::func()".to_string()];
        app.core.results = vec![None];
        app.core.cursor_line = 0;
        app.core.cursor_col = 0;

        // Delete 'hello-world ' (WORD + trailing space)
        app.delete_word_forward_big();
        assert_eq!(app.core.text_lines[0], "test::func()");
        assert_eq!(app.core.cursor_col, 0);
    }

    #[test]
    fn test_pending_normal_command() {
        let mut app = App {
            pending_normal_command: Some('d'),
            ..Default::default()
        };

        // Should be able to set and read pending command
        assert_eq!(app.pending_normal_command, Some('d'));

        // Clear it
        app.pending_normal_command = None;
        assert_eq!(app.pending_normal_command, None);
    }

    #[test]
    fn test_command_mode_initialization() {
        let mut app = App::default();

        // Initially not in command mode
        assert_eq!(app.mode, Mode::Insert);
        assert_eq!(app.command_line, "");
        assert_eq!(app.command_cursor, 0);

        // Enter command mode
        app.mode = Mode::Command;
        app.command_line = ":".to_string();
        app.command_cursor = 1;

        assert_eq!(app.mode, Mode::Command);
        assert_eq!(app.command_line, ":");
        assert_eq!(app.command_cursor, 1);
    }

    #[test]
    fn test_command_line_editing() {
        let mut app = App {
            mode: Mode::Command,
            command_line: ":w".to_string(),
            command_cursor: 2,
            ..Default::default()
        };

        // Test cursor movement
        assert_eq!(app.command_cursor, 2);

        // Test that command line can be modified
        app.command_line = ":wq".to_string();
        app.command_cursor = 3;

        assert_eq!(app.command_line, ":wq");
        assert_eq!(app.command_cursor, 3);
    }

    #[test]
    fn test_command_mode_toggle() {
        let mut app = App {
            mode: Mode::Normal,
            ..Default::default()
        };

        // Start in normal mode
        assert_eq!(app.mode, Mode::Normal);

        // Switch to command mode
        app.mode = Mode::Command;
        assert_eq!(app.mode, Mode::Command);

        // Switch back to normal mode
        app.mode = Mode::Normal;
        assert_eq!(app.mode, Mode::Normal);
    }

    #[test]
    fn test_delete_char_utf8() {
        let mut app = App::default();

        // Test deleting multi-byte characters
        app.insert_char('');
        assert_eq!(app.core.cursor_col, 1);
        app.insert_char(' ');
        assert_eq!(app.core.cursor_col, 2);
        app.insert_char('5');
        assert_eq!(app.core.cursor_col, 3);
        assert_eq!(app.core.text_lines[0], "€ 5");

        // Delete '5'
        app.delete_char();
        assert_eq!(app.core.text_lines[0], "");
        assert_eq!(app.core.cursor_col, 2);

        // Delete space
        app.delete_char();
        assert_eq!(app.core.text_lines[0], "");
        assert_eq!(app.core.cursor_col, 1);

        // Delete euro sign
        app.delete_char();
        assert_eq!(app.core.text_lines[0], "");
        assert_eq!(app.core.cursor_col, 0);

        // Test with emoji
        app.insert_char('🚀');
        app.insert_char('');
        app.insert_char('¥');
        assert_eq!(app.core.text_lines[0], "🚀€¥");
        assert_eq!(app.core.cursor_col, 3);

        // Delete yen
        app.delete_char();
        assert_eq!(app.core.text_lines[0], "🚀€");
        assert_eq!(app.core.cursor_col, 2);

        // Delete euro
        app.delete_char();
        assert_eq!(app.core.text_lines[0], "🚀");
        assert_eq!(app.core.cursor_col, 1);
    }
}