croft-software 0.2.232

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
//! The file tree (keys, type-to-jump, cut, copy and paste, rename and move with server
//! fixes, delete, drag targets), Explorer sections, workspace folders and
//! .code-workspace files, re-rooting, file watching and outside changes, zoxide jump.

use super::*;
use crate::widgets::picker_rows::{PickerMouse, answer_mouse};

/// The toggleable sub-views stacked inside the Explorer side panel, mirroring
/// VS Code's "Views and More Actions" (⋯) menu on the EXPLORER header. Ordered
/// top-to-bottom exactly as they stack in the panel.
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub enum ExplorerView {
    OpenEditors,
    Folders,
    Outline,
    Timeline,
    Dependencies,
    AgentLane,
}

impl ExplorerView {
    /// Every view, in stacking order — the order the ⋯ menu lists them.
    pub const ALL: [ExplorerView; 6] = [
        ExplorerView::OpenEditors,
        ExplorerView::Folders,
        ExplorerView::Outline,
        ExplorerView::Timeline,
        ExplorerView::Dependencies,
        ExplorerView::AgentLane,
    ];

    /// Menu / section-header label, matching VS Code's wording. Dependencies is
    /// a static fallback only — its real label is language-aware and computed
    /// from the detected ecosystems (see [`crate::widgets::dependencies`]).
    pub fn label(self) -> &'static str {
        match self {
            ExplorerView::OpenEditors => "Open Editors",
            ExplorerView::Folders => "Folders",
            ExplorerView::Outline => "Outline",
            ExplorerView::Timeline => "Timeline",
            ExplorerView::Dependencies => "Dependencies",
            ExplorerView::AgentLane => "Agent Lane",
        }
    }
}

impl ExplorerViewVisibility {
    pub(super) fn from_prefs(flags: crate::prefs::ExplorerViewsPrefs) -> Self {
        Self { flags }
    }

    pub(super) fn as_prefs(self) -> crate::prefs::ExplorerViewsPrefs {
        self.flags
    }

    pub fn is_visible(&self, view: ExplorerView) -> bool {
        match view {
            ExplorerView::OpenEditors => self.flags.open_editors,
            ExplorerView::Folders => self.flags.folders,
            ExplorerView::Outline => self.flags.outline,
            ExplorerView::Timeline => self.flags.timeline,
            ExplorerView::Dependencies => self.flags.dependencies,
            ExplorerView::AgentLane => self.flags.agent_lane,
        }
    }

    /// Flip one view's visibility, returning the new state.
    pub fn toggle(&mut self, view: ExplorerView) -> bool {
        let slot = match view {
            ExplorerView::OpenEditors => &mut self.flags.open_editors,
            ExplorerView::Folders => &mut self.flags.folders,
            ExplorerView::Outline => &mut self.flags.outline,
            ExplorerView::Timeline => &mut self.flags.timeline,
            ExplorerView::Dependencies => &mut self.flags.dependencies,
            ExplorerView::AgentLane => &mut self.flags.agent_lane,
        };
        *slot = !*slot;
        *slot
    }
}

#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub(super) enum ExplorerClipMode {
    Copy,
    Cut,
}

/// An Explorer rename or move, held while the language servers are asked
/// for their `willRenameFiles` edit (#610).
#[derive(Clone, Debug)]
pub(super) enum FileMove {
    Rename {
        parent: PathBuf,
        old: PathBuf,
        new_name: String,
    },
    Paste {
        dest_dir: PathBuf,
        paths: Vec<PathBuf>,
    },
}

/// The longest an Explorer move waits for the servers before it goes
/// ahead without their edit. Just past the manager's per-server timeout,
/// so a slow server's answer still lands when it can.
pub(super) const FILE_MOVE_DEADLINE: std::time::Duration = std::time::Duration::from_millis(2000);

/// Inter-keystroke gap that keeps the typeahead buffer alive. Anything
/// longer than this resets the prefix to a single character and cycles
/// past the current selection so repeating the same letter walks
/// through matches.
pub(super) const TREE_TYPEAHEAD_WINDOW: std::time::Duration = std::time::Duration::from_millis(750);

/// Build the right-click context-menu items for the explorer.
///
/// * Right-click on an entry (file or non-root folder) → entry-scoped
///   actions: Cut, Copy, Paste, Rename, Delete. Multi-select promotes
///   Delete to a count and keeps Rename on a single entry only.
/// * Right-click on empty tree space, or on the workspace root row →
///   workspace-scoped actions: New File, New Folder, Paste.
///
/// Test-side entry with no viewer lookup, so a viewer installed on the host
/// can never reach the built-in rows these tests assert; the app itself goes
/// through [`App::tree_context_menu_items`] with the dir and the disabled set
/// it carries.
#[cfg(test)]
pub(super) fn build_tree_context_menu_items(
    node: Option<&crate::widgets::file_tree::Node>,
    root: &Path,
    selection: &[PathBuf],
    target_dir: &Path,
    clipboard: Option<&ExplorerClipboard>,
    compare_anchor: Option<&Path>,
) -> Vec<(String, MenuAction)> {
    // No viewer lookup at all: these tests assert the built-in rows, so the
    // lookup must find nothing whatever the host has installed.
    build_tree_context_menu_items_with(
        node,
        root,
        selection,
        target_dir,
        clipboard,
        compare_anchor,
        |_| None,
    )
}

/// [`build_tree_context_menu_items`] with the viewer lookup injected (#465):
/// `viewer_for` answers "which installed viewer handles this file", as
/// `(label, id)`, so a test can offer one without an extensions dir.
pub(super) fn build_tree_context_menu_items_with(
    node: Option<&crate::widgets::file_tree::Node>,
    root: &Path,
    selection: &[PathBuf],
    target_dir: &Path,
    clipboard: Option<&ExplorerClipboard>,
    compare_anchor: Option<&Path>,
    viewer_for: impl Fn(&Path) -> Option<(String, String)>,
) -> Vec<(String, MenuAction)> {
    let entry_target = crate::widgets::file_tree::delete_target_for(node, root);
    let mut items: Vec<(String, MenuAction)> = Vec::new();
    if let Some(p) = entry_target {
        // Right-click on any node (file or folder, nested or not):
        // surface New File… / New Folder… at the top so the user can
        // create a new entry next to / inside whatever they clicked,
        // without having to scroll up to the workspace root first.
        // `target_dir` is already routed by `create_target_dir_for`:
        // a folder click resolves to the folder itself, a file click
        // to the file's parent — same convention VS Code uses.
        items.push((
            String::from("New File"),
            MenuAction::Create(CreateKind::File),
        ));
        items.push((
            String::from("New Folder"),
            MenuAction::Create(CreateKind::Folder),
        ));
        let paths_for_action: Vec<PathBuf> = if selection.iter().any(|sp| sp == &p) {
            selection.to_vec()
        } else {
            vec![p.clone()]
        };
        items.push((
            String::from("Cut"),
            MenuAction::Cut(paths_for_action.clone()),
        ));
        items.push((
            String::from("Copy"),
            MenuAction::Copy(paths_for_action.clone()),
        ));
        if clipboard.is_some() {
            items.push((
                String::from("Paste"),
                MenuAction::Paste(target_dir.to_path_buf()),
            ));
        }
        if paths_for_action.len() == 1 {
            items.push((String::from("Rename"), MenuAction::Rename(p.clone())));
        }
        // Compare actions only make sense for a single regular file.
        let single_file_target = paths_for_action
            .first()
            .filter(|_| paths_for_action.len() == 1)
            .filter(|pp| pp.is_file());
        if let Some(file) = single_file_target {
            // An installed viewer for this file kind (#465): `Open in csvlens`
            // on a .csv, once the csvlens extension has been added.
            if let Some((label, id)) = viewer_for(file) {
                items.push((label, MenuAction::OpenInViewer(id, file.clone())));
            }
            match compare_anchor {
                Some(anchor) if anchor != file.as_path() => {
                    items.push((
                        format!(
                            "Compare with Selected ({})",
                            anchor
                                .file_name()
                                .map(|n| n.to_string_lossy().into_owned())
                                .unwrap_or_else(|| anchor.display().to_string()),
                        ),
                        MenuAction::CompareWithSelected {
                            anchor: anchor.to_path_buf(),
                            other: file.clone(),
                        },
                    ));
                }
                _ => {}
            }
            items.push((
                String::from("Select for Compare"),
                MenuAction::SelectForCompare(file.clone()),
            ));
        }
        // Two regular files selected: offer a direct diff between them,
        // skipping the two-step Select-for-Compare anchor dance. Folders
        // can't be diffed, so any folder in the selection suppresses it.
        if paths_for_action.len() == 2 && paths_for_action.iter().all(|p| p.is_file()) {
            items.push((
                String::from("Compare Selected"),
                MenuAction::CompareWithSelected {
                    anchor: paths_for_action[0].clone(),
                    other: paths_for_action[1].clone(),
                },
            ));
        }
        // "Make root" appears only for non-root folders. Re-roots the
        // workspace at the clicked folder so the user can dive into a
        // nested git repo without relaunching croft.
        let single_folder_target: Option<&PathBuf> = paths_for_action
            .first()
            .filter(|_| paths_for_action.len() == 1)
            .filter(|_| node.map(|n| n.is_dir).unwrap_or(false))
            .filter(|pp| pp.as_path() != root);
        if let Some(folder) = single_folder_target {
            items.push((
                String::from("Make root"),
                MenuAction::MakeRoot(folder.clone()),
            ));
        }
        let label = if paths_for_action.len() > 1 {
            format!("Delete {} items", paths_for_action.len())
        } else {
            String::from("Delete")
        };
        items.push((
            label,
            MenuAction::Delete {
                paths: paths_for_action,
            },
        ));
    } else {
        items.push((
            String::from("New File"),
            MenuAction::Create(CreateKind::File),
        ));
        items.push((
            String::from("New Folder"),
            MenuAction::Create(CreateKind::Folder),
        ));
        // Root section rows (multi-root, #147): grow the folder set from
        // any root row; drop a SECONDARY root from its own row. The
        // primary is the launch identity — Make Root changes it, never a
        // removal.
        if let Some(n) = node.filter(|n| n.depth == 0) {
            items.push((
                String::from("Add Folder to Workspace"),
                MenuAction::AddWorkspaceFolder,
            ));
            if n.path != root {
                items.push((
                    String::from("Remove Folder from Workspace"),
                    MenuAction::RemoveWorkspaceFolder(n.path.clone()),
                ));
            }
        }
        if clipboard.is_some() {
            items.push((
                String::from("Paste"),
                MenuAction::Paste(target_dir.to_path_buf()),
            ));
        }
    }
    items
}

/// Insert "Reveal in Finder" into a freshly-built tree context menu, but only
/// when the action can actually do something: `enabled` is true (a local macOS
/// session) and a concrete entry was clicked (`clicked`). On a remote SSH
/// session croft runs on the headless host where there is no Finder, so the
/// item is omitted entirely rather than shown as a dead no-op, keeping the
/// local and remote menus honest. The entry lands directly beneath the New
/// File / New Folder block to mirror VS Code's top "reveal" placement.
pub(super) fn maybe_add_reveal_in_finder(
    items: &mut Vec<(String, MenuAction)>,
    clicked: Option<&Path>,
    enabled: bool,
) {
    let Some(path) = clicked.filter(|_| enabled) else {
        return;
    };
    // Insert directly after the leading New File / New Folder block: find the
    // first non-Create entry and slot in ahead of it. Deriving the position
    // this way (rather than a literal index) keeps the placement correct even
    // if the Create block grows or the empty-area menu shape changes.
    let pos = items
        .iter()
        .position(|(_, a)| !matches!(a, MenuAction::Create(_)))
        .unwrap_or(items.len());
    items.insert(
        pos,
        (
            String::from("Reveal in Finder"),
            MenuAction::RevealInFinder(path.to_path_buf()),
        ),
    );
}

/// Minimum width of the zoxide jump popup. The Explorer column it anchors
/// over is typically ~32 cells, too narrow to read frecency paths like
/// `~/Documents/platform/app/...`, so the box widens rightward to at least
/// this many cells (capped to the frame) while staying left-anchored on
/// the Explorer.
pub(super) const ZOXIDE_JUMP_MIN_WIDTH: u16 = 56;

/// The status-bar line for a jump popup with no zoxide to run: the first
/// line of what the popup body says.
pub(super) fn zoxide_jump_status(jump: &crate::widgets::zoxide_jump::ZoxideJump) -> String {
    let msg = crate::widgets::zoxide_jump::unavailable_message(jump);
    format!("Jump: {}", msg.lines().next().unwrap_or_default())
}

impl App {
    /// The Explorer's right-click menu (see [`build_tree_context_menu_items`]),
    /// with the viewer row read from the extensions under the app's config
    /// dir and its own disabled set, so the row it builds and the click that
    /// resolves the row read the same extensions, on or off as the
    /// Extensions panel shows them (#2076).
    pub(super) fn tree_context_menu_items(
        &self,
        node: Option<&crate::widgets::file_tree::Node>,
        selection: &[PathBuf],
        target_dir: &Path,
    ) -> Vec<(String, MenuAction)> {
        build_tree_context_menu_items_with(
            node,
            &self.tree.root,
            selection,
            target_dir,
            self.tree_clipboard.as_ref(),
            self.compare_anchor.as_deref(),
            |file| {
                crate::mcp::registry::viewer_for_path_in_dir(
                    &self.config_dir,
                    &self.disabled_extensions,
                    file,
                )
                .map(|v| (v.label.clone(), v.key()))
            },
        )
    }

    /// What the poll stats for open tabs: each tab's file, and for a SQLite
    /// tab its write-ahead log too, where a WAL-mode app's commits land
    /// while the database file stays untouched (#1643). The log may not
    /// exist yet; its appearing is a change like any other.
    pub(super) fn polled_open_file_paths(&self) -> Vec<PathBuf> {
        let mut paths = self.open_tab_paths();
        let wal = self
            .editor_layout
            .all_groups(&self.editor)
            .flat_map(|g| g.editors.iter())
            .filter(|e| {
                e.sheet
                    .as_ref()
                    .is_some_and(|s| s.kind == crate::sheet::SheetKind::Sqlite)
            })
            .filter_map(|e| {
                e.path
                    .as_deref()
                    .and_then(crate::widgets::editor::sqlite_wal_path)
            });
        paths.extend(wal);
        paths
    }

    /// Every file backing an open tab, across all editor groups. The poll
    /// stats these, so a background tab in a dir that carries no watch still
    /// sees an external write.
    pub(super) fn open_tab_paths(&self) -> Vec<PathBuf> {
        self.editor_layout
            .all_groups(&self.editor)
            .flat_map(|g| g.editors.iter())
            .filter_map(|e| e.path.clone())
            .collect()
    }

    /// Sweep EVERY open tab for external on-disk changes, not just the
    /// focused one — in every editor GROUP, not just the focused one either.
    /// The poll stats each open path once and re-baselines its stamp, so a
    /// change reported to a sweep that could not reach the tab is burned: no
    /// later tick reports it again and that pane keeps stale text forever.
    /// Clean tabs reload silently; dirty tabs are flagged as conflicts (the
    /// buffer and the disk are both preserved); tabs whose reload FAILED
    /// keep their last good view and say so in the status line (#37).
    /// Returns true if any tab reloaded, failed, or entered conflict, so
    /// the caller redraws.
    pub(super) fn reload_open_file_after_external_change(&mut self) -> bool {
        let settled = self.settle_sudo_saves();
        let guest = self.is_collab_guest();
        let root = self.tree.root.clone();
        let skip = |p: &Path| guest && collab_file_key(&root, p).is_some();
        let mut report = self.editor.reload_externally_changed_tabs(&skip);
        for group in self.editor_layout.inactive_groups_mut() {
            let extra = group.reload_externally_changed_tabs(&skip);
            report.reloaded.extend(extra.reloaded);
            report.conflicts.extend(extra.conflicts);
            report.failed.extend(extra.failed);
            report.deleted.extend(extra.deleted);
        }
        if report.is_empty() {
            return settled;
        }
        self.announce_external_changes(report);
        // Keep the watcher's active-file baseline aligned with the reload so
        // a just-applied change doesn't re-trigger on the next poll.
        self.sync_open_file_poll_mtime();
        true
    }

    /// Say what a sweep found: a conflict asks, a deletion or a failed
    /// reload is said in the status line, a reload says what was reloaded.
    /// Also for a PDF rebuild that lands on a job after its sweep (#2827),
    /// which is why the watcher's baselines are left to the sweep: moved
    /// here, a landing would take in writes to other files that no sweep
    /// had looked at, and they would never reload.
    pub(super) fn announce_external_changes(
        &mut self,
        report: crate::widgets::editor::ExternalReloadReport,
    ) {
        // The active file changed on disk under a clean buffer: its coverage
        // marks describe the old text now, so they dim like an edit's (#263).
        if let Some(active) = self.editor.path.as_ref()
            && report.reloaded.contains(active)
            && let Some(lens) = self.editor.coverage.as_mut()
        {
            lens.stale = true;
        }
        self.refresh_git_status_debounced();
        match (
            report.reloaded.len(),
            report.conflicts.len(),
            report.failed.len(),
        ) {
            // A dirty-buffer collision outranks everything: it needs a
            // decision, so it gets the modal.
            (_, c, _) if c > 0 => {
                self.prompt_disk_conflict(report.conflicts.clone());
            }
            // Gone is not a failed read (#900): the tab keeps its text, its
            // label says `(deleted)`, and a save puts the file back.
            // A failed reload in the same sweep is said too: it is reported
            // once, so dropping it here would hide it for good.
            _ if !report.deleted.is_empty() => {
                let deleted = match report.deleted.as_slice() {
                    [one] => format!(
                        "{} was deleted on disk; save to recreate it",
                        self.status_path(one)
                    ),
                    many => format!(
                        "{} open files were deleted on disk; save to recreate them",
                        many.len()
                    ),
                };
                let failed = match report.failed.as_slice() {
                    [] => String::new(),
                    [one] => format!(
                        "; reload failed for {}, keeping its last good view",
                        self.status_path(one)
                    ),
                    many => format!(
                        "; reload failed for {} files, keeping their last good view",
                        many.len()
                    ),
                };
                self.status = format!("{deleted}{failed}");
            }
            // A failed reload used to be silently discarded (#37): the tab
            // keeps its last good view, and the status must say so instead
            // of letting the user read stale content as current.
            (0, 0, 1) => {
                let p = report.failed[0].display();
                self.status = format!("Reload failed for {p}; keeping the last good view");
            }
            (r, 0, f) if f > 0 => {
                self.status = if r == 0 {
                    format!("Reload failed for {f} files changed on disk")
                } else {
                    format!("Reloaded {r} files, reload failed for {f} (kept their last good view)")
                };
            }
            (1, 0, 0) => {
                let p = report.reloaded[0].display();
                self.status = format!("Reloaded {p} (external change)");
            }
            (n, ..) => {
                self.status = format!("Reloaded {n} files changed on disk");
            }
        }
    }

    /// A dirty buffer collided with an external write. Surface it unmissably
    /// via the confirm popup (Enter reloads and discards local edits, Esc
    /// keeps them). Falls back to the status line only when another prompt
    /// already owns the modal slot, so we never stomp an in-progress input.
    pub(super) fn prompt_disk_conflict(&mut self, paths: Vec<PathBuf>) {
        use crate::widgets::input_prompt::{InputPrompt, InputPurpose};
        // It opens mid-typing and takes the keys, so nothing a stray Enter
        // or a typed line can confirm discards the edits: only the word
        // `reload` does, and the box says so (#910).
        let title = match paths.as_slice() {
            [one] => format!(
                "{} changed on disk and you have unsaved edits.",
                self.status_path(one)
            ),
            many => format!(
                "{} open files changed on disk with unsaved edits.",
                many.len()
            ),
        };
        if self.input_prompt.is_some() {
            self.status = title;
            return;
        }
        self.open_input_prompt(
            InputPrompt::new(
                InputPurpose::ReloadConflict { paths },
                title,
                "type reload to discard your unsaved edits",
            )
            .with_hint("reload + Enter discards your edits · anything else keeps them"),
        );
    }

    pub(super) fn poll_filesystem_changes(&mut self) -> bool {
        // Runs every frame; the poll itself fires at most every 50ms and backs
        // off from there, so don't build the path list until it would be used.
        if !self.fs_watch.poll_due() {
            return false;
        }
        let open_paths = self.polled_open_file_paths();
        let poll = self.fs_watch.poll(&mut self.tree, &open_paths);
        // Any directory change can also be a write to a file open in some
        // tab (background or active), so sweep all tabs whenever the poll
        // saw activity, not only when the active file's own stat moved.
        if poll.open_file_changed || poll.dirs_changed {
            self.reload_open_file_after_external_change();
        }
        if poll.dirs_changed {
            self.refresh_git_status_debounced();
            if poll.finder_relevant {
                self.kick_file_finder_index_rebuild();
            }
        }
        // Git used outside croft writes only to git's own folder, which the
        // watcher skips: a few stamps there, on the same cadence (#2577).
        // The status it asks for lands through `drain_git_responses`.
        self.refresh_git_status_if_git_moved();
        // Stamp-gated, so an idle poll costs a stat per open diff view; on
        // hosts without a native watcher this is how a diff follows the
        // file (#471).
        let diffs_changed =
            self.refresh_open_diff_views(false, &[], &std::collections::BTreeSet::new());
        poll.open_file_changed || poll.dirs_changed || diffs_changed
    }

    /// Drain any pending filesystem events from the watcher and refresh the
    /// tree directories whose contents may have changed. Also reloads the
    /// editor's open file when an external write (vim, git pull, etc.)
    /// changes it on disk.
    /// Drain pending filesystem events. Returns `true` iff anything was
    /// processed (so the main loop knows it owes a redraw).
    /// Install background-initialised resources (fs watcher, git status) if
    /// their threads have finished. Returns true if any were installed this
    /// tick (so the caller redraws). Cheap no-op once both are installed.
    pub fn try_install_pending_init(&mut self) -> bool {
        let mut changed = self.fs_watch.try_install_watcher();
        for w in &mut self.fs_watch_extra {
            if w.try_install_watcher() {
                changed = true;
            }
        }
        if self.drain_git_responses() {
            changed = true;
        }
        // Land the background-built Cmd+P file index the moment the
        // walker thread finishes, so the very next Cmd+P opens
        // against a hot index instead of triggering the synchronous
        // fallback walk.
        self.poll_file_finder_index();
        changed
    }

    /// Keep the Explorer's non-OUTLINE stacked sub-views current: re-project the
    /// OPEN EDITORS list from the tabs, kick off the TIMELINE / DEPENDENCIES
    /// background fetches when their input (active file / workspace root) changed,
    /// and drain any worker replies. Returns whether anything changed, so the
    /// main loop only repaints when it must. All the heavy work (`git log`,
    /// `cargo metadata`) runs on spawned threads — never on this tick.
    pub(super) fn sync_explorer_panels(&mut self) -> bool {
        let mut changed = false;

        // --- OPEN EDITORS: a pure projection of the focused group's tabs,
        // rebuilt only when what the rows are made of changed (#2062). ---
        let active = self.editor.active_index();
        if !self.open_editors.built_from(active, &self.editor.editors) {
            // Same disambiguated titles as the tab strip (#167), so the two
            // views never name one file two ways.
            let labels = self.editor.tab_display_labels();
            let items: Vec<OpenEditorItem> = self
                .editor
                .iter_tabs()
                .enumerate()
                .filter_map(|(i, ed)| {
                    ed.path.as_ref().map(|p| OpenEditorItem {
                        path: p.clone(),
                        name: labels
                            .get(i)
                            .cloned()
                            .unwrap_or_else(|| p.display().to_string()),
                        dirty: ed.dirty,
                        active: i == active,
                    })
                })
                .collect();
            changed |= self
                .open_editors
                .set_items_from(items, active, &self.editor.editors);
        }

        // --- TIMELINE: git history of the active file, fetched off-thread. ---
        if self.explorer_views.is_visible(ExplorerView::Timeline)
            && self.timeline_fetched.as_deref() != self.editor.path.as_deref()
        {
            match self.editor.path.clone() {
                Some(path) => {
                    if self.timeline_fetched.as_ref() != Some(&path) {
                        self.timeline_fetched = Some(path.clone());
                        self.timeline.begin_loading(path.clone());
                        changed = true;
                        let timeline_root = self
                            .roots
                            .owning_root(&path)
                            .unwrap_or_else(|| self.roots.primary())
                            .to_path_buf();
                        let rel = path
                            .strip_prefix(&timeline_root)
                            .ok()
                            .map(|r| r.to_string_lossy().into_owned());
                        let root = timeline_root;
                        let history_root = self.history_root.clone();
                        let abs = path.clone();
                        let now = now_millis();
                        let job = crate::jobs::Job::spawn(
                            "timeline",
                            crate::jobs::Start::Now,
                            move || {
                                // Merge git commits with local snapshots so the
                                // TIMELINE shows both sources, like VS Code. Git
                                // history only exists inside the workspace, but
                                // snapshots are path-keyed and repo-free, so a
                                // file outside the root still lists its versions.
                                let commits = rel
                                    .map(|rel| crate::git::file_history(&root, &rel, 50))
                                    .unwrap_or_default();
                                let snaps = crate::history::entries_in(&history_root, &abs);
                                let entries = crate::history::merged_timeline(commits, &snaps, now);
                                (path, entries)
                            },
                        );
                        // A lookup still running, for this file or the one
                        // just left, is dropped: its list is older (#2575).
                        match job {
                            Ok(job) => self.timeline_fetch = Some(job),
                            Err(e) => {
                                self.timeline_fetch = None;
                                self.timeline.set_failed();
                                self.status = format!("Could not load the file's history: {e}");
                            }
                        }
                    }
                }
                None => {
                    if self.timeline_fetched.is_some() {
                        self.timeline_fetched = None;
                        self.timeline_fetch = None;
                        self.timeline.clear();
                        changed = true;
                    }
                }
            }
        }
        // A background test-source lookup landed: jump or say it missed. A
        // reply from before an Explorer re-root names the old root; drop it.
        while let Ok((root, name, hit)) = self.test_jump_rx.try_recv() {
            if root != self.workspace_root() {
                continue;
            }
            match hit {
                Some((path, line)) => self.go_to_definition(path, line, 0),
                None => self.status = format!("No source found for test {name}"),
            }
            changed = true;
        }
        // A history snapshot finished writing: refetch the TIMELINE so the
        // new version shows (resetting the marker before the write landed
        // would list the store without it), and have every tab of that file
        // look at its history again, which now holds the snapshot and who
        // typed each line of it (#2925).
        while let Ok(path) = self.history_done_rx.try_recv() {
            if self.editor.path.as_deref() == Some(path.as_path()) {
                self.timeline_fetched = None;
                changed = true;
            }
            for ed in self
                .editor_layout
                .all_groups_mut(&mut self.editor)
                .flat_map(|g| g.editors.iter_mut())
                .filter(|ed| ed.path.as_deref() == Some(path.as_path()))
            {
                ed.provenance_synced_seq = None;
            }
        }
        // The workspace is too large for the sweep to start on its own.
        // Held rather than written: the result below sets `status`
        // unconditionally and nothing paints between the two, so writing it
        // here DESTROYED the hint whenever the checker finished inside one
        // tick - and it is a one-shot, so it never came back. The
        // fast-failure path (over-cap root, no compiler installed) is
        // exactly where that happened.
        let hinted_now = self.project_hint_rx.try_recv().is_ok();
        if hinted_now {
            // Here, not at the spawn: only a hint that was actually WARRANTED
            // spends the one-shot.
            self.project_check_hinted = true;
        }
        // A whole-project check finished: its rows and its verdict land
        // together, so the status cannot describe a different run's result.
        if self.drain_project_check() {
            changed = true;
        }
        if hinted_now {
            // Appended, so the outcome the user asked for still leads.
            self.status.push_str(
                " - too large for the whole-project check to run on its own; \
                 set it to always in Problems: Whole-Project Check Auto-Run",
            );
            changed = true;
        }
        // A CodeQL database's folder size landed; drop one for a database
        // removed while it was being summed.
        while let Ok((path, size)) = self.codeql_jobs.size_rx.try_recv() {
            self.codeql_jobs.size_pending.remove(&path);
            if let Some(size) = size
                && self.codeql.databases.iter().any(|db| db.path == path)
            {
                self.codeql.db_sizes.insert(path, size);
                changed = true;
            }
        }
        // Collect the TIMELINE lookup; ignore a list for a since-closed file.
        let settled = match self.timeline_fetch.as_ref().map(crate::jobs::Job::poll) {
            None | Some(crate::jobs::Poll::Pending) => None,
            Some(crate::jobs::Poll::Done(reply)) => Some(Some(reply)),
            // Its thread crashed, and the crash notice said so. The section
            // says the history could not be loaded instead of "Loading" until
            // another file is opened (#2574).
            Some(crate::jobs::Poll::Died) => Some(None),
        };
        if let Some(reply) = settled {
            self.timeline_fetch = None;
            match reply {
                Some((path, entries)) => {
                    if self.editor.path.as_deref() == Some(path.as_path()) {
                        self.timeline.set_history(path, entries);
                        changed = true;
                    }
                }
                None => {
                    self.timeline.set_failed();
                    changed = true;
                }
            }
        }
        // Collect the COMMITS graph fetch; ignore a reply for a since-left root.
        let settled = match self.graph_fetch.as_ref().map(crate::jobs::Job::poll) {
            None | Some(crate::jobs::Poll::Pending) => None,
            Some(crate::jobs::Poll::Done(reply)) => Some(Some(reply)),
            // Its thread crashed, and the crash notice said so. It is no
            // longer running, so the next refresh fetches again instead of
            // waiting on it until a restart (#2564).
            Some(crate::jobs::Poll::Died) => Some(None),
        };
        if let Some(reply) = settled {
            self.graph_fetch = None;
            if let Some((root, revspec, rows)) = reply
                && root == self.active_scm_root
                && revspec == self.graph_revspec
            {
                self.commit_graph.set_rows(rows);
                // COMMITS took the keys before its history landed (#871):
                // the newest commit is picked now, as Tab would have.
                if self.source_control.keys == crate::widgets::source_control::KeyStop::Commits
                    && self.commit_graph.selected_commit().is_none()
                {
                    self.commit_graph.select(0);
                }
                changed = true;
            }
            // A trigger arrived while this fetch was in flight: what it
            // asked for (new root, newer HEAD) is not what just landed.
            if self.graph_refetch_queued {
                self.graph_refetch_queued = false;
                self.refresh_commit_graph();
            }
        }

        // --- DEPENDENCIES: detect the workspace's package ecosystems from its
        // manifest files (cheap, once per root), set the language-aware header,
        // then resolve the packages off-thread. The section is gated on
        // detection: a root with no recognised manifest detects nothing, so it
        // is hidden and dropped from the ⋯ menu (the rule every reference editor
        // follows). The fetch only fires when the section is actually shown. ---
        // Compared in place: the root is copied only when it moved (#2062).
        if self.deps_fetched.as_ref() != Some(&self.tree.root) {
            let root = self.tree.root.clone();
            self.deps_fetched = Some(root.clone());
            self.dep_ecosystems = crate::dependencies::detect_ecosystems(&root);
            self.dependencies
                .set_header(crate::widgets::dependencies::header_label(
                    &self.dep_ecosystems,
                ));
            changed = true;
            // Whatever ran for the folder just left is dropped here, so its
            // list cannot land in this one's (#2573).
            self.deps_fetch = None;
            if !self.dep_ecosystems.is_empty()
                && self.explorer_views.is_visible(ExplorerView::Dependencies)
            {
                let job =
                    crate::jobs::Job::spawn("dependencies", crate::jobs::Start::Now, move || {
                        crate::dependencies::fetch_dependencies(&root)
                    });
                match job {
                    Ok(job) => {
                        self.dependencies.lookup_started();
                        self.deps_fetch = Some(job);
                    }
                    Err(e) => {
                        self.dependencies.set_failed();
                        self.status = format!("Could not list the dependencies: {e}");
                    }
                }
            }
        }
        let settled = match self.deps_fetch.as_ref().map(crate::jobs::Job::poll) {
            None | Some(crate::jobs::Poll::Pending) => None,
            Some(crate::jobs::Poll::Done(deps)) => Some(Some(deps)),
            // Its thread crashed, and the crash notice said so. The section
            // says the list could not be made instead of "Loading" until the
            // folder changes (#2572).
            Some(crate::jobs::Poll::Died) => Some(None),
        };
        if let Some(deps) = settled {
            self.deps_fetch = None;
            match deps {
                Some(deps) => self.dependencies.set_deps(deps),
                None => self.dependencies.set_failed(),
            }
            changed = true;
        }

        changed
    }

    /// Activate the editor tab for `path` (clicked in OPEN EDITORS). The path is
    /// already open by definition, so this selects its tab; should it have been
    /// closed between the projection and the click, it re-opens it.
    pub(super) fn activate_open_editor(&mut self, path: PathBuf) {
        if let Some(idx) = self.editor.find_tab_with_path(&path) {
            self.editor.select(idx);
        } else if self.editor.open_pinned(&path).is_err() {
            self.status = format!("Could not open {}", path.display());
            return;
        }
        self.focus_pane(Pane::Editor);
        self.sync_open_file_poll_mtime();
    }

    /// Open the diff a TIMELINE commit introduced for `path` (`git show <hash>
    /// -- <path>`) in a read-only side-by-side editor tab.
    pub(super) fn open_timeline_diff(&mut self, path: PathBuf, hash: String) {
        // Disarm first: every early exit below must leave "Restore Snapshot"
        // pointing at nothing, never at whatever snapshot was viewed before.
        self.history_restore = None;
        let rel = match path.strip_prefix(&self.tree.root) {
            Ok(r) => r.to_string_lossy().into_owned(),
            Err(_) => path.display().to_string(),
        };
        // A local-history row: diff the snapshot against the working file and
        // remember it so "Restore Snapshot" can write it back.
        if let Some(millis) = crate::history::snapshot_millis(&hash) {
            let Some(snap_file) =
                crate::history::snapshot_file_in(&self.history_root, &path, millis)
            else {
                self.status = String::from("Snapshot no longer available");
                return;
            };
            let Ok(content) = std::fs::read(&snap_file) else {
                self.status = String::from("Could not read snapshot");
                return;
            };
            // Snapshots hold the file's raw bytes; decode like `Editor::open`
            // so a UTF-16 file's history diffs as text, not mojibake.
            let enc = encoding_rs::Encoding::for_bom(&content)
                .map(|(e, _)| e)
                .unwrap_or(encoding_rs::UTF_8);
            let text = enc.decode(&content).0.into_owned();
            let label = std::path::PathBuf::from(format!("{rel} (local snapshot)"));
            let unsaved = self.unsaved_text_in_any_group(&path);
            if let Err(e) = self
                .editor
                .open_head_diff_with_text_resolved(label, &text, &path, false, unsaved)
            {
                self.status = format!("Could not open snapshot diff: {e}");
                return;
            }
            self.tag_open_diff(crate::widgets::diff::DiffSource::FixedLeft { left_text: text });
            self.history_restore = Some((path, millis, content));
            self.focus_pane(Pane::Editor);
            self.status = format!("Showing {rel} at a local snapshot");
            return;
        }
        // A git commit row: no snapshot to restore.
        let timeline_root = self
            .editor
            .path
            .as_deref()
            .and_then(|p| self.roots.owning_root(p))
            .unwrap_or_else(|| self.roots.primary())
            .to_path_buf();
        // Git: Open Commit for Line passes the full hash (#1556), named here
        // by its first eight digits as the inline blame names it; a TIMELINE
        // row's is git's own short form already.
        let shown = if hash.len() >= 40 {
            hash[..8].to_string()
        } else {
            hash.clone()
        };
        self.status = format!("Loading {rel} at {shown}\u{2026}");
        self.spawn_git_view(move || {
            let result = crate::git::show_commit_file_diff(&timeline_root, &hash, &rel);
            Box::new(move |app: &mut App| match result {
                Ok(raw) => {
                    let label = std::path::PathBuf::from(format!("{rel} @ {shown}"));
                    if let Err(e) = app.editor.open_git_diff_side_by_side(&label, &raw) {
                        app.status = format!("Could not open diff: {e}");
                        return;
                    }
                    app.focus_pane(Pane::Editor);
                    app.status = format!("Showing {rel} at {shown}");
                }
                Err(e) => app.status = format!("git show failed: {e}"),
            })
        });
    }

    /// Restore the local snapshot currently shown in a TIMELINE diff: write its
    /// contents back to the file and reload the buffer. Reachable from the
    /// Command Palette once a snapshot diff is open (which records the target).
    pub(super) fn restore_history_snapshot(&mut self) {
        let Some((file, millis, content)) = self.history_restore.clone() else {
            self.status = String::from("Open a local snapshot from the TIMELINE first");
            return;
        };
        // What is on disk now is kept first: it may never have been
        // snapshotted (an agent's write, a checkout), and the restore
        // overwrites it. Refused if it cannot be kept.
        if let Ok(current) = std::fs::read(&file)
            && let Err(e) =
                crate::history::record_kept_in(&self.history_root, &file, &current, now_millis())
        {
            self.status = format!("Restore cancelled: could not keep the current version ({e})");
            return;
        }
        if let Err(e) = std::fs::write(&file, &content) {
            self.status = format!("Restore failed: {e}");
            return;
        }
        // The snapshot's seats describe exactly the bytes just written (#349),
        // so they come along: onto the buffer, and onto the snapshot this
        // restore records below. No open tab's map can serve instead: a
        // reverted tab has just had its map cleared, and an unreverted one
        // still describes the text the restore replaced. A snapshot with no
        // sidecar restores every line unknown, which is what it knows.
        let seats =
            crate::history::seats_for(&self.history_root, &file, millis).unwrap_or_default();
        // If that file is the active tab (the snapshot diff opened it), reload
        // so the editor shows the restored contents and the disk stamp resyncs.
        // `holds_exactly` re-checks rather than assuming the reload landed the
        // restored bytes: another process can rewrite the file between the
        // write above and the read inside the revert.
        if self.editor.path.as_deref() == Some(file.as_path())
            && self.editor.revert_to_disk().is_ok()
            && self.editor.holds_exactly(&content)
        {
            let mut onto_buffer = seats.clone();
            onto_buffer.truncate(self.editor.lines.len());
            self.editor.provenance = onto_buffer;
        }
        // The restore is itself a new version worth keeping, kept like the
        // version it replaced.
        self.record_history_snapshot_of_kept(&file, seats);
        self.status = format!("Restored {}", self.status_path(&file));
    }

    /// Open the Explorer's "Views and More Actions" (⋯) menu: one row per
    /// stacked sub-view, each prefixed with a check when currently shown.
    /// Selecting a row toggles that view (see [`Self::toggle_explorer_view`]).
    /// The ⋯-menu / status label for a sub-view. DEPENDENCIES is language-aware
    /// ("Rust Dependencies", "Python Dependencies", or plain "Dependencies" for
    /// a polyglot root), computed from the detected ecosystems; every other view
    /// uses its static name.
    pub(super) fn explorer_view_label(&self, view: ExplorerView) -> String {
        match view {
            ExplorerView::Dependencies => {
                crate::widgets::dependencies::menu_label(&self.dep_ecosystems)
            }
            other => other.label().to_string(),
        }
    }

    /// Whether a sub-view may appear in the ⋯ menu and stack in the panel.
    /// DEPENDENCIES is additionally gated on a manifest being detected at the
    /// root, so a plain folder shows no dependency view at all; every other view
    /// is always available.
    pub(super) fn explorer_view_available(&self, view: ExplorerView) -> bool {
        match view {
            ExplorerView::Dependencies => !self.dep_ecosystems.is_empty(),
            _ => true,
        }
    }

    /// Anchored just below the ⋯ button on the EXPLORER title line; the
    /// menu-rect clamp pulls it left so it never spills off the right edge.
    pub(super) fn open_explorer_views_menu(&mut self) {
        let btn = self.tree.header_views_btn;
        let origin = if btn.width > 0 {
            (btn.x, btn.y + 1)
        } else {
            (self.tree.last_area.x + 1, self.tree.last_area.y + 1)
        };
        let items: Vec<(String, MenuAction)> = ExplorerView::ALL
            .iter()
            .filter(|&&view| self.explorer_view_available(view))
            .map(|&view| {
                let mark = if self.explorer_views.is_visible(view) {
                    "\u{2713} "
                } else {
                    "  "
                };
                (
                    format!("{mark}{}", self.explorer_view_label(view)),
                    MenuAction::ToggleExplorerView(view),
                )
            })
            .collect();
        self.context_menu = Some(ContextMenu::flat(
            origin,
            items,
            self.workspace_root().to_path_buf(),
        ));
    }

    /// Flip one Explorer sub-view's visibility, persist the new set, and (for a
    /// data-backed view turned on) clear its fetch latch so the next frame
    /// re-populates it. The layout reads `explorer_views` directly, so no other
    /// wiring is needed for the section to appear or vanish.
    pub(super) fn toggle_explorer_view(&mut self, view: ExplorerView) {
        let now_visible = self.explorer_views.toggle(view);
        if now_visible {
            match view {
                ExplorerView::Timeline => self.timeline_fetched = None,
                ExplorerView::Dependencies => self.deps_fetched = None,
                _ => {}
            }
        }
        self.status = format!(
            "{} {}",
            if now_visible { "Showing" } else { "Hiding" },
            self.explorer_view_label(view)
        );
        if !cfg!(test) {
            self.report_unsaved_setting(crate::prefs::save_explorer_views(
                self.explorer_views.as_prefs(),
            ));
        }
    }

    /// The AGENT LANE rows (#345): each agent that changed files, then those
    /// files as the ledger orders them (unreviewed first), labelled relative
    /// to their workspace root.
    pub(super) fn agent_lane_panel_rows(&mut self) -> Vec<crate::widgets::agent_lane::LaneRow> {
        use crate::widgets::agent_lane::LaneRow;
        let mut rows = Vec::new();
        let agents: Vec<String> = self
            .agent_ledger
            .agents()
            .into_iter()
            .map(str::to_string)
            .collect();
        for agent in agents {
            rows.push(LaneRow::Agent {
                name: agent.clone(),
                unreviewed: self.agent_ledger.unreviewed_count(&agent),
            });
            let files: Vec<_> = self
                .agent_ledger
                .lane(&agent)
                .into_iter()
                .map(|f| {
                    (
                        f.path.clone(),
                        f.reviewed_millis,
                        f.current_hash,
                        f.unreviewed(),
                    )
                })
                .collect();
            for (path, reviewed_millis, current_hash, unreviewed) in files {
                let changes = reviewed_millis
                    .and_then(|millis| self.lane_changes(&path, millis, current_hash));
                let label = self
                    .roots
                    .owning_root(&path)
                    .and_then(|root| path.strip_prefix(root).ok())
                    .unwrap_or(&path)
                    .display()
                    .to_string();
                rows.push(LaneRow::File {
                    agent: agent.clone(),
                    path,
                    label,
                    unreviewed,
                    changes,
                });
            }
        }
        rows
    }

    /// Lines added and removed in `path` since the snapshot it was reviewed
    /// against (#345), for its lane row. Rows render every frame, so the
    /// count is kept until the snapshot, the agent's last write or the file
    /// on disk changes. `None` when the snapshot or the file can't be read.
    pub(super) fn lane_changes(
        &mut self,
        path: &Path,
        millis: u64,
        current_hash: u64,
    ) -> Option<(usize, usize)> {
        let mtime = std::fs::metadata(path).and_then(|m| m.modified()).ok();
        let key = (millis, current_hash, mtime);
        if let Some((k, counts)) = self.lane_change_counts.get(path)
            && *k == key
        {
            return *counts;
        }
        let counts = (|| {
            let snap = crate::history::snapshot_file_in(&self.history_root, path, millis)?;
            let decode = |bytes: Vec<u8>| {
                let enc = encoding_rs::Encoding::for_bom(&bytes)
                    .map(|(e, _)| e)
                    .unwrap_or(encoding_rs::UTF_8);
                enc.decode(&bytes).0.into_owned()
            };
            let before = decode(std::fs::read(snap).ok()?);
            let after = decode(std::fs::read(path).ok()?);
            let diff = similar::TextDiff::from_lines(&before, &after);
            let (mut added, mut removed) = (0, 0);
            for change in diff.iter_all_changes() {
                match change.tag() {
                    similar::ChangeTag::Insert => added += 1,
                    similar::ChangeTag::Delete => removed += 1,
                    similar::ChangeTag::Equal => {}
                }
            }
            Some((added, removed))
        })();
        self.lane_change_counts
            .insert(path.to_path_buf(), (key, counts));
        counts
    }

    /// Cmd+K V (#345): make the AGENT LANE section visible and open, on the
    /// Explorer, or say there is nothing in it yet.
    pub(super) fn show_agent_lane_section(&mut self) {
        if self.agent_ledger.agents().is_empty() {
            self.status = String::from("Agent Lane: no agent has changed a file yet");
            return;
        }
        if !self.explorer_views.is_visible(ExplorerView::AgentLane) {
            self.toggle_explorer_view(ExplorerView::AgentLane);
        }
        self.agent_lane_panel.collapsed = false;
        self.show_tree = true;
        self.set_sidebar_view(SidebarView::Explorer);
        self.focus_pane(Pane::Tree);
    }

    /// Lay out and paint the Explorer's visible stacked sub-views top-to-bottom
    /// inside `area` (the region above the SYSTEM footer). Folders (the file
    /// tree) is the flexible region that absorbs leftover rows; every other
    /// visible section takes its own collapsible height, clipped so the tree
    /// always keeps a few rows. Sections hidden via the ⋯ menu are skipped, and
    /// their hit-test rects are zeroed so a stale click can't reach them.
    pub(super) fn render_explorer_sections(&mut self, frame: &mut ratatui::Frame, area: Rect) {
        self.open_editors.last_area = Rect::default();
        self.open_editors.save_all_btn = Rect::default();
        self.outline.last_area = Rect::default();
        self.timeline.last_area = Rect::default();
        self.dependencies.last_area = Rect::default();
        self.agent_lane_panel.last_area = Rect::default();
        let lane_rows = self.agent_lane_panel_rows();
        self.agent_lane_panel.set_rows(lane_rows);

        // The sections share the Explorer's focus, as VS Code dims them
        // together when the side bar loses the keyboard.
        let cx =
            self.view_cx(self.focus == Pane::Tree && self.sidebar_view == SidebarView::Explorer);
        let folders_visible = self.explorer_views.is_visible(ExplorerView::Folders);
        if !folders_visible {
            // The tree isn't painted, so zero its geometry too — otherwise a
            // click on a since-hidden row would still resolve against it.
            self.tree.last_inner = Rect::default();
            self.tree.last_area = Rect::default();
        }
        if area.height == 0 || area.width == 0 {
            if folders_visible {
                self.tree.draw(&cx, area, frame.buffer_mut());
            }
            return;
        }

        // Requested rows per visible section, in stacking order. Folders is a
        // placeholder (0) here; it claims the leftover after the others.
        let mut heights = [(ExplorerView::Folders, 0u16); ExplorerView::ALL.len()];
        let mut n_heights = 0usize;
        for &v in &ExplorerView::ALL {
            if !self.explorer_views.is_visible(v) || !self.explorer_view_available(v) {
                continue;
            }
            // Offered in the ⋯ menu always, but drawn only once an agent has
            // changed something: an empty lane would cost rows for nothing.
            if v == ExplorerView::AgentLane && self.agent_lane_panel.rows().is_empty() {
                continue;
            }
            let h = match v {
                ExplorerView::Folders => 0,
                ExplorerView::OpenEditors => self.open_editors.desired_height(area.height),
                ExplorerView::Outline => self.outline.desired_height(area.height),
                ExplorerView::Timeline => self.timeline.desired_height(area.height),
                ExplorerView::Dependencies => self.dependencies.desired_height(area.height),
                ExplorerView::AgentLane => self.agent_lane_panel.desired_height(area.height),
            };
            heights[n_heights] = (v, h);
            n_heights += 1;
        }
        let heights = &mut heights[..n_heights];
        if heights.is_empty() {
            return;
        }

        // Reserve a floor for the tree, then hand the fixed (non-Folders)
        // sections rows in order, clipping each to what's left.
        let total = area.height;
        let tree_floor = if folders_visible { 3u16.min(total) } else { 0 };
        let mut budget = total.saturating_sub(tree_floor);
        let mut fixed_sum = 0u16;
        for (v, h) in heights.iter_mut() {
            if *v == ExplorerView::Folders {
                continue;
            }
            let assigned = (*h).min(budget);
            *h = assigned;
            budget -= assigned;
            fixed_sum += assigned;
        }
        if folders_visible {
            if let Some(slot) = heights
                .iter_mut()
                .find(|(v, _)| *v == ExplorerView::Folders)
            {
                slot.1 = total.saturating_sub(fixed_sum);
            }
        } else {
            // No tree to absorb slack: grow the first section to fill the gap.
            let used: u16 = heights.iter().map(|(_, h)| *h).sum();
            if used < total
                && let Some(first) = heights.first_mut()
            {
                first.1 += total - used;
            }
        }

        let mut y = area.y;
        for &(v, h) in heights.iter() {
            if h == 0 {
                continue;
            }
            let rect = Rect {
                x: area.x,
                y,
                width: area.width,
                height: h,
            };
            y = y.saturating_add(h);
            match v {
                ExplorerView::Folders => {
                    self.tree.draw(&cx, rect, frame.buffer_mut());
                    // Croft Loch: every stacked section ends on a hairline,
                    // so the tree's blank bottom padding row takes the same
                    // seam when a section follows it.
                    if let Some(t) = crate::theme::tones(self.theme)
                        && rect.bottom() < area.bottom()
                        && rect.height > 1
                    {
                        crate::widgets::surface::paint_seam(
                            frame.buffer_mut(),
                            Rect {
                                y: rect.bottom() - 1,
                                height: 1,
                                ..rect
                            },
                            crate::widgets::surface::Edge::Bottom,
                            t.hairline(),
                        );
                    }
                }
                ExplorerView::OpenEditors => self.open_editors.draw(&cx, rect, frame.buffer_mut()),
                ExplorerView::Outline => self.outline.draw(&cx, rect, frame.buffer_mut()),
                ExplorerView::Timeline => self.timeline.draw(&cx, rect, frame.buffer_mut()),
                ExplorerView::Dependencies => self.dependencies.draw(&cx, rect, frame.buffer_mut()),
                ExplorerView::AgentLane => {
                    self.agent_lane_panel.draw(&cx, rect, frame.buffer_mut())
                }
            }
        }
    }

    pub(super) fn drain_fs_events(&mut self) -> bool {
        let init_changed = self.try_install_pending_init();
        let mut drain = self.fs_watch.drain(&mut self.tree, &self.editor);
        for w in &mut self.fs_watch_extra {
            let d = w.drain(&mut self.tree, &self.editor);
            drain.got_any |= d.got_any;
            drain.touched_open_file |= d.touched_open_file;
            drain.dirs_changed |= d.dirs_changed;
            drain.finder_relevant |= d.finder_relevant;
            // A secondary root's writes are an agent's writes too: dropping
            // them here would silently give a multi-root workspace an empty
            // review queue for every folder but the first (#345).
            drain.changed_files.extend(d.changed_files);
            drain.created_files.extend(d.created_files);
            drain.moved_in.extend(d.moved_in);
            drain.vanished.extend(d.vanished);
            drain.rescan_dropped_events |= d.rescan_dropped_events;
        }
        self.forward_disk_changes_to_lsp(&drain);
        if drain.dirs_changed {
            self.refresh_git_status_debounced();
            if drain.finder_relevant {
                self.kick_file_finder_index_rebuild();
            }
        }
        // `touched_open_file` only tracks the ACTIVE tab; a background tab's
        // file change still arrives as a watcher event (got_any), so sweep
        // every tab whenever any event lands. The sweep is stamp-based, so
        // a benign Metadata event that didn't move mtime+len is a no-op and
        // won't clobber selection or reload spuriously. Read-only Access
        // events never get here: `FsWatch::drain` drops them (#898).
        if drain.got_any {
            self.reload_open_file_after_external_change();
            // An open diff view follows the file the same way a buffer does
            // (#471): an agent rewriting the working file, or a save from a
            // sibling tab, re-diffs it in place.
            self.refresh_open_diff_views(false, &[], &drain.changed_files);
        }
        // Attribute the writes to whichever agents were working when they
        // landed (#345). Done after the reload so an agent's write to the
        // ACTIVE tab is hashed from the content the user is about to see.
        if !drain.changed_files.is_empty()
            && self.attribute_writes_to_working_agents(&drain.changed_files)
        {
            // Only when the ledger actually moved: the Explorer's dots must
            // match it, and a decoration that lags is worse than none.
            self.sync_agent_lane_decorations();
        }
        // A rescan means the watcher dropped events, so the queue is a
        // LOWER BOUND from here until it is emptied (#345). Recorded in the
        // ledger rather than only announced: a status line the next
        // keystroke clears would leave a partial queue looking complete.
        // Unconditional on whether an agent is working right now — the
        // dropped writes may have landed a moment ago, while the pane's
        // sampled status has since flipped to idle.
        if drain.rescan_dropped_events {
            self.agent_ledger.note_dropped_events();
            if !self.working_agents().is_empty() {
                self.status = String::from(
                    "File watcher overflowed: some agent changes may be missing from the review queue",
                );
            }
        }
        let polled = self.poll_filesystem_changes();
        drain.got_any || init_changed || polled
    }

    /// Tell the language servers about files that changed on disk (#908):
    /// a server like ty watches nothing itself, so a `git checkout` or an
    /// agent's edit to a file no tab holds stayed stale in it.
    pub(super) fn forward_disk_changes_to_lsp(&mut self, drain: &fs_watch::FsDrain) {
        if self.lsp.manager.is_none() {
            return;
        }
        let known = self.file_finder_index.clone();
        let known = known.as_deref().map_or(&[][..], Vec::as_slice);
        let changes = fs_watch::watched_file_changes(drain, known);
        if let Some(lsp) = self.lsp.manager.as_mut().filter(|_| !changes.is_empty()) {
            lsp.did_change_watched_files(changes);
        }
    }

    pub(super) fn handle_tree_key(&mut self, key: KeyEvent) {
        // Delete key (or Cmd+Backspace) trashes every selected node.
        if is_delete_node_key(key) {
            self.delete_selection();
            return;
        }
        if is_editor_select_all_key(key) {
            self.tree.select_all_visible();
            return;
        }
        if is_editor_copy_key(key) {
            self.copy_selection_to_explorer_clipboard(ExplorerClipMode::Copy);
            return;
        }
        if is_editor_cut_key(key) {
            self.copy_selection_to_explorer_clipboard(ExplorerClipMode::Cut);
            return;
        }
        if is_clipboard_paste_key(key) {
            self.paste_explorer_clipboard();
            return;
        }
        if is_compare_key(key) {
            self.toggle_compare_on_selected_file();
            return;
        }
        if key.code == KeyCode::Esc {
            if !self.tree.marked.is_empty() {
                self.tree.clear_marks();
            }
            return;
        }
        let extending = key.modifiers.contains(KeyModifiers::SHIFT);
        match key.code {
            KeyCode::Up => {
                if extending {
                    self.tree.move_up_extend();
                } else {
                    self.tree.move_up();
                }
            }
            KeyCode::Down => {
                if extending {
                    self.tree.move_down_extend();
                } else {
                    self.tree.move_down();
                }
            }
            KeyCode::PageUp => {
                if extending {
                    self.tree.page_up_extend(10);
                } else {
                    self.tree.page_up(10);
                }
            }
            KeyCode::PageDown => {
                if extending {
                    self.tree.page_down_extend(10);
                } else {
                    self.tree.page_down(10);
                }
            }
            KeyCode::Home => {
                if extending {
                    self.tree.home_extend();
                } else {
                    self.tree.home();
                }
            }
            KeyCode::End => {
                if extending {
                    self.tree.end_extend();
                } else {
                    self.tree.end();
                }
            }
            KeyCode::Enter => {
                if let Some(path) = self.tree.activate() {
                    // Ctrl+Enter is the primary "open in new tab" gesture — it
                    // reaches the app on every terminal. Cmd+Enter is also
                    // accepted but iTerm2 binds it to Toggle Fullscreen by
                    // default and never forwards it to the app unless the user
                    // has remapped that shortcut in iTerm Preferences →
                    // Profiles → Keys.
                    let in_new_tab = key.modifiers.intersects(
                        KeyModifiers::CONTROL | KeyModifiers::SUPER | KeyModifiers::META,
                    );
                    let result = if in_new_tab {
                        self.editor.open_pinned(&path)
                    } else {
                        self.editor.open_preview(&path)
                    };
                    match result {
                        Ok(()) => {
                            self.sync_open_file_poll_mtime();
                            self.status = self.editor.status.clone();
                            // Hand the keyboard to the editor, as VS Code does
                            // on Enter. Without this every navigation key the
                            // new tab defines is dead on arrival - arrows walk
                            // the file list while a PDF sits frozen on page 1
                            // and a spreadsheet never scrolls. Right-arrow
                            // (below) is the gesture that previews *without*
                            // leaving the tree.
                            self.focus_pane(Pane::Editor);
                        }
                        Err(e) => {
                            self.status = format!("Error: {e}");
                        }
                    }
                }
            }
            KeyCode::Right => {
                let selected_is_dir = self
                    .tree
                    .nodes
                    .get(self.tree.selected)
                    .is_some_and(|node| node.is_dir);
                if selected_is_dir {
                    self.tree.expand_selected();
                } else if let Some(path) = self.tree.activate() {
                    let result = self.editor.open_preview(&path);
                    match result {
                        Ok(()) => {
                            self.sync_open_file_poll_mtime();
                            self.status = self.editor.status.clone();
                        }
                        Err(e) => {
                            self.status = format!("Error: {e}");
                        }
                    }
                }
            }
            KeyCode::Left => {
                self.tree.collapse_selected();
            }
            KeyCode::Char(c)
                if !key.modifiers.intersects(
                    KeyModifiers::CONTROL | KeyModifiers::ALT | KeyModifiers::SUPER,
                ) =>
            {
                self.apply_tree_typeahead(c, std::time::Instant::now());
            }
            _ => {}
        }
    }
}

impl App {
    /// Move every dropped path into the directory the explorer is
    /// currently pointing at. Files outside the workspace are *moved* in
    /// (matching the user's Finder-drop expectation), not copied — they
    /// disappear from the source location and re-appear in the explorer.
    pub(super) fn import_paths_into_explorer(&mut self, paths: &[PathBuf]) {
        let dest_dir = self.paste_target_dir();
        let total = paths.len();
        let mut placed: Vec<PathBuf> = Vec::new();
        let mut errors: Vec<String> = Vec::new();
        let mut affected: BTreeSet<PathBuf> = BTreeSet::new();
        affected.insert(dest_dir.clone());
        for src in paths {
            if let Some(parent) = src.parent() {
                affected.insert(parent.to_path_buf());
            }
            match crate::widgets::file_tree::move_into(&dest_dir, src) {
                Ok(p) => placed.push(p),
                Err(e) => errors.push(format!("{}: {e}", src.display())),
            }
        }
        for dir in &affected {
            if let Some(idx) = self.tree.index_of_dir(dir) {
                self.tree.refresh_children(idx);
            }
        }
        self.tree.marked.clear();
        for p in &placed {
            self.tree.marked.insert(p.clone());
        }
        if let Some(first) = placed.first()
            && let Some(idx) = self.tree.nodes.iter().position(|n| &n.path == first)
        {
            self.tree.selected = idx;
            self.tree.anchor = idx;
        }
        self.status = if !errors.is_empty() {
            format!(
                "Imported {}/{}; failed: {}",
                placed.len(),
                total,
                errors.join("; ")
            )
        } else if total == 1 {
            format!(
                "Imported {} into {}",
                paths[0].display(),
                dest_dir.display()
            )
        } else {
            format!(
                "Imported {total} items into {}",
                self.status_path(&dest_dir)
            )
        };
    }

    /// Open the zoxide jump popup and seed it with the full frecency list
    /// (`zoxide query -l`), so it shows the user's top directories before
    /// they type anything — like pressing `j<Tab>` in the shell.
    pub(super) fn open_zoxide_jump(&mut self) {
        if self.zoxide_jump.is_some() {
            return;
        }
        let mut jump = crate::widgets::zoxide_jump::ZoxideJump::new();
        jump.install_state = crate::zoxide::install_state();
        jump.install_note = crate::zoxide::install_outcome();
        jump.termux = crate::iterm2_inline::detect_termux();
        jump.rc_hint = crate::zoxide::shell_rc_hint();
        jump.set_results(crate::zoxide::query(""));
        // Snapshot the full frecency list so the typo-tolerant fuzzy
        // fallback can rank against it without re-spawning zoxide on every
        // missed keystroke.
        jump.set_all_dirs(jump.results.clone());
        if jump.available {
            jump.hook_offer = crate::zoxide::shell_hook_missing();
        }
        let status = if let Some(rc) = jump
            .hook_offer
            .as_deref()
            .filter(|_| jump.hook_offer_shown())
        {
            let offer = crate::widgets::zoxide_jump::hook_offer_message(rc);
            format!("Jump: {}", offer.lines().next().unwrap_or_default())
        } else if jump.available {
            format!(
                "Jump: {} directories — type to filter, Esc to close",
                jump.results.len()
            )
        } else {
            // The popup body's first line (single source of truth).
            zoxide_jump_status(&jump)
        };
        self.zoxide_jump = Some(jump);
        self.overlays.popup_clear.request();
        self.status = status;
    }

    pub(super) fn close_zoxide_jump(&mut self) {
        self.zoxide_add_folder = false;
        if self.zoxide_jump.take().is_some() {
            self.overlays.popup_clear.request();
            self.overlays.activity.mark_dirty();
            self.overlays.welcome.mark_dirty();
            self.overlays.hero.mark_dirty();
            self.invalidate_editor_image_layouts();
            self.status.clear();
        }
    }

    /// Re-run `zoxide query` for the popup's current text and feed the
    /// ranked matches back to the widget. The one subprocess per keystroke
    /// is cheap (zoxide reads a small frecency DB; single-digit ms) and
    /// fires only on input, never on the render hot path.
    ///
    /// Two layers: the strict `zoxide query` is authoritative and handles
    /// the common case. Only when it returns zero matches for a non-empty
    /// needle does the typo-tolerant `fuzzy_rank` fallback run, ranking the
    /// cached frecency DB by edit distance so a transposition like `spilt`
    /// still finds `pr-split` (zoxide's own subsequence matcher cannot).
    /// The fallback is purely in-memory, so it never spawns zoxide twice.
    pub(super) fn refresh_zoxide_results(&mut self) {
        let Some(jump) = self.zoxide_jump.as_mut() else {
            return;
        };
        let needle = jump.query.clone();
        // The background install may have advanced (Running -> Done/Failed)
        // since the popup opened; keep the unavailable message current.
        jump.install_state = crate::zoxide::install_state();
        jump.install_note = crate::zoxide::install_outcome();
        match crate::zoxide::query(&needle) {
            Some(paths) if paths.is_empty() && !needle.trim().is_empty() => {
                let ranked = crate::zoxide::fuzzy_rank(&needle, &jump.all_dirs);
                if ranked.is_empty() {
                    jump.set_results(Some(Vec::new()));
                } else {
                    jump.set_approximate_results(ranked);
                }
            }
            other => jump.set_results(other),
        }
    }

    pub(super) fn handle_zoxide_jump_key(&mut self, key: KeyEvent) {
        let Some(jump) = self.zoxide_jump.as_mut() else {
            return;
        };
        match (key.code, key.modifiers) {
            (KeyCode::Esc, _) => {
                self.close_zoxide_jump();
            }
            (KeyCode::Enter, _) => self.apply_zoxide_jump_selection(),
            (KeyCode::Up, _) => jump.select_prev(),
            (KeyCode::Down, _) => jump.select_next(),
            (KeyCode::PageUp, _) => {
                for _ in 0..10 {
                    jump.select_prev();
                }
            }
            (KeyCode::PageDown, _) => {
                for _ in 0..10 {
                    jump.select_next();
                }
            }
            (KeyCode::Left, _) => jump.move_cursor_left(),
            (KeyCode::Right, _) => jump.move_cursor_right(),
            (KeyCode::Home, _) => jump.move_cursor_home(),
            (KeyCode::End, _) => jump.move_cursor_end(),
            (KeyCode::Backspace, _) => {
                jump.pop_char();
                self.refresh_zoxide_results();
            }
            (KeyCode::Delete, _) => {
                jump.delete_char();
                self.refresh_zoxide_results();
            }
            // The popup is asking "Install it? [y/N]" (zoxide is missing) or
            // "Add its `j` hook there? [y/N]" (installed, but nothing feeds it).
            (KeyCode::Char('y' | 'Y'), m)
                if jump.asks_yes_no()
                    && !m.intersects(KeyModifiers::CONTROL | KeyModifiers::SUPER) =>
            {
                if jump.available {
                    self.add_zoxide_hook();
                } else {
                    self.start_zoxide_install();
                }
            }
            (KeyCode::Char('n' | 'N'), m)
                if jump.asks_yes_no()
                    && !m.intersects(KeyModifiers::CONTROL | KeyModifiers::SUPER) =>
            {
                self.close_zoxide_jump();
            }
            (KeyCode::Char(c), m) if !m.intersects(KeyModifiers::CONTROL | KeyModifiers::SUPER) => {
                jump.push_char(c);
                self.refresh_zoxide_results();
            }
            _ => {}
        }
    }

    /// The user's `y` to the popup's install offer (#926): the only way croft
    /// ever runs the zoxide installer. A second `y` while it runs is a no-op.
    pub(super) fn start_zoxide_install(&mut self) {
        use crate::zoxide::InstallState;
        let Some(jump) = self.zoxide_jump.as_mut() else {
            return;
        };
        if jump.install_state == InstallState::Running {
            return;
        }
        crate::zoxide::install_in_background();
        jump.install_state = InstallState::Running;
        self.status = zoxide_jump_status(jump);
    }

    /// The user's `y` to the popup's hook offer (#926): zoxide is installed
    /// but no `zoxide init` line feeds its database, so the managed block
    /// goes into the rc the offer named. Launching croft no longer does it.
    pub(super) fn add_zoxide_hook(&mut self) {
        let Some(rc) = self.zoxide_jump.as_mut().and_then(|j| j.hook_offer.take()) else {
            return;
        };
        self.status = match crate::zoxide::add_shell_hook() {
            Some(rc) => {
                format!("Jump: added zoxide's `j` hook to {rc}; new terminals feed this list")
            }
            None => format!("Jump: zoxide's hook was not added to {rc} (see croft's log)"),
        };
    }

    /// Once the install the user asked for ends, bring the open popup up to
    /// date without a keystroke: the directory list when it worked, the
    /// reason when it did not, and what happened in the status bar. True
    /// when something changed.
    pub(super) fn poll_zoxide_install(&mut self) -> bool {
        use crate::zoxide::InstallState;
        let Some(jump) = self.zoxide_jump.as_mut() else {
            return false;
        };
        let state = crate::zoxide::install_state();
        if jump.install_state != InstallState::Running
            || !matches!(state, InstallState::Done | InstallState::Failed)
        {
            return false;
        }
        if state == InstallState::Done {
            jump.set_all_dirs(crate::zoxide::query("").unwrap_or_default());
        }
        self.refresh_zoxide_results();
        self.status = match (state, crate::zoxide::install_outcome()) {
            (InstallState::Done, Some(outcome)) => format!("Jump: {outcome}"),
            _ => self
                .zoxide_jump
                .as_ref()
                .map(zoxide_jump_status)
                .unwrap_or_default(),
        };
        true
    }

    /// Jump to (or add) the highlighted directory and dismiss the popup:
    /// Enter's action, shared with a mouse click on a result row.
    pub(super) fn apply_zoxide_jump_selection(&mut self) {
        let Some(jump) = self.zoxide_jump.as_ref() else {
            return;
        };
        let path = jump.selected_path().map(|p| p.to_path_buf());
        if let Some(path) = path {
            let add = self.zoxide_add_folder;
            self.close_zoxide_jump();
            // A zoxide row is a bare line from its database, so it can name a
            // directory in a spelling nothing else in croft uses (a symlinked
            // `/var/…` for `/private/var/…`). Normalise it here, where the
            // untrusted path enters, so both branches agree: the folder store
            // keys on `roots.primary().display()`, and two spellings of one
            // directory would file and restore under different keys.
            // `add_workspace_folder` canonicalises internally; the re-root
            // path must not, since its callers rely on the root being exactly
            // what they passed.
            let path = path.canonicalize().unwrap_or(path);
            if add {
                self.add_workspace_folder(path);
            } else {
                self.change_workspace_root(path);
            }
        }
    }

    pub(super) fn handle_zoxide_jump_mouse(&mut self, m: MouseEvent) {
        let Some(jump) = self.zoxide_jump.as_mut() else {
            return;
        };
        match answer_mouse(jump, m) {
            PickerMouse::Close => self.close_zoxide_jump(),
            PickerMouse::Accept => self.apply_zoxide_jump_selection(),
            PickerMouse::Stay => {}
        }
    }

    pub(super) fn render_zoxide_jump(&mut self, frame: &mut ratatui::Frame) {
        let sidebar = self.last_sidebar_area;
        let full = frame.area();
        let theme = self.theme;
        let Some(jump) = self.zoxide_jump.as_mut() else {
            return;
        };
        // Anchor the popup over the Explorer column (it is an Explorer
        // action), dropping from the sidebar's top-left. The sidebar is
        // narrow, so widen the box rightward over the editor for readable
        // paths, capped to the frame. When the sidebar is collapsed there
        // is nothing to anchor to, so fall back to a centered box.
        let rect = match sidebar {
            Some(side) if side.width >= 2 && side.height >= 4 => {
                let width = side
                    .width
                    .max(ZOXIDE_JUMP_MIN_WIDTH)
                    .min(full.width.saturating_sub(side.x));
                Rect {
                    x: side.x,
                    y: side.y,
                    width,
                    height: side.height,
                }
            }
            _ => fallback_popup_rect(full, 100),
        };
        crate::widgets::zoxide_jump::render_zoxide_jump(jump, rect, frame.buffer_mut(), theme);
    }

    /// Add `path` as another workspace root (multi-root, #147): the model
    /// grows, the Explorer gains the section, every root gets a watcher
    /// share, the Cmd+P index and workspace search span the new set, and
    /// the LSP layer widens its project-root bound (servers for the new
    /// root spawn per (language, project root) exactly like a fresh
    /// workspace's — croft's per-project server model needs no
    /// didChangeWorkspaceFolders, since no running server's folder set
    /// changes). Session/collab identity deliberately stays keyed on the
    /// PRIMARY root: adding a folder never changes what the window IS.
    pub fn add_workspace_folder(&mut self, path: PathBuf) {
        let path = path.canonicalize().unwrap_or(path);
        if !path.is_dir() {
            self.status = format!("Not a folder: {}", path.display());
            return;
        }
        if !self.roots.add(path.clone()) {
            self.status = String::from("Folder is already in the workspace");
            return;
        }
        self.tree.add_root(path.clone());
        crate::file_encoding::set_workspace_default(&path, self.files_encoding);
        self.reload_project_snippets();
        self.git_extra
            .push((path.clone(), GitWorker::spawn(path.clone())));
        self.rebind_workspace_watchers();
        self.sync_workspace_fanout();
        self.refresh_workspace_title();
        self.save_workspace_folders();
        self.install_file_associations(self.files_associations.clone());
        self.status = format!("Added folder: {}", path.display());
    }

    /// Remove a SECONDARY workspace root (#147). The primary is refused —
    /// it is the launch identity; changing it is Make Root's job. Open
    /// tabs under the removed root stay open (they become plain
    /// out-of-workspace files, VS Code behavior).
    pub fn remove_workspace_folder(&mut self, path: PathBuf) {
        if !self.roots.remove(&path) {
            self.status = String::from(
                "The primary workspace folder cannot be removed (use Make Root to change it)",
            );
            return;
        }
        self.tree.remove_root(&path);
        self.install_file_associations(self.files_associations.clone());
        crate::file_encoding::set_workspace_default(&path, None);
        self.snippets = load_all_snippets(self.roots.iter());
        // The removed folder's review queue goes with it (#345). Its rows
        // leave the tree, so no stale DOT renders — but the status chip
        // counts the same set, and a count that outlives the folder it
        // describes sends the user looking for rows that are not there.
        for gone in self
            .agent_ledger
            .unreviewed_paths()
            .into_iter()
            .filter(|p| p.starts_with(&path))
        {
            self.agent_ledger.forget_path(&gone);
        }
        self.sync_agent_lane_decorations();
        self.git_extra.retain(|(r, _)| r != &path);
        self.git_head_oids.remove(&path);
        if self.active_scm_root == path {
            // The panel was following the removed folder's repo: seed the
            // primary's immediately — assigning the field alone left the
            // removed repository on screen (#150 review).
            let primary = self.roots.primary().to_path_buf();
            self.seed_active_scm(primary);
        }
        self.rebind_workspace_watchers();
        self.sync_workspace_fanout();
        self.refresh_workspace_title();
        self.save_workspace_folders();
        self.status = format!("Removed folder: {}", path.display());
    }

    /// (Re)spawn one watcher per workspace root, splitting the per-OS
    /// watch caps across them (#147): the caps bound kernel resources per
    /// PROCESS, so N roots share one budget rather than multiplying it.
    pub(super) fn rebind_workspace_watchers(&mut self) {
        let shares = self.roots.iter().count();
        let primary = self.workspace_root().to_path_buf();
        self.fs_watch.rebind_sharing(&primary, &self.tree, shares);
        self.fs_watch_extra = self
            .roots
            .iter()
            .skip(1)
            .map(|r| FsWatch::spawn_event_only(r, shares))
            .collect();
    }

    /// Push the current root set into the per-root fan-outs that are not
    /// watcher-shaped: the Cmd+P index, the search worker + panel, and
    /// the LSP project-root bound.
    pub(super) fn sync_workspace_fanout(&mut self) {
        self.file_finder_index = None;
        self.file_finder = None;
        self.kick_file_finder_index_rebuild();
        let roots: Vec<PathBuf> = self.roots.iter().map(Path::to_path_buf).collect();
        let _ = self
            .search_query_tx
            .send(SearchRequest::SetRoots(roots.clone()));
        self.search_roots_changed();
        self.search.roots = roots.clone();
        if let Some(lsp) = &self.lsp.manager {
            lsp.set_extra_roots(roots.into_iter().skip(1).collect());
        }
    }

    /// Re-emit the OSC window title for the current folder set: the
    /// primary root's name, with VS Code's " (Workspace)" marker while
    /// more than one folder is open (#147).
    pub(super) fn refresh_workspace_title(&mut self) {
        use std::io::Write;
        let mut title = build_title(self.workspace_root());
        if self.roots.is_multi() {
            title.push_str(" (Workspace)");
        }
        let mut out = std::io::stdout();
        let _ = out.write_all(&set_title_seq(&title));
        let _ = out.flush();
    }

    /// Persist the current secondary-folder list under the primary
    /// root's key (#153) — the terminal-session model: an empty list
    /// prunes the key.
    pub(super) fn save_workspace_folders(&mut self) {
        let primary = self.roots.primary().display().to_string();
        let folders: Vec<PathBuf> = self.roots.iter().skip(1).map(Path::to_path_buf).collect();
        if let Err(e) =
            crate::workspace::save_folders_for_root(&self.workspace_folders_path, &primary, folders)
        {
            // A corrupt store is refused, never replaced (#156 review):
            // the arrangement still works this session, it just won't
            // persist until the store is repaired or removed.
            self.status = format!("Workspace folders not saved: {e}");
        }
    }

    /// Restore the saved folder set for the current primary root (#153):
    /// each surviving folder re-joins through the normal add fan-out; a
    /// folder that no longer exists on disk is dropped from the record
    /// rather than restored.
    pub fn restore_workspace_folders(&mut self) -> usize {
        let map = crate::workspace::load_folders(&self.workspace_folders_path);
        let primary = self.roots.primary().display().to_string();
        let Some(saved) = map.get(&primary).cloned() else {
            return 0;
        };
        let before = self.roots.iter().count();
        for folder in saved {
            if folder.is_dir() {
                self.add_workspace_folder(folder);
            }
        }
        // Rewrites the record from what actually restored, dropping
        // vanished folders (and pruning the key if none survived).
        self.save_workspace_folders();
        // The caller composes the user-facing message: setting status
        // here was immediately overwritten on the re-root path (#156
        // review).
        self.roots.iter().count() - before
    }

    /// Apply a `.code-workspace` file (#163): re-root to its first folder
    /// and install its folder set exactly (the pending set suppresses the
    /// automatic store restore for this re-root).
    pub(super) fn open_workspace_file(&mut self, path: &Path) {
        match crate::workspace::parse_workspace_file(path) {
            Ok(folders) => {
                let mut it = folders.into_iter();
                let primary = it.next().expect("parse guarantees a folder");
                if !primary.is_dir() {
                    // Refuse before touching the workspace: re-rooting
                    // into a vanished folder would strand the window
                    // (#164 review).
                    self.status = format!("Workspace file: missing folder {}", primary.display());
                    return;
                }
                self.pending_workspace_folders = Some(it.collect());
                self.change_workspace_root(primary);
            }
            Err(e) => self.status = format!("Workspace file: {e}"),
        }
    }

    /// Save the folder set to the `.code-workspace` at `dest`: a new file,
    /// or the `folders` of an existing one (#1012), and say where.
    pub(super) fn save_workspace_file(&mut self, dest: &Path) {
        let new_folder = dest.parent().filter(|p| !p.exists()).map(Path::to_path_buf);
        let folders: Vec<PathBuf> = self.roots.iter().map(Path::to_path_buf).collect();
        match crate::workspace::write_workspace_file(dest, &folders) {
            Ok(()) => {
                self.status = format!(
                    "Saved workspace ({} folder{}): {}{}",
                    folders.len(),
                    if folders.len() == 1 { "" } else { "s" },
                    dest.display(),
                    new_folder
                        .map(|f| format!(" (created folder {})", f.display()))
                        .unwrap_or_default()
                );
            }
            Err(e) => self.status = format!("Save workspace: {e}"),
        }
    }

    /// Open the fuzzy directory picker in add-folder mode (#147): the
    /// same zoxide-backed popup as Cmd+Z, with Enter adding the chosen
    /// directory as a workspace root instead of re-rooting.
    pub(super) fn open_add_folder_picker(&mut self) {
        self.zoxide_add_folder = true;
        self.open_zoxide_jump();
        if self.zoxide_jump.is_some() {
            self.status =
                String::from("Add Folder to Workspace: pick a directory — Enter adds, Esc cancels");
        }
    }

    /// Re-root the workspace at `new_root`. Resets the file tree, updates
    /// `workspace_root`, refreshes git status and the source-control
    /// panel, and respawns the FS watcher off-thread (a recursive stat
    /// walk on a multi-GB monorepo is the dominant cost - blocking the
    /// UI thread for it is what froze the app on "Make root"). The
    /// polling fallback in `drain_fs_events` keeps the tree fresh until
    /// the watcher comes online via `try_install_pending_init`. Open
    /// editor tabs are NOT closed - a path that escapes the new root
    /// still resolves on disk and the user may want to keep it open.
    /// Compare anchor / clipboard / marks are reset because they point
    /// into the old workspace.
    pub fn change_workspace_root(&mut self, new_root: PathBuf) {
        // The review queue being left is saved under its own root first.
        self.persist_agent_ledger(true);
        let display = new_root.display().to_string();
        // The remembered task belongs to the old workspace; Rerun Last Task
        // must rediscover, not rerun the old project's command here.
        self.last_task = None;
        // Read the panel's untouched-ness BEFORE the cd seed below: the seed
        // is an input write, which clears it. Then persist the outgoing
        // workspace's pane layout under ITS key while `workspace_root` still
        // names it — every save after the swap keys on the new root, which
        // both discarded this layout on re-root and filed the old panes
        // under the new workspace's entry (#137).
        let panel_pristine = self.terminals.len() == 1 && self.terminals[0].is_pristine();
        self.save_terminal_session();
        let mut shell_synced = true;
        if let Some(active) = self.terminals.get_mut(self.active_terminal) {
            // cwd_seed_is_safe, not foreground_is_shell: a pane whose shell
            // is still starting up (rc files own the tty, no input ever
            // written, no prompt yet) must still get the seed — it queues as
            // type-ahead for the first prompt (#94).
            if active.cwd_seed_is_safe() {
                active.change_cwd(&new_root);
            } else {
                shell_synced = false;
            }
        }
        // The hot-exit backup is keyed by the workspace: this croft's goes
        // with it to the new root at the next tick, rather than staying
        // where the old workspace's next launch would restore it (#862).
        self.remove_own_hot_exit();
        // The `files_encoding` defaults of the folders being left go with
        // them (#1432). The settings merge below sets only the new roots,
        // and a default left on a parent folder would still prefix-match
        // every file of a nested new root and decode it in the old encoding.
        for root in self.roots.iter() {
            crate::file_encoding::set_workspace_default(root, None);
        }
        self.roots.replace_primary(new_root.clone());
        // The old repository's snippets go, the new one's apply (#1484).
        self.reload_project_snippets();
        // A panel the user never touched — the startup default, one unnamed
        // shell with no input ever typed and no launched program — is swapped
        // for the incoming workspace's saved layout, the same restore startup
        // runs. A panel with any user state instead survives the re-root
        // as-is: navigation must never destroy live shells.
        if panel_pristine {
            self.restore_terminal_session();
        }
        // Rebind the LSP servers to the new root. The manager captured its
        // workspace folder once, at App::new, from croft's launch directory -
        // and the Croft.app launcher hardcodes that to ~/Documents. Re-rooting
        // into a child repo left rust-analyzer running `cargo metadata` against
        // the parent, which has no Cargo.toml, so every opened .rs came back as
        // an `unlinked-file` with no hover / completion / semantic tokens.
        // `LspState::rebind` drops the old manager off the UI thread (#737)
        // and starts one at the new root, unless a source build of croft has
        // the servers stopped (#694). What the old servers were sent and
        // reported goes with them: the next sync_lsp() re-opens every live
        // tab against the new servers, and their questions, whose answers
        // would go back to a server that is gone, are dropped (#2880).
        if let Some(msg) = self.lsp.rebind(&new_root) {
            // Not stderr: the TUI owns the screen here, and text written
            // around ratatui stays on it (#537's class of leak).
            crate::output::push("Language Servers", crate::output::OutputLevel::Error, &msg);
            self.status = msg;
        }
        if self
            .list_picker
            .as_ref()
            .is_some_and(|p| p.purpose == crate::widgets::list_picker::ListPurpose::ServerQuestion)
        {
            self.close_list_picker();
        }
        self.forget_build_problems();
        // Refresh the window/icon title: it was emitted once at startup from
        // the launch dir and never updated, so after a re-root it kept showing
        // the parent (e.g. "Documents") instead of the repo. OSC 0 is
        // grid-independent, so writing it straight to stdout between draws is
        // safe - the same pattern startup uses.
        {
            use std::io::Write;
            let mut out = std::io::stdout();
            let _ = out.write_all(&set_title_seq(&build_title(&new_root)));
            let _ = out.flush();
        }
        self.tree.set_root(new_root.clone());
        self.tree_clipboard = None;
        self.compare_anchor = None;
        // The agent review queue belongs to the workspace that was left
        // (#345). `set_root` has just cleared the Explorer's copy; this
        // clears the ledger it is drawn from, so the two cannot come back
        // apart the moment an agent writes under the new root. The new
        // root's own saved queue comes back in its place.
        self.agent_ledger = self.load_agent_ledger(&new_root);
        self.fs_watch.rebind(&new_root, &self.tree);
        // A re-root collapses the workspace to one folder: the secondary
        // watchers go with their roots (#147).
        self.fs_watch_extra.clear();
        self.rebind_source_control(&new_root);
        // Rebind the test runner too, else `cargo test -- --list` keeps running
        // in the launch dir captured at spawn (e.g. ~/Documents, no Cargo.toml)
        // and the Testing view stays empty after a Make Root into a child repo.
        // The reset is the folder follow's own (#2900). With the Testing view
        // in front, re-discover right away; otherwise the now-empty panel
        // discovers on its next open.
        self.reset_testing_to(new_root.clone());
        self.forget_workspace_launches();
        if self.sidebar_view == SidebarView::Testing {
            self.discover_tests();
        }
        self.rebind_search(&new_root);
        self.restart_file_index();
        // Rebind the resident navigator: the activation record, relay
        // socket, and single-host lock are all keyed by workspace root, so
        // without this a `croft pair` run in the NEW root writes a record
        // this window never reads and the navigator can never seat again
        // after a re-root. Everything else the navigator keeps starts over
        // with it (`PairState::rebind`): the old root's comment boxes, turn,
        // death latch and refusal already said (#2878, #2879). Its seat is
        // torn down here; if this window had self-appointed the old root's
        // collab owner, that session goes too (a fresh activation
        // self-appoints against the new root).
        let (host, host_lock) = self.pair.rebind(&new_root);
        if let Some(_host) = host {
            self.status = String::from("Navigator unseated (workspace changed)");
        }
        self.clear_problem_fix();
        self.editor.comment_focus = None;
        // The multiplayer session starts over with the navigator: one this
        // window hosted ends with the host lock, and old-root merge bases
        // and peer carets go (`CollabState::rebind`). A stream that ended
        // with the session closes its undo step here (#2891).
        if let Some(stream) = self.collab.rebind(host_lock.is_some()) {
            for ed in self
                .editor_layout
                .all_groups_mut(&mut self.editor)
                .flat_map(|g| g.editors.iter_mut())
            {
                ed.end_remote_undo_group(stream.undo_group);
            }
        }
        drop(host_lock);
        // The new primary's saved folder set comes back with it (#153),
        // the same way its terminal layout does: re-rooting into a
        // workspace restores that workspace's arrangement.
        let restored = match self.pending_workspace_folders.take() {
            Some(folders) => {
                let before = self.roots.iter().count();
                for folder in folders {
                    self.add_workspace_folder(folder);
                }
                self.roots.iter().count() - before
            }
            None => self.restore_workspace_folders(),
        };
        let folders_note = if restored > 0 {
            format!(
                " (+{restored} workspace folder{})",
                if restored == 1 { "" } else { "s" }
            )
        } else {
            String::new()
        };
        // The workspace settings layers follow the primary root (#251): the
        // old repo's .croft/config.json must stop applying and the new one
        // start, without a relaunch. Runs after the root swap above so the
        // merge reads the new primary. The status write below is deliberate:
        // remerge_settings sets its own status, and the re-root message is
        // the one that should win here.
        self.remerge_settings();
        self.status = if shell_synced {
            format!("Workspace root: {display}{folders_note}")
        } else {
            format!("Workspace root: {display}{folders_note} (terminal busy; shell not moved)")
        };
    }

    pub(super) fn is_explorer_focused(&self) -> bool {
        self.focus == Pane::Tree && self.sidebar_view == SidebarView::Explorer
    }

    /// Dispatch the Explorer-pane keyboard shortcuts (Cmd+F / Cmd+Shift+F /
    /// Cmd+R / F2 / Cmd+/). Returns true if the key was handled. The
    /// target node is whichever row the user has highlighted; folder-only
    /// actions (Make root) are no-ops on file selections.
    pub(super) fn handle_explorer_shortcut(&mut self, key: KeyEvent) -> bool {
        if is_tree_zoxide_jump_key(key) {
            self.open_zoxide_jump();
            return true;
        }
        if is_tree_new_file_key(key) {
            let target = self.explorer_create_target_dir();
            self.open_create_prompt(CreateKind::File, target);
            return true;
        }
        if is_tree_new_folder_key(key) {
            let target = self.explorer_create_target_dir();
            self.open_create_prompt(CreateKind::Folder, target);
            return true;
        }
        if is_tree_rename_key(key) {
            if let Some(path) = self.explorer_selected_path() {
                self.open_rename_prompt(path);
            }
            return true;
        }
        // Reveal in Finder is a local-macOS-only host bridge: on a remote SSH
        // session croft runs on a headless host with no Finder, so the chord
        // falls through there exactly as the menu entry is withheld.
        if !self.session.is_remote && cfg!(target_os = "macos") && is_reveal_in_finder_key(key) {
            if let Some(path) = self.explorer_selected_path() {
                self.reveal_in_finder(path);
            }
            return true;
        }
        if is_tree_make_root_key(key) {
            if let Some(path) = self.explorer_selected_folder() {
                self.change_workspace_root(path);
            }
            return true;
        }
        if is_tree_make_parent_root_key(key) {
            if let Some(path) = self.explorer_parent_for_make_root() {
                self.change_workspace_root(path);
            }
            return true;
        }
        false
    }

    /// Resolve the parent folder of the highlighted node for the
    /// `Cmd+Shift+/` "Make root at parent" gesture. Works on files
    /// (parent = containing directory), folders (parent = its parent),
    /// and the tree root itself (parent = one filesystem level up).
    /// Returns `None` when the selected path has no parent (filesystem
    /// root) or no node is currently selected.
    pub(super) fn explorer_parent_for_make_root(&self) -> Option<PathBuf> {
        let n = self.tree.nodes.get(self.tree.selected)?;
        n.path.parent().map(|p| p.to_path_buf())
    }

    pub(super) fn explorer_selected_path(&self) -> Option<PathBuf> {
        self.tree
            .nodes
            .get(self.tree.selected)
            .map(|n| n.path.clone())
    }

    pub(super) fn explorer_selected_folder(&self) -> Option<PathBuf> {
        let n = self.tree.nodes.get(self.tree.selected)?;
        if n.is_dir && n.path != self.tree.root {
            Some(n.path.clone())
        } else {
            None
        }
    }

    pub(super) fn explorer_create_target_dir(&self) -> PathBuf {
        let node = self.tree.nodes.get(self.tree.selected);
        crate::widgets::file_tree::create_target_dir_for(node, &self.tree.root)
    }

    /// Where the palette's File: New File… / New Folder… create (#852): the
    /// Explorer selection while the Explorer has focus, as its own chords
    /// do; otherwise beside the active file, falling back to the selection
    /// when no file is open.
    pub(super) fn palette_create_target_dir(&self) -> PathBuf {
        if !self.is_explorer_focused()
            && let Some(dir) = self.editor.path.as_deref().and_then(Path::parent)
        {
            return dir.to_path_buf();
        }
        self.explorer_create_target_dir()
    }

    /// VS Code-style type-to-jump for the Explorer. Each printable
    /// keystroke arriving while the file tree is focused appends to a
    /// short-lived prefix buffer and snaps selection to the first
    /// visible node whose name starts with that prefix. Keystrokes
    /// further apart than `TREE_TYPEAHEAD_WINDOW` reset the buffer to
    /// a single character and cycle to the next match, so re-pressing
    /// the same letter walks through siblings beginning with it.
    /// `now` is injected for deterministic tests.
    pub(super) fn apply_tree_typeahead(&mut self, c: char, now: std::time::Instant) {
        let within_window = self
            .tree_typeahead
            .as_ref()
            .is_some_and(|t| now.duration_since(t.last) < TREE_TYPEAHEAD_WINDOW);
        let prefix = if within_window {
            let mut p = self.tree_typeahead.as_ref().unwrap().prefix.clone();
            p.push(c.to_ascii_lowercase());
            p
        } else {
            c.to_ascii_lowercase().to_string()
        };
        let n = self.tree.nodes.len();
        if n > 0 {
            let start = if within_window {
                self.tree.selected
            } else {
                (self.tree.selected + 1) % n
            };
            if let Some(idx) = self.tree.find_prefix(&prefix, start) {
                self.tree.selected = idx;
                self.tree.anchor = idx;
                self.tree.marked.clear();
            }
        }
        self.tree_typeahead = Some(TreeTypeahead { prefix, last: now });
    }

    /// Trash every entry in the tree's current action set (the multi-
    /// selection if non-empty, otherwise just the focused row). Workspace
    /// root rows are filtered by `delete_paths` — the shared choke point —
    /// even if some other path in the set proceeds.
    pub(super) fn delete_selection(&mut self) {
        let paths = self.tree.action_paths();
        if paths.is_empty() {
            return;
        }
        self.delete_paths(paths);
    }

    pub(super) fn delete_paths(&mut self, paths: Vec<PathBuf>) {
        // No workspace root row is ever a delete target, whichever flow
        // assembled the set: the context menu forwards a mixed multi-
        // selection wholesale, and Select All marks the root rows too, so
        // the per-node `delete_target_for` guard alone cannot protect a
        // root riding along in the batch. Filter here — the one choke
        // point every delete flow funnels through — with the same
        // canonicalized comparison the old primary-root guard used, so a
        // root reached through a symlinked path is still refused.
        let canon_roots: Vec<PathBuf> = self
            .tree
            .root_paths()
            .filter_map(|r| r.canonicalize().ok())
            .collect();
        let is_root = |p: &PathBuf| {
            self.tree.root_paths().any(|r| r == p)
                || p.canonicalize()
                    .ok()
                    .is_some_and(|cp| canon_roots.contains(&cp))
        };
        let paths: Vec<PathBuf> = paths.into_iter().filter(|p| !is_root(p)).collect();
        let total = paths.len();
        if total == 0 {
            self.status = String::from("Workspace roots cannot be deleted");
            return;
        }
        // Confirm before trashing — Delete (or the context menu's Delete) must
        // not remove files from a single action. Enter or Y performs it; Esc
        // or N keeps them, and there is no field to type a different answer
        // into (#855). Files go to the OS Trash (recoverable), so the wording
        // reflects that.
        use crate::widgets::input_prompt::{InputPrompt, InputPurpose};
        let title = if total == 1 {
            let name = paths[0]
                .file_name()
                .map(|n| n.to_string_lossy().into_owned())
                .unwrap_or_else(|| paths[0].display().to_string());
            format!("Move '{name}' to Trash?")
        } else {
            format!("Move {total} items to Trash?")
        };
        self.open_input_prompt(InputPrompt::confirm(
            InputPurpose::TreeDelete { paths },
            title,
            "Enter or Y to trash · Esc or N to keep",
        ));
    }

    /// Re-point the tabs of a renamed or moved path in every editor group,
    /// not just the focused one: a tab in the other split kept the old path,
    /// raised a disk conflict, and a save brought the old file back.
    pub(super) fn rename_open_path_everywhere(&mut self, old: &Path, new: &Path) {
        for group in self.editor_layout.all_groups_mut(&mut self.editor) {
            group.rename_open_path(old, new);
        }
    }

    /// Perform the confirmed trash of `paths` (the popup's Enter path).
    pub(super) fn perform_delete_paths(&mut self, paths: Vec<PathBuf>) {
        let total = paths.len();
        if total == 0 {
            return;
        }
        let mut affected_dirs: BTreeSet<PathBuf> = BTreeSet::new();
        for path in &paths {
            let parent = path
                .parent()
                .map(Path::to_path_buf)
                .unwrap_or_else(|| self.tree.root.clone());
            affected_dirs.insert(parent);
        }
        // One trash call for the whole batch — on macOS this routes through
        // Finder so the trash sound effect fires once for the operation
        // instead of once per file. The single-path path keeps the cheaper
        // NSFileManager backend.
        let result = if total == 1 {
            crate::widgets::file_tree::move_to_trash(&paths[0])
        } else {
            crate::widgets::file_tree::move_to_trash_bulk(&paths)
        };
        match result {
            Ok(()) => {
                // Every group's tabs, and files under a deleted folder too.
                // A tab with unsaved edits is kept open: closing it threw the
                // edits away while the trash only held the saved copy.
                let mut kept = 0;
                for path in &paths {
                    for group in self.editor_layout.all_groups_mut(&mut self.editor) {
                        kept += group.close_clean_tabs_under(path);
                    }
                }
                self.sync_open_file_poll_mtime();
                for dir in &affected_dirs {
                    if let Some(idx) = self.tree.index_of_dir(dir) {
                        self.tree.refresh_children(idx);
                    }
                }
                self.tree.clear_marks();
                self.status = if total == 1 {
                    format!("Moved {} to Trash", paths[0].display())
                } else {
                    format!("Moved {total} items to Trash")
                };
                if kept > 0 {
                    self.status.push_str(&format!(
                        "; {kept} tab{} with unsaved changes kept open",
                        if kept == 1 { "" } else { "s" }
                    ));
                }
            }
            Err(e) => {
                for dir in &affected_dirs {
                    if let Some(idx) = self.tree.index_of_dir(dir) {
                        self.tree.refresh_children(idx);
                    }
                }
                self.status = if total == 1 {
                    format!("Delete failed: {e}")
                } else {
                    format!("Bulk delete failed: {e}")
                };
            }
        }
    }

    /// Stash the current action paths into the explorer clipboard. The
    /// system text clipboard is left untouched so editor/terminal text
    /// copy gestures still work elsewhere in the app.
    pub(super) fn copy_selection_to_explorer_clipboard(&mut self, mode: ExplorerClipMode) {
        let paths = self.tree.action_paths();
        if paths.is_empty() {
            return;
        }
        let n = paths.len();
        self.tree_clipboard = Some(ExplorerClipboard { mode, paths });
        self.status = match (mode, n) {
            (ExplorerClipMode::Copy, 1) => String::from("Copied 1 item"),
            (ExplorerClipMode::Copy, _) => format!("Copied {n} items"),
            (ExplorerClipMode::Cut, 1) => String::from("Cut 1 item"),
            (ExplorerClipMode::Cut, _) => format!("Cut {n} items"),
        };
    }

    /// Resolve the directory that an explorer paste/drop should land in,
    /// based on the currently focused node. Mirrors `create_target_dir_for`
    /// but for keyboard paste (no right-click coordinates).
    pub(super) fn paste_target_dir(&self) -> PathBuf {
        let node = self.tree.nodes.get(self.tree.selected);
        crate::widgets::file_tree::create_target_dir_for(node, &self.tree.root)
    }

    /// Move (Cut) or copy (Copy) every clipboard path into `dest_dir`,
    /// then refresh affected directories and select the freshly-pasted
    /// items. Cut clears the explorer clipboard on success; Copy preserves
    /// it so the user can paste again.
    pub(super) fn paste_explorer_clipboard(&mut self) {
        let dest_dir = self.paste_target_dir();
        self.paste_into(dest_dir);
    }

    /// `Ctrl/Cmd+D` from the explorer: smart toggle that mirrors the right-
    /// click "Select for Compare" / "Compare with Selected" / clear-anchor
    /// chain in a single key press.
    pub(super) fn toggle_compare_on_selected_file(&mut self) {
        let Some(node) = self.tree.nodes.get(self.tree.selected) else {
            self.status = String::from("No file selected to compare");
            return;
        };
        if node.is_dir {
            self.status = String::from("Compare needs a file, not a folder");
            return;
        }
        let path = node.path.clone();
        match self.compare_anchor.as_ref() {
            Some(anchor) if anchor == &path => {
                self.compare_anchor = None;
                self.status = String::from("Cleared compare anchor");
            }
            Some(anchor) => {
                let anchor_clone = anchor.clone();
                match self.open_diff_across_groups(&anchor_clone, &path) {
                    Ok(()) => {
                        self.focus_pane(Pane::Editor);
                        self.compare_anchor = None;
                        self.sync_open_file_poll_mtime();
                        let l = anchor_clone
                            .file_name()
                            .map(|n| n.to_string_lossy().into_owned())
                            .unwrap_or_else(|| anchor_clone.display().to_string());
                        let r = path
                            .file_name()
                            .map(|n| n.to_string_lossy().into_owned())
                            .unwrap_or_else(|| path.display().to_string());
                        self.status = format!("Diff: {l} \u{2194} {r}");
                    }
                    Err(e) => {
                        self.status = format!("Diff failed: {e}");
                    }
                }
            }
            None => {
                let label = path
                    .file_name()
                    .map(|n| n.to_string_lossy().into_owned())
                    .unwrap_or_else(|| path.display().to_string());
                self.compare_anchor = Some(path);
                self.status =
                    format!("Selected {label} for compare — Ctrl/Cmd+D again on another file");
            }
        }
    }

    pub(super) fn paste_into(&mut self, dest_dir: PathBuf) {
        let Some(clip) = self.tree_clipboard.clone() else {
            self.status = String::from("Explorer clipboard is empty");
            return;
        };
        let moved = self.move_or_copy_in_explorer(&dest_dir, &clip.paths, clip.mode);
        // A cut pasted back where it already is moved nothing, so it stays
        // on the clipboard for the paste into the folder it was meant for.
        if moved && matches!(clip.mode, ExplorerClipMode::Cut) {
            self.tree_clipboard = None;
        }
    }

    /// Explorer paste and drag-drop. A move asks the language servers for
    /// their import edits first (#610); a copy renames nothing, so it runs
    /// at once.
    pub(super) fn move_or_copy_in_explorer(
        &mut self,
        dest_dir: &Path,
        paths: &[PathBuf],
        mode: ExplorerClipMode,
    ) -> bool {
        if matches!(mode, ExplorerClipMode::Copy) {
            let _ = self.apply_paste_or_drop(dest_dir, paths, mode);
            return true;
        }
        // An item already in `dest_dir` stays as it is: moving it there is
        // no move, and a fresh name for it renamed the file to "<name> copy"
        // on disk, retargeting its tabs and the servers' imports (#1174).
        let paths: Vec<PathBuf> = paths
            .iter()
            .filter(|p| !crate::widgets::file_tree::is_directly_in(dest_dir, p))
            .cloned()
            .collect();
        if paths.is_empty() {
            self.status = String::from("Already in this folder: nothing to move");
            return false;
        }
        let renames = paths
            .iter()
            .map(|src| crate::lsp::manager::FileRenameOp {
                old: src.clone(),
                new: crate::widgets::file_tree::unique_destination_in(dest_dir, src),
                is_dir: src.is_dir(),
            })
            .collect();
        let op = FileMove::Paste {
            dest_dir: dest_dir.to_path_buf(),
            paths,
        };
        self.start_file_move(op, renames);
        true
    }

    /// Ask the servers about `renames`, then run `op` once they answer (or
    /// at once, with no language server running).
    pub(super) fn start_file_move(
        &mut self,
        op: FileMove,
        renames: Vec<crate::lsp::manager::FileRenameOp>,
    ) {
        // A second move while one is pending runs the first now: two moves
        // must not reorder, and the first has waited long enough.
        if let Some(prev) = self.pending_file_move.take() {
            self.finish_file_move(prev, Vec::new());
        }
        let Some(lsp) = self
            .lsp
            .manager
            .as_mut()
            .filter(|l| l.any_server_wants_file_renames())
        else {
            if self.run_file_move(op)
                && let Some(lsp) = self.lsp.manager.as_mut()
            {
                lsp.did_rename_files(renames);
            }
            return;
        };
        let request_id = lsp.request_will_rename_files(renames.clone());
        self.status = String::from("Updating references before the move\u{2026}");
        self.pending_file_move = Some(PendingFileMove {
            request_id,
            op,
            renames,
            deadline: std::time::Instant::now() + FILE_MOVE_DEADLINE,
            baseline: self.edit_seqs_in_every_group(),
        });
    }

    /// Land the servers' answer to a pending Explorer move, or give up on
    /// them at the deadline and move anyway. Returns true when the screen
    /// changed.
    pub fn tick_file_moves(&mut self) -> bool {
        let Some(pending) = self.pending_file_move.as_ref() else {
            return false;
        };
        let mut answer = None;
        if let Some(lsp) = self.lsp.manager.as_ref() {
            while let Some(result) = lsp.drain_will_rename_files() {
                if result.request_id == pending.request_id {
                    answer = Some(result.edits);
                }
            }
        }
        if answer.is_none() && std::time::Instant::now() < pending.deadline {
            return false;
        }
        let pending = self.pending_file_move.take().expect("checked above");
        self.finish_file_move(pending, answer.unwrap_or_default());
        true
    }

    /// Run the move, then apply the servers' edits and tell the servers it
    /// happened. The move goes first: a move that fails (a folder into its
    /// own child, a name already taken) must not leave every importer
    /// rewritten to a path that does not exist. The edits name files at
    /// their old paths, so they are carried to where the move put them.
    pub(super) fn finish_file_move(
        &mut self,
        pending: PendingFileMove,
        edits: Vec<(PathBuf, Vec<crate::widgets::editor::TextSpanEdit>)>,
    ) {
        if !self.run_file_move(pending.op) {
            return;
        }
        let moved_status = self.status.clone();
        if !edits.is_empty() {
            let edits: Vec<_> = edits
                .into_iter()
                .map(|(path, e)| (moved_path(&path, &pending.renames), e))
                .collect();
            // The move carried open tabs to their new paths; so too their
            // request-time seqs, for the check below to find them.
            let baseline: Vec<_> = pending
                .baseline
                .iter()
                .map(|(path, seq)| (moved_path(path, &pending.renames), *seq))
                .collect();
            if let Some(file) = self.stale_edit_target(&baseline, &edits) {
                self.status = format!(
                    "{moved_status}; references not updated: {file} changed while the server was working"
                );
            } else {
                match self.apply_rename_edits(&edits) {
                    Ok((files, _)) => {
                        self.status =
                            format!("{moved_status}, references updated in {files} file(s)");
                    }
                    Err(e) => {
                        self.status = format!("{moved_status}; could not update references: {e}")
                    }
                }
            }
        }
        if let Some(lsp) = self.lsp.manager.as_mut() {
            lsp.did_rename_files(pending.renames);
        }
    }

    /// Perform an Explorer rename or move. Returns whether anything moved.
    pub(super) fn run_file_move(&mut self, op: FileMove) -> bool {
        match op {
            FileMove::Paste { dest_dir, paths } => {
                self.apply_paste_or_drop(&dest_dir, &paths, ExplorerClipMode::Cut)
            }
            FileMove::Rename {
                parent,
                old,
                new_name,
            } => match crate::widgets::file_tree::rename_in(&parent, &old, &new_name) {
                Ok(new_path) => {
                    self.status = format!("Renamed to {}", self.status_path(&new_path));
                    if let Some(idx) = self.tree.index_of_dir(&parent) {
                        self.tree.refresh_children(idx);
                        if let Some(new_idx) =
                            self.tree.nodes.iter().position(|n| n.path == new_path)
                        {
                            self.tree.select(new_idx);
                        }
                    }
                    self.rename_open_path_everywhere(&old, &new_path);
                    self.rename_review_boxes_path(&old, &new_path);
                    crate::history::move_path(&self.history_root, &old, &new_path);
                    self.sync_open_file_poll_mtime();
                    true
                }
                Err(e) => {
                    self.status = format!("Rename failed: {e}");
                    false
                }
            },
        }
    }

    /// Shared implementation for explorer paste and drag-drop. `mode`
    /// distinguishes a move (Cut/drag) from a copy (Copy/Alt-drag).
    /// Returns whether every item landed.
    pub(super) fn apply_paste_or_drop(
        &mut self,
        dest_dir: &Path,
        paths: &[PathBuf],
        mode: ExplorerClipMode,
    ) -> bool {
        let mut affected: BTreeSet<PathBuf> = BTreeSet::new();
        affected.insert(dest_dir.to_path_buf());
        let mut placed: Vec<PathBuf> = Vec::new();
        let mut errors: Vec<String> = Vec::new();
        for src in paths {
            let result = match mode {
                ExplorerClipMode::Cut => {
                    let parent = src
                        .parent()
                        .map(Path::to_path_buf)
                        .unwrap_or_else(|| self.tree.root.clone());
                    affected.insert(parent);
                    crate::widgets::file_tree::move_into(dest_dir, src)
                }
                ExplorerClipMode::Copy => crate::widgets::file_tree::copy_into(dest_dir, src),
            };
            match result {
                Ok(p) => {
                    // Every tab, not just the active one: a background tab of
                    // a moved file kept the old path and recreated it on save.
                    if matches!(mode, ExplorerClipMode::Cut) {
                        self.rename_open_path_everywhere(src, &p);
                        self.rename_review_boxes_path(src, &p);
                        crate::history::move_path(&self.history_root, src, &p);
                    }
                    placed.push(p);
                }
                Err(e) => errors.push(format!("{}: {e}", src.display())),
            }
        }
        for dir in &affected {
            if let Some(idx) = self.tree.index_of_dir(dir) {
                self.tree.refresh_children(idx);
            }
        }
        self.tree.marked.clear();
        for p in &placed {
            self.tree.marked.insert(p.clone());
        }
        if let Some(first) = placed.first()
            && let Some(idx) = self.tree.nodes.iter().position(|n| &n.path == first)
        {
            self.tree.selected = idx;
            self.tree.anchor = idx;
        }
        let verb = match mode {
            ExplorerClipMode::Cut => "Moved",
            ExplorerClipMode::Copy => "Copied",
        };
        if !errors.is_empty() {
            self.status = format!(
                "{verb} {}/{}; failed: {}",
                placed.len(),
                paths.len(),
                errors.join("; ")
            );
        } else if placed.len() == 1 {
            self.status = format!("{verb} 1 item to {}", self.status_path(dest_dir));
        } else {
            self.status = format!(
                "{verb} {} items to {}",
                placed.len(),
                self.status_path(dest_dir)
            );
        }
        errors.is_empty()
    }
}

/// Map a screen row to the tree row that should be highlighted as a drop
/// target. Hovering a directory targets that directory; hovering a file
/// targets its parent directory (so dragging onto a file row drops the
/// payload as siblings of that file). Returns `None` when the pointer is
/// over empty tree space, when the row would be a no-op (hovering an
/// item that's part of the drag set), or when the resolved directory is
/// inside one of the dragged folders.
pub(super) fn drag_target_index(
    tree: &crate::widgets::file_tree::FileTree,
    y: u16,
    drag_paths: &[PathBuf],
) -> Option<usize> {
    // The sticky band wins the row resolution: a drop on a pinned ancestor
    // targets THAT directory, not whatever row the band covers (#126 — files
    // used to land in the covered row's directory, silently).
    let idx = tree.sticky_row_at(y).or_else(|| tree.node_at_y(y))?;
    let node = tree.nodes.get(idx)?;
    let dir_idx = if node.is_dir {
        idx
    } else {
        let parent = node.path.parent()?;
        tree.index_of_dir(parent)?
    };
    let dir_path = tree.nodes.get(dir_idx)?.path.clone();
    for src in drag_paths {
        if crate::widgets::file_tree::is_descendant_or_same(&dir_path, src) {
            return None;
        }
        if let Some(parent) = src.parent()
            && parent == dir_path
            && drag_paths.len() == 1
        {
            // Dropping a single source onto its own parent is a no-op;
            // don't show a highlight so the user knows nothing will move.
            return None;
        }
    }
    Some(dir_idx)
}

pub(super) fn drop_target_dir(
    tree: &crate::widgets::file_tree::FileTree,
    target_idx: usize,
) -> PathBuf {
    tree.nodes
        .get(target_idx)
        .map(|n| n.path.clone())
        .unwrap_or_else(|| tree.root.clone())
}

/// Parse a bracketed-paste payload that originated from a Finder-style
/// drag-and-drop into the host terminal. Returns the absolute existing
/// paths the user dropped, or an empty Vec when the payload is plain text
/// (in which case the caller falls back to the normal paste path). Each
/// candidate token survives only if it resolves to an existing absolute
/// path on disk, so a stray paste of "/usr/bin" worth of typed text still
/// behaves like text.
pub fn parse_dropped_paths(s: &str) -> Vec<PathBuf> {
    parsed_drop_tokens(s)
        .into_iter()
        .filter(|p| p.is_absolute() && p.exists())
        .filter(|p| !is_iterm2_inline_image_drop(p))
        .collect()
}

/// Parse a bracketed-paste payload as candidate filesystem paths WITHOUT
/// requiring the paths to exist on this machine. Used by the remote-
/// launched croft to recognise drops whose paths refer to files on the
/// user's local Mac, which the relay will fetch over scp.
pub fn parse_foreign_dropped_paths(s: &str) -> Vec<PathBuf> {
    parsed_drop_tokens(s)
        .into_iter()
        .filter(|p| p.is_absolute())
        .filter(|p| !is_iterm2_inline_image_drop(p))
        .collect()
}

/// True when every token in a bracketed-paste payload normalises to an absolute
/// path (and there is at least one). This is the signature of a genuine Finder
/// drag, which carries only the dragged file path(s). A real drag does NOT move
/// keyboard focus to the Explorer tree, so a focus-only gate silently dropped
/// every drag where the user had last clicked the terminal or editor. A text
/// paste that merely *contains* a path — a shell command like `cat /etc/hosts`,
/// a pasted code snippet — always has at least one non-path token, so it returns
/// false and is left to land as ordinary text rather than being hijacked into a
/// relay pull that has no local file to fetch.
pub(super) fn is_pure_path_payload(s: &str) -> bool {
    let trimmed = s.trim();
    if trimmed.is_empty() {
        return false;
    }
    let tokens = shell_split_drop_payload(trimmed);
    if tokens.is_empty() {
        return false;
    }
    tokens.iter().all(|t| {
        normalise_dropped_token(t)
            .map(|p| p.is_absolute())
            .unwrap_or(false)
    })
}

/// True when the dropped path is iTerm2's temp file for an inline-image
/// drag. iTerm2 lets the user drag any OSC-1337 inline image out of the
/// terminal; on mouse-up it writes the image bytes to `$TMPDIR/iTerm2.<rand>.png`
/// and re-injects the path into the same terminal as a bracketed paste.
/// Croft renders the activity-bar icons, welcome wordmark, run-debug
/// hero, no-repo hero, and SSH empty-state hero as OSC-1337 image cells,
/// so a click-with-microdrag on any of those triggers iTerm2's drag-
/// handler and croft would otherwise open the resulting PNG as an image
/// preview tab or import it into the Explorer. Filtering these paths is
/// always safe — the user has no legitimate reason to drop iTerm2's own
/// temp image into croft, and matching by filename pattern is iTerm2-
/// specific enough that it cannot collide with real user assets.
pub(super) fn is_iterm2_inline_image_drop(p: &Path) -> bool {
    let Some(name) = p.file_name().and_then(|n| n.to_str()) else {
        return false;
    };
    if !name.starts_with("iTerm2.") || !name.ends_with(".png") {
        return false;
    }
    let middle = &name["iTerm2.".len()..name.len() - ".png".len()];
    if middle.is_empty() || !middle.chars().all(|c| c.is_ascii_alphanumeric()) {
        return false;
    }
    // Defence in depth: only treat it as an iTerm2 drag artefact when
    // the file lives under a temp directory. macOS puts iTerm2's drag
    // temp under `/var/folders/.../TemporaryItems/` or `$TMPDIR`; both
    // resolve through `std::env::temp_dir()` ancestry. The starts_with
    // chain is forgiving because the path may be a symlinked variant
    // (e.g. `/private/var/...` vs `/var/...`).
    let parent = p.parent().unwrap_or(p);
    let parent_str = parent.to_string_lossy();
    parent_str.contains("/T/")
        || parent_str.contains("/TemporaryItems")
        || parent_str.starts_with("/tmp")
        || parent_str.starts_with("/var/folders")
        || parent_str.starts_with("/private/var/folders")
}

pub(super) fn parsed_drop_tokens(s: &str) -> Vec<PathBuf> {
    let trimmed = s.trim();
    if trimmed.is_empty() {
        return Vec::new();
    }
    shell_split_drop_payload(trimmed)
        .into_iter()
        .filter_map(|raw| normalise_dropped_token(&raw))
        .collect()
}

/// Split a Finder / iTerm2 drag-drop payload into individual path tokens
/// the way a POSIX-ish shell would. Backslash escapes the next char (so
/// `\ ` keeps a literal space inside a filename); single- and double-
/// quoted runs stay intact; unescaped whitespace (space, tab, CR, LF)
/// separates tokens. Quote/backslash characters are *retained* in the
/// emitted token so downstream `normalise_dropped_token` can reuse its
/// existing un-escape + un-quote logic.
pub(super) fn shell_split_drop_payload(s: &str) -> Vec<String> {
    let mut tokens: Vec<String> = Vec::new();
    let mut cur = String::new();
    let mut in_double = false;
    let mut in_single = false;
    let mut chars = s.chars().peekable();
    while let Some(c) = chars.next() {
        if c == '\\' && !in_single {
            // Carry the backslash and its escapee through verbatim;
            // `normalise_dropped_token` strips them later.
            cur.push('\\');
            if let Some(next) = chars.next() {
                cur.push(next);
            }
            continue;
        }
        if c == '"' && !in_single {
            in_double = !in_double;
            cur.push(c);
            continue;
        }
        if c == '\'' && !in_double {
            in_single = !in_single;
            cur.push(c);
            continue;
        }
        if (c == ' ' || c == '\t' || c == '\n' || c == '\r') && !in_single && !in_double {
            if !cur.is_empty() {
                tokens.push(std::mem::take(&mut cur));
            }
            continue;
        }
        cur.push(c);
    }
    if !cur.is_empty() {
        tokens.push(cur);
    }
    tokens
}

/// Strip surrounding quotes, decode `file://` URLs (with %xx escapes), and
/// unescape `\<space>` style backslash escapes that some shells / drag
/// sources produce. Anything that isn't a plausible filesystem path
/// returns None and is dropped by the caller.
pub(super) fn normalise_dropped_token(raw: &str) -> Option<PathBuf> {
    let mut s = raw.trim().to_string();
    if (s.starts_with('"') && s.ends_with('"') && s.len() >= 2)
        || (s.starts_with('\'') && s.ends_with('\'') && s.len() >= 2)
    {
        s = s[1..s.len() - 1].to_string();
    }
    if let Some(rest) = s.strip_prefix("file://") {
        // Decode into BYTES and build the string once. A `%xx` escape is one
        // byte of a UTF-8 sequence, not a character; pushing it `as char`
        // read it as Latin-1 and opened a path that does not exist (#414).
        // A sequence that is not valid UTF-8 names no path this process can
        // open, so per this function's contract the token is dropped rather
        // than turned into a plausible-looking path of replacement
        // characters, which would pass every caller's "is this a path"
        // screen and open the wrong thing.
        let decoded = crate::shell_integration::percent_decode(rest.as_bytes());
        s = String::from_utf8(decoded).ok()?;
    }
    let mut unescaped = String::with_capacity(s.len());
    let mut chars = s.chars();
    while let Some(c) = chars.next() {
        if c == '\\'
            && let Some(next) = chars.next()
        {
            unescaped.push(next);
            continue;
        }
        unescaped.push(c);
    }
    if unescaped.is_empty() {
        return None;
    }
    Some(PathBuf::from(unescaped))
}

/// Where `path` is after `renames` ran: under a moved file or folder, its
/// new place; anywhere else, unchanged.
pub(super) fn moved_path(path: &Path, renames: &[crate::lsp::manager::FileRenameOp]) -> PathBuf {
    renames
        .iter()
        .find_map(|r| path.strip_prefix(&r.old).ok().map(|rest| r.new.join(rest)))
        .map(|p| p.components().collect())
        .unwrap_or_else(|| path.to_path_buf())
}