edamame 0.1.2

A fast TUI Markdown editor and viewer
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
//! `TableView` — per-frame layout snapshot plus the row/column-button
//! rendering needed for mouse-driven row/column drag, column resize,
//! and row/column delete.
//!
//! This module deliberately does NOT introduce a standalone `StatefulWidget` for
//! tables.  The rendered table lines continue to flow through `ParsedDoc`'s
//! pre-rendered line list so scroll, wrap, and cell-scoped raw reveal keep
//! working unchanged.  What this module owns is the mouse-facing half of the
//! table surface:
//!
//!   1. **`TableLayoutSnapshot`** — a per-frame record of where each table's
//!      columns and data rows sit on screen, plus the optional row-handle
//!      column and column-handle row.
//!   2. **`hit_test`** — pure `(col, row)` → `TableHit` lookup on a snapshot.
//!   3. **`paint_handles`** — writes the `≡` / `⇔` glyphs into the buffer
//!      after the normal line-render pass has drawn the surrounding content.
//!   4. **`build_snapshots`** — scans the visible rendered lines for tables
//!      and produces one snapshot per visible table.
//!
//! Snapshots are stored on the `RenderedViewState` so mouse-event handling in
//! the next frame can hit-test against them.

use std::ops::Range;

use ratatui::buffer::Buffer as TuiBuf;
use ratatui::layout::Rect;
use ratatui::style::Style;
use ratatui::text::Line;

use crate::config::Theme;
use crate::editor::{table_edit, EditorState};
use crate::markdown::table_layout;

/// Reorder-handle glyph — `⠿` (U+283F, braille dots 1-2-3-4-5-6).  Used for
/// BOTH row-reorder (painted in the external left-side gutter at the `│` of
/// each data row) and column-reorder (painted at the centre of each column's
/// top `─` border cell).  The "dot grip" convention reads as "drag me".
pub const REORDER_HANDLE_GLYPH: char = '⠿';
/// Heavy horizontal box-drawing rule used to highlight the destination
/// separator during a row-handle drag.  Heavier weight (`━`) reads against
/// the standard `─` separator and `─` border of the surrounding table.
pub const DROP_ROW_GLYPH: char = '━';
/// Heavy vertical box-drawing rule used to highlight the destination
/// separator during a column-handle drag.  Heavier weight (`┃`) reads
/// against the standard `│` border of the surrounding table.
pub const DROP_COL_GLYPH: char = '┃';
/// Column-resize glyph — `⇔` (U+21D4, left-right arrow).  Painted on each
/// interior `│` of the header row so the user has a visible, hoverable
/// resize target — but clicks on any part of the interior border (the pipe
/// and the two columns adjacent to it, within the `±1` tolerance)
/// still drive a resize.
pub const COLUMN_RESIZE_GLYPH: char = '⇔';
/// Delete-handle glyph — `✕` (U+2715).  Painted on the table's outer
/// right `│` (overlaying the border for each data row) and on the
/// bottom-border row (centred over each column).  Clicks on the glyph
/// delete that row / column outright; undo restores it.  Gated by the
/// same `config.table.show_buttons` flag as the reorder / resize
/// handles.
pub const DELETE_HANDLE_GLYPH: char = '✕';

/// Number of leading rows in a `TableInfo`'s row table that come before the
/// first data row: row 0 is the header, row 1 is the alignment row, so data
/// row `i` lives at `HEADER_ROWS + i`.
pub const HEADER_ROWS: usize = 2;

/// Per-frame snapshot of one visible table's layout.
///
/// Screen coordinates (`col_ranges`, `row_ranges`, `row_handle_col`,
/// `top_border_row`, `header_row`) are in terminal cells, *relative to the document
/// area*'s origin.  They're valid only for the frame on which they were
/// built; rebuild on every render.
#[derive(Debug, Clone)]
pub struct TableLayoutSnapshot {
    /// Byte offset of this table's first row in the source buffer.
    pub table_byte_start: usize,
    /// Byte offset just past the last newline of the final row.
    pub table_byte_end: usize,
    /// Number of columns (`info.col_count`).
    pub col_count: usize,
    /// Number of TableInfo rows (header + alignment + data).
    pub row_count: usize,
    /// Per-column character-cell ranges inside the content area of each
    /// row — `col_ranges[c].start` is the column just after the opening `│`,
    /// `col_ranges[c].end` is the column of the closing `│`.
    pub col_ranges: Vec<Range<u16>>,
    /// Per-data-row vertical ranges (y).  `row_ranges[i]` spans the rendered
    /// row that displays `info.rows[2 + i]`.  Only data rows carry a drag
    /// handle so this intentionally skips the header + alignment.
    pub row_ranges: Vec<Range<u16>>,
    /// Column (doc-area x) where the `⠿` row-reorder glyph is painted.  Sits
    /// one cell left of the table's outer `│` (i.e. in the external gutter).
    /// `None` when handles are disabled on this frame.
    pub row_handle_col: Option<u16>,
    /// Row (doc-area y) of the `┌─┬─┐` top border, where the column-reorder
    /// `⠿` glyphs are painted (one per column, centred on the top-border
    /// cell).  `None` when handles are disabled OR when the top border
    /// scrolled off the viewport.
    pub top_border_row: Option<u16>,
    /// Row (doc-area y) of the rendered header row.  Used to place the
    /// `⇔` column-resize glyphs on each interior `│` of the header row.
    /// `None` when the header scrolled off the viewport.
    pub header_row: Option<u16>,
    /// Column (doc-area x) where the `✕` row-delete glyph is painted.
    /// Sits ON the table's outer right `│` (i.e. the same column as
    /// `col_ranges.last().end`) — the glyph overlays the border cell
    /// for each data row.  Resize on data rows therefore shifts to
    /// "one cell inside the border"; resize on the header (`⇔`),
    /// alignment, top-border, and bottom-border rows at the same x
    /// still works because those rows have no delete-row hit and fall
    /// through to `ColumnBorder`.  `None` when handles are disabled.
    pub delete_row_handle_col: Option<u16>,
    /// Row (doc-area y) of the `└─┴─┘` bottom border, where the
    /// column-delete `✕` glyphs are painted (one per column, centred on
    /// the bottom-border cell).  `None` when handles are disabled OR
    /// when the bottom border scrolled off the viewport.
    pub bottom_border_row: Option<u16>,
}

/// What a `(col, row)` click lands on inside a table.
///
/// `Cell::row_idx` and `RowHandle::row_idx` are **TableInfo row indices**
/// (i.e. `HEADER_ROWS + data_index` — header is row 0, alignment is row 1).
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum TableHit {
    /// Click inside a data cell's content area.
    Cell { row_idx: usize, col_idx: usize },
    /// Click on a vertical `│` border.  `col_idx` is `0..=col_count`:
    /// - `0` is the outer left `│`
    /// - `col_count` is the outer right `│`
    /// - `1..col_count` are the interior borders (resize targets).
    ///
    /// Only interior borders drive resize; the outer borders are exposed so
    /// callers can still classify the click precisely.
    ColumnBorder { col_idx: usize },
    /// Click on the `≡` row-drag glyph next to a data row.
    RowHandle { row_idx: usize },
    /// Click on the `⇔` column-drag glyph above a column.
    ColumnHandle { col_idx: usize },
    /// Click on the `✕` row-delete glyph on the right outer `│`.
    /// `row_idx` is a `TableInfo` row index (≥ 2 — header and alignment
    /// can't be deleted, so they don't carry a delete handle, leaving
    /// the same border cell as a resize target on those rows).
    DeleteRowHandle { row_idx: usize },
    /// Click on the `✕` column-delete glyph on the bottom-border row.
    /// `col_idx` is the 0-indexed column.  Only emitted when the table
    /// has more than one column (a single-column table can't lose its
    /// last column).
    DeleteColumnHandle { col_idx: usize },
}

impl TableLayoutSnapshot {
    /// `TableInfo` row index of the data row nearest to document-relative
    /// `row`, or `None` when `row` is outside the data-row band entirely.
    ///
    /// `row_ranges` covers only the rendered sub-lines of the data rows
    /// themselves, so consecutive entries are separated by a one-row gap:
    /// the `├─┼─┤` rule (or, under `table.row_striping`, the blank stripe
    /// line).  A strict range test therefore answers `None` for every
    /// *other* row of the gutter, which is what made grabbing the `⠿`
    /// handle a coin flip and made the drop indicator stall while the
    /// pointer crossed a separator.  Snapping to the nearest row closes
    /// those gaps so the whole vertical extent of the table's data rows is
    /// live.  The band is deliberately bounded at the separator above the
    /// first data row and the one below the last, so the header /
    /// alignment rows above it stay un-grabbable (they aren't reorderable)
    /// and a pointer that has left the table below stops moving the hover.
    pub fn data_row_at_y(&self, row: u16) -> Option<usize> {
        let first = self.row_ranges.first()?;
        let last = self.row_ranges.last()?;
        if row < first.start.saturating_sub(1) || row > last.end {
            return None;
        }
        let idx = self
            .row_ranges
            .iter()
            .enumerate()
            .min_by_key(|(_, r)| range_distance(r, row))
            .map(|(i, _)| i)?;
        Some(HEADER_ROWS + idx)
    }

    /// 0-indexed column nearest to document-relative `col`, or `None` when
    /// `col` is outside the table's horizontal extent (the outer `│`
    /// borders bound it).
    ///
    /// Same rationale as [`Self::data_row_at_y`] on the other axis: the
    /// `col_ranges` entries are separated by the one-cell `│` / `┬`
    /// vertices, and a strict test drops every click that lands on one —
    /// including the `┌`/`┬`/`┐` glyphs of the top border, which is
    /// exactly where a user aiming at a column-reorder handle tends to
    /// press.
    pub fn column_at_x(&self, col: u16) -> Option<usize> {
        let first = self.col_ranges.first()?;
        let last = self.col_ranges.last()?;
        if col < first.start.saturating_sub(1) || col > last.end {
            return None;
        }
        self.col_ranges
            .iter()
            .enumerate()
            .min_by_key(|(_, r)| range_distance(r, col))
            .map(|(i, _)| i)
    }

    /// Hit-test `(col, row)` — both in document-area-relative coordinates —
    /// against this snapshot.  Returns `None` when the click falls outside
    /// any tracked region.
    ///
    /// Precedence: delete handle → row handle → column handle → column
    /// border → cell.  Delete handles win over `ColumnBorder` because
    /// the row-delete glyph sits ON the outer right `│` itself — for
    /// data rows it overlays the border, so a click there deletes the
    /// row; resize on data rows is still reachable via the cell just
    /// inside (`border - 1`) or just outside (`border + 1`).  Clicks
    /// at the same x on header / alignment / top / bottom border rows
    /// have no delete handle and fall through to `ColumnBorder`, so
    /// the right column stays resizable from those rows.  Borders are
    /// hit within `±1` of their `│` col.
    pub fn hit_test(&self, col: u16, row: u16) -> Option<TableHit> {
        // Row-delete handle — click in the right-side external gutter at
        // the delete-handle column AND within a data-row y-range.  Checked
        // BEFORE ColumnBorder so the `✕` cell wins over the right-border
        // resize tolerance window.
        if let Some(handle_col) = self.delete_row_handle_col {
            if col == handle_col {
                for (i, y_range) in self.row_ranges.iter().enumerate() {
                    if row >= y_range.start && row < y_range.end {
                        return Some(TableHit::DeleteRowHandle {
                            row_idx: HEADER_ROWS + i,
                        });
                    }
                }
            }
        }

        // Column-delete handle — the `✕` cell on the bottom-border row,
        // within `±1` of the painted glyph.  Deliberately NOT the column's
        // whole width: this is the one destructive hit-test on the border
        // rows, so a user aiming at a column border from below shouldn't
        // drop a column instead, and a ±1 window is still a comfortable
        // target.
        if let Some(bot_y) = self.bottom_border_row {
            if row == bot_y {
                for (c, x_range) in self.col_ranges.iter().enumerate() {
                    if let Some(glyph_x) = column_glyph_x(x_range) {
                        if col.abs_diff(glyph_x) <= 1 {
                            return Some(TableHit::DeleteColumnHandle { col_idx: c });
                        }
                    }
                }
            }
        }

        // Row-reorder handle — click in the external gutter at the row-handle
        // column, anywhere across the data rows' vertical band (separators
        // included; see `data_row_at_y`).
        if let Some(handle_col) = self.row_handle_col {
            if col == handle_col {
                if let Some(row_idx) = self.data_row_at_y(row) {
                    return Some(TableHit::RowHandle { row_idx });
                }
            }
        }

        // Column-reorder handle — click on the top-border row (the `┌─┬─┐`
        // line), anywhere within the table's horizontal extent.  Interior
        // `│` vertices (`┬` positions) resolve to the adjacent column for
        // ergonomics — without that, a user who clicks exactly on the `┬`
        // glyph silently misses the handle and starts a resize instead.
        if let Some(top_y) = self.top_border_row {
            if row == top_y {
                if let Some(col_idx) = self.column_at_x(col) {
                    return Some(TableHit::ColumnHandle { col_idx });
                }
            }
        }

        // Column border / column-resize glyph — clicks anywhere within ±1 of
        // a vertical `│` border are resize targets, provided the click is on
        // a row that's actually part of the table (top border, header, or any
        // data row — interior `│` runs full height).  The resize glyph `⇔`
        // on the header row is effectively a highlighted sub-region of the
        // same hit-test area.
        if self.row_count > 0 && self.row_on_table(row) {
            if let Some(first) = self.col_ranges.first() {
                let left_border = first.start.saturating_sub(1);
                if col.abs_diff(left_border) <= 1 {
                    return Some(TableHit::ColumnBorder { col_idx: 0 });
                }
            }
            for (c, x_range) in self.col_ranges.iter().enumerate() {
                let border_col = x_range.end;
                if col.abs_diff(border_col) <= 1 {
                    return Some(TableHit::ColumnBorder { col_idx: c + 1 });
                }
            }
        }

        // Data cell.
        for (i, y_range) in self.row_ranges.iter().enumerate() {
            if row >= y_range.start && row < y_range.end {
                for (c, x_range) in self.col_ranges.iter().enumerate() {
                    if col >= x_range.start && col < x_range.end {
                        return Some(TableHit::Cell {
                            row_idx: HEADER_ROWS + i,
                            col_idx: c,
                        });
                    }
                }
            }
        }
        None
    }

    fn row_on_table(&self, row: u16) -> bool {
        let first_y = self
            .top_border_row
            .or_else(|| self.row_ranges.first().map(|r| r.start.saturating_sub(2)))
            .unwrap_or(0);
        let last_y = self
            .row_ranges
            .last()
            .map(|r| r.end + 1) // include the bottom border
            .unwrap_or(first_y);
        row >= first_y && row <= last_y
    }
}

/// Distance from `v` to `range`, in cells — `0` when `v` is inside it.
/// Used to snap a pointer position onto the nearest row / column when it
/// lands on one of the box-drawing cells *between* two ranges.
fn range_distance(range: &Range<u16>, v: u16) -> u16 {
    if v < range.start {
        range.start - v
    } else if v >= range.end {
        v - range.end + 1
    } else {
        0
    }
}

/// X of the reorder / delete glyph painted on a column's border cell —
/// the centre of the column's content span.  Returns `None` for a
/// zero-width column.  The single derivation shared by `paint_handles`
/// and the column-delete hit-test, so the `✕` a user sees and the cell
/// that actually deletes can't drift apart.
fn column_glyph_x(x_range: &Range<u16>) -> Option<u16> {
    if x_range.end <= x_range.start {
        return None;
    }
    Some(x_range.start + (x_range.end - x_range.start) / 2)
}

/// Per-frame instruction for `paint_drop_indicator`.  Captures just the
/// information the painter needs about the active drag — `paint_handles`'s
/// caller distills `mouse_ops::DragTarget` into one of these so the UI
/// layer doesn't import the editor-side enum.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum DropIndicator {
    /// Row-drag in progress; highlight the horizontal separator that would
    /// receive the dropped row.  `hover_row_idx` is the `TableInfo` row
    /// index the pointer is currently over (≥ 2 for data rows).  The
    /// painter draws on the separator just *above* that row when dropping
    /// upward, *below* when downward — derived from `src_row_idx`.
    Row {
        table_byte_start: usize,
        src_row_idx: usize,
        hover_row_idx: usize,
    },
    /// Column-drag in progress; highlight the vertical border that would
    /// receive the dropped column.  Same drop-side semantics as `Row`.
    Column {
        table_byte_start: usize,
        src_col_idx: usize,
        hover_col_idx: usize,
    },
    /// Column-border resize in progress; show a faint vertical guideline
    /// at the pointer's current X to indicate where the release will
    /// commit the new width. Currently driven only by integration tests
    /// in `tests/ui.rs`; the live column-border drag uses a different
    /// painting path.
    #[allow(dead_code)]
    ColumnBorder { table_byte_start: usize, x: u16 },
}

// ── Snapshot construction ───────────────────────────────────────────────────

/// Refresh `snapshots` in place when the cache key
/// (`scroll`, `area`, `parsed_version`, `show_handles`) differs from the
/// previous frame's; otherwise leave the vector untouched.  Mirrors
/// `image_view::build_snapshots_cached` and `link_view::build_snapshots_cached`.
/// `show_handles` is part of the key because it changes the snapshot
/// contents (handle columns / rows are conditionally populated).
pub fn build_snapshots_cached(
    state: &EditorState,
    area: Rect,
    show_handles: bool,
    snapshots: &mut Vec<TableLayoutSnapshot>,
    cache_key: &mut Option<(usize, Rect, u64, bool)>,
) {
    let key = (state.scroll, area, state.parsed_version, show_handles);
    if *cache_key == Some(key) {
        return;
    }
    *snapshots = build_snapshots(state, area, show_handles);
    *cache_key = Some(key);
}

/// Walk every visible rendered line and build a snapshot for every table
/// fully or partially on screen.
///
/// `show_handles` controls whether snapshots carry the row-handle / column-
/// handle coordinates (and therefore whether hit-testing classifies clicks
/// on those cells as handle hits).  Pass `capabilities.mouse && config.table
/// .show_buttons` from the caller.
pub fn build_snapshots(
    state: &EditorState,
    area: Rect,
    show_handles: bool,
) -> Vec<TableLayoutSnapshot> {
    let mut out: Vec<TableLayoutSnapshot> = Vec::new();
    if area.height == 0 {
        return out;
    }
    let source = state.buffer.contents();
    let lines = &state.parsed.lines;
    let total = lines.len();
    let width = area.width as usize;

    let (mut virtual_idx, mut first_sub_row) =
        state.rendered_line_at_visual_row(state.scroll, width);
    let mut vis_y: usize = 0;
    let height = area.height as usize;

    // Track the most-recently-started table so multi-row tables merge into
    // one snapshot.  Keyed by the `table_byte_start` so the same table is
    // not snapshotted twice.
    //
    // The snapshot stays open through border/separator rows (which map to
    // `info_row_idx == None`) so we don't produce one snapshot per data
    // row.  We only close it when the rendered line leaves the table
    // block entirely (either a different block or end of visible range).
    let mut open_table: Option<TableLayoutSnapshot> = None;
    let mut open_table_block: Option<usize> = None; // block byte_start
                                                    // Per-data-row span accumulator.  Set when a `DataRow` sub-line is
                                                    // first encountered for that row index, extended by subsequent
                                                    // continuations, and pushed onto `snap.row_ranges` when a separator
                                                    // (or end of block) closes the row.
    let mut current_data_row_y: Option<(usize, Range<u16>)> = None;

    while vis_y < height && virtual_idx < total {
        let Some(line) = lines.get(virtual_idx) else {
            break;
        };
        let full_rows_used = state
            .parsed
            .visual_rows_for_line_at(virtual_idx, width)
            .max(1);
        let rows_used = full_rows_used.saturating_sub(first_sub_row).max(1);

        // Does this rendered line belong to a table block?
        let block_byte = state
            .parsed
            .source_map
            .original_byte_for_rendered_line(virtual_idx);
        let mut current_block: Option<usize> = None;
        // sub_kind: classification of this rendered sub-line within its
        // table block — drives everything from row-handle placement to
        // row_range accumulation.
        let mut sub_kind: Option<TableSubLineKind> = None;
        if let Some(bb) = block_byte {
            if let Some(range) = state.parsed.source_map.original_range_for_byte(bb) {
                let end = range.end.min(source.len());
                // Use `get` rather than direct indexing: when an in-line edit
                // has set `parsed_dirty`, the source-map byte ranges are
                // stale relative to the live buffer and may now land inside
                // a multi-byte UTF-8 sequence (e.g. an emoji the user just
                // typed).  Falling back to `""` skips this block's snapshot
                // for the one frame between the keystroke and the next
                // parse flush — preferable to panicking.
                let block_text = source.get(range.start..end).unwrap_or("");
                if table_edit::is_table_block(block_text) {
                    current_block = Some(range.start);
                    let own = state.parsed.source_map.rendered_lines_for_byte(range.start);
                    let sub_in_block = virtual_idx.saturating_sub(own.start);
                    let block_lines = lines.get(own.start..own.end).unwrap_or(&[]);
                    let kinds = classify_table_sub_lines(block_lines);
                    sub_kind = kinds.get(sub_in_block).copied();
                }
            }
        }

        // Close the open snapshot if we've moved into a different block.
        if current_block != open_table_block {
            if let Some(mut prev) = open_table.take() {
                if let Some((_, range)) = current_data_row_y.take() {
                    prev.row_ranges.push(range);
                }
                out.push(prev);
            }
            open_table_block = None;
            current_data_row_y = None;
        }

        if let Some(table_start) = current_block {
            // Open a new snapshot if we aren't already tracking this table.
            if open_table.is_none() {
                if let Some(info) = table_edit::find_table_at(&source, table_start) {
                    open_table = Some(TableLayoutSnapshot {
                        table_byte_start: info.start,
                        table_byte_end: info.end,
                        col_count: info.col_count,
                        row_count: info.rows.len(),
                        col_ranges: Vec::new(),
                        row_ranges: Vec::new(),
                        row_handle_col: None,
                        top_border_row: None,
                        header_row: None,
                        delete_row_handle_col: None,
                        bottom_border_row: None,
                    });
                    open_table_block = Some(table_start);
                }
            }

            if let Some(snap) = open_table.as_mut() {
                // Fill col_ranges the first time we see a row with `│`
                // characters (any header or data row produces them).
                if snap.col_ranges.is_empty() {
                    let pipes = table_layout::rendered_pipe_positions(line);
                    if pipes.len() == snap.col_count + 1 {
                        for i in 0..snap.col_count {
                            let start = pipes[i] as u16 + 1;
                            let end = pipes[i + 1] as u16;
                            snap.col_ranges.push(Range { start, end });
                        }
                    }
                }

                let y = vis_y as u16;
                let y_end = y + rows_used as u16;

                match sub_kind {
                    Some(TableSubLineKind::TopBorder) => {
                        if show_handles && snap.top_border_row.is_none() {
                            snap.top_border_row = Some(y);
                        }
                    }
                    Some(TableSubLineKind::Header { sub: 0 }) => {
                        // Header's first rendered sub-line — anchor the
                        // column-resize glyph row here.
                        if show_handles && snap.header_row.is_none() {
                            snap.header_row = Some(y);
                        }
                    }
                    Some(TableSubLineKind::Header { .. }) => {
                        // Header continuation lines (when the header
                        // wraps) don't anchor anything beyond what the
                        // first line already set up.
                    }
                    Some(TableSubLineKind::ThickSeparator)
                    | Some(TableSubLineKind::ThinSeparator)
                    | Some(TableSubLineKind::BottomBorder) => {
                        // A separator closes the current data row's
                        // span — push and reset.
                        if let Some((_, range)) = current_data_row_y.take() {
                            snap.row_ranges.push(range);
                        }
                        if matches!(sub_kind, Some(TableSubLineKind::BottomBorder))
                            && show_handles
                            && snap.bottom_border_row.is_none()
                            && snap.col_count > 1
                        {
                            // Single-column tables can't lose their last
                            // column, so we don't surface the column-delete
                            // handle for them at all.
                            snap.bottom_border_row = Some(y);
                        }
                    }
                    Some(TableSubLineKind::DataRow { row, .. }) => {
                        match current_data_row_y.as_mut() {
                            Some((existing_row, range)) if *existing_row == row => {
                                range.end = y_end;
                            }
                            _ => {
                                if let Some((_, prev_range)) = current_data_row_y.take() {
                                    snap.row_ranges.push(prev_range);
                                }
                                current_data_row_y = Some((row, y..y_end));
                            }
                        }
                    }
                    None => {}
                }

                // Row-reorder gutter column — one cell left of the outer `│`.
                if show_handles && snap.row_handle_col.is_none() && !snap.col_ranges.is_empty() {
                    let outer_left = snap.col_ranges[0].start.saturating_sub(1);
                    snap.row_handle_col = Some(outer_left.saturating_sub(1));
                }
                // Row-delete column — ON the outer right `│` itself
                // (`col_ranges.last().end`), overlaying the border for
                // each data row.  `hit_test` checks delete handles
                // before `ColumnBorder`, so a click on the `✕` cell on
                // a data row deletes; clicks at the same x on header /
                // alignment / top / bottom border rows fall through to
                // `ColumnBorder` and resize the last column.
                if show_handles
                    && snap.delete_row_handle_col.is_none()
                    && !snap.col_ranges.is_empty()
                {
                    let outer_right = snap.col_ranges.last().unwrap().end;
                    snap.delete_row_handle_col = Some(outer_right);
                }
            }
        } else {
            // Left the table block — any open snapshot was closed above.
        }

        vis_y += rows_used;
        virtual_idx += 1;
        first_sub_row = 0;
    }

    if let Some(mut prev) = open_table.take() {
        if let Some((_, range)) = current_data_row_y.take() {
            prev.row_ranges.push(range);
        }
        out.push(prev);
    }
    out
}

/// Classification of one rendered line within a table block.  Drives
/// every consumer that needs to map a sub-line index back to a logical
/// row — `build_snapshots` for hit-testing, `mouse_ops::rendered_sub_line_to_offset`
/// for click-to-cell mapping.
///
/// Replaces the fixed-pattern `table_sub_to_row_idx` math
/// because multi-row data rows (cells that wrapped) can occupy any
/// number of consecutive `│`-prefixed lines, breaking the old
/// alternating-line assumption.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum TableSubLineKind {
    /// Top border (`┌─┬─┐`).  Always sub 0.
    TopBorder,
    /// One of (potentially several) header lines.  `sub` is the
    /// 0-indexed row within the header (header always logical row 0
    /// in `TableInfo.rows`).
    Header { sub: usize },
    /// Thick separator under the header (`┝━┿━┥`).
    ThickSeparator,
    /// One of (potentially several) lines making up data row `row`
    /// (0-indexed across data rows).  Maps to `TableInfo.rows[row + 2]`
    /// since header is row 0 and alignment is row 1.  `sub` is the
    /// 0-indexed visual row within that data row.
    DataRow { row: usize, sub: usize },
    /// Thin separator between two data rows (`├─┼─┤`).
    ThinSeparator,
    /// Bottom border (`└─┴─┘`).
    BottomBorder,
}

/// Classify every rendered sub-line of a table block by inspecting its
/// leading box-drawing glyph.  Returns one `TableSubLineKind` per
/// rendered sub-line.  Length matches `lines.len()` so callers can
/// look up by `sub_in_block`.
///
/// `lines` is the slice of rendered lines that make up the table block
/// (i.e. the slice of `parsed.lines[own.start..own.end]`).
///
/// When `config.table.row_striping` is on, the renderer
/// emits a *blank* `│ ... │ ... │` line in place of the `├─┼─┤` rule
/// between data rows.  We detect those by spotting a `│`-prefixed
/// line whose only chars are `│` and whitespace, immediately after a
/// data row — and classify them as `ThinSeparator` so row-counting
/// logic (and the `cursor_block_revealed` plumbing) keep working
/// without further changes.
pub fn classify_table_sub_lines(lines: &[Line<'_>]) -> Vec<TableSubLineKind> {
    let mut out = Vec::with_capacity(lines.len());
    let mut past_thick = false;
    let mut current_header_sub = 0usize;
    let mut current_data_row = 0usize;
    let mut current_data_sub = 0usize;
    let mut prev_was_data = false;
    for line in lines {
        let first = line
            .spans
            .iter()
            .flat_map(|s| s.content.chars())
            .next()
            .unwrap_or(' ');
        let kind = match first {
            '┌' => TableSubLineKind::TopBorder,
            '┝' => {
                past_thick = true;
                prev_was_data = false;
                TableSubLineKind::ThickSeparator
            }
            '├' => {
                if prev_was_data {
                    current_data_row += 1;
                    current_data_sub = 0;
                }
                prev_was_data = false;
                TableSubLineKind::ThinSeparator
            }
            '└' => TableSubLineKind::BottomBorder,
            '│' => {
                let blank_stripe_separator =
                    past_thick && prev_was_data && is_blank_stripe_line(line);
                if blank_stripe_separator {
                    current_data_row += 1;
                    current_data_sub = 0;
                    prev_was_data = false;
                    TableSubLineKind::ThinSeparator
                } else if past_thick {
                    let sub = current_data_sub;
                    current_data_sub += 1;
                    prev_was_data = true;
                    TableSubLineKind::DataRow {
                        row: current_data_row,
                        sub,
                    }
                } else {
                    let sub = current_header_sub;
                    current_header_sub += 1;
                    TableSubLineKind::Header { sub }
                }
            }
            _ => {
                // Defensive fallback: unrecognized leading glyph.  Treat
                // as a header line so the snapshot doesn't panic, even
                // though we don't expect to hit this path.
                TableSubLineKind::Header { sub: 0 }
            }
        };
        out.push(kind);
    }
    out
}

/// Stripe-aware separator detector.  Returns `true` for lines whose
/// only characters are `│` plus NBSP (U+00A0) — the exact shape
/// produced by `Renderer::blank_table_separator`.  ASCII-space-only
/// lines explicitly do *not* qualify, so the wrap-continuation line
/// of a multi-row data row (whose short cells emit `format!(" {}{} ",
/// "", " ".repeat(pad))` — ASCII spaces only) cannot be misidentified
/// as a separator.  Data rows may also carry NBSP from code-span pad
/// cells, but they always sit alongside the renderer's ASCII cell
/// padding, which disqualifies the line here.
fn is_blank_stripe_line(line: &Line<'_>) -> bool {
    let mut saw_nbsp = false;
    for c in line.spans.iter().flat_map(|s| s.content.chars()) {
        match c {
            '│' => {}
            '\u{00A0}' => saw_nbsp = true,
            _ => return false,
        }
    }
    saw_nbsp
}

// ── Handle rendering ────────────────────────────────────────────────────────

/// Paint the row/column-button glyphs on top of each snapshot's table.  The
/// underlying rendered lines have already been drawn; this layer overlays:
///   * `⠿` in the external left gutter for each data row (row-reorder),
///   * `⠿` on the centre of each column's top-border cell (column-reorder),
///   * `⇔` on each interior `│` in the header row (column-resize).
///
/// When `cursor_table_start` is `Some(byte)`, handles paint
/// only on the snapshot whose `table_byte_start` matches — i.e. the
/// table the cursor is currently inside.  Pass `None` to paint on every
/// visible table (the legacy, always-on behaviour used by tests).
pub fn paint_handles(
    snapshots: &[TableLayoutSnapshot],
    area: Rect,
    buf: &mut TuiBuf,
    theme: &Theme,
    cursor_table_start: Option<usize>,
) {
    // Reorder (`⠿`) and resize (`⇔`) glyphs share `theme.table_handle`
    // (interactive accent); delete (`✕`) glyphs use the louder
    // `theme.table_handle_delete` so the destructive affordance stands
    // out from its neighbours.  The cursor-in-table gating
    // (`paint_handles_for_cursor_table`) keeps these from painting on
    // every visible table.
    let move_style: Style = theme.table_handle;
    let delete_style: Style = theme.table_handle_delete;
    for snap in snapshots {
        if let Some(start) = cursor_table_start {
            if snap.table_byte_start != start {
                continue;
            }
        } else {
            // No cursor table — skip painting handles entirely.
            return;
        }
        // Row-reorder glyph: one per logical data row, painted at the
        // first rendered sub-line.  Multi-row (wrapped) data rows still
        // get exactly one glyph — putting one on every wrapped sub-line
        // reads as visual noise, and the row-drag is dispatched via
        // hit-testing against the row's full y-range so the user can
        // still grab anywhere in the gutter even though the glyph only
        // shows once.
        if let Some(col) = snap.row_handle_col {
            if col < area.width {
                for y_range in &snap.row_ranges {
                    let y = y_range.start;
                    if y < area.height {
                        if let Some(cell) = buf.cell_mut((area.x + col, area.y + y)) {
                            cell.set_char(REORDER_HANDLE_GLYPH);
                            cell.set_style(move_style);
                        }
                    }
                }
            }
        }

        // Column-reorder glyphs on the top border — centred within each
        // column's content span so they overlay the `─` between the `┌`/`┬`
        // corners without disturbing them.
        if let Some(y) = snap.top_border_row {
            if y < area.height {
                for x_range in &snap.col_ranges {
                    let Some(x) = column_glyph_x(x_range) else {
                        continue;
                    };
                    if x < area.width {
                        if let Some(cell) = buf.cell_mut((area.x + x, area.y + y)) {
                            cell.set_char(REORDER_HANDLE_GLYPH);
                            cell.set_style(move_style);
                        }
                    }
                }
            }
        }

        // Column-resize glyphs on the header-row `│` borders — every
        // interior border AND the rightmost outer border (which resizes the
        // last column).  Does NOT overwrite the leftmost outer `│` since
        // there's no column to its left to resize.
        if let Some(y) = snap.header_row {
            if y < area.height {
                for x_range in &snap.col_ranges {
                    let border_x = x_range.end;
                    if border_x < area.width {
                        if let Some(cell) = buf.cell_mut((area.x + border_x, area.y + y)) {
                            cell.set_char(COLUMN_RESIZE_GLYPH);
                            cell.set_style(move_style);
                        }
                    }
                }
            }
        }

        // Row-delete glyphs ON the right outer `│` — one per data row,
        // painted at the row's first rendered sub-line.  Skips header
        // (row_idx 0) and alignment (row_idx 1) because they aren't
        // deletable; `row_ranges` already only tracks data rows so this
        // loop naturally does the right thing.  Same multi-row "one
        // glyph per logical row" rule as the row-reorder gutter.
        if let Some(col) = snap.delete_row_handle_col {
            if col < area.width {
                for y_range in &snap.row_ranges {
                    let y = y_range.start;
                    if y < area.height {
                        if let Some(cell) = buf.cell_mut((area.x + col, area.y + y)) {
                            cell.set_char(DELETE_HANDLE_GLYPH);
                            cell.set_style(delete_style);
                        }
                    }
                }
            }
        }

        // Column-delete glyphs on the bottom border — centred within
        // each column's content span (mirrors the column-reorder
        // glyphs on the top border).  `bottom_border_row` is `None`
        // for single-column tables, so no glyph is painted there.
        if let Some(y) = snap.bottom_border_row {
            if y < area.height {
                for x_range in &snap.col_ranges {
                    let Some(x) = column_glyph_x(x_range) else {
                        continue;
                    };
                    if x < area.width {
                        if let Some(cell) = buf.cell_mut((area.x + x, area.y + y)) {
                            cell.set_char(DELETE_HANDLE_GLYPH);
                            cell.set_style(delete_style);
                        }
                    }
                }
            }
        }
    }
}

// ── Drop-indicator painter ──────────────────────────────────────────────────

/// Highlight every valid drop separator for an in-progress row / column
/// drag, with the active hover-target painted at the bright accent and
/// every other valid drop at a dimmer "candidate" shade.  Runs after
/// `paint_handles` so the indicator overlays the existing border glyphs.
/// No-op when no snapshot matches the indicator's `table_byte_start`
/// (e.g. the drag's source table scrolled off-screen).
pub fn paint_drop_indicator(
    snapshots: &[TableLayoutSnapshot],
    indicator: &DropIndicator,
    area: Rect,
    buf: &mut TuiBuf,
    theme: &Theme,
) {
    let active_style: Style = theme.table_drop_indicator;
    let candidate_style: Style = theme.table_drop_target;
    match *indicator {
        DropIndicator::Row {
            table_byte_start,
            src_row_idx,
            hover_row_idx,
        } => {
            let Some(snap) = snapshots
                .iter()
                .find(|s| s.table_byte_start == table_byte_start)
            else {
                return;
            };
            if src_row_idx < 2 || snap.row_ranges.is_empty() {
                return;
            }
            // The active drop target — the separator on the side of the
            // hover row matching the drag direction.  None when the
            // pointer is over an out-of-range index.
            let active_y = active_row_drop_y(snap, src_row_idx, hover_row_idx);

            // First pass: paint every valid drop separator dimly so the
            // user sees the full set of options.  Valid separators are
            // every horizontal border between (and around) the data
            // rows EXCEPT the two adjacent to the source row — moving a
            // row to its own slot is a no-op.
            let src_data_idx = src_row_idx - 2;
            let Some(first) = snap.col_ranges.first() else {
                return;
            };
            let Some(last) = snap.col_ranges.last() else {
                return;
            };
            let x_start = first.start.saturating_sub(1);
            let x_end = last.end;
            let x_max = area.width;
            for (i, y_range) in snap.row_ranges.iter().enumerate() {
                // Separator above this data row (between row i-1 and i).
                let above = y_range.start.saturating_sub(1);
                // Separator below this data row (between row i and i+1
                // or above the bottom border).
                let below = y_range.end;
                for &y in &[above, below] {
                    if y >= area.height {
                        continue;
                    }
                    // Skip the separators that bound the source row
                    // (a drop there would be a no-op).
                    if (i == src_data_idx && (y == above || y == below))
                        || (i + 1 == src_data_idx && y == below)
                        || (i == src_data_idx + 1 && y == above)
                    {
                        continue;
                    }
                    let style = if Some(y) == active_y {
                        active_style
                    } else {
                        candidate_style
                    };
                    paint_horizontal_drop(
                        buf,
                        area.x + x_start,
                        area.x + x_end,
                        area.x + x_max,
                        area.y + y,
                        style,
                    );
                }
            }
        }
        DropIndicator::Column {
            table_byte_start,
            src_col_idx,
            hover_col_idx,
        } => {
            let Some(snap) = snapshots
                .iter()
                .find(|s| s.table_byte_start == table_byte_start)
            else {
                return;
            };
            if snap.col_ranges.is_empty() {
                return;
            }
            let active_x = active_column_drop_x(snap, src_col_idx, hover_col_idx);
            // Vertical span shared across every candidate.
            let y_top = snap
                .top_border_row
                .or_else(|| snap.row_ranges.first().map(|r| r.start.saturating_sub(2)))
                .unwrap_or(0);
            let y_bot = snap
                .row_ranges
                .last()
                .map(|r| r.end)
                .unwrap_or(area.height.saturating_sub(1));
            let y_max = area.height;
            // Every column-border (interior + the two outer borders) is
            // a candidate drop point, except the two flanking the source
            // column.
            let mut borders: Vec<u16> = Vec::with_capacity(snap.col_ranges.len() + 1);
            if let Some(first) = snap.col_ranges.first() {
                borders.push(first.start.saturating_sub(1));
            }
            for r in &snap.col_ranges {
                borders.push(r.end);
            }
            for (i, &x) in borders.iter().enumerate() {
                if x >= area.width {
                    continue;
                }
                // Skip borders adjacent to the source column.
                if i == src_col_idx || i == src_col_idx + 1 {
                    continue;
                }
                let style = if Some(x) == active_x {
                    active_style
                } else {
                    candidate_style
                };
                paint_vertical_drop(
                    buf,
                    area.y + y_top,
                    area.y + y_bot,
                    area.y + y_max,
                    area.x + x,
                    style,
                );
            }
        }
        DropIndicator::ColumnBorder {
            table_byte_start,
            x,
        } => {
            let Some(snap) = snapshots
                .iter()
                .find(|s| s.table_byte_start == table_byte_start)
            else {
                return;
            };
            if x >= area.width {
                return;
            }
            let y_top = snap
                .top_border_row
                .or_else(|| snap.row_ranges.first().map(|r| r.start.saturating_sub(2)))
                .unwrap_or(0);
            let y_bot = snap
                .row_ranges
                .last()
                .map(|r| r.end)
                .unwrap_or(area.height.saturating_sub(1));
            let y_max = area.height;
            for y in y_top..=y_bot {
                if y >= y_max {
                    break;
                }
                if let Some(cell) = buf.cell_mut((area.x + x, area.y + y)) {
                    cell.set_char(DROP_COL_GLYPH);
                    cell.set_style(active_style);
                }
            }
        }
    }
}

/// Y-coordinate of the active drop separator for a row drag, or `None`
/// when the pointer is over the source row itself (no drop target).
fn active_row_drop_y(
    snap: &TableLayoutSnapshot,
    src_row_idx: usize,
    hover_row_idx: usize,
) -> Option<u16> {
    if hover_row_idx < 2 || hover_row_idx == src_row_idx {
        return None;
    }
    let data_idx = hover_row_idx - 2;
    let y_range = snap.row_ranges.get(data_idx)?;
    if src_row_idx > hover_row_idx {
        Some(y_range.start.saturating_sub(1))
    } else {
        Some(y_range.end)
    }
}

/// X-coordinate of the active drop separator for a column drag, or
/// `None` when the pointer is on the source column.
fn active_column_drop_x(
    snap: &TableLayoutSnapshot,
    src_col_idx: usize,
    hover_col_idx: usize,
) -> Option<u16> {
    if hover_col_idx == src_col_idx {
        return None;
    }
    if src_col_idx > hover_col_idx {
        snap.col_ranges
            .get(hover_col_idx)
            .map(|r| r.start.saturating_sub(1))
    } else {
        snap.col_ranges.get(hover_col_idx).map(|r| r.end)
    }
}

/// Draw a heavy horizontal rule across `[x_start, x_end]` at row `y`,
/// clipped at `x_max`.  Helper for the row-drag drop painter.
fn paint_horizontal_drop(
    buf: &mut TuiBuf,
    x_start: u16,
    x_end: u16,
    x_max: u16,
    y: u16,
    style: Style,
) {
    for x in x_start..=x_end {
        if x >= x_max {
            break;
        }
        if let Some(cell) = buf.cell_mut((x, y)) {
            cell.set_char(DROP_ROW_GLYPH);
            cell.set_style(style);
        }
    }
}

/// Draw a heavy vertical rule down `[y_top, y_bot]` at column `x`,
/// clipped at `y_max`.  Helper for the column-drag drop painter.
fn paint_vertical_drop(buf: &mut TuiBuf, y_top: u16, y_bot: u16, y_max: u16, x: u16, style: Style) {
    for y in y_top..=y_bot {
        if y >= y_max {
            break;
        }
        if let Some(cell) = buf.cell_mut((x, y)) {
            cell.set_char(DROP_COL_GLYPH);
            cell.set_style(style);
        }
    }
}

// ── Tests ───────────────────────────────────────────────────────────────────

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

    fn snap(col_ranges: Vec<Range<u16>>, row_ranges: Vec<Range<u16>>) -> TableLayoutSnapshot {
        TableLayoutSnapshot {
            table_byte_start: 0,
            table_byte_end: 100,
            col_count: col_ranges.len(),
            row_count: HEADER_ROWS + row_ranges.len(),
            col_ranges,
            row_ranges,
            row_handle_col: None,
            top_border_row: None,
            header_row: None,
            delete_row_handle_col: None,
            bottom_border_row: None,
        }
    }

    #[test]
    fn hit_test_returns_cell_for_click_in_content() {
        let s = snap(vec![1..4, 5..8], vec![3..4, 4..5]);
        let hit = s.hit_test(2, 3).unwrap();
        assert_eq!(
            hit,
            TableHit::Cell {
                row_idx: 2,
                col_idx: 0
            }
        );
    }

    #[test]
    fn hit_test_returns_border_for_click_on_pipe() {
        let s = snap(vec![1..4, 5..8], vec![3..4, 4..5]);
        // Interior border sits at col_ranges[0].end == 4.
        let hit = s.hit_test(4, 3).unwrap();
        assert_eq!(hit, TableHit::ColumnBorder { col_idx: 1 });
    }

    #[test]
    fn hit_test_border_tolerates_one_cell_miss() {
        // Interior border at col_ranges[0].end = 4; click at col 5 should
        // still resolve to ColumnBorder because the border ±1 window hits.
        //
        // NB: col 5 is also col_ranges[1].start; row-on-table check makes
        // sure we only classify as a border when we're vertically on the
        // table.  We set row 3 which is in row_ranges[0].
        let s = snap(vec![1..4, 5..8], vec![3..4, 4..5]);
        let hit = s.hit_test(5, 3).unwrap();
        // Could be classified as either Border (for ±1 of pipe at col 4)
        // or Cell(col 1).  Borders take precedence.
        assert!(matches!(
            hit,
            TableHit::ColumnBorder { col_idx: 1 } | TableHit::Cell { col_idx: 1, .. }
        ));
    }

    #[test]
    fn hit_test_row_handle_when_handles_enabled() {
        let mut s = snap(vec![5..8], vec![3..4]);
        s.row_handle_col = Some(2);
        let hit = s.hit_test(2, 3).unwrap();
        assert_eq!(hit, TableHit::RowHandle { row_idx: 2 });
    }

    #[test]
    fn hit_test_column_handle_when_handles_enabled() {
        let mut s = snap(vec![5..8, 9..12], vec![3..4]);
        s.top_border_row = Some(1);
        let hit = s.hit_test(6, 1).unwrap();
        assert_eq!(hit, TableHit::ColumnHandle { col_idx: 0 });
    }

    #[test]
    fn hit_test_border_in_header_row_when_resize_glyphs_on() {
        // Header at y=2, interior border at col_ranges[0].end=8.
        let mut s = snap(vec![5..8, 9..12], vec![3..4]);
        s.top_border_row = Some(1);
        s.header_row = Some(2);
        let hit = s.hit_test(8, 2).unwrap();
        assert_eq!(hit, TableHit::ColumnBorder { col_idx: 1 });
    }

    /// The `├─┼─┤` separator between two data rows sits in the one-row gap
    /// between consecutive `row_ranges` entries.  A gutter click there used
    /// to classify as nothing (falling through to the inert left outer
    /// border), which is what made grabbing the `⠿` handle feel like a coin
    /// flip on a striped or separator-ruled table.
    #[test]
    fn hit_test_row_handle_covers_the_separator_between_rows() {
        let mut s = snap(vec![5..8], vec![3..4, 5..6]);
        s.row_handle_col = Some(2);
        // y=4 is the separator between data row 0 (y=3) and row 1 (y=5).
        assert_eq!(s.hit_test(2, 4), Some(TableHit::RowHandle { row_idx: 2 }));
        // The bounding separators count too: above the first row and below
        // the last.
        assert_eq!(s.hit_test(2, 2), Some(TableHit::RowHandle { row_idx: 2 }));
        assert_eq!(s.hit_test(2, 6), Some(TableHit::RowHandle { row_idx: 3 }));
        // …but the header / alignment rows above the band do not.
        assert_eq!(s.hit_test(2, 1), None);
    }

    /// The `┬` vertices of the top border are exactly where a user aiming
    /// at a column-reorder handle tends to press.  They must resolve to an
    /// adjacent column rather than falling through to `ColumnBorder`, which
    /// would silently start a resize instead of a reorder.
    #[test]
    fn hit_test_column_handle_covers_the_border_vertices() {
        let mut s = snap(vec![5..8, 9..12], vec![3..4]);
        s.top_border_row = Some(1);
        // `┌` at x=4, `┬` at x=8, `┐` at x=12.
        assert_eq!(
            s.hit_test(4, 1),
            Some(TableHit::ColumnHandle { col_idx: 0 })
        );
        assert_eq!(
            s.hit_test(8, 1),
            Some(TableHit::ColumnHandle { col_idx: 0 })
        );
        assert_eq!(
            s.hit_test(12, 1),
            Some(TableHit::ColumnHandle { col_idx: 1 })
        );
        // Past the table's right edge the column-handle band ends and the
        // right border's `±1` resize window takes over again.
        assert_eq!(
            s.hit_test(13, 1),
            Some(TableHit::ColumnBorder { col_idx: 2 })
        );
    }

    /// The column-delete `✕` is a destructive one-shot, so its hitbox is
    /// the painted glyph ±1 — not the column's whole width.  That keeps the
    /// bottom border usable as a resize target and stops a click aimed at a
    /// border from dropping a column.
    #[test]
    fn hit_test_column_delete_is_scoped_to_the_glyph() {
        let mut s = snap(vec![1..8, 9..16], vec![3..4]);
        s.bottom_border_row = Some(5);
        // Glyph centre of column 0 is x = 1 + 7/2 = 4.
        assert_eq!(
            s.hit_test(4, 5),
            Some(TableHit::DeleteColumnHandle { col_idx: 0 })
        );
        assert_eq!(
            s.hit_test(5, 5),
            Some(TableHit::DeleteColumnHandle { col_idx: 0 })
        );
        // Well away from the glyph: the interior border at x=8 resizes.
        assert_eq!(
            s.hit_test(8, 5),
            Some(TableHit::ColumnBorder { col_idx: 1 })
        );
    }

    #[test]
    fn hit_test_returns_none_outside_any_region() {
        let s = snap(vec![1..4, 5..8], vec![3..4]);
        assert!(s.hit_test(100, 100).is_none());
    }

    #[test]
    fn hit_test_returns_delete_row_handle_when_handle_set() {
        // delete_row_handle_col sits ON the outer right `│`.  With
        // col_ranges last.end = 8, handle_col = 8 (same column as the
        // border).  For data rows that click resolves to delete; the
        // header / alignment rows have no row_range entry and fall
        // through to ColumnBorder.
        let mut s = snap(vec![1..4, 5..8], vec![3..4, 4..5]);
        s.delete_row_handle_col = Some(8);
        let hit = s.hit_test(8, 3).unwrap();
        assert_eq!(hit, TableHit::DeleteRowHandle { row_idx: 2 });
        let hit = s.hit_test(8, 4).unwrap();
        assert_eq!(hit, TableHit::DeleteRowHandle { row_idx: 3 });
    }

    /// With the `✕` glyph painted ON the right border, the cell just
    /// inside the border (`last.end - 1`) becomes the resize target on
    /// data rows.  This documents the new contract: the explicit `✕`
    /// cell deletes; the cell next to it still resizes via the
    /// `ColumnBorder ±1` tolerance.
    #[test]
    fn hit_test_cell_just_inside_right_border_still_resizes() {
        let mut s = snap(vec![1..4, 5..8], vec![3..4]);
        s.delete_row_handle_col = Some(8); // same as last.end
                                           // x=7 is one cell inside the right `│` at x=8 — within the
                                           // ColumnBorder ±1 window.
        let hit = s.hit_test(7, 3).unwrap();
        assert_eq!(hit, TableHit::ColumnBorder { col_idx: 2 });
    }

    /// On non-data rows (header / alignment / top / bottom border),
    /// the same x as the delete glyph has no `row_range` match, so the
    /// click falls through to `ColumnBorder`.  This keeps the right
    /// column resizable via the header `⇔` glyph (and via clicks on
    /// the surrounding border rows).
    #[test]
    fn hit_test_right_border_on_non_data_row_still_resizes() {
        let mut s = snap(vec![1..4, 5..8], vec![5..6]); // single data row at y=5
        s.delete_row_handle_col = Some(8);
        // y=3 is in the header / alignment region (between
        // top_border heuristic at y=3 and the data row at y=5) —
        // outside row_ranges, so the delete-handle check finds no
        // row and the click falls through to ColumnBorder.
        assert_eq!(
            s.hit_test(8, 3),
            Some(TableHit::ColumnBorder { col_idx: 2 })
        );
    }

    /// Disabled delete handles (`None` field) leave the original
    /// hit-test chain unchanged — the right border on a data row
    /// resolves to `ColumnBorder`, as it did before delete handles
    /// existed.  This is what `config.table.show_buttons = false`
    /// must guarantee.
    #[test]
    fn hit_test_falls_through_when_delete_handles_disabled() {
        let s = snap(vec![1..4, 5..8], vec![3..4]);
        // (8, 3) is the right `│`; with no delete handle, it's just a
        // border click.
        assert_eq!(
            s.hit_test(8, 3),
            Some(TableHit::ColumnBorder { col_idx: 2 })
        );
    }

    #[test]
    fn hit_test_returns_delete_column_handle_on_bottom_border() {
        let mut s = snap(vec![1..4, 5..8], vec![3..4]);
        s.bottom_border_row = Some(5);
        // Click in middle of column 0's content range (cols 1..4).
        let hit = s.hit_test(2, 5).unwrap();
        assert_eq!(hit, TableHit::DeleteColumnHandle { col_idx: 0 });
        // Click in middle of column 1's content range (cols 5..8).
        let hit = s.hit_test(6, 5).unwrap();
        assert_eq!(hit, TableHit::DeleteColumnHandle { col_idx: 1 });
    }

    /// `row_ranges` only tracks data rows, so the delete-row check
    /// can't fire on a y outside any data-row range.  Combined with
    /// `hit_test_right_border_on_non_data_row_still_resizes`, this
    /// ensures the header / alignment rows on the same border column
    /// keep their resize behaviour.
    #[test]
    fn hit_test_delete_row_handle_skips_header_and_alignment() {
        let mut s = snap(vec![1..4, 5..8], vec![5..6]); // single data row at y=5
        s.delete_row_handle_col = Some(8);
        // The next assertion — that y=2 produces ColumnBorder, not
        // DeleteRowHandle — is the actual contract.  See
        // `hit_test_right_border_on_non_data_row_still_resizes`.
        assert_ne!(
            s.hit_test(8, 2),
            Some(TableHit::DeleteRowHandle { row_idx: 2 })
        );
    }

    /// `build_snapshots_cached` must leave `snapshots` untouched when the
    /// cache key matches the previous frame.  The `parsed_version` stays
    /// constant, scroll/area/show_handles don't change — so the second
    /// call is a no-op.
    #[test]
    fn build_snapshots_cached_reuses_output_when_key_matches() {
        use crate::config::Theme;
        use crate::document::Buffer;
        use crate::editor::EditorState;

        let theme: &'static Theme = Box::leak(Box::new(Theme::default()));
        let src = "| a | b |\n|---|---|\n| 1 | 2 |\n";
        let state = EditorState::new(Buffer::from_str(src), theme);
        let area = Rect::new(0, 0, 40, 10);

        let mut snapshots: Vec<TableLayoutSnapshot> = Vec::new();
        let mut key: Option<(usize, Rect, u64, bool)> = None;
        build_snapshots_cached(&state, area, false, &mut snapshots, &mut key);
        let first_len = snapshots.len();
        assert!(first_len > 0, "expected at least one snapshot");

        // Tag the snapshots so we can detect whether they get rebuilt.
        snapshots[0].col_count = 999;
        build_snapshots_cached(&state, area, false, &mut snapshots, &mut key);
        assert_eq!(
            snapshots[0].col_count, 999,
            "snapshots must not be rebuilt when cache key matches",
        );

        // Changing show_handles busts the cache.
        build_snapshots_cached(&state, area, true, &mut snapshots, &mut key);
        assert_ne!(
            snapshots[0].col_count, 999,
            "snapshots must be rebuilt when show_handles changes",
        );
    }

    #[test]
    fn classify_table_sub_lines_simple_table() {
        use ratatui::text::Span;
        let lines = vec![
            Line::from(Span::raw("┌──┬──┐")),
            Line::from(Span::raw("│ a│ b│")),
            Line::from(Span::raw("┝━━┿━━┥")),
            Line::from(Span::raw("│ 1│ 2│")),
            Line::from(Span::raw("├──┼──┤")),
            Line::from(Span::raw("│ 3│ 4│")),
            Line::from(Span::raw("└──┴──┘")),
        ];
        let kinds = classify_table_sub_lines(&lines);
        assert_eq!(kinds[0], TableSubLineKind::TopBorder);
        assert_eq!(kinds[1], TableSubLineKind::Header { sub: 0 });
        assert_eq!(kinds[2], TableSubLineKind::ThickSeparator);
        assert_eq!(kinds[3], TableSubLineKind::DataRow { row: 0, sub: 0 });
        assert_eq!(kinds[4], TableSubLineKind::ThinSeparator);
        assert_eq!(kinds[5], TableSubLineKind::DataRow { row: 1, sub: 0 });
        assert_eq!(kinds[6], TableSubLineKind::BottomBorder);
    }

    #[test]
    fn classify_table_sub_lines_multirow_data_row() {
        use ratatui::text::Span;
        // Data row 0 wraps to two lines, data row 1 stays one line.
        let lines = vec![
            Line::from(Span::raw("┌──┬──┐")),
            Line::from(Span::raw("│ a│ b│")),
            Line::from(Span::raw("┝━━┿━━┥")),
            Line::from(Span::raw("│ 1│ x│")),
            Line::from(Span::raw("│  │ y│")),
            Line::from(Span::raw("├──┼──┤")),
            Line::from(Span::raw("│ 3│ 4│")),
            Line::from(Span::raw("└──┴──┘")),
        ];
        let kinds = classify_table_sub_lines(&lines);
        assert_eq!(kinds[3], TableSubLineKind::DataRow { row: 0, sub: 0 });
        assert_eq!(kinds[4], TableSubLineKind::DataRow { row: 0, sub: 1 });
        assert_eq!(kinds[5], TableSubLineKind::ThinSeparator);
        assert_eq!(kinds[6], TableSubLineKind::DataRow { row: 1, sub: 0 });
    }

    /// The renderer's `blank_table_separator` line uses
    /// NBSP-padded cells, distinguishing it from a wrap-continuation
    /// line whose short cells are ASCII-space-padded.  classify must
    /// recognise the NBSP-padded `│ … │ … │` line as ThinSeparator
    /// and treat the next `│`-prefixed line as the next data row.
    #[test]
    fn classify_table_sub_lines_blank_stripe_separator() {
        use ratatui::text::Span;
        // NBSP between pipes for the stripe separator (line index 4).
        // Wrap continuation (line index 5 here would be ASCII-padded
        // — but in this fixture the row wraps differently).  Just
        // check the stripe-separator detection.
        let lines = vec![
            Line::from(Span::raw("┌──┬──┐")),
            Line::from(Span::raw("│ a│ b│")),
            Line::from(Span::raw("┝━━┿━━┥")),
            Line::from(Span::raw("│ 1│ 2│")),
            Line::from(Span::raw(
                "│\u{00A0}\u{00A0}\u{00A0}│\u{00A0}\u{00A0}\u{00A0}│",
            )),
            Line::from(Span::raw("│ 3│ 4│")),
            Line::from(Span::raw("└──┴──┘")),
        ];
        let kinds = classify_table_sub_lines(&lines);
        assert_eq!(kinds[3], TableSubLineKind::DataRow { row: 0, sub: 0 });
        assert_eq!(kinds[4], TableSubLineKind::ThinSeparator);
        assert_eq!(kinds[5], TableSubLineKind::DataRow { row: 1, sub: 0 });
    }

    /// Counterpart to the NBSP test: a wrap-continuation line whose
    /// short cells are ASCII-space-padded must NOT be misclassified
    /// as a stripe separator — both cells continue the same data row.
    #[test]
    fn classify_table_sub_lines_ascii_space_padded_continuation_stays_data() {
        use ratatui::text::Span;
        let lines = vec![
            Line::from(Span::raw("┌──┬──┐")),
            Line::from(Span::raw("│ a│ b│")),
            Line::from(Span::raw("┝━━┿━━┥")),
            Line::from(Span::raw("│hi│ y│")),
            Line::from(Span::raw("│  │  │")), // both cells empty on continuation
            Line::from(Span::raw("└──┴──┘")),
        ];
        let kinds = classify_table_sub_lines(&lines);
        assert_eq!(kinds[3], TableSubLineKind::DataRow { row: 0, sub: 0 });
        assert_eq!(kinds[4], TableSubLineKind::DataRow { row: 0, sub: 1 });
    }
}