croft-software 0.1.758

VSCode-style TUI written in Rust
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
use globset::{Glob, GlobSet, GlobSetBuilder};
use grep_regex::{RegexMatcher, RegexMatcherBuilder};
use grep_searcher::{BinaryDetection, MmapChoice, Searcher, SearcherBuilder, Sink, SinkMatch};
use ignore::{WalkBuilder, WalkState};
use ratatui::{
    buffer::Buffer,
    layout::Rect,
    style::{Color, Modifier, Style},
    text::{Line, Span},
    widgets::{Block, Borders, Widget},
};
use std::collections::HashSet;
use std::path::{Path, PathBuf};
use std::sync::Arc;
use std::sync::atomic::{AtomicBool, Ordering};

const MAX_LINE_LEN: usize = 200;

/// Per-channel lift applied to the theme background to fill an idle input row,
/// mirroring VS Code's `input.background` sitting a touch lighter than the
/// panel. On Black (#000) this lands a neutral #141414; on Croft Dark
/// (#1e222e) a lighter navy. Single source of truth for both helpers below.
const FIELD_FILL_LIFT_IDLE: u8 = 0x14;
/// The focused input row lifts further still, standing in for VS Code's
/// `focusBorder` ring — in a cell grid we cannot draw a 1px sub-cell edge, so
/// the brighter fill plus the accent left-bar carry the focus signal.
const FIELD_FILL_LIFT_FOCUS: u8 = 0x22;
/// The dim left-edge bar an unfocused field wears, so every input reads as a
/// touch target even before it gains the accent bar on focus.
const FIELD_BAR_IDLE: Color = Color::Rgb(0x2b, 0x31, 0x3b);

/// Lift each channel of `(r, g, b)` by `d`, saturating. Used to derive the
/// input fill from the active theme background.
fn lift_bg((r, g, b): (u8, u8, u8), d: u8) -> Color {
    Color::Rgb(
        r.saturating_add(d),
        g.saturating_add(d),
        b.saturating_add(d),
    )
}

/// Draw a faint hairline rule from column `x0` to `x1` (inclusive) on row `y`,
/// used to separate the stacked Search inputs into distinct groups. A terminal
/// cannot gap by less than one whole cell, so the divider turns the separator
/// row into a deliberate rule instead of dead space. It is dimmer than, and
/// spans less width than, the full results separator so the two never read as
/// the same element.
fn draw_field_divider(buf: &mut Buffer, x0: u16, x1: u16, y: u16, theme: crate::theme::Theme) {
    let style = Style::default().fg(theme.ui(FIELD_BAR_IDLE));
    for x in x0..=x1 {
        buf.set_string(x, y, "─", style);
    }
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub struct SearchHit {
    pub path: PathBuf,
    /// 1-indexed line number, the way humans / editors talk about lines.
    pub line_no: usize,
    /// The matched line, with surrounding whitespace trimmed and length capped.
    pub line_text: String,
}

/// Mode toggles that drive `search_workspace`. Mirror VS Code's three
/// search input toggles, in the same left-to-right order:
///   - `case_sensitive` (Aa)
///   - `whole_word` (ab with underline)
///   - `use_regex` (.*)
///
/// All-false matches the original case-insensitive substring behaviour.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Default)]
pub struct SearchOpts {
    pub case_sensitive: bool,
    pub whole_word: bool,
    pub use_regex: bool,
}

fn build_matcher(query: &str, opts: SearchOpts) -> Option<RegexMatcher> {
    if query.is_empty() {
        return None;
    }
    let mut b = RegexMatcherBuilder::new();
    b.case_insensitive(!opts.case_sensitive);
    b.word(opts.whole_word);
    b.fixed_strings(!opts.use_regex);
    b.build(query).ok()
}

fn build_searcher() -> Searcher {
    SearcherBuilder::new()
        .line_number(true)
        .binary_detection(BinaryDetection::quit(b'\x00'))
        .memory_map(MmapChoice::never())
        .build()
}

/// Compiled `files to include` / `files to exclude` globs, mirroring VS
/// Code's two Search-sidebar glob inputs. Both inputs take a comma-separated
/// list of globs. A bare pattern with no path separator (e.g. `*.rs`) is
/// treated as match-at-any-depth (`**/*.rs`), exactly like VS Code, so users
/// don't have to remember to prefix `**/`.
#[derive(Clone, Debug, Default)]
pub struct PathFilter {
    include: Option<GlobSet>,
    exclude: Option<GlobSet>,
}

/// Build a `GlobSet` from a comma-separated glob list, returning `None` when
/// the trimmed input is empty (meaning "no filter"). Invalid individual globs
/// are skipped silently so one typo doesn't disable the whole filter.
fn build_glob_set(raw: &str) -> Option<GlobSet> {
    let mut builder = GlobSetBuilder::new();
    let mut any = false;
    for part in raw.split(',') {
        let pat = part.trim();
        if pat.is_empty() {
            continue;
        }
        // VS Code convention: a pattern without a path separator matches at
        // any depth, so `*.rs` finds Rust files in every subdirectory.
        let expanded = if pat.contains('/') {
            pat.to_string()
        } else {
            format!("**/{pat}")
        };
        if let Ok(g) = Glob::new(&expanded) {
            builder.add(g);
            any = true;
        }
    }
    if !any {
        return None;
    }
    builder.build().ok()
}

impl PathFilter {
    /// Compile a filter from the raw include/exclude input strings. An empty
    /// (or all-whitespace) string disables that side of the filter.
    pub fn new(include: &str, exclude: &str) -> Self {
        Self {
            include: build_glob_set(include),
            exclude: build_glob_set(exclude),
        }
    }

    /// True when no include and no exclude globs are active, so the walker can
    /// skip the per-file relative-path work entirely.
    pub fn is_empty(&self) -> bool {
        self.include.is_none() && self.exclude.is_none()
    }

    /// Decide whether `path` (under `root`) should be searched. Matching is
    /// done against the path relative to `root` (falling back to the full path
    /// when it isn't under `root`). Include wins first: if an include set is
    /// present, the path must match it; then any exclude match drops the path.
    pub fn allows(&self, root: &Path, path: &Path) -> bool {
        let rel = path.strip_prefix(root).unwrap_or(path);
        if let Some(inc) = &self.include
            && !inc.is_match(rel)
        {
            return false;
        }
        if let Some(exc) = &self.exclude
            && exc.is_match(rel)
        {
            return false;
        }
        true
    }
}

struct HitSink<'a> {
    path: PathBuf,
    batch: &'a mut Vec<SearchHit>,
    cancel: Option<Arc<AtomicBool>>,
}

impl<'a> Sink for HitSink<'a> {
    type Error = std::io::Error;
    fn matched(
        &mut self,
        _searcher: &Searcher,
        mat: &SinkMatch<'_>,
    ) -> Result<bool, std::io::Error> {
        if let Some(c) = self.cancel.as_ref()
            && c.load(Ordering::Relaxed)
        {
            return Ok(false);
        }
        let line_no = mat.line_number().unwrap_or(0) as usize;
        let line_str = std::str::from_utf8(mat.bytes()).unwrap_or("");
        let stripped = line_str.trim_end_matches(['\r', '\n']);
        let trimmed = stripped.trim_start();
        let cut = if trimmed.chars().count() > MAX_LINE_LEN {
            // Cut plainly, no trailing ellipsis marker (no ellipsis anywhere).
            trimmed.chars().take(MAX_LINE_LEN).collect()
        } else {
            trimmed.to_string()
        };
        self.batch.push(SearchHit {
            path: self.path.clone(),
            line_no,
            line_text: cut,
        });
        Ok(true)
    }
}

/// Stream every match for `query` under `root` into `on_batch`, one batch
/// per file. Honours `.gitignore` and skips binaries via NUL detection.
/// Walks in parallel using `WalkBuilder::build_parallel` and uses
/// `grep-searcher` + `grep-regex` (the same engine that powers ripgrep).
/// Set `cancel` to abort an in-flight scan.
pub fn search_workspace_streaming<F>(
    root: &Path,
    query: &str,
    opts: SearchOpts,
    cancel: &Arc<AtomicBool>,
    on_batch: F,
) where
    F: FnMut(Vec<SearchHit>) + Send + Clone,
{
    search_workspace_streaming_skipping(root, query, opts, cancel, &HashSet::new(), on_batch);
}

/// Like `search_workspace_streaming` but skips any walked file whose
/// canonical path is in `skip`. Used for dirty-aware search: files open in
/// unsaved editors are searched from their in-memory buffer instead, so the
/// stale on-disk copy must not also contribute (duplicate / outdated) hits.
pub fn search_workspace_streaming_skipping<F>(
    root: &Path,
    query: &str,
    opts: SearchOpts,
    cancel: &Arc<AtomicBool>,
    skip: &HashSet<PathBuf>,
    on_batch: F,
) where
    F: FnMut(Vec<SearchHit>) + Send + Clone,
{
    search_workspace_streaming_filtered(
        root,
        query,
        opts,
        cancel,
        skip,
        &PathFilter::default(),
        on_batch,
    );
}

/// Like `search_workspace_streaming_skipping` but also honours a `PathFilter`
/// (VS Code's "files to include" / "files to exclude" glob inputs). A file is
/// searched only when `filter.allows(root, path)` returns true.
pub fn search_workspace_streaming_filtered<F>(
    root: &Path,
    query: &str,
    opts: SearchOpts,
    cancel: &Arc<AtomicBool>,
    skip: &HashSet<PathBuf>,
    filter: &PathFilter,
    on_batch: F,
) where
    F: FnMut(Vec<SearchHit>) + Send + Clone,
{
    let q = query.trim();
    if q.is_empty() {
        return;
    }
    let Some(matcher) = build_matcher(q, opts) else {
        return;
    };
    let walker = WalkBuilder::new(root)
        .git_ignore(true)
        .hidden(true)
        .build_parallel();
    let root = root.to_path_buf();
    walker.run(|| {
        let matcher = matcher.clone();
        let cancel = cancel.clone();
        let skip = skip.clone();
        let filter = filter.clone();
        let root = root.clone();
        let mut on_batch = on_batch.clone();
        Box::new(move |entry| {
            if cancel.load(Ordering::Relaxed) {
                return WalkState::Quit;
            }
            let entry = match entry {
                Ok(e) => e,
                Err(_) => return WalkState::Continue,
            };
            if !entry.file_type().is_some_and(|t| t.is_file()) {
                return WalkState::Continue;
            }
            let path = entry.path();
            // Apply the include/exclude glob filter before any IO on the file.
            if !filter.is_empty() && !filter.allows(&root, path) {
                return WalkState::Continue;
            }
            // Dirty files are searched from their in-memory buffer elsewhere;
            // skip the stale disk copy. Only pay the canonicalize syscall when
            // there is actually something to skip.
            if !skip.is_empty() && path.canonicalize().is_ok_and(|canon| skip.contains(&canon)) {
                return WalkState::Continue;
            }
            let mut batch: Vec<SearchHit> = Vec::new();
            {
                let mut sink = HitSink {
                    path: path.to_path_buf(),
                    batch: &mut batch,
                    cancel: Some(cancel.clone()),
                };
                let mut searcher = build_searcher();
                let _ = searcher.search_path(&matcher, path, &mut sink);
            }
            if !batch.is_empty() {
                on_batch(batch);
            }
            WalkState::Continue
        })
    });
}

/// Aggregating wrapper around `search_workspace_streaming`. Returns every
/// match - no arbitrary cap. Honours `.gitignore`. Order is non-deterministic
/// because the walker is parallel; callers that need ordering must sort.
pub fn search_workspace(root: &Path, query: &str, opts: SearchOpts) -> Vec<SearchHit> {
    let cancel = Arc::new(AtomicBool::new(false));
    let collected: Arc<std::sync::Mutex<Vec<SearchHit>>> =
        Arc::new(std::sync::Mutex::new(Vec::new()));
    let collected_for_cb = collected.clone();
    search_workspace_streaming(root, query, opts, &cancel, move |batch| {
        if let Ok(mut g) = collected_for_cb.lock() {
            g.extend(batch);
        }
    });
    Arc::try_unwrap(collected)
        .map(|m| m.into_inner().unwrap_or_default())
        .unwrap_or_default()
}

/// Split `line` into `(segment, is_match)` runs against `needle` honouring
/// the supplied `opts`, so highlights in result rows / the editor stay
/// consistent with what `collect_matches_in_text` would actually match.
/// Concatenating the segments reproduces the original line byte-for-byte.
/// Empty needle, no match, or invalid regex all return the whole line as
/// a single non-match segment.
pub fn split_for_highlight(line: &str, needle: &str, opts: SearchOpts) -> Vec<(String, bool)> {
    if needle.is_empty() {
        return vec![(line.to_string(), false)];
    }
    if opts.use_regex {
        return split_for_highlight_regex(line, needle, opts);
    }
    let (haystack, search_for): (String, String) = if opts.case_sensitive {
        (line.to_string(), needle.to_string())
    } else {
        (line.to_lowercase(), needle.to_lowercase())
    };
    // If lowercasing changed the byte length (rare Unicode edge case) we
    // can't safely map `haystack` indices back into `line`, so bail out
    // without highlights.
    if haystack.len() != line.len() || search_for.is_empty() {
        return vec![(line.to_string(), false)];
    }
    let mut out: Vec<(String, bool)> = Vec::new();
    let mut last = 0usize;
    let mut start = 0usize;
    while let Some(rel) = haystack[start..].find(&search_for) {
        let abs = start + rel;
        let end = abs + search_for.len();
        if opts.whole_word && !is_whole_word_match(&haystack, abs, end) {
            start = end;
            continue;
        }
        if abs > last {
            out.push((line[last..abs].to_string(), false));
        }
        out.push((line[abs..end].to_string(), true));
        last = end;
        start = end;
    }
    if last < line.len() {
        out.push((line[last..].to_string(), false));
    }
    if out.is_empty() {
        out.push((line.to_string(), false));
    }
    out
}

fn split_for_highlight_regex(line: &str, needle: &str, opts: SearchOpts) -> Vec<(String, bool)> {
    let mut pattern = String::new();
    if !opts.case_sensitive {
        pattern.push_str("(?i)");
    }
    if opts.whole_word {
        pattern.push_str("\\b(?:");
        pattern.push_str(needle);
        pattern.push_str(")\\b");
    } else {
        pattern.push_str(needle);
    }
    let re = match regex::Regex::new(&pattern) {
        Ok(r) => r,
        Err(_) => return vec![(line.to_string(), false)],
    };
    let mut out: Vec<(String, bool)> = Vec::new();
    let mut last = 0usize;
    for m in re.find_iter(line) {
        if m.start() > last {
            out.push((line[last..m.start()].to_string(), false));
        }
        // Zero-width matches (`\b`, `^`) still count as match positions —
        // the replace preview inserts the expansion there, mirroring what
        // `replace_in_text` writes — and `last` must advance regardless, or
        // the text before the NEXT match re-emits and the row doubles
        // ("foofoo" for `\b` on "foo"; #124 review).
        if m.end() == m.start() {
            out.push((String::new(), true));
            last = m.end();
            continue;
        }
        out.push((line[m.start()..m.end()].to_string(), true));
        last = m.end();
    }
    if last < line.len() {
        out.push((line[last..].to_string(), false));
    }
    if out.is_empty() {
        out.push((line.to_string(), false));
    }
    out
}

/// One unit of streamed output from the search worker. The query and opts
/// are echoed back on every event so the receiver can drop stale events
/// when the user has typed past or flipped a toggle since the scan started.
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum SearchEvent {
    /// A batch of hits, one batch per file as the parallel walker finds them.
    Hits(String, SearchOpts, Vec<SearchHit>),
    /// Sentinel emitted exactly once per submitted request when the scan
    /// finishes (or short-circuits on an empty query). UI uses this to flip
    /// the "(searching…)" caption off.
    Done(String, SearchOpts),
}

/// Work submitted to the search worker. `Query` carries the query string and
/// the toggle state at the moment of submission, so the user can flip a toggle
/// and re-fire the same string with new opts. `SetRoot` rebinds the directory
/// the worker scans, so search always targets the Explorer's current root and
/// its subdirectories after a Make-root / open-folder, never the root captured
/// when the worker was spawned.
#[derive(Clone, Debug)]
pub enum SearchRequest {
    Query(String, SearchOpts),
    SetRoot(PathBuf),
    /// Replace the whole root set (multi-root workspaces, #147): the scan
    /// walks every root, so hits come from all workspace folders.
    SetRoots(Vec<PathBuf>),
    /// Snapshot of files open in unsaved (dirty) editors as `(path, content)`
    /// where `content` is the buffer's current text. The worker searches these
    /// in-memory copies and skips their stale disk versions, so an unsaved edit
    /// (e.g. a Rename Symbol) is findable immediately. Like `SetRoot`, this is
    /// state, not a query: it updates the snapshot used by the next scan.
    SetDirtyBuffers(Vec<(PathBuf, String)>),
    /// VS Code's "files to include" / "files to exclude" glob inputs, as raw
    /// comma-separated strings. Like `SetRoot`, this is state, not a query: it
    /// updates the filter the next scan applies. Empty strings clear the
    /// filter.
    SetFilter(String, String),
}

/// How long the user has to pause typing before the search fires. Each
/// fresh keystroke restarts the timer, so a fast typist running through
/// "cla" never triggers searches for "c" or "cl" - only the trailing
/// query gets scanned. This is the standard search-as-you-type pattern
/// (VS Code, GitHub, etc.). 200 ms is in the sweet spot: short enough
/// to feel responsive, long enough that bursts of typing are silent.
const SEARCH_DEBOUNCE: std::time::Duration = std::time::Duration::from_millis(200);

/// Background worker loop. Implements trailing-edge debounce: blocks on
/// `recv()` for the first input, then loops on `recv_timeout(200 ms)` -
/// each fresh keystroke restarts the quiet window, so the search fires
/// only after the user has actually paused. The scan runs on a dedicated
/// thread so a new keystroke arriving DURING a scan can cancel it via
/// the shared `Arc<AtomicBool>` and start fresh. Each scan emits per-file
/// `Hits` batches followed by exactly one `Done`, even when canceled,
/// so the receiver always sees a balanced event stream. The thread exits
/// cleanly when the channel closes.
pub fn search_worker_loop(
    root: PathBuf,
    rx: std::sync::mpsc::Receiver<SearchRequest>,
    tx: std::sync::mpsc::Sender<SearchEvent>,
) {
    let mut roots: Vec<PathBuf> = vec![root];
    use std::sync::mpsc::RecvTimeoutError;
    let mut current: Option<(std::thread::JoinHandle<()>, Arc<AtomicBool>)> = None;
    // Latest snapshot of unsaved editor buffers, refreshed by SetDirtyBuffers.
    let mut dirty: Vec<(PathBuf, String)> = Vec::new();
    // Latest include/exclude glob filter, refreshed by SetFilter.
    let mut filter = PathFilter::default();
    while let Ok(mut latest) = rx.recv() {
        // A new message landed: cancel the in-flight scan (if any) so its
        // worker thread bails out before the user pauses long enough to
        // trigger the next scan.
        if let Some((handle, cancel)) = current.take() {
            cancel.store(true, Ordering::Relaxed);
            let _ = handle.join();
        }
        // State updates (root rebind, dirty-buffer snapshot) are not queries
        // and do not debounce: apply and wait for the next message.
        match latest {
            SearchRequest::SetRoot(new_root) => {
                roots = vec![new_root];
                continue;
            }
            SearchRequest::SetRoots(new_roots) if !new_roots.is_empty() => {
                roots = new_roots;
                continue;
            }
            SearchRequest::SetRoots(_) => continue,
            SearchRequest::SetDirtyBuffers(buffers) => {
                dirty = buffers;
                continue;
            }
            SearchRequest::SetFilter(ref include, ref exclude) => {
                filter = PathFilter::new(include, exclude);
                continue;
            }
            SearchRequest::Query(..) => {}
        }
        // Trailing-edge debounce: each new keystroke resets the timer.
        // The search fires only after `SEARCH_DEBOUNCE` of silence. A
        // root rebind or dirty-buffer snapshot arriving mid-debounce updates
        // the state without displacing the pending query.
        loop {
            match rx.recv_timeout(SEARCH_DEBOUNCE) {
                Ok(SearchRequest::SetRoot(new_root)) => roots = vec![new_root],
                Ok(SearchRequest::SetRoots(new_roots)) if !new_roots.is_empty() => {
                    roots = new_roots
                }
                Ok(SearchRequest::SetRoots(_)) => {}
                Ok(SearchRequest::SetDirtyBuffers(buffers)) => dirty = buffers,
                Ok(SearchRequest::SetFilter(include, exclude)) => {
                    filter = PathFilter::new(&include, &exclude)
                }
                Ok(newer) => latest = newer,
                Err(RecvTimeoutError::Timeout) => break,
                Err(RecvTimeoutError::Disconnected) => return,
            }
        }
        let SearchRequest::Query(query, opts) = latest else {
            continue;
        };
        if query.trim().is_empty() {
            if tx.send(SearchEvent::Done(query, opts)).is_err() {
                return;
            }
            continue;
        }
        let cancel = Arc::new(AtomicBool::new(false));
        let cancel_for_thread = cancel.clone();
        let tx_for_thread = tx.clone();
        let roots_for_thread = roots.clone();
        let query_for_thread = query.clone();
        let dirty_for_thread = dirty.clone();
        let filter_for_thread = filter.clone();
        let handle = std::thread::spawn(move || {
            let q_for_emit = query_for_thread.clone();
            let tx_for_emit = tx_for_thread.clone();
            // First, search the unsaved buffers in-memory and collect the set
            // of their canonical paths so the disk walk skips the stale copies.
            // Dirty buffers must respect the include/exclude filter too, so a
            // glob that excludes a file hides its unsaved hits as well.
            let mut skip: HashSet<PathBuf> = HashSet::new();
            let mut buf_hits: Vec<SearchHit> = Vec::new();
            for (path, content) in &dirty_for_thread {
                if let Ok(canon) = path.canonicalize() {
                    skip.insert(canon);
                }
                // Filter globs are root-relative: judge against the root
                // that owns the buffer's path (the first containing one;
                // roots rarely nest and the glob answer matches VS Code's
                // folder-relative include semantics either way).
                let owner = roots_for_thread
                    .iter()
                    .find(|r| path.starts_with(r))
                    .unwrap_or(&roots_for_thread[0]);
                if !filter_for_thread.is_empty() && !filter_for_thread.allows(owner, path) {
                    continue;
                }
                collect_matches_in_text(path, content, &query_for_thread, opts, &mut buf_hits);
            }
            if !buf_hits.is_empty() {
                let _ = tx_for_emit.send(SearchEvent::Hits(q_for_emit.clone(), opts, buf_hits));
            }
            // Every workspace root in order: one balanced Hits stream, one
            // Done after the last root (#147).
            for scan_root in &roots_for_thread {
                if cancel_for_thread.load(Ordering::Relaxed) {
                    break;
                }
                let tx_for_root = tx_for_emit.clone();
                let q_for_root = q_for_emit.clone();
                search_workspace_streaming_filtered(
                    scan_root,
                    &query_for_thread,
                    opts,
                    &cancel_for_thread,
                    &skip,
                    &filter_for_thread,
                    move |batch| {
                        let _ =
                            tx_for_root.send(SearchEvent::Hits(q_for_root.clone(), opts, batch));
                    },
                );
            }
            let _ = tx_for_thread.send(SearchEvent::Done(query_for_thread, opts));
        });
        current = Some((handle, cancel));
    }
    if let Some((handle, cancel)) = current.take() {
        cancel.store(true, Ordering::Relaxed);
        let _ = handle.join();
    }
}

/// Per-text search helper. Wraps `grep-searcher` so the matching logic is
/// identical to the file-walking engine (same regex, line numbers, trimming).
/// Honours the three `SearchOpts` toggles. Used both by tests and by the
/// dirty-aware worker to search unsaved in-memory buffers. Regex compilation
/// failure for invalid patterns is silent: returns no matches.
pub fn collect_matches_in_text(
    path: &Path,
    content: &str,
    query: &str,
    opts: SearchOpts,
    out: &mut Vec<SearchHit>,
) {
    let q = query.trim();
    if q.is_empty() {
        return;
    }
    let Some(matcher) = build_matcher(q, opts) else {
        return;
    };
    let mut sink = HitSink {
        path: path.to_path_buf(),
        batch: out,
        cancel: None,
    };
    let mut searcher = SearcherBuilder::new()
        .line_number(true)
        .binary_detection(BinaryDetection::quit(b'\x00'))
        .build();
    let _ = searcher.search_slice(&matcher, content.as_bytes(), &mut sink);
}

/// Compile the search query + `SearchOpts` into a `regex::Regex`, mirroring
/// the pattern construction used by `split_for_highlight_regex` so replace
/// matches exactly what the highlighter shows. In literal (non-regex) mode the
/// query is escaped so metacharacters are matched verbatim. Returns `None` for
/// an empty query or an uncompilable pattern.
fn build_replace_regex(query: &str, opts: SearchOpts) -> Option<regex::Regex> {
    let q = query.trim();
    if q.is_empty() {
        return None;
    }
    let body = if opts.use_regex {
        q.to_string()
    } else {
        regex::escape(q)
    };
    let mut pattern = String::new();
    if !opts.case_sensitive {
        pattern.push_str("(?i)");
    }
    if opts.whole_word {
        pattern.push_str("\\b(?:");
        pattern.push_str(&body);
        pattern.push_str(")\\b");
    } else {
        pattern.push_str(&body);
    }
    regex::Regex::new(&pattern).ok()
}

/// Replace every match of `query` (honouring `opts`) in `content` with
/// `replacement`, returning `(new_content, count)`. In regex mode the
/// replacement honours `$1` / `${name}` capture references, like VS Code; in
/// literal mode the replacement is inserted verbatim (a `$` in the replacement
/// is escaped so it is not treated as a capture reference). Returns `None` when
/// the query is empty or the pattern can't compile, so callers leave the file
/// untouched.
pub fn replace_in_text(
    content: &str,
    query: &str,
    replacement: &str,
    opts: SearchOpts,
) -> Option<(String, usize)> {
    let re = build_replace_regex(query, opts)?;
    let count = re.find_iter(content).count();
    if count == 0 {
        return Some((content.to_string(), 0));
    }
    let out = if opts.use_regex {
        re.replace_all(content, replacement).into_owned()
    } else {
        // Literal replacement: escape `$` so the substituted text is inserted
        // verbatim rather than parsed as a capture reference.
        let escaped = replacement.replace('$', "$$");
        re.replace_all(content, escaped.as_str()).into_owned()
    };
    Some((out, count))
}

/// Expand the replacement for ONE matched slice — regex capture references
/// (`$1`, `${name}`) honoured in regex mode, verbatim otherwise. Drives the
/// result rows' inline old→new preview (#123).
pub fn expand_replacement(
    matched: &str,
    query: &str,
    replacement: &str,
    opts: SearchOpts,
) -> String {
    let Some(re) = build_replace_regex(query, opts) else {
        return replacement.to_string();
    };
    if opts.use_regex {
        re.replace(matched, replacement).into_owned()
    } else {
        replacement.to_string()
    }
}

/// Replace every match in the file at `path` on disk, writing the result back
/// only when at least one replacement was made. Returns the number of matches
/// replaced, or `None` when the file can't be read, the pattern can't compile,
/// or the write fails. Preserves files with no matches untouched (returns 0).
pub fn replace_in_file(
    path: &Path,
    query: &str,
    replacement: &str,
    opts: SearchOpts,
) -> Option<usize> {
    let content = std::fs::read_to_string(path).ok()?;
    let (new_content, count) = replace_in_text(&content, query, replacement, opts)?;
    if count == 0 {
        return Some(0);
    }
    std::fs::write(path, new_content).ok()?;
    Some(count)
}

fn is_whole_word_match(haystack: &str, start: usize, end: usize) -> bool {
    let prev_ok = start == 0
        || haystack[..start]
            .chars()
            .last()
            .map(|c| !is_word_char(c))
            .unwrap_or(true);
    let next_ok = end >= haystack.len()
        || haystack[end..]
            .chars()
            .next()
            .map(|c| !is_word_char(c))
            .unwrap_or(true);
    prev_ok && next_ok
}

fn is_word_char(c: char) -> bool {
    c.is_alphanumeric() || c == '_'
}

/// Side-panel widget shown when the active sidebar view is "Search".
pub struct SearchPanel {
    pub query: String,
    pub hits: Vec<SearchHit>,
    pub selected: usize,
    pub scroll: usize,
    pub focused: bool,
    /// True under the Black theme: overpaint the focused outer border with the
    /// orange→green brand gradient instead of the legacy solid blue. Set by the
    /// app's focus/theme sync.
    pub focus_gradient: bool,
    /// Active color theme; drives the scrollbar track/thumb colors. Set by the
    /// app's theme sync.
    pub theme: crate::theme::Theme,
    pub last_inner: Rect,
    pub last_area: Rect,
    pub root: PathBuf,
    /// Every workspace root (#147); `root` stays the primary. Display
    /// strips a hit against its OWNING root, prefixed with the root's
    /// name when more than one folder is open.
    pub roots: Vec<PathBuf>,
    pub opts: SearchOpts,
    /// Per-toggle absolute screen column captured by the most recent render.
    /// Used by `App::handle_mouse` to map clicks on `Aa`, `ab`, `.*` into
    /// flag flips. Zero means the row was too narrow to render that toggle.
    pub toggle_case_x: u16,
    pub toggle_word_x: u16,
    pub toggle_regex_x: u16,
    pub toggle_y: u16,
    pub paste_button_x: u16,
    pub paste_button_y: u16,
    pub paste_button_w: u16,
    pub selection: Option<(usize, usize)>,
    /// Hit-test rect captured during the last render of the result-list
    /// scrollbar. Empty when there's no overflow. Used by the mouse
    /// handler to route track clicks / drags to `scroll_to_bar_y`.
    pub last_scrollbar: Rect,
    /// Render-time pointer cell, fed from `App::pointer_cell` each frame so a
    /// hovered (but unselected) result row can lift to the hover background.
    pub hover_pointer: Option<(u16, u16)>,

    // --- Replace + files-to-include/exclude (VS Code Search sidebar) ---
    /// Replacement text. Editable only while the Replace row is expanded.
    pub replace: String,
    /// "files to include" glob list (comma-separated), shown when the details
    /// (`...`) row is expanded.
    pub include: String,
    /// "files to exclude" glob list (comma-separated), shown when details is
    /// expanded.
    pub exclude: String,
    /// Whether the Replace row is expanded, toggled by the left chevron.
    pub replace_open: bool,
    /// Whether the include/exclude detail rows are expanded, toggled by `...`.
    pub details_open: bool,
    /// Which input field currently receives typed characters. Tab cycles
    /// through the visible fields; clicking an input focuses it directly.
    pub field: SearchField,
    /// True once the worker's `Done` for the CURRENT query has drained:
    /// Replace All refuses while hits are still streaming, or files found
    /// after confirmation would be silently missed (#124 review).
    pub complete: bool,
    /// Selection range within the currently focused field's text.
    pub field_selection: Option<(usize, usize)>,

    // --- Hit-test geometry captured during render (absolute screen coords) ---
    /// The left expand chevron (toggles the Replace row).
    pub chevron_x: u16,
    pub chevron_y: u16,
    /// The replace-all action icon at the right of the Replace row.
    pub replace_all_x: u16,
    pub replace_all_y: u16,
    /// The `...` details toggle (expands include/exclude).
    pub ellipsis_x: u16,
    pub ellipsis_y: u16,
    /// The SEARCH header action icons (top-right): refresh re-runs the query,
    /// clear wipes the query + results. Single-cell hit targets.
    pub refresh_x: u16,
    pub refresh_y: u16,
    pub clear_x: u16,
    pub clear_y: u16,
    /// Content rects of each input box's editable area, for click-to-focus.
    pub query_input_rect: Rect,
    pub replace_input_rect: Rect,
    pub include_input_rect: Rect,
    pub exclude_input_rect: Rect,
    /// Row offset (from `inner.y`) where the results list starts. Recomputed
    /// each render because expanding Replace / details rows pushes results
    /// down. `results_viewport_height` and `hit_at_y` read this so scrolling
    /// and click mapping stay aligned with the rendered layout.
    pub results_start_offset: u16,
    /// Screen cell of the focused field's text caret, captured each render so
    /// the app can drive the host's blinking hardware caret there (matching
    /// the Source Control commit box). `None` when no field is focused or the
    /// panel has not laid out yet.
    caret_cell: Option<(u16, u16)>,
}

/// Which input field of the Search panel currently has the cursor. Drives
/// where typed characters land and which box draws the focus accent.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Default)]
pub enum SearchField {
    #[default]
    Query,
    Replace,
    Include,
    Exclude,
}

impl SearchPanel {
    pub fn new(root: PathBuf) -> Self {
        Self {
            roots: vec![root.clone()],
            query: String::new(),
            hits: Vec::new(),
            selected: 0,
            scroll: 0,
            focused: false,
            focus_gradient: false,
            theme: crate::theme::Theme::default(),
            last_inner: Rect::default(),
            last_area: Rect::default(),
            root,
            opts: SearchOpts::default(),
            toggle_case_x: 0,
            toggle_word_x: 0,
            toggle_regex_x: 0,
            toggle_y: 0,
            paste_button_x: 0,
            paste_button_y: 0,
            paste_button_w: 0,
            selection: None,
            last_scrollbar: Rect::default(),
            hover_pointer: None,
            replace: String::new(),
            include: String::new(),
            exclude: String::new(),
            replace_open: false,
            details_open: false,
            field: SearchField::Query,
            complete: false,
            field_selection: None,
            chevron_x: 0,
            chevron_y: 0,
            replace_all_x: 0,
            replace_all_y: 0,
            ellipsis_x: 0,
            ellipsis_y: 0,
            refresh_x: 0,
            refresh_y: 0,
            clear_x: 0,
            clear_y: 0,
            query_input_rect: Rect::default(),
            replace_input_rect: Rect::default(),
            include_input_rect: Rect::default(),
            exclude_input_rect: Rect::default(),
            results_start_offset: 5,
            caret_cell: None,
        }
    }

    /// Screen position of the focused field's text caret, in absolute terminal
    /// coordinates, for the app to drive the host's blinking hardware caret
    /// (mirroring `SourceControlPanel::cursor_screen_pos`). `None` when the
    /// panel isn't focused or hasn't been laid out yet.
    pub fn cursor_screen_pos(&self) -> Option<(u16, u16)> {
        if !self.focused {
            return None;
        }
        self.caret_cell
    }

    pub fn paste_button_at(&self, col: u16, row: u16) -> bool {
        if self.paste_button_w == 0 || row != self.paste_button_y {
            return false;
        }
        col >= self.paste_button_x && col < self.paste_button_x + self.paste_button_w
    }

    pub fn selection_range(&self) -> Option<(usize, usize)> {
        self.selection
            .map(|(a, b)| if a <= b { (a, b) } else { (b, a) })
    }

    /// Replace the query and both filter lists wholesale (the terminal-seed
    /// path). Text replacement invalidates any byte-range selection into the
    /// old text — a stale range into shorter text panics on the next edit —
    /// and focus moves to the query the caller just filled.
    pub fn seed(&mut self, query: String, include: String, exclude: String) {
        self.query = query;
        self.include = include;
        self.exclude = exclude;
        self.field = SearchField::Query;
        self.selection = None;
        self.field_selection = None;
    }

    pub fn select_all_query(&mut self) {
        if self.query.is_empty() {
            self.selection = None;
        } else {
            self.selection = Some((0, self.query.len()));
        }
    }

    pub fn clear_selection(&mut self) {
        self.selection = None;
    }

    pub fn delete_selection(&mut self) -> bool {
        let Some((a, b)) = self.selection_range() else {
            return false;
        };
        if a == b {
            self.selection = None;
            return false;
        }
        self.query.replace_range(a..b, "");
        self.selection = None;
        true
    }

    pub fn selection_text(&self) -> String {
        match self.selection_range() {
            Some((a, b)) if a < b => self.query[a..b].to_string(),
            _ => String::new(),
        }
    }

    /// Insert `s` at the end of the query, replacing the current selection
    /// (if any) first. Newlines and carriage returns are stripped — search
    /// queries are single-line.
    pub fn insert_str_into_query(&mut self, s: &str) {
        self.delete_selection();
        for c in s.chars() {
            if c != '\n' && c != '\r' {
                self.query.push(c);
            }
        }
    }

    /// Mutable handle to the text of whichever field is focused. The Query
    /// field keeps its own `selection`; the other fields share
    /// `field_selection`, so selecting text in one input clears it elsewhere.
    fn active_text_mut(&mut self) -> &mut String {
        match self.field {
            SearchField::Query => &mut self.query,
            SearchField::Replace => &mut self.replace,
            SearchField::Include => &mut self.include,
            SearchField::Exclude => &mut self.exclude,
        }
    }

    /// Read-only view of the focused field's text.
    pub fn active_text(&self) -> &str {
        match self.field {
            SearchField::Query => &self.query,
            SearchField::Replace => &self.replace,
            SearchField::Include => &self.include,
            SearchField::Exclude => &self.exclude,
        }
    }

    /// Clear the focused field's text entirely (used by Esc).
    pub fn active_text_mut_clear(&mut self) {
        self.active_text_mut().clear();
    }

    /// Clear the selection of the focused field.
    pub fn clear_active_selection(&mut self) {
        self.set_active_selection(None);
    }

    fn active_selection(&self) -> Option<(usize, usize)> {
        let sel = match self.field {
            SearchField::Query => self.selection,
            _ => self.field_selection,
        };
        sel.map(|(a, b)| if a <= b { (a, b) } else { (b, a) })
    }

    fn set_active_selection(&mut self, sel: Option<(usize, usize)>) {
        match self.field {
            SearchField::Query => self.selection = sel,
            _ => self.field_selection = sel,
        }
    }

    /// Append `c` to the focused field, replacing any selection first.
    pub fn type_char(&mut self, c: char) {
        if c == '\n' || c == '\r' {
            return;
        }
        self.delete_active_selection();
        self.active_text_mut().push(c);
    }

    /// Backspace one char (or the selection) from the focused field. Returns
    /// true when the text changed.
    pub fn backspace(&mut self) -> bool {
        if self.delete_active_selection() {
            return true;
        }
        self.active_text_mut().pop().is_some()
    }

    /// Insert `s` into the focused field, replacing the selection and stripping
    /// newlines (all Search inputs are single-line).
    pub fn insert_str(&mut self, s: &str) {
        self.delete_active_selection();
        let text = self.active_text_mut();
        for c in s.chars() {
            if c != '\n' && c != '\r' {
                text.push(c);
            }
        }
    }

    /// Select the whole text of the focused field.
    pub fn select_all_active(&mut self) {
        let len = self.active_text().len();
        if len == 0 {
            self.set_active_selection(None);
        } else {
            self.set_active_selection(Some((0, len)));
        }
    }

    fn delete_active_selection(&mut self) -> bool {
        let Some((a, b)) = self.active_selection() else {
            return false;
        };
        if a == b {
            self.set_active_selection(None);
            return false;
        }
        self.active_text_mut().replace_range(a..b, "");
        self.set_active_selection(None);
        true
    }

    /// Text currently selected in the focused field (empty when none).
    pub fn active_selection_text(&self) -> String {
        match self.active_selection() {
            Some((a, b)) if a < b => self.active_text()[a..b].to_string(),
            _ => String::new(),
        }
    }

    /// Move keyboard focus to the next visible field, wrapping around. Only
    /// fields whose row is currently expanded participate, mirroring VS Code
    /// where Tab skips collapsed inputs.
    pub fn focus_next_field(&mut self) {
        let order = self.visible_fields();
        let pos = order.iter().position(|f| *f == self.field).unwrap_or(0);
        let next = order[(pos + 1) % order.len()];
        self.focus_field(next);
    }

    /// Set the focused field, clearing selection in the field being left.
    pub fn focus_field(&mut self, field: SearchField) {
        if self.field != field {
            self.set_active_selection(None);
            self.field = field;
        }
    }

    /// Visible (focusable) fields, in tab order, honouring which rows are
    /// expanded. Query is always present.
    fn visible_fields(&self) -> Vec<SearchField> {
        let mut v = vec![SearchField::Query];
        if self.replace_open {
            v.push(SearchField::Replace);
        }
        if self.details_open {
            v.push(SearchField::Include);
            v.push(SearchField::Exclude);
        }
        v
    }

    /// Toggle the Replace row. When collapsing while it holds focus, focus
    /// falls back to the Query field.
    pub fn toggle_replace(&mut self) {
        self.replace_open = !self.replace_open;
        if self.replace_open {
            // VS Code focuses the revealed input; leaving focus in the
            // query made the next keystrokes silently extend the search
            // (#123).
            self.focus_field(SearchField::Replace);
        } else if self.field == SearchField::Replace {
            self.focus_field(SearchField::Query);
        }
    }

    /// Toggle the include/exclude detail rows. Collapsing while one holds
    /// focus returns focus to the Query field.
    pub fn toggle_details(&mut self) {
        self.details_open = !self.details_open;
        if !self.details_open && matches!(self.field, SearchField::Include | SearchField::Exclude) {
            self.focus_field(SearchField::Query);
        }
    }

    /// Compile the current include/exclude inputs into a `PathFilter`.
    pub fn path_filter(&self) -> PathFilter {
        PathFilter::new(&self.include, &self.exclude)
    }

    /// True when `(col, row)` lands on the expand chevron (toggle Replace row).
    pub fn chevron_at(&self, col: u16, row: u16) -> bool {
        row == self.chevron_y && col == self.chevron_x
    }

    /// True when `(col, row)` lands on the `...` ellipsis (toggle details).
    pub fn ellipsis_at(&self, col: u16, row: u16) -> bool {
        self.ellipsis_x != 0 && row == self.ellipsis_y && col == self.ellipsis_x
    }

    /// True when `(col, row)` lands on the replace-all action icon.
    pub fn replace_all_at(&self, col: u16, row: u16) -> bool {
        self.replace_all_x != 0 && row == self.replace_all_y && col == self.replace_all_x
    }

    /// True when `(col, row)` lands on the header "Refresh" action icon.
    pub fn refresh_at(&self, col: u16, row: u16) -> bool {
        self.refresh_x != 0 && row == self.refresh_y && col == self.refresh_x
    }

    /// True when `(col, row)` lands on the header "Clear Search Results" icon.
    pub fn clear_at(&self, col: u16, row: u16) -> bool {
        self.clear_x != 0 && row == self.clear_y && col == self.clear_x
    }

    /// The hover-tooltip label for whatever actionable control sits under
    /// `(col, row)`, or `None` over plain text/fields/empty space. Reuses the
    /// same hit-tests the click handler uses, so a hint targets exactly the
    /// cells that act. Text inputs deliberately return `None`: their purpose is
    /// shown by the inline placeholder, not a tooltip.
    pub fn tooltip_at(&self, col: u16, row: u16) -> Option<&'static str> {
        if self.refresh_at(col, row) {
            return Some("Refresh");
        }
        if self.clear_at(col, row) {
            return Some("Clear Search Results");
        }
        if self.chevron_at(col, row) {
            return Some(if self.replace_open {
                "Collapse Replace"
            } else {
                "Toggle Replace"
            });
        }
        if self.ellipsis_at(col, row) {
            return Some("Toggle Search Details");
        }
        if self.replace_all_at(col, row) {
            return Some("Replace All");
        }
        if let Some(toggle) = self.toggle_at(col, row) {
            return Some(match toggle {
                SearchToggle::CaseSensitive => "Match Case",
                SearchToggle::WholeWord => "Match Whole Word",
                SearchToggle::UseRegex => "Use Regular Expression",
            });
        }
        None
    }

    /// Clear the search like VS Code's "Clear Search Results": empty the query
    /// (and the replace text typed against it) and drop every hit, resetting
    /// selection and scroll. The include/exclude glob filters are intentionally
    /// preserved — they are scoping settings, not search results.
    pub fn clear_all(&mut self) {
        self.query.clear();
        self.replace.clear();
        self.hits.clear();
        self.selected = 0;
        self.scroll = 0;
        self.selection = None;
        self.field_selection = None;
    }

    /// Map a click to the input field whose editable row it lands on, for
    /// click-to-focus. Returns `None` when the click is outside every input.
    pub fn field_at(&self, col: u16, row: u16) -> Option<SearchField> {
        let on = |r: Rect| r.width != 0 && row == r.y && col >= r.x && col < r.x + r.width;
        if on(self.query_input_rect) {
            return Some(SearchField::Query);
        }
        if on(self.replace_input_rect) {
            return Some(SearchField::Replace);
        }
        if on(self.include_input_rect) {
            return Some(SearchField::Include);
        }
        if on(self.exclude_input_rect) {
            return Some(SearchField::Exclude);
        }
        None
    }

    /// Replace every match in every distinct file among the current hits with
    /// `self.replace`, honouring the active `SearchOpts`. Returns the total
    /// number of matches replaced across all files. The caller is responsible
    /// for re-running the query afterwards so the (now stale) hit list updates.
    pub fn replace_all_on_disk(&self) -> usize {
        let needle = self.query.trim();
        if needle.is_empty() {
            return 0;
        }
        let mut seen: HashSet<PathBuf> = HashSet::new();
        let mut total = 0usize;
        for hit in &self.hits {
            if !seen.insert(hit.path.clone()) {
                continue;
            }
            if let Some(n) = replace_in_file(&hit.path, needle, &self.replace, self.opts) {
                total += n;
            }
        }
        total
    }

    /// Count what Replace All WOULD do — `(occurrences, files)` — without
    /// writing anything. Feeds the confirmation modal (#123).
    pub fn count_replacements(&self, skip: &HashSet<PathBuf>) -> (usize, usize) {
        let needle = self.query.trim();
        if needle.is_empty() {
            return (0, 0);
        }
        let mut seen: HashSet<PathBuf> = HashSet::new();
        let mut occurrences = 0usize;
        let mut files = 0usize;
        for hit in &self.hits {
            if !seen.insert(hit.path.clone()) {
                continue;
            }
            if skip.contains(&hit.path) {
                // The confirmed action will not touch these; counting them
                // made the modal overpromise (#124 review).
                continue;
            }
            let Ok(content) = std::fs::read_to_string(&hit.path) else {
                continue;
            };
            if let Some((_, n)) = replace_in_text(&content, needle, &self.replace, self.opts)
                && n > 0
            {
                occurrences += n;
                files += 1;
            }
        }
        (occurrences, files)
    }

    /// Replace All, skipping `skip` (files open with unsaved edits — a disk
    /// write under a dirty buffer forces the tab into conflict divergence).
    /// Returns `(occurrences replaced, the skipped hit files)`.
    pub fn replace_all_skipping(&self, skip: &HashSet<PathBuf>) -> (usize, Vec<PathBuf>) {
        let needle = self.query.trim();
        if needle.is_empty() {
            return (0, Vec::new());
        }
        let mut seen: HashSet<PathBuf> = HashSet::new();
        let mut total = 0usize;
        let mut skipped = Vec::new();
        for hit in &self.hits {
            if !seen.insert(hit.path.clone()) {
                continue;
            }
            if skip.contains(&hit.path) {
                skipped.push(hit.path.clone());
                continue;
            }
            if let Some(n) = replace_in_file(&hit.path, needle, &self.replace, self.opts) {
                total += n;
            }
        }
        (total, skipped)
    }

    /// Run the current query, store the results, and reset selection.
    pub fn run_query(&mut self) {
        self.hits = search_workspace(&self.root, &self.query, self.opts);
        self.selected = 0;
        self.scroll = 0;
    }

    /// If the cell `(col, row)` falls on one of the three toggle glyphs
    /// (each rendered as a 2-cell pair), return a mutable pointer to the
    /// corresponding flag so the caller can flip it. Returns `None`
    /// otherwise.
    pub fn toggle_at(&self, col: u16, row: u16) -> Option<SearchToggle> {
        if row != self.toggle_y {
            return None;
        }
        for (start, kind) in [
            (self.toggle_case_x, SearchToggle::CaseSensitive),
            (self.toggle_word_x, SearchToggle::WholeWord),
            (self.toggle_regex_x, SearchToggle::UseRegex),
        ] {
            if start != 0 && col >= start && col < start + 2 {
                return Some(kind);
            }
        }
        None
    }

    pub fn move_up(&mut self) {
        if self.selected > 0 {
            self.selected -= 1;
        }
        self.ensure_selected_visible();
    }

    pub fn move_down(&mut self) {
        if self.selected + 1 < self.hits.len() {
            self.selected += 1;
        }
        self.ensure_selected_visible();
    }

    /// Pull `scroll` so the current `selected` row sits inside the viewport.
    /// Only invoked from keyboard-driven moves; wheel scroll and scrollbar
    /// drag must NOT call this - the user's wheel position is sacrosanct
    /// and selection trails wherever they put it.
    fn ensure_selected_visible(&mut self) {
        let viewport = self.results_viewport_height();
        if viewport == 0 {
            return;
        }
        if self.selected < self.scroll {
            self.scroll = self.selected;
        } else if self.selected >= self.scroll + viewport {
            self.scroll = self.selected + 1 - viewport;
        }
    }

    pub fn scroll_up(&mut self, rows: usize) {
        self.scroll = self.scroll.saturating_sub(rows);
    }

    pub fn scroll_down(&mut self, rows: usize) {
        let viewport = self.results_viewport_height();
        let max_scroll = self.hits.len().saturating_sub(viewport);
        self.scroll = self.scroll.saturating_add(rows).min(max_scroll);
    }

    /// Map a track-y from a scrollbar click/drag back into a scroll offset.
    pub fn scroll_to_bar_y(&mut self, y: u16) -> bool {
        let viewport = self.results_viewport_height();
        let Some(metrics) = crate::widgets::scrollbar::vertical_metrics(
            self.last_scrollbar,
            self.hits.len(),
            viewport,
            self.scroll,
        ) else {
            return false;
        };
        let new_scroll = crate::widgets::scrollbar::scroll_for_y(metrics, y);
        let max_scroll = self.hits.len().saturating_sub(viewport);
        self.scroll = new_scroll.min(max_scroll);
        true
    }

    fn results_viewport_height(&self) -> usize {
        // Results start at `results_start_offset` rows below `inner.y`. That
        // offset is recomputed each render and grows when the Replace row or
        // the include/exclude detail rows are expanded.
        let inner = self.last_inner;
        if inner.height == 0 {
            return 0;
        }
        let results_top = self.results_start_offset;
        if inner.height <= results_top {
            return 0;
        }
        (inner.height - results_top) as usize
    }

    pub fn selected_hit(&self) -> Option<&SearchHit> {
        self.hits.get(self.selected)
    }

    /// Map a click row to a hit index, if any. Hits sit below the input
    /// cluster; the exact first row depends on which optional rows (Replace,
    /// include/exclude) are expanded, captured in `results_start_offset`.
    pub fn hit_at_y(&self, y: u16) -> Option<usize> {
        let inner = self.last_inner;
        let results_start = inner.y + self.results_start_offset;
        if y < results_start || y >= inner.y + inner.height {
            return None;
        }
        let row_in_results = (y - results_start) as usize;
        let idx = self.scroll + row_in_results;
        if idx < self.hits.len() {
            Some(idx)
        } else {
            None
        }
    }
}

/// Identifies which of the three search-mode toggles a click landed on.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum SearchToggle {
    CaseSensitive,
    WholeWord,
    UseRegex,
}

/// Parameters for [`render_field`], bundled so the long argument list stays
/// readable at each call site.
struct FieldArgs<'a> {
    /// Column of the accent/idle left-edge bar; the filled content begins one
    /// cell to its right.
    bar_x: u16,
    /// The single content row this field occupies.
    y: u16,
    /// Last column of the fill (inclusive).
    fill_right: u16,
    /// Optional leading glyph (e.g. the magnifier) drawn at the fill start.
    lead_glyph: Option<&'a str>,
    /// Current text of the field.
    text: &'a str,
    /// Placeholder shown italic-grey when `text` is empty.
    placeholder: &'a str,
    /// Selection range within `text`, already normalised (a <= b).
    selection: Option<(usize, usize)>,
    /// Whether THIS field is focused (drives the accent bar, brighter fill and
    /// whether the inline cursor renders here).
    field_focused: bool,
    /// Accent color for the bar / lead glyph / cursor when focused.
    accent: Color,
    /// The active theme background, lifted to derive the idle/focused fill so
    /// the field reads as elevated on Black and on Croft Dark alike.
    bg: (u8, u8, u8),
    /// Cells kept clear at the right of the fill for inset controls (the query
    /// row's toggles). The returned hit rect also stops short of them so a
    /// toggle click is not swallowed as a click-to-focus.
    reserve_right: u16,
}

/// Render a single filled input row (VS Code's `input.background` model) and
/// return the click-to-focus hit `Rect` covering the bar + editable span (but
/// not any reserved control cells on the right). Shared by all four Search
/// inputs so they look identical: a one-cell accent/idle left bar, a subtly
/// lifted fill, an optional lead glyph, then the text/placeholder. Replaces the
/// old 3-row rounded box, which read as bulky next to VS Code / Zed's thin
/// single-line fields.
///
/// When the field is focused, `caret_out` is set to the screen cell where the
/// caret belongs (just past the text, at the field's left edge when empty) so
/// the app can drive the host's blinking hardware caret there, matching the
/// Source Control commit box. We no longer paint a static full block, which
/// never blinked and so read differently from the commit input.
fn render_field(
    buf: &mut Buffer,
    args: FieldArgs,
    theme: crate::theme::Theme,
    caret_out: &mut Option<(u16, u16)>,
) -> Rect {
    if args.fill_right <= args.bar_x {
        return Rect {
            x: args.bar_x,
            y: args.y,
            width: 0,
            height: 1,
        };
    }
    // Left-edge bar: accent when focused (standing in for VS Code's focus
    // border, which we cannot draw as a sub-cell stroke), dim otherwise so the
    // field still reads as a target at rest.
    let bar_color = if args.field_focused {
        args.accent
    } else {
        theme.ui(FIELD_BAR_IDLE)
    };
    buf.set_string(args.bar_x, args.y, "▏", Style::default().fg(bar_color));

    let fill_x = args.bar_x + 1;
    let lift = if args.field_focused {
        FIELD_FILL_LIFT_FOCUS
    } else {
        FIELD_FILL_LIFT_IDLE
    };
    let fill = lift_bg(args.bg, lift);
    let fill_style = Style::default().bg(fill);
    for x in fill_x..=args.fill_right {
        buf[(x, args.y)].set_symbol(" ").set_style(fill_style);
    }

    let lead_w: u16 = if args.lead_glyph.is_some() { 2 } else { 0 };
    if let Some(g) = args.lead_glyph {
        let lead_color = if args.field_focused {
            args.accent
        } else {
            theme.ui(Color::Rgb(0x9d, 0xa5, 0xb4))
        };
        buf.set_string(fill_x, args.y, g, Style::default().fg(lead_color).bg(fill));
    }

    // Editable span: after the lead glyph, up to the reserved control cells.
    let text_x = fill_x + lead_w;
    let text_right = args.fill_right.saturating_sub(args.reserve_right);
    let text_w = text_right.saturating_sub(text_x).saturating_add(1);

    let mut spans: Vec<Span> = Vec::with_capacity(4);
    if args.text.is_empty() {
        spans.push(Span::styled(
            args.placeholder.to_string(),
            Style::default()
                .fg(theme.ui(Color::Rgb(0x6c, 0x7d, 0x9c)))
                .bg(fill)
                .add_modifier(Modifier::ITALIC),
        ));
    } else {
        let plain = Style::default().fg(theme.ui(Color::White)).bg(fill);
        let selected = Style::default()
            .fg(theme.ui(Color::White))
            .bg(theme.ui(Color::Rgb(0x26, 0x4f, 0x78)));
        match args.selection {
            Some((a, b)) if a < b => {
                if a > 0 {
                    spans.push(Span::styled(args.text[..a].to_string(), plain));
                }
                spans.push(Span::styled(args.text[a..b].to_string(), selected));
                if b < args.text.len() {
                    spans.push(Span::styled(args.text[b..].to_string(), plain));
                }
            }
            _ => spans.push(Span::styled(args.text.to_string(), plain)),
        }
    }
    if text_w > 0 {
        buf.set_line(text_x, args.y, &Line::from(spans), text_w);
    }

    // The caret rides just past the text (at the field's left edge when empty),
    // clamped inside the editable span. The app shows the host's blinking
    // hardware caret here only while this field is focused, so we record the
    // cell for the focused field alone. Char count matches the commit box's
    // caret model (`message_cursor`); Search has no mid-text caret.
    if args.field_focused && text_w > 0 {
        let off = (args.text.chars().count() as u16).min(text_w.saturating_sub(1));
        *caret_out = Some((text_x + off, args.y));
    }

    // Hit rect: bar + editable span, excluding reserved control cells so the
    // query toggles (checked after `field_at`) still receive their clicks.
    Rect {
        x: args.bar_x,
        y: args.y,
        width: text_right.saturating_sub(args.bar_x).saturating_add(1),
        height: 1,
    }
}

impl Widget for &mut SearchPanel {
    fn render(self, area: Rect, buf: &mut Buffer) {
        let focus_blue = self.theme.ui(Color::Rgb(0x4e, 0x9a, 0xff));
        // Black theme: inner stroke accents (chevron, input ring, magnifier,
        // cursor) wear the muted brand teal instead of the legacy blue.
        let accent = if self.focus_gradient {
            crate::gradient::rgb_color(crate::gradient::INNER_ACCENT)
        } else {
            focus_blue
        };
        let outer_style = if self.focused {
            Style::default().fg(focus_blue)
        } else {
            Style::default().fg(Color::DarkGray)
        };
        let outer = Block::default()
            .borders(Borders::ALL)
            .border_style(outer_style);
        let inner = outer.inner(area);
        outer.render(area, buf);
        // Black theme: replace the solid focus border with the brand gradient.
        // No title sits on this border (the SEARCH header is inside), so there
        // is nothing to re-stamp.
        if self.focused && self.focus_gradient {
            crate::gradient::paint_gradient_box(buf, area);
        }
        self.last_area = area;
        self.last_inner = inner;
        // Clear last frame's caret up front so an early return (too small to
        // lay out a field) never leaves a stale hardware-caret position.
        self.caret_cell = None;
        if inner.height == 0 || inner.width == 0 {
            return;
        }

        // Row 0: "SEARCH" header (top-left) plus the VS Code SEARCH header
        // actions (refresh / clear) at the top-right.
        buf.set_string(
            inner.x,
            inner.y,
            "SEARCH",
            Style::default()
                .fg(self.theme.ui(Color::Rgb(0xb0, 0xb8, 0xc8)))
                .add_modifier(Modifier::BOLD),
        );

        // Header action icons, laid out from the right edge: clear (rightmost)
        // then refresh, each a single-cell click target that lifts to the accent
        // on hover. Only drawn when the panel is wide enough to clear the label.
        self.refresh_x = 0;
        self.refresh_y = inner.y;
        self.clear_x = 0;
        self.clear_y = inner.y;
        if inner.width > 16 {
            let clear_x = inner.x + inner.width - 1;
            let refresh_x = clear_x - 3;
            for (x, glyph) in [
                (refresh_x, crate::icons::SEARCH_REFRESH),
                (clear_x, crate::icons::SEARCH_CLEAR_ALL),
            ] {
                let hot = self
                    .hover_pointer
                    .is_some_and(|(px, py)| py == inner.y && px == x);
                let color = if hot {
                    accent
                } else {
                    self.theme.ui(Color::Rgb(0x8b, 0x94, 0xa4))
                };
                buf.set_string(x, inner.y, glyph.to_string(), Style::default().fg(color));
            }
            self.refresh_x = refresh_x;
            self.clear_x = clear_x;
        }

        // Below this the panel cannot host the header + a field row, or is too
        // narrow for the chevron column + a field bar + lead glyph + content.
        if inner.height < 4 || inner.width < 8 {
            return;
        }

        // ---- Field cluster geometry ----
        // VS Code / Zed model: each input is a single filled row, never a 3-row
        // box. A chevron sits at the far left (toggles the Replace row); every
        // field is indented two cells past it so the chevron column stays clear.
        // The query row's `Aa ab .*` toggles are inset INSIDE the field at its
        // right edge (VS Code's `.controls`), and the `...` details expander
        // sits in the rightmost cell, just past the field.
        let bg = self.theme.editor_bg_rgb();
        let chevron_x = inner.x;
        let bar_x = inner.x + 2; // left edge of every field's fill bar
        let inner_right = inner.x + inner.width - 1; // last usable column
        let content_y = inner.y + 2; // one blank row below the header

        // The `...` details expander occupies the rightmost cell of the query
        // row; the query field's fill ends two cells short of it.
        let ellipsis_x = inner_right;
        let query_fill_right = ellipsis_x.saturating_sub(2);

        // Inset toggles, right-aligned inside the query fill with a 1-cell pad.
        // Each glyph is two cells; one-cell gaps separate them. `toggle_at` keys
        // off these stored columns, so the layout must match cell-for-cell.
        let regex_x = query_fill_right.saturating_sub(2); // 1-cell pad + 2-cell glyph
        let word_x = regex_x.saturating_sub(3);
        let case_x = word_x.saturating_sub(3);
        // Cells the query field reserves on its right so its text never slides
        // under the toggles: from the first toggle column to the fill edge.
        let query_reserve = query_fill_right.saturating_sub(case_x).saturating_add(1);

        // Expand chevron (far left). ▾ when Replace is open, ▸ when collapsed.
        let chevron_color = if self.focused {
            accent
        } else {
            Color::DarkGray
        };
        let chevron_glyph = if self.replace_open {
            crate::icons::CHEVRON_OPEN
        } else {
            crate::icons::CHEVRON_CLOSED
        };
        buf.set_string(
            chevron_x,
            content_y,
            chevron_glyph.to_string(),
            Style::default()
                .fg(chevron_color)
                .add_modifier(Modifier::BOLD),
        );
        self.chevron_x = chevron_x;
        self.chevron_y = content_y;

        // Caret cell of whichever field is focused. `render_field` writes it
        // only for the focused field (and never clears it for the others), so
        // it accumulates the single focused field's caret across the calls
        // below; committed to `self.caret_cell` once the cluster is laid out.
        let mut caret: Option<(u16, u16)> = None;

        // Query field: filled single row, magnifier lead, inset toggles.
        let query_focused = self.focused && self.field == SearchField::Query;
        self.query_input_rect = render_field(
            buf,
            FieldArgs {
                bar_x,
                y: content_y,
                fill_right: query_fill_right,
                lead_glyph: Some("\u{ea6d}"),
                text: &self.query,
                placeholder: "Search",
                selection: self.selection_range(),
                field_focused: query_focused,
                accent,
                bg,
                reserve_right: query_reserve,
            },
            self.theme,
            &mut caret,
        );

        // Inset toggles `Aa ab .*`, painted on top of the query fill at its
        // right edge. Active = a muted accent chip (standing in for VS Code's
        // translucent `inputOption.activeBackground`); inactive = a dim glyph
        // sitting on the same fill so the chips read as part of the field.
        self.paste_button_x = 0;
        self.paste_button_y = content_y;
        self.paste_button_w = 0;
        let toggle_fill = lift_bg(
            bg,
            if query_focused {
                FIELD_FILL_LIFT_FOCUS
            } else {
                FIELD_FILL_LIFT_IDLE
            },
        );
        let active_style = Style::default()
            .fg(Color::White)
            .bg(crate::gradient::rgb_color(crate::gradient::POPUP_SEL_BG))
            .add_modifier(Modifier::BOLD);
        let inactive_style = Style::default()
            .fg(self.theme.ui(Color::Rgb(0x79, 0x82, 0x8f)))
            .bg(toggle_fill);
        self.toggle_y = content_y;
        self.toggle_case_x = case_x;
        self.toggle_word_x = word_x;
        self.toggle_regex_x = regex_x;
        for (x, glyph, active) in [
            (case_x, "Aa", self.opts.case_sensitive),
            (word_x, "ab", self.opts.whole_word),
            (regex_x, ".*", self.opts.use_regex),
        ] {
            let style = if active { active_style } else { inactive_style };
            buf.set_string(x, content_y, glyph, style);
        }

        // `...` details expander, just past the query field. Accent when open.
        let ellipsis_style = if self.details_open {
            Style::default().fg(accent).add_modifier(Modifier::BOLD)
        } else {
            Style::default().fg(self.theme.ui(Color::Rgb(0x79, 0x82, 0x8f)))
        };
        buf.set_string(
            ellipsis_x,
            content_y,
            crate::icons::SEARCH_ELLIPSIS.to_string(),
            ellipsis_style,
        );
        self.ellipsis_x = ellipsis_x;
        self.ellipsis_y = content_y;

        // ---- Optional Replace row ----
        // `next_y` tracks the next content row as optional fields stack. Each is
        // now a single row, so the cluster is far shorter than the old boxes.
        let mut next_y = content_y + 1;
        self.replace_input_rect = Rect::default();
        self.replace_all_x = 0;
        self.replace_all_y = 0;
        if self.replace_open && next_y + 1 < inner.y + inner.height {
            // A faint hairline between the query and replace fields so they read
            // as two separate inputs instead of one continuous filled container.
            draw_field_divider(buf, bar_x, query_fill_right, next_y, self.theme);
            next_y += 1;
            // The replace-all action icon sits at the far right (under the ...),
            // and the replace fill ends two cells short of it.
            let replace_all_x = inner_right;
            let replace_fill_right = replace_all_x.saturating_sub(2).max(bar_x + 1);
            let replace_focused = self.focused && self.field == SearchField::Replace;
            self.replace_input_rect = render_field(
                buf,
                FieldArgs {
                    bar_x,
                    y: next_y,
                    fill_right: replace_fill_right,
                    lead_glyph: Some(&crate::icons::SEARCH_REPLACE.to_string()),
                    text: &self.replace,
                    placeholder: "Replace (Tab switches)",
                    selection: if self.field == SearchField::Replace {
                        self.active_selection()
                    } else {
                        None
                    },
                    field_focused: replace_focused,
                    accent,
                    bg,
                    reserve_right: 0,
                },
                self.theme,
                &mut caret,
            );
            buf.set_string(
                replace_all_x,
                next_y,
                crate::icons::SEARCH_REPLACE_ALL.to_string(),
                Style::default().fg(if self.focused {
                    accent
                } else {
                    self.theme.ui(Color::Rgb(0x9d, 0xa5, 0xb4))
                }),
            );
            self.replace_all_x = replace_all_x;
            self.replace_all_y = next_y;
            next_y += 1;
        }

        // ---- Optional files-to-include / files-to-exclude rows ----
        // VS Code stacks an 11px caption above each thin field rather than using
        // placeholder-as-label, so each detail field is a caption row over a
        // single filled field.
        self.include_input_rect = Rect::default();
        self.exclude_input_rect = Rect::default();
        if self.details_open {
            let detail_fill_right = inner_right.saturating_sub(1).max(bar_x + 1);
            let caption_style = Style::default().fg(self.theme.ui(Color::Rgb(0x9d, 0xa5, 0xb4)));

            if next_y + 2 < inner.y + inner.height {
                // Hairline before the include group, matching the query/replace
                // separation so every input reads as its own row.
                draw_field_divider(buf, bar_x, detail_fill_right, next_y, self.theme);
                next_y += 1;
                buf.set_string(bar_x, next_y, "files to include", caption_style);
                next_y += 1;
                let inc_focused = self.focused && self.field == SearchField::Include;
                self.include_input_rect = render_field(
                    buf,
                    FieldArgs {
                        bar_x,
                        y: next_y,
                        fill_right: detail_fill_right,
                        lead_glyph: None,
                        text: &self.include,
                        placeholder: "e.g. src/**/*.rs",
                        selection: if self.field == SearchField::Include {
                            self.active_selection()
                        } else {
                            None
                        },
                        field_focused: inc_focused,
                        accent,
                        bg,
                        reserve_right: 0,
                    },
                    self.theme,
                    &mut caret,
                );
                next_y += 1;
            }
            if next_y + 2 < inner.y + inner.height {
                // Hairline before the exclude group, same as above.
                draw_field_divider(buf, bar_x, detail_fill_right, next_y, self.theme);
                next_y += 1;
                buf.set_string(bar_x, next_y, "files to exclude", caption_style);
                next_y += 1;
                let exc_focused = self.focused && self.field == SearchField::Exclude;
                self.exclude_input_rect = render_field(
                    buf,
                    FieldArgs {
                        bar_x,
                        y: next_y,
                        fill_right: detail_fill_right,
                        lead_glyph: None,
                        text: &self.exclude,
                        placeholder: "e.g. target, dist",
                        selection: if self.field == SearchField::Exclude {
                            self.active_selection()
                        } else {
                            None
                        },
                        field_focused: exc_focused,
                        accent,
                        bg,
                        reserve_right: 0,
                    },
                    self.theme,
                    &mut caret,
                );
                next_y += 1;
            }
        }
        // Commit the focused field's caret now that the whole input cluster is
        // laid out. Always runs (the optional rows above only skip their own
        // render), so the app sees `None` whenever no field carried a caret.
        self.caret_cell = caret;

        // ---- Separator + match-count caption ----
        let separator_y = next_y;
        if separator_y < inner.y + inner.height {
            let sep_style = Style::default().fg(self.theme.ui(Color::Rgb(0x40, 0x48, 0x58)));
            for x in inner.x..inner.x + inner.width {
                buf.set_string(x, separator_y, "─", sep_style);
            }
        }
        let caption_y = separator_y + 1;
        let results_start_y = caption_y + 1;
        // Record where results begin so scrolling / click mapping stay aligned.
        self.results_start_offset = results_start_y.saturating_sub(inner.y);
        if caption_y < inner.y + inner.height && !self.query.trim().is_empty() {
            let count = self.hits.len();
            let header = format!("{count} match{}", if count == 1 { "" } else { "es" });
            let caption = Line::from(Span::styled(
                header,
                Style::default()
                    .fg(self.theme.ui(Color::Rgb(0x9d, 0xa5, 0xb4)))
                    .add_modifier(Modifier::ITALIC),
            ));
            buf.set_line(inner.x, caption_y, &caption, inner.width);
        }

        // Results
        self.last_scrollbar = Rect::default();
        if results_start_y >= inner.y + inner.height {
            return;
        }
        let visible = (inner.y + inner.height - results_start_y) as usize;
        if visible == 0 {
            return;
        }
        // Clamp scroll so the viewport is always full when there's enough
        // content; otherwise the user can wheel past the end and see blanks.
        // Critically, we do NOT auto-follow `selected` here: that would
        // fight the user every time a streamed `Hits` batch lands and
        // re-renders. Selection-follow lives in `move_up`/`move_down`,
        // where it belongs - keyboard nav, not wheel scroll.
        let max_scroll = self.hits.len().saturating_sub(visible);
        if self.scroll > max_scroll {
            self.scroll = max_scroll;
        }
        // Reserve the rightmost column for the scrollbar when the result
        // list overflows the viewport. `vertical_metrics` returns None when
        // there's no overflow, so the rightmost column is freed for content.
        let scrollbar_area = Rect {
            x: inner.x + inner.width.saturating_sub(1),
            y: results_start_y,
            width: 1,
            height: inner.y + inner.height - results_start_y,
        };
        let scrollbar_metrics = crate::widgets::scrollbar::vertical_metrics(
            scrollbar_area,
            self.hits.len(),
            visible,
            self.scroll,
        );
        if let Some(metrics) = scrollbar_metrics {
            self.last_scrollbar = metrics.area;
        }
        let row_width = inner
            .width
            .saturating_sub(u16::from(scrollbar_metrics.is_some()));
        let end = (self.scroll + visible).min(self.hits.len());
        for (row_idx, hit_idx) in (self.scroll..end).enumerate() {
            let y = results_start_y + row_idx as u16;
            let hit = &self.hits[hit_idx];
            // Lift the row under the pointer (unless it is already the
            // selected row, which carries its own highlight). Pre-filling the
            // bg works because `set_line` only patches the cells it writes and
            // leaves their bg untouched when a span sets no bg of its own.
            let row_rect = Rect {
                x: inner.x,
                y,
                width: row_width,
                height: 1,
            };
            if hit_idx != self.selected
                && let Some(bg) =
                    crate::widgets::hover::row_hover_bg(row_rect, self.hover_pointer, self.theme)
            {
                let fill = Style::default().bg(bg);
                for rx in 0..row_width {
                    buf[(inner.x + rx, y)].set_symbol(" ").set_style(fill);
                }
            }
            // Show just the basename so the matched line itself has room
            // to render inside a narrow side panel — without this, deep
            // monorepo paths consumed the whole row and the highlight fell
            // off the right edge.
            let path_display = hit
                .path
                .file_name()
                .map(|s| s.to_string_lossy().into_owned())
                .unwrap_or_else(|| {
                    let owner = self
                        .roots
                        .iter()
                        .filter(|r| hit.path.starts_with(r))
                        .max_by_key(|r| r.components().count());
                    match owner {
                        Some(r) if self.roots.len() > 1 => {
                            let idx = self.roots.iter().position(|x| x == r).unwrap_or(0);
                            let name =
                                crate::workspace::root_display_labels(&self.roots).swap_remove(idx);
                            format!(
                                "{name}/{}",
                                hit.path
                                    .strip_prefix(r)
                                    .unwrap_or(hit.path.as_path())
                                    .display()
                            )
                        }
                        Some(r) => hit
                            .path
                            .strip_prefix(r)
                            .unwrap_or(hit.path.as_path())
                            .display()
                            .to_string(),
                        None => hit.path.display().to_string(),
                    }
                });
            // Black theme: selected result row matches the Explorer's
            // white-on-dark-teal selection; Croft Dark keeps black-on-blue.
            let header_style = if hit_idx == self.selected {
                if self.focus_gradient {
                    Style::default()
                        .fg(Color::White)
                        .bg(crate::gradient::rgb_color(crate::gradient::POPUP_SEL_BG))
                        .add_modifier(Modifier::BOLD)
                } else {
                    Style::default()
                        .fg(Color::Black)
                        .bg(self.theme.ui(Color::Rgb(0x4e, 0x9a, 0xff)))
                        .add_modifier(Modifier::BOLD)
                }
            } else {
                Style::default().fg(self.theme.ui(Color::White))
            };
            let needle = self.query.trim();
            // High-contrast yellow background like ripgrep / VS Code's
            // editor.findMatchHighlightBackground. Different treatment when
            // the row is selected so the highlight stays readable against
            // the blue row bg instead of fighting it.
            let highlight_style = Style::default()
                .fg(Color::Black)
                .bg(self.theme.ui(Color::Rgb(0xff, 0xd7, 0x4a)))
                .add_modifier(Modifier::BOLD);
            let plain_style = Style::default().fg(self.theme.ui(Color::Gray));
            let mut spans: Vec<Span> = vec![
                Span::styled(format!(" {path_display}"), header_style),
                Span::styled(
                    format!(":{}: ", hit.line_no),
                    Style::default().fg(self.theme.ui(Color::Rgb(0xeb, 0xcb, 0x8b))),
                ),
            ];
            // Inline replace preview (#123): while a replacement is typed,
            // each match renders dim and crossed out with the expanded
            // replacement (capture refs honoured) in green beside it — the
            // bad-pattern catch before Replace All commits anything.
            let preview = self.replace_open && !self.replace.is_empty();
            for (chunk, is_match) in split_for_highlight(&hit.line_text, needle, self.opts) {
                if is_match && preview {
                    let new_text = expand_replacement(&chunk, needle, &self.replace, self.opts);
                    spans.push(Span::styled(
                        chunk,
                        Style::default()
                            .fg(self.theme.ui(Color::Rgb(0x8b, 0x93, 0xa5)))
                            .add_modifier(Modifier::CROSSED_OUT),
                    ));
                    spans.push(Span::styled(
                        new_text,
                        Style::default()
                            .fg(self.theme.ui(Color::Rgb(0xb6, 0xee, 0xc4)))
                            .add_modifier(Modifier::BOLD),
                    ));
                    continue;
                }
                spans.push(Span::styled(
                    chunk,
                    if is_match {
                        highlight_style
                    } else {
                        plain_style
                    },
                ));
            }
            let line = Line::from(spans);
            buf.set_line(inner.x, y, &line, row_width);
        }
        if let Some(metrics) = scrollbar_metrics {
            crate::widgets::scrollbar::render_vertical(buf, metrics, self.focused, self.theme);
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::fs;
    use tempfile::TempDir;

    fn write(p: &Path, s: &str) {
        fs::write(p, s).unwrap();
    }

    #[test]
    fn collect_matches_substring_case_insensitive() {
        let mut out = Vec::new();
        let content = "Hello World\nfoo bar\nHELLO again\nno match here";
        collect_matches_in_text(
            Path::new("a.txt"),
            content,
            "hello",
            SearchOpts::default(),
            &mut out,
        );
        assert_eq!(out.len(), 2);
        assert_eq!(out[0].line_no, 1);
        assert_eq!(out[0].line_text, "Hello World");
        assert_eq!(out[1].line_no, 3);
        assert_eq!(out[1].line_text, "HELLO again");
    }

    #[test]
    fn collect_matches_truncates_very_long_lines() {
        let mut out = Vec::new();
        let long_line = "x".repeat(MAX_LINE_LEN + 50) + "needle";
        collect_matches_in_text(
            Path::new("a.txt"),
            &long_line,
            "needle",
            SearchOpts::default(),
            &mut out,
        );
        assert_eq!(out.len(), 1);
        // Overlong lines are cut to MAX_LINE_LEN with no trailing ellipsis.
        assert!(!out[0].line_text.contains('…'));
        assert_eq!(out[0].line_text.chars().count(), MAX_LINE_LEN);
    }

    #[test]
    fn search_workspace_finds_matches_across_files() {
        let tmp = TempDir::new().unwrap();
        write(&tmp.path().join("a.txt"), "alpha\nbeta\nbananas\n");
        write(&tmp.path().join("b.rs"), "fn beta() {}\nlet bananas = 1;\n");
        let hits = search_workspace(tmp.path(), "bananas", SearchOpts::default());
        assert_eq!(hits.len(), 2);
        let names: Vec<String> = hits
            .iter()
            .map(|h| h.path.file_name().unwrap().to_string_lossy().into_owned())
            .collect();
        assert!(names.contains(&"a.txt".to_string()));
        assert!(names.contains(&"b.rs".to_string()));
    }

    #[test]
    fn search_workspace_returns_empty_for_blank_query() {
        let tmp = TempDir::new().unwrap();
        write(&tmp.path().join("a.txt"), "anything");
        assert!(search_workspace(tmp.path(), "", SearchOpts::default()).is_empty());
        assert!(search_workspace(tmp.path(), "   ", SearchOpts::default()).is_empty());
    }

    #[test]
    fn search_workspace_skips_binary_or_unreadable_files() {
        let tmp = TempDir::new().unwrap();
        write(&tmp.path().join("text.txt"), "hello world");
        // A non-utf8 file: read_to_string returns Err, we silently skip.
        std::fs::write(tmp.path().join("blob.bin"), [0u8, 159, 146, 150]).unwrap();
        let hits = search_workspace(tmp.path(), "hello", SearchOpts::default());
        assert_eq!(hits.len(), 1);
        assert!(hits[0].path.ends_with("text.txt"));
    }

    #[test]
    fn search_workspace_returns_every_match_with_no_arbitrary_cap() {
        // Regression: the engine used to silently truncate at 200 matches,
        // which the user (correctly) called rubbish. Five files, each with
        // 100 lines containing the needle, must all show up.
        let tmp = TempDir::new().unwrap();
        for i in 0..5 {
            let many = (0..100).map(|_| "needle\n").collect::<String>();
            write(&tmp.path().join(format!("f{i}.txt")), &many);
        }
        let hits = search_workspace(tmp.path(), "needle", SearchOpts::default());
        assert_eq!(
            hits.len(),
            500,
            "engine must surface every match - silent truncation is unacceptable"
        );
    }

    #[test]
    fn search_panel_run_query_populates_hits_and_resets_selection() {
        let tmp = TempDir::new().unwrap();
        write(&tmp.path().join("x.txt"), "one\ntwo\nthree\n");
        let mut panel = SearchPanel::new(tmp.path().to_path_buf());
        panel.query = "two".into();
        panel.run_query();
        assert_eq!(panel.hits.len(), 1);
        assert_eq!(panel.selected, 0);
    }

    /// Layout x-offset for the typed-content cell, relative to `inner.x`:
    /// chevron (1) + gap (1) + field bar (1) + 2-cell magnifier glyph
    /// (codicon + 1 cell gap) = 5.
    const INPUT_TYPED_COL: u16 = 5;
    /// Layout y-offset for the input content row, relative to `inner.y`:
    /// header + blank + content = 2. The single filled field has no top
    /// border row, so the query sits one row higher than the old 3-row box.
    const INPUT_CONTENT_ROW: u16 = 2;

    #[test]
    fn cursor_sits_at_input_start_when_query_empty_and_focused() {
        use ratatui::buffer::Buffer;
        let tmp = TempDir::new().unwrap();
        let mut panel = SearchPanel::new(tmp.path().to_path_buf());
        panel.focused = true;
        let area = Rect {
            x: 0,
            y: 0,
            width: 60,
            height: 12,
        };
        let mut buf = Buffer::empty(area);
        ratatui::widgets::Widget::render(&mut panel, area, &mut buf);
        let inner_x = panel.last_inner.x;
        let inner_y = panel.last_inner.y;
        // The caret (driven as the host's blinking hardware caret, like the
        // commit box) sits at the start of the input content when empty.
        assert_eq!(
            panel.cursor_screen_pos(),
            Some((inner_x + INPUT_TYPED_COL, inner_y + INPUT_CONTENT_ROW)),
            "caret must sit at the start of the input content area when the query is empty"
        );
    }

    #[test]
    fn cursor_sits_after_typed_text_when_query_non_empty_and_focused() {
        use ratatui::buffer::Buffer;
        let tmp = TempDir::new().unwrap();
        let mut panel = SearchPanel::new(tmp.path().to_path_buf());
        panel.focused = true;
        panel.query = String::from("foo");
        let area = Rect {
            x: 0,
            y: 0,
            width: 60,
            height: 12,
        };
        let mut buf = Buffer::empty(area);
        ratatui::widgets::Widget::render(&mut panel, area, &mut buf);
        let inner_x = panel.last_inner.x;
        let inner_y = panel.last_inner.y;
        // INPUT_TYPED_COL + "foo" (3 cells) → caret sits 3 cells past the start
        // of typed content.
        assert_eq!(
            panel.cursor_screen_pos(),
            Some((inner_x + INPUT_TYPED_COL + 3, inner_y + INPUT_CONTENT_ROW)),
            "caret must sit immediately after the typed query"
        );
    }

    fn buffer_to_string(buf: &Buffer) -> String {
        let mut out = String::new();
        for y in buf.area.y..buf.area.y + buf.area.height {
            for x in buf.area.x..buf.area.x + buf.area.width {
                out.push_str(buf[(x, y)].symbol());
            }
            out.push('\n');
        }
        out
    }

    #[test]
    fn header_row_says_search_in_upper_case() {
        // Mockup-driven: an uppercase "SEARCH" header sits at the top-left
        // of the inner area, in light grey — replacing the old dark-blue
        // title bar that used to overlap the outer border.
        use ratatui::buffer::Buffer;
        let tmp = TempDir::new().unwrap();
        let mut panel = SearchPanel::new(tmp.path().to_path_buf());
        let area = Rect {
            x: 0,
            y: 0,
            width: 60,
            height: 10,
        };
        let mut buf = Buffer::empty(area);
        ratatui::widgets::Widget::render(&mut panel, area, &mut buf);
        let inner_x = panel.last_inner.x;
        let inner_y = panel.last_inner.y;
        let mut header = String::new();
        for x in inner_x..inner_x + 8 {
            header.push_str(buf[(x, inner_y)].symbol());
        }
        assert!(
            header.starts_with("SEARCH"),
            "first inner row must start with the uppercase SEARCH header; got: {header:?}"
        );
        // Outer-border title row must NOT carry a SEARCH chip — the mockup
        // has no title bar; the header lives inside the panel.
        let mut top_border = String::new();
        for x in area.x..area.x + area.width {
            top_border.push_str(buf[(x, area.y)].symbol());
        }
        assert!(
            !top_border.contains("SEARCH"),
            "outer border row must not carry a SEARCH title chip: {top_border:?}"
        );
    }

    #[test]
    fn input_box_renders_a_thin_focused_border_three_rows_tall() {
        use ratatui::buffer::Buffer;
        let tmp = TempDir::new().unwrap();
        let mut panel = SearchPanel::new(tmp.path().to_path_buf());
        panel.focused = true;
        let area = Rect {
            x: 0,
            y: 0,
            width: 60,
            height: 12,
        };
        let mut buf = Buffer::empty(area);
        ratatui::widgets::Widget::render(&mut panel, area, &mut buf);
        let dump = buffer_to_string(&buf);
        // The input box must render a single-line top-left corner glyph
        // somewhere in the upper half of the panel.
        assert!(
            dump.contains('┌') || dump.contains('╭'),
            "input box must have a top-left corner; got buffer:\n{dump}"
        );
        assert!(
            dump.contains('└') || dump.contains('╰'),
            "input box must have a bottom-left corner so the border reads as a 3-row box, not a single rule:\n{dump}"
        );
    }

    #[test]
    fn rendering_at_narrow_widths_does_not_panic() {
        use ratatui::buffer::Buffer;
        let tmp = TempDir::new().unwrap();
        for width in 0u16..40 {
            for height in 0u16..20 {
                let mut panel = SearchPanel::new(tmp.path().to_path_buf());
                let area = Rect {
                    x: 0,
                    y: 0,
                    width,
                    height,
                };
                if area.width == 0 || area.height == 0 {
                    continue;
                }
                let mut buf = Buffer::empty(area);
                ratatui::widgets::Widget::render(&mut panel, area, &mut buf);
            }
        }
    }

    #[test]
    fn asterisk_keeps_breathing_room_against_the_outer_right_border() {
        // User report: the asterisk in `.*` was sitting flush against the
        // outer right border, looking crowded into the corner. The fix
        // mirrors the gap that already exists between the input box and
        // `Aa` so the toggle cluster reads as a contained group on both
        // sides.
        use ratatui::buffer::Buffer;
        let tmp = TempDir::new().unwrap();
        let mut panel = SearchPanel::new(tmp.path().to_path_buf());
        let area = Rect {
            x: 0,
            y: 0,
            width: 60,
            height: 12,
        };
        let mut buf = Buffer::empty(area);
        ratatui::widgets::Widget::render(&mut panel, area, &mut buf);
        let inner = panel.last_inner;
        // `.*` toggle is 2 cells wide starting at toggle_regex_x.
        let regex_end = panel.toggle_regex_x + 1;
        let inner_right = inner.x + inner.width - 1;
        let right_gap = inner_right.saturating_sub(regex_end);
        assert!(
            right_gap >= 1,
            "asterisk must not sit flush against the outer right border (right_gap={right_gap})"
        );
    }

    #[test]
    fn input_row_carries_a_chevron_and_pipe_separated_toggles() {
        use ratatui::buffer::Buffer;
        let tmp = TempDir::new().unwrap();
        let mut panel = SearchPanel::new(tmp.path().to_path_buf());
        let area = Rect {
            x: 0,
            y: 0,
            width: 60,
            height: 10,
        };
        let mut buf = Buffer::empty(area);
        ratatui::widgets::Widget::render(&mut panel, area, &mut buf);
        let dump = buffer_to_string(&buf);
        // VS Code's expand-to-replace chevron points RIGHT (▸) while the
        // Replace row is collapsed and flips DOWN (▾) once expanded. A fresh
        // panel starts collapsed, so the right chevron must render.
        assert!(
            dump.contains('▸'),
            "collapsed panel must show a right-pointing expand chevron on the left:\n{dump}"
        );
        // Now expand the Replace row: the chevron flips to a down-arrow.
        panel.toggle_replace();
        let mut buf2 = Buffer::empty(area);
        ratatui::widgets::Widget::render(&mut panel, area, &mut buf2);
        let dump2 = buffer_to_string(&buf2);
        assert!(
            dump2.contains('▾'),
            "expanded panel must flip the chevron to a down-arrow:\n{dump2}"
        );
        // Toggles separated by pipe '│' so the trio reads "Aa │ ab │ .*".
        assert!(
            dump.contains("Aa") && dump.contains("ab") && dump.contains(".*"),
            "all three toggle glyphs must render:\n{dump}"
        );
        let pipes = dump.matches('│').count();
        assert!(
            pipes >= 2,
            "at least two pipe separators must sit between the three toggles; got {pipes} pipes in:\n{dump}"
        );
    }

    #[test]
    fn results_start_below_the_input_box_with_a_separator_in_between() {
        use ratatui::buffer::Buffer;
        let tmp = TempDir::new().unwrap();
        write(&tmp.path().join("a.txt"), "needle\n");
        let mut panel = SearchPanel::new(tmp.path().to_path_buf());
        panel.query = String::from("needle");
        panel.run_query();
        assert_eq!(panel.hits.len(), 1);
        let area = Rect {
            x: 0,
            y: 0,
            width: 60,
            height: 14,
        };
        let mut buf = Buffer::empty(area);
        ratatui::widgets::Widget::render(&mut panel, area, &mut buf);
        let dump = buffer_to_string(&buf);
        let row_with_needle = dump
            .lines()
            .position(|l| l.contains("needle") && l.contains("a.txt"))
            .expect("result row with needle must render");
        assert!(
            row_with_needle >= 5,
            "result row must sit below the input box (expected row >= 5, got {row_with_needle}):\n{dump}"
        );
    }

    #[test]
    fn search_panel_does_not_render_paste_button_in_input_row() {
        use ratatui::buffer::Buffer;
        let tmp = TempDir::new().unwrap();
        let mut panel = SearchPanel::new(tmp.path().to_path_buf());
        panel.focused = true;
        let area = Rect {
            x: 0,
            y: 0,
            width: 60,
            height: 5,
        };
        let mut buf = Buffer::empty(area);
        ratatui::widgets::Widget::render(&mut panel, area, &mut buf);
        assert_eq!(
            panel.paste_button_w, 0,
            "paste button should not reserve cells"
        );
        let mut collected = String::new();
        for x in area.x..area.x + area.width {
            collected.push_str(buf[(x, 1)].symbol());
        }
        assert!(
            !collected.to_lowercase().contains("paste"),
            "input row should not visibly contain a Paste button, got: {collected:?}"
        );
    }

    #[test]
    fn paste_button_at_returns_false_when_button_hidden() {
        use ratatui::buffer::Buffer;
        let tmp = TempDir::new().unwrap();
        let mut panel = SearchPanel::new(tmp.path().to_path_buf());
        panel.focused = true;
        let area = Rect {
            x: 0,
            y: 0,
            width: 60,
            height: 5,
        };
        let mut buf = Buffer::empty(area);
        ratatui::widgets::Widget::render(&mut panel, area, &mut buf);
        assert_eq!(panel.paste_button_w, 0);
        assert!(!panel.paste_button_at(0, 1));
        assert!(!panel.paste_button_at(40, 1));
    }

    #[test]
    fn split_highlight_returns_whole_line_when_needle_empty() {
        let segs = split_for_highlight("hello world", "", SearchOpts::default());
        assert_eq!(segs, vec![(String::from("hello world"), false)]);
    }

    #[test]
    fn split_highlight_returns_whole_line_when_no_match() {
        let segs = split_for_highlight("hello world", "xyz", SearchOpts::default());
        assert_eq!(segs, vec![(String::from("hello world"), false)]);
    }

    #[test]
    fn split_highlight_marks_each_match_run_case_insensitive() {
        let segs = split_for_highlight(
            "The Quick brown fox jumps over the QUICK fence",
            "quick",
            SearchOpts::default(),
        );
        // Two runs of "quick" (mixed case), three non-match tails.
        let matches: Vec<&String> = segs
            .iter()
            .filter_map(|(s, m)| if *m { Some(s) } else { None })
            .collect();
        assert_eq!(matches.len(), 2);
        // Original-case slice must be preserved in the matched segment.
        assert_eq!(matches[0], "Quick");
        assert_eq!(matches[1], "QUICK");
        // Concatenating all segments must reproduce the original line.
        let joined: String = segs.iter().map(|(s, _)| s.as_str()).collect();
        assert_eq!(joined, "The Quick brown fox jumps over the QUICK fence");
    }

    #[test]
    fn split_highlight_handles_match_at_start_and_end() {
        let segs = split_for_highlight("foo bar foo", "foo", SearchOpts::default());
        let joined: String = segs.iter().map(|(s, _)| s.as_str()).collect();
        assert_eq!(joined, "foo bar foo");
        assert_eq!(
            segs.first().map(|(_, m)| *m),
            Some(true),
            "first seg is match"
        );
        assert_eq!(
            segs.last().map(|(_, m)| *m),
            Some(true),
            "last seg is match"
        );
    }

    fn collect_until_done(
        rx: &std::sync::mpsc::Receiver<SearchEvent>,
        deadline: std::time::Duration,
    ) -> (Option<String>, Vec<SearchHit>, bool) {
        let until = std::time::Instant::now() + deadline;
        let mut last_q: Option<String> = None;
        let mut hits: Vec<SearchHit> = Vec::new();
        let mut saw_done = false;
        while std::time::Instant::now() < until {
            let remaining = until.saturating_duration_since(std::time::Instant::now());
            match rx.recv_timeout(remaining.max(std::time::Duration::from_millis(50))) {
                Ok(SearchEvent::Hits(q, _, batch)) => {
                    last_q = Some(q);
                    hits.extend(batch);
                }
                Ok(SearchEvent::Done(q, _)) => {
                    last_q = Some(q);
                    saw_done = true;
                    break;
                }
                Err(_) => break,
            }
        }
        (last_q, hits, saw_done)
    }

    #[test]
    fn search_worker_loop_searches_dirty_buffer_and_skips_stale_disk_copy() {
        // Disk holds the OLD name; the unsaved editor buffer holds the NEW
        // name. Dirty-aware search must find the new name (from the buffer)
        // and must NOT find the old name (the dirty file's stale disk copy is
        // skipped). This is the VS Code / Zed behavior the reported bug lacked.
        let tmp = TempDir::new().unwrap();
        let path = tmp.path().join("a.txt");
        write(&path, "oldname\n");
        let (q_tx, q_rx) = std::sync::mpsc::channel::<SearchRequest>();
        let (e_tx, e_rx) = std::sync::mpsc::channel::<SearchEvent>();
        let root = tmp.path().to_path_buf();
        let join = std::thread::spawn(move || search_worker_loop(root, q_rx, e_tx));

        q_tx.send(SearchRequest::SetDirtyBuffers(vec![(
            path.clone(),
            "newname\n".into(),
        )]))
        .unwrap();

        q_tx.send(SearchRequest::Query(
            "newname".into(),
            SearchOpts::default(),
        ))
        .unwrap();
        let (_, hits, done) = collect_until_done(&e_rx, std::time::Duration::from_secs(2));
        assert!(done);
        assert_eq!(hits.len(), 1, "the unsaved buffer's new name must be found");
        assert_eq!(hits[0].line_text, "newname");

        q_tx.send(SearchRequest::Query(
            "oldname".into(),
            SearchOpts::default(),
        ))
        .unwrap();
        let (_, stale, done2) = collect_until_done(&e_rx, std::time::Duration::from_secs(2));
        assert!(done2);
        assert_eq!(
            stale.len(),
            0,
            "the stale on-disk copy of a dirty file must be skipped"
        );

        drop(q_tx);
        join.join().unwrap();
    }

    #[test]
    fn search_worker_loop_returns_hits_for_a_typed_query() {
        let tmp = TempDir::new().unwrap();
        write(&tmp.path().join("a.txt"), "one\nneedle\nthree\n");
        let (q_tx, q_rx) = std::sync::mpsc::channel::<SearchRequest>();
        let (e_tx, e_rx) = std::sync::mpsc::channel::<SearchEvent>();
        let root = tmp.path().to_path_buf();
        let join = std::thread::spawn(move || search_worker_loop(root, q_rx, e_tx));
        q_tx.send(SearchRequest::Query("needle".into(), SearchOpts::default()))
            .unwrap();
        let (q, hits, done) = collect_until_done(&e_rx, std::time::Duration::from_secs(2));
        assert_eq!(q.as_deref(), Some("needle"));
        assert_eq!(hits.len(), 1);
        assert!(done, "worker must terminate the stream with a Done event");
        drop(q_tx);
        join.join().unwrap();
    }

    #[test]
    fn search_worker_loop_streams_per_file_batches_for_a_typed_query() {
        // Three files each containing matches must surface as separate Hits
        // batches followed by exactly one Done. This pins the streaming
        // protocol so the UI can show "N matches (still searching)".
        let tmp = TempDir::new().unwrap();
        write(&tmp.path().join("a.txt"), "needle\n");
        write(&tmp.path().join("b.txt"), "needle\nneedle\n");
        write(&tmp.path().join("c.txt"), "needle\n");
        let (q_tx, q_rx) = std::sync::mpsc::channel::<SearchRequest>();
        let (e_tx, e_rx) = std::sync::mpsc::channel::<SearchEvent>();
        let root = tmp.path().to_path_buf();
        let join = std::thread::spawn(move || search_worker_loop(root, q_rx, e_tx));
        q_tx.send(SearchRequest::Query("needle".into(), SearchOpts::default()))
            .unwrap();
        let mut total = 0usize;
        let mut hits_events = 0usize;
        let mut done_events = 0usize;
        let until = std::time::Instant::now() + std::time::Duration::from_secs(3);
        while std::time::Instant::now() < until {
            match e_rx.recv_timeout(std::time::Duration::from_millis(500)) {
                Ok(SearchEvent::Hits(_, _, batch)) => {
                    hits_events += 1;
                    total += batch.len();
                }
                Ok(SearchEvent::Done(_, _)) => {
                    done_events += 1;
                    break;
                }
                Err(_) => break,
            }
        }
        drop(q_tx);
        join.join().unwrap();
        assert_eq!(total, 4, "every match across all files must arrive");
        assert!(
            hits_events >= 3,
            "expected per-file batches, got {hits_events}"
        );
        assert_eq!(done_events, 1, "exactly one Done sentinel per request");
    }

    #[test]
    fn search_worker_loop_coalesces_a_burst_of_keystrokes() {
        // Simulate a fast typist sending o, on, one. Worker must coalesce
        // and only run the latest, returning hits for "one" — not for the
        // intermediate prefixes.
        let tmp = TempDir::new().unwrap();
        write(&tmp.path().join("a.txt"), "alpha one beta\nzeta\n");
        let (q_tx, q_rx) = std::sync::mpsc::channel::<SearchRequest>();
        let (e_tx, e_rx) = std::sync::mpsc::channel::<SearchEvent>();
        let root = tmp.path().to_path_buf();
        let join = std::thread::spawn(move || search_worker_loop(root, q_rx, e_tx));
        q_tx.send(SearchRequest::Query("o".into(), SearchOpts::default()))
            .unwrap();
        q_tx.send(SearchRequest::Query("on".into(), SearchOpts::default()))
            .unwrap();
        q_tx.send(SearchRequest::Query("one".into(), SearchOpts::default()))
            .unwrap();
        let (q, hits, done) = collect_until_done(&e_rx, std::time::Duration::from_secs(2));
        assert_eq!(
            q.as_deref(),
            Some("one"),
            "coalesce must drop intermediate prefixes"
        );
        assert_eq!(hits.len(), 1);
        assert!(done);
        drop(q_tx);
        join.join().unwrap();
    }

    #[test]
    fn search_worker_loop_resets_debounce_on_each_keystroke() {
        // Trailing-edge debounce: keys arriving within the quiet window
        // (200 ms) restart the timer, so the user typing "one" with
        // 100 ms between each keystroke must fire EXACTLY ONE search at
        // the end - for "one" - not three searches for "o", "on", "one".
        let tmp = TempDir::new().unwrap();
        write(&tmp.path().join("a.txt"), "alpha one beta\nzeta\n");
        let (q_tx, q_rx) = std::sync::mpsc::channel::<SearchRequest>();
        let (e_tx, e_rx) = std::sync::mpsc::channel::<SearchEvent>();
        let root = tmp.path().to_path_buf();
        let join = std::thread::spawn(move || search_worker_loop(root, q_rx, e_tx));
        // Each gap is well under SEARCH_DEBOUNCE (200 ms), so each new
        // send resets the timer and no search should fire mid-burst.
        q_tx.send(SearchRequest::Query("o".into(), SearchOpts::default()))
            .unwrap();
        std::thread::sleep(std::time::Duration::from_millis(100));
        q_tx.send(SearchRequest::Query("on".into(), SearchOpts::default()))
            .unwrap();
        std::thread::sleep(std::time::Duration::from_millis(100));
        q_tx.send(SearchRequest::Query("one".into(), SearchOpts::default()))
            .unwrap();
        // Now stop typing and let the debounce expire + the scan finish.
        let mut dones: Vec<String> = Vec::new();
        let until = std::time::Instant::now() + std::time::Duration::from_secs(2);
        while std::time::Instant::now() < until {
            if let Ok(SearchEvent::Done(q, _)) =
                e_rx.recv_timeout(std::time::Duration::from_millis(200))
            {
                dones.push(q)
            }
        }
        drop(q_tx);
        join.join().unwrap();
        assert_eq!(
            dones,
            vec!["one".to_string()],
            "exactly one search should have fired - for the trailing query - not one per keystroke"
        );
    }

    #[test]
    fn search_worker_loop_cancels_in_flight_scan_when_a_new_request_arrives() {
        // User-reported "0 matches" bug: a fast typist hits "c", which
        // matches almost every line of every file. The worker used to
        // block on that giant scan and ignore "cl"/"cla" until "c"
        // finished, so the user would stare at "0 matches" while typing.
        // Workspace: 200 files × 20000 lines = 4M matches. Without
        // cancellation, the "c" scan takes many seconds. With cancellation
        // wired up, switching to a no-match query must produce Done within
        // a tight window.
        let tmp = TempDir::new().unwrap();
        for i in 0..200 {
            let many = (0..20000)
                .map(|n| format!("ccc line {n}\n"))
                .collect::<String>();
            write(&tmp.path().join(format!("f{i}.txt")), &many);
        }
        let (q_tx, q_rx) = std::sync::mpsc::channel::<SearchRequest>();
        let (e_tx, e_rx) = std::sync::mpsc::channel::<SearchEvent>();
        let root = tmp.path().to_path_buf();
        let join = std::thread::spawn(move || search_worker_loop(root, q_rx, e_tx));
        q_tx.send(SearchRequest::Query("c".into(), SearchOpts::default()))
            .unwrap();
        // Stage on the scan observably RUNNING — the first Hits batch —
        // not on sleeping past the debounce: a sleep equal to the
        // debounce races the worker's own timer on a knife edge, and
        // when the send wins the new query displaces "c" in the debounce
        // loop, silently skipping the cancellation path this test exists
        // to cover (#68).
        let mut c_hits = 0usize;
        let staging = std::time::Instant::now() + std::time::Duration::from_secs(30);
        while c_hits == 0 && std::time::Instant::now() < staging {
            if let Ok(SearchEvent::Hits(q, _, hits)) =
                e_rx.recv_timeout(std::time::Duration::from_millis(100))
                && q == "c"
            {
                c_hits += hits.len();
            }
        }
        assert!(
            c_hits > 0,
            "staging: the \"c\" scan never started streaming"
        );
        q_tx.send(SearchRequest::Query(
            "zzznoneatall".into(),
            SearchOpts::default(),
        ))
        .unwrap();
        // The cancellation proof is structural, not wall-clock (#68): a
        // cancelled "c" scan is cut short, streaming fewer than its full
        // 4M matches before its Done; an uncancelled one streams every
        // single match first. A tight arrival deadline on the second Done
        // measured the same fact through scheduler latency and flaked
        // under suite load — the long timeout here only guards a hang.
        let deadline = std::time::Instant::now() + std::time::Duration::from_secs(60);
        let mut second_done = false;
        while std::time::Instant::now() < deadline {
            let remaining = deadline.saturating_duration_since(std::time::Instant::now());
            match e_rx.recv_timeout(remaining.max(std::time::Duration::from_millis(50))) {
                Ok(SearchEvent::Hits(q, _, hits)) if q == "c" => c_hits += hits.len(),
                Ok(SearchEvent::Done(q, _)) if q == "zzznoneatall" => {
                    second_done = true;
                    break;
                }
                _ => {}
            }
        }
        drop(q_tx);
        join.join().unwrap();
        assert!(
            second_done,
            "worker must emit Done for the new query after cancelling the in-flight scan"
        );
        assert!(
            c_hits < 4_000_000,
            "the in-flight scan must be cancelled, not run to completion; it streamed all {c_hits} matches"
        );
    }

    #[test]
    fn search_worker_loop_short_circuits_empty_queries() {
        let tmp = TempDir::new().unwrap();
        write(&tmp.path().join("a.txt"), "anything\n");
        let (q_tx, q_rx) = std::sync::mpsc::channel::<SearchRequest>();
        let (e_tx, e_rx) = std::sync::mpsc::channel::<SearchEvent>();
        let root = tmp.path().to_path_buf();
        let join = std::thread::spawn(move || search_worker_loop(root, q_rx, e_tx));
        q_tx.send(SearchRequest::Query("".into(), SearchOpts::default()))
            .unwrap();
        let (q, hits, done) = collect_until_done(&e_rx, std::time::Duration::from_secs(2));
        assert_eq!(q.as_deref(), Some(""));
        assert!(hits.is_empty());
        assert!(done, "empty query still emits a Done sentinel");
        drop(q_tx);
        join.join().unwrap();
    }

    #[test]
    fn search_worker_loop_targets_the_directory_set_by_the_latest_set_root() {
        let dir_a = TempDir::new().unwrap();
        write(&dir_a.path().join("a.txt"), "alpha only\n");
        let dir_b = TempDir::new().unwrap();
        write(&dir_b.path().join("b.txt"), "the needle is here\n");
        let (q_tx, q_rx) = std::sync::mpsc::channel::<SearchRequest>();
        let (e_tx, e_rx) = std::sync::mpsc::channel::<SearchEvent>();
        let start_root = dir_a.path().to_path_buf();
        let join = std::thread::spawn(move || search_worker_loop(start_root, q_rx, e_tx));
        q_tx.send(SearchRequest::SetRoot(dir_b.path().to_path_buf()))
            .unwrap();
        q_tx.send(SearchRequest::Query("needle".into(), SearchOpts::default()))
            .unwrap();
        let (q, hits, done) = collect_until_done(&e_rx, std::time::Duration::from_secs(2));
        assert_eq!(q.as_deref(), Some("needle"));
        assert_eq!(
            hits.len(),
            1,
            "after SetRoot, the worker must search the new directory, not the one captured at spawn"
        );
        assert!(
            hits[0].path.starts_with(dir_b.path()),
            "the hit must come from the directory the Explorer now points at"
        );
        assert!(done);
        drop(q_tx);
        join.join().unwrap();
    }

    #[test]
    fn search_opts_default_matches_legacy_case_insensitive_substring() {
        let mut out = Vec::new();
        let opts = SearchOpts::default();
        collect_matches_in_text(
            Path::new("a.txt"),
            "Hello World\nfoo bar\nHELLO again\nno match",
            "hello",
            opts,
            &mut out,
        );
        assert_eq!(out.len(), 2, "default opts mirror the original behaviour");
    }

    #[test]
    fn search_opts_case_sensitive_excludes_different_case() {
        let mut out = Vec::new();
        let opts = SearchOpts {
            case_sensitive: true,
            ..Default::default()
        };
        collect_matches_in_text(
            Path::new("a.txt"),
            "Hello World\nhello again\nHELLO loud",
            "hello",
            opts,
            &mut out,
        );
        assert_eq!(out.len(), 1);
        assert_eq!(out[0].line_text, "hello again");
    }

    #[test]
    fn search_opts_whole_word_excludes_partial_matches() {
        let mut out = Vec::new();
        let opts = SearchOpts {
            whole_word: true,
            ..Default::default()
        };
        collect_matches_in_text(
            Path::new("a.txt"),
            "cat sat\ncategory animal\ncat\nbobcat sleeps",
            "cat",
            opts,
            &mut out,
        );
        // Only the lines where 'cat' is a standalone word should match.
        let texts: Vec<&str> = out.iter().map(|h| h.line_text.as_str()).collect();
        assert_eq!(texts, vec!["cat sat", "cat"]);
    }

    #[test]
    fn search_opts_regex_finds_pattern_class() {
        let mut out = Vec::new();
        let opts = SearchOpts {
            use_regex: true,
            ..Default::default()
        };
        collect_matches_in_text(
            Path::new("a.txt"),
            "let x = 42;\nlet y = abc;\nlet z = 13;\nno number",
            r"\d+",
            opts,
            &mut out,
        );
        let texts: Vec<&str> = out.iter().map(|h| h.line_text.as_str()).collect();
        assert_eq!(texts, vec!["let x = 42;", "let z = 13;"]);
    }

    #[test]
    fn search_opts_regex_honours_case_sensitive_flag() {
        let mut out = Vec::new();
        let opts = SearchOpts {
            use_regex: true,
            case_sensitive: true,
            ..Default::default()
        };
        collect_matches_in_text(Path::new("a.txt"), "Foo\nfoo\nFOO", r"foo", opts, &mut out);
        let texts: Vec<&str> = out.iter().map(|h| h.line_text.as_str()).collect();
        assert_eq!(texts, vec!["foo"], "regex must respect case-sensitive flag");
    }

    #[test]
    fn search_opts_invalid_regex_returns_empty_quietly() {
        let mut out = Vec::new();
        let opts = SearchOpts {
            use_regex: true,
            ..Default::default()
        };
        collect_matches_in_text(
            Path::new("a.txt"),
            "anything\nat all",
            r"[unclosed",
            opts,
            &mut out,
        );
        assert!(
            out.is_empty(),
            "invalid regex must not crash, just yield no hits"
        );
    }

    #[test]
    fn toggle_at_maps_a_click_on_each_glyph_to_the_right_kind() {
        use ratatui::buffer::Buffer;
        let tmp = TempDir::new().unwrap();
        let mut panel = SearchPanel::new(tmp.path().to_path_buf());
        panel.focused = true;
        let area = Rect {
            x: 0,
            y: 0,
            width: 60,
            height: 12,
        };
        let mut buf = Buffer::empty(area);
        ratatui::widgets::Widget::render(&mut panel, area, &mut buf);
        let y = panel.toggle_y;
        // Each toggle is 2 cells; clicking either cell on the same row
        // should map to its kind.
        assert_eq!(
            panel.toggle_at(panel.toggle_case_x, y),
            Some(SearchToggle::CaseSensitive)
        );
        assert_eq!(
            panel.toggle_at(panel.toggle_case_x + 1, y),
            Some(SearchToggle::CaseSensitive)
        );
        assert_eq!(
            panel.toggle_at(panel.toggle_word_x, y),
            Some(SearchToggle::WholeWord)
        );
        assert_eq!(
            panel.toggle_at(panel.toggle_regex_x + 1, y),
            Some(SearchToggle::UseRegex)
        );
        // Wrong row → None.
        assert_eq!(panel.toggle_at(panel.toggle_case_x, y + 1), None);
        // Outside the toggle columns → None.
        assert_eq!(panel.toggle_at(0, y), None);
    }

    #[test]
    fn tooltip_at_labels_each_actionable_control_and_skips_inert_cells() {
        use ratatui::buffer::Buffer;
        let tmp = TempDir::new().unwrap();
        let mut panel = SearchPanel::new(tmp.path().to_path_buf());
        panel.focused = true;
        let area = Rect {
            x: 0,
            y: 0,
            width: 60,
            height: 12,
        };
        let mut buf = Buffer::empty(area);
        ratatui::widgets::Widget::render(&mut panel, area, &mut buf);
        let y = panel.toggle_y;
        assert_eq!(panel.tooltip_at(panel.toggle_case_x, y), Some("Match Case"));
        assert_eq!(
            panel.tooltip_at(panel.toggle_word_x, y),
            Some("Match Whole Word")
        );
        assert_eq!(
            panel.tooltip_at(panel.toggle_regex_x, y),
            Some("Use Regular Expression")
        );
        assert_eq!(
            panel.tooltip_at(panel.refresh_x, panel.refresh_y),
            Some("Refresh")
        );
        assert_eq!(
            panel.tooltip_at(panel.clear_x, panel.clear_y),
            Some("Clear Search Results")
        );
        assert_eq!(
            panel.tooltip_at(panel.chevron_x, panel.chevron_y),
            Some("Toggle Replace"),
            "the chevron reads as expand while the Replace row is collapsed"
        );
        // A plain text cell on the query row carries no tooltip.
        assert_eq!(panel.tooltip_at(panel.toggle_case_x, y + 5), None);
    }

    #[test]
    fn search_panel_navigation_clamps() {
        let tmp = TempDir::new().unwrap();
        write(&tmp.path().join("x.txt"), "a needle\nb needle\nc needle\n");
        let mut panel = SearchPanel::new(tmp.path().to_path_buf());
        panel.query = "needle".into();
        panel.run_query();
        assert_eq!(panel.hits.len(), 3);
        panel.move_up();
        assert_eq!(panel.selected, 0);
        panel.move_down();
        panel.move_down();
        panel.move_down(); // would go to 3, but clamped to 2
        assert_eq!(panel.selected, 2);
    }

    fn make_panel_with_hits(tmp: &TempDir, n: usize) -> SearchPanel {
        let mut panel = SearchPanel::new(tmp.path().to_path_buf());
        panel.query = "needle".into();
        panel.hits = (0..n)
            .map(|i| SearchHit {
                path: tmp.path().join("a.txt"),
                line_no: i + 1,
                line_text: format!("line {i}"),
            })
            .collect();
        panel
    }

    #[test]
    fn hovering_an_unselected_result_row_lifts_it() {
        let tmp = TempDir::new().unwrap();
        let mut panel = make_panel_with_hits(&tmp, 20);
        panel.focus_gradient = false; // Croft Dark
        panel.theme = crate::theme::Theme::DARK_BLUE;
        panel.selected = 0;
        panel.scroll = 0;
        let area = Rect {
            x: 0,
            y: 0,
            width: 60,
            height: 30,
        };
        // Results start at inner.y + 7 (see the click-mapping in `result_at`);
        // hover the second result row, which is not the selected one.
        let row_y = area.y + 1 + 7 + 1;
        panel.hover_pointer = Some((area.x + 1, row_y));
        let mut buf = ratatui::buffer::Buffer::empty(area);
        ratatui::widgets::Widget::render(&mut panel, area, &mut buf);
        assert_eq!(
            buf[(area.x + 1, row_y)].bg,
            Color::Rgb(0x2b, 0x31, 0x42),
            "the hovered unselected result row wears the Croft Dark hover lift"
        );
    }

    #[test]
    fn render_preserves_user_scroll_when_selected_is_at_top() {
        // User-reported bug: scroll appears to "come in batches" because
        // every render yanks scroll back to selected=0. Wheel scrolling
        // must be decoupled from selection auto-follow so streaming hits
        // and re-renders never fight the user's scroll position.
        let tmp = TempDir::new().unwrap();
        let mut panel = make_panel_with_hits(&tmp, 100);
        panel.selected = 0;
        panel.scroll = 30;
        let area = Rect {
            x: 0,
            y: 0,
            width: 60,
            height: 30,
        };
        let mut buf = ratatui::buffer::Buffer::empty(area);
        ratatui::widgets::Widget::render(&mut panel, area, &mut buf);
        assert_eq!(
            panel.scroll, 30,
            "render must not pull scroll back to selected when the user has wheeled away"
        );
    }

    #[test]
    fn appending_streamed_hits_does_not_reset_user_scroll() {
        // Simulates the streaming engine: user wheels down to row 30,
        // then a new batch arrives via `hits.extend(batch)`. The next
        // render must keep scroll at 30, not jump to 0.
        let tmp = TempDir::new().unwrap();
        let mut panel = make_panel_with_hits(&tmp, 80);
        panel.selected = 0;
        panel.scroll = 30;
        let area = Rect {
            x: 0,
            y: 0,
            width: 60,
            height: 30,
        };
        let mut buf = ratatui::buffer::Buffer::empty(area);
        ratatui::widgets::Widget::render(&mut panel, area, &mut buf);
        // Streaming batch: another 50 hits arrive.
        let extra: Vec<SearchHit> = (80..130)
            .map(|i| SearchHit {
                path: tmp.path().join("a.txt"),
                line_no: i + 1,
                line_text: format!("line {i}"),
            })
            .collect();
        panel.hits.extend(extra);
        ratatui::widgets::Widget::render(&mut panel, area, &mut buf);
        assert_eq!(
            panel.scroll, 30,
            "streaming hits must not reset the user's scroll offset"
        );
    }

    #[test]
    fn move_down_past_bottom_of_viewport_advances_scroll_to_keep_selection_visible() {
        let tmp = TempDir::new().unwrap();
        let mut panel = make_panel_with_hits(&tmp, 100);
        // Pretend a 30-row panel was rendered: viewport = 30 - 5 = 25 rows
        // (the single-row query field starts results two rows higher than the
        // old 3-row box did).
        panel.last_inner = Rect {
            x: 0,
            y: 0,
            width: 60,
            height: 30,
        };
        panel.selected = 24;
        panel.scroll = 0;
        panel.move_down();
        assert_eq!(panel.selected, 25);
        assert_eq!(
            panel.scroll, 1,
            "arrow-down past the bottom row must scroll to keep selection visible"
        );
    }

    #[test]
    fn move_up_above_top_of_viewport_retreats_scroll_to_keep_selection_visible() {
        let tmp = TempDir::new().unwrap();
        let mut panel = make_panel_with_hits(&tmp, 100);
        panel.last_inner = Rect {
            x: 0,
            y: 0,
            width: 60,
            height: 30,
        };
        panel.selected = 50;
        panel.scroll = 50;
        panel.move_up();
        assert_eq!(panel.selected, 49);
        assert_eq!(
            panel.scroll, 49,
            "arrow-up above the first visible row must scroll back to keep selection visible"
        );
    }

    #[test]
    fn scroll_down_via_wheel_does_not_change_selection() {
        let tmp = TempDir::new().unwrap();
        let mut panel = make_panel_with_hits(&tmp, 100);
        panel.last_inner = Rect {
            x: 0,
            y: 0,
            width: 60,
            height: 30,
        };
        panel.selected = 0;
        panel.scroll = 0;
        panel.scroll_down(20);
        assert_eq!(panel.selected, 0, "wheel-scrolling must not move selection");
        assert_eq!(panel.scroll, 20);
    }

    // ---- files-to-include / files-to-exclude glob filtering ----

    #[test]
    fn path_filter_empty_allows_everything() {
        let f = PathFilter::new("", "");
        assert!(f.is_empty());
        let root = Path::new("/proj");
        assert!(f.allows(root, Path::new("/proj/src/main.rs")));
        assert!(f.allows(root, Path::new("/proj/a/b/c.py")));
    }

    #[test]
    fn path_filter_include_bare_glob_matches_at_any_depth() {
        // VS Code: `*.rs` with no slash means `**/*.rs`.
        let f = PathFilter::new("*.rs", "");
        assert!(!f.is_empty());
        let root = Path::new("/proj");
        assert!(f.allows(root, Path::new("/proj/main.rs")));
        assert!(f.allows(root, Path::new("/proj/src/deep/mod.rs")));
        assert!(!f.allows(root, Path::new("/proj/src/app.py")));
    }

    #[test]
    fn path_filter_include_with_multiple_comma_separated_globs() {
        let f = PathFilter::new("*.rs, *.toml", "");
        let root = Path::new("/proj");
        assert!(f.allows(root, Path::new("/proj/main.rs")));
        assert!(f.allows(root, Path::new("/proj/Cargo.toml")));
        assert!(!f.allows(root, Path::new("/proj/notes.md")));
    }

    #[test]
    fn path_filter_exclude_drops_matching_files() {
        let f = PathFilter::new("", "*.lock");
        let root = Path::new("/proj");
        assert!(f.allows(root, Path::new("/proj/main.rs")));
        assert!(!f.allows(root, Path::new("/proj/Cargo.lock")));
    }

    #[test]
    fn path_filter_exclude_beats_include_for_overlapping_paths() {
        // include *.rs, but exclude anything under target/. A target/*.rs file
        // must be dropped because exclude wins.
        let f = PathFilter::new("*.rs", "target/**");
        let root = Path::new("/proj");
        assert!(f.allows(root, Path::new("/proj/src/main.rs")));
        assert!(!f.allows(root, Path::new("/proj/target/debug/build.rs")));
    }

    #[test]
    fn path_filter_directory_glob_matches_relative_to_root() {
        let f = PathFilter::new("src/**", "");
        let root = Path::new("/proj");
        assert!(f.allows(root, Path::new("/proj/src/a/b.rs")));
        assert!(!f.allows(root, Path::new("/proj/tests/c.rs")));
    }

    #[test]
    fn search_streaming_filtered_honours_include_glob() {
        let tmp = TempDir::new().unwrap();
        write(&tmp.path().join("keep.rs"), "needle here\n");
        write(&tmp.path().join("drop.txt"), "needle here too\n");
        let cancel = Arc::new(AtomicBool::new(false));
        let filter = PathFilter::new("*.rs", "");
        let collected = Arc::new(std::sync::Mutex::new(Vec::new()));
        let c2 = collected.clone();
        search_workspace_streaming_filtered(
            tmp.path(),
            "needle",
            SearchOpts::default(),
            &cancel,
            &HashSet::new(),
            &filter,
            move |batch| c2.lock().unwrap().extend(batch),
        );
        let hits = Arc::try_unwrap(collected).unwrap().into_inner().unwrap();
        assert_eq!(
            hits.len(),
            1,
            "only the .rs file should match the include glob"
        );
        assert!(hits[0].path.ends_with("keep.rs"));
    }

    #[test]
    fn search_streaming_filtered_honours_exclude_glob() {
        let tmp = TempDir::new().unwrap();
        write(&tmp.path().join("a.rs"), "needle\n");
        write(&tmp.path().join("b.rs"), "needle\n");
        let cancel = Arc::new(AtomicBool::new(false));
        let filter = PathFilter::new("", "b.rs");
        let collected = Arc::new(std::sync::Mutex::new(Vec::new()));
        let c2 = collected.clone();
        search_workspace_streaming_filtered(
            tmp.path(),
            "needle",
            SearchOpts::default(),
            &cancel,
            &HashSet::new(),
            &filter,
            move |batch| c2.lock().unwrap().extend(batch),
        );
        let hits = Arc::try_unwrap(collected).unwrap().into_inner().unwrap();
        assert_eq!(hits.len(), 1);
        assert!(hits[0].path.ends_with("a.rs"), "b.rs must be excluded");
    }

    #[test]
    fn search_worker_loop_applies_set_filter_to_subsequent_query() {
        let tmp = TempDir::new().unwrap();
        write(&tmp.path().join("keep.rs"), "needle\n");
        write(&tmp.path().join("skip.md"), "needle\n");
        let (q_tx, q_rx) = std::sync::mpsc::channel::<SearchRequest>();
        let (e_tx, e_rx) = std::sync::mpsc::channel::<SearchEvent>();
        let root = tmp.path().to_path_buf();
        let join = std::thread::spawn(move || search_worker_loop(root, q_rx, e_tx));
        q_tx.send(SearchRequest::SetFilter("*.rs".into(), String::new()))
            .unwrap();
        q_tx.send(SearchRequest::Query("needle".into(), SearchOpts::default()))
            .unwrap();
        let (_, hits, done) = collect_until_done(&e_rx, std::time::Duration::from_secs(2));
        assert!(done);
        assert_eq!(hits.len(), 1, "filter must restrict the scan to *.rs");
        assert!(hits[0].path.ends_with("keep.rs"));
        drop(q_tx);
        join.join().unwrap();
    }

    // ---- replace ----

    #[test]
    fn replace_in_text_literal_replaces_all_occurrences() {
        let (out, n) =
            replace_in_text("foo bar foo baz foo", "foo", "X", SearchOpts::default()).unwrap();
        assert_eq!(n, 3);
        assert_eq!(out, "X bar X baz X");
    }

    #[test]
    fn replace_in_text_no_match_returns_zero_and_unchanged() {
        let (out, n) = replace_in_text("hello world", "zzz", "X", SearchOpts::default()).unwrap();
        assert_eq!(n, 0);
        assert_eq!(out, "hello world");
    }

    #[test]
    fn replace_in_text_literal_dollar_is_inserted_verbatim() {
        // A literal replacement containing `$` must not be parsed as a capture
        // reference; it should appear as-is.
        let (out, n) = replace_in_text("price=AMT", "AMT", "$5", SearchOpts::default()).unwrap();
        assert_eq!(n, 1);
        assert_eq!(out, "price=$5");
    }

    #[test]
    fn replace_in_text_case_insensitive_by_default() {
        let (out, n) = replace_in_text("Foo foo FOO", "foo", "x", SearchOpts::default()).unwrap();
        assert_eq!(n, 3);
        assert_eq!(out, "x x x");
    }

    #[test]
    fn replace_in_text_case_sensitive_only_exact_case() {
        let opts = SearchOpts {
            case_sensitive: true,
            ..Default::default()
        };
        let (out, n) = replace_in_text("Foo foo FOO", "foo", "x", opts).unwrap();
        assert_eq!(n, 1);
        assert_eq!(out, "Foo x FOO");
    }

    #[test]
    fn replace_in_text_whole_word_skips_substrings() {
        let opts = SearchOpts {
            whole_word: true,
            ..Default::default()
        };
        let (out, n) = replace_in_text("cat category cat", "cat", "dog", opts).unwrap();
        assert_eq!(n, 2);
        assert_eq!(out, "dog category dog");
    }

    #[test]
    fn replace_in_text_regex_with_capture_reference() {
        let opts = SearchOpts {
            use_regex: true,
            ..Default::default()
        };
        let (out, n) = replace_in_text("a1 b2 c3", r"([a-z])(\d)", "$2$1", opts).unwrap();
        assert_eq!(n, 3);
        assert_eq!(out, "1a 2b 3c");
    }

    #[test]
    fn replace_in_text_invalid_regex_returns_none() {
        let opts = SearchOpts {
            use_regex: true,
            ..Default::default()
        };
        assert!(replace_in_text("anything", "[unclosed", "x", opts).is_none());
    }

    #[test]
    fn replace_in_file_writes_only_when_a_match_is_replaced() {
        let tmp = TempDir::new().unwrap();
        let p = tmp.path().join("a.txt");
        write(&p, "old value old\n");
        let n = replace_in_file(&p, "old", "new", SearchOpts::default()).unwrap();
        assert_eq!(n, 2);
        assert_eq!(fs::read_to_string(&p).unwrap(), "new value new\n");
    }

    #[test]
    fn replace_all_on_disk_replaces_across_distinct_hit_files() {
        let tmp = TempDir::new().unwrap();
        write(&tmp.path().join("a.txt"), "foo foo\n");
        write(&tmp.path().join("b.txt"), "foo\n");
        let mut panel = SearchPanel::new(tmp.path().to_path_buf());
        panel.query = "foo".into();
        panel.replace = "bar".into();
        panel.run_query();
        // a.txt contributes 2 hits, b.txt 1 hit (3 hit rows, 2 files).
        let total = panel.replace_all_on_disk();
        assert_eq!(total, 3, "all three matches across both files replaced");
        assert_eq!(
            fs::read_to_string(tmp.path().join("a.txt")).unwrap(),
            "bar bar\n"
        );
        assert_eq!(
            fs::read_to_string(tmp.path().join("b.txt")).unwrap(),
            "bar\n"
        );
    }

    // ---- panel field focus + control hit-testing ----

    #[test]
    fn toggle_replace_expands_and_collapses_the_replace_row() {
        let tmp = TempDir::new().unwrap();
        let mut panel = SearchPanel::new(tmp.path().to_path_buf());
        assert!(!panel.replace_open);
        panel.toggle_replace();
        assert!(panel.replace_open);
        // Focus the replace field, then collapse: focus falls back to Query.
        panel.focus_field(SearchField::Replace);
        panel.toggle_replace();
        assert!(!panel.replace_open);
        assert_eq!(panel.field, SearchField::Query);
    }

    #[test]
    fn tab_cycles_only_through_visible_fields() {
        let tmp = TempDir::new().unwrap();
        let mut panel = SearchPanel::new(tmp.path().to_path_buf());
        // Only Query visible: Tab stays on Query.
        panel.focus_next_field();
        assert_eq!(panel.field, SearchField::Query);
        // Expand replace + details: Query -> Replace -> Include -> Exclude -> Query.
        panel.replace_open = true;
        panel.details_open = true;
        panel.focus_next_field();
        assert_eq!(panel.field, SearchField::Replace);
        panel.focus_next_field();
        assert_eq!(panel.field, SearchField::Include);
        panel.focus_next_field();
        assert_eq!(panel.field, SearchField::Exclude);
        panel.focus_next_field();
        assert_eq!(panel.field, SearchField::Query);
    }

    #[test]
    fn type_char_lands_in_the_focused_field() {
        let tmp = TempDir::new().unwrap();
        let mut panel = SearchPanel::new(tmp.path().to_path_buf());
        panel.replace_open = true;
        panel.type_char('a');
        assert_eq!(panel.query, "a");
        assert_eq!(panel.replace, "");
        panel.focus_field(SearchField::Replace);
        panel.type_char('b');
        assert_eq!(panel.query, "a");
        assert_eq!(panel.replace, "b");
    }

    #[test]
    fn focused_query_field_exposes_a_caret_and_paints_no_static_block() {
        use ratatui::buffer::Buffer;
        let tmp = TempDir::new().unwrap();
        let mut panel = SearchPanel::new(tmp.path().to_path_buf());
        panel.focused = true;
        panel.type_char('a');
        panel.type_char('b');
        let area = Rect {
            x: 0,
            y: 0,
            width: 40,
            height: 20,
        };
        let mut buf = Buffer::empty(area);
        Widget::render(&mut panel, area, &mut buf);
        // The caret sits on the query row so the app can drive the host's
        // blinking hardware caret there, matching the Source Control commit
        // box, instead of painting a static block that never blinks.
        let (cx, cy) = panel
            .cursor_screen_pos()
            .expect("a focused query field must expose a caret position");
        let r = panel.query_input_rect;
        assert_eq!(cy, r.y, "caret rides the query row");
        assert!(cx > r.x, "caret sits past the magnifier-led text start");
        let row: String = (r.x..r.right())
            .map(|x| buf[(x, r.y)].symbol().to_string())
            .collect();
        assert!(
            !row.contains('\u{2588}'),
            "the static full-block cursor must be gone now that the hardware caret blinks; row was {row:?}"
        );
    }

    #[test]
    fn an_unfocused_search_panel_exposes_no_caret() {
        use ratatui::buffer::Buffer;
        let tmp = TempDir::new().unwrap();
        let mut panel = SearchPanel::new(tmp.path().to_path_buf());
        panel.focused = false;
        panel.type_char('x');
        let area = Rect {
            x: 0,
            y: 0,
            width: 40,
            height: 20,
        };
        let mut buf = Buffer::empty(area);
        Widget::render(&mut panel, area, &mut buf);
        assert!(
            panel.cursor_screen_pos().is_none(),
            "an unfocused panel must not claim the hardware caret"
        );
    }

    #[test]
    fn backspace_removes_from_the_focused_field() {
        let tmp = TempDir::new().unwrap();
        let mut panel = SearchPanel::new(tmp.path().to_path_buf());
        panel.details_open = true;
        panel.focus_field(SearchField::Include);
        panel.insert_str("*.rs");
        assert_eq!(panel.include, "*.rs");
        assert!(panel.backspace());
        assert_eq!(panel.include, "*.r");
        assert_eq!(panel.query, "");
    }

    #[test]
    fn chevron_ellipsis_and_replace_all_hit_tests_after_render() {
        use ratatui::buffer::Buffer;
        let tmp = TempDir::new().unwrap();
        let mut panel = SearchPanel::new(tmp.path().to_path_buf());
        panel.focused = true;
        panel.replace_open = true;
        panel.details_open = true;
        let area = Rect {
            x: 0,
            y: 0,
            width: 60,
            height: 24,
        };
        let mut buf = Buffer::empty(area);
        ratatui::widgets::Widget::render(&mut panel, area, &mut buf);
        assert!(panel.chevron_at(panel.chevron_x, panel.chevron_y));
        assert!(panel.ellipsis_at(panel.ellipsis_x, panel.ellipsis_y));
        assert!(panel.replace_all_at(panel.replace_all_x, panel.replace_all_y));
        // Clicking the query input row focuses the Query field.
        let q = panel.query_input_rect;
        assert_eq!(panel.field_at(q.x, q.y), Some(SearchField::Query));
        let r = panel.replace_input_rect;
        assert_eq!(panel.field_at(r.x, r.y), Some(SearchField::Replace));
    }

    #[test]
    fn expanded_rows_push_results_start_down() {
        use ratatui::buffer::Buffer;
        let tmp = TempDir::new().unwrap();
        let mut panel = SearchPanel::new(tmp.path().to_path_buf());
        let area = Rect {
            x: 0,
            y: 0,
            width: 60,
            height: 30,
        };
        let mut buf = Buffer::empty(area);
        ratatui::widgets::Widget::render(&mut panel, area, &mut buf);
        let collapsed_offset = panel.results_start_offset;
        panel.replace_open = true;
        panel.details_open = true;
        let mut buf2 = Buffer::empty(area);
        ratatui::widgets::Widget::render(&mut panel, area, &mut buf2);
        assert!(
            panel.results_start_offset > collapsed_offset,
            "expanding replace + include/exclude must push results down (was {collapsed_offset}, now {})",
            panel.results_start_offset
        );
        // Single-row fields, each group separated by a hairline row: a divider
        // + replace field (2 rows), plus a divider + caption + field (3) each
        // for include and exclude = 8 rows.
        assert_eq!(panel.results_start_offset, collapsed_offset + 8);
    }

    #[test]
    fn collapsed_panel_results_offset_is_five() {
        use ratatui::buffer::Buffer;
        let tmp = TempDir::new().unwrap();
        let mut panel = SearchPanel::new(tmp.path().to_path_buf());
        let area = Rect {
            x: 0,
            y: 0,
            width: 60,
            height: 20,
        };
        let mut buf = Buffer::empty(area);
        ratatui::widgets::Widget::render(&mut panel, area, &mut buf);
        // header(0) + blank(1) + query(2) + separator(3) + caption(4) → results
        // begin at row 5. The single-row query field saves two rows versus the
        // old 3-row box (which started results at 7).
        assert_eq!(
            panel.results_start_offset, 5,
            "collapsed layout starts results at the 5-row offset"
        );
    }
    #[test]
    fn expand_replacement_honours_capture_references_in_regex_mode() {
        let opts = SearchOpts {
            use_regex: true,
            ..Default::default()
        };
        assert_eq!(
            expand_replacement("beta_42", r"beta_(\d+)", "id-$1", opts),
            "id-42",
            "regex mode expands $1 against the matched slice"
        );
        let literal = SearchOpts::default();
        assert_eq!(
            expand_replacement("beta", "beta", "x$1y", literal),
            "x$1y",
            "literal mode inserts the replacement verbatim"
        );
    }

    #[test]
    fn toggling_replace_open_focuses_the_replace_input() {
        let mut panel = SearchPanel::new(PathBuf::from("/tmp"));
        assert_eq!(panel.field, SearchField::Query);
        panel.toggle_replace();
        assert_eq!(
            panel.field,
            SearchField::Replace,
            "revealing the row must focus it — typing used to extend the query"
        );
        panel.toggle_replace();
        assert_eq!(panel.field, SearchField::Query, "collapsing returns focus");
    }
    #[test]
    fn zero_width_regex_matches_advance_and_mark_positions() {
        let opts = SearchOpts {
            use_regex: true,
            ..Default::default()
        };
        let segs = split_for_highlight("foo", r"\b", opts);
        // Two boundary positions, the text emitted exactly once between
        // them — the old code re-emitted it and rows doubled ("foofoo").
        assert_eq!(
            segs,
            vec![
                (String::new(), true),
                (String::from("foo"), false),
                (String::new(), true),
            ]
        );
    }
}