htl-cli 0.17.0

htl command line (also `cargo htl`): check / run / test / fmt / build / pkg / new for Teal projects
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
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
//! `htl lsp`: a language server on stdio whose diagnostics are `htl check`'s (#500).
//!
//! An editor that runs `htl lsp` is told what `htl check` would say about the file it has
//! open — the project's resolution rules, its lints and their `-- htl: allow(..)` comments,
//! the `.d.tl` files `htl dts` writes — because the server does not reimplement any of it.
//! It builds the same things `htl check` builds for a project (the config, the
//! [model](htl::model), the lint [`Scope`](project::Scope), the checker
//! [`project::checker`] returns) and asks the same per-file question of them
//! ([`project::file_findings`], the part of [`project::check_one`] that decides what a file
//! reports). What is the server's own is the protocol: which file, when, and how a
//! [`htl::Diagnostic`] becomes an LSP one.
//!
//! # Shape
//!
//! One thread reads messages and owns every [`Htl`] state. mlua is built without its
//! `send` feature, so a state is `!Send` and has to stay on the thread that made it; a
//! synchronous loop over `lsp-server`'s channel (rust-analyzer's, which ruff and gleam use
//! as well) is the shape that needs no lock and no runtime. The alternative that was
//! weighed, `tower-lsp`, runs handlers concurrently and can apply ordered notifications out
//! of order, which is the wrong default for a server whose answer depends on the order of
//! saves.
//!
//! # What is checked, and when
//!
//! A file is checked when the client opens it and when it saves it, from disk, never from a
//! buffer, so what it reports is always about a text that exists as a file. The server does
//! take every change (`textDocumentSync.change` is `Full`) and keeps each open document's
//! buffer, but only to answer formatting, code actions, hover, definition and the outline about
//! the text the client holds; a `didChange` triggers no check. Diagnostics while typing need the checker to answer for
//! text that is not on disk and is often not a program, which is a later step.
//!
//! **Every check is a fresh one.** The check is [`Htl::check_written`], the call `htl fix`
//! measures a file by after writing it: a cold environment, nothing seeded from the
//! checker's store and nothing put into it. [`Htl::check`] would serve a module the checker
//! has already loaded, and a server that outlives the edit is exactly the caller that
//! answer is wrong for — it would describe the file as it was when the session started.
//! The price is that each check walks the file's whole `require` closure again; on a small
//! project that is milliseconds (the state itself, built once per project, is most of the
//! cost of a first check).
//!
//! **Dependents.** A check records the files the checked one reached through `require`
//! ([`htl::CheckInfo::deps`], the closure). When a file is saved, every other open file
//! whose recorded closure holds it is checked again as well, so a field renamed in a
//! required module is an error in the file that uses it without that file being saved. An
//! open or a close re-checks them too: an open file reports its own errors and a closed one
//! is reported through the files that require it, as in a walk of the open files.
//! Because each check is cold, the dependent sees the new text of the dependency without
//! any invalidation to get right. A module that did not exist when the dependent was last
//! checked is not in its closure, so creating it does not re-check the file that was
//! waiting for it; saving that file does.
//!
//! **Superseded work.** Messages are taken off the channel in batches: one blocking read,
//! then everything already queued. A batch is applied in order to the set of open files and
//! produces one set of files to check, each once, so three saves of one file that arrived
//! while a check was running become one check of what is on disk now. "Already queued"
//! needs a queue: `lsp-server`'s stdio reader hands messages over a zero-capacity channel,
//! so its reader would sit on the second message and leave the rest in the pipe, and every
//! save would be a check of its own. A thread moves them into an unbounded channel as they
//! are read, which is what the batch is drained from. A `shutdown` in a batch is answered
//! after the checks the messages before it asked for, so a client that waits for the
//! answer has had every publish those messages lead to.
//! `$/cancelRequest` is accepted and does nothing: a check is triggered by a notification,
//! which has no response to cancel, and the requests it answers (formatting, code actions)
//! are answered on this one thread, so by the time a cancellation is read the request it
//! names has been answered or is the next message.
//!
//! # What a diagnostic becomes
//!
//! - `message` is [`htl::Diagnostic::message`], the sentence `htl check` prints, and `code` is
//!   its [`rule`](htl::Diagnostic::rule) (`nil-index`, `tl:unused`, `tl:error`, ...), the name
//!   `-- htl: allow(..)` and `htl check --explain` take. `source` is `htl`.
//! - The range starts where `htl check` says (`line` and `col`, 1-based, `col` counted in
//!   bytes) converted to LSP's 0-based line and UTF-16 column. The checker reports a point;
//!   the range ends after the identifier that starts there, or after one character if what
//!   is there is not part of one, so that a client has something to underline.
//! - Severity: an `error` is an error; a Teal `warning` and an htl `lint` are warnings,
//!   except that a finding under a rule the project sets to `deny`, or any finding of a
//!   project with `[lint] strict`, is an error, because that is what makes `htl check` fail
//!   on it ([`htl::verdict::verdict`]).
//! - A finding about another file — an error in a module the file requires, which `htl
//!   check` reports against that module — is shown on the first line of the file that
//!   requires it, with the dependency's own position in `relatedInformation`; the
//!   dependency reports itself when it is open. A finding with no position is on the
//!   first line too.
//! - A file that is not on disk (a buffer opened before its first save, a file deleted
//!   while open) has nothing to report until it is saved, and is published empty.
//!
//! # Formatting and code actions
//!
//! A client formats on save, or runs its fixes on save, by asking *before* it writes: the
//! buffer is then not the file, and an edit computed from the file would put the file's text
//! back over everything not yet saved. So the server keeps each open document's buffer
//! (the `didOpen` text, then the text of every full-sync `didChange`) and answers about
//! that. A document the client never opened (a save is all the server heard of it) has its
//! disk for a buffer. The version a request or a change carries is not read: a change is
//! stored as it arrives and the last one is the buffer.
//!
//! Every request is answered about the files as the messages before it left them: a
//! request is preceded by the checks those messages made stale, as a `shutdown` is. (That
//! is true of the requests below; any other is refused as unknown.)
//!
//! Every edit is one `TextEdit` over the span in which the buffer and the new text differ
//! (the text they share at both ends is left out, so the cursor, folds and marks outside it
//! survive); applied, it gives the new text exactly.
//!
//! **Formatting.** `textDocument/formatting` returns the edits that make the file what
//! `htl fmt` writes for the buffer's text ([`Htl::format_text`]), with the indent `htl fmt`
//! resolves for it (`[fmt] indent` of the file's project, else 3; the request's `options`
//! are not read). The answer is `[]` when the buffer is already formatted. A file `htl fmt` cannot format (a parse
//! error) is answered `null`, not an error response: most clients show an error response
//! as a failed command, and a file that does not parse is already a diagnostic. The reason
//! goes to stderr. The file does not have to be open (its disk is the buffer then). `textDocument/rangeFormatting` is
//! not offered: `htl fmt` formats files.
//!
//! **Quick fixes.** A check keeps, with each diagnostic it publishes, the
//! [`htl::Fix`] the checker attached to it, and a `textDocument/codeAction` request is
//! answered from those: nothing is checked again. For every finding whose range touches
//! the request's range and that has a fix, there is a `quickfix` action titled `Fix <rule>`
//! (` (unsafe)` after it for an unsafe fix; `htl fix` prints no description of a fix, so
//! the rule is all there is to name), whose `diagnostics` is that finding and whose edit
//! is the text [`htl::fix::apply_fix`] makes of the buffer with that fix, the code `htl fix`
//! applies its edits with, as a span of the buffer. The action is tied to its diagnostic by the server's own record of
//! which finding it came from, so nothing is matched on range, code and message and
//! `Diagnostic.data` is left empty. `isPreferred` is set for a fix `htl fix` applies without
//! being asked ([`htl::fix::FixOptions::disposition`] is that question, asked of the
//! project's `[fix]`: a `safe` fix, or one `[fix] unsafe` promotes, of a rule `[fix]
//! disable` does not name). An unsafe fix and a disabled one are offered, not preferred (the person
//! asked for it; nothing takes it unasked). A `suggest` fix is not offered: what it writes
//! is a placeholder for the author to replace (`htl_fixme(..)`), which `htl fix` shows
//! and never applies. Only the file's own findings carry a fix: the edit of a finding
//! about a required module is a position in that module.
//!
//! **Fix all.** The action of kind `source.fixAll.htl` (so a client asking for
//! `source.fixAll` gets it) is one edit to what `htl fix <file>` leaves the file as: safe
//! fixes, with the project's `[fix] unsafe` and `[fix] disable`, to a fixpoint of up to
//! [`htl::fix::MAX_PASSES`] passes, reverting what made the file worse. It is computed by
//! [`htl::fix::fix_file`] as a dry run (`htl fix --dry-run`, minus the git guard, which
//! protects a file that is about to be written), in a checker state of its own, because
//! `fix_file` begins with a check that reads the state's store and the shared checker's
//! store must not hold anything (see "Every check is a fresh one"). That costs a check or
//! two of the file, so it is computed only for a request that asks for it (`only` names
//! `source.fixAll`, or the menu was invoked rather than opened by a cursor move), only
//! when a fix `htl fix` applies is on file, and once per check: the answer is kept until
//! the next one. It is offered only when it changes the file.
//!
//! **Only for the text that was checked.** A fix is positions in the text a check read.
//! Quick fixes and fix-all are offered only while the buffer is that text (and, for
//! fix-all, which `fix_file` reads from disk, the disk too); a buffer that has been edited
//! since the last save gets none, because the positions belong to another text. That
//! includes the request a client makes on save before it writes: formatting works on the
//! buffer and is the part that can be done then; fixes follow once the save has been
//! checked. A `[fix]` section `htl fix` would refuse (a rule that is none) is published
//! with the file's diagnostics as the setup error it is, and no fix is offered while it
//! stands.
//!
//! # Hover and definition
//!
//! Both are read off the check that was made of the file, as quick fixes are: the check is
//! [`Htl::check_indexed`], a [`Htl::check_written`] that also copies out of the checker's type
//! report what an editor asks about a position ([`htl::typeindex`]; its module doc says what
//! a question is and what the report cannot answer). The copy is made once per check and no
//! check is made for a request. It is one pass over the report's positions, small beside
//! the check it belongs to (`HTL_PROFILE=1` prints it as `type_index`, beside `check`),
//! which is why it is made on every check and not on the first request: a request would
//! otherwise be a check of the file, which is what the server avoids by checking on open
//! and on save.
//!
//! **Hover** is the type of the name under the cursor, as Teal writes it
//! (`function(string): sample.Greeting`), as a `teal` code block, over the range of the
//! name. The cursor may be anywhere in the name or just after it. `null`, not an error, for
//! anything that is not a name — whitespace, a bracket, a keyword, a number, the inside of a
//! comment or of a string — and for a name the checker has no type for.
//!
//! **Definition** is, for the string of a literal `require("m")` (the quotes, and just after
//! the closing one), the file htl's resolution picked for it — the same
//! [`RequireSite::path`](htl::RequireSite::path) `htl resolve m` explains — at its first
//! character. For a name, it is the declaration the report names: a local, a parameter or a
//! local function or type in scope at the cursor, or, for a function reached through a field
//! (`m.hello`), the function in the file that declares it. What the report cannot name is
//! answered `null`: a field of a primitive type (`g.text`, which Teal shares one `string` for),
//! a name of the standard library (its declarations are in a file Teal carries inside itself,
//! not one on disk), a name the report has nothing for. The location is a point: the line and
//! column Teal records (the name for a variable or a parameter, the start of the statement for a
//! function or a record), converted to LSP's UTF-16 column against the declaring file as it is
//! on disk now. On the name a header declares (`greet` of `function sample.greet`) it is the
//! declaration itself, at its `function`.
//!
//! **Only for the text that was checked.** Both are positions in the text the check read, so
//! they are answered only while the client's buffer is that text; a buffer that has been
//! edited since the last save gets `null` from both, not an answer about a text it no
//! longer holds. Both are answered again after the next save is checked, or if the edit is
//! undone. (Diagnostics while typing, which would let them follow the buffer, are a later
//! step.)
//!
//! # Outline
//!
//! `textDocument/documentSymbol` lists what a file declares, as a tree of `DocumentSymbol`s,
//! from the same index ([`htl::typeindex::TypeIndex::declarations`]): the declarations of the
//! file's top level, and for a record its fields, the types nested in it and the functions
//! declared on it anywhere in the file. A module record's `function M.f` is written after the
//! record's `end` and is its child all the same, so a child's range may lie outside its
//! parent's. The kinds: a record is a `Struct`, an interface an `Interface`, an enum an `Enum`,
//! a type alias a `TypeParameter`, a variable a `Variable` (a `Function` if it holds one), a
//! field a `Field`; `function R.f` and a field typed as a function are `Function`s and
//! `function R:m` a `Method`. `detail` is the type as Teal writes it. `range` runs from the
//! first token of the declaration to its `end` where the checker records one and otherwise
//! to the end of the line the declaration starts on (the index's module doc lists which is
//! which), and `selectionRange` is the name. Columns are UTF-16 units.
//!
//! It is answered about the text that was checked, like hover and definition: `null` for a
//! buffer that has been edited since, and also `null`, never `[]`, for a file the server
//! cannot say anything about. That is a file the client has not opened (nothing has been
//! checked for it; the server does not check one for a request), one not on disk, and one
//! that does not parse (the checker keeps no tree for it). `[]` is what a file that parses
//! and declares nothing gets. An open file whose check found type errors still has its
//! outline, as it still has hover.
//!
//! # Workspace symbols
//!
//! The workspace-symbol request searches the declarations of the whole
//! project by name, so a client finds `greet` without knowing which file declares it. The
//! answer is a flat list of `SymbolInformation`: the name (`R.f` for a record function whose
//! record is declared in another file), the kind (the outline's), the file and the range of
//! the name, and as `containerName` the record a field or a function is declared on.
//!
//! **What is indexed.** The `.tl` files `htl check <root>` walks for the project: the
//! project's own sources, its tests and the modules it patches, not an installed or vendored
//! dependency, and no file that no module of the project holds (the walk is
//! [`Project::source_files`], made of the calls `htl check` makes, so the two cannot
//! disagree). For each file, the tree of declarations the type index keeps for an open file
//! ([`htl::typeindex::TypeIndex::declarations`]), flattened. A file that is not open has no
//! check, and is not checked for this: [`Htl::declarations_of`] parses it and walks its
//! top-level statements, which costs a parse of the file and nothing of what it requires
//! (no type check, no `require` followed). What that
//! leaves out is the type strings, which this answer has no field for, and one kind: a
//! variable that holds a function it did not write (`local f = m.hello`) is a `Variable`
//! until a check says it is a function. A file that does not parse has no declarations,
//! and is not an error: its syntax error is a diagnostic of its own. A file with a type
//! error is listed.
//!
//! **Which projects.** Those of the open files, so a request about a file of one project is
//! not an answer about another; when no file is open, the projects of the workspace folders
//! the client named at `initialize`. A directory that is no project (a file opened outside any
//! `htl.toml`) has its open files and nothing else, for there is no walk to make. The index
//! of a project is built at the first request that asks about it, not before, so a session
//! that never searches pays nothing.
//!
//! **Kept current.** Each file's entry holds the stamp (modification time and length) of the
//! file and a hash of the text its symbols were read from; an entry is replaced only when the
//! text moved, so a refresh parses the files that changed and no others.
//!
//! - A check of an open file (open, save, or a dependency's save) puts the declarations of
//!   that check in as the file's entry: fresher than a read of the disk, and kinds the types
//!   decide. A file whose last check read the text now on disk gets its entry from that check
//!   when the index is built, too.
//! - A client that watches files (the registration was accepted) says when a `.tl` under the
//!   project is made, changed or deleted (`**/*.tl` is in the registration). A file the
//!   index holds is read again at the next request, or dropped if it is gone; a file it does
//!   not hold makes the next request walk the project again. A request answered between
//!   events reads no file, and stats only the files its hits are in: a watcher names files,
//!   so a directory deleted or renamed is one event the glob does not match, and a hit in a
//!   file that is gone makes the project walked again and the search made once more.
//! - A client that does not (it never said it could, or refused) is not told, and the index
//!   cannot know: before each answer the project is walked again and every file stamped, and a
//!   file whose stamp moved, one that appeared and one that went are put right. That is a
//!   directory walk and a `stat` per file on every request, however large the project, and
//!   nothing is read or parsed that did not change: the price of an answer that is not stale.
//!   A stamp is not proof of the text: one write can land in the same tick of the file
//!   system's clock as the one before and leave the same length, so a file written in the
//!   last two seconds is read and compared with the text its entry came from, however its
//!   stamp looks (git's index treats such "racily clean" entries alike). A directory that is
//!   no project is listed this way whether or not the client watches, since which files are
//!   open is what lists it and no watcher says that.
//! - A project that is dropped because a manifest changed ("Files that change the answer")
//!   takes its index with it, and the next request builds it again. One dropped for a
//!   declaration under `types/` or a Rust source keeps it: what a file declares is its
//!   syntax, which neither changes.
//!
//! The index is of what is on disk, or what the last check read. The text of an unsaved
//! buffer is not in it, as it is in no diagnostic.
//!
//! **The bound.** At most [`symbols::MAX_RESULTS`] symbols are answered (1000), because the
//! protocol has no paging for this request. The order is by how well the name matches, the
//! query being a case-insensitive substring: the name itself first (`greet`, `GREET`), then
//! names that start with the query, then names that hold it; inside each by path, line and
//! column, so the same query on the same files is the same answer. A project with more
//! matches than the bound gets the best of them and a line on stderr says how many there
//! were; a client that wants one of the rest narrows the query. An empty query matches
//! everything and so lists the first thousand by path. The match is on the name as written
//! (`f` of an `R.f`, and the whole `R.f` too).
//!
//! **`null`, not `[]`.** `[]` is "an index was built and nothing in it matches". `null` is
//! "there is no index to ask": no file is open and the workspace folders are in no project,
//! or the project cannot be loaded, or `htl check` would refuse to walk it (a flat layout
//! without `[layout] source = "."`). The reason is on stderr. An empty answer a client reads
//! as "there is none" is the failure this distinction is for.
//!
//! # References
//!
//! `textDocument/references` lists every use of the declaration the name at the cursor
//! stands for, in all the files of the project, so a client that changes a function finds
//! its callers without searching text (which finds every word `greet`, a local of that name
//! and a field of another record among them).
//!
//! **Which declaration.** The one the type index resolves the position to
//! ([`htl::typeindex::TypeIndex::at`], what go-to-definition answers): from a use, the
//! declaration it names, and from the name a declaration writes (`greet` of `function
//! sample.greet`, a local's name), that declaration itself. A local's references are its
//! uses in its scope, so a local of the same name in another function is not among them.
//! What the answer lists is each use as the range of the name, in the file it is in; with
//! `includeDeclaration` the declaration's own name is kept, and without it left out. A
//! function a record's body declares (`f: function(..)`) and the module defines below is one
//! declaration with two names, and both are kept or left out together; the request may be made
//! at either. (A declaration whose name the check does not resolve is added, as the point the
//! checker names it by, when `includeDeclaration` asks for it.)
//!
//! **What is indexed.** The files of the symbol index (see "Workspace symbols": the walk of
//! `htl check <root>`, one list for both), and for each the uses
//! [`htl::typeindex::TypeIndex::uses`] reads out of its check: every name that resolves to a
//! declaration, and whether it is called there, which a call hierarchy is built on. Only a
//! check can make them; a parse cannot say what `m.f` is. They are made at the first
//! references request that asks about the project, and not before, so a session that never
//! asks pays nothing. An open file contributes its last check, when that check read the text
//! now on disk and nothing it read has changed since. Every other file is checked as it is on
//! disk by that request, once, and kept: the first request is a check of every file that is
//! not open (each check walks the file's `require` closure, as every check here does), and
//! the ones after it are a lookup, plus what keeping current costs. The first request does not
//! trust any open file's last check, which may be older than a change the index was not there
//! to see: the open files are checked again for it, and the file the request is made in is
//! checked before the position is resolved (a publish follows, as after any check).
//!
//! **Kept current.** By the symbol index's rules (checks of open files, watched `*.tl`
//! events, a stat of each file for a client that does not watch, the stamp that is not trusted
//! within two seconds of a write, a project dropped when a manifest changes). What the uses
//! add is that they say where a name declared in *another* file is, so they go stale when
//! that file changes and not only when their own does:
//!
//! - a file whose text changed takes with it the uses of every file whose check read it
//!   (the closure its check recorded), which are made again at the next request;
//! - a file appearing or going takes all of them: a new file can change what a `require` finds
//!   in a file that did not read it;
//! - a change to a `types/` declaration or a Rust source (which the symbol index survives)
//!   takes all of them. A client that watches says so; one that does not is not told, so the
//!   request fingerprints the files `htl dts` and the model read (every `.d.tl` under a
//!   `types` directory, every `Cargo.toml` and every `.rs` under `src`, `examples`, `tests` or
//!   `benches`, by path, modification time and length) and a change is treated as the
//!   watcher's event is: the project is built again, its open files checked and the uses
//!   made again. That is a second walk of the project directory (the `target`, `.git` and
//!   other skipped directories are not entered) and a `stat` per such file on every request,
//!   beside the walk and the `stat` per `.tl` file the symbol index already costs a client
//!   that does not watch;
//! - a file whose uses were taken for one of these reasons is not made again from the last
//!   check of the open file, which read the files as they were: it is checked again. That
//!   includes the file the request is made in, which is checked first, so that the position
//!   is resolved against the files as they are (the request is then preceded by a publish for
//!   it, as after any check).
//!
//! The index is of what is on disk; an unsaved buffer's text is not in it, as it is in no
//! diagnostic. The request itself is answered only while the buffer of the file it is made
//! in is the text the check read, like hover and definition.
//!
//! **`null`, not `[]`.** `[]` is "the position is a declaration and nothing uses it" (and
//! `includeDeclaration` is false). `null` is "no answer": the position is on no name, the
//! name has no declaration the report names, the declaration is in the standard library (a file
//! Teal carries inside itself, as for definition), the buffer is not the checked text, the file
//! was never checked, the file is not one the walk visits (it is in no module of the
//! project, so none of its uses is indexed and "no uses" would be a guess), or the project's
//! files cannot be listed (the reason is on stderr).
//!
//! **What cannot be resolved.** A use is listed only if the check resolved it, so what is
//! missing is what the checker does not know:
//!
//! - a name whose type is `any` or is not known (a `require` that failed, an unannotated
//!   parameter): `x.f` for such an `x` is no use of anything;
//! - a field of a primitive type (`g.text`: Teal shares one `string` type among all of them,
//!   so a field has no place) and a table's dynamic key (`t[name]`);
//! - the key of a table constructor (`who` of `{ who = who }`) is a field's name, not the
//!   local of that name, and is left out of that local's references, though it is not listed
//!   as a use of the record's field either;
//! - a call through an alias: `local g = m.greet` is a use of `greet` and `g(..)` is a use of
//!   the local `g`;
//! - the standard library, whose declarations have no file to answer with;
//! - a file that does not parse, which has no index and so no uses.
//!
//! # Call hierarchy
//!
//! `textDocument/prepareCallHierarchy`, `callHierarchy/incomingCalls` and
//! `callHierarchy/outgoingCalls` answer "who calls this" and "what does this call" from the
//! reference index, which is built, kept current and limited as "References" says: there is
//! no index of its own, and what a request finds is what a references request would. The
//! uses it reads are the ones that are calls (`f(`, `o:f(`, `f "s"`, `f {`), and the
//! declarations are the tree of the file's last check, so a variable that holds a function
//! (`local g = m.greet`) is a function here as in the outline. The code is in `calls.rs`.
//!
//! **Preparing.** The position is resolved as references resolve it, from a use or from the
//! name a declaration writes, to the declaration the type index names. The answer is one
//! item when that declaration is a function the tree of the file it is in holds: `local
//! function`, `global function`, `function R.f`, `function R:m`, a record's field typed as a
//! function and a variable that holds one, at the top level of its file. A function declared
//! in a record's body and defined below is one item whichever name the request starts from.
//! The item's `name` and `kind` (`Function`, or `Method` for `R:m`) are the outline's, its
//! `detail` is the type as Teal writes it, `range` is the declaration from its first token to
//! its end and `selectionRange` is its name. The answer is `null` for a position on no name,
//! on a name with no declaration the check names, on one that is not a function (a local
//! that holds a number, a field that is not a function) and on one the tree does not hold: a
//! parameter, a function written inside another function (the tree does not enter bodies),
//! the standard library's. The buffer must be the text the check read, as for references.
//!
//! **The item.** Its `data` is what finds the declaration again: the two places the checker
//! names it by and its name (`{"at": [[line, col], [line, col]], "name": ".."}`, lines and byte
//! columns from 1); the file is the item's URI. The two requests that follow take the item as
//! the client got it and look the declaration up in the file's uses: if the file changed so
//! that no function of that name is declared at those places, or the file is gone, or is not
//! one the walk visits, or the `data` is not one this server made, the answer is `null` (and
//! the reason is on stderr). A file the client has open is answered about only while its
//! buffer is the text the check read, as everywhere else, since the item's ranges are
//! positions in that text. `[]` is the answer for a function nothing calls (or that calls
//! nothing).
//!
//! **Incoming calls** are the uses of the item's declaration that are calls, in every file of
//! the project, whether or not it is open, grouped by the function each call is inside of:
//! the innermost function of the file's tree whose code holds the call
//! ([`htl::typeindex::Decl::fn_body`]): a function's whole extent, and for a variable only
//! the function literal written for that name (the k-th expression of `local f, g =
//! function() .. end, function() .. end`). A variable that merely holds a function, `local
//! log = logger.make("x")`, has no code of its own: the call to `make` is at the top level,
//! and `log` has no outgoing calls. A function's `from`
//! item is its own, and `fromRanges` are the call sites in that file. The tree does not
//! enter a function's body, so a closure or a local function inside a function is part of
//! it: a call inside either is the enclosing function's. A call at the top level of a file,
//! in no function (the `local g = m.greet(..)` of `main.tl`, a call inside the function
//! literal a test passes to `t.it`), is grouped under an item for the file's chunk: kind
//! `File`, named by the file's path from the project's root, whose range is the whole file.
//! The groups are in the order of the files (by path), the chunk of a file before its
//! functions, so the same request is the same answer. A function that calls itself is among
//! its own callers.
//!
//! **Outgoing calls** are the calls inside the item's function (its own file's uses, those
//! whose innermost function it is), grouped by the function each calls, whose `to` item is
//! that function's and `fromRanges` the call sites, in the item's file. Left out: a call
//! whose name the check does not resolve (see "What cannot be resolved" in References), a
//! call of the standard library, of a parameter or of a local the tree does not hold
//! (including a local function inside the function), of a variable that is not typed as a
//! function, and of a function declared in a file the walk does not visit (a dependency's).
//! The item of a file's chunk, which an incoming answer may carry, has outgoing calls too
//! (the calls at the top level of the file) and no incoming ones.
//!
//! **What cannot be resolved.** A call through a value whose type the check does not know
//! is no call of anything, and a call through an alias is a call of the alias: `local g =
//! m.greet` is a function `g`, and `g(..)` is among `g`'s incoming calls and not `greet`'s.
//! `self:n()` in a method of the record `R` is a call of `R:n` where the check knows `self`
//! as an `R`, which it does for a `function R:m` written in the file.
//!
//! # Files that change the answer
//!
//! A file's diagnostics depend on more than the file: the project's `htl.toml` (lint levels,
//! the resolution rules, `[fix]`, `[fmt]`), its `mlua-pkg.toml` and `mlua-pkg.lock` (which
//! modules a `require` can reach), the declarations under `types/`, and the Rust that `htl
//! dts` writes declarations from. A [`Project`] is built from those once, so a change to any
//! of them has to drop it, and the open files of the project have to be checked again —
//! without being saved, since it is not they that changed. There are two ways the server
//! learns of it, and either alone is enough for the manifests.
//!
//! **The client says so** (the `didChangeWatchedFiles` notification). If the client's `initialize`
//! says it takes a dynamic registration for it, the server sends one
//! `client/registerCapability` request after the handshake, with these globs ([`WATCHED`]):
//! `**/htl.toml`, `**/mlua-pkg.toml`, `**/mlua-pkg.lock`, `**/types/**/*.d.tl`,
//! `**/*.tl` (the symbol index's, see "Workspace symbols"), `**/Cargo.toml` and
//! `**/{src,examples,tests,benches}/**/*.rs`. The last two are what
//! `htl dts` reads ([`htl::dts::generate_crate_to`] walks those four directories under the
//! crate's `Cargo.toml`), and nothing else is registered, so a client does not watch every
//! `.rs` of a workspace (nor the `target/` that holds most of them). A change to a file that
//! `htl dts` would not read is dropped on arrival ([`Watched::reads_rust`]), for a client that
//! cannot narrow its globs. Which projects a file reaches:
//!
//! - a manifest: the projects whose root is under its directory or above it, and every open
//!   file under that directory, which has its project looked up again (a new `htl.toml` in a
//!   subdirectory makes a project of the files under it);
//! - a `.d.tl` under a `types` directory: the projects it is in (the model scans `types/` when
//!   it is built, so a declaration that appears or goes is as much a change as one that is
//!   edited; a project whose `[layout] types` is elsewhere is reached by the fallback below
//!   if at all);
//! - Rust: the projects whose crate `htl dts` reads that file for. They are rebuilt, which
//!   runs `htl dts` again, and a `.d.tl` it writes is another event and another rebuild that
//!   writes nothing (it writes a file only when it differs), so it settles at once.
//!
//! The events of a batch drop projects and nothing else, so a hundred events for one project
//! (a branch switch) are one rebuild, made by the checks the batch leads to; the open files of
//! the project are each checked once.
//!
//! **The server looks** (the fallback, and what makes this work on a client that does not
//! watch files, or refuses the registration — which is said on stderr). Before it checks
//! the files of a project, and before it formats one, it reads the project's `htl.toml`,
//! `mlua-pkg.toml` and `mlua-pkg.lock` and compares them with what the project was built
//! from ([`Manifests`]); if any differs, the project is rebuilt, and every open file of it is
//! checked, not only the one that asked. The cost is reading three small files per check (a
//! few kilobytes, microseconds beside a check). It sees a manifest at the next open or save
//! of any file of the project, not at the moment it is edited: a client that does not watch
//! files has nothing to say when it changes. It does not see `types/` or Rust, which are not
//! the project's own files at its root; a client that does not watch files has those at the
//! next time the project is rebuilt for some other reason (a references request is the
//! exception: it looks at them, see "References").
//!
//! # What it does not do yet
//!
//! The findings no single file carries (a require cycle, a contract no host enforces) are not
//! reported: they need the whole walk, and the scope here is the open files and what they
//! require. A `---@contract` type, which `htl dts` publishes as a declaration when the project
//! is built, is published again only when it is: a save of the `.tl` that declares it does not
//! rebuild the project.

mod calls;
mod refs;
mod symbols;

use super::{auto_dts, load_config, warn_cargo_htl_mismatch};
use anyhow::{Context, Result};
use htl::Htl;
use htl::project::{self, Collect, Sink};
use lsp_server::{Connection, ErrorCode, Message, Notification, RequestId, Response};
use lsp_types::notification::{
    Cancel, DidChangeTextDocument, DidChangeWatchedFiles, DidCloseTextDocument,
    DidOpenTextDocument, DidSaveTextDocument, Exit, Notification as _, PublishDiagnostics,
};
use lsp_types::request::{
    CallHierarchyIncomingCalls, CallHierarchyOutgoingCalls, CallHierarchyPrepare,
    CodeActionRequest, DocumentSymbolRequest, Formatting, GotoDefinition, HoverRequest, References,
    RegisterCapability, Request as _, Shutdown, WorkspaceSymbolRequest,
};
use lsp_types::{
    CallHierarchyIncomingCallsParams, CallHierarchyItem, CallHierarchyOutgoingCallsParams,
    CallHierarchyPrepareParams, CallHierarchyServerCapability, CodeAction, CodeActionKind,
    CodeActionOptions, CodeActionOrCommand, CodeActionParams, CodeActionProviderCapability,
    CodeActionTriggerKind, DiagnosticRelatedInformation, DiagnosticSeverity,
    DidChangeTextDocumentParams, DidChangeWatchedFilesParams,
    DidChangeWatchedFilesRegistrationOptions, DidCloseTextDocumentParams,
    DidOpenTextDocumentParams, DidSaveTextDocumentParams, DocumentFormattingParams, DocumentSymbol,
    DocumentSymbolParams, DocumentSymbolResponse, FileSystemWatcher, GlobPattern,
    GotoDefinitionParams, GotoDefinitionResponse, Hover, HoverContents, HoverParams,
    HoverProviderCapability, Location, MarkupContent, MarkupKind, NumberOrString, OneOf, Position,
    PublishDiagnosticsParams, Range, ReferenceParams, Registration, RegistrationParams,
    SaveOptions, ServerCapabilities, SymbolInformation, TextDocumentSyncCapability,
    TextDocumentSyncKind, TextDocumentSyncOptions, TextDocumentSyncSaveOptions, TextEdit, Uri,
    WorkspaceEdit, WorkspaceSymbolParams, WorkspaceSymbolResponse,
};
use std::collections::{HashMap, HashSet};
use std::hash::{DefaultHasher, Hash, Hasher};
use std::path::{Path, PathBuf};
use std::process::ExitCode;
use std::str::FromStr;
use symbols::{Entry, Stamp, SymbolIndex};

/// Run the server on stdio until the client says `exit` (or goes away).
///
/// The exit code is the one the protocol asks for: 0 after a `shutdown` request, 1 when the
/// client exits without one.
pub fn serve() -> Result<ExitCode> {
    let (stdio, io_threads) = Connection::stdio();
    // The stdio reader hands each message over a zero-capacity channel and reads the next
    // only once it is taken, so behind a running check there is never more than one
    // message queued. Moved into an unbounded channel as they are read, the ones that
    // arrive during a check are all there for the next batch (module doc, "Superseded
    // work"). The thread ends with the reader, at `exit` or the end of stdin.
    let (tx, receiver) = crossbeam_channel::unbounded();
    let source = stdio.receiver;
    std::thread::spawn(move || {
        for message in source {
            if tx.send(message).is_err() {
                break;
            }
        }
    });
    let connection = Connection {
        sender: stdio.sender,
        receiver,
    };
    let code = run_on(&connection)?;
    // The writer thread ends when the last sender is gone.
    drop(connection);
    io_threads.join().context("closing stdio")?;
    Ok(code)
}

/// The handshake, then the loop, over any [`Connection`]: stdio for the server, an
/// in-memory pair for a test.
fn run_on(connection: &Connection) -> Result<ExitCode> {
    let (id, params) = connection
        .initialize_start()
        .context("waiting for the client's initialize request")?;
    // A client that can take a registration at run time says so; one that cannot is not
    // sent a request it would answer with an error (module doc, "Files that change the
    // answer").
    let dynamic_watch = params
        .pointer("/capabilities/workspace/didChangeWatchedFiles/dynamicRegistration")
        .and_then(serde_json::Value::as_bool)
        == Some(true);
    let capabilities = ServerCapabilities {
        // Open, close and save to know when to check; the text of every change to answer
        // formatting about the buffer. A change is stored and not checked (module doc).
        text_document_sync: Some(TextDocumentSyncCapability::Options(
            TextDocumentSyncOptions {
                open_close: Some(true),
                change: Some(TextDocumentSyncKind::FULL),
                save: Some(TextDocumentSyncSaveOptions::SaveOptions(SaveOptions {
                    include_text: Some(false),
                })),
                will_save: None,
                will_save_wait_until: None,
            },
        )),
        document_formatting_provider: Some(OneOf::Left(true)),
        hover_provider: Some(HoverProviderCapability::Simple(true)),
        definition_provider: Some(OneOf::Left(true)),
        document_symbol_provider: Some(OneOf::Left(true)),
        workspace_symbol_provider: Some(OneOf::Left(true)),
        references_provider: Some(OneOf::Left(true)),
        call_hierarchy_provider: Some(CallHierarchyServerCapability::Simple(true)),
        code_action_provider: Some(CodeActionProviderCapability::Options(CodeActionOptions {
            code_action_kinds: Some(vec![CodeActionKind::QUICKFIX, fix_all_kind()]),
            work_done_progress_options: Default::default(),
            resolve_provider: Some(false),
        })),
        ..ServerCapabilities::default()
    };
    connection
        .initialize_finish(
            id,
            serde_json::json!({
                "capabilities": capabilities,
                "serverInfo": { "name": "htl", "version": env!("CARGO_PKG_VERSION") },
            }),
        )
        .context("finishing the handshake")?;
    Server::new(connection, dynamic_watch, workspace_of(&params)).run()
}

/// What the server knows about a project, built when its first file is opened.
///
/// The things `cmd_check` builds once per run, held for the session instead.
struct Project {
    cfg: project::Config,
    model: Option<htl::model::Project>,
    scope: project::Scope,
    origins: project::Origins,
    /// How `htl check` judges the project's findings: `[lint] strict` makes every one of
    /// them fail the run, so every one of them is an error here.
    policy: htl::verdict::Policy,
    /// The state every check of the project runs in. Built once ([`project::checker`]
    /// costs about as much as a small project's whole check) and never asked to
    /// remember anything: checks go through [`Htl::check_written`].
    checker: Htl,
    /// What `htl fix` with no flags would do in this project: safe fixes only, the rules
    /// `[fix] unsafe` promotes counted as safe, the ones `[fix] disable` names left alone.
    /// A dry run, since the server never writes.
    fix: htl::fix::FixOptions,
    /// Why `fix` cannot be trusted ([`htl::fix::FixOptions::validate`]: `htl fix` refuses
    /// such a configuration outright), said with every check of the project. While it is
    /// set no fix is offered: which fixes `htl fix` would apply is not a question with an
    /// answer.
    fix_error: Option<String>,
    /// The manifest files as they were when the rest was built from them, to tell whether
    /// they still are (module doc, "Files that change the answer").
    manifests: Manifests,
}

/// The files at a project's root that decide what a [`Project`] is, and a hash of each as
/// it was read: `htl.toml` (lint levels, resolution, `[fix]`, `[fmt]`, the toolchain pin),
/// `mlua-pkg.toml` (the dependencies and so the modules a name resolves to) and
/// `mlua-pkg.lock` (which version of them).
///
/// Compared by content and not by modification time: they are a few kilobytes, reading
/// three of them is what a check spends nothing on, and a content hash has no clock
/// granularity to be wrong about (an edit that keeps the size and lands in the same tick of
/// a coarse file system is still seen).
struct Manifests {
    seen: Vec<(PathBuf, Option<u64>)>,
}

impl Manifests {
    /// The names, as the other crates spell them.
    fn names() -> [&'static str; 3] {
        [
            htl::config::CONFIG_NAME,
            htl::pkg::MANIFEST_NAME,
            htl::pkg::LOCKFILE_NAME,
        ]
    }

    fn read(root: &Path) -> Self {
        let seen = Self::names()
            .iter()
            .map(|n| {
                let p = root.join(n);
                let h = hash_of(&p);
                (p, h)
            })
            .collect();
        Self { seen }
    }

    /// Whether any of them reads differently now (or has appeared or gone).
    fn changed(&self) -> bool {
        self.seen.iter().any(|(p, h)| hash_of(p) != *h)
    }
}

/// A hash of the file's bytes; `None` when it cannot be read (no such file).
fn hash_of(path: &Path) -> Option<u64> {
    let bytes = std::fs::read(path).ok()?;
    let mut h = DefaultHasher::new();
    bytes.hash(&mut h);
    Some(h.finish())
}

impl Project {
    /// The project `file` belongs to (`root` is its key), set up as `htl check` sets one
    /// up: config, then the declarations generated from Rust, then the model, the lint
    /// scope, the checker.
    fn load(file: &Path, root: &Path) -> Result<Self> {
        // Taken before anything is read: an edit that lands while the project loads then
        // leaves the manifests unlike this, and the next check loads again.
        let manifests = Manifests::read(root);
        let cfg = load_config(file)?;
        warn_cargo_htl_mismatch(file);
        // A file `htl dts` writes is an input to the check; the failure to write one is
        // said and does not stop the file being checked.
        if let Err(e) = auto_dts(file) {
            eprintln!("htl lsp: {e:#}");
        }
        let model = project::model_of(&cfg, file)?;
        let scope = project::Scope::new(&cfg, model.as_ref(), file, None)?;
        let origins = project::Origins::new(model.as_ref());
        let policy = htl::verdict::Policy::resolve(cfg.as_ref().map(|(_, _, c)| c), false);
        let checker = project::checker(
            model.as_ref(),
            scope.lints.selection(),
            &project::lang_of(&cfg),
        )?;
        let fix = htl::fix::FixOptions {
            unsafe_fixes: false,
            promoted: cfg
                .as_ref()
                .map(|(_, _, c)| c.fix.unsafe_.clone())
                .unwrap_or_default(),
            disabled: cfg
                .as_ref()
                .map(|(_, _, c)| c.fix.disable.clone())
                .unwrap_or_default(),
            only: Vec::new(),
            dry_run: true,
        };
        let fix_error = fix.validate().err().map(|e| format!("{e:#}"));
        Ok(Self {
            cfg,
            model,
            scope,
            origins,
            policy,
            checker,
            fix,
            fix_error,
            manifests,
        })
    }

    /// `text`, the contents of `file`, as `htl fmt` would write it: the project's
    /// `[fmt] indent`, or 3.
    fn format(&self, file: &Path, text: &str) -> Result<String> {
        let indent = self
            .cfg
            .as_ref()
            .and_then(|(_, _, c)| c.fmt.indent)
            .unwrap_or(3);
        self.checker.format_text(text, file, indent)
    }

    /// The `.tl` files the symbol index covers: the ones `htl check` walks for the project
    /// (`htl check <root>`: the walk of the project root, without the dependencies the model
    /// says are not the project's own, and keeping what a module of the project holds), so the
    /// index and the checker never disagree about which files are the project's. A project
    /// `htl check` refuses to walk (one laid out flat without `[layout] source = "."`) is an
    /// error here too.
    ///
    /// A directory that is no project has no walk to answer with: its files are the open ones
    /// (`open`, the paths of the open files of that directory).
    fn source_files(&self, root: &Path, open: &[PathBuf]) -> Result<Vec<PathBuf>> {
        let Some(model) = self.model.as_ref() else {
            return Ok(open.to_vec());
        };
        let paths = [root.to_path_buf()];
        let skip = project::not_walked(Some(model), &paths, htl::model::Purpose::Check);
        let (files, _outside) = project::held_by_modules(
            Some(model),
            &paths,
            htl::collect_tl_skipping(&paths, &skip)?,
        )?;
        Ok(files)
    }

    /// The declarations of `path`, whose text is `text`, as flat symbols: from the last check
    /// of the file if it is open and that check read this text, else from the parse of the
    /// file that [`Htl::declarations_of`] makes. A file that does not parse has none.
    fn symbols_of(
        &self,
        open: &[&Doc],
        canonical: &Path,
        path: &Path,
        text: &str,
    ) -> Vec<symbols::Sym> {
        let checked = open
            .iter()
            .find(|d| d.canonical == canonical)
            .and_then(|d| d.checked.as_ref())
            .filter(|c| c.text == text);
        if let Some(decls) = checked.and_then(|c| c.types.declarations()) {
            return symbols::flatten(text, decls);
        }
        match self.checker.declarations_of(path) {
            Ok(Some(decls)) => symbols::flatten(text, &decls),
            Ok(None) => Vec::new(),
            Err(e) => {
                eprintln!("htl lsp: no symbols for {}: {e:#}", path.display());
                Vec::new()
            }
        }
    }

    /// `file` as `htl fix <file>` would leave it, or `None` when it would not change it.
    ///
    /// A dry run of [`htl::fix::fix_file`] (which checks, applies, re-checks and reverts what
    /// made the file worse, exactly as the command does) in a state of its own. The shared
    /// [`checker`](Self::checker) cannot be used: `fix_file` begins with [`Htl::check`],
    /// which serves a module from the state's store, and a store that outlives an edit
    /// describes the file as it was. A new state has an empty store, so that first check is
    /// of what is on disk now. It costs what a first check costs, which is why the answer is
    /// kept until the next check of the file ([`Checked::fix_all`]).
    fn fixed(&self, file: &Path) -> Result<Option<String>> {
        let h = project::checker(
            self.model.as_ref(),
            self.scope.lints.selection(),
            &project::lang_of(&self.cfg),
        )?;
        project::file_view(&h, self.model.as_ref(), file)?;
        Ok(htl::fix::fix_file(&h, file, &self.fix)?.contents)
    }

    /// The check of `file` as it is on disk that keeps what the checker knows of its names,
    /// and none of what `htl check` would say about it: for the uses of a file that is not
    /// open. Under the view the file itself may read, as [`project::check_file`] sets it, and
    /// with the search path put back.
    fn indexed(&self, file: &Path) -> Result<htl::CheckInfo> {
        let h = &self.checker;
        let saved = h.search_path()?;
        let found = (|| {
            project::file_view(h, self.model.as_ref(), file)?;
            h.check_indexed(file)
        })();
        h.set_search_path(&saved)?;
        found
    }

    /// Check `file` as it is on disk, the way `htl check` reports it: everything the file
    /// and this layer have to say about it, and the files its check reached.
    ///
    /// `open` is every file the client has open, which the sink treats as walked: a
    /// dependency error in one of them is that file's to report, as it is for the files
    /// of an `htl check` walk.
    fn check(&self, file: &Path, open: &[PathBuf]) -> Result<Done> {
        let mut sink = Sink::new(Collect::default());
        sink.walking(open);
        let walk = self
            .scope
            .walk(&self.cfg, self.model.as_ref(), &self.origins);
        // The per-file question `htl check`'s walk asks, from disk, keeping what the
        // checker knows of the file's names for hover and definition.
        let (c, _) = project::check_file(
            &self.checker,
            &mut sink,
            file,
            &walk,
            project::Fresh::DiskIndexed,
        )?;
        Ok(Done {
            found: sink.out().take(),
            deps: c.deps,
            requires: c.requires,
            types: c.types,
        })
    }
}

/// What [`Project::check`] made of a file.
struct Done {
    found: Vec<htl::Diagnostic>,
    /// The files its check reached through `require`.
    deps: Vec<PathBuf>,
    /// Its literal `require`s and the files they resolved to.
    requires: Vec<htl::RequireSite>,
    /// What the checker knows of its names, by position.
    types: htl::typeindex::TypeIndex,
}

/// A document the client has open.
struct Doc {
    path: PathBuf,
    /// Where `path` really is, for comparing it with the files a check reached.
    canonical: PathBuf,
    /// The key of the [`Project`] it belongs to, once the walk up for it has answered. A
    /// walk that failed (an `htl.toml` that does not parse) is asked again at the next
    /// check, so the file is not filed under its directory for the rest of the session.
    project: Option<PathBuf>,
    /// The files its last check reached through `require` (canonical): what a save of any
    /// of them makes it stale for.
    deps: HashSet<PathBuf>,
    /// The text the client holds: what it opened the file with, then what its last
    /// `didChange` said. `None` for a file known only from a save, whose buffer is then
    /// the disk. It is what formatting formats and what an edit is computed against; a
    /// check never reads it.
    buffer: Option<String>,
    /// Set when the client sent a change this server cannot apply (an incremental one): the
    /// client holds a text the server does not know, so neither `buffer` nor the disk is
    /// what an edit may be computed against, and formatting and code actions answer nothing
    /// until a `didOpen` or a full change says what the text is again.
    unsynced: bool,
    /// What the last check of it found, as it was found; `None` before the first check and
    /// for a file that was not on disk.
    checked: Option<Checked>,
}

/// One check's result for a document, kept for the requests that follow it (a code action
/// is answered from this, not from a check of its own).
struct Checked {
    /// The file's text when it was checked. A fix is a set of positions in it, so a request
    /// offers one only while the buffer is still this text, and is answered with nothing
    /// rather than with edits for a text that is gone.
    text: String,
    findings: Vec<Finding>,
    /// The file's literal `require`s and where htl's resolution put each: what go-to-definition
    /// on a `require`'s string answers.
    requires: Vec<htl::RequireSite>,
    /// What the checker knows of the names in `text`, by position: what hover and
    /// go-to-definition answer.
    types: htl::typeindex::TypeIndex,
    /// What `htl fix` would make of the file: not asked for until a request wants it, and
    /// asked once (`Some(None)` is "asked, and it changes nothing").
    fix_all: Option<Option<String>>,
}

/// A diagnostic as published, with the fix the checker attached to it.
struct Finding {
    lsp: lsp_types::Diagnostic,
    /// Present only for a finding in the file itself: the edits of a fix are positions in
    /// the file it is attached to, and a finding about a required module is shown here
    /// but is not this file's to rewrite.
    fix: Option<htl::Fix>,
    /// What `htl fix --rule` and `[fix]` name the fix by.
    rule: String,
}

/// What a batch of messages left to check.
#[derive(Default)]
struct Stale {
    /// The files to check, each once, in the order the batch first named them.
    files: Vec<Uri>,
    /// Files (canonical) whose change makes the open files that require them stale: the
    /// ones saved, and the ones opened or closed — an open file reports its own errors,
    /// a closed one is reported through the files that require it, as in an `htl check`
    /// walk of the open files, so which files are open changes what a dependent says.
    changed: Vec<PathBuf>,
}

struct Server<'c> {
    connection: &'c Connection,
    /// By project root; a file in no project is keyed by its directory.
    projects: HashMap<PathBuf, Project>,
    /// By the URI the client used, which is the one a publish has to name.
    open: HashMap<Uri, Doc>,
    /// Set by the `shutdown` request: from then on only `exit` is answered.
    shutting_down: bool,
    /// Whether the client takes a `client/registerCapability` for file watchers.
    dynamic_watch: bool,
    /// Whether the client took the registration: only then are the `didChangeWatchedFiles`
    /// notifications taken to say every change to a `.tl` (module doc, "Workspace symbols").
    watching: bool,
    /// The folders the client opened the workspace on, for a symbol search made before any
    /// file is open.
    workspace: Vec<PathBuf>,
    /// The symbols of each project a workspace-symbol request has asked about, by project
    /// root. A project dropped because a manifest moved takes its index with it; one dropped
    /// for a declaration or a Rust source keeps it (what a file declares does not depend on
    /// either), and is loaded again by the next request.
    indices: HashMap<PathBuf, SymbolIndex>,
}

/// The id of the one request the server sends, so that its response is known when it comes.
const REGISTER_ID: &str = "htl-watched-files";

/// What the client is asked to watch (module doc, "Files that change the answer").
const WATCHED: [&str; 7] = [
    "**/htl.toml",
    "**/mlua-pkg.toml",
    "**/mlua-pkg.lock",
    "**/types/**/*.d.tl",
    // The sources whose declarations the workspace-symbol request lists.
    "**/*.tl",
    // What `htl dts` reads to write declarations: a crate's manifest and the Rust under the
    // four directories it walks ([`htl::dts::generate_crate_to`]), nowhere else.
    "**/Cargo.toml",
    "**/{src,examples,tests,benches}/**/*.rs",
];

impl<'c> Server<'c> {
    fn new(connection: &'c Connection, dynamic_watch: bool, workspace: Vec<PathBuf>) -> Self {
        Self {
            connection,
            projects: HashMap::new(),
            open: HashMap::new(),
            shutting_down: false,
            dynamic_watch,
            watching: false,
            workspace,
            indices: HashMap::new(),
        }
    }

    /// Ask the client to tell us when the files in [`WATCHED`] change. The answer comes back
    /// as a `Message::Response` ([`Self::apply`]).
    fn register_watchers(&self) {
        let options = DidChangeWatchedFilesRegistrationOptions {
            watchers: WATCHED
                .iter()
                .map(|g| FileSystemWatcher {
                    glob_pattern: GlobPattern::String((*g).to_string()),
                    // Create, change and delete: a manifest that appears or goes changes the
                    // answer as much as one that is edited.
                    kind: None,
                })
                .collect(),
        };
        let params = RegistrationParams {
            registrations: vec![Registration {
                id: REGISTER_ID.to_string(),
                method: DidChangeWatchedFiles::METHOD.to_string(),
                register_options: serde_json::to_value(options).ok(),
            }],
        };
        let request = lsp_server::Request::new(
            RequestId::from(REGISTER_ID.to_string()),
            RegisterCapability::METHOD.to_string(),
            params,
        );
        self.send(request.into());
    }

    /// Read a batch, apply it, check what it made stale; until `exit`.
    fn run(&mut self) -> Result<ExitCode> {
        if self.dynamic_watch {
            self.register_watchers();
        }
        loop {
            let Ok(first) = self.connection.receiver.recv() else {
                eprintln!("htl lsp: the client closed the connection without `exit`");
                return Ok(ExitCode::FAILURE);
            };
            // Everything already queued comes with it: a save that was superseded while
            // the last check ran is applied in the same batch as the save that replaced it
            // and checked once.
            let mut batch = vec![first];
            while let Ok(next) = self.connection.receiver.try_recv() {
                batch.push(next);
            }
            let mut stale = Stale::default();
            for message in batch {
                // What the messages before a `shutdown` asked for is done before it is
                // answered: from then on nothing is checked.
                if !self.shutting_down
                    && matches!(&message, Message::Request(r) if r.method == Shutdown::METHOD)
                {
                    self.check_all(&std::mem::take(&mut stale));
                }
                if let Some(code) = self.apply(message, &mut stale) {
                    return Ok(code);
                }
            }
            if !self.shutting_down {
                self.check_all(&stale);
            }
        }
    }

    /// Apply one message. Files to check are added to `stale`; `Some` is the exit code once
    /// `exit` has come.
    fn apply(&mut self, message: Message, stale: &mut Stale) -> Option<ExitCode> {
        match message {
            Message::Request(req) => {
                let response = if req.method == Shutdown::METHOD {
                    self.shutting_down = true;
                    Response::new_ok(req.id, ())
                } else if self.shutting_down {
                    Response::new_err(
                        req.id,
                        ErrorCode::InvalidRequest as i32,
                        "the server is shutting down".to_string(),
                    )
                } else if [
                    Formatting::METHOD,
                    CodeActionRequest::METHOD,
                    HoverRequest::METHOD,
                    GotoDefinition::METHOD,
                    DocumentSymbolRequest::METHOD,
                    WorkspaceSymbolRequest::METHOD,
                    References::METHOD,
                    CallHierarchyPrepare::METHOD,
                    CallHierarchyIncomingCalls::METHOD,
                    CallHierarchyOutgoingCalls::METHOD,
                ]
                .contains(&req.method.as_str())
                {
                    // Answered about the files as the messages before it left them: what
                    // they made stale is checked first, as before a `shutdown`. A code
                    // action, a hover, a definition and an outline are read off the last
                    // check, so one asked right behind a save would otherwise describe the
                    // file as it was before.
                    self.check_all(&std::mem::take(stale));
                    self.document_request(req)
                } else {
                    let what = format!("htl lsp does not answer {}", req.method);
                    Response::new_err(req.id, ErrorCode::MethodNotFound as i32, what)
                };
                self.send(response.into());
            }
            Message::Notification(n) => return self.notification(n, stale),
            // The only request the server sends is the watcher registration. A client that
            // refuses it still gets the fallback (module doc); the refusal is said.
            Message::Response(r) => {
                if r.id == RequestId::from(REGISTER_ID.to_string()) {
                    match r.response_result {
                        Ok(_) => self.watching = true,
                        Err(e) => eprintln!(
                            "htl lsp: the client refused to watch files ({}): {}; the manifests \
                             are compared at each check instead",
                            e.code, e.message
                        ),
                    }
                }
            }
        }
        None
    }

    fn notification(&mut self, n: Notification, stale: &mut Stale) -> Option<ExitCode> {
        match n.method.as_str() {
            Exit::METHOD => {
                return Some(if self.shutting_down {
                    ExitCode::SUCCESS
                } else {
                    ExitCode::FAILURE
                });
            }
            // Accepted and ignored: see the module doc.
            Cancel::METHOD => {}
            DidOpenTextDocument::METHOD => {
                if let Some(p) = params::<DidOpenTextDocumentParams>(n) {
                    let d = p.text_document;
                    self.opened(d.uri, stale, false, Some(d.text));
                }
            }
            // Stored, not checked: the diagnostics are about what is saved (module doc).
            DidChangeTextDocument::METHOD => {
                if let Some(p) = params::<DidChangeTextDocumentParams>(n) {
                    self.changed(&p.text_document.uri, p.content_changes);
                }
            }
            DidSaveTextDocument::METHOD => {
                if let Some(p) = params::<DidSaveTextDocumentParams>(n) {
                    // A save of a file never opened is a file to check like any other.
                    self.opened(p.text_document.uri, stale, true, None);
                }
            }
            DidCloseTextDocument::METHOD => {
                if let Some(p) = params::<DidCloseTextDocumentParams>(n) {
                    let uri = p.text_document.uri;
                    stale.files.retain(|u| *u != uri);
                    if let Some(doc) = self.open.remove(&uri) {
                        stale.changed.push(doc.canonical);
                        self.publish(uri, Vec::new());
                    }
                }
            }
            DidChangeWatchedFiles::METHOD => {
                if let Some(p) = params::<DidChangeWatchedFilesParams>(n) {
                    let paths: Vec<PathBuf> =
                        p.changes.iter().filter_map(|c| path_of(&c.uri)).collect();
                    self.watched(&paths, stale);
                }
            }
            // Other notifications (`initialized` was consumed by the handshake, the
            // `$/` ones are optional by the protocol) are not this server's.
            _ => {}
        }
        None
    }

    /// A `didChange`: the buffer is now the text the (full-sync) change carries. A change
    /// for a document that is not open is a client that has not said it opened it, and is
    /// not this server's to keep.
    fn changed(&mut self, uri: &Uri, changes: Vec<lsp_types::TextDocumentContentChangeEvent>) {
        // Full sync: the last event is the whole text. One with a range is a client that
        // did not take the capability, and is not applied to a text it does not describe.
        let Some(last) = changes.into_iter().next_back() else {
            return;
        };
        if let Some(doc) = self.open.get_mut(uri) {
            if last.range.is_none() {
                doc.buffer = Some(last.text);
                doc.unsynced = false;
            } else {
                // Falling back to the disk here would hand out edits for the saved text over
                // an unsaved buffer: nothing is answered until the text is known again.
                eprintln!("htl lsp: an incremental change, which was not asked for; ignored");
                doc.buffer = None;
                doc.unsynced = true;
            }
        }
    }

    /// `uri` is to be checked, because it was opened (with `text`) or (`saved`) saved: make
    /// it an open document if it is not one yet.
    fn opened(&mut self, uri: Uri, stale: &mut Stale, saved: bool, text: Option<String>) {
        let Some(path) = path_of(&uri) else {
            eprintln!("htl lsp: not a file: {}", uri.as_str());
            return;
        };
        let newly_open = !self.open.contains_key(&uri);
        if newly_open {
            let project = project_key(&path).ok();
            self.open.insert(
                uri.clone(),
                Doc {
                    canonical: canonical(&path),
                    path,
                    project,
                    deps: HashSet::new(),
                    buffer: None,
                    unsynced: false,
                    checked: None,
                },
            );
        }
        if let Some(text) = text
            && let Some(doc) = self.open.get_mut(&uri)
        {
            doc.buffer = Some(text);
            doc.unsynced = false;
        }
        if let Some(doc) = self.open.get(&uri)
            && (saved || newly_open)
        {
            stale.changed.push(doc.canonical.clone());
        }
        if !stale.files.contains(&uri) {
            stale.files.push(uri);
        }
    }

    /// Files the client says changed: drop the projects they belong to, so the next check
    /// builds them again, and make the open files of those projects stale.
    ///
    /// Dropping is all that is done here, so any number of events for one project (a `git
    /// checkout` touches hundreds of files) is one rebuild, at the check that follows the
    /// batch. Which projects and which files each kind of change reaches is [`Watched`].
    fn watched(&mut self, paths: &[PathBuf], stale: &mut Stale) {
        let mut gone: Vec<PathBuf> = Vec::new();
        // The projects a manifest drops: the only ones whose symbol index goes with them.
        let mut gone_manifest: Vec<PathBuf> = Vec::new();
        let mut manifest_dirs: Vec<PathBuf> = Vec::new();
        let mut hit: Vec<Uri> = Vec::new();
        // Where each open document's crate is, asked once per document for the batch.
        let mut crates: HashMap<PathBuf, Option<PathBuf>> = HashMap::new();
        // A project's root and a crate's are found by walking up from a canonical
        // directory, and a key outside any project is the file's directory as the client
        // named it; the paths of the events are the client's. Everything is compared in
        // canonical form, or a project under a symlinked directory is reached by nothing.
        let mut keys: HashMap<PathBuf, PathBuf> = HashMap::new();
        let mut canonical_key = |k: &Path| -> PathBuf {
            keys.entry(k.to_path_buf())
                .or_insert_with(|| canonical(k))
                .clone()
        };
        for path in paths {
            // A `.tl` is the symbol index's, and nothing else's: its check is the save's.
            if is_tl(path) {
                let path = canonical_dir_of(path);
                for (root, index) in &mut self.indices {
                    if path.starts_with(canonical_key(root)) {
                        index.event(&path);
                    }
                }
            }
            let Some(kind) = Watched::of(path) else {
                continue;
            };
            let path = canonical_dir_of(path);
            let dir = htl::parent_dir(&path);
            if kind == Watched::Manifest {
                manifest_dirs.push(dir.clone());
            }
            for (uri, doc) in &mut self.open {
                let key = doc.project.clone();
                let reached = match kind {
                    // A manifest decides the project of every file under its directory,
                    // including one that has none yet or whose project is another: the
                    // walk up is asked again for those. A project whose root is on the
                    // way up from the manifest (or under it) is rebuilt.
                    Watched::Manifest => {
                        let under = doc.canonical.starts_with(&dir) || doc.path.starts_with(&dir);
                        if under {
                            doc.project = None;
                        }
                        under
                            || key.as_ref().is_some_and(|k| {
                                let k = canonical_key(k);
                                k.starts_with(&dir) || dir.starts_with(&k)
                            })
                    }
                    Watched::Types => key
                        .as_ref()
                        .is_some_and(|k| path.starts_with(canonical_key(k))),
                    Watched::Rust => crates
                        .entry(doc.path.clone())
                        .or_insert_with(|| htl::dts::find_cargo_package_root(&doc.path))
                        .as_deref()
                        .is_some_and(|c| Watched::reads_rust(&path, c)),
                };
                if reached {
                    if kind == Watched::Manifest {
                        gone_manifest.extend(key.clone());
                    }
                    gone.extend(key);
                    if !hit.contains(uri) {
                        hit.push(uri.clone());
                    }
                }
            }
        }
        self.projects.retain(|k, _| {
            let ck = canonical_key(k);
            !gone.contains(k)
                && !manifest_dirs
                    .iter()
                    .any(|d| ck.starts_with(d) || d.starts_with(&ck))
        });
        // A declaration under `types/` or a Rust source changes what a check says, not what a
        // file declares or which files the walk visits: the index is kept, and the project
        // is loaded again by the next request that asks it. A manifest decides the walk and
        // `[lang]`, so the index of a project it reaches goes with the project.
        self.indices.retain(|k, _| {
            let ck = canonical_key(k);
            !gone_manifest.contains(k)
                && !manifest_dirs
                    .iter()
                    .any(|d| ck.starts_with(d) || d.starts_with(&ck))
        });
        // What the check resolves a name against changed, though no `.tl` of the project did:
        // the declarations the index keeps are as they were, the uses made against the old
        // ones are not (module doc, "References").
        for (k, index) in &mut self.indices {
            if gone.contains(k) {
                index.forget_refs();
            }
        }
        hit.sort();
        for uri in hit {
            if !stale.files.contains(&uri) {
                stale.files.push(uri);
            }
        }
    }

    /// Check what the batch left stale, and the open files that require a file it saved,
    /// opened or closed, each once.
    fn check_all(&mut self, stale: &Stale) {
        let changed: HashSet<PathBuf> = stale.changed.iter().cloned().collect();
        // Who needs a re-check is read before any check runs, from the closures the last
        // checks recorded. In URI order, so that a run is the same run every time.
        let mut dependents: Vec<Uri> = self
            .open
            .iter()
            .filter(|(u, d)| !stale.files.contains(u) && !d.deps.is_disjoint(&changed))
            .map(|(u, _)| u.clone())
            .collect();
        dependents.sort();
        let mut files: Vec<Uri> = stale.files.iter().chain(&dependents).cloned().collect();
        // A document whose walk up has not answered yet is asked here, so that the project
        // its check will use is among the ones compared.
        let keys: HashSet<PathBuf> = files
            .iter()
            .filter_map(|u| {
                let d = self.open.get(u)?;
                d.project.clone().or_else(|| project_key(&d.path).ok())
            })
            .collect();
        for uri in self.drop_moved(keys) {
            if !files.contains(&uri) {
                files.push(uri);
            }
        }
        for uri in &files {
            self.check_one(uri);
        }
    }

    /// The projects among `keys` whose manifests read differently than when they were
    /// built (module doc, "Files that change the answer"): dropped, so that the next use
    /// builds them again, and their open files returned in URI order, since what such a
    /// project says changes for every open file of it and not only for the one that asked.
    fn drop_moved(&mut self, keys: impl IntoIterator<Item = PathBuf>) -> Vec<Uri> {
        let moved: Vec<PathBuf> = keys
            .into_iter()
            .filter(|k| self.projects.get(k).is_some_and(|p| p.manifests.changed()))
            .collect();
        if moved.is_empty() {
            return Vec::new();
        }
        for k in &moved {
            self.projects.remove(k);
            self.indices.remove(k);
        }
        let mut files: Vec<Uri> = self
            .open
            .iter()
            .filter(|(_, d)| d.project.as_ref().is_some_and(|k| moved.contains(k)))
            .map(|(u, _)| u.clone())
            .collect();
        files.sort();
        files
    }

    /// Check one open document and publish what it reports.
    fn check_one(&mut self, uri: &Uri) {
        let open: Vec<PathBuf> = self.open.values().map(|d| d.path.clone()).collect();
        let Some(doc) = self.open.get(uri) else {
            return;
        };
        let (path, known) = (doc.path.clone(), doc.project.clone());
        // Not on disk: nothing has been saved to check, and the checker's answer for a
        // file it cannot open is not a diagnostic (see the module doc).
        if !path.is_file() {
            if let Some(doc) = self.open.get_mut(uri) {
                doc.deps.clear();
                doc.checked = None;
            }
            self.publish(uri.clone(), Vec::new());
            return;
        }
        let key = match known {
            Some(key) => key,
            None => match project_key(&path) {
                Ok(key) => {
                    if let Some(doc) = self.open.get_mut(uri) {
                        doc.project = Some(key.clone());
                    }
                    key
                }
                // Asked again next time; the load below says why it failed.
                Err(_) => htl::parent_dir(&path),
            },
        };
        // Read before the check, and again after it (below): a write that lands while the
        // check runs then leaves the disk unlike `text`, and the check's fixes are dropped,
        // where a text read only after would match the disk and hand out the old text's
        // fixes.
        let stamp = Stamp::of(&path);
        let text = read_lossy(&path);
        let outcome = self.project(&key, &path).and_then(|p| {
            let done = p.check(&path, &open)?;
            Ok((
                done,
                p.scope.lints.selection().clone(),
                p.policy,
                p.fix_error.clone(),
            ))
        });
        let (findings, deps, requires, types) = match outcome {
            Ok((done, levels, policy, fix_error)) => {
                let mut findings = convert(&path, &text, &done.found, &levels, &policy);
                // A `[fix]` that `htl fix` refuses is a setup error, said like one.
                if let Some(e) = fix_error {
                    findings.push(Finding {
                        lsp: failure(&anyhow::anyhow!("{e}")),
                        fix: None,
                        rule: String::new(),
                    });
                }
                (findings, done.deps, done.requires, done.types)
            }
            // Nothing could be said about the file: say that, on the file, so the editor
            // shows why it has no diagnostics instead of looking clean.
            Err(e) => (
                vec![Finding {
                    lsp: failure(&e),
                    fix: None,
                    rule: String::new(),
                }],
                Vec::new(),
                Vec::new(),
                htl::typeindex::TypeIndex::default(),
            ),
        };
        // A write that landed while the check ran leaves the checker's positions in a text
        // that may not be `text`; the buffer can still be `text` (an external write), so the
        // comparison in `code_actions` would not catch it. Its fixes, the index of its
        // names and its `require` sites are dropped; the save that follows the write checks
        // again.
        let (mut findings, mut requires, mut types) = (findings, requires, types);
        if read_lossy(&path) != text {
            for f in &mut findings {
                f.fix = None;
            }
            requires.clear();
            types = htl::typeindex::TypeIndex::default();
        }
        let diagnostics = findings.iter().map(|f| f.lsp.clone()).collect();
        // The symbol index, if the project has one, takes this check's declarations for the
        // file: fresher than a read of the disk, and kinds the types decide (module doc,
        // "Workspace symbols").
        if let Some(index) = self.indices.get_mut(&key)
            && let Some(stamp) = stamp
            && let Some(decls) = types.declarations()
        {
            let syms = symbols::flatten(&text, decls);
            index.put(
                &canonical(&path),
                Entry::new(path.clone(), stamp, &text, syms),
                deps.iter().map(|d| canonical(d)).collect(),
            );
        }
        if let Some(doc) = self.open.get_mut(uri) {
            doc.deps = deps.iter().map(|d| canonical(d)).collect();
            doc.checked = Some(Checked {
                text,
                findings,
                requires,
                types,
                fix_all: None,
            });
        }
        self.publish(uri.clone(), diagnostics);
    }

    /// Answer `textDocument/formatting`, `codeAction`, `hover`, `definition`,
    /// `documentSymbol`, `references`, the workspace-symbol request or one of the three of the
    /// call hierarchy.
    fn document_request(&mut self, req: lsp_server::Request) -> Response {
        let id = req.id.clone();
        let bad = |e: serde_json::Error| {
            Response::new_err(
                id.clone(),
                ErrorCode::InvalidParams as i32,
                format!("malformed {}: {e}", req.method),
            )
        };
        let params = req.params.clone();
        match req.method.as_str() {
            Formatting::METHOD => {
                match serde_json::from_value::<DocumentFormattingParams>(params) {
                    Ok(p) => Response::new_ok(id.clone(), self.formatting(&p.text_document.uri)),
                    Err(e) => bad(e),
                }
            }
            HoverRequest::METHOD => match serde_json::from_value::<HoverParams>(params) {
                Ok(p) => {
                    let at = p.text_document_position_params;
                    Response::new_ok(id.clone(), self.hover(&at.text_document.uri, at.position))
                }
                Err(e) => bad(e),
            },
            GotoDefinition::METHOD => {
                match serde_json::from_value::<GotoDefinitionParams>(params) {
                    Ok(p) => {
                        let at = p.text_document_position_params;
                        Response::new_ok(
                            id.clone(),
                            self.definition(&at.text_document.uri, at.position),
                        )
                    }
                    Err(e) => bad(e),
                }
            }
            DocumentSymbolRequest::METHOD => {
                match serde_json::from_value::<DocumentSymbolParams>(params) {
                    Ok(p) => {
                        Response::new_ok(id.clone(), self.document_symbols(&p.text_document.uri))
                    }
                    Err(e) => bad(e),
                }
            }
            References::METHOD => match serde_json::from_value::<ReferenceParams>(params) {
                Ok(p) => {
                    let at = p.text_document_position;
                    Response::new_ok(
                        id.clone(),
                        self.references(
                            &at.text_document.uri,
                            at.position,
                            p.context.include_declaration,
                        ),
                    )
                }
                Err(e) => bad(e),
            },
            CallHierarchyPrepare::METHOD => {
                match serde_json::from_value::<CallHierarchyPrepareParams>(params) {
                    Ok(p) => {
                        let at = p.text_document_position_params;
                        Response::new_ok(
                            id.clone(),
                            self.prepare_calls(&at.text_document.uri, at.position),
                        )
                    }
                    Err(e) => bad(e),
                }
            }
            CallHierarchyIncomingCalls::METHOD => {
                match serde_json::from_value::<CallHierarchyIncomingCallsParams>(params) {
                    Ok(p) => Response::new_ok(id.clone(), self.incoming_calls(&p.item)),
                    Err(e) => bad(e),
                }
            }
            CallHierarchyOutgoingCalls::METHOD => {
                match serde_json::from_value::<CallHierarchyOutgoingCallsParams>(params) {
                    Ok(p) => Response::new_ok(id.clone(), self.outgoing_calls(&p.item)),
                    Err(e) => bad(e),
                }
            }
            WorkspaceSymbolRequest::METHOD => {
                match serde_json::from_value::<WorkspaceSymbolParams>(params) {
                    Ok(p) => Response::new_ok(id.clone(), self.workspace_symbols(&p.query)),
                    Err(e) => bad(e),
                }
            }
            _ => match serde_json::from_value::<CodeActionParams>(params) {
                Ok(p) => Response::new_ok(id.clone(), self.code_actions(&p)),
                Err(e) => bad(e),
            },
        }
    }

    /// What the last check of `uri` read, while the client's buffer is still that text:
    /// positions in the check's answers are positions in it. `None` for a document that was
    /// never checked, one whose buffer has moved on, and one whose buffer the server lost
    /// track of.
    fn checked_buffer(&self, uri: &Uri) -> Option<&Checked> {
        let doc = self.open.get(uri).filter(|d| !d.unsynced)?;
        let checked = doc.checked.as_ref()?;
        let buffer = match &doc.buffer {
            Some(buffer) => buffer.as_str(),
            None => &read_lossy(&doc.path),
        };
        (buffer == checked.text).then_some(checked)
    }

    /// The type of the name at `at`, as Teal writes it; `null` when there is no name there
    /// or the checker has no type for it.
    fn hover(&self, uri: &Uri, at: Position) -> Option<Hover> {
        let checked = self.checked_buffer(uri)?;
        let lines = lines_of(&checked.text);
        let (line, col) = byte_position(&lines, at)?;
        let found = checked.types.at(&checked.text, line, col)?;
        let ty = found.ty?;
        Some(Hover {
            contents: HoverContents::Markup(MarkupContent {
                kind: MarkupKind::Markdown,
                value: format!("```teal\n{ty}\n```"),
            }),
            range: Some(range_at(&lines, found.line, found.col)),
        })
    }

    /// Where the thing at `at` is declared: for the string of a `require`, the file htl's
    /// resolution picked; for a name, the declaration the checker's report points to. `null`
    /// when neither is known, or the declaration is in a file that is not on disk.
    fn definition(&self, uri: &Uri, at: Position) -> Option<GotoDefinitionResponse> {
        let checked = self.checked_buffer(uri)?;
        let lines = lines_of(&checked.text);
        let (line, col) = byte_position(&lines, at)?;
        let here = path_of(uri)?;
        if let Some(site) = required_at(&lines, &checked.requires, line, col) {
            // The module's first character: a `require` is of the file, not of a line in it.
            let file = site.path.as_ref()?;
            let target = location_of(&here, uri, file, &checked.text, None)?;
            return Some(GotoDefinitionResponse::Scalar(target));
        }
        let place = checked.types.at(&checked.text, line, col)?.declared?;
        let target = location_of(
            &here,
            uri,
            &place.file,
            &checked.text,
            Some((place.line, place.col)),
        )?;
        Some(GotoDefinitionResponse::Scalar(target))
    }

    /// The declarations of the file as a tree, from the index of the last check of it. `null`
    /// when that is not known: the file is not open, it was never checked or is not on disk,
    /// the buffer is not the text the check read, or it did not parse. `[]` only for a file
    /// that parses and declares nothing.
    fn document_symbols(&self, uri: &Uri) -> Option<DocumentSymbolResponse> {
        let checked = self.checked_buffer(uri)?;
        let decls = checked.types.declarations()?;
        let lines = lines_of(&checked.text);
        Some(DocumentSymbolResponse::Nested(
            decls.iter().map(|d| symbol_of(&lines, d)).collect(),
        ))
    }

    /// The declarations of the project's files whose name has `query` in it, as the answer to
    /// a workspace-symbol request. The projects asked are those of the open files, else those
    /// of the workspace folders. `null` when no project's index could be made (none to ask, or
    /// the one there is cannot be loaded or walked: the reason is on stderr); `[]` when an
    /// index was made and nothing in it matches.
    fn workspace_symbols(&mut self, query: &str) -> Option<WorkspaceSymbolResponse> {
        let mut docs: Vec<(&Uri, &Doc)> = self.open.iter().collect();
        docs.sort_by(|a, b| a.0.cmp(b.0));
        let mut asked: Vec<(PathBuf, PathBuf)> = Vec::new();
        for (_, d) in docs {
            let key = d
                .project
                .clone()
                .or_else(|| project_key(&d.path).ok())
                .unwrap_or_else(|| htl::parent_dir(&d.path));
            if !asked.iter().any(|(k, _)| *k == key) {
                asked.push((key, d.path.clone()));
            }
        }
        if asked.is_empty() {
            for dir in &self.workspace {
                match htl::model::Project::find_root(dir) {
                    Ok(Some(found)) => {
                        if !asked.iter().any(|(k, _)| *k == found.root) {
                            asked.push((found.root, dir.clone()));
                        }
                    }
                    Ok(None) => {}
                    Err(e) => eprintln!("htl lsp: no symbols for {}: {e:#}", dir.display()),
                }
            }
            if asked.is_empty() {
                eprintln!(
                    "htl lsp: no symbols to search: no file is open and no workspace folder \
                     is in a project"
                );
                return None;
            }
        }
        // A project whose manifests moved is built again, and its open files are checked
        // against it, before it is asked (as before a formatting request).
        for other in self.drop_moved(asked.iter().map(|(k, _)| k.clone()).collect::<Vec<_>>()) {
            self.check_one(&other);
        }
        let mut built: Vec<&PathBuf> = Vec::new();
        for (key, file) in &asked {
            match self.refresh_symbols(key, file) {
                Ok(()) => built.push(key),
                Err(e) => eprintln!("htl lsp: no symbols for {}: {e:#}", key.display()),
            }
        }
        if built.is_empty() {
            return None;
        }
        // A watcher names files, not the directories they were in: a directory deleted or
        // renamed is one event for the directory, which `**/*.tl` does not match, and its
        // files would stay in the index. A hit on a file that is gone says so; the indices
        // that hold it walk their project again, and the search is made once more.
        let mut out = self.symbol_answer(&built, query);
        if !out.1.is_empty() {
            let mut again: Vec<&PathBuf> = Vec::new();
            for key in &built {
                if let Some(index) = self.indices.get_mut(*key)
                    && index.entries().any(|e| out.1.contains(&e.path))
                {
                    index.relist();
                    again.push(*key);
                }
            }
            for key in again {
                let file = &asked.iter().find(|(k, _)| k == key).expect("asked").1;
                if let Err(e) = self.refresh_symbols(key, file) {
                    eprintln!("htl lsp: no symbols for {}: {e:#}", key.display());
                }
            }
            out = self.symbol_answer(&built, query);
        }
        Some(WorkspaceSymbolResponse::Flat(out.0))
    }

    /// The search of a workspace-symbol request over the indices of `built`, and the files
    /// among its hits that are no longer there (left out of the answer).
    fn symbol_answer(
        &self,
        built: &[&PathBuf],
        query: &str,
    ) -> (Vec<SymbolInformation>, Vec<PathBuf>) {
        let (hits, total) = symbols::search(
            built
                .iter()
                .filter_map(|k| self.indices.get(*k))
                .flat_map(SymbolIndex::entries),
            query,
        );
        if total > hits.len() {
            eprintln!(
                "htl lsp: {total} symbols match {query:?}; the first {} are answered",
                hits.len()
            );
        }
        let mut gone: Vec<PathBuf> = Vec::new();
        let mut uri: Option<(&Path, Option<Uri>)> = None;
        let mut out = Vec::with_capacity(hits.len());
        for h in hits {
            if uri.as_ref().is_none_or(|(p, _)| *p != h.path) {
                let target = if h.path.is_file() {
                    std::path::absolute(h.path).ok().and_then(|p| uri_of(&p))
                } else {
                    if !gone.iter().any(|g| g == h.path) {
                        gone.push(h.path.to_path_buf());
                    }
                    None
                };
                uri = Some((h.path, target));
            }
            let Some((_, Some(target))) = &uri else {
                continue;
            };
            #[allow(deprecated)]
            out.push(SymbolInformation {
                name: h.sym.name.clone(),
                kind: h.sym.kind,
                tags: None,
                deprecated: None,
                location: Location {
                    uri: target.clone(),
                    range: h.sym.range,
                },
                container_name: h.sym.container.clone(),
            });
        }
        (out, gone)
    }

    /// Bring the symbol index of the project under `key` up to date, making it (and loading
    /// the project, from `file`) if the session has not met it.
    fn refresh_symbols(&mut self, key: &Path, file: &Path) -> Result<()> {
        self.project(key, file)?;
        let mut index = self.indices.remove(key).unwrap_or_default();
        let project = &self.projects[key];
        let open: Vec<&Doc> = self.open.values().collect();
        // The files of a directory that is no project: the open ones.
        let own: Vec<PathBuf> = open
            .iter()
            .filter(|d| d.project.as_deref() == Some(key) || htl::parent_dir(&d.path) == key)
            .map(|d| d.path.clone())
            .collect();
        // A directory that is no project is listed by its open files, which no watcher
        // reports: it is listed again each time (that costs a `stat` per open file).
        let sweep = !self.watching || project.model.is_none();
        let result = index.refresh(
            sweep,
            || project.source_files(key, &own),
            &mut |canonical, path, text| project.symbols_of(&open, canonical, path, text),
        );
        self.indices.insert(key.to_path_buf(), index);
        result
    }

    /// What every request that asks about the uses of the project has first: the project
    /// of `uri` built (again, if its manifests moved) and its symbol index brought up to
    /// date, the files that decide what the uses resolve against looked at when the client
    /// does not watch, the file a known file's uses are not trusted for checked again, and
    /// the walk asked to hold `uri`'s file ("References" in the module doc says why each).
    /// `what` names the request in the line on stderr that says why there is no answer.
    /// The project's key and the file's path, or `None` when nothing can be answered (the
    /// project's files cannot be listed, or the file is not one the walk visits).
    fn project_ready(&mut self, uri: &Uri, what: &str) -> Option<(PathBuf, PathBuf)> {
        let here = path_of(uri)?;
        let key = self
            .open
            .get(uri)
            .and_then(|d| d.project.clone())
            .or_else(|| project_key(&here).ok())
            .unwrap_or_else(|| htl::parent_dir(&here));
        // A project whose manifests moved is built again and its open files are checked
        // against it before it is asked, as for a workspace-symbol request.
        for other in self.drop_moved([key.clone()]) {
            self.check_one(&other);
        }
        if let Err(e) = self.refresh_symbols(&key, &here) {
            eprintln!("htl lsp: no {what} for {}: {e:#}", key.display());
            return None;
        }
        // A client that does not watch is not told that a `types/` declaration or a Rust
        // source changed, and neither the project (which `htl dts` and the model were run
        // for) nor the uses made against it would be rebuilt: the files that decide it are
        // fingerprinted at each request, and a change is what a watcher's event would have
        // been, so the project is built again, its open files checked against it and the
        // uses made again.
        if !self.watching
            && self.projects.get(&key).is_some_and(|p| p.model.is_some())
            && self
                .indices
                .get_mut(&key)
                .is_some_and(|i| i.inputs_changed(project_inputs(&key)))
        {
            self.projects.remove(&key);
            if let Some(index) = self.indices.get_mut(&key) {
                index.forget_refs();
            }
            let mut files: Vec<Uri> = self
                .open
                .iter()
                .filter(|(_, d)| d.project.as_ref() == Some(&key))
                .map(|(u, _)| u.clone())
                .collect();
            files.sort();
            for other in files {
                self.check_one(&other);
            }
        }
        // A file the answer needs the uses of that the walk does not visit (a file in no
        // module of the project, a dependency's) has no uses indexed: there is nothing to
        // answer with, and an answer from the declaration alone would say "no uses".
        let held = self
            .indices
            .get(&key)
            .is_some_and(|i| i.holds(&canonical(&here)));
        if !held {
            return None;
        }
        if let Some(index) = self.indices.get_mut(&key) {
            index.first_refs();
        }
        // The file a file reads changed under it (a branch switch, a formatter, no save from
        // the editor), or this is the first request and the last check may be older than the
        // index: the last check names the declarations where they were. The check that
        // answers the position is made again, as the uses of its project are.
        if self
            .indices
            .get(&key)
            .is_some_and(|i| i.distrusts(&canonical(&here)))
        {
            self.check_one(uri);
        }
        Some((key, here))
    }

    /// Every use of the declaration the name at `at` stands for, across the files of its
    /// project: where each is written, and with `include_declaration` the declaration
    /// itself. `null` when the position resolves to no declaration the answer can name (not
    /// a name, a name the report has no declaration for, a declaration in the standard
    /// library, a buffer that is not the text the check read, a file nothing was checked
    /// for, a project whose files cannot be listed); `[]` when it resolves and nothing but
    /// the declaration uses it.
    fn references(
        &mut self,
        uri: &Uri,
        at: Position,
        include_declaration: bool,
    ) -> Option<Vec<Location>> {
        let (key, here) = self.project_ready(uri, "references")?;
        let (text, spelled, target) = {
            let checked = self.checked_buffer(uri)?;
            let lines = lines_of(&checked.text);
            let (line, col) = byte_position(&lines, at)?;
            let spelled = checked.types.at(&checked.text, line, col)?.declared?;
            let target = refs::Place::of(&spelled)?;
            (checked.text.clone(), spelled, target)
        };
        let here_canonical = canonical(&here);
        let mut round = 0;
        loop {
            self.refresh_refs(&key);
            let index = self.indices.get(&key)?;
            let class = index.same_as(&target);
            let mut hits: Vec<(&PathBuf, &symbols::Entry, &refs::Ref)> = Vec::new();
            for (canonical, e) in index.iter() {
                for r in e.refs.iter().flat_map(|f| &f.refs) {
                    if class.contains(&r.declared) {
                        hits.push((canonical, e, r));
                    }
                }
            }
            // The use that is the declaration's own name: the first use of it on the line the
            // checker names the declaration at (`function` of `function M.f`, the name of a
            // local, the type after `f:` in a record's body, whose name is before it). A
            // function declared in the body and defined below has two, one per place. A
            // declaration whose name the check did not resolve has none, and is added as the
            // point the checker names.
            let own: HashSet<usize> = class
                .iter()
                .filter_map(|p| {
                    hits.iter()
                        .enumerate()
                        .filter(|(_, (c, _, r))| {
                            **c == *p.file && r.declared == *p && r.line == p.line
                        })
                        .min_by_key(|(_, (_, _, r))| r.col)
                        .map(|(i, _)| i)
                })
                .collect();
            let mut out: Vec<Location> = Vec::new();
            if include_declaration
                && own.is_empty()
                && let Some(point) = location_of(
                    &here,
                    uri,
                    &spelled.file,
                    &text,
                    Some((spelled.line, spelled.col)),
                )
            {
                out.push(point);
            }
            let mut gone = false;
            for (i, (canonical, e, r)) in hits.iter().enumerate() {
                if own.contains(&i) && !include_declaration {
                    continue;
                }
                if !e.path.is_file() {
                    gone = true;
                    continue;
                }
                let target_uri = if **canonical == here_canonical {
                    Some(uri.clone())
                } else {
                    std::path::absolute(&e.path).ok().and_then(|p| uri_of(&p))
                };
                if let Some(target_uri) = target_uri {
                    out.push(Location {
                        uri: target_uri,
                        range: r.range,
                    });
                }
            }
            // A watcher names files, not the directory a file was in: a hit in a file that is
            // gone says the project is to be walked again, and the answer made once more.
            if gone && round == 0 {
                round += 1;
                if let Some(index) = self.indices.get_mut(&key) {
                    index.relist();
                }
                if let Err(e) = self.refresh_symbols(&key, &here) {
                    eprintln!("htl lsp: no references for {}: {e:#}", key.display());
                    return None;
                }
                continue;
            }
            return Some(out);
        }
    }

    /// The item of the function the name at `at` is, or is a use of: one, or `null` when the
    /// answer cannot be given as for references (the position resolves to no declaration,
    /// the buffer is not the text the check read, the file is not one the walk visits), or
    /// the declaration is not a function the tree of declarations holds (a local that does
    /// not hold one, a parameter, a local inside a function, a declaration in the standard
    /// library).
    fn prepare_calls(&mut self, uri: &Uri, at: Position) -> Option<Vec<CallHierarchyItem>> {
        let (key, here) = self.project_ready(uri, "call hierarchy")?;
        let here_canonical = canonical(&here);
        // The declaration's symbol: from the check that resolved the name when the
        // declaration is in the same file, so that its ranges are the buffer's; else from
        // the uses of the file it is in.
        let (target, mine) = {
            let checked = self.checked_buffer(uri)?;
            let lines = lines_of(&checked.text);
            let (line, col) = byte_position(&lines, at)?;
            let spelled = checked.types.at(&checked.text, line, col)?.declared?;
            let target = refs::Place::of(&spelled)?;
            let mine = (*target.file == *here_canonical).then(|| {
                checked
                    .types
                    .declarations()
                    .map(|d| symbols::flatten(&checked.text, d))
                    .unwrap_or_default()
            });
            (target, mine)
        };
        let place = (target.line, target.col);
        if let Some(syms) = mine {
            let sym = &syms[symbols::find(&syms, place)?];
            return sym
                .is_function()
                .then(|| vec![calls::item(uri.clone(), sym)]);
        }
        self.refresh_refs(&key);
        let e = self.indices.get(&key)?.entry(&target.file)?;
        let syms = &e.refs.as_ref()?.syms;
        let sym = &syms[symbols::find(syms, place)?];
        if !sym.is_function() || !e.path.is_file() {
            return None;
        }
        let uri = std::path::absolute(&e.path).ok().and_then(|p| uri_of(&p))?;
        Some(vec![calls::item(uri, sym)])
    }

    /// The functions that call the one `item` stands for, each with its call sites, and the
    /// top level of a file that calls it as a file ("Call hierarchy" in the module doc).
    /// `null` when the item's declaration is not there any more (the file changed, was
    /// deleted, or the data is not an item this server made), the item's file is open and
    /// its buffer is not the text the check read, or the project cannot be asked; `[]` for
    /// a function nothing calls.
    fn incoming_calls(
        &mut self,
        item: &CallHierarchyItem,
    ) -> Option<Vec<lsp_types::CallHierarchyIncomingCall>> {
        let (key, canonical_here) = self.calls_ready(item)?;
        // No `data` is no item of this server's, answered as one whose function is gone.
        let data = item.data.clone().unwrap_or_default();
        let root = canonical(&key);
        self.calls_answer(&key, &item.uri, |index| {
            let ctx = calls::Ctx {
                root: &root,
                here: &canonical_here,
                uri: &item.uri,
            };
            let (_, fr, which) = calls::resolve(index, &canonical_here, &data)?;
            // A file's top level is called by no one.
            let Some(i) = which else {
                return Some((Vec::new(), false));
            };
            Some(calls::incoming(
                index,
                &ctx,
                &calls::places(&canonical_here, &fr.syms[i]),
            ))
        })
    }

    /// The functions the one `item` stands for calls, each with the call sites in it; for
    /// the item of a file's top level, the functions its statements call. Answered `null`
    /// and `[]` as [`incoming_calls`](Self::incoming_calls) is.
    fn outgoing_calls(
        &mut self,
        item: &CallHierarchyItem,
    ) -> Option<Vec<lsp_types::CallHierarchyOutgoingCall>> {
        let (key, canonical_here) = self.calls_ready(item)?;
        // No `data` is no item of this server's, answered as one whose function is gone.
        let data = item.data.clone().unwrap_or_default();
        let root = canonical(&key);
        self.calls_answer(&key, &item.uri, |index| {
            let ctx = calls::Ctx {
                root: &root,
                here: &canonical_here,
                uri: &item.uri,
            };
            let (_, fr, which) = calls::resolve(index, &canonical_here, &data)?;
            Some(calls::outgoing(index, &ctx, fr, which))
        })
    }

    /// What a request made with an item has first ([`project_ready`](Self::project_ready), for
    /// the file of the item), and the rule of every answer made from a checked buffer: an
    /// item of an open file is answered only while the buffer is the text the check read,
    /// since its ranges are positions in it. Whether the file is open is decided by where
    /// the file really is, not by how the URI spells it: a symlinked path or a differently
    /// written URI of an open file is that file, with its unsaved edits. The project's key
    /// and the file, canonical.
    fn calls_ready(&mut self, item: &CallHierarchyItem) -> Option<(PathBuf, PathBuf)> {
        let canonical_here = canonical(&path_of(&item.uri)?);
        let open = self
            .open
            .iter()
            .find(|(_, d)| d.canonical == canonical_here)
            .map(|(u, _)| u.clone());
        let uri = open.clone().unwrap_or_else(|| item.uri.clone());
        let (key, _) = self.project_ready(&uri, "call hierarchy")?;
        if let Some(open) = open {
            self.checked_buffer(&open)?;
        }
        Some((key, canonical_here))
    }

    /// `answer` made from the uses of the project under `key`, once they are all made. An
    /// answer that found a use in a file that is gone says so, and the project is walked
    /// again and the answer made once more, as a references request does ("References" in
    /// the module doc: a watcher names files, not the directory a file was in).
    fn calls_answer<T>(
        &mut self,
        key: &Path,
        uri: &Uri,
        answer: impl Fn(&SymbolIndex) -> Option<(T, bool)>,
    ) -> Option<T> {
        let here = path_of(uri)?;
        let mut round = 0;
        loop {
            self.refresh_refs(key);
            let (out, gone) = match answer(self.indices.get(key)?) {
                Some(found) => found,
                None => {
                    eprintln!(
                        "htl lsp: no call hierarchy for an item of {}: it declares no such function",
                        here.display()
                    );
                    return None;
                }
            };
            if gone && round == 0 {
                round += 1;
                if let Some(index) = self.indices.get_mut(key) {
                    index.relist();
                }
                if let Err(e) = self.refresh_symbols(key, &here) {
                    eprintln!("htl lsp: no call hierarchy for {}: {e:#}", key.display());
                    return None;
                }
                continue;
            }
            return Some(out);
        }
    }

    /// Make the uses of every file of the project under `key` that has none: from the last
    /// check of the file when it is open and that check read the text on disk and what it
    /// read has not changed since, else from a check of the file as it is on disk. A project
    /// whose symbol index is not there ([`refresh_symbols`](Self::refresh_symbols) failed)
    /// has nothing made.
    fn refresh_refs(&mut self, key: &Path) {
        let Some(mut index) = self.indices.remove(key) else {
            return;
        };
        if let Some(project) = self.projects.get(key) {
            for p in index.pending() {
                let text = read_lossy(&p.path);
                let hash = symbols::hash_of(&text);
                let open = self.open.values().find(|d| d.canonical == p.canonical);
                let from_check = open
                    .filter(|_| !p.distrust)
                    .and_then(|d| d.checked.as_ref().map(|c| (d, c)))
                    .filter(|(_, c)| c.text == text)
                    .map(|(d, c)| refs::collect(&text, &c.types, d.deps.iter().cloned()));
                let made = from_check.or_else(|| match project.indexed(&p.path) {
                    // The text moved while the check ran: the positions are not its.
                    Ok(_) if read_lossy(&p.path) != text => None,
                    Ok(c) => Some(refs::collect(
                        &text,
                        &c.types,
                        c.deps.iter().map(|d| canonical(d)),
                    )),
                    // No uses, and no closure: the next change to any file asks again.
                    Err(e) => {
                        eprintln!("htl lsp: no references from {}: {e:#}", p.path.display());
                        Some(refs::FileRefs::default())
                    }
                });
                if let Some(made) = made {
                    index.fill(&p.canonical, hash, made);
                }
            }
        }
        self.indices.insert(key.to_path_buf(), index);
    }

    /// The edits that make the buffer what `htl fmt` writes for it: `[]` when it already is,
    /// one edit over the changed span when not, and `null` when `htl fmt` could not format
    /// it.
    fn formatting(&mut self, uri: &Uri) -> Option<Vec<TextEdit>> {
        let path = path_of(uri)?;
        let key = project_key(&path).unwrap_or_else(|_| htl::parent_dir(&path));
        // The buffer if the client has one; a file it has not opened is its disk. A buffer
        // the server lost track of is neither, and gets no edits.
        if self.open.get(uri).is_some_and(|d| d.unsynced) {
            eprintln!(
                "htl lsp: not formatting {}: its buffer is not known",
                path.display()
            );
            return None;
        }
        let buffer = self.open.get(uri).and_then(|d| d.buffer.clone());
        // A project whose manifests moved is built again for this answer; its open files
        // are checked against the new one first, as a check of any of them would have them,
        // or they would keep what the old one said with nothing left to notice the change.
        for other in self.drop_moved([key.clone()]) {
            self.check_one(&other);
        }
        let result = (|| {
            let before = match buffer {
                Some(text) => text,
                None => std::fs::read_to_string(&path)
                    .with_context(|| format!("reading {}", path.display()))?,
            };
            let after = self.project(&key, &path)?.format(&path, &before)?;
            Ok::<_, anyhow::Error>((before, after))
        })();
        match result {
            Ok((before, after)) => Some(minimal_edit(&before, &after).into_iter().collect()),
            // Not a formatting problem the client can do anything about (a file the parser
            // rejects is already a diagnostic): no edits, and the reason where the
            // server's other complaints go.
            Err(e) => {
                eprintln!("htl lsp: not formatting {}: {e:#}", path.display());
                None
            }
        }
    }

    /// The quick fixes for the findings at the request's range, and "fix all".
    fn code_actions(&mut self, p: &CodeActionParams) -> Vec<CodeActionOrCommand> {
        let uri = &p.text_document.uri;
        let wanted = |kind: &CodeActionKind| match &p.context.only {
            None => true,
            Some(only) => only.iter().any(|o| kind_within(kind, o)),
        };
        let quick = wanted(&CodeActionKind::QUICKFIX);
        // Fix-all costs a dry run of `htl fix`, so it is computed for a client that asked
        // for it: by kind (a code action on save), or by invoking the menu. A lightbulb
        // that asks on every cursor move asks for neither.
        let all = match &p.context.only {
            Some(only) => only.iter().any(|o| kind_within(&fix_all_kind(), o)),
            None => p.context.trigger_kind == Some(CodeActionTriggerKind::INVOKED),
        };
        let Some(doc) = self.open.get(uri).filter(|d| !d.unsynced) else {
            return Vec::new();
        };
        let (Some(checked), Some(key)) = (&doc.checked, doc.project.clone()) else {
            return Vec::new();
        };
        // A fix is positions in the text that was checked: the buffer must still be it
        // (module doc, "Formatting and code actions"). A buffer the server never saw
        // change is the disk, which is what the check read.
        let buffer = doc.buffer.clone().unwrap_or_else(|| read_lossy(&doc.path));
        if buffer != checked.text {
            return Vec::new();
        }
        let Some(project) = self.projects.get(&key) else {
            return Vec::new();
        };
        if project.fix_error.is_some() {
            return Vec::new();
        }
        let mut actions = Vec::new();
        if quick {
            for f in &checked.findings {
                let Some(fix) = &f.fix else { continue };
                if !overlaps(&f.lsp.range, &p.range) {
                    continue;
                }
                let class = project.fix.effective(&f.rule, fix);
                if class == htl::Applicability::Suggest {
                    continue;
                }
                // The text this fix alone leaves, which is `htl fix`'s to say.
                let Some(after) = htl::fix::apply_fix(&buffer, fix) else {
                    continue;
                };
                let Some(edit) = minimal_edit(&buffer, &after) else {
                    continue;
                };
                let applied = project.fix.disposition(&f.rule, fix)
                    == htl::fix::Disposition::Apply(htl::Applicability::Safe);
                let unsafe_ = class == htl::Applicability::Unsafe;
                actions.push(CodeActionOrCommand::CodeAction(CodeAction {
                    title: format!("Fix {}{}", f.rule, if unsafe_ { " (unsafe)" } else { "" }),
                    kind: Some(CodeActionKind::QUICKFIX),
                    diagnostics: Some(vec![f.lsp.clone()]),
                    edit: Some(edit_of(uri, vec![edit])),
                    is_preferred: Some(applied),
                    ..Default::default()
                }));
            }
        }
        // `htl fix` has something to apply only if a fix it applies is on file; without
        // one the dry run is not worth its cost.
        let applicable = checked.findings.iter().any(|f| {
            f.fix.as_ref().is_some_and(|fix| {
                matches!(
                    project.fix.disposition(&f.rule, fix),
                    htl::fix::Disposition::Apply(_)
                )
            })
        });
        // `fix_file` reads the disk, so the disk must be the text too.
        if all && applicable && read_lossy(&doc.path) == checked.text {
            let path = doc.path.clone();
            // A failure is said and not kept: the next request asks again.
            let fixed = match checked.fix_all.clone() {
                Some(known) => Some(known),
                None => match project.fixed(&path) {
                    Ok(known) => {
                        if let Some(doc) = self.open.get_mut(uri)
                            && let Some(checked) = &mut doc.checked
                        {
                            checked.fix_all = Some(known.clone());
                        }
                        Some(known)
                    }
                    Err(e) => {
                        eprintln!("htl lsp: no fix-all for {}: {e:#}", path.display());
                        None
                    }
                },
            }
            .flatten();
            if let Some(edit) = fixed.and_then(|text| minimal_edit(&buffer, &text)) {
                actions.push(CodeActionOrCommand::CodeAction(CodeAction {
                    title: "Fix all htl problems that are safe to fix".to_string(),
                    kind: Some(fix_all_kind()),
                    edit: Some(edit_of(uri, vec![edit])),
                    ..Default::default()
                }));
            }
        }
        actions
    }

    /// The project under `key`, loaded for `file` if the session has not met it yet. A
    /// project that failed to load is not remembered, so the next save tries again.
    ///
    /// Whether its manifests have changed is asked by the callers, before they come here
    /// ([`Self::drop_moved`]): a project rebuilt here would leave its other open files with
    /// what the old one said.
    fn project(&mut self, key: &Path, file: &Path) -> Result<&Project> {
        if !self.projects.contains_key(key) {
            let loaded = Project::load(file, key)?;
            self.projects.insert(key.to_path_buf(), loaded);
        }
        Ok(&self.projects[key])
    }

    fn publish(&self, uri: Uri, diagnostics: Vec<lsp_types::Diagnostic>) {
        let params = PublishDiagnosticsParams {
            uri,
            diagnostics,
            // The text checked is the file's, not a version of a buffer.
            version: None,
        };
        self.send(Notification::new(PublishDiagnostics::METHOD.to_string(), params).into());
    }

    fn send(&self, message: Message) {
        // A closed connection is noticed by the next read.
        let _ = self.connection.sender.send(message);
    }
}

/// What a changed file is to a project (module doc, "Files that change the answer").
#[derive(Clone, Copy, PartialEq, Eq)]
enum Watched {
    /// `htl.toml`, `mlua-pkg.toml` or `mlua-pkg.lock`.
    Manifest,
    /// A `.d.tl` under a directory named `types`.
    Types,
    /// A Rust source or `Cargo.toml` (whether `htl dts` reads it is [`Self::reads_rust`]).
    Rust,
}

impl Watched {
    fn of(path: &Path) -> Option<Self> {
        let name = path.file_name()?.to_str()?;
        if Manifests::names().contains(&name) {
            Some(Self::Manifest)
        } else if name.ends_with(".d.tl") && path.components().any(|c| c.as_os_str() == "types") {
            Some(Self::Types)
        } else if name == "Cargo.toml" || name.ends_with(".rs") {
            Some(Self::Rust)
        } else {
            None
        }
    }

    /// Whether `htl dts` reads `path` for the crate at `root`: its `Cargo.toml`, or a `.rs`
    /// under `src`, `examples`, `tests` or `benches` (the walk of
    /// [`htl::dts::generate_crate_to`]). The `.rs` files cargo writes under `target/` are
    /// not, which is most of the Rust events a build makes.
    fn reads_rust(path: &Path, root: &Path) -> bool {
        let Ok(rel) = path.strip_prefix(root) else {
            return false;
        };
        let mut parts = rel.components().map(|c| c.as_os_str());
        matches!(
            (parts.next().and_then(|p| p.to_str()), parts.next()),
            (Some("Cargo.toml"), None) | (Some("src" | "examples" | "tests" | "benches"), Some(_))
        )
    }
}

/// A fingerprint of the files under `root` that the checker resolves names against and no
/// walk of `.tl` files sees: the `.d.tl` under a `types` directory, and the Rust `htl dts`
/// reads (`Cargo.toml`, and `.rs` under `src`, `examples`, `tests` or `benches`), each by
/// path, modification time and length: what [`Watched`] names for a client that watches,
/// and a client that does not has this instead (module doc, "References").
fn project_inputs(root: &Path) -> u64 {
    fn walk(dir: &Path, root: &Path, out: &mut Vec<(PathBuf, Option<std::time::SystemTime>, u64)>) {
        let Ok(entries) = std::fs::read_dir(dir) else {
            return;
        };
        for e in entries.flatten() {
            let path = e.path();
            if path.is_dir() {
                if !htl::is_skipped_dir(&path, &[]) {
                    walk(&path, root, out);
                }
                continue;
            }
            let read = match Watched::of(&path) {
                Some(Watched::Types) => true,
                // Any crate under the root, not only one at the root: which crate an
                // `htl dts` run reads is not asked here.
                Some(Watched::Rust) => {
                    path.file_name().is_some_and(|n| n == "Cargo.toml")
                        || path.strip_prefix(root).is_ok_and(|rel| {
                            rel.components().any(|c| {
                                ["src", "examples", "tests", "benches"]
                                    .iter()
                                    .any(|d| c.as_os_str() == *d)
                            })
                        })
                }
                _ => false,
            };
            if read && let Ok(m) = e.metadata() {
                out.push((path, m.modified().ok(), m.len()));
            }
        }
    }
    let mut seen = Vec::new();
    walk(root, root, &mut seen);
    seen.sort();
    let mut h = DefaultHasher::new();
    seen.hash(&mut h);
    h.finish()
}

/// The 1-based line and byte column of an LSP position in `lines`; `None` for a line the
/// text does not have. A character past the end of its line is the end.
fn byte_position(lines: &[&str], at: Position) -> Option<(usize, usize)> {
    let text = lines.get(at.line as usize)?;
    let mut units = 0u32;
    let mut byte = text.len();
    for (i, c) in text.char_indices() {
        if units >= at.character {
            byte = i;
            break;
        }
        units += c.len_utf16() as u32;
    }
    Some((at.line as usize + 1, byte + 1))
}

/// The `require` site, among `sites`, whose module string the cursor (`line`, byte `col`)
/// is in, the quotes included and the end of the closing one too, as a cursor after the
/// last character is on it.
///
/// A site is where the checker saw the call; its string is the first quote after that
/// point on the line. A call spelled across lines is not found.
fn required_at<'a>(
    lines: &[&str],
    sites: &'a [htl::RequireSite],
    line: usize,
    col: usize,
) -> Option<&'a htl::RequireSite> {
    let text = lines.get(line - 1)?;
    sites.iter().filter(|s| s.line == line).find(|s| {
        let from = (s.col.saturating_sub(1)).min(text.len());
        let Some(open) = text.get(from..).and_then(|t| t.find(['"', '\''])) else {
            return false;
        };
        let open = from + open;
        let quote = text.as_bytes()[open];
        let close = text.as_bytes()[open + 1..]
            .iter()
            .position(|c| *c == quote)
            .map_or(text.len(), |p| open + 1 + p + 1);
        (open + 1..=close + 1).contains(&col)
    })
}

/// A `Location` for `file`, as a report spells it (relative to the working directory, or
/// absolute, `./` and all), at `place` (1-based line and byte column) or at the start. The
/// file the request was made in answers with the URI the client used for it. `None` for a
/// file that is not on disk: the standard library's declarations are named by a file Teal
/// carries inside itself.
fn location_of(
    here: &Path,
    uri: &Uri,
    file: &Path,
    text: &str,
    place: Option<(usize, usize)>,
) -> Option<Location> {
    let mut path = PathBuf::new();
    for part in std::env::current_dir()
        .unwrap_or_default()
        .join(file)
        .components()
    {
        if part != std::path::Component::CurDir {
            path.push(part);
        }
    }
    let target = if path == here {
        uri.clone()
    } else if path.is_file() {
        uri_of(&path)?
    } else {
        return None;
    };
    let start = match place {
        None => Position::new(0, 0),
        Some((line, col)) => {
            let theirs;
            let theirs_text = if path == here {
                text
            } else {
                theirs = read_lossy(&path);
                &theirs
            };
            range_at(&lines_of(theirs_text), line, col).start
        }
    };
    Some(Location {
        uri: target,
        range: Range::new(start, start),
    })
}

/// A notification's parameters, or a line on stderr saying they were not what the method
/// takes.
fn params<P: serde::de::DeserializeOwned>(n: Notification) -> Option<P> {
    match serde_json::from_value(n.params) {
        Ok(p) => Some(p),
        Err(e) => {
            eprintln!("htl lsp: malformed {}: {e}", n.method);
            None
        }
    }
}

/// Whether `path` names a Teal source (`m.tl`, not the declaration file `m.d.tl`).
fn is_tl(path: &Path) -> bool {
    path.file_name()
        .and_then(|n| n.to_str())
        .is_some_and(|n| n.ends_with(".tl") && !n.ends_with(".d.tl"))
}

/// The folders a client opened the workspace on: `workspaceFolders`, else `rootUri`, else
/// `rootPath`, in the `initialize` parameters.
fn workspace_of(params: &serde_json::Value) -> Vec<PathBuf> {
    let uri_path = |v: &serde_json::Value| {
        v.as_str()
            .and_then(|u| Uri::from_str(u).ok())
            .and_then(|u| path_of(&u))
    };
    let folders: Vec<PathBuf> = params
        .get("workspaceFolders")
        .and_then(serde_json::Value::as_array)
        .map(|a| a.iter().filter_map(|f| uri_path(&f["uri"])).collect())
        .unwrap_or_default();
    if !folders.is_empty() {
        return folders;
    }
    if let Some(root) = uri_path(&params["rootUri"]) {
        return vec![root];
    }
    params["rootPath"]
        .as_str()
        .map(|p| vec![PathBuf::from(p)])
        .unwrap_or_default()
}

/// The file a `file:` URI names, percent-decoded. `None` for any other scheme.
fn path_of(uri: &Uri) -> Option<PathBuf> {
    url::Url::parse(uri.as_str()).ok()?.to_file_path().ok()
}

/// The `file:` URI of a path, percent-encoded.
fn uri_of(path: &Path) -> Option<Uri> {
    let url = url::Url::from_file_path(path).ok()?;
    Uri::from_str(url.as_str()).ok()
}

/// Which project `file` belongs to: its root, or the file's directory when it is in none.
fn project_key(file: &Path) -> Result<PathBuf> {
    Ok(match htl::model::Project::find_root(file)? {
        Some(found) => found.root,
        None => htl::parent_dir(file),
    })
}

fn canonical(p: &Path) -> PathBuf {
    std::fs::canonicalize(p).unwrap_or_else(|_| p.to_path_buf())
}

/// `p` in its canonical directory, with its own name kept: a file a watcher reports may be
/// gone (a delete), and a manifest that is a symlink is still the manifest of the directory
/// it is in, not of the one it points into.
fn canonical_dir_of(p: &Path) -> PathBuf {
    match (p.parent(), p.file_name()) {
        (Some(dir), Some(name)) => canonical(dir).join(name),
        _ => p.to_path_buf(),
    }
}

/// The diagnostic for a check that could not be made (an `htl.toml` that does not parse,
/// a toolchain pin this binary does not satisfy): the reason, at the top of the file.
fn failure(e: &anyhow::Error) -> lsp_types::Diagnostic {
    lsp_types::Diagnostic {
        range: Range::default(),
        severity: Some(DiagnosticSeverity::ERROR),
        source: Some("htl".to_string()),
        message: format!("{e:#}"),
        ..Default::default()
    }
}

/// `htl check`'s findings for `file`, as the client shows them.
fn convert(
    file: &Path,
    text: &str,
    found: &[htl::Diagnostic],
    levels: &htl::lint::Selection,
    policy: &htl::verdict::Policy,
) -> Vec<Finding> {
    // What the positions are read against: the file as the check saw it.
    let lines = lines_of(text);
    let cwd = std::env::current_dir().unwrap_or_default();
    let file_canonical = canonical(file);
    found
        .iter()
        .map(|d| {
            // A report spells a path relative to the working directory when it is under
            // it ([`htl::Diagnostic::spelled`]); undone to compare it with the file.
            let at = cwd.join(&d.file);
            let own = d.file.is_empty() || at == file || canonical(&at) == file_canonical;
            let (range, related) = if own {
                (range_at(&lines, d.line, d.col), None)
            } else {
                // A module the file requires: its position is in that module, and the
                // requiring file has nowhere better to show it than its first line. The
                // column is converted against that module's text, as the file's own are.
                let related = uri_of(&at).map(|uri| {
                    let theirs = read_lossy(&at);
                    let start = range_at(&lines_of(&theirs), d.line, d.col).start;
                    vec![DiagnosticRelatedInformation {
                        location: Location {
                            uri,
                            range: Range::new(start, start),
                        },
                        message: d.message.clone(),
                    }]
                });
                (Range::default(), related)
            };
            let message = if own {
                d.message.clone()
            } else {
                format!("{}:{}:{}: {}", d.file, d.line, d.col, d.message)
            };
            let lsp = lsp_types::Diagnostic {
                range,
                severity: Some(severity(d, levels, policy)),
                code: d.rule.clone().map(NumberOrString::String),
                source: Some("htl".to_string()),
                message,
                related_information: related,
                ..Default::default()
            };
            // The fixes `htl fix` looks at are the file's own errors' and lints' (a Teal
            // warning is none of them), named by the rule or, for an error the checker
            // gave none, by `tl:error` (`htl_core::fix`'s `rule_of`).
            let fix = d
                .fix
                .clone()
                .filter(|_| own && d.severity != htl::Severity::Warning);
            Finding {
                lsp,
                fix,
                rule: htl::fix::rule_of(d),
            }
        })
        .collect()
}

/// A file's text for reading positions against; empty when it cannot be read.
fn read_lossy(file: &Path) -> String {
    std::fs::read(file)
        .map(|b| String::from_utf8_lossy(&b).into_owned())
        .unwrap_or_default()
}

/// `text`'s lines as the checker numbers them, a `\r` before each `\n` left out.
fn lines_of(text: &str) -> Vec<&str> {
    text.split('\n')
        .map(|l| l.strip_suffix('\r').unwrap_or(l))
        .collect()
}

/// The kind of the action that applies every fix `htl fix` would.
fn fix_all_kind() -> CodeActionKind {
    CodeActionKind::new("source.fixAll.htl")
}

/// Whether `kind` is `only` or one of its sub-kinds (`source.fixAll.htl` is within
/// `source.fixAll` and `source`, not within `source.fix`): the filter's rule.
fn kind_within(kind: &CodeActionKind, only: &CodeActionKind) -> bool {
    let (kind, only) = (kind.as_str(), only.as_str());
    kind == only || kind.strip_prefix(only).is_some_and(|r| r.starts_with('.'))
}

/// Whether two ranges share a point, the ends included: a cursor at either end of a
/// finding is on it, and an empty range (a cursor) is a point.
fn overlaps(a: &Range, b: &Range) -> bool {
    a.start <= b.end && b.start <= a.end
}

/// A `WorkspaceEdit` of `edits` in the one file `uri`.
fn edit_of(uri: &Uri, edits: Vec<TextEdit>) -> WorkspaceEdit {
    WorkspaceEdit {
        changes: Some(HashMap::from([(uri.clone(), edits)])),
        ..Default::default()
    }
}

/// One edit that turns `before` into `after`, over the span in which they differ: the
/// text they share at the start and at the end is left out, so an editor keeps its cursors,
/// folds and marks everywhere else. `None` when they are the same text.
///
/// Positions are 0-based lines and UTF-16 columns of `before`, its lines ended by `\n`,
/// `\r\n` or a lone `\r` as LSP counts them. The span is kept off the middle of a `\r\n`,
/// which a position is not allowed to be in.
fn minimal_edit(before: &str, after: &str) -> Option<TextEdit> {
    if before == after {
        return None;
    }
    let mut start = before
        .bytes()
        .zip(after.bytes())
        .take_while(|(a, b)| a == b)
        .count();
    while !before.is_char_boundary(start) || !after.is_char_boundary(start) {
        start -= 1;
    }
    if start > 0
        && before.as_bytes()[start - 1] == b'\r'
        && before.as_bytes().get(start) == Some(&b'\n')
    {
        start -= 1;
    }
    let (rb, ra) = (&before.as_bytes()[start..], &after.as_bytes()[start..]);
    let mut shared = rb
        .iter()
        .rev()
        .zip(ra.iter().rev())
        .take_while(|(a, b)| a == b)
        .count();
    // The tail starts on a character of both, and at a `\n` only if its `\r` is in it too.
    while shared > 0
        && (!before.is_char_boundary(before.len() - shared)
            || !after.is_char_boundary(after.len() - shared)
            || (before.as_bytes()[before.len() - shared] == b'\n'
                && before.len() - shared > start
                && before.as_bytes()[before.len() - shared - 1] == b'\r'))
    {
        shared -= 1;
    }
    let (end_before, end_after) = (before.len() - shared, after.len() - shared);
    // A line ends at `\n`, `\r\n` or a lone `\r`: LSP counts all three, so a client
    // reads a position past a lone `\r` as on the next line.
    let at = |upto: usize| {
        let b = before.as_bytes();
        let (mut line, mut line_start) = (0u32, 0usize);
        for i in 0..upto {
            if b[i] == b'\n' || (b[i] == b'\r' && b.get(i + 1) != Some(&b'\n')) {
                line += 1;
                line_start = i + 1;
            }
        }
        let col = before[line_start..upto].encode_utf16().count();
        Position::new(line, col as u32)
    };
    Some(TextEdit::new(
        Range::new(at(start), at(end_before)),
        after[start..end_after].to_string(),
    ))
}

/// An error is an error. A Teal warning and an htl lint are warnings, unless they are what
/// makes `htl check` fail ([`htl::verdict::verdict`]): their rule is at `deny`, or the
/// project is `strict` and every finding fails it.
fn severity(
    d: &htl::Diagnostic,
    levels: &htl::lint::Selection,
    policy: &htl::verdict::Policy,
) -> DiagnosticSeverity {
    match d.severity {
        htl::Severity::Error => DiagnosticSeverity::ERROR,
        htl::Severity::Warning | htl::Severity::Lint
            if !policy.capped && (policy.strict || htl::verdict::is_denied(d, levels)) =>
        {
            DiagnosticSeverity::ERROR
        }
        htl::Severity::Warning | htl::Severity::Lint => DiagnosticSeverity::WARNING,
    }
}

/// The LSP position of a point the checker reports: `line` counted from 1 and `col` from 1
/// in bytes, as a 0-based line and a UTF-16 column. A column past the end of the line is its
/// end, and a line past the end of the text is its last line's.
fn position_at(lines: &[&str], line: usize, col: usize) -> Position {
    let (n, text, at) = clamped(lines, line, col);
    Position::new(n as u32, text[..at].encode_utf16().count() as u32)
}

/// The point at `line` (from 1) and byte `col` (from 1) as the 0-based line, its text and a
/// byte offset in it on a character boundary: a column past the end is the end, one inside a
/// character the start of that character, and a line past the end of the text the last line.
fn clamped<'a>(lines: &[&'a str], line: usize, col: usize) -> (usize, &'a str, usize) {
    let n = line.saturating_sub(1).min(lines.len().saturating_sub(1));
    let text = lines.get(n).copied().unwrap_or("");
    let mut at = col.saturating_sub(1).min(text.len());
    while !text.is_char_boundary(at) {
        at -= 1;
    }
    (n, text, at)
}

/// One declaration of the index as an LSP `DocumentSymbol`, its children with it.
///
/// The kinds: a record is a `Struct`, an interface an `Interface`, an enum an `Enum`; a
/// type alias a `TypeParameter` (rust-analyzer's choice for one); a variable a `Variable`; a
/// record's field that is not a function a `Field`; any function, `function R.f` and a
/// function-typed field included, a `Function`, and `function R:m` (or a field typed as a
/// method) a `Method`. `detail` is Teal's type string. `range` is the declaration's extent as
/// far as the index knows it ([`htl::typeindex`], "Declarations"; an unknown end is the end of
/// the line) and `selectionRange` is the name, which `range` is made to contain.
fn symbol_of(lines: &[&str], d: &htl::typeindex::Decl) -> DocumentSymbol {
    let start = position_at(lines, d.line, d.col);
    let end = position_at(lines, d.end_line, d.end_col.unwrap_or(usize::MAX));
    let name_start = position_at(lines, d.name_line, d.name_col);
    let name_end = position_at(
        lines,
        d.name_line,
        d.name_col.saturating_add(d.short_name().len()),
    );
    let children: Vec<DocumentSymbol> = d.children.iter().map(|c| symbol_of(lines, c)).collect();
    #[allow(deprecated)]
    DocumentSymbol {
        name: d.name.clone(),
        detail: d.detail.clone(),
        kind: symbols::kind_of(d.kind),
        tags: None,
        deprecated: None,
        range: Range::new(start.min(name_start), end.max(name_end)),
        selection_range: Range::new(name_start, name_end),
        children: (!children.is_empty()).then_some(children),
    }
}

/// The range for the point a check reports: `line` counted from 1 and `col` from 1 in
/// bytes (the checker counts bytes), as LSP's 0-based line and UTF-16 column. The range
/// runs to the end of the identifier that starts at the point, or over one character if
/// what is there is not part of one, and is empty at the end of a line. No position
/// (`line` 0) is the start of the file.
fn range_at(lines: &[&str], line: usize, col: usize) -> Range {
    if line == 0 {
        return Range::default();
    }
    let (n, text, start) = clamped(lines, line, col);
    let word = |c: char| c.is_alphanumeric() || c == '_';
    let mut end = start;
    let mut rest = text[start..].chars();
    if let Some(first) = rest.next() {
        end += first.len_utf8();
        if word(first) {
            end += rest
                .take_while(|c| word(*c))
                .map(char::len_utf8)
                .sum::<usize>();
        }
    }
    let units = |upto: usize| text[..upto].encode_utf16().count() as u32;
    Range::new(
        Position::new(n as u32, units(start)),
        Position::new(n as u32, units(end)),
    )
}

/// The temp directory the integration tests use, linked in by path as
/// `tests/common/mod.rs` does ([`core_common::tempdir`] says what it promises).
#[cfg(test)]
#[allow(dead_code)]
#[path = "../../htl-core/tests/common/mod.rs"]
mod core_common;

#[cfg(test)]
mod tests {
    use super::*;
    use lsp_server::Request;
    use std::time::Duration;

    #[test]
    fn a_column_in_bytes_becomes_a_column_in_utf16_units() {
        // `é` is two bytes and one unit, the emoji four bytes and two units: `n` is at
        // byte 26 and unit 23.
        let lines = ["local s = \"é😀\"; local n"];
        let byte = lines[0].rfind('n').unwrap();
        assert_eq!(byte, 26);
        let r = range_at(&lines, 1, byte + 1);
        assert_eq!(r.start, Position::new(0, 23));
        assert_eq!(r.end, Position::new(0, 24));
    }

    #[test]
    fn a_range_covers_the_identifier_that_starts_at_the_point() {
        let lines = ["local name_1 = 3", ""];
        let r = range_at(&lines, 1, 7);
        assert_eq!(
            (r.start, r.end),
            (Position::new(0, 6), Position::new(0, 12))
        );
        // A symbol is one character; the end of a line is empty; no position is 0:0.
        let r = range_at(&lines, 1, 13);
        assert_eq!(
            (r.start, r.end),
            (Position::new(0, 12), Position::new(0, 13))
        );
        let r = range_at(&lines, 2, 1);
        assert_eq!((r.start, r.end), (Position::new(1, 0), Position::new(1, 0)));
        assert_eq!(range_at(&lines, 0, 0), Range::default());
    }

    #[test]
    fn a_column_past_the_line_or_inside_a_character_is_clamped() {
        let lines = ["é"];
        let r = range_at(&lines, 1, 2);
        assert_eq!((r.start, r.end), (Position::new(0, 0), Position::new(0, 1)));
        let r = range_at(&lines, 1, 40);
        assert_eq!((r.start, r.end), (Position::new(0, 1), Position::new(0, 1)));
    }

    #[test]
    fn a_path_and_its_uri_name_each_other() {
        let path = Path::new("/work/my project/é.tl");
        let uri = uri_of(path).unwrap();
        assert_eq!(uri.as_str(), "file:///work/my%20project/%C3%A9.tl");
        assert_eq!(path_of(&uri).as_deref(), Some(path));
        assert_eq!(
            path_of(&Uri::from_str("untitled:Untitled-1").unwrap()),
            None
        );
    }

    /// `edit` applied to `before`, by the positions it carries.
    fn applied(before: &str, edit: &TextEdit) -> String {
        let at = |p: Position| {
            let line_start: usize = before
                .split('\n')
                .take(p.line as usize)
                .map(|l| l.len() + 1)
                .sum();
            let mut units = 0;
            let mut off = line_start;
            for c in before[line_start..].chars() {
                if units >= p.character || c == '\n' {
                    break;
                }
                units += c.len_utf16() as u32;
                off += c.len_utf8();
            }
            off
        };
        let mut out = before.to_string();
        out.replace_range(at(edit.range.start)..at(edit.range.end), &edit.new_text);
        out
    }

    #[test]
    fn an_edit_covers_only_the_span_that_differs() {
        assert!(minimal_edit("a\nb\n", "a\nb\n").is_none());
        let e = minimal_edit("a\nbcd\ne\n", "a\nbXd\ne\n").unwrap();
        assert_eq!(e.new_text, "X");
        assert_eq!(
            (e.range.start, e.range.end),
            (Position::new(1, 1), Position::new(1, 2))
        );
        // An insertion is an empty range; a deletion an empty text.
        let e = minimal_edit("ab", "aXb").unwrap();
        assert_eq!(
            (e.range.start, e.range.end),
            (Position::new(0, 1), Position::new(0, 1))
        );
        let e = minimal_edit("aXb", "ab").unwrap();
        assert_eq!(
            (e.new_text.as_str(), e.range.end),
            ("", Position::new(0, 2))
        );
        // Repeated text: the shared prefix and suffix may not overlap.
        let e = minimal_edit("aaa", "aa").unwrap();
        assert_eq!(applied("aaa", &e), "aa");
    }

    #[test]
    fn an_edit_is_in_utf16_and_not_in_the_middle_of_a_character_or_a_crlf() {
        // `é` and the emoji share their first bytes with other characters.
        for (a, b) in [
            ("x \u{e9}y", "x \u{e8}y"),
            ("\u{1F600}a", "\u{1F601}a"),
            ("a\u{1F600}", "a\u{1F601}"),
            ("l1\r\nl2\r\n", "l1\r\nl2\r\nl3\r\n"),
            ("l1\r\nl2\r\n", "l1\nl2\n"),
            ("a\r\nb", "a\r\n\r\nb"),
            ("a\r\n", "a\r\n\r\n"),
            ("", "x"),
            ("x", ""),
        ] {
            let e = minimal_edit(a, b).unwrap();
            assert_eq!(applied(a, &e), b, "{a:?} -> {b:?}: {e:?}");
        }
        let e = minimal_edit("\u{1F600}a", "\u{1F601}a").unwrap();
        assert_eq!(
            (e.range.start, e.range.end),
            (Position::new(0, 0), Position::new(0, 2))
        );
        // No position between the `\r` and the `\n` of a line end.
        let e = minimal_edit("l1\r\nl2\r\n", "l1\r\nl2\r\nl3\r\n").unwrap();
        assert_eq!(e.range.start, Position::new(2, 0));
        // A lone `\r` ends a line for the client: past it is the next line's start.
        let e = minimal_edit("a\rb", "a\r\nb").unwrap();
        assert_eq!(
            (e.range.start, e.new_text.as_str()),
            (Position::new(1, 0), "\n")
        );
    }

    fn notify(method: &str, params: serde_json::Value) -> Message {
        Message::Notification(Notification {
            method: method.to_string(),
            params,
        })
    }

    /// Saves of one file that are queued together are one check. The server is started
    /// after the messages are in its channel, so they are one batch however the threads
    /// are scheduled.
    #[test]
    fn saves_queued_together_are_checked_once() {
        let dir = super::core_common::tempdir("htl-cli-lsp", "superseded");
        std::fs::write(dir.join("htl.toml"), "[layout]\nsource = \".\"\n").unwrap();
        std::fs::write(dir.join("a.tl"), "local n: number = \"x\"\nreturn n\n").unwrap();
        let uri = uri_of(&dir.join("a.tl")).unwrap();
        let doc = serde_json::json!({ "textDocument": { "uri": uri.as_str() } });

        let (server_side, client) = Connection::memory();
        let send = |m: Message| client.sender.send(m).unwrap();
        send(Message::Request(Request::new(
            1.into(),
            "initialize".to_string(),
            serde_json::json!({ "capabilities": {} }),
        )));
        send(notify("initialized", serde_json::json!({})));
        send(notify(
            "textDocument/didOpen",
            serde_json::json!({ "textDocument": {
                "uri": uri.as_str(), "languageId": "teal", "version": 1, "text": ""
            } }),
        ));
        for _ in 0..3 {
            send(notify("textDocument/didSave", doc.clone()));
        }
        // Started once everything is queued: the open and the three saves are one batch.
        let server = std::thread::spawn(move || run_on(&server_side).unwrap());
        let is_publish = |m: &Message| matches!(m, Message::Notification(n) if n.method == PublishDiagnostics::METHOD);
        // The first publish is waited for; nothing is waiting behind it.
        loop {
            let m = client
                .receiver
                .recv_timeout(Duration::from_secs(60))
                .unwrap();
            if is_publish(&m) {
                break;
            }
        }
        send(Message::Request(Request::new(
            2.into(),
            "shutdown".to_string(),
            serde_json::Value::Null,
        )));
        send(notify("exit", serde_json::Value::Null));
        let code = server.join().unwrap();
        assert_eq!(format!("{code:?}"), format!("{:?}", ExitCode::SUCCESS));
        // The server has gone, so anything it was going to say is in the channel: a second
        // check of the file would be a second publish.
        let rest: Vec<Message> = client.receiver.try_iter().collect();
        assert!(!rest.iter().any(is_publish), "{rest:?}");
    }
}