aboutsummaryrefslogtreecommitdiffstats
path: root/elpa/lsp-mode-20220505.630/lsp-mode.el
blob: f5602950ba6b120fb18e78cd719b1b7fbc5e5843 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
5894
5895
5896
5897
5898
5899
5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
5911
5912
5913
5914
5915
5916
5917
5918
5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947
5948
5949
5950
5951
5952
5953
5954
5955
5956
5957
5958
5959
5960
5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
5973
5974
5975
5976
5977
5978
5979
5980
5981
5982
5983
5984
5985
5986
5987
5988
5989
5990
5991
5992
5993
5994
5995
5996
5997
5998
5999
6000
6001
6002
6003
6004
6005
6006
6007
6008
6009
6010
6011
6012
6013
6014
6015
6016
6017
6018
6019
6020
6021
6022
6023
6024
6025
6026
6027
6028
6029
6030
6031
6032
6033
6034
6035
6036
6037
6038
6039
6040
6041
6042
6043
6044
6045
6046
6047
6048
6049
6050
6051
6052
6053
6054
6055
6056
6057
6058
6059
6060
6061
6062
6063
6064
6065
6066
6067
6068
6069
6070
6071
6072
6073
6074
6075
6076
6077
6078
6079
6080
6081
6082
6083
6084
6085
6086
6087
6088
6089
6090
6091
6092
6093
6094
6095
6096
6097
6098
6099
6100
6101
6102
6103
6104
6105
6106
6107
6108
6109
6110
6111
6112
6113
6114
6115
6116
6117
6118
6119
6120
6121
6122
6123
6124
6125
6126
6127
6128
6129
6130
6131
6132
6133
6134
6135
6136
6137
6138
6139
6140
6141
6142
6143
6144
6145
6146
6147
6148
6149
6150
6151
6152
6153
6154
6155
6156
6157
6158
6159
6160
6161
6162
6163
6164
6165
6166
6167
6168
6169
6170
6171
6172
6173
6174
6175
6176
6177
6178
6179
6180
6181
6182
6183
6184
6185
6186
6187
6188
6189
6190
6191
6192
6193
6194
6195
6196
6197
6198
6199
6200
6201
6202
6203
6204
6205
6206
6207
6208
6209
6210
6211
6212
6213
6214
6215
6216
6217
6218
6219
6220
6221
6222
6223
6224
6225
6226
6227
6228
6229
6230
6231
6232
6233
6234
6235
6236
6237
6238
6239
6240
6241
6242
6243
6244
6245
6246
6247
6248
6249
6250
6251
6252
6253
6254
6255
6256
6257
6258
6259
6260
6261
6262
6263
6264
6265
6266
6267
6268
6269
6270
6271
6272
6273
6274
6275
6276
6277
6278
6279
6280
6281
6282
6283
6284
6285
6286
6287
6288
6289
6290
6291
6292
6293
6294
6295
6296
6297
6298
6299
6300
6301
6302
6303
6304
6305
6306
6307
6308
6309
6310
6311
6312
6313
6314
6315
6316
6317
6318
6319
6320
6321
6322
6323
6324
6325
6326
6327
6328
6329
6330
6331
6332
6333
6334
6335
6336
6337
6338
6339
6340
6341
6342
6343
6344
6345
6346
6347
6348
6349
6350
6351
6352
6353
6354
6355
6356
6357
6358
6359
6360
6361
6362
6363
6364
6365
6366
6367
6368
6369
6370
6371
6372
6373
6374
6375
6376
6377
6378
6379
6380
6381
6382
6383
6384
6385
6386
6387
6388
6389
6390
6391
6392
6393
6394
6395
6396
6397
6398
6399
6400
6401
6402
6403
6404
6405
6406
6407
6408
6409
6410
6411
6412
6413
6414
6415
6416
6417
6418
6419
6420
6421
6422
6423
6424
6425
6426
6427
6428
6429
6430
6431
6432
6433
6434
6435
6436
6437
6438
6439
6440
6441
6442
6443
6444
6445
6446
6447
6448
6449
6450
6451
6452
6453
6454
6455
6456
6457
6458
6459
6460
6461
6462
6463
6464
6465
6466
6467
6468
6469
6470
6471
6472
6473
6474
6475
6476
6477
6478
6479
6480
6481
6482
6483
6484
6485
6486
6487
6488
6489
6490
6491
6492
6493
6494
6495
6496
6497
6498
6499
6500
6501
6502
6503
6504
6505
6506
6507
6508
6509
6510
6511
6512
6513
6514
6515
6516
6517
6518
6519
6520
6521
6522
6523
6524
6525
6526
6527
6528
6529
6530
6531
6532
6533
6534
6535
6536
6537
6538
6539
6540
6541
6542
6543
6544
6545
6546
6547
6548
6549
6550
6551
6552
6553
6554
6555
6556
6557
6558
6559
6560
6561
6562
6563
6564
6565
6566
6567
6568
6569
6570
6571
6572
6573
6574
6575
6576
6577
6578
6579
6580
6581
6582
6583
6584
6585
6586
6587
6588
6589
6590
6591
6592
6593
6594
6595
6596
6597
6598
6599
6600
6601
6602
6603
6604
6605
6606
6607
6608
6609
6610
6611
6612
6613
6614
6615
6616
6617
6618
6619
6620
6621
6622
6623
6624
6625
6626
6627
6628
6629
6630
6631
6632
6633
6634
6635
6636
6637
6638
6639
6640
6641
6642
6643
6644
6645
6646
6647
6648
6649
6650
6651
6652
6653
6654
6655
6656
6657
6658
6659
6660
6661
6662
6663
6664
6665
6666
6667
6668
6669
6670
6671
6672
6673
6674
6675
6676
6677
6678
6679
6680
6681
6682
6683
6684
6685
6686
6687
6688
6689
6690
6691
6692
6693
6694
6695
6696
6697
6698
6699
6700
6701
6702
6703
6704
6705
6706
6707
6708
6709
6710
6711
6712
6713
6714
6715
6716
6717
6718
6719
6720
6721
6722
6723
6724
6725
6726
6727
6728
6729
6730
6731
6732
6733
6734
6735
6736
6737
6738
6739
6740
6741
6742
6743
6744
6745
6746
6747
6748
6749
6750
6751
6752
6753
6754
6755
6756
6757
6758
6759
6760
6761
6762
6763
6764
6765
6766
6767
6768
6769
6770
6771
6772
6773
6774
6775
6776
6777
6778
6779
6780
6781
6782
6783
6784
6785
6786
6787
6788
6789
6790
6791
6792
6793
6794
6795
6796
6797
6798
6799
6800
6801
6802
6803
6804
6805
6806
6807
6808
6809
6810
6811
6812
6813
6814
6815
6816
6817
6818
6819
6820
6821
6822
6823
6824
6825
6826
6827
6828
6829
6830
6831
6832
6833
6834
6835
6836
6837
6838
6839
6840
6841
6842
6843
6844
6845
6846
6847
6848
6849
6850
6851
6852
6853
6854
6855
6856
6857
6858
6859
6860
6861
6862
6863
6864
6865
6866
6867
6868
6869
6870
6871
6872
6873
6874
6875
6876
6877
6878
6879
6880
6881
6882
6883
6884
6885
6886
6887
6888
6889
6890
6891
6892
6893
6894
6895
6896
6897
6898
6899
6900
6901
6902
6903
6904
6905
6906
6907
6908
6909
6910
6911
6912
6913
6914
6915
6916
6917
6918
6919
6920
6921
6922
6923
6924
6925
6926
6927
6928
6929
6930
6931
6932
6933
6934
6935
6936
6937
6938
6939
6940
6941
6942
6943
6944
6945
6946
6947
6948
6949
6950
6951
6952
6953
6954
6955
6956
6957
6958
6959
6960
6961
6962
6963
6964
6965
6966
6967
6968
6969
6970
6971
6972
6973
6974
6975
6976
6977
6978
6979
6980
6981
6982
6983
6984
6985
6986
6987
6988
6989
6990
6991
6992
6993
6994
6995
6996
6997
6998
6999
7000
7001
7002
7003
7004
7005
7006
7007
7008
7009
7010
7011
7012
7013
7014
7015
7016
7017
7018
7019
7020
7021
7022
7023
7024
7025
7026
7027
7028
7029
7030
7031
7032
7033
7034
7035
7036
7037
7038
7039
7040
7041
7042
7043
7044
7045
7046
7047
7048
7049
7050
7051
7052
7053
7054
7055
7056
7057
7058
7059
7060
7061
7062
7063
7064
7065
7066
7067
7068
7069
7070
7071
7072
7073
7074
7075
7076
7077
7078
7079
7080
7081
7082
7083
7084
7085
7086
7087
7088
7089
7090
7091
7092
7093
7094
7095
7096
7097
7098
7099
7100
7101
7102
7103
7104
7105
7106
7107
7108
7109
7110
7111
7112
7113
7114
7115
7116
7117
7118
7119
7120
7121
7122
7123
7124
7125
7126
7127
7128
7129
7130
7131
7132
7133
7134
7135
7136
7137
7138
7139
7140
7141
7142
7143
7144
7145
7146
7147
7148
7149
7150
7151
7152
7153
7154
7155
7156
7157
7158
7159
7160
7161
7162
7163
7164
7165
7166
7167
7168
7169
7170
7171
7172
7173
7174
7175
7176
7177
7178
7179
7180
7181
7182
7183
7184
7185
7186
7187
7188
7189
7190
7191
7192
7193
7194
7195
7196
7197
7198
7199
7200
7201
7202
7203
7204
7205
7206
7207
7208
7209
7210
7211
7212
7213
7214
7215
7216
7217
7218
7219
7220
7221
7222
7223
7224
7225
7226
7227
7228
7229
7230
7231
7232
7233
7234
7235
7236
7237
7238
7239
7240
7241
7242
7243
7244
7245
7246
7247
7248
7249
7250
7251
7252
7253
7254
7255
7256
7257
7258
7259
7260
7261
7262
7263
7264
7265
7266
7267
7268
7269
7270
7271
7272
7273
7274
7275
7276
7277
7278
7279
7280
7281
7282
7283
7284
7285
7286
7287
7288
7289
7290
7291
7292
7293
7294
7295
7296
7297
7298
7299
7300
7301
7302
7303
7304
7305
7306
7307
7308
7309
7310
7311
7312
7313
7314
7315
7316
7317
7318
7319
7320
7321
7322
7323
7324
7325
7326
7327
7328
7329
7330
7331
7332
7333
7334
7335
7336
7337
7338
7339
7340
7341
7342
7343
7344
7345
7346
7347
7348
7349
7350
7351
7352
7353
7354
7355
7356
7357
7358
7359
7360
7361
7362
7363
7364
7365
7366
7367
7368
7369
7370
7371
7372
7373
7374
7375
7376
7377
7378
7379
7380
7381
7382
7383
7384
7385
7386
7387
7388
7389
7390
7391
7392
7393
7394
7395
7396
7397
7398
7399
7400
7401
7402
7403
7404
7405
7406
7407
7408
7409
7410
7411
7412
7413
7414
7415
7416
7417
7418
7419
7420
7421
7422
7423
7424
7425
7426
7427
7428
7429
7430
7431
7432
7433
7434
7435
7436
7437
7438
7439
7440
7441
7442
7443
7444
7445
7446
7447
7448
7449
7450
7451
7452
7453
7454
7455
7456
7457
7458
7459
7460
7461
7462
7463
7464
7465
7466
7467
7468
7469
7470
7471
7472
7473
7474
7475
7476
7477
7478
7479
7480
7481
7482
7483
7484
7485
7486
7487
7488
7489
7490
7491
7492
7493
7494
7495
7496
7497
7498
7499
7500
7501
7502
7503
7504
7505
7506
7507
7508
7509
7510
7511
7512
7513
7514
7515
7516
7517
7518
7519
7520
7521
7522
7523
7524
7525
7526
7527
7528
7529
7530
7531
7532
7533
7534
7535
7536
7537
7538
7539
7540
7541
7542
7543
7544
7545
7546
7547
7548
7549
7550
7551
7552
7553
7554
7555
7556
7557
7558
7559
7560
7561
7562
7563
7564
7565
7566
7567
7568
7569
7570
7571
7572
7573
7574
7575
7576
7577
7578
7579
7580
7581
7582
7583
7584
7585
7586
7587
7588
7589
7590
7591
7592
7593
7594
7595
7596
7597
7598
7599
7600
7601
7602
7603
7604
7605
7606
7607
7608
7609
7610
7611
7612
7613
7614
7615
7616
7617
7618
7619
7620
7621
7622
7623
7624
7625
7626
7627
7628
7629
7630
7631
7632
7633
7634
7635
7636
7637
7638
7639
7640
7641
7642
7643
7644
7645
7646
7647
7648
7649
7650
7651
7652
7653
7654
7655
7656
7657
7658
7659
7660
7661
7662
7663
7664
7665
7666
7667
7668
7669
7670
7671
7672
7673
7674
7675
7676
7677
7678
7679
7680
7681
7682
7683
7684
7685
7686
7687
7688
7689
7690
7691
7692
7693
7694
7695
7696
7697
7698
7699
7700
7701
7702
7703
7704
7705
7706
7707
7708
7709
7710
7711
7712
7713
7714
7715
7716
7717
7718
7719
7720
7721
7722
7723
7724
7725
7726
7727
7728
7729
7730
7731
7732
7733
7734
7735
7736
7737
7738
7739
7740
7741
7742
7743
7744
7745
7746
7747
7748
7749
7750
7751
7752
7753
7754
7755
7756
7757
7758
7759
7760
7761
7762
7763
7764
7765
7766
7767
7768
7769
7770
7771
7772
7773
7774
7775
7776
7777
7778
7779
7780
7781
7782
7783
7784
7785
7786
7787
7788
7789
7790
7791
7792
7793
7794
7795
7796
7797
7798
7799
7800
7801
7802
7803
7804
7805
7806
7807
7808
7809
7810
7811
7812
7813
7814
7815
7816
7817
7818
7819
7820
7821
7822
7823
7824
7825
7826
7827
7828
7829
7830
7831
7832
7833
7834
7835
7836
7837
7838
7839
7840
7841
7842
7843
7844
7845
7846
7847
7848
7849
7850
7851
7852
7853
7854
7855
7856
7857
7858
7859
7860
7861
7862
7863
7864
7865
7866
7867
7868
7869
7870
7871
7872
7873
7874
7875
7876
7877
7878
7879
7880
7881
7882
7883
7884
7885
7886
7887
7888
7889
7890
7891
7892
7893
7894
7895
7896
7897
7898
7899
7900
7901
7902
7903
7904
7905
7906
7907
7908
7909
7910
7911
7912
7913
7914
7915
7916
7917
7918
7919
7920
7921
7922
7923
7924
7925
7926
7927
7928
7929
7930
7931
7932
7933
7934
7935
7936
7937
7938
7939
7940
7941
7942
7943
7944
7945
7946
7947
7948
7949
7950
7951
7952
7953
7954
7955
7956
7957
7958
7959
7960
7961
7962
7963
7964
7965
7966
7967
7968
7969
7970
7971
7972
7973
7974
7975
7976
7977
7978
7979
7980
7981
7982
7983
7984
7985
7986
7987
7988
7989
7990
7991
7992
7993
7994
7995
7996
7997
7998
7999
8000
8001
8002
8003
8004
8005
8006
8007
8008
8009
8010
8011
8012
8013
8014
8015
8016
8017
8018
8019
8020
8021
8022
8023
8024
8025
8026
8027
8028
8029
8030
8031
8032
8033
8034
8035
8036
8037
8038
8039
8040
8041
8042
8043
8044
8045
8046
8047
8048
8049
8050
8051
8052
8053
8054
8055
8056
8057
8058
8059
8060
8061
8062
8063
8064
8065
8066
8067
8068
8069
8070
8071
8072
8073
8074
8075
8076
8077
8078
8079
8080
8081
8082
8083
8084
8085
8086
8087
8088
8089
8090
8091
8092
8093
8094
8095
8096
8097
8098
8099
8100
8101
8102
8103
8104
8105
8106
8107
8108
8109
8110
8111
8112
8113
8114
8115
8116
8117
8118
8119
8120
8121
8122
8123
8124
8125
8126
8127
8128
8129
8130
8131
8132
8133
8134
8135
8136
8137
8138
8139
8140
8141
8142
8143
8144
8145
8146
8147
8148
8149
8150
8151
8152
8153
8154
8155
8156
8157
8158
8159
8160
8161
8162
8163
8164
8165
8166
8167
8168
8169
8170
8171
8172
8173
8174
8175
8176
8177
8178
8179
8180
8181
8182
8183
8184
8185
8186
8187
8188
8189
8190
8191
8192
8193
8194
8195
8196
8197
8198
8199
8200
8201
8202
8203
8204
8205
8206
8207
8208
8209
8210
8211
8212
8213
8214
8215
8216
8217
8218
8219
8220
8221
8222
8223
8224
8225
8226
8227
8228
8229
8230
8231
8232
8233
8234
8235
8236
8237
8238
8239
8240
8241
8242
8243
8244
8245
8246
8247
8248
8249
8250
8251
8252
8253
8254
8255
8256
8257
8258
8259
8260
8261
8262
8263
8264
8265
8266
8267
8268
8269
8270
8271
8272
8273
8274
8275
8276
8277
8278
8279
8280
8281
8282
8283
8284
8285
8286
8287
8288
8289
8290
8291
8292
8293
8294
8295
8296
8297
8298
8299
8300
8301
8302
8303
8304
8305
8306
8307
8308
8309
8310
8311
8312
8313
8314
8315
8316
8317
8318
8319
8320
8321
8322
8323
8324
8325
8326
8327
8328
8329
8330
8331
8332
8333
8334
8335
8336
8337
8338
8339
8340
8341
8342
8343
8344
8345
8346
8347
8348
8349
8350
8351
8352
8353
8354
8355
8356
8357
8358
8359
8360
8361
8362
8363
8364
8365
8366
8367
8368
8369
8370
8371
8372
8373
8374
8375
8376
8377
8378
8379
8380
8381
8382
8383
8384
8385
8386
8387
8388
8389
8390
8391
8392
8393
8394
8395
8396
8397
8398
8399
8400
8401
8402
8403
8404
8405
8406
8407
8408
8409
8410
8411
8412
8413
8414
8415
8416
8417
8418
8419
8420
8421
8422
8423
8424
8425
8426
8427
8428
8429
8430
8431
8432
8433
8434
8435
8436
8437
8438
8439
8440
8441
8442
8443
8444
8445
8446
8447
8448
8449
8450
8451
8452
8453
8454
8455
8456
8457
8458
8459
8460
8461
8462
8463
8464
8465
8466
8467
8468
8469
8470
8471
8472
8473
8474
8475
8476
8477
8478
8479
8480
8481
8482
8483
8484
8485
8486
8487
8488
8489
8490
8491
8492
8493
8494
8495
8496
8497
8498
8499
8500
8501
8502
8503
8504
8505
8506
8507
8508
8509
8510
8511
8512
8513
8514
8515
8516
8517
8518
8519
8520
8521
8522
8523
8524
8525
8526
8527
8528
8529
8530
8531
8532
8533
8534
8535
8536
8537
8538
8539
8540
8541
8542
8543
8544
8545
8546
8547
8548
8549
8550
8551
8552
8553
8554
8555
8556
8557
8558
8559
8560
8561
8562
8563
8564
8565
8566
8567
8568
8569
8570
8571
8572
8573
8574
8575
8576
8577
8578
8579
8580
8581
8582
8583
8584
8585
8586
8587
8588
8589
8590
8591
8592
8593
8594
8595
8596
8597
8598
8599
8600
8601
8602
8603
8604
8605
8606
8607
8608
8609
8610
8611
8612
8613
8614
8615
8616
8617
8618
8619
8620
8621
8622
8623
8624
8625
8626
8627
8628
8629
8630
8631
8632
8633
8634
8635
8636
8637
8638
8639
8640
8641
8642
8643
8644
8645
8646
8647
8648
8649
8650
8651
8652
8653
8654
8655
8656
8657
8658
8659
8660
8661
8662
8663
8664
8665
8666
8667
8668
8669
8670
8671
8672
8673
8674
8675
8676
8677
8678
8679
8680
8681
8682
8683
8684
8685
8686
8687
8688
8689
8690
8691
8692
8693
8694
8695
8696
8697
8698
8699
8700
8701
8702
8703
8704
8705
8706
8707
8708
8709
8710
8711
8712
8713
8714
8715
8716
8717
8718
8719
8720
8721
8722
8723
8724
8725
8726
8727
8728
8729
8730
8731
8732
8733
8734
8735
8736
8737
8738
8739
8740
8741
8742
8743
8744
8745
8746
8747
8748
8749
8750
8751
8752
8753
8754
8755
8756
8757
8758
8759
8760
8761
8762
8763
8764
8765
8766
8767
8768
8769
8770
8771
8772
8773
8774
8775
8776
8777
8778
8779
8780
8781
8782
8783
8784
8785
8786
8787
8788
8789
8790
8791
8792
8793
8794
8795
8796
8797
8798
8799
8800
8801
8802
8803
8804
8805
8806
8807
8808
8809
8810
8811
8812
8813
8814
8815
8816
8817
8818
8819
8820
8821
8822
8823
8824
8825
8826
8827
8828
8829
8830
8831
8832
8833
8834
8835
8836
8837
8838
8839
8840
8841
8842
8843
8844
8845
8846
8847
8848
8849
8850
8851
8852
8853
8854
8855
8856
8857
8858
8859
8860
8861
8862
8863
8864
8865
8866
8867
8868
8869
8870
8871
8872
8873
8874
8875
8876
8877
8878
8879
8880
8881
8882
8883
8884
8885
8886
8887
8888
8889
8890
8891
8892
8893
8894
8895
8896
8897
8898
8899
8900
8901
8902
8903
8904
8905
8906
8907
8908
8909
8910
8911
8912
8913
8914
8915
8916
8917
8918
8919
8920
8921
8922
8923
8924
8925
8926
8927
8928
8929
8930
8931
8932
8933
8934
8935
8936
8937
8938
8939
8940
8941
8942
8943
8944
8945
8946
8947
8948
8949
8950
8951
8952
8953
8954
8955
8956
8957
8958
8959
8960
8961
8962
8963
8964
8965
8966
8967
8968
8969
8970
8971
8972
8973
8974
8975
8976
8977
8978
8979
8980
8981
8982
8983
8984
8985
8986
8987
8988
8989
8990
8991
8992
8993
8994
8995
8996
8997
;;; lsp-mode.el --- LSP mode                              -*- lexical-binding: t; -*-

;; Copyright (C) 2020 emacs-lsp maintainers

;; Author: Vibhav Pant, Fangrui Song, Ivan Yonchovski
;; Keywords: languages
;; Package-Requires: ((emacs "26.1") (dash "2.18.0") (f "0.20.0") (ht "2.3") (spinner "1.7.3") (markdown-mode "2.3") (lv "0"))
;; Version: 8.0.1

;; URL: https://github.com/emacs-lsp/lsp-mode
;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.

;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with this program.  If not, see <https://www.gnu.org/licenses/>.

;;; Commentary:

;; Emacs client/library for the Language Server Protocol

;;; Code:

(require 'cl-generic)
(require 'cl-lib)
(require 'compile)
(require 'dash)
(require 'epg)
(require 'ewoc)
(require 'f)
(require 'filenotify)
(require 'files)
(require 'ht)
(require 'imenu)
(require 'inline)
(require 'json)
(require 'lv)
(require 'markdown-mode)
(require 'network-stream)
(require 'pcase)
(require 'rx)
(require 's)
(require 'seq)
(require 'spinner)
(require 'subr-x)
(require 'tree-widget)
(require 'url-parse)
(require 'url-util)
(require 'widget)
(require 'xref)
(require 'minibuffer)
(require 'yasnippet nil t)
(require 'lsp-protocol)

(defgroup lsp-mode nil
  "Language Server Protocol client."
  :group 'tools
  :tag "Language Server (lsp-mode)")

(declare-function evil-set-command-property "ext:evil-common")
(declare-function projectile-project-root "ext:projectile")
(declare-function yas-expand-snippet "ext:yasnippet")
(declare-function dap-mode "ext:dap-mode")
(declare-function dap-auto-configure-mode "ext:dap-mode")

(defvar yas-inhibit-overlay-modification-protection)
(defvar yas-indent-line)
(defvar yas-wrap-around-region)
(defvar yas-also-auto-indent-first-line)
(defvar dap-auto-configure-mode)
(defvar dap-ui-menu-items)
(defvar company-minimum-prefix-length)

(defconst lsp--message-type-face
  `((1 . ,compilation-error-face)
    (2 . ,compilation-warning-face)
    (3 . ,compilation-message-face)
    (4 . ,compilation-info-face)))

(defconst lsp--errors
  '((-32700 "Parse Error")
    (-32600 "Invalid Request")
    (-32601 "Method not Found")
    (-32602 "Invalid Parameters")
    (-32603 "Internal Error")
    (-32099 "Server Start Error")
    (-32000 "Server End Error")
    (-32002 "Server Not Initialized")
    (-32001 "Unknown Error Code")
    (-32800 "Request Cancelled"))
  "Alist of error codes to user friendly strings.")

(defconst lsp--empty-ht (make-hash-table))

(eval-and-compile
  (defun dash-expand:&lsp-wks (key source)
    `(,(intern-soft (format "lsp--workspace-%s" (eval key))) ,source))

  (defun dash-expand:&lsp-cln (key source)
    `(,(intern-soft (format "lsp--client-%s" (eval key))) ,source)))

(define-obsolete-variable-alias 'lsp-print-io 'lsp-log-io "lsp-mode 6.1")

(defcustom lsp-log-io nil
  "If non-nil, log all messages from the language server to a *lsp-log* buffer."
  :group 'lsp-mode
  :type 'boolean)

(defcustom lsp-log-io-allowlist-methods '()
  "The methods to filter before print to lsp-log-io."
  :group 'lsp-mode
  :type '(repeat string)
  :package-version '(lsp-mode . "8.0.1"))

(defcustom lsp-log-max message-log-max
  "Maximum number of lines to keep in the log buffer.
If nil, disable message logging.  If t, log messages but don’t truncate
the buffer when it becomes large."
  :group 'lsp-mode
  :type '(choice (const :tag "Disable" nil)
                 (integer :tag "lines")
                 (const :tag "Unlimited" t))
  :package-version '(lsp-mode . "6.1"))

(defcustom lsp-io-messages-max t
  "Maximum number of messages that can be locked in a `lsp-io' buffer."
  :group 'lsp-mode
  :type '(choice (const :tag "Unlimited" t)
                 (integer :tag "Messages"))
  :package-version '(lsp-mode . "6.1"))

(defcustom lsp-keep-workspace-alive t
  "If non nil keep workspace alive when the last workspace buffer is closed."
  :group 'lsp-mode
  :type 'boolean)

(defcustom lsp-enable-snippet t
  "Enable/disable snippet completion support."
  :group 'lsp-completion
  :type 'boolean)

(defcustom lsp-enable-folding t
  "Enable/disable code folding support."
  :group 'lsp-mode
  :type 'boolean
  :package-version '(lsp-mode . "6.1"))

(define-obsolete-variable-alias 'lsp-enable-semantic-highlighting 'lsp-semantic-tokens-enable "lsp-mode 8.0.0")

(defcustom lsp-semantic-tokens-enable nil
  "Enable/disable support for semantic tokens.
As defined by the Language Server Protocol 3.16."
  :group 'lsp-semantic-tokens
  :type 'boolean)

(defcustom lsp-folding-range-limit nil
  "The maximum number of folding ranges to receive from the language server."
  :group 'lsp-mode
  :type '(choice (const :tag "No limit." nil)
                 (integer :tag "Number of lines."))
  :package-version '(lsp-mode . "6.1"))

(defcustom lsp-folding-line-folding-only nil
  "If non-nil, only fold complete lines."
  :group 'lsp-mode
  :type 'boolean
  :package-version '(lsp-mode . "6.1"))

(defcustom lsp-client-packages
  '(ccls lsp-actionscript lsp-ada lsp-angular lsp-ansible lsp-bash lsp-beancount lsp-clangd lsp-clojure
         lsp-cmake lsp-crystal lsp-csharp lsp-css lsp-d lsp-dart lsp-dhall lsp-docker lsp-dockerfile
         lsp-elm lsp-elixir lsp-emmet lsp-erlang lsp-eslint lsp-fortran lsp-fsharp lsp-gdscript lsp-go
         lsp-graphql lsp-hack lsp-grammarly lsp-groovy lsp-haskell lsp-haxe lsp-idris lsp-java lsp-javascript
         lsp-json lsp-kotlin lsp-latex lsp-ltex lsp-lua lsp-markdown lsp-nginx lsp-nim lsp-nix lsp-magik
         lsp-metals lsp-mssql lsp-ocaml lsp-openscad lsp-pascal lsp-perl lsp-perlnavigator lsp-php lsp-pwsh lsp-pyls lsp-pylsp
         lsp-pyright lsp-python-ms lsp-purescript lsp-r lsp-remark lsp-rf lsp-rust lsp-solargraph
         lsp-sorbet lsp-sourcekit lsp-sonarlint lsp-tailwindcss lsp-tex lsp-terraform lsp-toml
         lsp-ttcn3 lsp-typeprof lsp-v lsp-vala lsp-verilog lsp-vetur lsp-volar lsp-vhdl lsp-vimscript
         lsp-xml lsp-yaml lsp-sqls lsp-svelte lsp-steep lsp-zig)
  "List of the clients to be automatically required."
  :group 'lsp-mode
  :type '(repeat symbol))

(defcustom lsp-progress-via-spinner t
  "If non-nil, display LSP $/progress reports via a spinner in the modeline."
  :group 'lsp-mode
  :type 'boolean)

(defvar-local lsp--cur-workspace nil)

(defvar-local lsp--cur-version 0)
(defvar-local lsp--virtual-buffer-connections nil)
(defvar-local lsp--virtual-buffer nil)
(defvar lsp--virtual-buffer-mappings (ht))

(defvar lsp--uri-file-prefix (pcase system-type
                               (`windows-nt "file:///")
                               (_ "file://"))
  "Prefix for a file-uri.")

(defvar-local lsp-buffer-uri nil
  "If set, return it instead of calculating it using `buffer-file-name'.")

(define-error 'lsp-error "Unknown lsp-mode error")
(define-error 'lsp-empty-response-error
  "Empty response from the language server" 'lsp-error)
(define-error 'lsp-timed-out-error
  "Timed out while waiting for a response from the language server" 'lsp-error)
(define-error 'lsp-capability-not-supported
  "Capability not supported by the language server" 'lsp-error)
(define-error 'lsp-file-scheme-not-supported
  "Unsupported file scheme" 'lsp-error)
(define-error 'lsp-client-already-exists-error
  "A client with this server-id already exists" 'lsp-error)
(define-error 'lsp-no-code-actions
  "No code actions" 'lsp-error)

(defcustom lsp-auto-guess-root nil
  "Automatically guess the project root using projectile/project.
Do *not* use this setting unless you are familiar with `lsp-mode'
internals and you are sure that all of your projects are
following `projectile'/`project.el' conventions."
  :group 'lsp-mode
  :type 'boolean)

(defcustom lsp-restart 'interactive
  "Defines how server-exited events must be handled."
  :group 'lsp-mode
  :type '(choice (const interactive)
                 (const auto-restart)
                 (const ignore)))

(defcustom lsp-session-file (expand-file-name (locate-user-emacs-file ".lsp-session-v1"))
  "File where session information is stored."
  :group 'lsp-mode
  :type 'file)

(defcustom lsp-auto-configure t
  "Auto configure `lsp-mode' main features.
When set to t `lsp-mode' will auto-configure completion,
code-actions, breadcrumb, `flycheck', `flymake', `imenu', symbol highlighting,
lenses, links, and so on.

For finer granularity you may use `lsp-enable-*' properties."
  :group 'lsp-mode
  :type 'boolean
  :package-version '(lsp-mode . "6.1"))

(defcustom lsp-disabled-clients nil
  "A list of disabled/blacklisted clients.
Each entry in the list can be either:
a symbol, the server-id for the LSP client, or
a cons pair (MAJOR-MODE . CLIENTS), where MAJOR-MODE is the major-mode,
and CLIENTS is either a client or a list of clients.

This option can also be used as a file- or directory-local variable to
disable a language server for individual files or directories/projects
respectively."
  :group 'lsp-mode
  :type '(repeat (symbol))
  :safe 'listp
  :package-version '(lsp-mode . "6.1"))

(defvar lsp-clients (make-hash-table :test 'eql)
  "Hash table server-id -> client.
It contains all of the clients that are currently registered.")

(defvar lsp-enabled-clients nil
  "List of clients allowed to be used for projects.
When nil, all registered clients are considered candidates.")

(defvar lsp-last-id 0
  "Last request id.")

(defcustom lsp-before-initialize-hook nil
  "List of functions to be called before a Language Server has been initialized
for a new workspace."
  :type 'hook
  :group 'lsp-mode)

(defcustom lsp-after-initialize-hook nil
  "List of functions to be called after a Language Server has been initialized
for a new workspace."
  :type 'hook
  :group 'lsp-mode)

(defcustom lsp-before-open-hook nil
  "List of functions to be called before a new file with LSP support is opened."
  :type 'hook
  :group 'lsp-mode)

(defcustom lsp-after-open-hook nil
  "List of functions to be called after a new file with LSP support is opened."
  :type 'hook
  :group 'lsp-mode)

(defcustom lsp-enable-file-watchers t
  "If non-nil lsp-mode will watch the files in the workspace if
the server has requested that."
  :type 'boolean
  :group 'lsp-mode
  :package-version '(lsp-mode . "6.1"))
;;;###autoload(put 'lsp-enable-file-watchers 'safe-local-variable #'booleanp)

(define-obsolete-variable-alias 'lsp-file-watch-ignored 'lsp-file-watch-ignored-directories "8.0.0")

(defcustom lsp-file-watch-ignored-directories
  '(; SCM tools
    "[/\\\\]\\.git\\'"
    "[/\\\\]\\.github\\'"
    "[/\\\\]\\.circleci\\'"
    "[/\\\\]\\.hg\\'"
    "[/\\\\]\\.bzr\\'"
    "[/\\\\]_darcs\\'"
    "[/\\\\]\\.svn\\'"
    "[/\\\\]_FOSSIL_\\'"
    ;; IDE or build tools
    "[/\\\\]\\.idea\\'"
    "[/\\\\]\\.ensime_cache\\'"
    "[/\\\\]\\.eunit\\'"
    "[/\\\\]node_modules"
    "[/\\\\]\\.yarn\\'"
    "[/\\\\]\\.fslckout\\'"
    "[/\\\\]\\.tox\\'"
    "[/\\\\]dist\\'"
    "[/\\\\]dist-newstyle\\'"
    "[/\\\\]\\.stack-work\\'"
    "[/\\\\]\\.bloop\\'"
    "[/\\\\]\\.metals\\'"
    "[/\\\\]target\\'"
    "[/\\\\]\\.ccls-cache\\'"
    "[/\\\\]\\.vscode\\'"
    "[/\\\\]\\.venv\\'"
    "[/\\\\]\\.mypy_cache\\'"
    ;; Autotools output
    "[/\\\\]\\.deps\\'"
    "[/\\\\]build-aux\\'"
    "[/\\\\]autom4te.cache\\'"
    "[/\\\\]\\.reference\\'"
    ;; Clojure
    "[/\\\\]\\.lsp\\'"
    "[/\\\\]\\.clj-kondo\\'"
    "[/\\\\]\\.shadow-cljs\\'"
    "[/\\\\]\\.babel_cache\\'"
    "[/\\\\]\\.cpcache\\'"
    "[/\\\\]\\checkouts\\'"
    ;; Maven
    "[/\\\\]\\.m2\\'"
    ;; .Net Core build-output
    "[/\\\\]bin/Debug\\'"
    "[/\\\\]obj\\'"
    ;; OCaml and Dune
    "[/\\\\]_opam\\'"
    "[/\\\\]_build\\'"
    ;; Elixir
    "[/\\\\]\\.elixir_ls\\'"
    ;; nix-direnv
    "[/\\\\]\\.direnv\\'")
  "List of regexps matching directory paths which won't be monitored when
creating file watches. Customization of this variable is only honored at
the global level or at a root of an lsp workspace."
  :group 'lsp-mode
  :type '(repeat string)
  :package-version '(lsp-mode . "8.0.0"))

(define-obsolete-function-alias 'lsp-file-watch-ignored 'lsp-file-watch-ignored-directories "7.0.1")

(defun lsp-file-watch-ignored-directories ()
  lsp-file-watch-ignored-directories)

;; Allow lsp-file-watch-ignored-directories as a file or directory-local variable
;;;###autoload(put 'lsp-file-watch-ignored-directories 'safe-local-variable 'lsp--string-listp)

(defcustom lsp-file-watch-ignored-files
  '(
    ;; Flycheck tempfiles
    "[/\\\\]flycheck_[^/\\\\]+\\'"
    ;; lockfiles
    "[/\\\\]\\.#[^/\\\\]+\\'"
    ;; backup files
    "[/\\\\][^/\\\\]+~\\'" )
  "List of regexps matching files for which change events will
not be sent to the server.

This setting has no impact on whether a file-watch is created for
a directory; it merely prevents notifications pertaining to
matched files from being sent to the server.  To prevent a
file-watch from being created for a directory, customize
`lsp-file-watch-ignored-directories'

Customization of this variable is only honored at the global
level or at a root of an lsp workspace."
  :group 'lsp-mode
  :type '(repeat string)
  :package-version '(lsp-mode . "8.0.0"))

;; Allow lsp-file-watch-ignored-files as a file or directory-local variable
;;;###autoload(put 'lsp-file-watch-ignored-files 'safe-local-variable 'lsp--string-listp)

(defcustom lsp-after-uninitialized-functions nil
  "List of functions to be called after a Language Server has been uninitialized."
  :type 'hook
  :group 'lsp-mode
  :package-version '(lsp-mode . "6.3"))

(defconst lsp--sync-none 0)
(defconst lsp--sync-full 1)
(defconst lsp--sync-incremental 2)

(defcustom lsp-debounce-full-sync-notifications t
  "If non-nil debounce full sync events.
This flag affects only servers which do not support incremental updates."
  :type 'boolean
  :group 'lsp-mode
  :package-version '(lsp-mode . "6.1"))

(defcustom lsp-debounce-full-sync-notifications-interval 1.0
  "Time to wait before sending full sync synchronization after buffer modification."
  :type 'float
  :group 'lsp-mode
  :package-version '(lsp-mode . "6.1"))

(defvar lsp--stderr-index 0)

(defvar lsp--delayed-requests nil)
(defvar lsp--delay-timer nil)

(defcustom lsp-document-sync-method nil
  "How to sync the document with the language server."
  :type '(choice (const :tag "Documents should not be synced at all." nil)
                 (const :tag "Documents are synced by always sending the full content of the document." lsp--sync-full)
                 (const :tag "Documents are synced by always sending incremental changes to the document." lsp--sync-incremental)
                 (const :tag "Use the method recommended by the language server." nil))
  :group 'lsp-mode)

(defcustom lsp-auto-execute-action t
  "Auto-execute single action."
  :type 'boolean
  :group 'lsp-mode)

(defcustom lsp-enable-links t
  "If non-nil, all references to links in a file will be made clickable, if
supported by the language server."
  :type 'boolean
  :group 'lsp-mode
  :package-version '(lsp-mode . "6.1"))

(defcustom lsp-enable-imenu t
  "If non-nil, automatically enable `imenu' integration when server provides
`textDocument/documentSymbol'."
  :type 'boolean
  :group 'lsp-mode
  :package-version '(lsp-mode . "6.2"))

(defcustom lsp-enable-dap-auto-configure t
  "If non-nil, enable `dap-auto-configure-mode`."
  :type 'boolean
  :group 'lsp-mode
  :package-version '(lsp-mode . "7.0"))

(defcustom lsp-eldoc-enable-hover t
  "If non-nil, `eldoc' will display hover info when it is present."
  :type 'boolean
  :group 'lsp-mode)

(defcustom lsp-eldoc-render-all nil
  "Display all of the info returned by document/onHover.
If this is set to nil, `eldoc' will show only the symbol information."
  :type 'boolean
  :group 'lsp-mode)

(define-obsolete-variable-alias 'lsp-enable-completion-at-point
  'lsp-completion-enable "lsp-mode 7.0.1")

(defcustom lsp-completion-enable t
  "Enable `completion-at-point' integration."
  :type 'boolean
  :group 'lsp-completion)

(defcustom lsp-enable-symbol-highlighting t
  "Highlight references of the symbol at point."
  :type 'boolean
  :group 'lsp-mode)

(defcustom lsp-enable-xref t
  "Enable xref integration."
  :type 'boolean
  :group 'lsp-mode)

(defcustom lsp-enable-indentation t
  "Indent regions using the file formatting functionality provided by the
language server."
  :type 'boolean
  :group 'lsp-mode)

(defcustom lsp-enable-on-type-formatting t
  "Enable `textDocument/onTypeFormatting' integration."
  :type 'boolean
  :group 'lsp-mode)

(defcustom lsp-enable-text-document-color t
  "Enable `textDocument/documentColor' integration."
  :type 'boolean
  :group 'lsp-mode)

(defcustom lsp-before-save-edits t
  "If non-nil, `lsp-mode' will apply edits suggested by the language server
before saving a document."
  :type 'boolean
  :group 'lsp-mode)

(defcustom lsp-after-apply-edits-hook nil
  "Hooks to run when text edit is applied.
It contains the operation source."
  :type 'hook
  :group 'lsp-mode
  :package-version '(lsp-mode . "8.0.0"))

(defcustom lsp-apply-edits-after-file-operations t
  "Whether to apply edits returned by server after file operations if any.
Applicable only if server supports workspace.fileOperations for operations:
`workspace/willRenameFiles', `workspace/willCreateFiles' and
`workspace/willDeleteFiles'."
  :group 'lsp-mode
  :type 'boolean)

(defcustom lsp-modeline-code-actions-enable t
  "Whether to show code actions on modeline."
  :type 'boolean
  :group 'lsp-modeline)

(defcustom lsp-modeline-diagnostics-enable t
  "Whether to show diagnostics on modeline."
  :type 'boolean
  :group 'lsp-modeline)

(defcustom lsp-modeline-workspace-status-enable t
  "Whether to show workspace status on modeline."
  :type 'boolean
  :group 'lsp-modeline
  :package-version '(lsp-mode . "8.0.0"))

(defcustom lsp-headerline-breadcrumb-enable t
  "Whether to enable breadcrumb on headerline."
  :type 'boolean
  :group 'lsp-headerline)

(defcustom lsp-configure-hook nil
  "Hooks to run when `lsp-configure-buffer' is called."
  :type 'hook
  :group 'lsp-mode)

(defcustom lsp-unconfigure-hook nil
  "Hooks to run when `lsp-unconfig-buffer' is called."
  :type 'hook
  :group 'lsp-mode)

(defcustom lsp-after-diagnostics-hook nil
  "Hooks to run after diagnostics are received.
Note: it runs only if the receiving buffer is open. Use
`lsp-diagnostics-updated-hook'if you want to be notified when
diagnostics have changed."
  :type 'hook
  :group 'lsp-mode)

(define-obsolete-variable-alias 'lsp-after-diagnostics-hook
  'lsp-diagnostics-updated-hook "lsp-mode 6.4")

(defcustom lsp-diagnostics-updated-hook nil
  "Hooks to run after diagnostics are received."
  :type 'hook
  :group 'lsp-mode)

(define-obsolete-variable-alias 'lsp-workspace-folders-changed-hook
  'lsp-workspace-folders-changed-functions "lsp-mode 6.3")

(defcustom lsp-workspace-folders-changed-functions nil
  "Hooks to run after the folders has changed.
The hook will receive two parameters list of added and removed folders."
  :type 'hook
  :group 'lsp-mode)

(defcustom lsp-eldoc-hook '(lsp-hover)
  "Hooks to run for eldoc."
  :type 'hook
  :group 'lsp-mode)

(defcustom lsp-before-apply-edits-hook nil
  "Hooks to run before applying edits."
  :type 'hook
  :group 'lsp-mode)

(defgroup lsp-imenu nil
  "LSP Imenu."
  :group 'lsp-mode
  :tag "LSP Imenu")

(defcustom lsp-imenu-show-container-name t
  "Display the symbol's container name in an imenu entry."
  :type 'boolean
  :group 'lsp-imenu)

(defcustom lsp-imenu-container-name-separator "/"
  "Separator string to use to separate the container name from the symbol while
displaying imenu entries."
  :type 'string
  :group 'lsp-imenu)

(defcustom lsp-imenu-sort-methods '(kind name)
  "How to sort the imenu items.

The value is a list of `kind' `name' or `position'.  Priorities
are determined by the index of the element."
  :type '(repeat (choice (const name)
                         (const position)
                         (const kind)))
  :group 'lsp-imenu)

(defcustom lsp-imenu-index-symbol-kinds nil
  "Which symbol kinds to show in imenu."
  :type '(repeat (choice (const :tag "Miscellaneous" nil)
                         (const :tag "File" File)
                         (const :tag "Module" Module)
                         (const :tag "Namespace" Namespace)
                         (const :tag "Package" Package)
                         (const :tag "Class" Class)
                         (const :tag "Method" Method)
                         (const :tag "Property" Property)
                         (const :tag "Field" Field)
                         (const :tag "Constructor" Constuctor)
                         (const :tag "Enum" Enum)
                         (const :tag "Interface" Interface)
                         (const :tag "Function" Function)
                         (const :tag "Variable" Variable)
                         (const :tag "Constant" Constant)
                         (const :tag "String" String)
                         (const :tag "Number" Number)
                         (const :tag "Boolean" Boolean)
                         (const :tag "Array" Array)
                         (const :tag "Object" Object)
                         (const :tag "Key" Key)
                         (const :tag "Null" Null)
                         (const :tag "Enum Member" EnumMember)
                         (const :tag "Struct" Struct)
                         (const :tag "Event" Event)
                         (const :tag "Operator" Operator)
                         (const :tag "Type Parameter" TypeParameter)))
  :group 'lsp-imenu)

;; vibhavp: Should we use a lower value (5)?
(defcustom lsp-response-timeout 10
  "Number of seconds to wait for a response from the language server before
timing out. Nil if no timeout."
  :type '(choice
          (number :tag "Seconds")
          (const :tag "No timeout" nil))
  :group 'lsp-mode)

(defcustom lsp-tcp-connection-timeout 2
  "The timeout for tcp connection in seconds."
  :type 'number
  :group 'lsp-mode
  :package-version '(lsp-mode . "6.2"))

(defconst lsp--imenu-compare-function-alist
  (list (cons 'name #'lsp--imenu-compare-name)
        (cons 'kind #'lsp--imenu-compare-kind)
        (cons 'position #'lsp--imenu-compare-line-col))
  "An alist of (METHOD . FUNCTION).
METHOD is one of the symbols accepted by
`lsp-imenu-sort-methods'.

FUNCTION takes two hash tables representing DocumentSymbol.  It
returns a negative number, 0, or a positive number indicating
whether the first parameter is less than, equal to, or greater
than the second parameter.")

(defcustom lsp-diagnostic-clean-after-change nil
  "When non-nil, clean the diagnostics on change.

Note that when that setting is nil, `lsp-mode' will show stale
diagnostics until server publishes the new set of diagnostics"
  :type 'boolean
  :group 'lsp-diagnostics
  :package-version '(lsp-mode . "7.0.1"))

(defcustom lsp-server-trace nil
  "Request tracing on the server side.
The actual trace output at each level depends on the language server in use.
Changes take effect only when a new session is started."
  :type '(choice (const :tag "Disabled" "off")
                 (const :tag "Messages only" "messages")
                 (const :tag "Verbose" "verbose")
                 (const :tag "Default (disabled)" nil))
  :group 'lsp-mode
  :package-version '(lsp-mode . "6.1"))

(defcustom lsp-auto-touch-files t
  "If non-nil ensure the files exist before sending
`textDocument/didOpen' notification."
  :type 'boolean
  :group 'lsp-mode
  :package-version '(lsp-mode . "8.0.1"))

(defvar lsp-language-id-configuration '((".*\\.vue$" . "vue")
                                        (".*\\.tsx$" . "typescriptreact")
                                        (".*\\.ts$" . "typescript")
                                        (".*\\.jsx$" . "javascriptreact")
                                        (".*\\.js$" . "javascript")
                                        (".*\\.xml$" . "xml")
                                        (".*\\.hx$" . "haxe")
                                        (".*\\.lua$" . "lua")
                                        (".*\\.sql$" . "sql")
                                        (".*\\.html$" . "html")
                                        (".*\\.css" . "css")
                                        (".*/settings.json$" . "jsonc")
                                        (".*\\.json$" . "json")
                                        (".*\\.jsonc$" . "jsonc")
                                        (".*\\.php$" . "php")
                                        (".*\\.svelte$" . "svelte")
                                        (".*\\.ebuild$" . "shellscript")
                                        (".*/PKGBUILD$" . "shellscript")
                                        (".*\\.ttcn3$" . "ttcn3")
                                        (ada-mode . "ada")
                                        (nxml-mode . "xml")
                                        (sql-mode . "sql")
                                        (vimrc-mode . "vim")
                                        (sh-mode . "shellscript")
                                        (ebuild-mode . "shellscript")
                                        (pkgbuild-mode . "shellscript")
                                        (scala-mode . "scala")
                                        (julia-mode . "julia")
                                        (clojure-mode . "clojure")
                                        (clojurec-mode . "clojure")
                                        (clojurescript-mode . "clojurescript")
                                        (java-mode . "java")
                                        (jdee-mode . "java")
                                        (groovy-mode . "groovy")
                                        (python-mode . "python")
                                        (cython-mode . "python")
                                        (lsp--render-markdown . "markdown")
                                        (rust-mode . "rust")
                                        (rustic-mode . "rust")
                                        (kotlin-mode . "kotlin")
                                        (css-mode . "css")
                                        (less-mode . "less")
                                        (less-css-mode . "less")
                                        (lua-mode . "lua")
                                        (sass-mode . "sass")
                                        (ssass-mode . "sass")
                                        (scss-mode . "scss")
                                        (scad-mode . "openscad")
                                        (xml-mode . "xml")
                                        (c-mode . "c")
                                        (c++-mode . "cpp")
                                        (objc-mode . "objective-c")
                                        (html-mode . "html")
                                        (sgml-mode . "html")
                                        (mhtml-mode . "html")
                                        (go-dot-mod-mode . "go.mod")
                                        (go-mode . "go")
                                        (graphql-mode . "graphql")
                                        (haskell-mode . "haskell")
                                        (hack-mode . "hack")
                                        (php-mode . "php")
                                        (powershell-mode . "powershell")
                                        (powershell-mode . "PowerShell")
                                        (json-mode . "json")
                                        (jsonc-mode . "jsonc")
                                        (rjsx-mode . "javascript")
                                        (js2-mode . "javascript")
                                        (js-mode . "javascript")
                                        (typescript-mode . "typescript")
                                        (fsharp-mode . "fsharp")
                                        (reason-mode . "reason")
                                        (caml-mode . "ocaml")
                                        (tuareg-mode . "ocaml")
                                        (swift-mode . "swift")
                                        (elixir-mode . "elixir")
                                        (conf-javaprop-mode . "spring-boot-properties")
                                        (yaml-mode . "spring-boot-properties-yaml")
                                        (ruby-mode . "ruby")
                                        (enh-ruby-mode . "ruby")
                                        (fortran-mode . "fortran")
                                        (f90-mode . "fortran")
                                        (elm-mode . "elm")
                                        (dart-mode . "dart")
                                        (erlang-mode . "erlang")
                                        (dockerfile-mode . "dockerfile")
                                        (csharp-mode . "csharp")
                                        (csharp-tree-sitter-mode . "csharp")
                                        (plain-tex-mode . "plaintex")
                                        (latex-mode . "latex")
                                        (v-mode . "v")
                                        (vhdl-mode . "vhdl")
                                        (verilog-mode . "verilog")
                                        (terraform-mode . "terraform")
                                        (ess-julia-mode . "julia")
                                        (ess-r-mode . "r")
                                        (crystal-mode . "crystal")
                                        (nim-mode . "nim")
                                        (dhall-mode . "dhall")
                                        (cmake-mode . "cmake")
                                        (purescript-mode . "purescript")
                                        (gdscript-mode . "gdscript")
                                        (perl-mode . "perl")
                                        (cperl-mode . "perl")
                                        (robot-mode . "robot")
                                        (racket-mode . "racket")
                                        (nix-mode . "nix")
                                        (prolog-mode . "prolog")
                                        (vala-mode . "vala")
                                        (actionscript-mode . "actionscript")
                                        (d-mode . "d")
                                        (zig-mode . "zig")
                                        (text-mode . "plaintext")
                                        (markdown-mode . "markdown")
                                        (gfm-mode . "markdown")
                                        (beancount-mode . "beancount")
                                        (conf-toml-mode . "toml")
                                        (org-mode . "org")
                                        (org-journal-mode . "org")
                                        (nginx-mode . "nginx")
                                        (magik-mode . "magik")
                                        (idris-mode . "idris")
                                        (idris2-mode . "idris2"))
  "Language id configuration.")

(defvar lsp--last-active-workspaces nil
  "Keep track of last active workspace.
We want to try the last workspace first when jumping into a library
directory")

(defvar lsp-method-requirements
  '(("textDocument/callHierarchy" :capability :callHierarchyProvider)
    ("textDocument/codeAction" :capability :codeActionProvider)
    ("codeAction/resolve"
     :check-command (lambda (workspace)
                      (with-lsp-workspace workspace
                        (lsp:code-action-options-resolve-provider?
                         (or (lsp--capability :codeActionProvider)
                             (when-let ((maybe-capability (lsp--registered-capability "textDocument/codeAction"))
                                        (capability-options (lsp--registered-capability-options maybe-capability)))
                               capability-options))))))
    ("textDocument/codeLens" :capability :codeLensProvider)
    ("textDocument/completion" :capability :completionProvider)
    ("completionItem/resolve"
     :check-command (lambda (wk)
                      (with-lsp-workspace wk
                        (lsp:completion-options-resolve-provider?
                         (lsp--capability :completionProvider)))))
    ("textDocument/declaration" :capability :declarationProvider)
    ("textDocument/definition" :capability :definitionProvider)
    ("textDocument/documentColor" :capability :colorProvider)
    ("textDocument/documentLink" :capability :documentLinkProvider)
    ("textDocument/documentHighlight" :capability :documentHighlightProvider)
    ("textDocument/documentSymbol" :capability :documentSymbolProvider)
    ("textDocument/foldingRange" :capability :foldingRangeProvider)
    ("textDocument/formatting" :capability :documentFormattingProvider)
    ("textDocument/hover" :capability :hoverProvider)
    ("textDocument/implementation" :capability :implementationProvider)
    ("textDocument/linkedEditingRange" :capability :linkedEditingRangeProvider)
    ("textDocument/onTypeFormatting" :capability :documentOnTypeFormattingProvider)
    ("textDocument/prepareRename"
     :check-command (lambda (workspace)
                      (with-lsp-workspace workspace
                        (lsp:rename-options-prepare-provider?
                         (or (lsp--capability :renameProvider)
                             (when-let ((maybe-capability (lsp--registered-capability "textDocument/rename")))
                               (lsp--registered-capability-options maybe-capability)))))))
    ("textDocument/rangeFormatting" :capability :documentRangeFormattingProvider)
    ("textDocument/references" :capability :referencesProvider)
    ("textDocument/rename" :capability :renameProvider)
    ("textDocument/selectionRange" :capability :selectionRangeProvider)
    ("textDocument/semanticTokens" :capability :semanticTokensProvider)
    ("textDocument/semanticTokensFull"
     :check-command (lambda (workspace)
                      (with-lsp-workspace workspace
                        (lsp-get (lsp--capability :semanticTokensProvider) :full))))
    ("textDocument/semanticTokensFull/Delta"
     :check-command (lambda (workspace)
                      (with-lsp-workspace workspace
                        (let ((capFull (lsp-get (lsp--capability :semanticTokensProvider) :full)))
                          (and (not (booleanp capFull)) (lsp-get capFull :delta))))))
    ("textDocument/semanticTokensRangeProvider"
     :check-command (lambda (workspace)
                      (with-lsp-workspace workspace
                        (lsp-get (lsp--capability :semanticTokensProvider) :range))))
    ("textDocument/signatureHelp" :capability :signatureHelpProvider)
    ("textDocument/typeDefinition" :capability :typeDefinitionProvider)
    ("workspace/executeCommand" :capability :executeCommandProvider)
    ("workspace/symbol" :capability :workspaceSymbolProvider))

  "Map methods to requirements.
It is used by request-sending functions to determine which server
must be used for handling a particular message.")

(defconst lsp--file-change-type
  `((created . 1)
    (changed . 2)
    (deleted . 3)))

(defconst lsp--watch-kind
  `((create . 1)
    (change . 2)
    (delete . 4)))

(defvar lsp-window-body-width 40
  "Window body width when rendering doc.")

(defface lsp-face-highlight-textual
  '((t :inherit highlight))
  "Face used for textual occurrences of symbols."
  :group 'lsp-mode)

(defface lsp-face-highlight-read
  '((t :inherit highlight :underline t))
  "Face used for highlighting symbols being read."
  :group 'lsp-mode)

(defface lsp-face-highlight-write
  '((t :inherit highlight :weight bold))
  "Face used for highlighting symbols being written to."
  :group 'lsp-mode)

(define-obsolete-variable-alias 'lsp-lens-auto-enable
  'lsp-lens-enable "lsp-mode 7.0.1")

(defcustom lsp-lens-enable t
  "Auto enable lenses if server supports."
  :group 'lsp-lens
  :type 'boolean
  :package-version '(lsp-mode . "6.3"))

(defcustom lsp-symbol-highlighting-skip-current nil
  "If non-nil skip current symbol when setting symbol highlights."
  :group 'lsp-mode
  :type 'boolean)

(defcustom lsp-file-watch-threshold 1000
  "Show warning if the files to watch are more than.
Set to nil to disable the warning."
  :type 'number
  :group 'lsp-mode)
;;;###autoload(put 'lsp-file-watch-threshold 'safe-local-variable (lambda (i) (or (numberp i) (not i))))

(defvar lsp-custom-markup-modes
  '((rust-mode "no_run" "rust,no_run" "rust,ignore" "rust,should_panic"))
  "Mode to uses with markdown code blocks.
They are added to `markdown-code-lang-modes'")

(defcustom lsp-signature-render-documentation t
  "Display signature documentation in `eldoc'."
  :type 'boolean
  :group 'lsp-mode
  :package-version '(lsp-mode . "6.2"))

(defcustom lsp-signature-auto-activate '(:on-trigger-char :on-server-request)
  "Auto activate signature conditions."
  :type '(repeat (choice (const :tag "On trigger chars pressed." :on-trigger-char)
                         (const :tag "After selected completion." :after-completion)
                         (const :tag "When the server has sent show signature help." :on-server-request)))
  :group 'lsp-mode
  :package-version '(lsp-mode . "6.2"))

(defcustom lsp-signature-doc-lines 20
  "If number, limit the number of lines to show in the docs."
  :type 'number
  :group 'lsp-mode
  :package-version '(lsp-mode . "6.3"))

(defcustom lsp-signature-function 'lsp-lv-message
  "The function used for displaying signature info.
It will be called with one param - the signature info. When
called with nil the signature info must be cleared."
  :type 'function
  :group 'lsp-mode
  :package-version '(lsp-mode . "6.3"))

(defcustom lsp-keymap-prefix "s-l"
  "LSP-mode keymap prefix."
  :group 'lsp-mode
  :type 'string
  :package-version '(lsp-mode . "6.3"))

(defvar-local lsp--buffer-workspaces ()
  "List of the buffer workspaces.")

(defvar lsp--session nil
  "Contain the `lsp-session' for the current Emacs instance.")

(defvar lsp--tcp-port 10000)

(defvar lsp--client-packages-required nil
  "If nil, `lsp-client-packages' are yet to be required.")

(defvar lsp--tcp-server-port 0
  "The server socket which is opened when using `lsp-tcp-server' (a server
socket is opened in Emacs and the language server connects to it).  The
default value of 0 ensures that a random high port is used. Set it to a positive
integer to use a specific port.")

(defvar lsp--tcp-server-wait-seconds 10
  "Wait this amount of time for the client to connect to our server socket
when using `lsp-tcp-server'.")

(defvar-local lsp--document-symbols nil
  "The latest document symbols.")

(defvar-local lsp--document-selection-range-cache nil
  "The document selection cache.")

(defvar-local lsp--document-symbols-request-async nil
  "If non-nil, request document symbols asynchronously.")

(defvar-local lsp--document-symbols-tick -1
  "The value of `buffer-chars-modified-tick' when document
  symbols were last retrieved.")

(defvar-local lsp--have-document-highlights nil
  "Set to `t' on symbol highlighting, cleared on
`lsp--cleanup-highlights-if-needed'. Checking a separately
defined flag is substantially faster than unconditionally
calling `remove-overlays'.")

;; Buffer local variable for storing number of lines.
(defvar lsp--log-lines)

(defvar-local lsp--eldoc-saved-message nil)

(defvar lsp--on-change-timer nil)
(defvar lsp--on-idle-timer nil)

(defvar-local lsp--signature-last nil)
(defvar-local lsp--signature-last-index nil)
(defvar lsp--signature-last-buffer nil)

(defvar-local lsp--virtual-buffer-point-max nil)

(cl-defgeneric lsp-execute-command (server command arguments)
  "Ask SERVER to execute COMMAND with ARGUMENTS.")

(defun lsp-elt (sequence n)
  "Return Nth element of SEQUENCE or nil if N is out of range."
  (cond
   ((listp sequence) (elt sequence n))
   ((arrayp sequence)
    (and (> (length sequence) n) (aref sequence n)))
   (t (and (> (length sequence) n) (elt sequence n)))))

;; define seq-first and seq-rest for older emacs
(defun lsp-seq-first (sequence)
  "Return the first element of SEQUENCE."
  (lsp-elt sequence 0))

(defun lsp-seq-rest (sequence)
  "Return a sequence of the elements of SEQUENCE except the first one."
  (seq-drop sequence 1))

;;;###autoload
(defun lsp--string-listp (sequence)
  "Return t if all elements of SEQUENCE are strings, else nil."
  (not (seq-find (lambda (x) (not (stringp x))) sequence)))

(defun lsp--string-vector-p (candidate)
  "Returns true if CANDIDATE is a vector data structure and
every element of it is of type string, else nil."
  (and
   (vectorp candidate)
   (seq-every-p #'stringp candidate)))

(make-obsolete 'lsp--string-vector-p nil "lsp-mode 8.0.0")

(defun lsp--editable-vector-match (widget value)
  "Function for `lsp-editable-vector' :match."
  ;; Value must be a list or a vector and all the members must match the type.
  (and (or (listp value) (vectorp value))
       (length (cdr (lsp--editable-vector-match-inline widget value)))))

(defun lsp--editable-vector-match-inline (widget value)
  "Value for `lsp-editable-vector' :match-inline."
  (let ((type (nth 0 (widget-get widget :args)))
        (ok t)
        found)
    (while (and value ok)
      (let ((answer (widget-match-inline type value)))
        (if answer
            (let ((head (if (vectorp answer) (aref answer 0) (car answer)))
                  (tail (if (vectorp answer) (seq-drop 1 answer) (cdr answer))))
              (setq found (append found head)
                    value tail))
          (setq ok nil))))
    (cons found value)))

(defun lsp--editable-vector-value-to-external (_widget internal-value)
  "Convert the internal list value to a vector."
  (if (listp internal-value)
      (apply 'vector internal-value)
    internal-value))

(defun lsp--editable-vector-value-to-internal (_widget external-value)
  "Convert the external vector value to a list."
  (if (vectorp external-value)
      (append external-value nil)
    external-value))

(define-widget 'lsp--editable-vector 'editable-list
  "A subclass of `editable-list' that accepts and returns a
vector instead of a list."
  :value-to-external 'lsp--editable-vector-value-to-external
  :value-to-internal 'lsp--editable-vector-value-to-internal
  :match 'lsp--editable-vector-match
  :match-inline 'lsp--editable-vector-match-inline)

(define-widget 'lsp-repeatable-vector 'lsp--editable-vector
  "A variable length homogeneous vector."
  :tag "Repeat"
  :format "%{%t%}:\n%v%i\n")

(define-widget 'lsp-string-vector 'lazy
  "A vector of zero or more elements, every element of which is a string.
Appropriate for any language-specific `defcustom' that needs to
serialize as a JSON array of strings.

Deprecated. Use `lsp-repeatable-vector' instead. "
  :offset 4
  :tag "Vector"
  :type '(lsp-repeatable-vector string))

(make-obsolete 'lsp-string-vector nil "lsp-mode 8.0.0")

(defvar lsp--show-message t
  "If non-nil, show debug message from `lsp-mode'.")

(defun lsp--message  (format &rest args)
  "Wrapper for `message'

We `inhibit-message' the message when the cursor is in the
minibuffer and when emacs version is before emacs 27 due to the
fact that we often use `lsp--info', `lsp--warn' and `lsp--error'
in async context and the call to these function is removing the
minibuffer prompt. The issue with async messages is already fixed
in emacs 27.

See #2049"
  (when lsp--show-message
    (let ((inhibit-message (and (minibufferp)
                                (version< emacs-version "27.0"))))
      (apply #'message format args))))

(defun lsp--info (format &rest args)
  "Display lsp info message with FORMAT with ARGS."
  (lsp--message "%s :: %s" (propertize "LSP" 'face 'success) (apply #'format format args)))

(defun lsp--warn (format &rest args)
  "Display lsp warn message with FORMAT with ARGS."
  (lsp--message "%s :: %s" (propertize "LSP" 'face 'warning) (apply #'format format args)))

(defun lsp--error (format &rest args)
  "Display lsp error message with FORMAT with ARGS."
  (lsp--message "%s :: %s" (propertize "LSP" 'face 'error) (apply #'format format args)))

(defun lsp--eldoc-message (&optional msg)
  "Show MSG in eldoc."
  (setq lsp--eldoc-saved-message msg)
  (run-with-idle-timer 0 nil (lambda ()
                               ;; XXX: new eldoc in Emacs 28
                               ;; recommends running the hook variable
                               ;; `eldoc-documentation-functions'
                               ;; instead of using eldoc-message
                               (with-no-warnings
                                 (eldoc-message msg)))))

(defun lsp-log (format &rest args)
  "Log message to the ’*lsp-log*’ buffer.

FORMAT and ARGS i the same as for `message'."
  (when lsp-log-max
    (let ((log-buffer (get-buffer "*lsp-log*"))
          (inhibit-read-only t))
      (unless log-buffer
        (setq log-buffer (get-buffer-create "*lsp-log*"))
        (with-current-buffer log-buffer
          (buffer-disable-undo)
          (view-mode 1)
          (set (make-local-variable 'lsp--log-lines) 0)))
      (with-current-buffer log-buffer
        (save-excursion
          (let* ((message (apply 'format format args))
                 ;; Count newlines in message.
                 (newlines (1+ (cl-loop with start = 0
                                        for count from 0
                                        while (string-match "\n" message start)
                                        do (setq start (match-end 0))
                                        finally return count))))
            (goto-char (point-max))

            ;; in case the buffer is not empty insert before last \n to preserve
            ;; the point position(in case it is in the end)
            (if (eq (point) (point-min))
                (progn
                  (insert "\n")
                  (backward-char))
              (backward-char)
              (insert "\n"))
            (insert message)

            (setq lsp--log-lines (+ lsp--log-lines newlines))

            (when (and (integerp lsp-log-max) (> lsp--log-lines lsp-log-max))
              (let ((to-delete (- lsp--log-lines lsp-log-max)))
                (goto-char (point-min))
                (forward-line to-delete)
                (delete-region (point-min) (point))
                (setq lsp--log-lines lsp-log-max)))))))))

(defalias 'lsp-message 'lsp-log)

(defalias 'lsp-ht 'ht)

(defalias 'lsp-file-local-name 'file-local-name)

(defun lsp-f-canonical (file-name)
  "Return the canonical FILE-NAME, without a trailing slash."
  (directory-file-name (expand-file-name file-name)))

(defalias 'lsp-canonical-file-name 'lsp-f-canonical)

(defun lsp-f-same? (path-a path-b)
  "Return t if PATH-A and PATH-B are references to the same file.
Symlinks are not followed."
  (when (and (f-exists? path-a)
             (f-exists? path-b))
    (equal
     (lsp-f-canonical (directory-file-name (f-expand path-a)))
     (lsp-f-canonical (directory-file-name (f-expand path-b))))))

(defun lsp-f-parent (path)
  "Return the parent directory to PATH.
Symlinks are not followed."
  (let ((parent (file-name-directory
                 (directory-file-name (f-expand path default-directory)))))
    (unless (lsp-f-same? path parent)
      (if (f-relative? path)
          (f-relative parent)
        (directory-file-name parent)))))

(defun lsp-f-ancestor-of? (path-a path-b)
  "Return t if PATH-A is an ancestor of PATH-B.
Symlinks are not followed."
  (unless (lsp-f-same? path-a path-b)
    (s-prefix? (concat (lsp-f-canonical path-a) (f-path-separator))
               (lsp-f-canonical path-b))))

(defun lsp--merge-results (results method)
  "Merge RESULTS by filtering the empty hash-tables and merging
the lists according to METHOD."
  (pcase (--map (if (vectorp it)
                    (append it nil) it)
                (-filter #'identity results))
    (`() ())
    ;; only one result - simply return it
    (`(,fst) fst)
    ;; multiple results merge it based on strategy
    (results
     (pcase method
       ("textDocument/hover" (pcase (seq-filter
                                     (-compose #'not #'lsp-empty?)
                                     results)
                               (`(,hover) hover)
                               (hovers (lsp-make-hover
                                        :contents
                                        (-mapcat
                                         (-lambda ((&Hover :contents))
                                           (if (and (sequencep contents)
                                                    (not (stringp contents)))
                                               (append contents ())
                                             (list contents)))
                                         hovers)))))
       ("textDocument/completion"
        (lsp-make-completion-list
         :is-incomplete (seq-some
                         #'lsp:completion-list-is-incomplete
                         results)
         :items (cl-mapcan (lambda (it) (append (if (lsp-completion-list? it)
                                                    (lsp:completion-list-items it)
                                                  it)
                                                nil))
                           results)))
       ("completionItem/resolve"
        (let ((item (cl-first results)))
          (when-let ((details (seq-filter #'identity
                                          (seq-map #'lsp:completion-item-detail? results))))
            (lsp:set-completion-item-detail?
             item
             (string-join details " ")))
          (when-let ((docs (seq-filter #'identity
                                       (seq-map #'lsp:completion-item-documentation? results))))
            (lsp:set-completion-item-documentation?
             item
             (lsp-make-markup-content
              :kind (or (seq-some (lambda (it)
                                    (when (equal (lsp:markup-content-kind it)
                                                 lsp/markup-kind-markdown)
                                      lsp/markup-kind-markdown))
                                  docs)
                        lsp/markup-kind-plain-text)
              :value (string-join (seq-map (lambda (doc)
                                             (or (lsp:markup-content-value doc)
                                                 (and (stringp doc) doc)))
                                           docs)
                                  "\n"))))
          (when-let ((edits (seq-filter #'identity
                                        (seq-map #'lsp:completion-item-additional-text-edits? results))))
            (lsp:set-completion-item-additional-text-edits?
             item
             (cl-mapcan (lambda (it) (if (seqp it) it (list it))) edits)))
          item))
       (_ (cl-mapcan (lambda (it) (if (seqp it) it (list it))) results))))))

(defun lsp--spinner-start ()
  "Start spinner indication."
  (condition-case _err (spinner-start 'progress-bar-filled) (error)))

(defun lsp--propertize (str type)
  "Propertize STR as per TYPE."
  (propertize str 'face (alist-get type lsp--message-type-face)))

(defun lsp-workspaces ()
  "Return the lsp workspaces associated with the current project."
  (if lsp--cur-workspace (list lsp--cur-workspace) lsp--buffer-workspaces))

(defun lsp--completing-read (prompt collection transform-fn &optional predicate
                                    require-match initial-input
                                    hist def inherit-input-method)
  "Wrap `completing-read' to provide transformation function and disable sort.

TRANSFORM-FN will be used to transform each of the items before displaying.

PROMPT COLLECTION PREDICATE REQUIRE-MATCH INITIAL-INPUT HIST DEF
INHERIT-INPUT-METHOD will be proxied to `completing-read' without changes."
  (let* ((col (--map (cons (funcall transform-fn it) it) collection))
         (completion (completing-read prompt
                                      (lambda (string pred action)
                                        (if (eq action 'metadata)
                                            `(metadata (display-sort-function . identity))
                                          (complete-with-action action col string pred)))
                                      predicate require-match initial-input hist
                                      def inherit-input-method)))
    (cdr (assoc completion col))))

(defmacro lsp-with-current-buffer (buffer-id &rest body)
  (declare (indent 1) (debug t))
  `(if-let ((wcb (plist-get ,buffer-id :with-current-buffer)))
       (with-lsp-workspaces (plist-get ,buffer-id :workspaces)
         (funcall wcb (lambda () ,@body)))
     (with-current-buffer ,buffer-id
       ,@body)))

(defvar lsp--throw-on-input nil
  "Make `lsp-*-while-no-input' throws `input' on interrupted.")

(defmacro lsp--catch (tag bodyform &rest handlers)
  "Catch TAG thrown in BODYFORM.
The return value from TAG will be handled in HANDLERS by `pcase'."
  (declare (debug (form form &rest (pcase-PAT body))) (indent 2))
  (let ((re-sym (make-symbol "re")))
    `(let ((,re-sym (catch ,tag ,bodyform)))
       (pcase ,re-sym
         ,@handlers))))

(defmacro lsp--while-no-input (&rest body)
  "Wrap BODY in `while-no-input' and respecting `non-essential'.
If `lsp--throw-on-input' is set, will throw if input is pending, else
return value of `body' or nil if interrupted."
  (declare (debug t) (indent 0))
  `(if non-essential
       (let ((res (while-no-input ,@body)))
         (cond
          ((and lsp--throw-on-input (equal res t))
           (throw 'input :interrupted))
          ((booleanp res) nil)
          (t res)))
     ,@body))

;; A ‘lsp--client’ object describes the client-side behavior of a language
;; server.  It is used to start individual server processes, each of which is
;; represented by a ‘lsp--workspace’ object.  Client objects are normally
;; created using ‘lsp-define-stdio-client’ or ‘lsp-define-tcp-client’.  Each
;; workspace refers to exactly one client, but there can be multiple workspaces
;; for a single client.
(cl-defstruct lsp--client
  ;; ‘language-id’ is a function that receives a buffer as a single argument
  ;; and should return the language identifier for that buffer.  See
  ;; https://microsoft.github.io/language-server-protocol/specification#textdocumentitem
  ;; for a list of language identifiers.  Also consult the documentation for
  ;; the language server represented by this client to find out what language
  ;; identifiers it supports or expects.
  (language-id nil)

  ;; ‘add-on?’ when set to t the server will be started no matter whether there
  ;; is another server handling the same mode.
  (add-on? nil)
  ;; ‘new-connection’ is a function that should start a language server process
  ;; and return a cons (COMMAND-PROCESS . COMMUNICATION-PROCESS).
  ;; COMMAND-PROCESS must be a process object representing the server process
  ;; just started.  COMMUNICATION-PROCESS must be a process (including pipe and
  ;; network processes) that ‘lsp-mode’ uses to communicate with the language
  ;; server using the language server protocol.  COMMAND-PROCESS and
  ;; COMMUNICATION-PROCESS may be the same process; in that case
  ;; ‘new-connection’ may also return that process as a single
  ;; object. ‘new-connection’ is called with two arguments, FILTER and
  ;; SENTINEL.  FILTER should be used as process filter for
  ;; COMMUNICATION-PROCESS, and SENTINEL should be used as process sentinel for
  ;; COMMAND-PROCESS.
  (new-connection nil)

  ;; ‘ignore-regexps’ is a list of regexps.  When a data packet from the
  ;; language server matches any of these regexps, it will be ignored.  This is
  ;; intended for dealing with language servers that output non-protocol data.
  (ignore-regexps nil)

  ;; ‘ignore-messages’ is a list of regexps.  When a message from the language
  ;; server matches any of these regexps, it will be ignored.  This is useful
  ;; for filtering out unwanted messages; such as servers that send nonstandard
  ;; message types, or extraneous log messages.
  (ignore-messages nil)

  ;; ‘notification-handlers’ is a hash table mapping notification method names
  ;; (strings) to functions handling the respective notifications.  Upon
  ;; receiving a notification, ‘lsp-mode’ will call the associated handler
  ;; function passing two arguments, the ‘lsp--workspace’ object and the
  ;; deserialized notification parameters.
  (notification-handlers (make-hash-table :test 'equal))

  ;; ‘request-handlers’ is a hash table mapping request method names
  ;; (strings) to functions handling the respective notifications.  Upon
  ;; receiving a request, ‘lsp-mode’ will call the associated handler function
  ;; passing two arguments, the ‘lsp--workspace’ object and the deserialized
  ;; request parameters.
  (request-handlers (make-hash-table :test 'equal))

  ;; ‘response-handlers’ is a hash table mapping integral JSON-RPC request
  ;; identifiers for pending asynchronous requests to functions handling the
  ;; respective responses.  Upon receiving a response from the language server,
  ;; ‘lsp-mode’ will call the associated response handler function with a
  ;; single argument, the deserialized response parameters.
  (response-handlers (make-hash-table :test 'eql))

  ;; ‘prefix-function’ is called for getting the prefix for completion.
  ;; The function takes no parameter and returns a cons (start . end) representing
  ;; the start and end bounds of the prefix. If it's not set, the client uses a
  ;; default prefix function."
  (prefix-function nil)

  ;; Contains mapping of scheme to the function that is going to be used to load
  ;; the file.
  (uri-handlers (make-hash-table :test #'equal))

  ;; ‘action-handlers’ is a hash table mapping action to a handler function. It
  ;; can be used in `lsp-execute-code-action' to determine whether the action
  ;; current client is interested in executing the action instead of sending it
  ;; to the server.
  (action-handlers (make-hash-table :test 'equal))

  ;; major modes supported by the client.
  major-modes
  ;; Function that will be called to decide if this language client
  ;; should manage a particular buffer. The function will be passed
  ;; the file name and major mode to inform the decision. Setting
  ;; `activation-fn' will override `major-modes', if
  ;; present.
  activation-fn
  ;; Break the tie when major-mode is supported by multiple clients.
  (priority 0)
  ;; Unique identifier for representing the client object.
  server-id
  ;; defines whether the client supports multi root workspaces.
  multi-root
  ;; Initialization options or a function that returns initialization options.
  initialization-options
  ;; Overrides semantic tokens faces for specific clients
  semantic-tokens-faces-overrides
  ;; Provides support for registering LSP Server specific capabilities.
  custom-capabilities
  ;; Function which returns the folders that are considered to be not projects but library files.
  ;; The function accepts one parameter currently active workspace.
  ;; See: https://github.com/emacs-lsp/lsp-mode/issues/225.
  library-folders-fn
  ;; function which will be called when opening file in the workspace to perform
  ;; client specific initialization. The function accepts one parameter
  ;; currently active workspace.
  before-file-open-fn
  ;; Function which will be called right after a workspace has been initialized.
  initialized-fn
  ;; ‘remote?’ indicate whether the client can be used for LSP server over TRAMP.
  (remote? nil)

  ;; ‘completion-in-comments?’ t if the client supports completion in comments.
  (completion-in-comments? nil)

  ;; ‘path->uri-fn’ the function to use for path->uri conversion for the client.
  (path->uri-fn nil)

  ;; ‘uri->path-fn’ the function to use for uri->path conversion for the client.
  (uri->path-fn nil)
  ;; Function that returns an environment structure that will be used
  ;; to set some environment variables when starting the language
  ;; server process. These environment variables enable some
  ;; additional features in the language server. The environment
  ;; structure is an alist of the form (KEY . VALUE), where KEY is a
  ;; string (regularly in all caps), and VALUE may be a string, a
  ;; boolean, or a sequence of strings.
  environment-fn

  ;; ‘after-open-fn’ workspace after open specific hooks.
  (after-open-fn nil)

  ;; ‘async-request-handlers’ is a hash table mapping request method names
  ;; (strings) to functions handling the respective requests that may take
  ;; time to finish.  Upon receiving a request, ‘lsp-mode’ will call the
  ;; associated handler function passing three arguments, the ‘lsp--workspace’
  ;; object, the deserialized request parameters and the callback which accept
  ;; result as its parameter.
  (async-request-handlers (make-hash-table :test 'equal))
  download-server-fn
  download-in-progress?
  buffers
  synchronize-sections)

(defun lsp-clients-executable-find (find-command &rest args)
  "Finds an executable by invoking a search command.

FIND-COMMAND is the executable finder that searches for the
actual language server executable. ARGS is a list of arguments to
give to FIND-COMMAND to find the language server.  Returns the
output of FIND-COMMAND if it exits successfully, nil otherwise.

Typical uses include finding an executable by invoking 'find' in
a project, finding LLVM commands on macOS with 'xcrun', or
looking up project-specific language servers for projects written
in the various dynamic languages, e.g. 'nvm', 'pyenv' and 'rbenv'
etc."
  (when-let* ((find-command-path (executable-find find-command))
              (executable-path
               (with-temp-buffer
                 (when (zerop (apply 'call-process find-command-path nil t nil args))
                   (buffer-substring-no-properties (point-min) (point-max))))))
    (string-trim executable-path)))

(defvar lsp--already-widened nil)

(defmacro lsp-save-restriction-and-excursion (&rest form)
  (declare (indent 0) (debug t))
  `(if lsp--already-widened
       (save-excursion ,@form)
     (-let [lsp--already-widened t]
       (save-restriction
         (widen)
         (save-excursion ,@form)))))

;; from http://emacs.stackexchange.com/questions/8082/how-to-get-buffer-position-given-line-number-and-column-number
(defun lsp--line-character-to-point (line character)
  "Return the point for character CHARACTER on line LINE."
  (or (lsp-virtual-buffer-call :line/character->point line character)
      (let ((inhibit-field-text-motion t))
        (lsp-save-restriction-and-excursion
          (goto-char (point-min))
          (forward-line line)
          ;; server may send character position beyond the current line and we
          ;; should fallback to line end.
          (-let [line-end (line-end-position)]
            (if (> character (- line-end (point)))
                line-end
              (forward-char character)
              (point)))))))

(lsp-defun lsp--position-to-point ((&Position :line :character))
  "Convert `Position' object in PARAMS to a point."
  (lsp--line-character-to-point line character))

(lsp-defun lsp--range-to-region ((&RangeToPoint :start :end))
  (cons start end))

(lsp-defun lsp--range-text ((&RangeToPoint :start :end))
  (buffer-substring start end))

(lsp-defun lsp--find-wrapping-range ((&SelectionRange :parent? :range (&RangeToPoint :start :end)))
  (cond
   ((and
     (region-active-p)
     (<= start (region-beginning) end)
     (<= start (region-end) end)
     (or (not (= start (region-beginning)))
         (not (= end (region-end)))))
    (cons start end))
   ((and (<= start (point) end)
         (not (region-active-p)))
    (cons start end))
   (parent? (lsp--find-wrapping-range parent?))))

(defun lsp--get-selection-range ()
  (or
   (-when-let ((cache . cache-tick) lsp--document-selection-range-cache)
     (when (= cache-tick (buffer-modified-tick)) cache))
   (let ((response (cl-first
                    (lsp-request
                     "textDocument/selectionRange"
                     (list :textDocument (lsp--text-document-identifier)
                           :positions (vector (lsp--cur-position)))))))
     (setq lsp--document-selection-range-cache
           (cons response (buffer-modified-tick)))
     response)))

(defun lsp-extend-selection ()
  "Extend selection."
  (interactive)
  (unless (lsp--capability :selectionRangeProvider)
    (signal 'lsp-capability-not-supported (list "selectionRangeProvider")))
  (-when-let ((start . end) (lsp--find-wrapping-range (lsp--get-selection-range)))
    (goto-char start)
    (set-mark (point))
    (goto-char end)
    (exchange-point-and-mark)))

(defun lsp-warn (message &rest args)
  "Display a warning message made from (`format-message' MESSAGE ARGS...).
This is equivalent to `display-warning', using `lsp-mode' as the type and
`:warning' as the level."
  (display-warning 'lsp-mode (apply #'format-message message args)))

(defun lsp--get-uri-handler (scheme)
  "Get uri handler for SCHEME in the current workspace."
  (--some (gethash scheme (lsp--client-uri-handlers (lsp--workspace-client it)))
          (or (lsp-workspaces) (lsp--session-workspaces (lsp-session)))))

(defun lsp--fix-path-casing (path)
  "On windows, downcases path because the windows file system is
case-insensitive.

On other systems, returns path without change."
  (if (eq system-type 'windows-nt) (downcase path) path))

(defun lsp--uri-to-path (uri)
  "Convert URI to a file path."
  (if-let ((fn (->> (lsp-workspaces)
                    (-keep (-compose #'lsp--client-uri->path-fn #'lsp--workspace-client))
                    (cl-first))))
      (funcall fn uri)
    (lsp--uri-to-path-1 uri)))

(defun lsp-remap-path-if-needed (file-name)
  (-if-let ((virtual-buffer &as &plist :buffer) (gethash file-name lsp--virtual-buffer-mappings))
      (propertize (buffer-local-value 'buffer-file-name buffer)
                  'lsp-virtual-buffer virtual-buffer)
    file-name))

(defun lsp--uri-to-path-1 (uri)
  "Convert URI to a file path."
  (let* ((url (url-generic-parse-url (url-unhex-string uri)))
         (type (url-type url))
         (target (url-target url))
         (file
          (concat (decode-coding-string (url-filename url)
                                        (or locale-coding-system 'utf-8))
                  (when target
                    (concat "#" target))))
         (file-name (if (and type (not (string= type "file")))
                        (if-let ((handler (lsp--get-uri-handler type)))
                            (funcall handler uri)
                          uri)
                      ;; `url-generic-parse-url' is buggy on windows:
                      ;; https://github.com/emacs-lsp/lsp-mode/pull/265
                      (or (and (eq system-type 'windows-nt)
                               (eq (elt file 0) ?\/)
                               (substring file 1))
                          file))))
    (->> file-name
         (concat (-some #'lsp--workspace-host-root (lsp-workspaces)))
         (lsp-remap-path-if-needed))))

(defun lsp--buffer-uri ()
  "Return URI of the current buffer."
  (or lsp-buffer-uri
      (plist-get lsp--virtual-buffer :buffer-uri)
      (lsp--path-to-uri
       (or (buffer-file-name) (buffer-file-name (buffer-base-buffer))))))

(defun lsp-register-client-capabilities (&rest _args)
  "Implemented only to make `company-lsp' happy.
DELETE when `lsp-mode.el' is deleted.")

(defconst lsp--url-path-allowed-chars
  (url--allowed-chars (append '(?/) url-unreserved-chars))
  "`url-unreserved-chars' with additional delim ?/.
This set of allowed chars is enough for hexifying local file paths.")

(defun lsp--path-to-uri-1 (path)
  (concat lsp--uri-file-prefix
          (--> path
            (expand-file-name it)
            (or (file-remote-p it 'localname t) it)
            (url-hexify-string it lsp--url-path-allowed-chars))))

(defun lsp--path-to-uri (path)
  "Convert PATH to a uri."
  (if-let ((uri-fn (->> (lsp-workspaces)
                        (-keep (-compose #'lsp--client-path->uri-fn #'lsp--workspace-client))
                        (cl-first))))
      (funcall uri-fn path)
    (lsp--path-to-uri-1 path)))

(defun lsp--string-match-any (regex-list str)
  "Return the first regex, if any, within REGEX-LIST matching STR."
  (--first (string-match it str) regex-list))

(cl-defstruct lsp-watch
  (descriptors (make-hash-table :test 'equal))
  root-directory)

(defun lsp--folder-watch-callback (event callback watch ignored-files ignored-directories)
  (let ((file-name (cl-third event))
        (event-type (cl-second event)))
    (cond
     ((and (file-directory-p file-name)
           (equal 'created event-type)
           (not (lsp--string-match-any ignored-directories file-name)))

      (lsp-watch-root-folder (file-truename file-name) callback ignored-files ignored-directories watch)

      ;; process the files that are already present in
      ;; the directory.
      (->> (directory-files-recursively file-name ".*" t)
           (seq-do (lambda (f)
                     (unless (file-directory-p f)
                       (funcall callback (list nil 'created f)))))))
     ((and (memq event-type '(created deleted changed))
           (not (file-directory-p file-name))
           (not (lsp--string-match-any ignored-files file-name)))
      (funcall callback event))
     ((and (memq event-type '(renamed))
           (not (file-directory-p file-name))
           (not (lsp--string-match-any ignored-files file-name)))
      (funcall callback `(,(cl-first event) deleted ,(cl-third event)))
      (funcall callback `(,(cl-first event) created ,(cl-fourth event)))))))

(defun lsp--ask-about-watching-big-repo (number-of-directories dir)
  "Ask the user if they want to watch NUMBER-OF-DIRECTORIES from a repository DIR.
This is useful when there is a lot of files in a repository, as
that may slow Emacs down. Returns t if the user wants to watch
the entire repository, nil otherwise."
  (prog1
      (yes-or-no-p
       (format
        "Watching all the files in %s would require adding watches to %s directories, so watching the repo may slow Emacs down.
Do you want to watch all files in %s? "
        dir
        number-of-directories
        dir))
    (lsp--info
     (concat "You can configure this warning with the `lsp-enable-file-watchers' "
             "and `lsp-file-watch-threshold' variables"))))


(defun lsp--path-is-watchable-directory (path dir ignored-directories)
  "Figure out whether PATH (inside of DIR) is meant to have a file watcher set.
IGNORED-DIRECTORIES is a list of regexes to filter out directories we don't want to watch."
  (let
      ((full-path (f-join dir path)))
    (and (f-dir-p full-path)
         (not (equal path "."))
         (not (equal path ".."))
         (not (lsp--string-match-any ignored-directories full-path)))))


(defun lsp--all-watchable-directories (dir ignored-directories)
  "Traverse DIR recursively and return a list of paths that should have watchers set on them.
IGNORED-DIRECTORIES will be used for exclusions"
  (let* ((dir (if (f-symlink? dir)
                  (file-truename dir)
                dir)))
    (apply #'nconc
           ;; the directory itself is assumed to be part of the set
           (list dir)
           ;; collect all subdirectories that are watchable
           (-map
            (lambda (path) (lsp--all-watchable-directories (f-join dir path) ignored-directories))
            ;; but only look at subdirectories that are watchable
            (-filter (lambda (path) (lsp--path-is-watchable-directory path dir ignored-directories))
                     (directory-files dir))))))

(defun lsp-watch-root-folder (dir callback ignored-files ignored-directories &optional watch warn-big-repo?)
  "Create recursive file notification watch in DIR.
CALLBACK will be called when there are changes in any of
the monitored files. WATCHES is a hash table directory->file
notification handle which contains all of the watch that
already have been created. Watches will not be created for
any directory that matches any regex in IGNORED-DIRECTORIES.
Watches will not be created for any file that matches any
regex in IGNORED-FILES."
  (let* ((dir (if (f-symlink? dir)
                  (file-truename dir)
                dir))
         (watch (or watch (make-lsp-watch :root-directory dir)))
         (dirs-to-watch (lsp--all-watchable-directories dir ignored-directories)))
    (lsp-log "Creating watchers for following %s folders:\n  %s"
             (length dirs-to-watch)
             (s-join "\n  " dirs-to-watch))
    (when (or
           (not warn-big-repo?)
           (not lsp-file-watch-threshold)
           (let ((number-of-directories (length dirs-to-watch)))
             (or
              (< number-of-directories lsp-file-watch-threshold)
              (condition-case _err
                  (lsp--ask-about-watching-big-repo number-of-directories dir)
                ('quit)))))
      (dolist (current-dir dirs-to-watch)
        (condition-case err
            (progn
              (puthash
               current-dir
               (file-notify-add-watch current-dir
                                      '(change)
                                      (lambda (event)
                                        (lsp--folder-watch-callback event callback watch ignored-files ignored-directories)))
               (lsp-watch-descriptors watch)))
          (error (lsp-log "Failed to create a watch for %s: message" (error-message-string err)))
          (file-missing (lsp-log "Failed to create a watch for %s: message" (error-message-string err))))))
    watch))

(defun lsp-kill-watch (watch)
  "Delete WATCH."
  (-> watch lsp-watch-descriptors hash-table-values (-each #'file-notify-rm-watch))
  (ht-clear! (lsp-watch-descriptors watch)))

(defun lsp-json-bool (val)
  "Convert VAL to JSON boolean."
  (if val t :json-false))

(defmacro with-lsp-workspace (workspace &rest body)
  "Helper macro for invoking BODY in WORKSPACE context."
  (declare (debug (form body))
           (indent 1))
  `(let ((lsp--cur-workspace ,workspace)) ,@body))

(defmacro with-lsp-workspaces (workspaces &rest body)
  "Helper macro for invoking BODY against multiple WORKSPACES."
  (declare (debug (form body))
           (indent 1))
  `(let ((lsp--buffer-workspaces ,workspaces)) ,@body))



(defconst lsp-downstream-deps
  '(;; external packages
    ccls consult-lsp dap-mode helm-lsp lsp-dart lsp-docker lsp-focus lsp-grammarly
    lsp-haskell lsp-ivy lsp-java lsp-javacomp lsp-jedi lsp-julia lsp-latex lsp-ltex
    lsp-metals lsp-mssql lsp-origami lsp-p4 lsp-pascal lsp-pyre lsp-pyright
    lsp-python-ms lsp-rescript lsp-sonarlint lsp-sourcekit lsp-tailwindcss lsp-treemacs
    lsp-ui swift-helpful
    ;; clients
    lsp-actionscript lsp-ada lsp-angular lsp-bash lsp-beancount lsp-clangd
    lsp-clojure lsp-cmake lsp-crystal lsp-csharp lsp-css lsp-d lsp-dhall
    lsp-dockerfile lsp-elixir lsp-elm lsp-erlang lsp-eslint lsp-fortran lsp-fsharp lsp-gdscript
    lsp-go lsp-graphql lsp-groovy lsp-hack lsp-haxe lsp-html lsp-idris lsp-javascript lsp-json lsp-kotlin lsp-lua
    lsp-markdown lsp-nginx lsp-nim lsp-nix lsp-ocaml lsp-perl lsp-perlnavigator lsp-php lsp-prolog lsp-purescript lsp-pwsh
    lsp-pyls lsp-pylsp lsp-racket lsp-r lsp-rf lsp-rust lsp-solargraph lsp-sorbet lsp-sqls
    lsp-steep lsp-svelte lsp-terraform lsp-tex lsp-toml lsp-ttcn3 lsp-typeprof lsp-v lsp-vala lsp-verilog
    lsp-vetur lsp-volar lsp-vhdl lsp-vimscript lsp-xml lsp-yaml lsp-zig)
  "List of downstream deps.")

(defmacro lsp-consistency-check (package)
  `(defconst ,(intern (concat (symbol-name package)
                              "-plist-value-when-compiled"))
     (eval-when-compile lsp-use-plists)))

;; (mapc
;;  (lambda (package)
;;    (with-eval-after-load package
;;      (let ((symbol-name (intern
;;                          (concat (symbol-name package)
;;                                  "-plist-value-when-compiled"))))
;;        (cond
;;         ((not (boundp symbol-name))
;;          (warn "We have detected that you are using version of %s that is not compatible with current version of lsp-mode.el, please update it." (propertize (symbol-name package)
;;                                                                                                                                                              'face 'bold)))
;;         ((not (eq (symbol-value symbol-name) lsp-use-plists))
;;          (warn "Package %s is inconsistent with lsp-mode.el. This is indication of race during installation. In order to solve that please delete all packages related to lsp-mode, restar Emacs and install them again." (propertize (symbol-name package) 'face 'bold)))))))
;;  lsp-downstream-deps)


;; loading code-workspace files

;;;###autoload
(defun lsp-load-vscode-workspace (file)
  "Load vscode workspace from FILE"
  (interactive "fSelect file to import: ")
  (mapc #'lsp-workspace-folders-remove (lsp-session-folders (lsp-session)))

  (let ((dir (f-dirname file)))
    (->> file
         (json-read-file)
         (alist-get 'folders)
         (-map (-lambda ((&alist 'path))
                 (lsp-workspace-folders-add (expand-file-name path dir)))))))

;;;###autoload
(defun lsp-save-vscode-workspace (file)
  "Save vscode workspace to FILE"
  (interactive "FSelect file to save to: ")

  (let ((json-encoding-pretty-print t))
    (f-write-text (json-encode
                   `((folders . ,(->> (lsp-session)
                                      (lsp-session-folders)
                                      (--map `((path . ,it)))))))
                  'utf-8
                  file)))


(defmacro lsp-foreach-workspace (&rest body)
  "Execute BODY for each of the current workspaces."
  (declare (debug (form body)))
  `(--map (with-lsp-workspace it ,@body) (lsp-workspaces)))

(defmacro when-lsp-workspace (workspace &rest body)
  "Helper macro for invoking BODY in WORKSPACE context if present."
  (declare (debug (form body))
           (indent 1))
  `(when-let ((lsp--cur-workspace ,workspace)) ,@body))

(lsp-defun lsp--window-show-quick-pick (_workspace (&ShowQuickPickParams :place-holder :can-pick-many :items))
  (if-let* ((selectfunc (if can-pick-many #'completing-read-multiple #'completing-read))
            (itemLabels (seq-map (-lambda ((item &as &QuickPickItem :label)) (format "%s" label))
                                 items))
            (result (funcall-interactively
                     selectfunc
                     (format "%s%s " place-holder (if can-pick-many " (* for all)" "")) itemLabels))
            (choices (if (listp result)
                         (if (equal result '("*"))
                             itemLabels
                           result)
                       (list result))))
      (vconcat (seq-filter #'identity (seq-map (-lambda ((item &as &QuickPickItem :label :user-data))
                                                 (if (member label choices)
                                                     (lsp-make-quick-pick-item :label label :picked t :user-data user-data)
                                                   nil))
                                               items)))))

(lsp-defun lsp--window-show-input-box (_workspace (&ShowInputBoxParams :prompt :value?))
  (read-string (format "%s: " prompt) (or value? "")))

(lsp-defun lsp--window-show-message (_workspace (&ShowMessageRequestParams :message :type))
  "Send the server's messages to log.
PARAMS - the data sent from _WORKSPACE."
  (funcall (cl-case type
             (1 'lsp--error)
             (2 'lsp--warn)
             (t 'lsp--info))
           "%s"
           message))

(lsp-defun lsp--window-log-message (workspace (&ShowMessageRequestParams :message :type))
  "Send the server's messages to log.
PARAMS - the data sent from WORKSPACE."
  (ignore
   (let ((client (lsp--workspace-client workspace)))
     (when (or (not client)
               (cl-notany (-rpartial #'string-match-p message)
                          (lsp--client-ignore-messages client)))
       (lsp-log "%s" (lsp--propertize message type))))))

(lsp-defun lsp--window-log-message-request ((&ShowMessageRequestParams :message :type :actions?))
  "Display a message request to the user and send the user's selection back to the server."
  (let* ((message (lsp--propertize message type))
         (choices (seq-map #'lsp:message-action-item-title actions?)))
    (if choices
        (completing-read (concat message " ") (seq-into choices 'list) nil t)
      (lsp-log message))))

(lsp-defun lsp--window-show-document ((&ShowDocumentParams :uri :selection?))
  "Show document URI in a buffer and go to SELECTION if any."
  (let ((path (lsp--uri-to-path uri)))
    (when (f-exists? path)
      (with-current-buffer (find-file path)
        (when selection?
          (goto-char (lsp--position-to-point (lsp:range-start selection?))))
        t))))

(defcustom lsp-progress-prefix " ⌛ "
  "Progress prefix."
  :group 'lsp-mode
  :type 'string
  :package-version '(lsp-mode . "8.0.0"))

(defcustom lsp-progress-function #'lsp-on-progress-modeline
  "Function for handling the progress notifications."
  :group 'lsp-mode
  :type '(choice
          (const :tag "Use modeline" lsp-on-progress-modeline)
          (const :tag "Legacy(uses either `progress-reporter' or `spinner' based on `lsp-progress-via-spinner')"
                 lsp-on-progress-legacy)
          (const ignore :tag "Ignore")
          (function :tag "Other function"))
  :package-version '(lsp-mode . "8.0.0"))

(defun lsp--progress-status ()
  "Returns the status of the progress for the current workspaces."
  (-let ((progress-status
          (s-join
           "|"
           (-keep
            (lambda (workspace)
              (let ((tokens (lsp--workspace-work-done-tokens workspace)))
                (unless (ht-empty? tokens)
                  (mapconcat
                   (-lambda ((&WorkDoneProgressBegin :message? :title :percentage?))
                     (concat (if percentage?
                                 (if (numberp percentage?)
                                     (format "%.0f%%%% " percentage?)
                                   (format "%s%%%% " percentage?))
                               "")
                             (or message? title)))
                   (ht-values tokens)
                   "|"))))
            (lsp-workspaces)))))
    (unless (s-blank? progress-status)
      (concat lsp-progress-prefix progress-status))))

(lsp-defun lsp-on-progress-modeline (workspace (&ProgressParams :token :value
                                                                (value &as &WorkDoneProgress :kind)))
  "PARAMS contains the progress data.
WORKSPACE is the workspace that contains the progress token."
  (add-to-list 'global-mode-string '(t (:eval (lsp--progress-status))))
  (pcase kind
    ("begin" (lsp-workspace-set-work-done-token token value workspace))
    ("report" (lsp-workspace-set-work-done-token token value workspace))
    ("end" (lsp-workspace-rem-work-done-token token workspace)))
  (force-mode-line-update))

(lsp-defun lsp-on-progress-legacy (workspace (&ProgressParams :token :value
                                                              (value &as &WorkDoneProgress :kind)))
  "PARAMS contains the progress data.
WORKSPACE is the workspace that contains the progress token."
  (pcase kind
    ("begin"
     (-let* (((&WorkDoneProgressBegin :title :percentage?) value)
             (reporter
              (if lsp-progress-via-spinner
                  (let* ((spinner-strings (alist-get 'progress-bar spinner-types))
                         ;; Set message as a tooltip for the spinner strings
                         (propertized-strings
                          (seq-map (lambda (string) (propertize string 'help-echo title))
                                   spinner-strings))
                         (spinner-type (vconcat propertized-strings)))
                    ;; The progress relates to the server as a whole,
                    ;; display it on all buffers.
                    (mapcar (lambda (buffer)
                              (lsp-with-current-buffer buffer
                                (spinner-start spinner-type))
                              buffer)
                            (lsp--workspace-buffers workspace)))
                (if percentage?
                    (make-progress-reporter title 0 100 percentage?)
                  ;; No percentage, just progress
                  (make-progress-reporter title nil nil)))))
       (lsp-workspace-set-work-done-token token reporter workspace)))
    ("report"
     (when-let ((reporter (lsp-workspace-get-work-done-token token workspace)))
       (unless lsp-progress-via-spinner
         (progress-reporter-update reporter (lsp:work-done-progress-report-percentage? value)))))

    ("end"
     (when-let ((reporter (lsp-workspace-get-work-done-token token workspace)))
       (if lsp-progress-via-spinner
           (mapc (lambda (buffer)
                   (when (lsp-buffer-live-p buffer)
                     (lsp-with-current-buffer buffer
                       (spinner-stop))))
                 reporter)
         (progress-reporter-done reporter))
       (lsp-workspace-rem-work-done-token token workspace)))))


;; diagnostics

(defvar lsp-diagnostic-filter nil
  "A a function which will be called with
  `&PublishDiagnosticsParams' and `workspace' which can be used
  to filter out the diagnostics. The function should return
  `&PublishDiagnosticsParams'.

Common usecase are:
1. Filter the diagnostics for a particular language server.
2. Filter out the diagnostics under specific level.")

(defvar lsp-diagnostic-stats (ht))

(defun lsp-diagnostics (&optional current-workspace?)
  "Return the diagnostics from all workspaces."
  (or (pcase (if current-workspace?
                 (lsp-workspaces)
               (lsp--session-workspaces (lsp-session)))
        (`() ())
        (`(,workspace) (lsp--workspace-diagnostics workspace))
        (`,workspaces (let ((result (make-hash-table :test 'equal)))
                        (mapc (lambda (workspace)
                                (->> workspace
                                     (lsp--workspace-diagnostics)
                                     (maphash (lambda (file-name diagnostics)
                                                (puthash file-name
                                                         (append (gethash file-name result) diagnostics)
                                                         result)))))
                              workspaces)
                        result)))
      (ht)))

(defun lsp-diagnostics-stats-for (path)
  "Get diagnostics statistics for PATH.
The result format is vector [_ errors warnings infos hints] or nil."
  (gethash (lsp--fix-path-casing path) lsp-diagnostic-stats))

(defun lsp-diagnostics--update-path (path new-stats)
  (let ((new-stats (copy-sequence new-stats))
        (path (lsp--fix-path-casing (directory-file-name path))))
    (if-let ((old-data (gethash path lsp-diagnostic-stats)))
        (dotimes (idx 5)
          (cl-callf + (aref old-data idx)
            (aref new-stats idx)))
      (puthash path new-stats lsp-diagnostic-stats))))

(lsp-defun lsp--on-diagnostics-update-stats (workspace
                                             (&PublishDiagnosticsParams :uri :diagnostics))
  (let ((path (lsp--fix-path-casing (lsp--uri-to-path uri)))
        (new-stats (make-vector 5 0)))
    (mapc (-lambda ((&Diagnostic :severity?))
            (cl-incf (aref new-stats (or severity? 1))))
          diagnostics)
    (when-let ((old-diags (gethash path (lsp--workspace-diagnostics workspace))))
      (mapc (-lambda ((&Diagnostic :severity?))
              (cl-decf (aref new-stats (or severity? 1))))
            old-diags))
    (lsp-diagnostics--update-path path new-stats)
    (while (not (string= path (setf path (file-name-directory
                                          (directory-file-name path)))))
      (lsp-diagnostics--update-path path new-stats))))

(defun lsp--on-diagnostics (workspace params)
  "Callback for textDocument/publishDiagnostics.
interface PublishDiagnosticsParams {
    uri: string;
    diagnostics: Diagnostic[];
}
PARAMS contains the diagnostics data.
WORKSPACE is the workspace that contains the diagnostics."
  (when lsp-diagnostic-filter
    (setf params (funcall lsp-diagnostic-filter params workspace)))

  (lsp--on-diagnostics-update-stats workspace params)

  (-let* (((&PublishDiagnosticsParams :uri :diagnostics) params)
          (lsp--virtual-buffer-mappings (ht))
          (file (lsp--fix-path-casing (lsp--uri-to-path uri)))
          (workspace-diagnostics (lsp--workspace-diagnostics workspace)))

    (if (seq-empty-p diagnostics)
        (remhash file workspace-diagnostics)
      (puthash file (append diagnostics nil) workspace-diagnostics))

    (run-hooks 'lsp-diagnostics-updated-hook)))

(defun lsp-diagnostics--workspace-cleanup (workspace)
  (->> workspace
       (lsp--workspace-diagnostics)
       (maphash (lambda (key _)
                  (lsp--on-diagnostics-update-stats
                   workspace
                   (lsp-make-publish-diagnostics-params
                    :uri (lsp--path-to-uri key)
                    :diagnostics [])))))
  (clrhash (lsp--workspace-diagnostics workspace)))



;; textDocument/foldingRange support

(cl-defstruct lsp--folding-range beg end kind children)

(defvar-local lsp--cached-folding-ranges nil)
(defvar-local lsp--cached-nested-folding-ranges nil)

(defun lsp--folding-range-width (range)
  (- (lsp--folding-range-end range)
     (lsp--folding-range-beg range)))

(defun lsp--get-folding-ranges ()
  "Get the folding ranges for the current buffer."
  (unless (eq (buffer-chars-modified-tick) (car lsp--cached-folding-ranges))
    (let* ((ranges (lsp-request "textDocument/foldingRange"
                                `(:textDocument ,(lsp--text-document-identifier))))
           (sorted-line-col-pairs (->> ranges
                                       (cl-mapcan (-lambda ((&FoldingRange :start-line
                                                                           :start-character?
                                                                           :end-line
                                                                           :end-character?))
                                                    (list (cons start-line start-character?)
                                                          (cons end-line end-character?))))
                                       (-sort #'lsp--line-col-comparator)))
           (line-col-to-point-map (lsp--convert-line-col-to-points-batch
                                   sorted-line-col-pairs)))
      (setq lsp--cached-folding-ranges
            (cons (buffer-chars-modified-tick)
                  (--> ranges
                    (seq-map (-lambda ((range &as
                                              &FoldingRange :start-line
                                              :start-character?
                                              :end-line
                                              :end-character?
                                              :kind?))
                               (make-lsp--folding-range
                                :beg (ht-get line-col-to-point-map
                                             (cons start-line start-character?))
                                :end (ht-get line-col-to-point-map
                                             (cons end-line end-character?))
                                :kind kind?))
                             it)
                    (seq-filter (lambda (folding-range)
                                  (< (lsp--folding-range-beg folding-range)
                                     (lsp--folding-range-end folding-range)))
                                it)
                    (seq-into it 'list)
                    (delete-dups it))))))
  (cdr lsp--cached-folding-ranges))

(defun lsp--get-nested-folding-ranges ()
  "Get a list of nested folding ranges for the current buffer."
  (-let [(tick . _) lsp--cached-folding-ranges]
    (if (and (eq tick (buffer-chars-modified-tick))
             lsp--cached-nested-folding-ranges)
        lsp--cached-nested-folding-ranges
      (setq lsp--cached-nested-folding-ranges
            (lsp--folding-range-build-trees (lsp--get-folding-ranges))))))

(defun lsp--folding-range-build-trees (ranges)
  (setq ranges (seq-sort #'lsp--range-before-p ranges))
  (let* ((dummy-node (make-lsp--folding-range
                      :beg most-negative-fixnum
                      :end most-positive-fixnum))
         (stack (list dummy-node)))
    (dolist (range ranges)
      (while (not (lsp--range-inside-p range (car stack)))
        (pop stack))
      (push range (lsp--folding-range-children (car stack)))
      (push range stack))
    (lsp--folding-range-children dummy-node)))

(defun lsp--range-inside-p (r1 r2)
  "Return non-nil if folding range R1 lies inside R2"
  (and (>= (lsp--folding-range-beg r1) (lsp--folding-range-beg r2))
       (<= (lsp--folding-range-end r1) (lsp--folding-range-end r2))))

(defun lsp--range-before-p (r1 r2)
  "Return non-nil if folding range R1 ends before R2"
  ;; Ensure r1 comes before r2
  (or (< (lsp--folding-range-beg r1)
         (lsp--folding-range-beg r2))
      ;; If beg(r1) == beg(r2) make sure r2 ends first
      (and (= (lsp--folding-range-beg r1)
              (lsp--folding-range-beg r2))
           (< (lsp--folding-range-end r2)
              (lsp--folding-range-end r1)))))

(defun lsp--point-inside-range-p (point range)
  "Return non-nil if POINT lies inside folding range RANGE."
  (and (>= point (lsp--folding-range-beg range))
       (<= point (lsp--folding-range-end range))))

(cl-defun lsp--get-current-innermost-folding-range (&optional (point (point)))
  "Return the innermost folding range POINT lies in."
  (seq-reduce (lambda (innermost-range curr-range)
                (if (and (lsp--point-inside-range-p point curr-range)
                         (or (null innermost-range)
                             (lsp--range-inside-p curr-range innermost-range)))
                    curr-range
                  innermost-range))
              (lsp--get-folding-ranges)
              nil))

(cl-defun lsp--get-current-outermost-folding-range (&optional (point (point)))
  "Return the outermost folding range POINT lies in."
  (cdr (seq-reduce (-lambda ((best-pair &as outermost-width . _) curr-range)
                     (let ((curr-width (lsp--folding-range-width curr-range)))
                       (if (and (lsp--point-inside-range-p point curr-range)
                                (or (null best-pair)
                                    (> curr-width outermost-width)))
                           (cons curr-width curr-range)
                         best-pair)))
                   (lsp--get-folding-ranges)
                   nil)))

(defun lsp--folding-range-at-point-bounds ()
  (if (and (or (lsp--capability :foldingRangeProvider)
               (lsp--registered-capability "textDocument/foldingRange"))
           lsp-enable-folding)
      (if-let ((range (lsp--get-current-innermost-folding-range)))
          (cons (lsp--folding-range-beg range)
                (lsp--folding-range-end range)))
    nil))
(put 'lsp--folding-range 'bounds-of-thing-at-point
     #'lsp--folding-range-at-point-bounds)

(defun lsp--get-nearest-folding-range (&optional backward)
  (let ((point (point))
        (found nil))
    (while (not
            (or found
                (if backward
                    (<= point (point-min))
                  (>= point (point-max)))))
      (if backward (cl-decf point) (cl-incf point))
      (setq found (lsp--get-current-innermost-folding-range point)))
    found))

(defun lsp--folding-range-at-point-forward-op (n)
  (when (and (or (lsp--capability :foldingRangeProvider)
                 (lsp--registered-capability "textDocument/foldingRange"))
             lsp-enable-folding
             (not (zerop n)))
    (cl-block break
      (dotimes (_ (abs n))
        (if-let ((range (lsp--get-nearest-folding-range (< n 0))))
            (goto-char (if (< n 0)
                           (lsp--folding-range-beg range)
                         (lsp--folding-range-end range)))
          (cl-return-from break))))))
(put 'lsp--folding-range 'forward-op
     #'lsp--folding-range-at-point-forward-op)

(defun lsp--folding-range-at-point-beginning-op ()
  (goto-char (car (lsp--folding-range-at-point-bounds))))
(put 'lsp--folding-range 'beginning-op
     #'lsp--folding-range-at-point-beginning-op)

(defun lsp--folding-range-at-point-end-op ()
  (goto-char (cdr (lsp--folding-range-at-point-bounds))))
(put 'lsp--folding-range 'end-op
     #'lsp--folding-range-at-point-end-op)

(defun lsp--range-at-point-bounds ()
  (or (lsp--folding-range-at-point-bounds)
      (when-let ((range (and
                         (lsp--capability :hoverProvider)
                         (->> (lsp--text-document-position-params)
                              (lsp-request "textDocument/hover")
                              (lsp:hover-range?)))))
        (lsp--range-to-region range))))

;; A more general purpose "thing", useful for applications like focus.el
(put 'lsp--range 'bounds-of-thing-at-point
     #'lsp--range-at-point-bounds)

(defun lsp--log-io-p (method)
  "Return non nil if should log for METHOD."
  (and lsp-log-io
       (or (not lsp-log-io-allowlist-methods)
           (member method lsp-log-io-allowlist-methods))))


;; toggles

(defun lsp-toggle-trace-io ()
  "Toggle client-server protocol logging."
  (interactive)
  (setq lsp-log-io (not lsp-log-io))
  (lsp--info "Server logging %s." (if lsp-log-io "enabled" "disabled")))

(defun lsp-toggle-signature-auto-activate ()
  "Toggle signature auto activate."
  (interactive)
  (setq lsp-signature-auto-activate
        (unless lsp-signature-auto-activate '(:on-trigger-char)))
  (lsp--info "Signature autoactivate %s." (if lsp-signature-auto-activate "enabled" "disabled"))
  (lsp--update-signature-help-hook))

(defun lsp-toggle-on-type-formatting ()
  "Toggle on type formatting."
  (interactive)
  (setq lsp-enable-on-type-formatting (not lsp-enable-on-type-formatting))
  (lsp--info "On type formatting is %s." (if lsp-enable-on-type-formatting "enabled" "disabled"))
  (lsp--update-on-type-formatting-hook))

(defun lsp-toggle-symbol-highlight ()
  "Toggle symbol highlighting."
  (interactive)
  (setq lsp-enable-symbol-highlighting (not lsp-enable-symbol-highlighting))

  (cond
   ((and lsp-enable-symbol-highlighting
         (lsp-feature? "textDocument/documentHighlight"))
    (add-hook 'lsp-on-idle-hook #'lsp--document-highlight nil t)
    (lsp--info "Symbol highlighting enabled in current buffer."))
   ((not lsp-enable-symbol-highlighting)
    (remove-hook 'lsp-on-idle-hook #'lsp--document-highlight t)
    (lsp--remove-overlays 'lsp-highlight)
    (lsp--info "Symbol highlighting disabled in current buffer."))))


;; keybindings
(defvar lsp--binding-descriptions nil
  "List of key binding/short description pair.")

(defmacro lsp-define-conditional-key (keymap key def desc cond &rest bindings)
  "In KEYMAP, define key sequence KEY as DEF conditionally.
This is like `define-key', except the definition disappears
whenever COND evaluates to nil.
DESC is the short-description for the binding.
BINDINGS is a list of (key def desc cond)."
  (declare (indent defun)
           (debug (form form form form form &rest sexp)))
  (->> (cl-list* key def desc cond bindings)
       (-partition 4)
       (-mapcat (-lambda ((key def desc cond))
                  `((define-key ,keymap ,key
                      '(menu-item
                        ,(format "maybe-%s" def)
                        ,def
                        :filter
                        (lambda (item)
                          (when (with-current-buffer (or (when (buffer-live-p lsp--describe-buffer)
                                                           lsp--describe-buffer)
                                                         (current-buffer))
                                  ,cond)
                            item))))
                    (when (stringp ,key)
                      (setq lsp--binding-descriptions
                            (append lsp--binding-descriptions '(,key ,desc)))))))
       macroexp-progn))

(defvar lsp--describe-buffer nil)

(defun lsp-describe-buffer-bindings-advice (fn buffer &optional prefix menus)
  (let ((lsp--describe-buffer buffer))
    (funcall fn buffer prefix menus)))

(advice-add 'describe-buffer-bindings
            :around
            #'lsp-describe-buffer-bindings-advice)

(defun lsp--prepend-prefix (mappings)
  (->> mappings
       (-partition 2)
       (-mapcat (-lambda ((key description))
                  (list (concat lsp-keymap-prefix " " key)
                        description)))))

(defvar lsp-command-map
  (-doto (make-sparse-keymap)
    (lsp-define-conditional-key
      ;; workspaces
      "wD" lsp-disconnect "disconnect" (lsp-workspaces)
      "wd" lsp-describe-session "describe session" t
      "wq" lsp-workspace-shutdown "shutdown server" (lsp-workspaces)
      "wr" lsp-workspace-restart "restart server" (lsp-workspaces)
      "ws" lsp "start server" t

      ;; formatting
      "==" lsp-format-buffer "format buffer" (or (lsp-feature? "textDocument/rangeFormatting")
                                                 (lsp-feature? "textDocument/formatting"))
      "=r" lsp-format-region "format region" (lsp-feature? "textDocument/rangeFormatting")

      ;; folders
      "Fa" lsp-workspace-folders-add "add folder" t
      "Fb" lsp-workspace-blacklist-remove "un-blacklist folder" t
      "Fr" lsp-workspace-folders-remove "remove folder" t

      ;; toggles
      "TD" lsp-modeline-diagnostics-mode "toggle modeline diagnostics" (lsp-feature?
                                                                        "textDocument/publishDiagnostics")
      "TL" lsp-toggle-trace-io "toggle log io" t
      "TS" lsp-ui-sideline-mode "toggle sideline" (featurep 'lsp-ui-sideline)
      "TT" lsp-treemacs-sync-mode "toggle treemacs integration" (featurep 'lsp-treemacs)
      "Ta" lsp-modeline-code-actions-mode "toggle modeline code actions" (lsp-feature?
                                                                          "textDocument/codeAction")
      "Tb" lsp-headerline-breadcrumb-mode "toggle breadcrumb" (lsp-feature?
                                                               "textDocument/documentSymbol")
      "Td" lsp-ui-doc-mode "toggle documentation popup" (featurep 'lsp-ui-doc)
      "Tf" lsp-toggle-on-type-formatting "toggle on type formatting" (lsp-feature?
                                                                      "textDocument/onTypeFormatting")
      "Th" lsp-toggle-symbol-highlight "toggle highlighting" (lsp-feature? "textDocument/documentHighlight")
      "Tl" lsp-lens-mode "toggle lenses" (lsp-feature? "textDocument/codeLens")
      "Ts" lsp-toggle-signature-auto-activate "toggle signature" (lsp-feature? "textDocument/signatureHelp")

      ;; goto
      "ga" xref-find-apropos "find symbol in workspace" (lsp-feature? "workspace/symbol")
      "gd" lsp-find-declaration "find declarations" (lsp-feature? "textDocument/declaration")
      "ge" lsp-treemacs-errors-list "show errors" (fboundp 'lsp-treemacs-errors-list)
      "gg" lsp-find-definition "find definitions" (lsp-feature? "textDocument/definition")
      "gh" lsp-treemacs-call-hierarchy "call hierarchy" (and (lsp-feature? "callHierarchy/incomingCalls")
                                                             (fboundp 'lsp-treemacs-call-hierarchy))
      "gi" lsp-find-implementation "find implementations" (lsp-feature? "textDocument/implementation")
      "gr" lsp-find-references "find references" (lsp-feature? "textDocument/references")
      "gt" lsp-find-type-definition "find type definition" (lsp-feature? "textDocument/typeDefinition")

      ;; help
      "hg" lsp-ui-doc-glance "glance symbol" (and (featurep 'lsp-ui-doc)
                                                  (lsp-feature? "textDocument/hover"))
      "hh" lsp-describe-thing-at-point "describe symbol at point" (lsp-feature? "textDocument/hover")
      "hs" lsp-signature-activate "signature help" (lsp-feature? "textDocument/signatureHelp")

      ;; refactoring
      "ro" lsp-organize-imports "organize imports" (lsp-feature? "textDocument/codeAction")
      "rr" lsp-rename "rename" (lsp-feature? "textDocument/rename")

      ;; actions
      "aa" lsp-execute-code-action "code actions" (lsp-feature? "textDocument/codeAction")
      "ah" lsp-document-highlight "highlight symbol" (lsp-feature? "textDocument/documentHighlight")
      "al" lsp-avy-lens "lens" (and (bound-and-true-p lsp-lens-mode) (featurep 'avy))

      ;; peeks
      "Gg" lsp-ui-peek-find-definitions "peek definitions" (and (lsp-feature? "textDocument/definition")
                                                                (fboundp 'lsp-ui-peek-find-definitions))
      "Gi" lsp-ui-peek-find-implementation "peek implementations" (and
                                                                   (fboundp 'lsp-ui-peek-find-implementation)
                                                                   (lsp-feature? "textDocument/implementation"))
      "Gr" lsp-ui-peek-find-references "peek references" (and (fboundp 'lsp-ui-peek-find-references)
                                                              (lsp-feature? "textDocument/references"))
      "Gs" lsp-ui-peek-find-workspace-symbol "peek workspace symbol" (and (fboundp
                                                                           'lsp-ui-peek-find-workspace-symbol)
                                                                          (lsp-feature? "workspace/symbol")))))


;; which-key integration

(declare-function which-key-add-major-mode-key-based-replacements "ext:which-key")
(declare-function which-key-add-key-based-replacements "ext:which-key")

(defun lsp-enable-which-key-integration (&optional all-modes)
  "Adds descriptions for `lsp-mode-map' to `which-key-mode' for the current
active `major-mode', or for all major modes when ALL-MODES is t."
  (cl-flet ((which-key-fn (if all-modes
                              'which-key-add-key-based-replacements
                            (apply-partially 'which-key-add-major-mode-key-based-replacements major-mode))))
    (apply
     #'which-key-fn
     (lsp--prepend-prefix
      (cl-list*
       ""    "lsp"
       "w"   "workspaces"
       "F"   "folders"
       "="   "formatting"
       "T"   "toggle"
       "g"   "goto"
       "h"   "help"
       "r"   "refactor"
       "a"   "code actions"
       "G"   "peek"
       lsp--binding-descriptions)))))


;; Globbing syntax

;; We port VSCode's glob-to-regexp code
;; (https://github.com/Microsoft/vscode/blob/466da1c9013c624140f6d1473b23a870abc82d44/src/vs/base/common/glob.ts)
;; since the LSP globbing syntax seems to be the same as that of
;; VSCode.

(defconst lsp-globstar "**"
  "Globstar pattern.")

(defconst lsp-glob-split ?/
  "The character by which we split path components in a glob
pattern.")

(defconst lsp-path-regexp "[/\\\\]"
  "Forward or backslash to be used as a path separator in
computed regexps.")

(defconst lsp-non-path-regexp "[^/\\\\]"
  "A regexp matching anything other than a slash.")

(defconst lsp-globstar-regexp
  (format "\\(?:%s\\|%s+%s\\|%s%s+\\)*?"
          lsp-path-regexp
          lsp-non-path-regexp lsp-path-regexp
          lsp-path-regexp lsp-non-path-regexp)
  "Globstar in regexp form.")

(defun lsp-split-glob-pattern (pattern split-char)
  "Split PATTERN at SPLIT-CHAR while respecting braces and brackets."
  (when pattern
    (let ((segments nil)
          (in-braces nil)
          (in-brackets nil)
          (current-segment ""))
      (dolist (char (string-to-list pattern))
        (cl-block 'exit-point
          (if (eq char split-char)
              (when (and (null in-braces)
                         (null in-brackets))
                (push current-segment segments)
                (setq current-segment "")
                (cl-return-from 'exit-point))
            (pcase char
              (?{
               (setq in-braces t))
              (?}
               (setq in-braces nil))
              (?\[
               (setq in-brackets t))
              (?\]
               (setq in-brackets nil))))
          (setq current-segment (concat current-segment
                                        (char-to-string char)))))
      (unless (string-empty-p current-segment)
        (push current-segment segments))
      (nreverse segments))))

(defun lsp--glob-to-regexp (pattern)
  "Helper function to convert a PATTERN from LSP's glob syntax to
an Elisp regexp."
  (if (string-empty-p pattern)
      ""
    (let ((current-regexp "")
          (glob-segments (lsp-split-glob-pattern pattern lsp-glob-split)))
      (if (-all? (lambda (segment) (eq segment lsp-globstar))
                 glob-segments)
          ".*"
        (let ((prev-segment-was-globstar nil))
          (seq-do-indexed
           (lambda (segment index)
             (if (string-equal segment lsp-globstar)
                 (unless prev-segment-was-globstar
                   (setq current-regexp (concat current-regexp
                                                lsp-globstar-regexp))
                   (setq prev-segment-was-globstar t))
               (let ((in-braces nil)
                     (brace-val "")
                     (in-brackets nil)
                     (bracket-val ""))
                 (dolist (char (string-to-list segment))
                   (cond
                    ((and (not (char-equal char ?\}))
                          in-braces)
                     (setq brace-val (concat brace-val
                                             (char-to-string char))))
                    ((and in-brackets
                          (or (not (char-equal char ?\]))
                              (string-empty-p bracket-val)))
                     (let ((curr (cond
                                  ((char-equal char ?-)
                                   "-")
                                  ;; NOTE: ?\^ and ?^ are different characters
                                  ((and (memq char '(?^ ?!))
                                        (string-empty-p bracket-val))
                                   "^")
                                  ((char-equal char lsp-glob-split)
                                   "")
                                  (t
                                   (regexp-quote (char-to-string char))))))
                       (setq bracket-val (concat bracket-val curr))))
                    (t
                     (cl-case char
                       (?{
                        (setq in-braces t))
                       (?\[
                        (setq in-brackets t))
                       (?}
                        (let* ((choices (lsp-split-glob-pattern brace-val ?\,))
                               (brace-regexp (concat "\\(?:"
                                                     (mapconcat #'lsp--glob-to-regexp choices "\\|")
                                                     "\\)")))
                          (setq current-regexp (concat current-regexp
                                                       brace-regexp))
                          (setq in-braces nil)
                          (setq brace-val "")))
                       (?\]
                        (setq current-regexp
                              (concat current-regexp
                                      "[" bracket-val "]"))
                        (setq in-brackets nil)
                        (setq bracket-val ""))
                       (??
                        (setq current-regexp
                              (concat current-regexp
                                      lsp-non-path-regexp)))
                       (?*
                        (setq current-regexp
                              (concat current-regexp
                                      lsp-non-path-regexp "*?")))
                       (t
                        (setq current-regexp
                              (concat current-regexp
                                      (regexp-quote (char-to-string char)))))))))
                 (when (and (< index (1- (length glob-segments)))
                            (or (not (string-equal (nth (1+ index) glob-segments)
                                                   lsp-globstar))
                                (< (+ index 2)
                                   (length glob-segments))))
                   (setq current-regexp
                         (concat current-regexp
                                 lsp-path-regexp)))
                 (setq prev-segment-was-globstar nil))))
           glob-segments)
          current-regexp)))))

;; See https://github.com/emacs-lsp/lsp-mode/issues/2365
(defun lsp-glob-unbrace-at-top-level (glob-pattern)
  "If GLOB-PATTERN does not start with a brace, return a singleton list containing GLOB-PATTERN.

If GLOB-PATTERN does start with a brace, return a list of the
comma-separated globs within the top-level braces."
  (if (not (string-prefix-p "{" glob-pattern))
      (list glob-pattern)
    (lsp-split-glob-pattern (substring glob-pattern 1 -1) ?\,)))

(defun lsp-glob-convert-to-wrapped-regexp (glob-pattern)
  "Convert GLOB-PATTERN to a regexp wrapped with the beginning-
and end-of-string meta-characters."
  (concat "\\`" (lsp--glob-to-regexp (string-trim glob-pattern)) "\\'"))

(defun lsp-glob-to-regexps (glob-pattern)
  "Convert a GLOB-PATTERN to a list of Elisp regexps."
  (let* ((trimmed-pattern (string-trim glob-pattern))
         (top-level-unbraced-patterns (lsp-glob-unbrace-at-top-level trimmed-pattern)))
    (seq-map #'lsp-glob-convert-to-wrapped-regexp
             top-level-unbraced-patterns)))



(defvar lsp-mode-menu)

(defun lsp-mouse-click (event)
  (interactive "e")
  (let* ((ec (event-start event))
         (choice (x-popup-menu event lsp-mode-menu))
         (action (lookup-key lsp-mode-menu (apply 'vector choice))))

    (select-window (posn-window ec))

    (unless (and (region-active-p) (eq action 'lsp-execute-code-action))
      (goto-char (posn-point ec)))
    (run-with-idle-timer
     0.001 nil
     (lambda ()
       (cl-labels ((check (value) (not (null value))))
         (when choice
           (call-interactively action)))))))

(defvar lsp-mode-map
  (let ((map (make-sparse-keymap)))
    (define-key map (kbd "C-<down-mouse-1>") #'lsp-find-definition-mouse)
    (define-key map (kbd "C-<mouse-1>") #'ignore)
    (define-key map (kbd "<mouse-3>") #'lsp-mouse-click)
    (define-key map (kbd "C-S-SPC") #'lsp-signature-activate)
    (when lsp-keymap-prefix
      (define-key map (kbd lsp-keymap-prefix) lsp-command-map))
    map)
  "Keymap for `lsp-mode'.")

(define-minor-mode lsp-mode "Mode for LSP interaction."
  :keymap lsp-mode-map
  :lighter
  (" LSP["
   (lsp--buffer-workspaces
    (:eval (mapconcat #'lsp--workspace-print lsp--buffer-workspaces "]["))
    (:propertize "Disconnected" face warning))
   "]")
  :group 'lsp-mode)

(defvar lsp-mode-menu
  (easy-menu-create-menu
   nil
   `(["Go to definition" lsp-find-definition
      :active (lsp-feature? "textDocument/definition")]
     ["Find references" lsp-find-references
      :active (lsp-feature? "textDocument/references")]
     ["Find implementations" lsp-find-implementation
      :active (lsp-feature? "textDocument/implementation")]
     ["Find declarations" lsp-find-declaration
      :active (lsp-feature? "textDocument/declaration")]
     ["Go to type declaration" lsp-find-type-definition
      :active (lsp-feature? "textDocument/typeDefinition")]
     "--"
     ["Describe" lsp-describe-thing-at-point]
     ["Code action" lsp-execute-code-action]
     ["Format" lsp-format-buffer]
     ["Highlight references" lsp-document-highlight]
     ["Type Hierarchy" lsp-java-type-hierarchy
      :visible (lsp-can-execute-command? "java.navigate.resolveTypeHierarchy")]
     ["Type Hierarchy" lsp-treemacs-type-hierarchy
      :visible (and (not (lsp-can-execute-command? "java.navigate.resolveTypeHierarchy"))
                    (functionp 'lsp-treemacs-type-hierarchy)
                    (lsp-feature? "textDocument/typeHierarchy"))]
     ["Call Hierarchy" lsp-treemacs-call-hierarchy
      :visible (and (functionp 'lsp-treemacs-call-hierarchy)
                    (lsp-feature? "textDocument/callHierarchy"))]
     ["Rename" lsp-rename
      :active (lsp-feature? "textDocument/rename")]
     "--"
     ("Session"
      ["View logs" lsp-workspace-show-log]
      ["Describe" lsp-describe-session]
      ["Shutdown" lsp-shutdown-workspace]
      ["Restart" lsp-restart-workspace])
     ("Workspace Folders"
      ["Add" lsp-workspace-folders-add]
      ["Remove" lsp-workspace-folders-remove]
      ["Open" lsp-workspace-folders-open])
     ("Toggle features"
      ["Lenses" lsp-lens-mode]
      ["Headerline breadcrumb" lsp-headerline-breadcrumb-mode]
      ["Modeline code actions" lsp-modeline-code-actions-mode]
      ["Modeline diagnostics" lsp-modeline-diagnostics-mode])
     "---"
     ("Debug"
      :active (bound-and-true-p dap-ui-mode)
      :filter ,(lambda (_)
                 (and (boundp 'dap-ui-menu-items)
                      (nthcdr 3 dap-ui-menu-items))))))
  "Menu for lsp-mode.")

(defalias 'make-lsp-client 'make-lsp--client)

(cl-defstruct lsp--registered-capability
  (id "")
  (method " ")
  (options nil))

;; A ‘lsp--workspace’ object represents exactly one language server process.
(cl-defstruct lsp--workspace
  ;; the `ewoc' object for displaying I/O to and from the server
  (ewoc nil)

  ;; ‘server-capabilities’ is a hash table of the language server capabilities.
  ;; It is the hash table representation of a LSP ServerCapabilities structure;
  ;; cf. https://microsoft.github.io/language-server-protocol/specification#initialize.
  (server-capabilities nil)

  ;; ‘registered-server-capabilities’ is a list of hash tables that represent
  ;; dynamically-registered Registration objects.  See
  ;; https://microsoft.github.io/language-server-protocol/specification#client_registerCapability.
  (registered-server-capabilities nil)

  ;; ‘root’ is a directory name or a directory file name for the workspace
  ;; root.  ‘lsp-mode’ passes this directory to the ‘initialize’ method of the
  ;; language server; see
  ;; https://microsoft.github.io/language-server-protocol/specification#initialize.
  (root nil)

  ;; ‘client’ is the ‘lsp--client’ object associated with this workspace.
  (client nil)

  ;; ‘host-root’ contains the host root info as derived from `file-remote-p'. It
  ;; used to derive the file path in `lsp--uri-to-path' when using tramp
  ;; connection.
  (host-root nil)

  ;; ‘proc’ is a process object; it may represent a regular process, a pipe, or
  ;; a network connection.  ‘lsp-mode’ communicates with ‘proc’ using the
  ;; language server protocol.  ‘proc’ corresponds to the COMMUNICATION-PROCESS
  ;; element of the return value of the client’s ‘get-root’ field, which see.
  (proc nil)

  ;; ‘proc’ is a process object; it must represent a regular process, not a
  ;; pipe or network process.  It represents the actual server process that
  ;; corresponds to this workspace.  ‘cmd-proc’ corresponds to the
  ;; COMMAND-PROCESS element of the return value of the client’s ‘get-root’
  ;; field, which see.
  (cmd-proc nil)

  ;; ‘buffers’ is a list of buffers associated with this workspace.
  (buffers nil)

  ;; if semantic tokens is enabled, `semantic-tokens-faces' contains
  ;; one face (or nil) for each token type supported by the language server.
  (semantic-tokens-faces nil)

  ;; If semantic highlighting is enabled, `semantic-tokens-modifier-faces'
  ;; contains one face (or nil) for each modifier type supported by the language
  ;; server
  (semantic-tokens-modifier-faces nil)

  ;; Extra client capabilities provided by third-party packages using
  ;; `lsp-register-client-capabilities'. It's value is an alist of (PACKAGE-NAME
  ;; . CAPS), where PACKAGE-NAME is a symbol of the third-party package name,
  ;; and CAPS is either a plist of the client capabilities, or a function that
  ;; takes no argument and returns a plist of the client capabilities or nil.")
  (extra-client-capabilities nil)

  ;; Workspace status
  (status nil)

  ;; ‘metadata’ is a generic storage for workspace specific data. It is
  ;; accessed via `lsp-workspace-set-metadata' and `lsp-workspace-set-metadata'
  (metadata (make-hash-table :test 'equal))

  ;; contains all the file notification watches that have been created for the
  ;; current workspace in format filePath->file notification handle.
  (watches (make-hash-table :test 'equal))

  ;; list of workspace folders
  (workspace-folders nil)

  ;; ‘last-id’ the last request id for the current workspace.
  (last-id 0)

  ;; ‘status-string’ allows extensions to specify custom status string based on
  ;; the Language Server specific messages.
  (status-string nil)

  ;; ‘shutdown-action’ flag used to mark that workspace should not be restarted (e.g. it
  ;; was stopped).
  shutdown-action

  ;; ‘diagnostics’ a hashmap with workspace diagnostics.
  (diagnostics (make-hash-table :test 'equal))

  ;; contains all the workDone progress tokens that have been created
  ;; for the current workspace.
  (work-done-tokens (make-hash-table :test 'equal)))


(cl-defstruct lsp-session
  ;; contains the folders that are part of the current session
  folders
  ;; contains the folders that must not be imported in the current workspace.
  folders-blacklist
  ;; contains the list of folders that must be imported in a project in case of
  ;; multi root LSP server.
  (server-id->folders (make-hash-table :test 'equal))
  ;; folder to list of the servers that are associated with the folder.
  (folder->servers (make-hash-table :test 'equal))
  ;; ‘metadata’ is a generic storage for workspace specific data. It is
  ;; accessed via `lsp-workspace-set-metadata' and `lsp-workspace-set-metadata'
  (metadata (make-hash-table :test 'equal)))

(defun lsp-workspace-status (status-string &optional workspace)
  "Set current workspace status to STATUS-STRING.
If WORKSPACE is not specified defaults to lsp--cur-workspace."
  (let ((status-string (when status-string (replace-regexp-in-string "%" "%%" status-string))))
    (setf (lsp--workspace-status-string (or workspace lsp--cur-workspace)) status-string)))

(defun lsp-session-set-metadata (key value &optional _workspace)
  "Associate KEY with VALUE in the WORKSPACE metadata.
If WORKSPACE is not provided current workspace will be used."
  (puthash key value (lsp-session-metadata (lsp-session))))

(defalias 'lsp-workspace-set-metadata 'lsp-session-set-metadata)

(defun lsp-session-get-metadata (key &optional _workspace)
  "Lookup KEY in WORKSPACE metadata.
If WORKSPACE is not provided current workspace will be used."
  (gethash key (lsp-session-metadata (lsp-session))))

(defalias 'lsp-workspace-get-metadata 'lsp-session-get-metadata)

(defun lsp-workspace-set-work-done-token (token value workspace)
  "Associate TOKEN with VALUE in the WORKSPACE work-done-tokens."
  (puthash token value (lsp--workspace-work-done-tokens workspace)))

(defun lsp-workspace-get-work-done-token (token workspace)
  "Lookup TOKEN in the WORKSPACE work-done-tokens."
  (gethash token (lsp--workspace-work-done-tokens workspace)))

(defun lsp-workspace-rem-work-done-token (token workspace)
  "Remove TOKEN from the WORKSPACE work-done-tokens."
  (remhash token (lsp--workspace-work-done-tokens workspace)))


(defun lsp--make-notification (method &optional params)
  "Create notification body for method METHOD and parameters PARAMS."
  (list :jsonrpc "2.0" :method method :params params))

(defalias 'lsp--make-request 'lsp--make-notification)
(defalias 'lsp-make-request 'lsp--make-notification)

(defun lsp--make-response (id result)
  "Create response for REQUEST with RESULT."
  `(:jsonrpc "2.0" :id ,id :result ,result))

(defun lsp-make-notification (method &optional params)
  "Create notification body for method METHOD and parameters PARAMS."
  (lsp--make-notification method params))

(defmacro lsp--json-serialize (params)
  (if (progn
        (require 'json)
        (fboundp 'json-serialize))
      `(json-serialize ,params
                       :null-object nil
                       :false-object :json-false)
    `(let ((json-false :json-false))
       (json-encode ,params))))

(defun lsp--make-message (params)
  "Create a LSP message from PARAMS, after encoding it to a JSON string."
  (let ((body (lsp--json-serialize params)))
    (concat "Content-Length: "
            (number-to-string (1+ (string-bytes body)))
            "\r\n\r\n"
            body
            "\n")))

(cl-defstruct lsp--log-entry timestamp process-time type method id body)

(defun lsp--make-log-entry (method id body type &optional process-time)
  "Create an outgoing log object from BODY with method METHOD and id ID.
If ID is non-nil, then the body is assumed to be a notification.
TYPE can either be 'incoming or 'outgoing"
  (cl-assert (memq type '(incoming-req outgoing-req incoming-notif
                                       outgoing-notif incoming-resp
                                       outgoing-resp)))
  (make-lsp--log-entry
   :timestamp (format-time-string "%I:%M:%S %p")
   :process-time process-time
   :method method
   :id id
   :type type
   :body body))

(defun lsp--log-entry-pp (entry)
  (cl-assert (lsp--log-entry-p entry))
  (pcase-let (((cl-struct lsp--log-entry timestamp method id type process-time
                          body)
               entry)
              (json-false :json-false)
              (json-encoding-pretty-print t)
              (str nil))
    (setq str
          (concat (format "[Trace - %s] " timestamp)
                  (pcase type
                    ('incoming-req (format "Received request '%s - (%s)." method id))
                    ('outgoing-req (format "Sending request '%s - (%s)'." method id))

                    ('incoming-notif (format "Received notification '%s'." method))
                    ('outgoing-notif (format "Sending notification '%s'." method))

                    ('incoming-resp (format "Received response '%s - (%s)' in %dms."
                                            method id process-time))
                    ('outgoing-resp
                     (format
                      "Sending response '%s - (%s)'. Processing request took %dms"
                      method id process-time)))
                  "\n"
                  (if (memq type '(incoming-resp ougoing-resp))
                      "Result: "
                    "Params: ")
                  (json-encode body)
                  "\n\n\n"))
    (setq str (propertize str 'mouse-face 'highlight 'read-only t))
    (insert str)))

(defvar-local lsp--log-io-ewoc nil)

(defun lsp--get-create-io-ewoc (workspace)
  (if (and (lsp--workspace-ewoc workspace)
           (buffer-live-p (ewoc-buffer (lsp--workspace-ewoc workspace))))
      (lsp--workspace-ewoc workspace)
    (with-current-buffer (lsp--get-log-buffer-create workspace)
      (unless (eq 'lsp-log-io-mode major-mode) (lsp-log-io-mode))
      (setq-local window-point-insertion-type t)
      (setq lsp--log-io-ewoc (ewoc-create #'lsp--log-entry-pp nil nil t))
      (setf (lsp--workspace-ewoc workspace) lsp--log-io-ewoc))
    (lsp--workspace-ewoc workspace)))

(defun lsp--ewoc-count (ewoc)
  (let* ((count 0)
         (count-fn (lambda (_) (setq count (1+ count)))))
    (ewoc-map count-fn ewoc)
    count))

(defun lsp--log-entry-new (entry workspace)
  (let* ((ewoc (lsp--get-create-io-ewoc workspace))
         (count (and (not (eq lsp-io-messages-max t)) (lsp--ewoc-count ewoc)))
         (node (if (or (eq lsp-io-messages-max t)
                       (>= lsp-io-messages-max count))
                   nil
                 (ewoc-nth ewoc (1- lsp-io-messages-max))))
         (prev nil)
         (inhibit-read-only t))
    (while node
      (setq prev (ewoc-prev ewoc node))
      (ewoc-delete ewoc node)
      (setq node prev))
    (ewoc-enter-last ewoc entry)))

(defun lsp--send-notification (body)
  "Send BODY as a notification to the language server."
  (lsp-foreach-workspace
   (when (lsp--log-io-p (plist-get body :method))
     (lsp--log-entry-new (lsp--make-log-entry
                          (plist-get body :method)
                          nil (plist-get body :params) 'outgoing-notif)
                         lsp--cur-workspace))
   (lsp--send-no-wait (lsp--make-message body)
                      (lsp--workspace-proc lsp--cur-workspace))))

(defalias 'lsp-send-notification 'lsp--send-notification)

(defun lsp-notify (method params)
  "Send notification METHOD with PARAMS."
  (lsp--send-notification (lsp--make-notification method params)))

(defun lsp--cur-workspace-check ()
  "Check whether buffer lsp workspace(s) are set."
  (cl-assert (lsp-workspaces) nil
             "No language server(s) is associated with this buffer."))

(defun lsp--send-request (body &optional no-wait no-merge)
  "Send BODY as a request to the language server, get the response.
If NO-WAIT is non-nil, don't synchronously wait for a response.
If NO-MERGE is non-nil, don't merge the results but return an
alist mapping workspace->result."
  (lsp-request (plist-get body :method)
               (plist-get body :params)
               :no-wait no-wait
               :no-merge no-merge))

(defalias 'lsp-send-request 'lsp--send-request
  "Send BODY as a request to the language server and return the response
synchronously.
\n(fn BODY)")

(cl-defun lsp-request (method params &key no-wait no-merge)
  "Send request METHOD with PARAMS.
If NO-MERGE is non-nil, don't merge the results but return alist
workspace->result.
If NO-WAIT is non-nil send the request as notification."
  (if no-wait
      (lsp-notify method params)
    (let* ((send-time (time-to-seconds (current-time)))
           ;; max time by which we must get a response
           (expected-time
            (and
             lsp-response-timeout
             (+ send-time lsp-response-timeout)))
           resp-result resp-error done?)
      (unwind-protect
          (progn
            (lsp-request-async method params
                               (lambda (res) (setf resp-result (or res :finished)) (throw 'lsp-done '_))
                               :error-handler (lambda (err) (setf resp-error err) (throw 'lsp-done '_))
                               :no-merge no-merge
                               :mode 'detached
                               :cancel-token :sync-request)
            (while (not (or resp-error resp-result))
              (catch 'lsp-done
                (accept-process-output
                 nil
                 (if expected-time (- expected-time send-time) 1)))
              (setq send-time (time-to-seconds (current-time)))
              (when (and expected-time (< expected-time send-time))
                (error "Timeout while waiting for response.  Method: %s" method)))
            (setq done? t)
            (cond
             ((eq resp-result :finished) nil)
             (resp-result resp-result)
             ((lsp-json-error? resp-error) (error (lsp:json-error-message resp-error)))
             ((lsp-json-error? (cl-first resp-error))
              (error (lsp:json-error-message (cl-first resp-error))))))
        (unless done?
          (lsp-cancel-request-by-token :sync-request))))))

(cl-defun lsp-request-while-no-input (method params)
  "Send request METHOD with PARAMS and waits until there is no input.
Return same value as `lsp--while-no-input' and respecting `non-essential'."
  (if non-essential
    (let* ((send-time (time-to-seconds (current-time)))
           ;; max time by which we must get a response
           (expected-time
            (and
             lsp-response-timeout
             (+ send-time lsp-response-timeout)))
           resp-result resp-error done?)
        (unwind-protect
            (progn
              (lsp-request-async method params
                                 (lambda (res) (setf resp-result (or res :finished)) (throw 'lsp-done '_))
                                 :error-handler (lambda (err) (setf resp-error err) (throw 'lsp-done '_))
                                 :mode 'detached
                                 :cancel-token :sync-request)
              (while (not (or resp-error resp-result (input-pending-p)))
                (catch 'lsp-done
                  (sit-for
                   (if expected-time (- expected-time send-time) 1)))
                (setq send-time (time-to-seconds (current-time)))
                (when (and expected-time (< expected-time send-time))
                  (error "Timeout while waiting for response.  Method: %s" method)))
              (setq done? (or resp-error resp-result))
              (cond
               ((eq resp-result :finished) nil)
               (resp-result resp-result)
               ((lsp-json-error? resp-error) (error (lsp:json-error-message resp-error)))
               ((lsp-json-error? (cl-first resp-error))
                (error (lsp:json-error-message (cl-first resp-error))))))
          (unless done?
            (lsp-cancel-request-by-token :sync-request))
          (when (and (input-pending-p) lsp--throw-on-input)
            (throw 'input :interrupted))))
    (lsp-request method params)))

(defvar lsp--cancelable-requests (ht))

(cl-defun lsp-request-async (method params callback
                                    &key mode error-handler cancel-handler no-merge cancel-token)
  "Send METHOD with PARAMS as a request to the language server.
Call CALLBACK with the response received from the server
asynchronously.
MODE determines when the callback will be called depending on the
condition of the original buffer.  It could be:
- `detached' which means that the callback will be executed no
matter what has happened to the buffer.
- `alive' - the callback will be executed only if the buffer from
which the call was executed is still alive.
- `current' the callback will be executed only if the original buffer
is still selected.
- `tick' - the callback will be executed only if the buffer was not modified.
- `unchanged' - the callback will be executed only if the buffer hasn't
changed and if the buffer is not modified.

ERROR-HANDLER will be called in case the request has failed.
CANCEL-HANDLER will be called in case the request is being canceled.
If NO-MERGE is non-nil, don't merge the results but return alist
workspace->result.
CANCEL-TOKEN is the token that can be used to cancel request."
  (lsp--send-request-async `(:jsonrpc "2.0" :method ,method :params ,params)
                           callback mode error-handler cancel-handler no-merge cancel-token))

(defun lsp--create-request-cancel (id workspaces hook buf method cancel-callback)
  (lambda (&rest _)
    (unless (and (equal 'post-command-hook hook)
                 (equal (current-buffer) buf))
      (lsp--request-cleanup-hooks id)
      (with-lsp-workspaces workspaces
        (lsp--cancel-request id)
        (when cancel-callback (funcall cancel-callback)))
      (lsp-log "Cancelling %s(%s) in hook %s" method id hook))))

(defun lsp--create-async-callback
    (callback method no-merge workspaces)
  "Create async handler expecting COUNT results, merge them and call CALLBACK.
MODE determines when the callback will be called depending on the
condition of the original buffer. METHOD is the invoked method.
If NO-MERGE is non-nil, don't merge the results but return alist workspace->result.
ID is the request id. "
  (let (results errors)
    (lambda (result)
      (push (cons lsp--cur-workspace result)
            (if (eq result :error) errors results))
      (when (and (not (eq (length errors) (length workspaces)))
                 (eq (+ (length errors) (length results)) (length workspaces)))
        (funcall callback
                 (if no-merge
                     results
                   (lsp--merge-results (-map #'cl-rest results) method)))))))

(defcustom lsp-default-create-error-handler-fn nil
  "Default error handler customization.
Handler should give METHOD as argument and return function of one argument
ERROR."
  :type 'function
  :group 'lsp-mode
  :package-version '(lsp-mode . "8.0.1"))

(defun lsp--create-default-error-handler (method)
  "Default error handler.
METHOD is the executed method."
  (if lsp-default-create-error-handler-fn
      (funcall lsp-default-create-error-handler-fn method)
    (lambda (error)
      (lsp--warn "%s" (or (lsp--error-string error)
                          (format "%s Request has failed" method))))))

(defvar lsp--request-cleanup-hooks (ht))

(defun lsp--request-cleanup-hooks (request-id)
  (when-let ((cleanup-function (gethash request-id lsp--request-cleanup-hooks)))
    (funcall cleanup-function)
    (remhash request-id lsp--request-cleanup-hooks)))

(defun lsp-cancel-request-by-token (cancel-token)
  "Cancel request using CANCEL-TOKEN."
  (-when-let ((request-id . workspaces) (gethash cancel-token lsp--cancelable-requests))
    (with-lsp-workspaces workspaces
      (lsp--cancel-request request-id))
    (remhash cancel-token lsp--cancelable-requests)
    (lsp--request-cleanup-hooks request-id)))

(defun lsp--send-request-async (body callback
                                     &optional mode error-callback cancel-callback
                                     no-merge cancel-token)
  "Send BODY as a request to the language server.
Call CALLBACK with the response received from the server
asynchronously.
MODE determines when the callback will be called depending on the
condition of the original buffer.  It could be:
- `detached' which means that the callback will be executed no
matter what has happened to the buffer.
- `alive' - the callback will be executed only if the buffer from
which the call was executed is still alive.
- `current' the callback will be executed only if the original buffer
is still selected.
- `tick' - the callback will be executed only if the buffer was not modified.
- `unchanged' - the callback will be executed only if the buffer hasn't
changed and if the buffer is not modified.

ERROR-CALLBACK will be called in case the request has failed.
CANCEL-CALLBACK will be called in case the request is being canceled.
If NO-MERGE is non-nil, don't merge the results but return alist
workspace->result.
CANCEL-TOKEN is the token that can be used to cancel request."
  (when cancel-token
    (lsp-cancel-request-by-token cancel-token))

  (if-let ((target-workspaces (lsp--find-workspaces-for body)))
      (let* ((start-time (current-time))
             (method (plist-get body :method))
             (id (cl-incf lsp-last-id))
             ;; calculate what are the (hook . local) pairs which will cancel
             ;; the request
             (hooks (pcase mode
                      ('alive     '((kill-buffer-hook . t)))
                      ('tick      '((kill-buffer-hook . t) (after-change-functions . t)))
                      ('unchanged '((after-change-functions . t) (post-command-hook . nil)))
                      ('current   '((post-command-hook . nil)))))
             (buf (current-buffer))
             ;; note: lambdas in emacs can be compared but we should make sure
             ;; that all of the captured arguments are the same - in our case
             ;; `lsp--create-request-cancel' will return the same lambda when
             ;; called with the same params.
             (cleanup-hooks
              (lambda () (mapc
                          (-lambda ((hook . local))
                            (if local
                                (when (buffer-live-p buf)
                                  (with-current-buffer buf
                                    (remove-hook hook
                                                 (lsp--create-request-cancel
                                                  id target-workspaces hook buf method cancel-callback)
                                                 t)))
                              (remove-hook hook (lsp--create-request-cancel
                                                 id target-workspaces hook buf method cancel-callback))))
                          hooks)
                (remhash cancel-token lsp--cancelable-requests)))
             (callback (pcase mode
                         ((or 'alive 'tick) (lambda (&rest args)
                                              (with-current-buffer buf
                                                (apply callback args))))
                         (_ callback)))
             (callback (lsp--create-async-callback callback
                                                   method
                                                   no-merge
                                                   target-workspaces))
             (callback (lambda (result)
                         (lsp--request-cleanup-hooks id)
                         (funcall callback result)))
             (error-callback (lsp--create-async-callback
                              (or error-callback
                                  (lsp--create-default-error-handler method))
                              method
                              nil
                              target-workspaces))
             (error-callback (lambda (error)
                               (funcall callback :error)
                               (lsp--request-cleanup-hooks id)
                               (funcall error-callback error)))
             (cancel-callback (when cancel-callback
                                (pcase mode
                                  ((or 'alive 'tick 'unchanged)
                                   (lambda ()
                                     (with-current-buffer buf
                                       (funcall cancel-callback))))
                                  (_ cancel-callback))))
             (body (plist-put body :id id)))

        ;; cancel request in any of the hooks
        (mapc (-lambda ((hook . local))
                (add-hook hook
                          (lsp--create-request-cancel
                           id target-workspaces hook buf method cancel-callback)
                          nil local))
              hooks)
        (puthash id cleanup-hooks lsp--request-cleanup-hooks)

        (setq lsp--last-active-workspaces target-workspaces)

        (when cancel-token
          (puthash cancel-token (cons id target-workspaces) lsp--cancelable-requests))

        (seq-doseq (workspace target-workspaces)
          (when (lsp--log-io-p method)
            (lsp--log-entry-new (lsp--make-log-entry method id
                                                     (plist-get body :params)
                                                     'outgoing-req)
                                workspace))
          (let ((message (lsp--make-message body)))
            (puthash id
                     (list callback error-callback method start-time (current-time))
                     (-> workspace
                         (lsp--workspace-client)
                         (lsp--client-response-handlers)))
            (lsp--send-no-wait message (lsp--workspace-proc workspace))))
        body)
    (error "The connected server(s) does not support method %s.
To find out what capabilities support your server use `M-x lsp-describe-session'
and expand the capabilities section"
           (plist-get body :method))))

;; deprecated, use lsp-request-async.
(defalias 'lsp-send-request-async 'lsp--send-request-async)
(make-obsolete 'lsp-send-request-async 'lsp-request-async "lsp-mode 7.0.1")

;; Clean up the entire state of lsp mode when Emacs is killed, to get rid of any
;; pending language servers.
(add-hook 'kill-emacs-hook #'lsp--global-teardown)

(defun lsp--global-teardown ()
  "Unload working workspaces."
  (lsp-foreach-workspace (lsp--shutdown-workspace)))

(defun lsp--shutdown-workspace (&optional restart)
  "Shut down the language server process for ‘lsp--cur-workspace’."
  (with-demoted-errors "LSP error: %S"
    (let ((lsp-response-timeout 0.5))
      (condition-case _err
          (lsp-request "shutdown" lsp--empty-ht)
        (error (lsp--error "Timeout while sending shutdown request"))))
    (lsp-notify "exit" nil))
  (setf (lsp--workspace-shutdown-action lsp--cur-workspace) (or (and restart 'restart) 'shutdown))
  (lsp--uninitialize-workspace))

(defun lsp--uninitialize-workspace ()
  "Cleanup buffer state.
When a workspace is shut down, by request or from just
disappearing, unset all the variables related to it."
  (let ((proc (lsp--workspace-cmd-proc lsp--cur-workspace))
        (buffers (lsp--workspace-buffers lsp--cur-workspace)))
    (when (process-live-p proc)
      (kill-process proc))
    (mapc (lambda (buf)
            (when (lsp-buffer-live-p buf)
              (lsp-with-current-buffer buf
                (lsp-managed-mode -1))))
          buffers)
    (lsp-diagnostics--workspace-cleanup lsp--cur-workspace)))

(defun lsp--client-capabilities (&optional custom-capabilities)
  "Return the client capabilities appending CUSTOM-CAPABILITIES."
  (append
   `((workspace . ((workspaceEdit . ((documentChanges . t)
                                     (resourceOperations . ["create" "rename" "delete"])))
                   (applyEdit . t)
                   (symbol . ((symbolKind . ((valueSet . ,(apply 'vector (number-sequence 1 26)))))))
                   (executeCommand . ((dynamicRegistration . :json-false)))
                   ,@(when lsp-enable-file-watchers '((didChangeWatchedFiles . ((dynamicRegistration . t)))))
                   (workspaceFolders . t)
                   (configuration . t)
                   ,@(when lsp-semantic-tokens-enable
                       `((semanticTokens . ((refreshSupport . ,(or (and (boundp 'lsp-semantic-tokens-honor-refresh-requests)
                                                                        lsp-semantic-tokens-honor-refresh-requests)
                                                                   :json-false))))))
                   ,@(when lsp-lens-enable '((codeLens . ((refreshSupport . t)))))
                   (fileOperations . ((didCreate . :json-false)
                                      (willCreate . :json-false)
                                      (didRename . t)
                                      (willRename . t)
                                      (didDelete . :json-false)
                                      (willDelete . :json-false)))))
     (textDocument . ((declaration . ((linkSupport . t)))
                      (definition . ((linkSupport . t)))
                      (implementation . ((linkSupport . t)))
                      (typeDefinition . ((linkSupport . t)))
                      (synchronization . ((willSave . t) (didSave . t) (willSaveWaitUntil . t)))
                      (documentSymbol . ((symbolKind . ((valueSet . ,(apply 'vector (number-sequence 1 26)))))
                                         (hierarchicalDocumentSymbolSupport . t)))
                      (formatting . ((dynamicRegistration . t)))
                      (rangeFormatting . ((dynamicRegistration . t)))
                      ,@(when (and lsp-semantic-tokens-enable
                                   (boundp 'lsp-semantic-tokens-capabilities))
                          lsp-semantic-tokens-capabilities)
                      (rename . ((dynamicRegistration . t) (prepareSupport . t)))
                      (codeAction . ((dynamicRegistration . t)
                                     (isPreferredSupport . t)
                                     (codeActionLiteralSupport . ((codeActionKind . ((valueSet . [""
                                                                                                  "quickfix"
                                                                                                  "refactor"
                                                                                                  "refactor.extract"
                                                                                                  "refactor.inline"
                                                                                                  "refactor.rewrite"
                                                                                                  "source"
                                                                                                  "source.organizeImports"])))))
                                     (resolveSupport . ((properties . ["edit" "command"])))
                                     (dataSupport . t)))
                      (completion . ((completionItem . ((snippetSupport . ,(cond
                                                                            ((and lsp-enable-snippet (not (featurep 'yasnippet)) t)
                                                                             (lsp--warn (concat
                                                                                         "Yasnippet is not installed, but `lsp-enable-snippet' is set to `t'. "
                                                                                         "You must either install yasnippet, or disable snippet support."))
                                                                             :json-false)
                                                                            (lsp-enable-snippet t)
                                                                            (t :json-false)))
                                                        (documentationFormat . ["markdown" "plaintext"])
                                                        ;; Remove this after jdtls support resolveSupport
                                                        (resolveAdditionalTextEditsSupport . t)
                                                        (insertReplaceSupport . t)
                                                        (deprecatedSupport . t)
                                                        (resolveSupport
                                                         . ((properties . ["documentation"
                                                                           "detail"
                                                                           "additionalTextEdits"
                                                                           "command"])))
                                                        (insertTextModeSupport . ((valueSet . [1 2])))))
                                     (contextSupport . t)))
                      (signatureHelp . ((signatureInformation . ((parameterInformation . ((labelOffsetSupport . t)))))))
                      (documentLink . ((dynamicRegistration . t)
                                       (tooltipSupport . t)))
                      (hover . ((contentFormat . ["markdown" "plaintext"])))
                      (foldingRange . ,(when lsp-enable-folding
                                         `((dynamicRegistration . t)
                                           ,@(when lsp-folding-range-limit
                                               `((rangeLimit . ,lsp-folding-range-limit)))
                                           ,@(when lsp-folding-line-folding-only
                                               `((lineFoldingOnly . t))))))
                      (callHierarchy . ((dynamicRegistration . :json-false)))
                      (publishDiagnostics . ((relatedInformation . t)
                                             (tagSupport . ((valueSet . [1 2])))
                                             (versionSupport . t)))
                      (linkedEditingRange . ((dynamicRegistration . t)))))
     (window . ((workDoneProgress . t)
                (showMessage . nil)
                (showDocument . ((support . t))))))
   custom-capabilities))

(defun lsp-find-roots-for-workspace (workspace session)
  "Get all roots for the WORKSPACE."
  (-filter #'identity (ht-map (lambda (folder workspaces)
                                (when (-contains? workspaces workspace)
                                  folder))
                              (lsp-session-folder->servers session))))

(defun lsp-session-watches (&optional session)
  "Get watches created for SESSION."
  (or (gethash "__watches" (lsp-session-metadata (or session (lsp-session))))
      (-let [res (make-hash-table :test 'equal)]
        (puthash "__watches" res (lsp-session-metadata (or session (lsp-session))))
        res)))

(defun lsp--file-process-event (session root-folder event)
  "Process file event."
  (let* ((changed-file (cl-third event))
         (rel-changed-file (f-relative changed-file root-folder))
         (event-numeric-kind (alist-get (cl-second event) lsp--file-change-type))
         (bit-position (1- event-numeric-kind))
         (watch-bit (ash 1 bit-position)))
    (->>
     session
     lsp-session-folder->servers
     (gethash root-folder)
     (seq-do (lambda (workspace)
               (when (->>
                      workspace
                      lsp--workspace-registered-server-capabilities
                      (-any?
                       (lambda (capability)
                         (and
                          (equal (lsp--registered-capability-method capability)
                                 "workspace/didChangeWatchedFiles")
                          (->>
                           capability
                           lsp--registered-capability-options
                           (lsp:did-change-watched-files-registration-options-watchers)
                           (seq-find
                            (-lambda ((fs-watcher &as &FileSystemWatcher :glob-pattern :kind? :_cachedRegexp cached-regexp))
                              (when (or (null kind?)
                                        (> (logand kind? watch-bit) 0))
                                (-let [regexes (or cached-regexp
                                                   (let ((regexp (lsp-glob-to-regexps glob-pattern)))
                                                     (lsp-put fs-watcher :_cachedRegexp regexp)
                                                     regexp))]
                                  (-any? (lambda (re)
                                           (or (string-match re changed-file)
                                               (string-match re rel-changed-file)))
                                         regexes))))))))))
                 (with-lsp-workspace workspace
                   (lsp-notify
                    "workspace/didChangeWatchedFiles"
                    `((changes . [((type . ,event-numeric-kind)
                                   (uri . ,(lsp--path-to-uri changed-file)))]))))))))))

(lsp-defun lsp--server-register-capability ((&Registration :method :id :register-options?))
  "Register capability REG."
  (when (and lsp-enable-file-watchers
             (equal method "workspace/didChangeWatchedFiles"))
    (-let* ((created-watches (lsp-session-watches (lsp-session)))
            (root-folders (cl-set-difference
                           (lsp-find-roots-for-workspace lsp--cur-workspace (lsp-session))
                           (ht-keys created-watches))))
      ;; create watch for each root folder without such
      (dolist (folder root-folders)
        (let* ((watch (make-lsp-watch :root-directory folder))
               (ignored-things (lsp--get-ignored-regexes-for-workspace-root folder))
               (ignored-files-regex-list (car ignored-things))
               (ignored-directories-regex-list (cadr ignored-things)))
          (puthash folder watch created-watches)
          (lsp-watch-root-folder (file-truename folder)
                                 (-partial #'lsp--file-process-event (lsp-session) folder)
                                 ignored-files-regex-list
                                 ignored-directories-regex-list
                                 watch
                                 t)))))

  (push
   (make-lsp--registered-capability :id id :method method :options register-options?)
   (lsp--workspace-registered-server-capabilities lsp--cur-workspace)))

(defmacro lsp--with-workspace-temp-buffer (workspace-root &rest body)
  "With a temp-buffer under `WORKSPACE-ROOT' and evaluate `BODY', useful to access dir-local variables."
  (declare (indent 1) (debug t))
  `(with-temp-buffer
     ;; Set the buffer's name to something under the root so that we can hack the local variables
     ;; This file doesn't need to exist and will not be created due to this.
     (setq-local buffer-file-name (expand-file-name "lsp-mode-temp" (expand-file-name ,workspace-root)))
     (hack-local-variables)
     (prog1 ,@body
       (setq-local buffer-file-name nil))))

(defun lsp--get-ignored-regexes-for-workspace-root (workspace-root)
  "Return a list of the form (lsp-file-watch-ignored-files lsp-file-watch-ignored-directories) for the given WORKSPACE-ROOT."
  ;; The intent of this function is to provide per-root workspace-level customization of the
  ;; lsp-file-watch-ignored-directories and lsp-file-watch-ignored-files variables.
  (lsp--with-workspace-temp-buffer workspace-root
    (list lsp-file-watch-ignored-files (lsp-file-watch-ignored-directories))))


(defun lsp--cleanup-hanging-watches ()
  "Cleanup watches in case there are no more workspaces that are interested
in that particular folder."
  (let* ((session (lsp-session))
         (watches (lsp-session-watches session)))
    (dolist (watched-folder (ht-keys watches))
      (when (-none? (lambda (workspace)
                      (with-lsp-workspace workspace
                        (lsp--registered-capability "workspace/didChangeWatchedFiles")))
                    (gethash watched-folder (lsp-session-folder->servers (lsp-session))))
        (lsp-log "Cleaning up watches for folder %s. There is no workspace watching this folder..." watched-folder)
        (lsp-kill-watch (gethash watched-folder watches))
        (remhash watched-folder watches)))))

(lsp-defun lsp--server-unregister-capability ((&Unregistration :id :method))
  "Unregister capability UNREG."
  (setf (lsp--workspace-registered-server-capabilities lsp--cur-workspace)
        (seq-remove (lambda (e) (equal (lsp--registered-capability-id e) id))
                    (lsp--workspace-registered-server-capabilities lsp--cur-workspace)))
  (when (equal method "workspace/didChangeWatchedFiles")
    (lsp--cleanup-hanging-watches)))

(defun lsp--server-capabilities ()
  "Return the capabilities of the language server associated with the buffer."
  (->> (lsp-workspaces)
       (-keep #'lsp--workspace-server-capabilities)
       (apply #'lsp-merge)))

(defun lsp--send-open-close-p ()
  "Return whether open and close notifications should be sent to the server."
  (let ((sync (lsp:server-capabilities-text-document-sync? (lsp--server-capabilities))))
    (or (memq sync '(1 2))
        (lsp:text-document-sync-options-open-close? sync))))

(defun lsp--send-will-save-p ()
  "Return whether willSave notifications should be sent to the server."
  (-> (lsp--server-capabilities)
      (lsp:server-capabilities-text-document-sync?)
      (lsp:text-document-sync-options-will-save?)))

(defun lsp--send-will-save-wait-until-p ()
  "Return whether willSaveWaitUntil notifications should be sent to the server."
  (-> (lsp--server-capabilities)
      (lsp:server-capabilities-text-document-sync?)
      (lsp:text-document-sync-options-will-save-wait-until?)))

(defun lsp--send-did-save-p ()
  "Return whether didSave notifications should be sent to the server."
  (let ((sync (lsp:server-capabilities-text-document-sync? (lsp--server-capabilities))))
    (or (memq sync '(1 2))
        (lsp:text-document-sync-options-save? sync))))

(defun lsp--save-include-text-p ()
  "Return whether save notifications should include the text document's contents."
  (->> (lsp--server-capabilities)
       (lsp:server-capabilities-text-document-sync?)
       (lsp:text-document-sync-options-save?)
       (lsp:text-document-save-registration-options-include-text?)))

(defun lsp--send-will-rename-files-p (path)
  "Return whether willRenameFiles request should be sent to the server.
If any filters, checks if it applies for PATH."
  (let* ((will-rename (-> (lsp--server-capabilities)
                          (lsp:server-capabilities-workspace?)
                          (lsp:workspace-server-capabilities-file-operations?)
                          (lsp:workspace-file-operations-will-rename?)))
         (filters (seq-into (lsp:file-operation-registration-options-filters will-rename) 'list)))
    (and will-rename
         (or (seq-empty-p filters)
             (-any? (-lambda ((&FileOperationFilter :scheme? :pattern (&FileOperationPattern :glob)))
                      (-let [regexes (lsp-glob-to-regexps glob)]
                        (and (or (not scheme?)
                                 (string-prefix-p scheme? (lsp--path-to-uri path)))
                             (-any? (lambda (re)
                                      (string-match re path))
                                    regexes))))
                    filters)))))

(defun lsp--send-did-rename-files-p ()
  "Return whether didRenameFiles notification should be sent to the server."
  (-> (lsp--server-capabilities)
      (lsp:server-capabilities-workspace?)
      (lsp:workspace-server-capabilities-file-operations?)
      (lsp:workspace-file-operations-did-rename?)))

(declare-function project-roots "ext:project" (project) t)
(declare-function project-root "ext:project" (project) t)

(defun lsp--suggest-project-root ()
  "Get project root."
  (or
   (when (featurep 'projectile) (condition-case nil
                                    (projectile-project-root)
                                  (error nil)))
   (when (featurep 'project)
     (when-let ((project (project-current)))
       (if (fboundp 'project-root)
           (project-root project)
         (car (with-no-warnings
                (project-roots project))))))
   default-directory))

(defun lsp--read-from-file (file)
  "Read FILE content."
  (when (file-exists-p file)
    (cl-first (read-from-string (f-read-text file 'utf-8)))))

(defun lsp--persist (file-name to-persist)
  "Persist TO-PERSIST in FILE-NAME.

This function creates the parent directories if they don't exist
yet."
  (let ((print-length nil)
        (print-level nil))
    ;; Create all parent directories:
    (make-directory (f-parent file-name) t)
    (f-write-text (prin1-to-string to-persist) 'utf-8 file-name)))

(defun lsp-workspace-folders-add (project-root)
  "Add PROJECT-ROOT to the list of workspace folders."
  (interactive
   (list (read-directory-name "Select folder to add: "
                              (or (lsp--suggest-project-root) default-directory) nil t)))
  (cl-pushnew (lsp-f-canonical project-root)
              (lsp-session-folders (lsp-session)) :test 'equal)
  (lsp--persist-session (lsp-session))

  (run-hook-with-args 'lsp-workspace-folders-changed-functions (list project-root) nil))

(defun lsp-workspace-folders-remove (project-root)
  "Remove PROJECT-ROOT from the list of workspace folders."
  (interactive (list (completing-read "Select folder to remove: "
                                      (lsp-session-folders (lsp-session))
                                      nil t nil nil
                                      (lsp-find-session-folder (lsp-session) default-directory))))

  (setq project-root (lsp-f-canonical project-root))

  ;; send remove folder to each multiroot workspace associated with the folder
  (dolist (wks (->> (lsp-session)
                    (lsp-session-folder->servers)
                    (gethash project-root)
                    (--filter (lsp--client-multi-root (lsp--workspace-client it)))))
    (with-lsp-workspace wks
      (lsp-notify "workspace/didChangeWorkspaceFolders"
                  (lsp-make-did-change-workspace-folders-params
                   :event (lsp-make-workspace-folders-change-event
                           :removed (vector (lsp-make-workspace-folder
                                             :uri (lsp--path-to-uri project-root)
                                             :name (f-filename project-root)))
                           :added [])))))

  ;; turn off servers in the removed directory
  (let* ((session (lsp-session))
         (folder->servers (lsp-session-folder->servers session))
         (server-id->folders (lsp-session-server-id->folders session))
         (workspaces (gethash project-root folder->servers)))

    (remhash project-root folder->servers)

    ;; turn off the servers without root folders
    (dolist (workspace workspaces)
      (when (--none? (-contains? it workspace) (ht-values folder->servers))
        (lsp--info "Shutdown %s since folder %s is removed..."
                   (lsp--workspace-print workspace) project-root)
        (with-lsp-workspace workspace (lsp--shutdown-workspace))))

    (setf (lsp-session-folders session)
          (-remove-item project-root (lsp-session-folders session)))

    (ht-aeach (puthash key
                       (-remove-item project-root value)
                       server-id->folders)
              server-id->folders)
    (lsp--persist-session (lsp-session)))

  (run-hook-with-args 'lsp-workspace-folders-changed-functions nil (list project-root)))

(defun lsp-workspace-blacklist-remove (project-root)
  "Remove PROJECT-ROOT from the workspace blacklist."
  (interactive (list (completing-read "Select folder to remove:"
                                      (lsp-session-folders-blacklist (lsp-session))
                                      nil t)))
  (setf (lsp-session-folders-blacklist (lsp-session))
        (delete project-root
                (lsp-session-folders-blacklist (lsp-session))))
  (lsp--persist-session (lsp-session)))

(define-obsolete-function-alias 'lsp-workspace-folders-switch
  'lsp-workspace-folders-open "lsp-mode 6.1")

(defun lsp-workspace-folders-open (project-root)
  "Open the directory located at PROJECT-ROOT"
  (interactive (list (completing-read "Open folder: "
                                      (lsp-session-folders (lsp-session))
                                      nil t)))
  (find-file project-root))

(defun lsp--maybe-enable-signature-help (trigger-characters)
  (let ((ch last-command-event))
    (when (cl-find ch trigger-characters :key #'string-to-char)
      (lsp-signature-activate))))

(defun lsp--on-type-formatting-handler-create ()
  (when-let ((provider (lsp--capability :documentOnTypeFormattingProvider)))
    (-let [(&DocumentOnTypeFormattingOptions :more-trigger-character?
                                             :first-trigger-character) provider]
      (lambda ()
        (lsp--on-type-formatting first-trigger-character
                                 more-trigger-character?)))))

(defun lsp--update-on-type-formatting-hook (&optional cleanup?)
  (let ((on-type-formatting-handler (lsp--on-type-formatting-handler-create)))
    (cond
     ((and lsp-enable-on-type-formatting on-type-formatting-handler (not cleanup?))
      (add-hook 'post-self-insert-hook on-type-formatting-handler nil t))
     ((or cleanup?
          (not lsp-enable-on-type-formatting))
      (remove-hook 'post-self-insert-hook on-type-formatting-handler t)))))

(defun lsp--signature-help-handler-create ()
  (-when-let ((&SignatureHelpOptions? :trigger-characters?)
              (lsp--capability :signatureHelpProvider))
    (lambda ()
      (lsp--maybe-enable-signature-help trigger-characters?))))

(defun lsp--update-signature-help-hook (&optional cleanup?)
  (let ((signature-help-handler (lsp--signature-help-handler-create)))
    (cond
     ((and (or (equal lsp-signature-auto-activate t)
               (memq :on-trigger-char lsp-signature-auto-activate))
           signature-help-handler)
      (add-hook 'post-self-insert-hook signature-help-handler nil t))

     ((or cleanup?
          (not (or (equal lsp-signature-auto-activate t)
                   (memq :on-trigger-char lsp-signature-auto-activate))))
      (remove-hook 'post-self-insert-hook signature-help-handler t)))))

(defun lsp--after-set-visited-file-name ()
  (lsp-disconnect)
  (lsp))

(define-minor-mode lsp-managed-mode
  "Mode for source buffers managed by lsp-mode."
  :lighter nil
  (cond
   (lsp-managed-mode
    (when (lsp-feature? "textDocument/hover")
      (add-function :before-until (local 'eldoc-documentation-function) #'lsp-eldoc-function)
      (eldoc-mode 1))

    (add-hook 'after-change-functions #'lsp-on-change nil t)
    (add-hook 'after-revert-hook #'lsp-on-revert nil t)
    (add-hook 'after-save-hook #'lsp-on-save nil t)
    (add-hook 'auto-save-hook #'lsp--on-auto-save nil t)
    (add-hook 'before-change-functions #'lsp-before-change nil t)
    (add-hook 'before-save-hook #'lsp--before-save nil t)
    (add-hook 'kill-buffer-hook #'lsp--text-document-did-close nil t)
    (add-hook 'post-command-hook #'lsp--post-command nil t)

    (lsp--update-on-type-formatting-hook)
    (lsp--update-signature-help-hook)

    (when lsp-enable-xref
      (add-hook 'xref-backend-functions #'lsp--xref-backend nil t))

    (lsp-configure-buffer)

    ;; make sure we turn off lsp-mode in case major mode changes, because major
    ;; mode change will wipe the buffer locals.
    (add-hook 'change-major-mode-hook #'lsp-disconnect nil t)
    (add-hook 'after-set-visited-file-name-hook #'lsp--after-set-visited-file-name nil t)

    (let ((buffer (lsp-current-buffer)))
      (run-with-idle-timer
       0.0 nil
       (lambda ()
         (when (lsp-buffer-live-p buffer)
           (lsp-with-current-buffer buffer
             (lsp--on-change-debounce buffer)
             (lsp--on-idle buffer)))))))
   (t
    (lsp-unconfig-buffer)
    (remove-function (local 'eldoc-documentation-function) #'lsp-eldoc-function)

    (remove-hook 'post-command-hook #'lsp--post-command t)
    (remove-hook 'after-change-functions #'lsp-on-change t)
    (remove-hook 'after-revert-hook #'lsp-on-revert t)
    (remove-hook 'after-save-hook #'lsp-on-save t)
    (remove-hook 'auto-save-hook #'lsp--on-auto-save t)
    (remove-hook 'before-change-functions #'lsp-before-change t)
    (remove-hook 'before-save-hook #'lsp--before-save t)
    (remove-hook 'kill-buffer-hook #'lsp--text-document-did-close t)

    (lsp--update-on-type-formatting-hook :cleanup)
    (lsp--update-signature-help-hook :cleanup)

    (when lsp--on-idle-timer
      (cancel-timer lsp--on-idle-timer)
      (setq lsp--on-idle-timer nil))

    (remove-hook 'lsp-on-idle-hook #'lsp--document-links t)
    (remove-hook 'lsp-on-idle-hook #'lsp--document-highlight t)

    (lsp--remove-overlays 'lsp-highlight)
    (lsp--remove-overlays 'lsp-links)

    (remove-hook 'xref-backend-functions #'lsp--xref-backend t)
    (remove-hook 'change-major-mode-hook #'lsp-disconnect t)
    (remove-hook 'after-set-visited-file-name-hook #'lsp--after-set-visited-file-name t)
    (setq-local lsp-buffer-uri nil))))

(defun lsp-configure-buffer ()
  "Configure LSP features for current buffer."
  ;; make sure the core is running in the context of all available workspaces
  ;; to avoid misconfiguration in case we are running in `with-lsp-workspace' context
  (let ((lsp--buffer-workspaces (cond
                                 (lsp--buffer-workspaces)
                                 (lsp--cur-workspace (list lsp--cur-workspace))))
        lsp--cur-workspace)
    (when lsp-auto-configure
      (when (and lsp-enable-text-document-color
                 (lsp-feature? "textDocument/documentColor"))
        (add-hook 'lsp-on-change-hook #'lsp--document-color nil t))

      (when (and lsp-enable-imenu
                 (lsp-feature? "textDocument/documentSymbol"))
        (lsp-enable-imenu))

      (when (and lsp-enable-indentation
                 (lsp-feature? "textDocument/rangeFormatting"))
        (setq-local indent-region-function #'lsp-format-region))

      (when (and lsp-enable-symbol-highlighting
                 (lsp-feature? "textDocument/documentHighlight"))
        (add-hook 'lsp-on-idle-hook #'lsp--document-highlight nil t))

      (when (and lsp-enable-links
                 (lsp-feature? "textDocument/documentLink"))
        (add-hook 'lsp-on-idle-hook #'lsp--document-links nil t))

      (when (and lsp-enable-dap-auto-configure
                 (functionp 'dap-mode))
        (dap-auto-configure-mode 1)))
    (run-hooks 'lsp-configure-hook)))

(defun lsp-unconfig-buffer ()
  "Unconfigure LSP features for buffer."
  (run-hooks 'lsp-unconfigure-hook)

  (lsp--remove-overlays 'lsp-color)
  (when (eq indent-region-function #'lsp-format-region)
    (setq-local indent-region-function nil))
  (remove-hook 'lsp-on-change-hook #'lsp--document-color t)
  (remove-hook 'lsp-on-idle-hook #'lsp--document-highlight t)
  (remove-hook 'lsp-on-idle-hook #'lsp--document-links t))

(defun lsp--buffer-content ()
  (lsp-save-restriction-and-excursion
    (or (lsp-virtual-buffer-call :buffer-string)
        (buffer-substring-no-properties (point-min)
                                        (point-max)))))

(defun lsp--text-document-did-open ()
  "'document/didOpen' event."
  (run-hooks 'lsp-before-open-hook)
  (when (and lsp-auto-touch-files
             (not (f-exists? (lsp--uri-to-path (lsp--buffer-uri)))))
    (lsp--info "Saving file '%s' because it is not present on the disk." (lsp--buffer-uri))
    (save-buffer))

  (setq lsp--cur-version (or lsp--cur-version 0))
  (cl-pushnew (lsp-current-buffer) (lsp--workspace-buffers lsp--cur-workspace))
  (lsp-notify
   "textDocument/didOpen"
   (list :textDocument
         (list :uri (lsp--buffer-uri)
               :languageId (lsp-buffer-language)
               :version lsp--cur-version
               :text (lsp--buffer-content))))

  (lsp-managed-mode 1)

  (run-hooks 'lsp-after-open-hook)
  (when-let ((client (-some-> lsp--cur-workspace (lsp--workspace-client))))
    (-some-> (lsp--client-after-open-fn client)
      (funcall))
    (-some-> (format "lsp-%s-after-open-hook" (lsp--client-server-id client))
      (intern-soft)
      (run-hooks))))

(defun lsp--text-document-identifier ()
  "Make TextDocumentIdentifier."
  (list :uri (lsp--buffer-uri)))

(defun lsp--versioned-text-document-identifier ()
  "Make VersionedTextDocumentIdentifier."
  (plist-put (lsp--text-document-identifier) :version lsp--cur-version))

(defun lsp--cur-line (&optional point)
  (1- (line-number-at-pos point)))

(defun lsp--cur-position ()
  "Make a Position object for the current point."
  (or (lsp-virtual-buffer-call :cur-position)
      (lsp-save-restriction-and-excursion
        (list :line (lsp--cur-line)
              :character (- (point) (line-beginning-position))))))

(defun lsp--point-to-position (point)
  "Convert POINT to Position."
  (lsp-save-restriction-and-excursion
    (goto-char point)
    (lsp--cur-position)))

(defun lsp--range (start end)
  "Make Range body from START and END."
  ;; make sure start and end are Position objects
  (list :start start :end end))

(defun lsp--region-to-range (start end)
  "Make Range object for the current region."
  (lsp--range (lsp--point-to-position start)
              (lsp--point-to-position end)))

(defun lsp--region-or-line ()
  "The active region or the current line."
  (if (use-region-p)
      (lsp--region-to-range (region-beginning) (region-end))
    (lsp--region-to-range (point-at-bol) (point-at-eol))))

(defun lsp--check-document-changes-version (document-changes)
  "Verify that DOCUMENT-CHANGES have the proper version."
  (unless (seq-every-p
           (-lambda ((&TextDocumentEdit :text-document))
             (or
              (not text-document)
              (let* ((filename (-> text-document
                                   lsp:versioned-text-document-identifier-uri
                                   lsp--uri-to-path))
                     (version (lsp:versioned-text-document-identifier-version? text-document)))
                (with-current-buffer (find-file-noselect filename)
                  (or (null version) (zerop version) (= -1 version)
                      (equal version lsp--cur-version))))))
           document-changes)
    (error "Document changes cannot be applied")))

(defun lsp--apply-workspace-edit (workspace-edit &optional operation)
  "Apply the WorkspaceEdit object WORKSPACE-EDIT.
OPERATION is symbol representing the source of this text edit."
  (-let (((&WorkspaceEdit :document-changes? :changes?) workspace-edit))
    (if-let ((document-changes (seq-reverse document-changes?)))
        (progn
          (lsp--check-document-changes-version document-changes)
          (->> document-changes
               (seq-filter (-lambda ((&CreateFile :kind)) (equal kind "create")))
               (seq-do (lambda (change) (lsp--apply-text-document-edit change operation))))
          (->> document-changes
               (seq-filter (-lambda ((&CreateFile :kind))
                             (and (or (not kind) (equal kind "edit"))
                                  (not (equal kind "create")))))
               (seq-do (lambda (change) (lsp--apply-text-document-edit change operation))))
          (->> document-changes
               (seq-filter (-lambda ((&CreateFile :kind))
                             (and (not (or (not kind) (equal kind "edit")))
                                  (not (equal kind "create")))))
               (seq-do (lambda (change) (lsp--apply-text-document-edit change operation)))))
      (lsp-map
       (lambda (uri text-edits)
         (with-current-buffer (-> uri lsp--uri-to-path find-file-noselect)
           (lsp--apply-text-edits text-edits operation)))
       changes?))))

(defmacro lsp-with-filename (file &rest body)
  "Execute BODY with FILE as a context.
Need to handle the case when FILE indicates virtual buffer."
  (declare (indent 1) (debug t))
  `(if-let ((lsp--virtual-buffer (get-text-property 0 'lsp-virtual-buffer ,file)))
       (lsp-with-current-buffer lsp--virtual-buffer
         ,@body)
     ,@body))

(defun lsp--apply-text-document-edit (edit &optional operation)
  "Apply the TextDocumentEdit object EDIT.
OPERATION is symbol representing the source of this text edit.
If the file is not being visited by any buffer, it is opened with
`find-file-noselect'.
Because lsp-mode does not store previous document versions, the edit is only
applied if the version of the textDocument matches the version of the
corresponding file.

interface TextDocumentEdit {
  textDocument: VersionedTextDocumentIdentifier;
  edits: TextEdit[];
}"
  (pcase (lsp:edit-kind edit)
    ("create" (-let* (((&CreateFile :uri :options?) edit)
                      (file-name (lsp--uri-to-path uri)))
                (mkdir (f-dirname file-name) t)
                (f-touch file-name)
                (when (lsp:create-file-options-overwrite? options?)
                  (f-write-text "" nil file-name))
                (find-file-noselect file-name)))
    ("delete" (-let (((&DeleteFile :uri :options? (&DeleteFileOptions? :recursive?)) edit))
                (f-delete (lsp--uri-to-path uri) recursive?)))
    ("rename" (-let* (((&RenameFile :old-uri :new-uri :options? (&RenameFileOptions? :overwrite?)) edit)
                      (old-file-name (lsp--uri-to-path old-uri))
                      (new-file-name (lsp--uri-to-path new-uri))
                      (buf (find-buffer-visiting old-file-name)))
                (when buf
                  (lsp-with-current-buffer buf
                    (save-buffer)
                    (lsp--text-document-did-close)))
                (mkdir (f-dirname new-file-name) t)
                (rename-file old-file-name new-file-name overwrite?)
                (when buf
                  (lsp-with-current-buffer buf
                    (set-buffer-modified-p nil)
                    (setq lsp-buffer-uri nil)
                    (set-visited-file-name new-file-name)
                    (lsp)))))
    (_ (let ((file-name (->> edit
                             (lsp:text-document-edit-text-document)
                             (lsp:versioned-text-document-identifier-uri)
                             (lsp--uri-to-path))))
         (lsp-with-current-buffer (find-buffer-visiting file-name)
           (lsp-with-filename file-name
             (lsp--apply-text-edits (lsp:text-document-edit-edits edit) operation)))))))

(lsp-defun lsp--position-compare ((&Position :line left-line
                                             :character left-character)
                                  (&Position :line right-line
                                             :character right-character))
  "Return t if position LEFT is greater than RIGHT."
  (if (= left-line right-line)
      (> left-character right-character)
    (> left-line right-line)))

(lsp-defun lsp-point-in-range? (position (&Range :start :end))
  "Returns if POINT is in RANGE."
  (not (or (lsp--position-compare start position)
           (lsp--position-compare position end))))

(lsp-defun lsp--position-equal ((&Position :line left-line
                                           :character left-character)
                                (&Position :line right-line
                                           :character right-character))
  "Return whether LEFT and RIGHT positions are equal."
  (and (= left-line right-line)
       (= left-character right-character)))

(lsp-defun lsp--text-edit-sort-predicate ((&TextEdit :range (&Range :start left-start :end left-end))
                                          (&TextEdit :range (&Range :start right-start :end right-end)))
  (if (lsp--position-equal left-start right-start)
      (lsp--position-compare left-end right-end)
    (lsp--position-compare left-start right-start)))

(lsp-defun lsp--apply-text-edit ((edit &as &TextEdit :range (&RangeToPoint :start :end) :new-text))
  "Apply the edits described in the TextEdit object in TEXT-EDIT."
  ;; We sort text edits so as to apply edits that modify latter parts of the
  ;; document first. Furthermore, because the LSP spec dictates that:
  ;; "If multiple inserts have the same position, the order in the array
  ;; defines which edit to apply first."
  ;; We reverse the initial list and sort stably to make sure the order among
  ;; edits with the same position is preserved.
  (setq new-text (s-replace "\r" "" (or new-text "")))
  (lsp:set-text-edit-new-text edit new-text)
  (goto-char start)
  (delete-region start end)
  (insert new-text))

;; WORKAROUND: typescript-language might send -1 when applying code actions.
;; see https://github.com/emacs-lsp/lsp-mode/issues/1582
(lsp-defun lsp--fix-point ((point &as &Position :character :line))
  (-doto point
    (lsp:set-position-line (max 0 line))
    (lsp:set-position-character (max 0 character))))

(lsp-defun lsp--apply-text-edit-replace-buffer-contents ((edit &as
                                                               &TextEdit
                                                               :range (&Range :start :end)
                                                               :new-text))
  "Apply the edits described in the TextEdit object in TEXT-EDIT.
The method uses `replace-buffer-contents'."
  (setq new-text (s-replace "\r" "" (or new-text "")))
  (lsp:set-text-edit-new-text edit new-text)
  (-let* ((source (current-buffer))
          ((beg . end) (lsp--range-to-region (lsp-make-range :start (lsp--fix-point start)
                                                             :end (lsp--fix-point end)))))
    (with-temp-buffer
      (insert new-text)
      (let ((temp (current-buffer)))
        (with-current-buffer source
          (save-excursion
            (save-restriction
              (narrow-to-region beg end)

              ;; On emacs versions < 26.2,
              ;; `replace-buffer-contents' is buggy - it calls
              ;; change functions with invalid arguments - so we
              ;; manually call the change functions here.
              ;;
              ;; See emacs bugs #32237, #32278:
              ;; https://debbugs.gnu.org/cgi/bugreport.cgi?bug=32237
              ;; https://debbugs.gnu.org/cgi/bugreport.cgi?bug=32278
              (let ((inhibit-modification-hooks t)
                    (length (- end beg)))
                (run-hook-with-args 'before-change-functions
                                    beg end)
                (replace-buffer-contents temp)
                (run-hook-with-args 'after-change-functions
                                    beg (+ beg (length new-text))
                                    length)))))))))

(defun lsp--to-yasnippet-snippet (snippet)
  "Convert LSP SNIPPET to yasnippet snippet."
  ;; LSP snippet doesn't escape "{" and "`", but yasnippet requires escaping it.
  (replace-regexp-in-string (rx (or bos (not (any "$" "\\"))) (group (or "{" "`")))
                            (rx "\\" (backref 1))
                            snippet
                            nil nil 1))

(defvar-local lsp-enable-relative-indentation nil
  "Enable relative indentation when insert texts, snippets ...
from language server.")

(defun lsp--expand-snippet (snippet &optional start end expand-env)
  "Wrapper of `yas-expand-snippet' with all of it arguments.
The snippet will be convert to LSP style and indent according to
LSP server result."
  (let* ((inhibit-field-text-motion t)
         (yas-wrap-around-region nil)
         (yas-indent-line 'none)
         (yas-also-auto-indent-first-line nil))
    (yas-expand-snippet
     (lsp--to-yasnippet-snippet snippet)
     start end expand-env)))

(defun lsp--indent-lines (start end &optional insert-text-mode?)
  "Indent from START to END based on INSERT-TEXT-MODE? value.
- When INSERT-TEXT-MODE? is provided
  - if it's `lsp/insert-text-mode-as-it', do no editor indentation.
  - if it's `lsp/insert-text-mode-adjust-indentation', adjust leading
    whitespaces to match the line where text is inserted.
- When it's not provided, using `indent-line-function' for each line."
  (save-excursion
    (goto-char end)
    (let* ((end-line (line-number-at-pos))
           (offset (save-excursion
                     (goto-char start)
                     (current-indentation)))
           (indent-line-function
            (cond ((equal insert-text-mode? lsp/insert-text-mode-as-it)
                   #'ignore)
                  ((or (equal insert-text-mode? lsp/insert-text-mode-adjust-indentation)
                       lsp-enable-relative-indentation
                       ;; Indenting snippets is extremely slow in `org-mode' buffers
                       ;; since it has to calculate indentation based on SRC block
                       ;; position.  Thus we use relative indentation as default.
                       (derived-mode-p 'org-mode))
                   (lambda () (save-excursion
                                (beginning-of-line)
                                (indent-to-column offset))))
                  (t indent-line-function))))
      (goto-char start)
      (forward-line)
      (while (and (not (eobp))
                  (<= (line-number-at-pos) end-line))
        (funcall indent-line-function)
        (forward-line)))))

(defun lsp--apply-text-edits (edits &optional operation)
  "Apply the EDITS described in the TextEdit[] object.
OPERATION is symbol representing the source of this text edit."
  (unless (seq-empty-p edits)
    (atomic-change-group
      (run-hooks 'lsp-before-apply-edits-hook)
      (let* ((change-group (prepare-change-group))
             (howmany (length edits))
             (message (format "Applying %s edits to `%s' ..." howmany (current-buffer)))
             (_ (lsp--info message))
             (reporter (make-progress-reporter message 0 howmany))
             (done 0)
             (apply-edit (if (not lsp--virtual-buffer)
                             #'lsp--apply-text-edit-replace-buffer-contents
                           #'lsp--apply-text-edit)))
        (unwind-protect
            (->> edits
                 (nreverse)
                 (seq-sort #'lsp--text-edit-sort-predicate)
                 (mapc (lambda (edit)
                         (progress-reporter-update reporter (cl-incf done))
                         (funcall apply-edit edit)
                         (when (lsp:snippet-text-edit-insert-text-format? edit)
                           (-when-let ((&SnippetTextEdit :range (&RangeToPoint :start)
                                                         :insert-text-format? :new-text) edit)
                             (when (eq insert-text-format? lsp/insert-text-format-snippet)
                               ;; No `save-excursion' needed since expand snippet will change point anyway
                               (goto-char (+ start (length new-text)))
                               (lsp--indent-lines start (point))
                               (lsp--expand-snippet new-text start (point)))))
                         (run-hook-with-args 'lsp-after-apply-edits-hook operation))))
          (undo-amalgamate-change-group change-group)
          (progress-reporter-done reporter))))))

(defun lsp--create-apply-text-edits-handlers ()
  "Create (handler cleanup-fn) for applying text edits in async request.
Only works when mode is 'tick or 'alive."
  (let* (first-edited
         (func (lambda (start &rest _)
                 (setq first-edited (if first-edited
                                        (min start first-edited)
                                      start)))))
    (add-hook 'before-change-functions func nil t)
    (list
     (lambda (edits)
       (if (and first-edited
                (seq-find (-lambda ((&TextEdit :range (&RangeToPoint :end)))
                            ;; Text edit region is overlapped
                            (> end first-edited))
                          edits))
           (lsp--warn "TextEdits will not be applied since document has been modified before of them.")
         (lsp--apply-text-edits edits 'completion-cleanup)))
     (lambda ()
       (remove-hook 'before-change-functions func t)))))

(defun lsp--capability (cap &optional capabilities)
  "Get the value of capability CAP.  If CAPABILITIES is non-nil, use them instead."
  (when (stringp cap)
    (setq cap (intern (concat ":" cap))))

  (lsp-get (or capabilities
               (lsp--server-capabilities))
           cap))

(defun lsp--registered-capability (method)
  "Check whether there is workspace providing METHOD."
  (->> (lsp-workspaces)
       (--keep (seq-find (lambda (reg)
                           (equal (lsp--registered-capability-method reg) method))
                         (lsp--workspace-registered-server-capabilities it)))
       cl-first))

(defvar-local lsp--before-change-vals nil
  "Store the positions from the `lsp-before-change' function call, for
validation and use in the `lsp-on-change' function.")

(defun lsp--text-document-content-change-event (start end length)
  "Make a TextDocumentContentChangeEvent body for START to END, of length LENGTH."
  ;; So (47 54 0) means add    7 chars starting at pos 47
  ;; must become
  ;;   {"range":{"start":{"line":5,"character":6}
  ;;             ,"end" :{"line":5,"character":6}}
  ;;             ,"rangeLength":0
  ;;             ,"text":"\nbb = 5"}
  ;;
  ;; And (47 47 7) means delete 7 chars starting at pos 47
  ;; must become
  ;;   {"range":{"start":{"line":6,"character":0}
  ;;            ,"end"  :{"line":7,"character":0}}
  ;;            ,"rangeLength":7
  ;;            ,"text":""}
  ;;
  ;; (208 221 3) means delete 3 chars starting at pos 208, and replace them with
  ;; 13 chars. So it must become
  ;;   {"range":{"start":{"line":5,"character":8}
  ;;             ,"end" :{"line":5,"character":11}}
  ;;             ,"rangeLength":3
  ;;             ,"text":"new-chars-xxx"}
  ;;

  ;; Adding text:
  ;;   lsp-before-change:(start,end)=(33,33)
  ;;   lsp-on-change:(start,end,length)=(33,34,0)
  ;;
  ;; Changing text:
  ;;   lsp-before-change:(start,end)=(208,211)
  ;;   lsp-on-change:(start,end,length)=(208,221,3)
  ;;
  ;; Deleting text:
  ;;   lsp-before-change:(start,end)=(19,27)
  ;;   lsp-on-change:(start,end,length)=(19,19,8)
  (if (zerop length)
      ;; Adding something only, work from start only
      `( :range ,(lsp--range
                  (lsp--point-to-position start)
                  (lsp--point-to-position start))
         :rangeLength 0
         :text ,(buffer-substring-no-properties start end))

    (if (eq start end)
        ;; Deleting something only
        (if (lsp--bracketed-change-p start length)
            ;; The before-change value is bracketed, use it
            `( :range ,(lsp--range
                        (lsp--point-to-position start)
                        (plist-get lsp--before-change-vals :end-pos))
               :rangeLength ,length
               :text "")
          ;; If the change is not bracketed, send a full change event instead.
          (lsp--full-change-event))

      ;; Deleting some things, adding others
      (if (lsp--bracketed-change-p start length)
          ;; The before-change value is valid, use it
          `( :range ,(lsp--range
                      (lsp--point-to-position start)
                      (plist-get lsp--before-change-vals :end-pos))
             :rangeLength ,length
             :text ,(buffer-substring-no-properties start end))
        (lsp--full-change-event)))))

(defun lsp--bracketed-change-p (start length)
  "If the before and after positions are the same, and the length
is the size of the start range, we are probably good."
  (-let [(&plist :end before-end :start before-start) lsp--before-change-vals]
    (and (eq start before-start)
         (eq length (- before-end before-start)))))

(defun lsp--full-change-event ()
  `(:text ,(lsp--buffer-content)))

(defun lsp-before-change (start end)
  "Executed before a file is changed.
Added to `before-change-functions'."
  ;; Note:
  ;;
  ;; This variable holds a list of functions to call when Emacs is about to
  ;; modify a buffer. Each function gets two arguments, the beginning and end of
  ;; the region that is about to change, represented as integers. The buffer
  ;; that is about to change is always the current buffer when the function is
  ;; called.
  ;;
  ;; WARNING:
  ;;
  ;; Do not expect the before-change hooks and the after-change hooks be called
  ;; in balanced pairs around each buffer change. Also don't expect the
  ;; before-change hooks to be called for every chunk of text Emacs is about to
  ;; delete. These hooks are provided on the assumption that Lisp programs will
  ;; use either before- or the after-change hooks, but not both, and the
  ;; boundaries of the region where the changes happen might include more than
  ;; just the actual changed text, or even lump together several changes done
  ;; piecemeal.
  (save-match-data
    (lsp-save-restriction-and-excursion
      (setq lsp--before-change-vals
            (list :start start
                  :end end
                  :end-pos (lsp--point-to-position end))))))

(defun lsp--flush-delayed-changes ()
  (let ((inhibit-quit t))
    (when lsp--delay-timer
      (cancel-timer lsp--delay-timer))
    (mapc (-lambda ((workspace buffer document change))
            (with-current-buffer buffer
              (with-lsp-workspace workspace
                (lsp-notify "textDocument/didChange"
                            (list :textDocument document
                                  :contentChanges (vector change))))))
          (prog1 (nreverse lsp--delayed-requests)
            (setq lsp--delayed-requests nil)))))

(defun lsp--workspace-sync-method (workspace)
  (let* ((sync (-> workspace
                   (lsp--workspace-server-capabilities)
                   (lsp:server-capabilities-text-document-sync?))))
    (if (lsp-text-document-sync-options? sync)
        (lsp:text-document-sync-options-change? sync)
      sync)))

(defun lsp-on-change (start end length &optional content-change-event-fn)
  "Executed when a file is changed.
Added to `after-change-functions'."
  ;; Note:
  ;;
  ;; Each function receives three arguments: the beginning and end of the region
  ;; just changed, and the length of the text that existed before the change.
  ;; All three arguments are integers. The buffer that has been changed is
  ;; always the current buffer when the function is called.
  ;;
  ;; The length of the old text is the difference between the buffer positions
  ;; before and after that text as it was before the change. As for the
  ;; changed text, its length is simply the difference between the first two
  ;; arguments.
  ;;
  ;; So (47 54 0) means add    7 chars starting at pos 47
  ;; So (47 47 7) means delete 7 chars starting at pos 47
  (save-match-data
    (let ((inhibit-quit t)
          ;; make sure that `lsp-on-change' is called in multi-workspace context
          ;; see #2901
          lsp--cur-workspace)
      ;; A (revert-buffer) call with the 'preserve-modes parameter (eg, as done
      ;; by auto-revert-mode) will cause this handler to get called with a nil
      ;; buffer-file-name. We need the buffer-file-name to send notifications;
      ;; so we skip handling revert-buffer-caused changes and instead handle
      ;; reverts separately in lsp-on-revert
      (when (not revert-buffer-in-progress-p)
        (cl-incf lsp--cur-version)
        (mapc
         (lambda (workspace)
           (pcase (or lsp-document-sync-method
                      (lsp--workspace-sync-method workspace))
             (1
              (if lsp-debounce-full-sync-notifications
                  (setq lsp--delayed-requests
                        (->> lsp--delayed-requests
                             (-remove (-lambda ((_ buffer))
                                        (equal (current-buffer) buffer)))
                             (cons (list workspace
                                         (current-buffer)
                                         (lsp--versioned-text-document-identifier)
                                         (lsp--full-change-event)))))
                (with-lsp-workspace workspace
                  (lsp-notify "textDocument/didChange"
                              (list :contentChanges (vector (lsp--full-change-event))
                                    :textDocument (lsp--versioned-text-document-identifier))))))
             (2
              (with-lsp-workspace workspace
                (lsp-notify
                 "textDocument/didChange"
                 (list :textDocument (lsp--versioned-text-document-identifier)
                       :contentChanges (vector
                                        (if content-change-event-fn
                                            (funcall content-change-event-fn start end length)
                                          (lsp--text-document-content-change-event
                                           start end length)))))))))
         (lsp-workspaces))
        (when lsp--delay-timer (cancel-timer lsp--delay-timer))
        (setq lsp--delay-timer (run-with-idle-timer
                                lsp-debounce-full-sync-notifications-interval
                                nil
                                #'lsp--flush-delayed-changes))
        ;; force cleanup overlays after each change
        (lsp--remove-overlays 'lsp-highlight)
        (lsp--after-change (current-buffer))
        (setq lsp--signature-last-index nil
              lsp--signature-last nil)
        ;; cleanup diagnostics
        (when lsp-diagnostic-clean-after-change
          (lsp-foreach-workspace
           (-let [diagnostics (lsp--workspace-diagnostics lsp--cur-workspace)]
             (remhash (lsp--fix-path-casing (buffer-file-name)) diagnostics))))))))



;; facilities for on change hooks. We do not want to make lsp calls on each
;; change event so we add debounce to avoid flooding the server with events.
;; Additionally, we want to have a mechanism for stopping the server calls in
;; particular cases like, e. g. when performing completion.

(defvar lsp-inhibit-lsp-hooks nil
  "Flag to control.")

(defcustom lsp-on-change-hook nil
  "Hooks to run when buffer has changed."
  :type 'hook
  :group 'lsp-mode)

(defcustom lsp-idle-delay 0.500
  "Debounce interval for `after-change-functions'."
  :type 'number
  :group 'lsp-mode)

(defcustom lsp-on-idle-hook nil
  "Hooks to run after `lsp-idle-delay'."
  :type 'hook
  :group 'lsp-mode)

(defun lsp--idle-reschedule (buffer)
  (when lsp--on-idle-timer
    (cancel-timer lsp--on-idle-timer))

  (setq lsp--on-idle-timer (run-with-idle-timer
                            lsp-idle-delay
                            nil
                            #'lsp--on-idle
                            buffer)))

(defun lsp--post-command ()
  (lsp--cleanup-highlights-if-needed)
  (lsp--idle-reschedule (current-buffer)))

(defun lsp--on-idle (buffer)
  "Start post command loop."
  (when (and (buffer-live-p buffer)
             (equal buffer (current-buffer))
             (not lsp-inhibit-lsp-hooks)
             lsp-managed-mode)
    (run-hooks 'lsp-on-idle-hook)))

(defun lsp--on-change-debounce (buffer)
  (when (and (buffer-live-p buffer)
             (equal buffer (current-buffer))
             (not lsp-inhibit-lsp-hooks)
             lsp-managed-mode)
    (run-hooks 'lsp-on-change-hook)))

(defun lsp--after-change (buffer)
  (when (fboundp 'lsp--semantic-tokens-refresh-if-enabled)
    (lsp--semantic-tokens-refresh-if-enabled buffer))
  (when lsp--on-change-timer
    (cancel-timer lsp--on-change-timer))
  (setq lsp--on-change-timer (run-with-idle-timer
                              lsp-idle-delay
                              nil
                              #'lsp--on-change-debounce
                              buffer))
  (lsp--idle-reschedule buffer))



(defun lsp--on-type-formatting (first-trigger-characters more-trigger-characters)
  "Self insert handling.
Applies on type formatting."
  (let ((ch last-command-event))
    (when (or (eq (string-to-char first-trigger-characters) ch)
              (cl-find ch more-trigger-characters :key #'string-to-char))
      (lsp-request-async "textDocument/onTypeFormatting"
                         (lsp-make-document-on-type-formatting-params
                          :text-document (lsp--text-document-identifier)
                          :options (lsp-make-formatting-options
                                    :tab-size (symbol-value (lsp--get-indent-width major-mode))
                                    :insert-spaces (if indent-tabs-mode :json-false t))
                          :ch (char-to-string ch)
                          :position (lsp--cur-position))
                         (lambda (data) (lsp--apply-text-edits data 'format))
                         :mode 'tick))))


;; links
(defun lsp--document-links ()
  (when (lsp-feature? "textDocument/documentLink")
    (lsp-request-async
     "textDocument/documentLink"
     `(:textDocument ,(lsp--text-document-identifier))
     (lambda (links)
       (lsp--remove-overlays 'lsp-link)
       (seq-do
        (-lambda ((link &as &DocumentLink :range (&Range :start :end)))
          (-doto (make-button (lsp--position-to-point start)
                              (lsp--position-to-point end)
                              'action (lsp--document-link-keymap link)
                              'keymap (let ((map (make-sparse-keymap)))
                                        (define-key map [M-return] 'push-button)
                                        (define-key map [mouse-2] 'push-button)
                                        map)
                              'help-echo "mouse-2, M-RET: Visit this link")
            (overlay-put 'lsp-link t)))
        links))
     :mode 'unchanged)))

(defun lsp--document-link-handle-target (url)
  (let* ((parsed-url (url-generic-parse-url (url-unhex-string url)))
         (type (url-type parsed-url)))
    (pcase type
      ("file"
       (find-file (lsp--uri-to-path url))
       (-when-let ((_ line column) (s-match (rx "#" (group (1+ num)) "," (group (1+ num))) url))
         (goto-char (lsp--position-to-point
                     (lsp-make-position :character (1- (string-to-number column))
                                        :line (1- (string-to-number line)))))))
      ((or "http" "https") (browse-url url))
      (type (if-let ((handler (lsp--get-uri-handler type)))
                (funcall handler url)
              (signal 'lsp-file-scheme-not-supported (list url)))))))

(lsp-defun lsp--document-link-keymap ((link &as &DocumentLink :target?))
  (if target?
      (lambda (_)
        (interactive)
        (lsp--document-link-handle-target target?))
    (lambda (_)
      (interactive)
      (when (lsp:document-link-registration-options-resolve-provider?
             (lsp--capability :documentLinkProvider))
        (lsp-request-async
         "documentLink/resolve"
         link
         (-lambda ((&DocumentLink :target?))
           (lsp--document-link-handle-target target?)))))))



(defun lsp-buffer-language ()
  "Get language corresponding current buffer."
  (or (->> lsp-language-id-configuration
           (-first (-lambda ((mode-or-pattern . language))
                     (cond
                      ((and (stringp mode-or-pattern)
                            (s-matches? mode-or-pattern (buffer-file-name))) language)
                      ((eq mode-or-pattern major-mode) language))))
           cl-rest)
      (lsp-warn "Unable to calculate the languageId for buffer `%s'. Take a look at `lsp-language-id-configuration'. The `major-mode' is %s"
                (buffer-name)
                major-mode)))

(defun lsp-activate-on (&rest languages)
  "Returns language activation function.
The function will return t when the `lsp-buffer-language' returns
one of the LANGUAGES."
  (lambda (_file-name _mode)
    (-contains? languages (lsp-buffer-language))))

(defun lsp-workspace-root (&optional path)
  "Find the workspace root for the current file or PATH."
  (-when-let* ((file-name (or path (buffer-file-name)))
               (file-name (lsp-f-canonical file-name)))
    (->> (lsp-session)
         (lsp-session-folders)
         (--filter (and (lsp--files-same-host it file-name)
                        (or (lsp-f-ancestor-of? it file-name)
                            (equal it file-name))))
         (--max-by (> (length it) (length other))))))

(defun lsp-on-revert ()
  "Executed when a file is reverted.
Added to `after-revert-hook'."
  (let ((n (buffer-size))
        (revert-buffer-in-progress-p nil))
    (lsp-on-change 0 n n)))

(defun lsp--text-document-did-close (&optional keep-workspace-alive)
  "Executed when the file is closed, added to `kill-buffer-hook'.

If KEEP-WORKSPACE-ALIVE is non-nil, do not shutdown the workspace
if it's closing the last buffer in the workspace."
  (lsp-foreach-workspace
   (cl-callf2 delq (lsp-current-buffer) (lsp--workspace-buffers lsp--cur-workspace))
   (with-demoted-errors "Error sending didClose notification in ‘lsp--text-document-did-close’: %S"
     (lsp-notify "textDocument/didClose"
                 `(:textDocument ,(lsp--text-document-identifier))))
   (when (and (not lsp-keep-workspace-alive)
              (not keep-workspace-alive)
              (not (lsp--workspace-buffers lsp--cur-workspace)))
     (lsp--shutdown-workspace))))

(defun lsp--will-save-text-document-params (reason)
  (list :textDocument (lsp--text-document-identifier)
        :reason reason))

(defun lsp--before-save ()
  "Before save handler."
  (with-demoted-errors "Error in ‘lsp--before-save’: %S"
    (let ((params (lsp--will-save-text-document-params 1)))
      (when (lsp--send-will-save-p)
        (lsp-notify "textDocument/willSave" params))
      (when (and (lsp--send-will-save-wait-until-p) lsp-before-save-edits)
        (let ((lsp-response-timeout 0.1))
          (condition-case nil
              (lsp--apply-text-edits
               (lsp-request "textDocument/willSaveWaitUntil"
                            params)
               'before-save)
            (error)))))))

(defun lsp--on-auto-save ()
  "Handler for auto-save."
  (when (lsp--send-will-save-p)
    (with-demoted-errors "Error in ‘lsp--on-auto-save’: %S"
      (lsp-notify "textDocument/willSave" (lsp--will-save-text-document-params 2)))))

(defun lsp--text-document-did-save ()
  "Executed when the file is closed, added to `after-save-hook''."
  (when (lsp--send-did-save-p)
    (with-demoted-errors "Error on ‘lsp--text-document-did-save: %S’"
      (lsp-notify "textDocument/didSave"
                  `( :textDocument ,(lsp--versioned-text-document-identifier)
                     ,@(when (lsp--save-include-text-p)
                         (list :text (lsp--buffer-content))))))))

(defun lsp--text-document-position-params (&optional identifier position)
  "Make TextDocumentPositionParams for the current point in the current document.
If IDENTIFIER and POSITION are non-nil, they will be used as the document identifier
and the position respectively."
  (list :textDocument (or identifier (lsp--text-document-identifier))
        :position (or position (lsp--cur-position))))

(defun lsp--get-buffer-diagnostics ()
  "Return buffer diagnostics."
  (gethash (or
            (plist-get lsp--virtual-buffer :buffer-file-name)
            (lsp--fix-path-casing (buffer-file-name)))
           (lsp-diagnostics t)))

(defun lsp-cur-line-diagnostics ()
  "Return any diagnostics that apply to the current line."
  (-let [(&plist :start (&plist :line start) :end (&plist :line end)) (lsp--region-or-line)]
    (cl-coerce (-filter
                (-lambda ((&Diagnostic :range (&Range :start (&Position :line))))
                  (and (>= line start) (<= line end)))
                (lsp--get-buffer-diagnostics))
               'vector)))

(defalias 'lsp--cur-line-diagnotics 'lsp-cur-line-diagnostics)

(defun lsp--extract-line-from-buffer (pos)
  "Return the line pointed to by POS (a Position object) in the current buffer."
  (let* ((point (lsp--position-to-point pos))
         (inhibit-field-text-motion t))
    (save-excursion
      (goto-char point)
      (buffer-substring (line-beginning-position) (line-end-position)))))

(lsp-defun lsp--xref-make-item (filename (&Range :start (start &as &Position :character start-char :line start-line)
                                                 :end (end &as &Position :character end-char)))
  "Return a xref-item from a RANGE in FILENAME."
  (let* ((line (lsp--extract-line-from-buffer start))
         (len (length line)))
    (add-face-text-property (max (min start-char len) 0)
                            (max (min end-char len) 0)
                            'highlight t line)
    ;; LINE is nil when FILENAME is not being current visited by any buffer.
    (xref-make (or line filename)
               (xref-make-file-location
                filename
                (lsp-translate-line (1+ start-line))
                (lsp-translate-column start-char)))))

(defun lsp--location-uri (loc)
  (if (lsp-location? loc)
      (lsp:location-uri loc)
    (lsp:location-link-target-uri loc)))

(lsp-defun lsp-goto-location ((loc &as &Location :uri :range (&Range :start)))
  "Go to location."
  (let ((path (lsp--uri-to-path uri)))
    (if (f-exists? path)
        (with-current-buffer (find-file path)
          (goto-char (lsp--position-to-point start)))
      (error "There is no file %s" path))))

(defun lsp--location-range (loc)
  (if (lsp-location? loc)
      (lsp:location-range loc)
    (lsp:location-link-target-selection-range loc)))

(defun lsp--locations-to-xref-items (locations)
  "Return a list of `xref-item' given LOCATIONS, which can be of
type Location, LocationLink, Location[] or LocationLink[]."
  (setq locations
        (pcase locations
          ((seq (or (Location)
                    (LocationLink)))
           (append locations nil))
          ((or (Location)
               (LocationLink))
           (list locations))))

  (cl-labels ((get-xrefs-in-file
               (file-locs)
               (-let [(filename . matches) file-locs]
                 (condition-case err
                     (let ((visiting (find-buffer-visiting filename))
                           (fn (lambda (loc)
                                 (lsp-with-filename filename
                                   (lsp--xref-make-item filename
                                                        (lsp--location-range loc))))))
                       (if visiting
                           (with-current-buffer visiting
                             (seq-map fn matches))
                         (when (file-readable-p filename)
                           (with-temp-buffer
                             (insert-file-contents-literally filename)
                             (seq-map fn matches)))))
                   (error (lsp-warn "Failed to process xref entry for filename '%s': %s"
                                    filename (error-message-string err)))
                   (file-error (lsp-warn "Failed to process xref entry, file-error, '%s': %s"
                                         filename (error-message-string err)))))))

    (->> locations
         (seq-sort #'lsp--location-before-p)
         (seq-group-by (-compose #'lsp--uri-to-path #'lsp--location-uri))
         (seq-map #'get-xrefs-in-file)
         (apply #'nconc))))

(defun lsp--location-before-p (left right)
  "Sort first by file, then by line, then by column."
  (let ((left-uri (lsp--location-uri left))
        (right-uri (lsp--location-uri right)))
    (if (not (string= left-uri right-uri))
        (string< left-uri right-uri)
      (-let (((&Range :start left-start) (lsp--location-range left))
             ((&Range :start right-start) (lsp--location-range right)))
        (lsp--position-compare right-start left-start)))))

(defun lsp--make-reference-params (&optional td-position include-declaration)
  "Make a ReferenceParam object.
If TD-POSITION is non-nil, use it as TextDocumentPositionParams object instead.
If INCLUDE-DECLARATION is non-nil, request the server to include declarations."
  (let ((json-false :json-false))
    (plist-put (or td-position (lsp--text-document-position-params))
               :context `(:includeDeclaration ,(or include-declaration json-false)))))

(defun lsp--cancel-request (id)
  "Cancel request with ID in all workspaces."
  (lsp-foreach-workspace
   (->> lsp--cur-workspace lsp--workspace-client lsp--client-response-handlers (remhash id))
   (lsp-notify "$/cancelRequest" `(:id ,id))))

(defun lsp-eldoc-function ()
  "`lsp-mode' eldoc function."
  (run-hooks 'lsp-eldoc-hook)
  eldoc-last-message)

(defun lsp--point-on-highlight? ()
  (-some? (lambda (overlay)
            (overlay-get overlay 'lsp-highlight))
          (overlays-at (point))))

(defun lsp--cleanup-highlights-if-needed ()
  (when (and lsp-enable-symbol-highlighting
             lsp--have-document-highlights
             (not (lsp--point-on-highlight?)))
    (lsp--remove-overlays 'lsp-highlight)
    (setq lsp--have-document-highlights nil)
    (lsp-cancel-request-by-token :highlights)))

(defvar-local lsp--symbol-bounds-of-last-highlight-invocation nil
  "The bounds of the symbol from which `lsp--document-highlight'
  most recently requested highlights.")

(defun lsp--document-highlight ()
  (when (lsp-feature? "textDocument/documentHighlight")
    (let ((curr-sym-bounds (bounds-of-thing-at-point 'symbol)))
      (unless (or (looking-at "[[:space:]\n]")
                  (not lsp-enable-symbol-highlighting)
                  (and lsp--have-document-highlights
                       curr-sym-bounds
                       (equal curr-sym-bounds
                              lsp--symbol-bounds-of-last-highlight-invocation)))
        (setq lsp--symbol-bounds-of-last-highlight-invocation
              curr-sym-bounds)
        (lsp-request-async "textDocument/documentHighlight"
                           (lsp--text-document-position-params)
                           #'lsp--document-highlight-callback
                           :mode 'tick
                           :cancel-token :highlights)))))

(defun lsp-describe-thing-at-point ()
  "Display the type signature and documentation of the thing at
point."
  (interactive)
  (let ((contents (-some->> (lsp--text-document-position-params)
                    (lsp--make-request "textDocument/hover")
                    (lsp--send-request)
                    (lsp:hover-contents))))
    (if (and contents (not (equal contents "")))
        (let ((lsp-help-buf-name "*lsp-help*"))
          (with-current-buffer (get-buffer-create lsp-help-buf-name)
            (with-help-window lsp-help-buf-name
              (insert (string-trim-right (lsp--render-on-hover-content contents t))))))
      (lsp--info "No content at point."))))

(defun lsp--point-in-bounds-p (bounds)
  "Return whether the current point is within BOUNDS."
  (and (<= (car bounds) (point)) (< (point) (cdr bounds))))

(defun lsp-get-renderer (language)
  "Get renderer for LANGUAGE."
  (lambda (str)
    (lsp--render-string str language)))

(defun lsp--setup-markdown (mode)
  "Setup the ‘markdown-mode’ in the frame.
MODE is the mode used in the parent frame."
  (make-local-variable 'markdown-code-lang-modes)
  (dolist (mark (alist-get mode lsp-custom-markup-modes))
    (add-to-list 'markdown-code-lang-modes (cons mark mode)))
  (setq-local markdown-fontify-code-blocks-natively t)
  (setq-local markdown-fontify-code-block-default-mode mode)
  (setq-local markdown-hide-markup t)

  ;; Render some common HTML entities.
  ;; This should really happen in markdown-mode instead,
  ;; but it doesn't, so we do it here for now.
  (setq prettify-symbols-alist
        (cl-loop for i from 0 to 255
                 collect (cons (format "&#x%02X;" i) i)))
  (push '("&lt;" . ?<) prettify-symbols-alist)
  (push '("&gt;" . ?>) prettify-symbols-alist)
  (push '("&amp;" . ?&) prettify-symbols-alist)
  (push '("&nbsp;" . ? ) prettify-symbols-alist)
  (setq prettify-symbols-compose-predicate
        (lambda (_start _end _match) t))
  (prettify-symbols-mode 1))

(defun lsp--buffer-string-visible ()
  "Return visible buffer string.
Stolen from `org-copy-visible'."
  (let ((temp (generate-new-buffer " *temp*"))
        (beg (point-min))
        (end (point-max)))
    (while (/= beg end)
      (when (get-char-property beg 'invisible)
        (setq beg (next-single-char-property-change beg 'invisible nil end)))
      (let* ((next (next-single-char-property-change beg 'invisible nil end))
             (substring (buffer-substring beg next)))
        (with-current-buffer temp (insert substring))
        ;; (setq result (concat result substring))
        (setq beg next)))
    (setq deactivate-mark t)
    (prog1 (with-current-buffer temp
             (s-chop-suffix "\n" (buffer-string)))
      (kill-buffer temp))))

(defvar lsp-buffer-major-mode nil
  "Holds the major mode when fontification function is running.
See #2588")

(defvar view-inhibit-help-message)

(defun lsp--render-markdown ()
  "Render markdown."

  (let ((markdown-enable-math nil))
    (goto-char (point-min))
    (while (re-search-forward
            (rx (and "\\" (group (or "\\" "`" "*" "_" ":" "/"
                                     "{" "}" "[" "]" "(" ")"
                                     "#" "+" "-" "." "!" "|"))))
            nil t)
      (replace-match (rx (backref 1))))

    ;; markdown-mode v2.3 does not yet provide gfm-view-mode
    (if (fboundp 'gfm-view-mode)
        (let ((view-inhibit-help-message t))
          (gfm-view-mode))
      (gfm-mode))

    (lsp--setup-markdown lsp-buffer-major-mode)))

(defvar lsp--display-inline-image-alist
  '((lsp--render-markdown
     (:regexp
      "!\\[.*?\\](data:image/[a-zA-Z]+;base64,\\([A-Za-z0-9+/\n]+?=*?\\)\\(|[^)]+\\)?)"
      :sexp
      (create-image
       (base64-decode-string
        (buffer-substring-no-properties (match-beginning 1) (match-end 1)))
       nil t))))
  "Replaced string regexp and function returning image.
Each element should have the form (MODE . (PROPERTY-LIST...)).
MODE (car) is function which is defined in `lsp-language-id-configuration'.
Cdr should be list of PROPERTY-LIST.

Each PROPERTY-LIST should have properties:
:regexp  Regexp which determines what string is relpaced to image.
         You should also get information of image, by parenthesis constructs.
         By default, all matched string is replaced to image, but you can
         change index of replaced string by keyword :replaced-index.

:sexp    Return image when evaluated. You can use information of regexp
         by using (match-beggining N), (match-end N) or (match-substring N).

In addition, each can have property:
:replaced-index  Determine index which is used to replace regexp to image.
                 The value means first argument of `match-beginning' and
                 `match-end'. If omitted, interpreted as index 0.")

(defcustom lsp-display-inline-image t
  "Showing inline image or not."
  :group 'lsp-mode
  :type 'boolean)

(defcustom lsp-enable-suggest-server-download t
  "When non-nil enable server downloading suggestions."
  :group 'lsp-mode
  :type 'boolean
  :package-version '(lsp-mode . "8.0.1"))

(defun lsp--display-inline-image (mode)
  "Add image property if available."
  (let ((plist-list (cdr (assq mode lsp--display-inline-image-alist))))
    (when (and (display-images-p) lsp-display-inline-image)
      (cl-loop
       for plist in plist-list
       with regexp with replaced-index
       do
       (setq regexp (plist-get plist :regexp))
       (setq replaced-index (or (plist-get plist :replaced-index) 0))

       (font-lock-remove-keywords nil (list regexp replaced-index))
       (let ((inhibit-read-only t))
         (save-excursion
           (goto-char (point-min))
           (while (re-search-forward regexp nil t)
             (set-text-properties
              (match-beginning replaced-index) (match-end replaced-index)
              nil)
             (add-text-properties
              (match-beginning replaced-index) (match-end replaced-index)
              `(display ,(eval (plist-get plist :sexp)))))))))))

(defun lsp--fontlock-with-mode (str mode)
  "Fontlock STR with MODE."
  (let ((lsp-buffer-major-mode major-mode))
    (condition-case nil
        (with-temp-buffer
          (insert str)
          (delay-mode-hooks (funcall mode))
          (cl-flet ((window-body-width () lsp-window-body-width))
            ;; This can go wrong in some cases, and the fontification would
            ;; not work as expected.
            ;;
            ;; See #2984
            (ignore-errors (font-lock-ensure))
            (lsp--display-inline-image mode))
          (lsp--buffer-string-visible))
      (error str))))

(defun lsp--render-string (str language)
  "Render STR using `major-mode' corresponding to LANGUAGE.
When language is nil render as markup if `markdown-mode' is loaded."
  (setq str (s-replace "\r" "" (or str "")))
  (if-let ((mode (-some (-lambda ((mode . lang))
                          (when (and (equal lang language) (functionp mode))
                            mode))
                        lsp-language-id-configuration)))
      (lsp--fontlock-with-mode str mode)
    str))

(defun lsp--render-element (content)
  "Render CONTENT element."
  (let ((inhibit-message t))
    (or
     (pcase content
       ((MarkedString :value :language)
        (lsp--render-string value language))
       ((MarkupContent :value :kind)
        (lsp--render-string value kind))
       ;; plain string
       ((pred stringp) (lsp--render-string content "markdown"))
       ((pred null) "")
       (_ (error "Failed to handle %s" content)))
     "")))

(defun lsp--create-unique-string-fn ()
  (let (elements)
    (lambda (element)
      (let ((count (cl-count element elements :test #'string=)))
        (prog1 (if (zerop count)
                   element
                 (format "%s (%s)" element count))
          (push element elements))))))

(defun lsp--select-action (actions)
  "Select an action to execute from ACTIONS."
  (cond
   ((seq-empty-p actions) (signal 'lsp-no-code-actions nil))
   ((and (eq (seq-length actions) 1) lsp-auto-execute-action)
    (lsp-seq-first actions))
   (t (let ((completion-ignore-case t))
        (lsp--completing-read "Select code action: "
                              (seq-into actions 'list)
                              (-compose (lsp--create-unique-string-fn)
                                        #'lsp:code-action-title)
                              nil t)))))

(defun lsp--workspace-server-id (workspace)
  "Return the server ID of WORKSPACE."
  (-> workspace lsp--workspace-client lsp--client-server-id))

(defun lsp--handle-rendered-for-echo-area (contents)
  "Return a single line from RENDERED, appropriate for display in the echo area."
  (pcase (lsp-workspaces)
    (`(,workspace)
     (lsp-clients-extract-signature-on-hover contents (lsp--workspace-server-id workspace)))
    ;; For projects with multiple active workspaces we also default to
    ;; render the first line.
    (_ (lsp-clients-extract-signature-on-hover contents nil))))

(cl-defgeneric lsp-clients-extract-signature-on-hover (contents _server-id)
  "Extract a representative line from CONTENTS, to show in the echo area."
  (car (s-lines (s-trim (lsp--render-element contents)))))

(defun lsp--render-on-hover-content (contents render-all)
  "Render the content received from 'document/onHover' request.
CONTENTS  - MarkedString | MarkedString[] | MarkupContent
RENDER-ALL - nil if only the signature should be rendered."
  (cond
   ((lsp-markup-content? contents)
    ;; MarkupContent.
    ;; It tends to be long and is not suitable to display fully in the echo area.
    ;; Just display the first line which is typically the signature.
    (if render-all
        (lsp--render-element contents)
      (lsp--handle-rendered-for-echo-area contents)))
   ((and (stringp contents) (not (string-match-p "\n" contents)))
    ;; If the contents is a single string containing a single line,
    ;; render it always.
    (lsp--render-element contents))
   (t
    ;; MarkedString -> MarkedString[]
    (when (or (lsp-marked-string? contents) (stringp contents))
      (setq contents (list contents)))
    ;; Consider the signature consisting of the elements who have a renderable
    ;; "language" property. When render-all is nil, ignore other elements.
    (string-join
     (seq-map
      #'lsp--render-element
      (if render-all
          contents
        ;; Only render contents that have an available renderer.
        (seq-take
         (seq-filter
          (-andfn #'lsp-marked-string?
                  (-compose #'lsp-get-renderer #'lsp:marked-string-language))
          contents)
         1)))
     (if (bound-and-true-p page-break-lines-mode)
         "\n\n"
       "\n")))))



(defvar lsp-signature-mode-map
  (-doto (make-sparse-keymap)
    (define-key (kbd "M-n") #'lsp-signature-next)
    (define-key (kbd "M-p") #'lsp-signature-previous)
    (define-key (kbd "M-a") #'lsp-signature-toggle-full-docs)
    (define-key (kbd "C-c C-k") #'lsp-signature-stop)
    (define-key (kbd "C-g") #'lsp-signature-stop))
  "Keymap for `lsp-signature-mode-map'.")

(define-minor-mode lsp-signature-mode
  "Mode used to show signature popup."
  :keymap lsp-signature-mode-map
  :lighter ""
  :group 'lsp-mode)

(defun lsp-signature-stop ()
  "Stop showing current signature help."
  (interactive)
  (lsp-cancel-request-by-token :signature)
  (remove-hook 'post-command-hook #'lsp-signature)
  (funcall lsp-signature-function nil)
  (lsp-signature-mode -1))

(declare-function page-break-lines--update-display-tables "ext:page-break-lines")

(defun lsp--setup-page-break-mode-if-present ()
  "Enable `page-break-lines-mode' in current buffer."
  (when (fboundp 'page-break-lines-mode)
    (page-break-lines-mode)
    ;; force page-break-lines-mode to update the display tables.
    (page-break-lines--update-display-tables)))

(defun lsp-lv-message (message)
  (add-hook 'lv-window-hook #'lsp--setup-page-break-mode-if-present)
  (if message
      (progn
        (setq lsp--signature-last-buffer (current-buffer))
        (let ((lv-force-update t))
          (lv-message "%s" message)))
    (lv-delete-window)
    (remove-hook 'lv-window-hook #'lsp--setup-page-break-mode-if-present)))

(declare-function posframe-show "ext:posframe")
(declare-function posframe-hide "ext:posframe")
(declare-function posframe-poshandler-point-bottom-left-corner-upward "ext:posframe")

(defface lsp-signature-posframe
  '((t :inherit tooltip))
  "Background and foreground for `lsp-signature-posframe'."
  :group 'lsp-mode)

(defvar lsp-signature-posframe-params
  (list :poshandler #'posframe-poshandler-point-bottom-left-corner-upward
        :height 10
        :width 60
        :border-width 1
        :min-width 60)
  "Params for signature and `posframe-show'.")

(defun lsp-signature-posframe (str)
  "Use posframe to show the STR signatureHelp string."
  (if str
      (apply #'posframe-show
             (with-current-buffer (get-buffer-create " *lsp-signature*")
               (erase-buffer)
               (insert str)
               (visual-line-mode 1)
               (lsp--setup-page-break-mode-if-present)
               (current-buffer))
             (append
              lsp-signature-posframe-params
              (list :position (point)
                    :background-color (face-attribute 'lsp-signature-posframe :background nil t)
                    :foreground-color (face-attribute 'lsp-signature-posframe :foreground nil t)
                    :border-color (face-attribute 'font-lock-comment-face :foreground nil t))))
    (posframe-hide " *lsp-signature*")))

(defun lsp--handle-signature-update (signature)
  (let ((message
         (if (lsp-signature-help? signature)
             (lsp--signature->message signature)
           (mapconcat #'lsp--signature->message signature "\n"))))
    (if (s-present? message)
        (funcall lsp-signature-function message)
      (lsp-signature-stop))))

(defun lsp-signature-activate ()
  "Activate signature help.
It will show up only if current point has signature help."
  (interactive)
  (setq lsp--signature-last nil
        lsp--signature-last-index nil
        lsp--signature-last-buffer (current-buffer))
  (add-hook 'post-command-hook #'lsp-signature)
  (lsp-signature-mode t))

(defcustom lsp-signature-cycle t
  "Whether `lsp-signature-next' and prev should cycle."
  :type 'boolean
  :group 'lsp-mode)

(defun lsp-signature-next ()
  "Show next signature."
  (interactive)
  (let ((nsigs (length (lsp:signature-help-signatures lsp--signature-last))))
    (when (and lsp--signature-last-index
               lsp--signature-last
               (or lsp-signature-cycle (< (1+ lsp--signature-last-index) nsigs)))
      (setq lsp--signature-last-index (% (1+ lsp--signature-last-index) nsigs))
      (funcall lsp-signature-function (lsp--signature->message lsp--signature-last)))))

(defun lsp-signature-previous ()
  "Next signature."
  (interactive)
  (when (and lsp--signature-last-index
             lsp--signature-last
             (or lsp-signature-cycle (not (zerop lsp--signature-last-index))))
    (setq lsp--signature-last-index (1- (if (zerop lsp--signature-last-index)
                                            (length (lsp:signature-help-signatures lsp--signature-last))
                                          lsp--signature-last-index)))
    (funcall lsp-signature-function (lsp--signature->message lsp--signature-last))))

(defun lsp-signature-toggle-full-docs ()
  "Toggle full/partial signature documentation."
  (interactive)
  (let ((all? (not (numberp lsp-signature-doc-lines))))
    (setq lsp-signature-doc-lines (if all?
                                      (or (car-safe lsp-signature-doc-lines)
                                          20)
                                    (list lsp-signature-doc-lines))))
  (lsp-signature-activate))

(defun lsp--signature->message (signature-help)
  "Generate eldoc message from SIGNATURE-HELP response."
  (setq lsp--signature-last signature-help)

  (when (and signature-help (not (seq-empty-p (lsp:signature-help-signatures signature-help))))
    (-let* (((&SignatureHelp :active-signature?
                             :active-parameter?
                             :signatures) signature-help)
            (active-signature? (or lsp--signature-last-index active-signature? 0))
            (_ (setq lsp--signature-last-index active-signature?))
            ((signature &as &SignatureInformation? :label :parameters?) (seq-elt signatures active-signature?))
            (prefix (if (= (length signatures) 1)
                        ""
                      (concat (propertize (format " %s/%s"
                                                  (1+ active-signature?)
                                                  (length signatures))
                                          'face 'success)
                              " ")))
            (method-docs (when
                             (and lsp-signature-render-documentation
                                  (or (not (numberp lsp-signature-doc-lines)) (< 0 lsp-signature-doc-lines)))
                           (let ((docs (lsp--render-element
                                        (lsp:parameter-information-documentation? signature))))
                             (when (s-present? docs)
                               (concat
                                "\n"
                                (if (fboundp 'page-break-lines-mode)
                                    "\n"
                                  "")
                                (if (and (numberp lsp-signature-doc-lines)
                                         (> (length (s-lines docs)) lsp-signature-doc-lines))
                                    (concat (s-join "\n" (-take lsp-signature-doc-lines (s-lines docs)))
                                            (propertize "\nTruncated..." 'face 'highlight))
                                  docs)))))))
      (when (and active-parameter? (not (seq-empty-p parameters?)))
        (-when-let* ((param (when (and (< -1 active-parameter? (length parameters?)))
                              (seq-elt parameters? active-parameter?)))
                     (selected-param-label (let ((label (lsp:parameter-information-label param)))
                                             (if (stringp label) label (append label nil))))
                     (start (if (stringp selected-param-label)
                                (s-index-of selected-param-label label)
                              (cl-first selected-param-label)))
                     (end (if (stringp selected-param-label)
                              (+ start (length selected-param-label))
                            (cl-second selected-param-label))))
          (add-face-text-property start end 'eldoc-highlight-function-argument nil label)))
      (concat prefix label method-docs))))

(defun lsp-signature ()
  "Display signature info (based on `textDocument/signatureHelp')"
  (if (and lsp--signature-last-buffer
           (not (equal (current-buffer) lsp--signature-last-buffer)))
      (lsp-signature-stop)
    (lsp-request-async "textDocument/signatureHelp"
                       (lsp--text-document-position-params)
                       #'lsp--handle-signature-update
                       :cancel-token :signature)))


(defcustom lsp-overlay-document-color-char "■"
  "Display the char represent the document color in overlay"
  :type 'string
  :group 'lsp-mode)

;; color presentation
(defun lsp--color-create-interactive-command (color range)
  (lambda ()
    (interactive)
    (-let [(&ColorPresentation? :text-edit?
                                :additional-text-edits?)
           (lsp--completing-read
            "Select color presentation: "
            (lsp-request
             "textDocument/colorPresentation"
             `( :textDocument ,(lsp--text-document-identifier)
                :color ,color
                :range ,range))
            #'lsp:color-presentation-label
            nil
            t)]
      (when text-edit?
        (lsp--apply-text-edit text-edit?))
      (when additional-text-edits?
        (lsp--apply-text-edits additional-text-edits? 'color-presentation)))))

(defun lsp--number->color (number)
  (let ((result (format "%x"
                        (round (* (or number 0) 255.0)))))
    (if (= 1 (length result))
        (concat "0" result)
      result)))

(defun lsp--document-color ()
  "Document color handler."
  (when (lsp-feature? "textDocument/documentColor")
    (lsp-request-async
     "textDocument/documentColor"
     `(:textDocument ,(lsp--text-document-identifier))
     (lambda (result)
       (lsp--remove-overlays 'lsp-color)
       (seq-do
        (-lambda ((&ColorInformation :color (color &as &Color :red :green :blue)
                                     :range))
          (-let* (((beg . end) (lsp--range-to-region range))
                  (overlay (make-overlay beg end))
                  (command (lsp--color-create-interactive-command color range)))
            (overlay-put overlay 'lsp-color t)
            (overlay-put overlay 'evaporate t)
            (overlay-put overlay
                         'before-string
                         (propertize
                          lsp-overlay-document-color-char
                          'face `((:foreground ,(format
                                                 "#%s%s%s"
                                                 (lsp--number->color red)
                                                 (lsp--number->color green)
                                                 (lsp--number->color blue))))
                          'action command
                          'mouse-face 'lsp-lens-mouse-face
                          'local-map (-doto (make-sparse-keymap)
                                       (define-key [mouse-1] command))))))
        result))
     :mode 'unchanged
     :cancel-token :document-color-token)))


;; hover

(defvar-local lsp--hover-saved-bounds nil)

(defun lsp-hover ()
  "Display hover info (based on `textDocument/signatureHelp')."
  (if (and lsp--hover-saved-bounds
           (lsp--point-in-bounds-p lsp--hover-saved-bounds))
      (lsp--eldoc-message lsp--eldoc-saved-message)
    (setq lsp--hover-saved-bounds nil
          lsp--eldoc-saved-message nil)
    (if (looking-at "[[:space:]\n]")
        (lsp--eldoc-message nil)
      (when (and lsp-eldoc-enable-hover (lsp--capability :hoverProvider))
        (lsp-request-async
         "textDocument/hover"
         (lsp--text-document-position-params)
         (-lambda ((hover &as &Hover? :range? :contents))
           (when hover
             (when range?
               (setq lsp--hover-saved-bounds (lsp--range-to-region range?)))
             (lsp--eldoc-message (and contents
                                      (lsp--render-on-hover-content
                                       contents
                                       lsp-eldoc-render-all)))))
         :error-handler #'ignore
         :mode 'tick
         :cancel-token :eldoc-hover)))))



(defun lsp--action-trigger-parameter-hints (_command)
  "Handler for editor.action.triggerParameterHints."
  (when (member :on-server-request lsp-signature-auto-activate)
    (lsp-signature-activate)))

(defun lsp--action-trigger-suggest (_command)
  "Handler for editor.action.triggerSuggest."
  (cond
   ((and (bound-and-true-p company-mode)
         (fboundp 'company-auto-begin)
         (fboundp 'company-post-command))
    (run-at-time 0 nil
                 (lambda ()
                   (let ((this-command 'company-idle-begin)
                         (company-minimum-prefix-length 0))
                     (company-auto-begin)
                     (company-post-command)))))
   (t
    (completion-at-point))))

(defconst lsp--default-action-handlers
  (ht ("editor.action.triggerParameterHints" #'lsp--action-trigger-parameter-hints)
      ("editor.action.triggerSuggest" #'lsp--action-trigger-suggest))
  "Default action handlers.")

(defun lsp--find-action-handler (command)
  "Find action handler for particular COMMAND."
  (or
   (--some (-some->> it
             (lsp--workspace-client)
             (lsp--client-action-handlers)
             (gethash command))
           (lsp-workspaces))
   (gethash command lsp--default-action-handlers)))

(defun lsp--text-document-code-action-params (&optional kind)
  "Code action params."
  (list :textDocument (lsp--text-document-identifier)
        :range (if (use-region-p)
                   (lsp--region-to-range (region-beginning) (region-end))
                 (lsp--region-to-range (point) (point)))
        :context `( :diagnostics ,(lsp-cur-line-diagnostics)
                    ,@(when kind (list :only (vector kind))))))

(defun lsp-code-actions-at-point (&optional kind)
  "Retrieve the code actions for the active region or the current line.
It will filter by KIND if non nil."
  (lsp-request "textDocument/codeAction" (lsp--text-document-code-action-params kind)))

(defun lsp-execute-code-action-by-kind (command-kind)
  "Execute code action by COMMAND-KIND."
  (if-let ((action (->> (lsp-get-or-calculate-code-actions command-kind)
                        (-filter (-lambda ((&CodeAction :kind?))
                                   (and kind? (s-prefix? command-kind kind?))))
                        lsp--select-action)))
      (lsp-execute-code-action action)
    (signal 'lsp-no-code-actions '(command-kind))))

(defalias 'lsp-get-or-calculate-code-actions 'lsp-code-actions-at-point)

(lsp-defun lsp--execute-command ((action &as &Command :command :arguments?))
  "Parse and execute a code ACTION represented as a Command LSP type."
  (let ((server-id (->> (lsp-workspaces)
                        (cl-first)
                        (or lsp--cur-workspace)
                        (lsp--workspace-client)
                        (lsp--client-server-id))))
    (condition-case nil
        (with-no-warnings
          (lsp-execute-command server-id (intern command) arguments?))
      (cl-no-applicable-method
       (if-let ((action-handler (lsp--find-action-handler command)))
           (funcall action-handler action)
         (lsp-send-execute-command command arguments?))))))

(lsp-defun lsp-execute-code-action ((action &as &CodeAction :command? :edit?))
  "Execute code action ACTION.
If ACTION is not set it will be selected from `lsp-code-actions-at-point'.
Request codeAction/resolve for more info if server supports."
  (interactive (list (lsp--select-action (lsp-code-actions-at-point))))
  (if (and (lsp-feature? "codeAction/resolve")
           (not command?)
           (not edit?))
      (lsp--execute-code-action (lsp-request "codeAction/resolve" action))
    (lsp--execute-code-action action)))

(lsp-defun lsp--execute-code-action ((action &as &CodeAction :command? :edit?))
  "Execute code action ACTION."
  (when edit?
    (lsp--apply-workspace-edit edit? 'code-action))

  (cond
   ((stringp command?) (lsp--execute-command action))
   ((lsp-command? command?) (lsp--execute-command command?))))

(defvar lsp--formatting-indent-alist
  ;; Taken from `dtrt-indent-mode'
  '((c-mode                     . c-basic-offset)                   ; C
    (c++-mode                   . c-basic-offset)                   ; C++
    (csharp-mode                . c-basic-offset)                   ; C#
    (csharp-tree-sitter-mode    . csharp-tree-sitter-indent-offset) ; C#
    (d-mode                     . c-basic-offset)                   ; D
    (java-mode                  . c-basic-offset)                   ; Java
    (jde-mode                   . c-basic-offset)                   ; Java (JDE)
    (js-mode                    . js-indent-level)                  ; JavaScript
    (js2-mode                   . js2-basic-offset)                 ; JavaScript-IDE
    (js3-mode                   . js3-indent-level)                 ; JavaScript-IDE
    (json-mode                  . js-indent-level)                  ; JSON
    (lua-mode                   . lua-indent-level)                 ; Lua
    (objc-mode                  . c-basic-offset)                   ; Objective C
    (php-mode                   . c-basic-offset)                   ; PHP
    (perl-mode                  . perl-indent-level)                ; Perl
    (cperl-mode                 . cperl-indent-level)               ; Perl
    (raku-mode                  . raku-indent-offset)               ; Perl6/Raku
    (erlang-mode                . erlang-indent-level)              ; Erlang
    (ada-mode                   . ada-indent)                       ; Ada
    (sgml-mode                  . sgml-basic-offset)                ; SGML
    (nxml-mode                  . nxml-child-indent)                ; XML
    (pascal-mode                . pascal-indent-level)              ; Pascal
    (typescript-mode            . typescript-indent-level)          ; Typescript
    (sh-mode                    . sh-basic-offset)                  ; Shell Script
    (ruby-mode                  . ruby-indent-level)                ; Ruby
    (enh-ruby-mode              . enh-ruby-indent-level)            ; Ruby
    (crystal-mode               . crystal-indent-level)             ; Crystal (Ruby)
    (css-mode                   . css-indent-offset)                ; CSS
    (rust-mode                  . rust-indent-offset)               ; Rust
    (rustic-mode                . rustic-indent-offset)             ; Rust
    (scala-mode                 . scala-indent:step)                ; Scala
    (powershell-mode            . powershell-indent)                ; PowerShell
    (ess-mode                   . ess-indent-offset)                ; ESS (R)
    (yaml-mode                  . yaml-indent-offset)               ; YAML
    (hack-mode                  . hack-indent-offset)               ; Hack

    (default                    . standard-indent))                 ; default fallback
  "A mapping from `major-mode' to its indent variable.")

(defun lsp--get-indent-width (mode)
  "Get indentation offset for MODE."
  (or (alist-get mode lsp--formatting-indent-alist)
      (lsp--get-indent-width (or (get mode 'derived-mode-parent) 'default))))

(defun lsp--make-document-formatting-params ()
  "Create document formatting params."
  (lsp-make-document-formatting-params
   :text-document (lsp--text-document-identifier)
   :options (lsp-make-formatting-options
             :tab-size (symbol-value (lsp--get-indent-width major-mode))
             :insert-spaces (if indent-tabs-mode :json-false t))))

(defun lsp-format-buffer ()
  "Ask the server to format this document."
  (interactive "*")
  (cond ((lsp-feature? "textDocument/formatting")
         (let ((edits (lsp-request "textDocument/formatting"
                                   (lsp--make-document-formatting-params))))
           (if (seq-empty-p edits)
               (lsp--info "No formatting changes provided")
             (lsp--apply-text-edits edits 'format))))
        ((lsp-feature? "textDocument/rangeFormatting")
         (save-restriction
           (widen)
           (lsp-format-region (point-min) (point-max))))
        (t (signal 'lsp-capability-not-supported (list "documentFormattingProvider")))))

(defun lsp-format-region (s e)
  "Ask the server to format the region, or if none is selected, the current line."
  (interactive "r")
  (let ((edits (lsp-request
                "textDocument/rangeFormatting"
                (lsp--make-document-range-formatting-params s e))))
    (if (seq-empty-p edits)
        (lsp--info "No formatting changes provided")
      (lsp--apply-text-edits edits 'format))))

(defmacro lsp-make-interactive-code-action (func-name code-action-kind)
  "Define an interactive function FUNC-NAME that attempts to
execute a CODE-ACTION-KIND action."
  `(defun ,(intern (concat "lsp-" (symbol-name func-name))) ()
     ,(format "Perform the %s code action, if available." code-action-kind)
     (interactive)
     (condition-case nil
         (lsp-execute-code-action-by-kind ,code-action-kind)
       (lsp-no-code-actions
        (when (called-interactively-p 'any)
          (lsp--info ,(format "%s action not available" code-action-kind)))))))

(lsp-make-interactive-code-action organize-imports "source.organizeImports")

(defun lsp--make-document-range-formatting-params (start end)
  "Make DocumentRangeFormattingParams for selected region."
  (lsp:set-document-range-formatting-params-range (lsp--make-document-formatting-params)
                                                  (lsp--region-to-range start end)))

(defconst lsp--highlight-kind-face
  '((1 . lsp-face-highlight-textual)
    (2 . lsp-face-highlight-read)
    (3 . lsp-face-highlight-write)))

(defun lsp--remove-overlays (name)
  (save-restriction
    (widen)
    (remove-overlays (point-min) (point-max) name t)))

(defun lsp-document-highlight ()
  "Highlight all relevant references to the symbol under point."
  (interactive)
  (lsp--remove-overlays 'lsp-highlight) ;; clear any previous highlights
  (setq lsp--have-document-highlights nil
        lsp--symbol-bounds-of-last-highlight-invocation nil)
  (let ((lsp-enable-symbol-highlighting t))
    (lsp--document-highlight)))

(defun lsp--document-highlight-callback (highlights)
  "Create a callback to process the reply of a
'textDocument/documentHighlight' message for the buffer BUF.
A reference is highlighted only if it is visible in a window."
  (lsp--remove-overlays 'lsp-highlight)

  (let* ((wins-visible-pos (-map (lambda (win)
                                   (cons (1- (line-number-at-pos (window-start win) t))
                                         (1+ (line-number-at-pos (window-end win) t))))
                                 (get-buffer-window-list nil nil 'visible))))
    (setq lsp--have-document-highlights t)
    (-map
     (-lambda ((&DocumentHighlight :range (&Range :start (start &as &Position :line start-line)
                                                  :end (end &as &Position :line end-line))
                                   :kind?))
       (-map
        (-lambda ((start-window . end-window))
          ;; Make the overlay only if the reference is visible
          (let ((start-point (lsp--position-to-point start))
                (end-point (lsp--position-to-point end)))
            (when (and (> (1+ start-line) start-window)
                       (< (1+ end-line) end-window)
                       (not (and lsp-symbol-highlighting-skip-current
                                 (<= start-point (point) end-point))))
              (-doto (make-overlay start-point end-point)
                (overlay-put 'face (cdr (assq (or kind? 1) lsp--highlight-kind-face)))
                (overlay-put 'lsp-highlight t)))))
        wins-visible-pos))
     highlights)))

(defcustom lsp-symbol-kinds
  '((1 . "File")
    (2 . "Module")
    (3 . "Namespace")
    (4 . "Package")
    (5 . "Class")
    (6 . "Method")
    (7 . "Property")
    (8 . "Field")
    (9 . "Constructor")
    (10 . "Enum")
    (11 . "Interface")
    (12 . "Function")
    (13 . "Variable")
    (14 . "Constant")
    (15 . "String")
    (16 . "Number")
    (17 . "Boolean")
    (18 . "Array")
    (19 . "Object")
    (20 . "Key")
    (21 . "Null")
    (22 . "Enum Member")
    (23 . "Struct")
    (24 . "Event")
    (25 . "Operator")
    (26 . "Type Parameter"))
  "Alist mapping SymbolKinds to human-readable strings.
Various Symbol objects in the LSP protocol have an integral type,
specifying what they are. This alist maps such type integrals to
readable representations of them. See
`https://microsoft.github.io/language-server-protocol/specifications/specification-current/',
namespace SymbolKind."
  :group 'lsp-mode
  :type '(alist :key-type integer :value-type string))
(defalias 'lsp--symbol-kind 'lsp-symbol-kinds)

(lsp-defun lsp--symbol-information-to-xref
  ((&SymbolInformation :kind :name
                       :location (&Location :uri :range (&Range :start
                                                                (&Position :line :character)))))
  "Return a `xref-item' from SYMBOL information."
  (xref-make (format "[%s] %s" (alist-get kind lsp-symbol-kinds) name)
             (xref-make-file-location (lsp--uri-to-path uri)
                                      line
                                      character)))

(defun lsp--get-document-symbols ()
  "Get document symbols.

If the buffer has not been modified since symbols were last
retrieved, simply return the latest result.

Else, if the request was initiated by Imenu updating its menu-bar
entry, perform it asynchronously; i.e., give Imenu the latest
result and then force a refresh when a new one is available.

Else (e.g., due to interactive use of `imenu' or `xref'),
perform the request synchronously."
  (if (= (buffer-chars-modified-tick) lsp--document-symbols-tick)
      lsp--document-symbols
    (let ((method "textDocument/documentSymbol")
          (params `(:textDocument ,(lsp--text-document-identifier)))
          (tick (buffer-chars-modified-tick)))
      (if (not lsp--document-symbols-request-async)
          (prog1
              (setq lsp--document-symbols (lsp-request method params))
            (setq lsp--document-symbols-tick tick))
        (lsp-request-async method params
                           (lambda (document-symbols)
                             (setq lsp--document-symbols document-symbols
                                   lsp--document-symbols-tick tick)
                             (lsp--imenu-refresh))
                           :mode 'alive)
        lsp--document-symbols))))

(advice-add 'imenu-update-menubar :around
            (lambda (oldfun &rest r)
              (let ((lsp--document-symbols-request-async t))
                (apply oldfun r))))

(defun lsp--document-symbols->document-symbols-hierarchy (document-symbols current-position)
  "Convert DOCUMENT-SYMBOLS to symbols hierarchy on CURRENT-POSITION."
  (-let (((symbol &as &DocumentSymbol? :children?)
          (seq-find (-lambda ((&DocumentSymbol :range))
                      (lsp-point-in-range? current-position range))
                    document-symbols)))
    (if children?
        (cons symbol (lsp--document-symbols->document-symbols-hierarchy children? current-position))
      (when symbol
        (list symbol)))))

(lsp-defun lsp--symbol-information->document-symbol ((&SymbolInformation :name :kind :location :container-name? :deprecated?))
  "Convert a SymbolInformation to a DocumentInformation"
  (lsp-make-document-symbol :name name
                            :kind kind
                            :range (lsp:location-range location)
                            :children? nil
                            :deprecated? deprecated?
                            :selection-range (lsp:location-range location)
                            :detail? container-name?))

(defun lsp--symbols-informations->document-symbols-hierarchy (symbols-informations current-position)
  "Convert SYMBOLS-INFORMATIONS to symbols hierarchy on CURRENT-POSITION."
  (--> symbols-informations
    (-keep (-lambda ((symbol &as &SymbolInformation :location (&Location :range)))
             (when (lsp-point-in-range? current-position range)
               (lsp--symbol-information->document-symbol symbol)))
           it)
    (sort it (-lambda ((&DocumentSymbol :range (&Range :start a-start-position :end a-end-position))
                       (&DocumentSymbol :range (&Range :start b-start-position :end b-end-position)))
               (and (lsp--position-compare b-start-position a-start-position)
                    (lsp--position-compare a-end-position b-end-position))))))

(defun lsp--symbols->document-symbols-hierarchy (symbols)
  "Convert SYMBOLS to symbols-hierarchy."
  (when-let ((first-symbol (lsp-seq-first symbols)))
    (let ((cur-position (lsp-make-position :line (plist-get (lsp--cur-position) :line)
                                           :character (plist-get (lsp--cur-position) :character))))
      (if (lsp-symbol-information? first-symbol)
          (lsp--symbols-informations->document-symbols-hierarchy symbols cur-position)
        (lsp--document-symbols->document-symbols-hierarchy symbols cur-position)))))

(defun lsp--xref-backend () 'xref-lsp)

(cl-defmethod xref-backend-identifier-at-point ((_backend (eql xref-lsp)))
  (propertize (or (thing-at-point 'symbol) "")
              'identifier-at-point t))

(defun lsp--xref-elements-index (symbols path)
  (-mapcat
   (-lambda (sym)
     (pcase-exhaustive sym
       ((DocumentSymbol :name :children? :selection-range (Range :start))
        (cons (cons (concat path name)
                    (lsp--position-to-point start))
              (lsp--xref-elements-index children? (concat path name " / "))))
       ((SymbolInformation :name :location (Location :range (Range :start)))
        (list (cons (concat path name)
                    (lsp--position-to-point start))))))
   symbols))

(defvar-local lsp--symbols-cache nil)

(cl-defmethod xref-backend-identifier-completion-table ((_backend (eql xref-lsp)))
  (if (lsp--find-workspaces-for "textDocument/documentSymbol")
      (progn
        (setq lsp--symbols-cache (lsp--xref-elements-index
                                  (lsp--get-document-symbols) nil))
        lsp--symbols-cache)
    (list (propertize (or (thing-at-point 'symbol) "")
                      'identifier-at-point t))))

(cl-defmethod xref-backend-definitions ((_backend (eql xref-lsp)) identifier)
  (save-excursion
    (unless (get-text-property 0 'identifier-at-point identifier)
      (goto-char (cl-rest (or (assoc identifier lsp--symbols-cache)
                              (user-error "Unable to find symbol %s in current document" identifier)))))
    (lsp--locations-to-xref-items (lsp-request "textDocument/definition"
                                               (lsp--text-document-position-params)))))

(cl-defmethod xref-backend-references ((_backend (eql xref-lsp)) identifier)
  (save-excursion
    (unless (get-text-property 0 'identifier-at-point identifier)
      (goto-char (cl-rest (or (assoc identifier lsp--symbols-cache)
                              (user-error "Unable to find symbol %s" identifier)))))
    (lsp--locations-to-xref-items (lsp-request "textDocument/references"
                                               (lsp--make-reference-params)))))

(cl-defmethod xref-backend-apropos ((_backend (eql xref-lsp)) pattern)
  (seq-map #'lsp--symbol-information-to-xref
           (lsp-request "workspace/symbol" `(:query ,pattern))))

(defcustom lsp-rename-use-prepare t
  "Whether `lsp-rename' should do a prepareRename first.
For some language servers, textDocument/prepareRename might be
too slow, in which case this variable may be set to nil.
`lsp-rename' will then use `thing-at-point' `symbol' to determine
the symbol to rename at point."
  :group 'lsp-mode
  :type 'boolean)

(defun lsp--get-symbol-to-rename ()
  "Get a symbol to rename and placeholder at point.
Returns a cons ((START . END) . PLACEHOLDER?), and nil if
renaming is generally supported but cannot be done at point.
START and END are the bounds of the identifiers being renamed,
while PLACEHOLDER?, is either nil or a string suggested by the
language server as the initial input of a new-name prompt."
  (unless (lsp-feature? "textDocument/rename")
    (error "The connected server(s) doesn't support renaming"))
  (if (and lsp-rename-use-prepare (lsp-feature? "textDocument/prepareRename"))
      (when-let ((response
                  (lsp-request "textDocument/prepareRename"
                               (lsp--text-document-position-params))))
        (let* ((bounds (lsp--range-to-region
                        (if (lsp-range? response)
                            response
                          (lsp:prepare-rename-result-range response))))
               (placeholder
                (and (not (lsp-range? response))
                     (lsp:prepare-rename-result-placeholder response))))
          (cons bounds placeholder)))
    (when-let ((bounds (bounds-of-thing-at-point 'symbol)))
      (cons bounds nil))))

(defface lsp-face-rename '((t :underline t))
  "Face used to highlight the identifier being renamed.
Renaming can be done using `lsp-rename'."
  :group 'lsp-mode)

(defface lsp-rename-placeholder-face '((t :inherit font-lock-variable-name-face))
  "Face used to display the rename placeholder in.
When calling `lsp-rename' interactively, this will be the face of
the new name."
  :group 'lsp-mode)

(defvar lsp-rename-history '()
  "History for `lsp--read-rename'.")

(defun lsp--read-rename (at-point)
  "Read a new name for a `lsp-rename' at `point' from the user.
AT-POINT shall be a structure as returned by
`lsp--get-symbol-to-rename'.

Returns a string, which should be the new name for the identifier
at point. If renaming cannot be done at point (as determined from
AT-POINT), throw a `user-error'.

This function is for use in `lsp-rename' only, and shall not be
relied upon."
  (unless at-point
    (user-error "`lsp-rename' is invalid here"))
  (-let* ((((start . end) . placeholder?) at-point)
          ;; Do the `buffer-substring' first to not include `lsp-face-rename'
          (rename-me (buffer-substring start end))
          (placeholder (or placeholder? rename-me))
          (placeholder (propertize placeholder 'face 'lsp-rename-placeholder-face))

          overlay)
    ;; We need unwind protect, as the user might cancel here, causing the
    ;; overlay to linger.
    (unwind-protect
        (progn
          (setq overlay (make-overlay start end))
          (overlay-put overlay 'face 'lsp-face-rename)

          (read-string (format "Rename %s to: " rename-me) placeholder
                       'lsp-rename-history))
      (and overlay (delete-overlay overlay)))))

(defun lsp-rename (newname)
  "Rename the symbol (and all references to it) under point to NEWNAME."
  (interactive (list (lsp--read-rename (lsp--get-symbol-to-rename))))
  (when-let ((edits (lsp-request "textDocument/rename"
                                 `( :textDocument ,(lsp--text-document-identifier)
                                    :position ,(lsp--cur-position)
                                    :newName ,newname))))
    (lsp--apply-workspace-edit edits 'rename)))

(defun lsp--on-rename-file (old-func old-name new-name &optional ok-if-already-exists?)
  "Advice around function `rename-file'.
Applies OLD-FUNC with OLD-NAME, NEW-NAME and OK-IF-ALREADY-EXISTS?.

This advice sends workspace/willRenameFiles before renaming file
to check if server wants to apply any workspaceEdits after renamed."
  (if (and lsp-apply-edits-after-file-operations
           (lsp--send-will-rename-files-p old-name))
      (let ((params (lsp-make-rename-files-params
                     :files (vector (lsp-make-file-rename
                                     :oldUri (lsp--path-to-uri old-name)
                                     :newUri (lsp--path-to-uri new-name))))))
        (when-let ((edits (lsp-request "workspace/willRenameFiles" params)))
          (lsp--apply-workspace-edit edits 'rename-file)
          (funcall old-func old-name new-name ok-if-already-exists?)
          (when (lsp--send-did-rename-files-p)
            (lsp-notify "workspace/didRenameFiles" params))))
    (funcall old-func old-name new-name ok-if-already-exists?)))

(advice-add 'rename-file :around #'lsp--on-rename-file)

(defun lsp-show-xrefs (xrefs display-action references?)
  (unless (region-active-p) (push-mark nil t))
  (if (boundp 'xref-show-definitions-function)
      (with-no-warnings
        (xref-push-marker-stack)
        (funcall (if references? xref-show-xrefs-function xref-show-definitions-function)
                 (-const xrefs)
                 `((window . ,(selected-window))
                   (display-action . ,display-action))))
    (xref--show-xrefs xrefs display-action)))

(cl-defmethod seq-empty-p ((ht hash-table))
  "Function `seq-empty-p' for hash-table."
  (hash-table-empty-p ht))

(cl-defun lsp-find-locations (method &optional extra &key display-action references?)
  "Send request named METHOD and get cross references of the symbol under point.
EXTRA is a plist of extra parameters.
REFERENCES? t when METHOD returns references."
  (let ((loc (lsp-request method
                          (append (lsp--text-document-position-params) extra))))
    (if (seq-empty-p loc)
        (lsp--error "Not found for: %s" (or (thing-at-point 'symbol t) ""))
      (lsp-show-xrefs (lsp--locations-to-xref-items loc) display-action references?))))

(cl-defun lsp-find-declaration (&key display-action)
  "Find declarations of the symbol under point."
  (interactive)
  (lsp-find-locations "textDocument/declaration" nil :display-action display-action))

(cl-defun lsp-find-definition (&key display-action)
  "Find definitions of the symbol under point."
  (interactive)
  (lsp-find-locations "textDocument/definition" nil :display-action display-action))

(defun lsp-find-definition-mouse (click)
  "Click to start `lsp-find-definition' at clicked point."
  (interactive "e")
  (let* ((ec (event-start click))
         (p1 (posn-point ec))
         (w1 (posn-window ec)))
    (select-window w1)
    (goto-char p1)
    (lsp-find-definition)))

(cl-defun lsp-find-implementation (&key display-action)
  "Find implementations of the symbol under point."
  (interactive)
  (lsp-find-locations "textDocument/implementation"
                      nil
                      :display-action display-action
                      :references? t))

(cl-defun lsp-find-references (&optional include-declaration &key display-action)
  "Find references of the symbol under point."
  (interactive "P")
  (lsp-find-locations "textDocument/references"
                      (list :context `(:includeDeclaration ,(lsp-json-bool include-declaration)))
                      :display-action display-action
                      :references? t))

(cl-defun lsp-find-type-definition (&key display-action)
  "Find type definitions of the symbol under point."
  (interactive)
  (lsp-find-locations "textDocument/typeDefinition" nil :display-action display-action))

(defalias 'lsp-find-custom #'lsp-find-locations)
(defalias 'lsp-goto-implementation #'lsp-find-implementation)
(defalias 'lsp-goto-type-definition #'lsp-find-type-definition)

(with-eval-after-load 'evil
  (evil-set-command-property 'lsp-find-definition :jump t)
  (evil-set-command-property 'lsp-find-implementation :jump t)
  (evil-set-command-property 'lsp-find-references :jump t)
  (evil-set-command-property 'lsp-find-type-definition :jump t))

(defun lsp--find-workspaces-for (msg-or-method)
  "Find all workspaces in the current project that can handle MSG."
  (let ((method (if (stringp msg-or-method)
                    msg-or-method
                  (plist-get msg-or-method :method))))
    (-if-let (reqs (cdr (assoc method lsp-method-requirements)))
        (-let (((&plist :capability :check-command) reqs))
          (--filter
           (with-lsp-workspace it
             (or
              (when check-command (funcall check-command it))
              (when capability (lsp--capability capability))
              (lsp--registered-capability method)
              (and (not capability) (not check-command))))
           (lsp-workspaces)))
      (lsp-workspaces))))

(defun lsp-can-execute-command? (command-name)
  "Returns non-nil if current language server(s) can execute COMMAND-NAME.
The command is executed via `workspace/executeCommand'"
  (cl-position
   command-name
   (lsp:execute-command-options-commands
    (lsp:server-capabilities-execute-command-provider?
     (lsp--server-capabilities)))
   :test #'equal))

(defalias 'lsp-feature? 'lsp--find-workspaces-for)

(cl-defmethod lsp-execute-command (_server _command _arguments)
  "Dispatch COMMAND execution."
  (signal 'cl-no-applicable-method nil))

(defun lsp-workspace-command-execute (command &optional args)
  "Execute workspace COMMAND with ARGS."
  (condition-case-unless-debug err
      (let ((params (if args
                        (list :command command :arguments args)
                      (list :command command))))
        (lsp-request "workspace/executeCommand" params))
    (error
     (error "`workspace/executeCommand' with `%s' failed.\n\n%S"
            command err))))

(defun lsp-send-execute-command (command &optional args)
  "Create and send a 'workspace/executeCommand' message having command COMMAND and optional ARGS."
  (lsp-workspace-command-execute command args))

(defalias 'lsp-point-to-position #'lsp--point-to-position)
(defalias 'lsp-text-document-identifier #'lsp--text-document-identifier)
(defalias 'lsp--send-execute-command #'lsp-send-execute-command)
(defalias 'lsp-on-open #'lsp--text-document-did-open)
(defalias 'lsp-on-save #'lsp--text-document-did-save)

(defun lsp--set-configuration (settings)
  "Set the SETTINGS for the lsp server."
  (lsp-notify "workspace/didChangeConfiguration" `(:settings ,settings)))

(defun lsp-current-buffer ()
  (or lsp--virtual-buffer
      (current-buffer)))

(defun lsp-buffer-live-p (buffer-id)
  (if-let ((buffer-live (plist-get buffer-id :buffer-live?)))
      (funcall buffer-live buffer-id)
    (buffer-live-p buffer-id)))

(defun lsp--on-set-visited-file-name (old-func &rest args)
  "Advice around function `set-visited-file-name'.

This advice sends textDocument/didClose for the old file and
textDocument/didOpen for the new file."
  (when lsp--cur-workspace
    (lsp--text-document-did-close t))
  (prog1 (apply old-func args)
    (when lsp--cur-workspace
      (lsp--text-document-did-open))))

(advice-add 'set-visited-file-name :around #'lsp--on-set-visited-file-name)

(defvar lsp--flushing-delayed-changes nil)

(defun lsp--send-no-wait (message proc)
  "Send MESSAGE to PROC without waiting for further output."

  (unless lsp--flushing-delayed-changes
    (let ((lsp--flushing-delayed-changes t))
      (lsp--flush-delayed-changes)))

  (condition-case err
      (process-send-string proc message)
    ('error (lsp--error "Sending to process failed with the following error: %s"
                        (error-message-string err)))))

(define-error 'lsp-parse-error
  "Error parsing message from language server" 'lsp-error)
(define-error 'lsp-unknown-message-type
  "Unknown message type" '(lsp-error lsp-parse-error))
(define-error 'lsp-unknown-json-rpc-version
  "Unknown JSON-RPC protocol version" '(lsp-error lsp-parse-error))
(define-error 'lsp-no-content-length
  "Content-Length header missing in message" '(lsp-error lsp-parse-error))
(define-error 'lsp-invalid-header-name
  "Invalid header name" '(lsp-error lsp-parse-error))

;;  id  method
;;   x    x     request
;;   x    .     response
;;   .    x     notification
(defun lsp--get-message-type (json-data)
  "Get the message type from JSON-DATA."
  (if (lsp:json-message-id? json-data)
      (if (lsp:json-message-error? json-data)
          'response-error
        (if (lsp:json-message-method? json-data)
            'request
          'response))
    'notification))

(defconst lsp--default-notification-handlers
  (ht ("window/showMessage" #'lsp--window-show-message)
      ("window/logMessage" #'lsp--window-log-message)
      ("window/showInputBox" #'lsp--window-show-input-box)
      ("window/showQuickPick" #'lsp--window-show-quick-pick)
      ("textDocument/publishDiagnostics" #'lsp--on-diagnostics)
      ("textDocument/diagnosticsEnd" #'ignore)
      ("textDocument/diagnosticsBegin" #'ignore)
      ("telemetry/event" #'ignore)
      ("$/progress" (lambda (workspace params)
                      (funcall lsp-progress-function workspace params)))))

(lsp-defun lsp--on-notification (workspace (&JSONNotification :params :method))
  "Call the appropriate handler for NOTIFICATION."
  (-let ((client (lsp--workspace-client workspace)))
    (when (lsp--log-io-p method)
      (lsp--log-entry-new (lsp--make-log-entry method nil params 'incoming-notif)
                          lsp--cur-workspace))
    (if-let ((handler (or (gethash method (lsp--client-notification-handlers client))
                          (gethash method lsp--default-notification-handlers))))
        (funcall handler workspace params)
      (when (and method (not (string-prefix-p "$" method)))
        (lsp-warn "Unknown notification: %s" method)))))

(lsp-defun lsp--build-workspace-configuration-response ((&ConfigurationParams :items))
  "Get section configuration.
PARAMS are the `workspace/configuration' request params"
  (->> items
       (-map (-lambda ((&ConfigurationItem :section?))
               (-let* ((path-parts (split-string section? "\\."))
                       (path-without-last (s-join "." (-slice path-parts 0 -1)))
                       (path-parts-len (length path-parts)))
                 (cond
                  ((<= path-parts-len 1)
                   (ht-get (lsp-configuration-section section?)
                           (car-safe path-parts)
                           (ht-create)))
                  ((> path-parts-len 1)
                   (when-let ((section (lsp-configuration-section path-without-last))
                              (keys path-parts))
                     (while (and keys section)
                       (setf section (ht-get section (pop keys))))
                     section))))))
       (apply #'vector)))

(defun lsp--send-request-response (workspace recv-time request response)
  "Send the RESPONSE for REQUEST in WORKSPACE and log if needed."
  (-let* (((&JSONResponse :params :method :id) request)
          (process (lsp--workspace-proc workspace))
          (response (lsp--make-response id response))
          (req-entry (and lsp-log-io
                          (lsp--make-log-entry method id params 'incoming-req)))
          (resp-entry (and lsp-log-io
                           (lsp--make-log-entry method id response 'outgoing-resp
                                                (/ (nth 2 (time-since recv-time)) 1000)))))
    ;; Send response to the server.
    (when (lsp--log-io-p method)
      (lsp--log-entry-new req-entry workspace)
      (lsp--log-entry-new resp-entry workspace))
    (lsp--send-no-wait (lsp--make-message response) process)))

(lsp-defun lsp--on-request (workspace (request &as &JSONRequest :params :method))
  "Call the appropriate handler for REQUEST, and send the return value to the server.
WORKSPACE is the active workspace."
  (-let* ((recv-time (current-time))
          (client (lsp--workspace-client workspace))
          (buffers (lsp--workspace-buffers workspace))
          handler
          (response (cond
                     ((setq handler (gethash method (lsp--client-request-handlers client) nil))
                      (funcall handler workspace params))
                     ((setq handler (gethash method (lsp--client-async-request-handlers client) nil))
                      (funcall handler workspace params
                               (-partial #'lsp--send-request-response
                                         workspace recv-time request))
                      'delay-response)
                     ((equal method "client/registerCapability")
                      (mapc #'lsp--server-register-capability
                            (lsp:registration-params-registrations params))
                      (mapc (lambda (buf)
                              (when (lsp-buffer-live-p buf)
                                (lsp-with-current-buffer buf
                                  (lsp-unconfig-buffer)
                                  (lsp-configure-buffer))))
                            buffers)
                      nil)
                     ((equal method "window/showMessageRequest")
                      (let ((choice (lsp--window-log-message-request params)))
                        `(:title ,choice)))
                     ((equal method "window/showDocument")
                      (let ((success? (lsp--window-show-document params)))
                        (lsp-make-show-document-result :success (or success?
                                                                    :json-false))))
                     ((equal method "client/unregisterCapability")
                      (mapc #'lsp--server-unregister-capability
                            (lsp:unregistration-params-unregisterations params))
                      (mapc (lambda (buf)
                              (when (lsp-buffer-live-p buf)
                                (lsp-with-current-buffer buf
                                  (lsp-unconfig-buffer)
                                  (lsp-configure-buffer))))
                            buffers)
                      nil)
                     ((equal method "workspace/applyEdit")
                      (list :applied (condition-case err
                                         (prog1 t
                                           (lsp--apply-workspace-edit (lsp:apply-workspace-edit-params-edit params) 'server-requested))
                                       (error
                                        (lsp--error "Failed to apply edits with message %s"
                                                    (error-message-string err))
                                        :json-false))))
                     ((equal method "workspace/configuration")
                      (with-lsp-workspace workspace
                        (if-let ((buf (car buffers)))
                            (lsp-with-current-buffer buf
                              (lsp--build-workspace-configuration-response params))
                          (lsp--with-workspace-temp-buffer (lsp--workspace-root workspace)
                            (lsp--build-workspace-configuration-response params)))))
                     ((equal method "workspace/workspaceFolders")
                      (let ((folders (or (-> workspace
                                             (lsp--workspace-client)
                                             (lsp--client-server-id)
                                             (gethash (lsp-session-server-id->folders (lsp-session))))
                                         (lsp-session-folders (lsp-session)))))
                        (->> folders
                             (-distinct)
                             (-map (lambda (folder)
                                     (list :uri (lsp--path-to-uri folder))))
                             (apply #'vector))))
                     ((equal method "window/workDoneProgress/create")
                      nil ;; no specific reply, no processing required
                      )
                     ((equal method "workspace/semanticTokens/refresh")
                      (when (and lsp-semantic-tokens-enable
                                 (fboundp 'lsp--semantic-tokens-on-refresh))
                        (lsp--semantic-tokens-on-refresh workspace))
                      nil)
                     ((equal method "workspace/codeLens/refresh")
                      (when (and lsp-lens-enable
                                 (fboundp 'lsp--lens-on-refresh))
                        (lsp--lens-on-refresh workspace))
                      nil)
                     (t (lsp-warn "Unknown request method: %s" method) nil))))
    ;; Send response to the server.
    (unless (eq response 'delay-response)
      (lsp--send-request-response workspace recv-time request response))))

(lsp-defun lsp--error-string ((&JSONError :message :code))
  "Format ERR as a user friendly string."
  (format "Error from the Language Server: %s (%s)"
          message
          (or (car (alist-get code lsp--errors)) "Unknown error")))

(defun lsp--get-body-length (headers)
  (let ((content-length (cdr (assoc "Content-Length" headers))))
    (if content-length
        (string-to-number content-length)

      ;; This usually means either the server or our parser is
      ;; screwed up with a previous Content-Length
      (error "No Content-Length header"))))

(defun lsp--parse-header (s)
  "Parse string S as a LSP (KEY . VAL) header."
  (let ((pos (string-match "\:" s))
        key val)
    (unless pos
      (signal 'lsp-invalid-header-name (list s)))
    (setq key (substring s 0 pos)
          val (s-trim-left (substring s (+ 1 pos))))
    (when (equal key "Content-Length")
      (cl-assert (cl-loop for c across val
                          when (or (> c ?9) (< c ?0)) return nil
                          finally return t)
                 nil (format "Invalid Content-Length value: %s" val)))
    (cons key val)))

(defmacro lsp--read-json (str)
  "Read json string STR."
  (if (progn
        (require 'json)
        (fboundp 'json-parse-string))
      `(json-parse-string ,str
                          :object-type (if lsp-use-plists
                                           'plist
                                         'hash-table)
                          :null-object nil
                          :false-object nil)
    `(let ((json-array-type 'vector)
           (json-object-type (if lsp-use-plists
                                 'plist
                               'hash-table))
           (json-false nil))
       (json-read-from-string ,str))))

(defmacro lsp-json-read-buffer ()
  "Read json from the current buffer."
  (if (progn
        (require 'json)
        (fboundp 'json-parse-string))
      `(json-parse-buffer :object-type (if lsp-use-plists
                                           'plist
                                         'hash-table)
                          :null-object nil
                          :false-object nil)
    `(let ((json-array-type 'vector)
           (json-object-type (if lsp-use-plists
                                 'plist
                               'hash-table))
           (json-false nil))
       (json-read))))

(defun lsp--read-json-file (file-path)
  "Read json file."
  (-> file-path
    (f-read-text)
    (lsp--read-json)))

(defun lsp--parser-on-message (json-data workspace)
  "Called when the parser P read a complete MSG from the server."
  (with-demoted-errors "Error processing message %S."
    (with-lsp-workspace workspace
      (let* ((client (lsp--workspace-client workspace))
             (id (--when-let (lsp:json-response-id json-data)
                   (if (stringp it) (string-to-number it) it)))
             (data (lsp:json-response-result json-data)))
        (pcase (lsp--get-message-type json-data)
          ('response
           (cl-assert id)
           (-let [(callback _ method _ before-send) (gethash id (lsp--client-response-handlers client))]
             (when (lsp--log-io-p method)
               (lsp--log-entry-new
                (lsp--make-log-entry method id data 'incoming-resp
                                     (/ (nth 2 (time-since before-send)) 1000))
                workspace))
             (when callback
               (remhash id (lsp--client-response-handlers client))
               (funcall callback (lsp:json-response-result json-data)))))
          ('response-error
           (cl-assert id)
           (-let [(_ callback method _ before-send) (gethash id (lsp--client-response-handlers client))]
             (when (lsp--log-io-p method)
               (lsp--log-entry-new
                (lsp--make-log-entry method id (lsp:json-response-error-error json-data)
                                     'incoming-resp (/ (nth 2 (time-since before-send)) 1000))
                workspace))
             (when callback
               (remhash id (lsp--client-response-handlers client))
               (funcall callback (lsp:json-response-error-error json-data)))))
          ('notification
           (lsp--on-notification workspace json-data))
          ('request (lsp--on-request workspace json-data)))))))

(defvar lsp-parsed-message nil
  "This will store the string representation of the json message.

In some cases like #1807 we lose information during json
deserialization.")

(defun lsp--create-filter-function (workspace)
  "Make filter for the workspace."
  (let ((body-received 0)
        leftovers body-length body chunk)
    (lambda (_proc input)
      (setf chunk (if (s-blank? leftovers)
                      input
                    (concat leftovers input)))

      (let (messages)
        (while (not (s-blank? chunk))
          (if (not body-length)
              ;; Read headers
              (if-let ((body-sep-pos (string-match-p "\r\n\r\n" chunk)))
                  ;; We've got all the headers, handle them all at once:
                  (setf body-length (lsp--get-body-length
                                     (mapcar #'lsp--parse-header
                                             (split-string
                                              (substring-no-properties chunk
                                                                       (or (string-match-p "Content-Length" chunk)
                                                                           (error "Unable to find Content-Length header."))
                                                                       body-sep-pos)
                                              "\r\n")))
                        body-received 0
                        leftovers nil
                        chunk (substring-no-properties chunk (+ body-sep-pos 4)))

                ;; Haven't found the end of the headers yet. Save everything
                ;; for when the next chunk arrives and await further input.
                (setf leftovers chunk
                      chunk nil))
            (let* ((chunk-length (string-bytes chunk))
                   (left-to-receive (- body-length body-received))
                   (this-body (if (< left-to-receive chunk-length)
                                  (prog1 (substring-no-properties chunk 0 left-to-receive)
                                    (setf chunk (substring-no-properties chunk left-to-receive)))
                                (prog1 chunk
                                  (setf chunk nil))))
                   (body-bytes (string-bytes this-body)))
              (push this-body body)
              (setf body-received (+ body-received body-bytes))
              (when (>= chunk-length left-to-receive)
                (condition-case err
                    (with-temp-buffer
                      (apply #'insert
                             (nreverse
                              (prog1 body
                                (setf leftovers nil
                                      body-length nil
                                      body-received nil
                                      body nil))))
                      (decode-coding-region (point-min)
                                            (point-max)
                                            'utf-8)
                      (goto-char (point-min))
                      (push (lsp-json-read-buffer) messages))

                  (error
                   (lsp-warn "Failed to parse the following chunk:\n'''\n%s\n'''\nwith message %s"
                             (concat leftovers input)
                             err)))))))
        (mapc (lambda (msg)
                (lsp--parser-on-message msg workspace))
              (nreverse messages))))))

(defvar-local lsp--line-col-to-point-hash-table nil
  "Hash table with keys (line . col) and values that are either point positions
or markers.")

(defcustom lsp-imenu-detailed-outline t
  "Whether `lsp-imenu' should include signatures.
This will be ignored if the server doesn't provide the necessary
information, for example if it doesn't support DocumentSymbols."
  :group 'lsp-imenu
  :type 'boolean)

(defface lsp-details-face '((t :height 0.8 :inherit shadow))
  "Used to display additional information troughout `lsp'.
Things like line numbers, signatures, ... are considered
additional information. Often, additional faces are defined that
inherit from this face by default, like `lsp-signature-face', and
they may be customized for finer control."
  :group 'lsp-mode)

(defface lsp-signature-face '((t :inherit lsp-details-face))
  "Used to display signatures in `imenu', ...."
  :group 'lsp-mode)

(lsp-defun lsp-render-symbol ((&DocumentSymbol :name :detail? :deprecated?)
                              show-detail?)
  "Render INPUT0, an `&DocumentSymbol', to a string.
If SHOW-DETAIL? is set, make use of its `:detail?' field (often
the signature)."
  (let ((detail (and show-detail? (s-present? detail?)
                     (propertize (concat " " (s-trim-left detail?))
                                 'face 'lsp-signature-face)))
        (name (if deprecated?
                  (propertize name 'face 'lsp-face-semhl-deprecated) name)))
    (concat name detail)))

(lsp-defun lsp-render-symbol-information ((&SymbolInformation :name :deprecated? :container-name?)
                                          separator)
  "Render a piece of SymbolInformation.
Handle :deprecated?. If SEPARATOR is non-nil, the
symbol's (optional) parent, SEPARATOR and the symbol itself are
concatenated."
  (when (and separator container-name? (not (string-empty-p container-name?)))
    (setq name (concat name separator container-name?)))
  (if deprecated? (propertize name 'face 'lsp-face-semhl-deprecated) name))

(defun lsp--symbol-to-imenu-elem (sym)
  "Convert SYM to imenu element.

SYM is a SymbolInformation message.

Return a cons cell (full-name . start-point)."
  (let ((start-point (ht-get lsp--line-col-to-point-hash-table
                             (lsp--get-line-and-col sym))))
    (cons (lsp-render-symbol-information
           sym (and lsp-imenu-show-container-name
                    lsp-imenu-container-name-separator))
          start-point)))

(lsp-defun lsp--symbol-to-hierarchical-imenu-elem ((sym &as &DocumentSymbol :children?))
  "Convert SYM to hierarchical imenu elements.

SYM is a DocumentSymbol message.

Return cons cell (\"symbol-name (symbol-kind)\" . start-point) if
SYM doesn't have any children. Otherwise return a cons cell with
an alist

  (\"symbol-name\" . ((\"(symbol-kind)\" . start-point)
                    cons-cells-from-children))"
  (let ((filtered-children (lsp--imenu-filter-symbols children?))
        (signature (lsp-render-symbol sym lsp-imenu-detailed-outline)))
    (if (seq-empty-p filtered-children)
        (cons signature
              (ht-get lsp--line-col-to-point-hash-table
                      (lsp--get-line-and-col sym)))
      (cons signature
            (lsp--imenu-create-hierarchical-index filtered-children)))))

(lsp-defun lsp--symbol-ignore ((&SymbolInformation :kind))
  "Determine if SYM is for the current document and is to be shown."
  ;; It's a SymbolInformation or DocumentSymbol, which is always in the
  ;; current buffer file.
  (and lsp-imenu-index-symbol-kinds
       (numberp kind)
       (let ((clamped-kind (if (< 0 kind (length lsp/symbol-kind-lookup))
                               kind
                             0)))
         (not (memql (aref lsp/symbol-kind-lookup clamped-kind)
                     lsp-imenu-index-symbol-kinds)))))

(lsp-defun lsp--get-symbol-type ((&SymbolInformation :kind))
  "The string name of the kind of SYM."
  (alist-get kind lsp-symbol-kinds "Other"))

(defun lsp--get-line-and-col (sym)
  "Obtain the line and column corresponding to SYM."
  (-let* ((location (lsp:symbol-information-location sym))
          (name-range (or (and location (lsp:location-range location))
                          (lsp:document-symbol-selection-range sym)))
          ((&Range :start (&Position :line :character)) name-range))
    (cons line character)))

(defun lsp--collect-lines-and-cols (symbols)
  "Return a sorted list ((line . col) ...) of the locations of SYMBOLS."
  (let ((stack (mapcar 'identity symbols))
        line-col-list)
    (while stack
      (let ((sym (pop stack)))
        (push (lsp--get-line-and-col sym) line-col-list)
        (unless (seq-empty-p (lsp:document-symbol-children? sym))
          (setf stack (nconc (lsp--imenu-filter-symbols (lsp:document-symbol-children? sym)) stack)))))
    (-sort #'lsp--line-col-comparator line-col-list)))

(defun lsp--convert-line-col-to-points-batch (line-col-list)
  "Convert a sorted list of positions from line-column
representation to point representation."
  (let ((line-col-to-point-map (ht-create))
        (inhibit-field-text-motion t)
        (curr-line 0))
    (lsp-save-restriction-and-excursion
      (goto-char (point-min))
      (cl-loop for (line . col) in line-col-list do
               (forward-line (- line curr-line))
               (setq curr-line line)
               (let ((line-end (line-end-position)))
                 (if (or (not col) (> col (- line-end (point))))
                     (goto-char line-end)
                   (forward-char col)))
               (ht-set! line-col-to-point-map (cons line col) (if imenu-use-markers
                                                                  (point-marker)
                                                                (point)))))
    line-col-to-point-map))

(cl-defun lsp--line-col-comparator ((l1 . c1) (l2 . c2))
  (or (< l1 l2)
      (and (= l1 l2)
           (cond ((and c1 c2)
                  (< c1 c2))
                 (c1 t)))))

(defun lsp-imenu-create-uncategorized-index (symbols)
  "Create imenu index from document SYMBOLS.
This function, unlike `lsp-imenu-create-categorized-index', does
not categorize by type, but instead returns an `imenu' index
corresponding to the symbol hierarchy returned by the server
directly."
  (let* ((lsp--line-col-to-point-hash-table (-> symbols
                                                lsp--collect-lines-and-cols
                                                lsp--convert-line-col-to-points-batch)))
    (if (lsp--imenu-hierarchical-p symbols)
        (lsp--imenu-create-hierarchical-index symbols)
      (lsp--imenu-create-non-hierarchical-index symbols))))

(defcustom lsp-imenu-symbol-kinds
  '((1 . "Files")
    (2 . "Modules")
    (3 . "Namespaces")
    (4 . "Packages")
    (5 . "Classes")
    (6 . "Methods")
    (7 . "Properties")
    (8 . "Fields")
    (9 . "Constructors")
    (10 . "Enums")
    (11 . "Interfaces")
    (12 . "Functions")
    (13 . "Variables")
    (14 . "Constants")
    (15 . "Strings")
    (16 . "Numbers")
    (17 . "Booleans")
    (18 . "Arrays")
    (19 . "Objects")
    (20 . "Keys")
    (21 . "Nulls")
    (22 . "Enum Members")
    (23 . "Structs")
    (24 . "Events")
    (25 . "Operators")
    (26 . "Type Parameters"))
  "`lsp-symbol-kinds', but only used by `imenu'.
A new variable is needed, as it is `imenu' convention to use
pluralized categories, which `lsp-symbol-kinds' doesn't. If the
non-pluralized names are preferred, this can be set to
`lsp-symbol-kinds'."
  :type '(alist :key-type integer :value-type string))

(defun lsp--imenu-kind->name (kind)
  (alist-get kind lsp-imenu-symbol-kinds "?"))

(defun lsp-imenu-create-top-level-categorized-index (symbols)
  "Create an `imenu' index categorizing SYMBOLS by type.
Only root symbols are categorized.

See `lsp-symbol-kinds' to customize the category naming. SYMBOLS
shall be a list of DocumentSymbols or SymbolInformation."
  (mapcan
   (-lambda ((type . symbols))
     (let ((cat (lsp--imenu-kind->name type))
           (symbols (lsp-imenu-create-uncategorized-index symbols)))
       ;; If there is no :kind (this is being defensive), or we couldn't look it
       ;; up, just display the symbols inline, without categories.
       (if cat (list (cons cat symbols)) symbols)))
   (sort (seq-group-by #'lsp:document-symbol-kind symbols)
         (-lambda ((kinda) (kindb)) (< kinda kindb)))))

(lsp-defun lsp--symbol->imenu ((sym &as &DocumentSymbol :selection-range (&RangeToPoint :start)))
  "Convert an `&DocumentSymbol' to an `imenu' entry."
  (cons (lsp-render-symbol sym lsp-imenu-detailed-outline) start))

(defun lsp--imenu-create-categorized-index-1 (symbols)
  "Returns an `imenu' index from SYMBOLS categorized by type.
The result looks like this: ((\"Variables\" . (...)))."
  (->>
   symbols
   (mapcan
    (-lambda ((sym &as &DocumentSymbol :kind :children?))
      (if (seq-empty-p children?)
          (list (list kind (lsp--symbol->imenu sym)))
        (let ((parent (lsp-render-symbol sym lsp-imenu-detailed-outline)))
          (cons
           (list kind (lsp--symbol->imenu sym))
           (mapcar (-lambda ((type .  imenu-items))
                     (list type (cons parent (mapcan #'cdr imenu-items))))
                   (-group-by #'car (lsp--imenu-create-categorized-index-1 children?))))))))
   (-group-by #'car)
   (mapcar
    (-lambda ((kind . syms))
      (cons kind (mapcan #'cdr syms))))))

(defun lsp--imenu-create-categorized-index (symbols)
  (let ((syms (lsp--imenu-create-categorized-index-1 symbols)))
    (dolist (sym syms)
      (setcar sym (lsp--imenu-kind->name (car sym))))
    syms))

(lsp-defun lsp--symbol-information->imenu ((sym &as &SymbolInformation :location (&Location :range (&RangeToPoint :start))))
  (cons (lsp-render-symbol-information sym nil) start))

(defun lsp--imenu-create-categorized-index-flat (symbols)
  "Create a kind-categorized index for SymbolInformation."
  (mapcar (-lambda ((kind . syms))
            (cons (lsp--imenu-kind->name kind)
                  (mapcan (-lambda ((parent . children))
                            (let ((children (mapcar #'lsp--symbol-information->imenu children)))
                              (if parent (list (cons parent children)) children)))
                          (-group-by #'lsp:symbol-information-container-name? syms))))
          (seq-group-by #'lsp:symbol-information-kind symbols)))

(defun lsp-imenu-create-categorized-index (symbols)
  (if (lsp--imenu-hierarchical-p symbols)
      (lsp--imenu-create-categorized-index symbols)
    (lsp--imenu-create-categorized-index-flat symbols)))

(defcustom lsp-imenu-index-function #'lsp-imenu-create-uncategorized-index
  "Function that should create an `imenu' index.
It will be called with a list of SymbolInformation or
DocumentSymbols, whose first level is already filtered. It shall
then return an appropriate `imenu' index (see
`imenu-create-index-function').

Note that this interface is not stable, and subject to change any
time."
  :group 'lsp-imenu
  :type '(radio
          (const :tag "Categorize by type"
                 lsp-imenu-create-categorized-index)
          (const :tag "Categorize root symbols by type"
                 lsp-imenu-create-top-level-categorized-index)
          (const :tag "Uncategorized, inline entries"
                 lsp-imenu-create-uncategorized-index)
          (function :tag "Custom function")))

(defun lsp--imenu-create-index ()
  "Create an `imenu' index based on the language server.
Respects `lsp-imenu-index-function'."
  (let ((symbols (lsp--imenu-filter-symbols (lsp--get-document-symbols))))
    (funcall lsp-imenu-index-function symbols)))

(defun lsp--imenu-filter-symbols (symbols)
  "Filter out unsupported symbols from SYMBOLS."
  (seq-remove #'lsp--symbol-ignore symbols))

(defun lsp--imenu-hierarchical-p (symbols)
  "Determine whether any element in SYMBOLS has children."
  (seq-some #'lsp-document-symbol? symbols))

(defun lsp--imenu-create-non-hierarchical-index (symbols)
  "Create imenu index for non-hierarchical SYMBOLS.

SYMBOLS are a list of DocumentSymbol messages.

Return a nested alist keyed by symbol names. e.g.

   ((\"SomeClass\" (\"(Class)\" . 10)
                 (\"someField (Field)\" . 20)
                 (\"someFunction (Function)\" . 25)
                 (\"SomeSubClass\" (\"(Class)\" . 30)
                                  (\"someSubField (Field)\" . 35))
    (\"someFunction (Function)\" . 40))"
  (seq-map (lambda (nested-alist)
             (cons (car nested-alist)
                   (seq-map #'lsp--symbol-to-imenu-elem (cdr nested-alist))))
           (seq-group-by #'lsp--get-symbol-type symbols)))

(defun lsp--imenu-create-hierarchical-index (symbols)
  "Create imenu index for hierarchical SYMBOLS.

SYMBOLS are a list of DocumentSymbol messages.

Return a nested alist keyed by symbol names. e.g.

   ((\"SomeClass\" (\"(Class)\" . 10)
                 (\"someField (Field)\" . 20)
                 (\"someFunction (Function)\" . 25)
                 (\"SomeSubClass\" (\"(Class)\" . 30)
                                  (\"someSubField (Field)\" . 35))
    (\"someFunction (Function)\" . 40))"
  (seq-map #'lsp--symbol-to-hierarchical-imenu-elem
           (seq-sort #'lsp--imenu-symbol-lessp symbols)))

(defun lsp--imenu-symbol-lessp (sym1 sym2)
  (let* ((compare-results (mapcar (lambda (method)
                                    (funcall (alist-get method lsp--imenu-compare-function-alist)
                                             sym1 sym2))
                                  lsp-imenu-sort-methods))
         (result (seq-find (lambda (result)
                             (not (= result 0)))
                           compare-results
                           0)))
    (and (numberp result) (< result 0))))

(lsp-defun lsp--imenu-compare-kind ((&SymbolInformation :kind left)
                                    (&SymbolInformation :kind right))
  "Compare SYM1 and SYM2 by kind."
  (- left right))

(defun lsp--imenu-compare-line-col (sym1 sym2)
  (if (lsp--line-col-comparator
       (lsp--get-line-and-col sym1)
       (lsp--get-line-and-col sym2))
      -1
    1))

(lsp-defun lsp--imenu-compare-name ((&SymbolInformation :name name1)
                                    (&SymbolInformation :name name2))
  "Compare SYM1 and SYM2 by name."
  (let ((result (compare-strings name1 0 (length name1) name2 0 (length name2))))
    (if (numberp result) result 0)))

(defun lsp--imenu-refresh ()
  "Force Imenu to refresh itself."
  (imenu--menubar-select imenu--rescan-item))

(defun lsp-enable-imenu ()
  "Use lsp-imenu for the current buffer."
  (imenu--cleanup)
  (setq-local imenu-create-index-function #'lsp--imenu-create-index)
  (setq-local imenu-menubar-modified-tick -1)
  (setq-local imenu--index-alist nil)
  (when menu-bar-mode
    (lsp--imenu-refresh)))

(defun lsp-resolve-final-function (command)
  "Resolve final function COMMAND."
  (-let [command (if (functionp command) (funcall command) command)]
    (cl-etypecase command
      (list
       (cl-assert (seq-every-p (apply-partially #'stringp) command) nil
                  "Invalid command list")
       command)
      (string (list command)))))

(defun lsp-server-present? (final-command)
  "Check whether FINAL-COMMAND is present."
  ;; executable-find only gained support for remote checks after 27 release
  (or (and (cond
            ((not (file-remote-p default-directory))
             (executable-find (cl-first final-command)))
            ((version<= "27.0" emacs-version)
             (with-no-warnings (executable-find (cl-first final-command) (file-remote-p default-directory))))
            (t))
           (prog1 t
             (lsp-log "Command \"%s\" is present on the path." (s-join " " final-command))))
      (ignore (lsp-log "Command \"%s\" is not present on the path." (s-join " " final-command)))))

(defun lsp--value-to-string (value)
  "Convert VALUE to a string that can be set as value in an environment variable."
  (cond
   ((stringp value) value)
   ((booleanp value) (if value
                         "1"
                       "0"))
   ((and (sequencep value)
         (seq-every-p #'stringp value)) (string-join value ":"))
   (t (user-error "Only strings, booleans, and sequences of strings are supported as environment variables"))))

(defun lsp--compute-process-environment (environment-fn)
  "Append a list of KEY=VALUE from the alist ENVIRONMENT to `process-environment'.
Ignore non-boolean keys whose value is nil."
  (let ((environment (if environment-fn
                         (funcall environment-fn)
                       nil)))
    (-flatten (cons (cl-loop for (key . value) in environment
                             if (or (eval value)
                                    (eq (get value 'custom-type) 'boolean))
                             collect (concat key "=" (lsp--value-to-string
                                                      (eval value))))
                    process-environment))))

(defun lsp-stdio-connection (command &optional test-command)
  "Returns a connection property list using COMMAND.
COMMAND can be: A string, denoting the command to launch the
language server. A list of strings, denoting an executable with
its command line arguments. A function, that either returns a
string or a list of strings. In all cases, the launched language
server should send and receive messages on standard I/O.
TEST-COMMAND is a function with no arguments which returns
whether the command is present or not. When not specified
`lsp-mode' will check whether the first element of the list
returned by COMMAND is available via `executable-find'"
  (cl-check-type command (or string
                             function
                             (and list
                                  (satisfies (lambda (l)
                                               (seq-every-p (lambda (el)
                                                              (stringp el))
                                                            l))))))
  (list :connect (lambda (filter sentinel name environment-fn)
                   (let ((final-command (lsp-resolve-final-function command))
                         (process-name (generate-new-buffer-name name))
                         (process-environment
                          (lsp--compute-process-environment environment-fn)))
                     (let* ((stderr-buf (format "*%s::stderr*" process-name))
                            (proc (make-process
                                   :name process-name
                                   :connection-type 'pipe
                                   :buffer (format "*%s*" process-name)
                                   :coding 'no-conversion
                                   :command final-command
                                   :filter filter
                                   :sentinel sentinel
                                   :stderr stderr-buf
                                   :noquery t)))
                       (set-process-query-on-exit-flag proc nil)
                       (set-process-query-on-exit-flag (get-buffer-process stderr-buf) nil)
                       (with-current-buffer (get-buffer stderr-buf)
                         ;; Make the *NAME::stderr* buffer buffer-read-only, q to bury, etc.
                         (special-mode))
                       (cons proc proc))))
        :test? (or
                test-command
                (lambda () (-> command lsp-resolve-final-function lsp-server-present?)))))

(defun lsp--open-network-stream (host port name)
  "Open network stream to HOST:PORT.
  NAME will be passed to `open-network-stream'.
  RETRY-COUNT is the number of the retries.
  SLEEP-INTERVAL is the sleep interval between each retry."
  (let* ((retries 0)
         (sleep-interval 0.01)
         (number-of-retries (/ lsp-tcp-connection-timeout sleep-interval))
         connection)
    (while (and (not connection) (< retries number-of-retries))
      (condition-case err
          (setq connection (open-network-stream name nil host port
                                                :type 'plain
                                                :coding 'no-conversion))
        (file-error
         (let ((inhibit-message t))
           (lsp--warn "Failed to connect to %s:%s with error message %s"
                      host
                      port
                      (error-message-string err))
           (sleep-for sleep-interval)
           (cl-incf retries)))))
    (or connection (error "Port %s was never taken. Consider increasing `lsp-tcp-connection-timeout'." port))))

(defun lsp--find-available-port (host starting-port)
  "Find available port on HOST starting from STARTING-PORT."
  (let ((success nil)
        (port starting-port))
    (while (and (not success))
      (condition-case _err
          (progn
            (delete-process (open-network-stream "*connection-test*" nil host port :type 'plain))
            (cl-incf port))
        (file-error (setq success t))))
    port))

(defun lsp-tcp-connection (command-fn)
  "Returns a connection property list similar to `lsp-stdio-connection'.
COMMAND-FN can only be a function that takes a single argument, a
port number. It should return a command for launches a language server
process listening for TCP connections on the provided port."
  (cl-check-type command-fn function)
  (list
   :connect (lambda (filter sentinel name environment-fn)
              (let* ((host "localhost")
                     (port (lsp--find-available-port host (cl-incf lsp--tcp-port)))
                     (command (funcall command-fn port))
                     (final-command (if (consp command) command (list command)))
                     (_ (unless (executable-find (cl-first final-command))
                          (user-error (format "Couldn't find executable %s" (cl-first final-command)))))
                     (process-environment
                      (lsp--compute-process-environment environment-fn))
                     (proc (make-process :name name :connection-type 'pipe :coding 'no-conversion
                                         :command final-command :sentinel sentinel :stderr (format "*%s::stderr*" name) :noquery t))
                     (tcp-proc (lsp--open-network-stream host port (concat name "::tcp"))))

                ;; TODO: Same :noquery issue (see above)
                (set-process-query-on-exit-flag proc nil)
                (set-process-query-on-exit-flag tcp-proc nil)
                (set-process-filter tcp-proc filter)
                (cons tcp-proc proc)))
   :test? (lambda () (executable-find (cl-first (funcall command-fn 0))))))

(defalias 'lsp-tcp-server 'lsp-tcp-server-command)

(defun lsp-tcp-server-command (command-fn)
  "Create tcp server connection.
In this mode Emacs is TCP server and the language server connects
to it. COMMAND is function with one parameter(the port) and it
should return the command to start the LS server."
  (cl-check-type command-fn function)
  (list
   :connect (lambda (filter sentinel name environment-fn)
              (let* (tcp-client-connection
                     (tcp-server (make-network-process :name (format "*tcp-server-%s*" name)
                                                       :buffer (format "*tcp-server-%s*" name)
                                                       :family 'ipv4
                                                       :service lsp--tcp-server-port
                                                       :sentinel (lambda (proc _string)
                                                                   (lsp-log "Language server %s is connected." name)
                                                                   (setf tcp-client-connection proc))
                                                       :server 't))
                     (port (process-contact tcp-server :service))
                     (final-command (funcall command-fn port))
                     (process-environment
                      (lsp--compute-process-environment environment-fn))
                     (cmd-proc (make-process :name name
                                             :connection-type 'pipe
                                             :coding 'no-conversion
                                             :command final-command
                                             :stderr (format "*tcp-server-%s*::stderr" name)
                                             :noquery t)))
                (let ((retries 0))
                  ;; wait for the client to connect (we sit-for 500 ms, so have to double lsp--tcp-server-wait-seconds)
                  (while (and (not tcp-client-connection) (< retries (* 2 lsp--tcp-server-wait-seconds)))
                    (lsp--info "Waiting for connection for %s, retries: %s" name retries)
                    (sit-for 0.500)
                    (cl-incf retries)))

                (unless tcp-client-connection
                  (condition-case nil (delete-process tcp-server) (error))
                  (condition-case nil (delete-process cmd-proc) (error))
                  (error "Failed to create connection to %s on port %s" name port))
                (lsp--info "Successfully connected to %s" name)

                (set-process-query-on-exit-flag cmd-proc nil)
                (set-process-query-on-exit-flag tcp-client-connection nil)
                (set-process-query-on-exit-flag tcp-server nil)

                (set-process-filter tcp-client-connection filter)
                (set-process-sentinel tcp-client-connection sentinel)
                (cons tcp-client-connection cmd-proc)))
   :test? (lambda () (executable-find (cl-first (funcall command-fn 0))))))

(defun lsp-tramp-connection (local-command &optional generate-error-file-fn)
  "Create LSP stdio connection named name.
LOCAL-COMMAND is either list of strings, string or function which
returns the command to execute."
  (list :connect (lambda (filter sentinel name environment-fn)
                   (let* ((final-command (lsp-resolve-final-function local-command))
                          ;; wrap with stty to disable converting \r to \n
                          (process-name (generate-new-buffer-name name))
                          (wrapped-command (s-join
                                            " "
                                            (append '("stty" "raw" ";")
                                                    final-command
                                                    (list
                                                     (concat "2>"
                                                             (or (when generate-error-file-fn
                                                                   (funcall generate-error-file-fn name))
                                                                 (format "/tmp/%s-%s-stderr" name
                                                                         (cl-incf lsp--stderr-index))))))))
                          (process-environment
                           (lsp--compute-process-environment environment-fn)))
                     (let ((proc (start-file-process-shell-command process-name
                                                                   (format "*%s*" process-name)
                                                                   wrapped-command)))
                       (set-process-sentinel proc sentinel)
                       (set-process-filter proc filter)
                       (set-process-query-on-exit-flag proc nil)
                       (set-process-coding-system proc 'binary 'binary)
                       (cons proc proc))))
        :test? (lambda () (-> local-command lsp-resolve-final-function lsp-server-present?))))

(defun lsp--auto-configure ()
  "Autoconfigure `company', `flycheck', `lsp-ui', etc if they are installed."
  (when (functionp 'lsp-ui-mode)
    (lsp-ui-mode))

  (when lsp-headerline-breadcrumb-enable
    (add-hook 'lsp-configure-hook 'lsp-headerline-breadcrumb-mode))
  (when lsp-modeline-code-actions-enable
    (add-hook 'lsp-configure-hook 'lsp-modeline-code-actions-mode))
  (when lsp-modeline-diagnostics-enable
    (add-hook 'lsp-configure-hook 'lsp-modeline-diagnostics-mode))
  (when lsp-modeline-workspace-status-enable
    (add-hook 'lsp-configure-hook 'lsp-modeline-workspace-status-mode))
  (when lsp-lens-enable
    (add-hook 'lsp-configure-hook 'lsp-lens--enable))
  (when lsp-semantic-tokens-enable
    (add-hook 'lsp-configure-hook 'lsp-semantic-tokens--enable))

  ;; yas-snippet config
  (setq-local yas-inhibit-overlay-modification-protection t))

(defvar-local lsp--buffer-deferred nil
  "Whether buffer was loaded via `lsp-deferred'.")

(defun lsp--restart-if-needed (workspace)
  "Handler restart for WORKSPACE."
  (when (or (eq lsp-restart 'auto-restart)
            (eq (lsp--workspace-shutdown-action workspace) 'restart)
            (and (eq lsp-restart 'interactive)
                 (let ((query (format
                               "Server %s exited with status %s(check corresponding stderr buffer for details). Do you want to restart it?"
                               (lsp--workspace-print workspace)
                               (process-status (lsp--workspace-proc workspace)))))
                   (y-or-n-p query))))
    (--each (lsp--workspace-buffers workspace)
      (when (lsp-buffer-live-p it)
        (lsp-with-current-buffer it
          (if lsp--buffer-deferred
              (lsp-deferred)
            (lsp--info "Restarting LSP in buffer %s" (buffer-name))
            (lsp)))))))

(defun lsp--update-key (table key fn)
  "Apply FN on value corresponding to KEY in TABLE."
  (let ((existing-value (gethash key table)))
    (if-let ((new-value (funcall fn existing-value)))
        (puthash key new-value table)
      (remhash key table))))

(defun lsp--process-sentinel (workspace process exit-str)
  "Create the sentinel for WORKSPACE."
  (unless (process-live-p process)
    (let* ((status (process-status process))
           (folder->workspaces (lsp-session-folder->servers (lsp-session)))
           (stderr (-> workspace lsp--workspace-proc process-name get-buffer)))

      (lsp--warn "%s has exited (%s)"
                 (process-name (lsp--workspace-proc workspace))
                 (string-trim-right exit-str))

      (with-lsp-workspace workspace
        ;; Clean workspace related data in each of the buffers
        ;; in the workspace.
        (--each (lsp--workspace-buffers workspace)
          (when (lsp-buffer-live-p it)
            (lsp-with-current-buffer it
              (setq lsp--buffer-workspaces (delete workspace lsp--buffer-workspaces))
              (lsp--uninitialize-workspace)
              (lsp--spinner-stop)
              (lsp--remove-overlays 'lsp-highlight))))

        ;; Cleanup session from references to the closed workspace.
        (--each (hash-table-keys folder->workspaces)
          (lsp--update-key folder->workspaces it (apply-partially 'delete workspace)))

        ;; Kill standard error buffer only if the process exited normally.
        ;; Leave it intact otherwise for debugging purposes.
        (when (and (eq status 'exit) (zerop (process-exit-status process)) (buffer-live-p stderr))
          (kill-buffer stderr)))

      (run-hook-with-args 'lsp-after-uninitialized-functions workspace)

      (if (eq (lsp--workspace-shutdown-action workspace) 'shutdown)
          (lsp--info "Workspace %s shutdown." (lsp--workspace-print workspace))
        (lsp--restart-if-needed workspace))
      (lsp--cleanup-hanging-watches))))

(defun lsp-workspace-folders (workspace)
  "Return all folders associated with WORKSPACE."
  (let (result)
    (->> (lsp-session)
      (lsp-session-folder->servers)
      (maphash (lambda (folder workspaces)
                 (when (-contains? workspaces workspace)
                   (push folder result)))))
    result))

(defun lsp--start-workspace (session client-template root &optional initialization-options)
  "Create new workspace for CLIENT-TEMPLATE with project root ROOT.
INITIALIZATION-OPTIONS are passed to initialize function.
SESSION is the active session."
  (lsp--spinner-start)
  (-let* ((default-directory root)
          (client (copy-lsp--client client-template))
          (workspace (make-lsp--workspace
                      :root root
                      :client client
                      :status 'starting
                      :buffers (list (lsp-current-buffer))
                      :host-root (file-remote-p root)))
          ((&lsp-cln 'server-id 'environment-fn 'new-connection 'custom-capabilities
                     'multi-root 'initialized-fn) client)
          ((proc . cmd-proc) (funcall
                              (or (plist-get new-connection :connect)
                                  (user-error "Client %s is configured incorrectly" client))
                              (lsp--create-filter-function workspace)
                              (apply-partially #'lsp--process-sentinel workspace)
                              (format "%s" server-id)
                              environment-fn))
          (workspace-folders (gethash server-id (lsp-session-server-id->folders session))))
    (setf (lsp--workspace-proc workspace) proc
          (lsp--workspace-cmd-proc workspace) cmd-proc)

    ;; update (lsp-session-folder->servers) depending on whether we are starting
    ;; multi/single folder workspace
    (mapc (lambda (project-root)
            (->> session
                 (lsp-session-folder->servers)
                 (gethash project-root)
                 (cl-pushnew workspace)))
          (or workspace-folders (list root)))

    (with-lsp-workspace workspace
      (run-hooks 'lsp-before-initialize-hook)
      (lsp-request-async
       "initialize"
       (append
        (list :processId nil
              :rootPath (lsp-file-local-name (expand-file-name root))
              :clientInfo (list :name "emacs"
                                :version (emacs-version))
              :rootUri (lsp--path-to-uri root)
              :capabilities (lsp--client-capabilities custom-capabilities)
              :initializationOptions initialization-options
              :workDoneToken "1")
        (when lsp-server-trace
          (list :trace lsp-server-trace))
        (when multi-root
          (->> workspace-folders
               (-distinct)
               (-map (lambda (folder)
                       (list :uri (lsp--path-to-uri folder)
                             :name (f-filename folder))))
               (apply 'vector)
               (list :workspaceFolders))))
       (lambda (response)
         (unless response
           (lsp--spinner-stop)
           (signal 'lsp-empty-response-error (list "initialize")))

         (let* ((capabilities (lsp:initialize-result-capabilities response))
                (json-object-type 'hash-table)
                (text-document-sync (-some-> lsp-parsed-message
                                      (json-read-from-string)
                                      (ht-get "result")
                                      (ht-get "capabilities")
                                      (ht-get "textDocumentSync")))
                (save (when (ht? text-document-sync)
                        (ht-get text-document-sync "save"))))
           ;; see #1807
           (when (and (ht? save) (ht-empty? save))
             (-> capabilities
                 (lsp:server-capabilities-text-document-sync?)
                 (lsp:set-text-document-sync-options-save? save)))

           (setf (lsp--workspace-server-capabilities workspace) capabilities
                 (lsp--workspace-status workspace) 'initialized)

           (with-lsp-workspace workspace
             (lsp-notify "initialized" lsp--empty-ht))

           (when initialized-fn (funcall initialized-fn workspace))

           (cl-callf2 -filter #'lsp-buffer-live-p (lsp--workspace-buffers workspace))
           (->> workspace
                (lsp--workspace-buffers)
                (mapc (lambda (buffer)
                        (lsp-with-current-buffer buffer
                          (lsp--open-in-workspace workspace)))))

           (with-lsp-workspace workspace
             (run-hooks 'lsp-after-initialize-hook))
           (lsp--info "%s initialized successfully in folders: %s"
                      (lsp--workspace-print workspace)
                      (lsp-workspace-folders workspace))))
       :mode 'detached))
    workspace))

(defun lsp--load-default-session ()
  "Load default session."
  (setq lsp--session (or (condition-case err
                             (lsp--read-from-file lsp-session-file)
                           (error (lsp--error "Failed to parse the session %s, starting with clean one."
                                              (error-message-string err))
                                  nil))
                         (make-lsp-session))))

(defun lsp-session ()
  "Get the session associated with the current buffer."
  (or lsp--session (setq lsp--session (lsp--load-default-session))))

(defun lsp--client-disabled-p (buffer-major-mode client)
  (seq-some
   (lambda (entry)
     (pcase entry
       ((pred symbolp) (eq entry client))
       (`(,mode . ,client-or-list)
        (and (eq mode buffer-major-mode)
             (if (listp client-or-list)
                 (memq client client-or-list)
               (eq client client-or-list))))))
   lsp-disabled-clients))


;; download server

(defcustom lsp-server-install-dir (expand-file-name
                                   (locate-user-emacs-file (f-join ".cache" "lsp")))
  "Directory in which the servers will be installed."
  :risky t
  :type 'directory
  :package-version '(lsp-mode . "6.3")
  :group 'lsp-mode)

(defcustom lsp-verify-signature t
  "Whether to check GPG signatures of downloaded files."
  :type 'boolean
  :package-version '(lsp-mode . "8.0.0")
  :group 'lsp-mode)

(defvar lsp--dependencies (ht))

(defun lsp-dependency (name &rest definitions)
  "Used to specify a language server DEPENDENCY, the server
executable or other required file path. Typically, the
DEPENDENCY is found by locating it on the system path using
`executable-find'.

You can explicitly call lsp-dependency in your environment to
specify the absolute path to the DEPENDENCY. For example, the
typescript-language-server requires both the server and the
typescript compiler. If you've installed them in a team shared
read-only location, you can instruct lsp-mode to use them via

 (eval-after-load 'lsp-mode
   '(progn
      (require 'lsp-javascript)
      (lsp-dependency 'typescript-language-server `(:system ,tls-exe))
      (lsp-dependency 'typescript `(:system ,ts-js))))

where tls-exe is the absolute path to the typescript-language-server
executable and ts-js is the absolute path to the typescript compiler
JavaScript file, tsserver.js (the *.js is required for Windows)."
  (ht-set lsp--dependencies name definitions))

(defun lsp--server-binary-present? (client)
  (unless (equal (lsp--client-server-id client) 'lsp-pwsh)
    (condition-case ()
        (-some-> client lsp--client-new-connection (plist-get :test?) funcall)
      (error nil)
      (args-out-of-range nil))))

(define-minor-mode lsp-installation-buffer-mode
  "Mode used in *lsp-installation* buffers.
It can be used to set-up keybindings, etc. Disabling this mode
detaches the installation buffer from commands like
`lsp-select-installation-buffer'."
  :init-value nil
  :lighter nil)

(defface lsp-installation-finished-buffer-face '((t :foreground "orange"))
  "Face used for finished installation buffers.
Used in `lsp-select-installation-buffer'."
  :group 'lsp-mode)

(defface lsp-installation-buffer-face '((t :foreground "green"))
  "Face used for installation buffers still in progress.
Used in `lsp-select-installation-buffer'."
  :group 'lsp-mode)

(defun lsp--installation-buffer? (buf)
  "Check whether BUF is an `lsp-async-start-process' buffer."
  (buffer-local-value 'lsp-installation-buffer-mode buf))

(defun lsp-select-installation-buffer (&optional show-finished)
  "Interactively choose an installation buffer.
If SHOW-FINISHED is set, leftover (finished) installation buffers
are still shown."
  (interactive "P")
  (let ((bufs (--filter (and (lsp--installation-buffer? it)
                             (or show-finished (get-buffer-process it)))
                        (buffer-list))))
    (pcase bufs
      (`nil (user-error "No installation buffers"))
      (`(,buf) (pop-to-buffer buf))
      (bufs (pop-to-buffer (completing-read "Select installation buffer: "
                                            (--map (propertize (buffer-name it) 'face
                                                               (if (get-buffer-process it)
                                                                   'lsp-installation-buffer-face
                                                                 'lsp-installation-finished-buffer-face))
                                                   bufs)))))))

(defun lsp-cleanup-installation-buffers ()
  "Delete finished *lsp-installation* buffers."
  (interactive)
  (dolist (buf (buffer-list))
    (when (and (lsp--installation-buffer? buf) (not (get-buffer-process buf)))
      (kill-buffer buf))))

(defun lsp--download-status ()
  (-some--> #'lsp--client-download-in-progress?
    (lsp--filter-clients it)
    (-map (-compose #'symbol-name #'lsp--client-server-id) it)
    (format "%s" it)
    (propertize it 'face 'success)
    (format " Installing following servers: %s" it)
    (propertize it
                'local-map (make-mode-line-mouse-map
                            'mouse-1 #'lsp-select-installation-buffer)
                'mouse-face 'highlight)))

(defun lsp--install-server-internal (client &optional update?)
  (unless (lsp--client-download-server-fn client)
    (user-error "There is no automatic installation for `%s', you have to install it manually following lsp-mode's documentation."
                (lsp--client-server-id client)))

  (setf (lsp--client-download-in-progress? client) t)
  (add-to-list 'global-mode-string '(t (:eval (lsp--download-status))))
  (cl-flet ((done
             (success? &optional error-message)
             ;; run with idle timer to make sure the lsp command is executed in
             ;; the main thread, see #2739.
             (run-with-timer
              0.0
              nil
              (lambda ()
                (-let [(&lsp-cln 'server-id 'buffers) client]
                  (setf (lsp--client-download-in-progress? client) nil
                        (lsp--client-buffers client) nil)
                  (if success?
                      (lsp--info "Server %s downloaded, auto-starting in %s buffers." server-id
                                 (length buffers))
                    (lsp--error "Server %s install process failed with the following error message: %s.
Check `*lsp-install*' and `*lsp-log*' buffer."
                                server-id
                                error-message))
                  (seq-do
                   (lambda (buffer)
                     (when (lsp-buffer-live-p buffer)
                       (lsp-with-current-buffer buffer
                         (cl-callf2 -remove-item '(t (:eval (lsp--download-status)))
                                    global-mode-string)
                         (when success? (lsp)))))
                   buffers)
                  (unless (lsp--filter-clients #'lsp--client-download-in-progress?)
                    (cl-callf2 -remove-item '(t (:eval (lsp--download-status)))
                               global-mode-string)))))))
    (lsp--info "Download %s started." (lsp--client-server-id client))
    (condition-case err
        (funcall
         (lsp--client-download-server-fn client)
         client
         (lambda () (done t))
         (lambda (msg) (done nil msg))
         update?)
      (error
       (done nil (error-message-string err))))))

(defun lsp--require-packages ()
  "Load `lsp-client-packages' if needed."
  (when (and lsp-auto-configure (not lsp--client-packages-required))
    (seq-do (lambda (package)
              ;; loading client is slow and `lsp' can be called repeatedly
              (unless (featurep package)
                (require package nil t)))
            lsp-client-packages)
    (setq lsp--client-packages-required t)))

;;;###autoload
(defun lsp-install-server (update? &optional server-id)
  "Interactively install or re-install server.
When prefix UPDATE? is t force installation even if the server is present."
  (interactive "P")
  (lsp--require-packages)
  (let* ((chosen-client (or (gethash server-id lsp-clients)
                            (lsp--completing-read
                             "Select server to install/re-install: "
                             (or (->> lsp-clients
                                      (ht-values)
                                      (-filter (-andfn
                                                (-not #'lsp--client-download-in-progress?)
                                                #'lsp--client-download-server-fn)))
                                 (user-error "There are no servers with automatic installation"))
                             (lambda (client)
                               (let ((server-name (-> client lsp--client-server-id symbol-name)))
                                 (if (lsp--server-binary-present? client)
                                     (concat server-name " (Already installed)")
                                   server-name)))
                             nil
                             t)))
         (update? (or update?
                      (and (not (lsp--client-download-in-progress? chosen-client))
                           (lsp--server-binary-present? chosen-client)))))
    (lsp--install-server-internal chosen-client update?)))

;;;###autoload
(defun lsp-update-server (&optional server-id)
  "Interactively update a server."
  (interactive)
  (lsp--require-packages)
  (let ((chosen-client (or (gethash server-id lsp-clients)
                           (lsp--completing-read
                            "Select server to update (if not on the list, probably you need to `lsp-install-server`): "
                            (or (->> lsp-clients
                                     (ht-values)
                                     (-filter (-andfn
                                               (-not #'lsp--client-download-in-progress?)
                                               #'lsp--client-download-server-fn
                                               #'lsp--server-binary-present?)))
                                (user-error "There are no servers to update"))
                            (lambda (client)
                              (-> client lsp--client-server-id symbol-name))
                            nil
                            t))))
    (lsp--install-server-internal chosen-client t)))

;;;###autoload
(defun lsp-ensure-server (server-id)
  "Ensure server SERVER-ID"
  (lsp--require-packages)
  (if-let ((client (gethash server-id lsp-clients)))
      (unless (lsp--server-binary-present? client)
        (lsp--info "Server `%s' is not preset, installing..." server-id)
        (lsp-install-server nil server-id))
    (warn "Unable to find server registration with id %s" server-id)))

(defun lsp-async-start-process (callback error-callback &rest command)
  "Start async process COMMAND with CALLBACK and ERROR-CALLBACK."
  (let ((name (cl-first command)))
    (with-current-buffer (compilation-start (mapconcat #'shell-quote-argument command " ") t
                                            (lambda (&rest _)
                                              (generate-new-buffer-name (format "*lsp-install: %s*" name))))
      (lsp-installation-buffer-mode +1)
      (view-mode +1)
      (add-hook
       'compilation-finish-functions
       (lambda (_buf status)
         (if (string= "finished\n" status)
             (condition-case err
                 (funcall callback)
               (error
                (funcall error-callback (error-message-string err))))
           (funcall error-callback (s-trim-right status))))
       nil t))))

(defun lsp-resolve-value (value)
  "Resolve VALUE's value.
If it is function - call it.
If it is symbol - return it's value
Otherwise returns value itself."
  (cond
   ((functionp value) (funcall value))
   ((symbolp value) (symbol-value value))
   (value)))

(defvar lsp-deps-providers
  (list :npm (list :path #'lsp--npm-dependency-path
                   :install #'lsp--npm-dependency-install)
        :system (list :path #'lsp--system-path)
        :download (list :path #'lsp-download-path
                        :install #'lsp-download-install)))

(defun lsp--system-path (path)
  "If PATH is absolute and exists return it as is. Otherwise,
return the absolute path to the executable defined by PATH or
nil."
  ;; For node.js 'sub-packages' PATH may point to a *.js file. Consider the
  ;; typescript-language-server. When lsp invokes the server, lsp needs to
  ;; supply the path to the typescript compiler, tsserver.js, as an argument. To
  ;; make code platform independent, one must pass the absolute path to the
  ;; tsserver.js file (Windows requires a *.js file - see help on the JavaScript
  ;; child process spawn command that is invoked by the
  ;; typescript-language-server). This is why we check for existence and not
  ;; that the path is executable.
  (let ((path (lsp-resolve-value path)))
    (if (and (f-absolute? path)
             (f-exists? path))
        path
      (executable-find path))))

(defun lsp-package-path (dependency)
  "Path to the DEPENDENCY each of the registered providers."
  (let (path)
    (-first (-lambda ((provider . rest))
              (setq path (-some-> lsp-deps-providers
                           (plist-get provider)
                           (plist-get :path)
                           (apply rest))))
            (gethash dependency lsp--dependencies))
    path))

(defun lsp-package-ensure (dependency callback error-callback)
  "Asynchronously ensure a package."
  (or (-first (-lambda ((provider . rest))
                (-some-> lsp-deps-providers
                  (plist-get provider)
                  (plist-get :install)
                  (apply (cl-list* callback error-callback rest))))
              (gethash dependency lsp--dependencies))
      (funcall error-callback (format "Unable to find a way to install %s" dependency))))


;; npm handling

;; https://docs.npmjs.com/files/folders#executables
(cl-defun lsp--npm-dependency-path (&key package path &allow-other-keys)
  "Return npm dependency PATH for PACKAGE."
  (let ((path (executable-find
               (f-join lsp-server-install-dir "npm" package
                       (cond ((eq system-type 'windows-nt) "")
                             (t "bin"))
                       path))))
    (unless (and path (f-exists? path))
      (error "The package %s is not installed.  Unable to find %s" package path))
    path))

(cl-defun lsp--npm-dependency-install (callback error-callback &key package &allow-other-keys)
  (if-let ((npm-binary (executable-find "npm")))
      (progn
        ;; Explicitly `make-directory' to work around NPM bug in
        ;; versions 7.0.0 through 7.4.1. See
        ;; https://github.com/emacs-lsp/lsp-mode/issues/2364 for
        ;; discussion.
        (make-directory (f-join lsp-server-install-dir "npm" package "lib") 'parents)
        (lsp-async-start-process callback
                                 error-callback
                                 npm-binary
                                 "-g"
                                 "--prefix"
                                 (f-join lsp-server-install-dir "npm" package)
                                 "install"
                                 package))
    (lsp-log "Unable to install %s via `npm' because it is not present" package)
    nil))


;; Download URL handling
(cl-defun lsp-download-install (callback error-callback &key url asc-url pgp-key store-path decompress &allow-other-keys)
  (let* ((url (lsp-resolve-value url))
         (store-path (lsp-resolve-value store-path))
         ;; (decompress (lsp-resolve-value decompress))
         (download-path
          (pcase decompress
            (:gzip (concat store-path ".gz"))
            (:zip (concat store-path ".zip"))
            (`nil store-path)
            (_ (error ":decompress must be `:gzip', `:zip' or `nil'")))))
    (make-thread
     (lambda ()
       (condition-case err
           (progn
             (when (f-exists? download-path)
               (f-delete download-path))
             (when (f-exists? store-path)
               (f-delete store-path))
             (lsp--info "Starting to download %s to %s..." url download-path)
             (mkdir (f-parent download-path) t)
             (url-copy-file url download-path)
             (lsp--info "Finished downloading %s..." download-path)
             (when (and lsp-verify-signature asc-url pgp-key)
               (if (executable-find epg-gpg-program)
                   (let ((asc-download-path (concat download-path ".asc"))
                         (context (epg-make-context))
                         (fingerprint)
                         (signature))
                     (when (f-exists? asc-download-path)
                       (f-delete asc-download-path))
                     (lsp--info "Starting to download %s to %s..." asc-url asc-download-path)
                     (url-copy-file asc-url asc-download-path)
                     (lsp--info "Finished downloading %s..." asc-download-path)
                     (epg-import-keys-from-string context pgp-key)
                     (setq fingerprint (epg-import-status-fingerprint
                                        (car
                                         (epg-import-result-imports
                                          (epg-context-result-for context 'import)))))
                     (lsp--info "Verifying signature %s..." asc-download-path)
                     (epg-verify-file context asc-download-path download-path)
                     (setq signature (car (epg-context-result-for context 'verify)))
                     (unless (and
                              (eq (epg-signature-status signature) 'good)
                              (equal (epg-signature-fingerprint signature) fingerprint))
                       (error "Failed to verify GPG signature: %s" (epg-signature-to-string signature))))
                 (lsp--warn "GPG is not installed, skipping the signature check.")))
             (when decompress
               (lsp--info "Decompressing %s..." download-path)
               (pcase decompress
                 (:gzip
                  (lsp-gunzip download-path))
                 (:zip (lsp-unzip download-path (f-parent store-path))))
               (lsp--info "Decompressed %s..." store-path))
             (funcall callback))
         (error (funcall error-callback err)))))))

(cl-defun lsp-download-path (&key store-path binary-path set-executable? &allow-other-keys)
  "Download URL and store it into STORE-PATH.

SET-EXECUTABLE? when non-nil change the executable flags of
STORE-PATH to make it executable. BINARY-PATH can be specified
when the binary to start does not match the name of the
archieve(e. g. when the archieve has multiple files)"
  (let ((store-path (or (lsp-resolve-value binary-path)
                        (lsp-resolve-value store-path))))
    (cond
     ((executable-find store-path) store-path)
     ((and set-executable? (f-exists? store-path))
      (set-file-modes store-path #o0700)
      store-path)
     ((f-exists? store-path) store-path))))

;; unzip

(defconst lsp-ext-pwsh-script "powershell -noprofile -noninteractive \
-nologo -ex bypass Expand-Archive -path '%s' -dest '%s'"
  "Powershell script to unzip file.")

(defconst lsp-ext-unzip-script "bash -c 'mkdir -p %2$s && unzip -qq -o %1$s -d %2$s'"
  "Unzip script to unzip file.")

(defcustom lsp-unzip-script (cond ((executable-find "powershell") lsp-ext-pwsh-script)
                                  ((executable-find "unzip") lsp-ext-unzip-script)
                                  (t nil))
  "The script to unzip."
  :group 'lsp-mode
  :type 'string
  :package-version '(lsp-mode . "8.0.0"))

(defun lsp-unzip (zip-file dest)
  "Unzip ZIP-FILE to DEST."
  (unless lsp-unzip-script
    (error "Unable to find `unzip' or `powershell' on the path, please customize `lsp-unzip-script'"))
  (shell-command (format lsp-unzip-script zip-file dest)))

;; gunzip

(defconst lsp-ext-gunzip-script "gzip -d %1$s"
  "Script to decompress a gzippped file with gzip.")

(defcustom lsp-gunzip-script (cond ((executable-find "gzip") lsp-ext-gunzip-script)
                                   (t nil))
  "The script to decompress a gzipped file.
Should be a format string with one argument for the file to be decompressed
in place."
  :group 'lsp-mode
  :type 'string
  :package-version '(lsp-mode . "8.0.0"))

(defun lsp-gunzip (gz-file)
  "Decompress GZ-FILE in place."
  (unless lsp-gunzip-script
    (error "Unable to find `gzip' on the path, please either customize `lsp-gunzip-script' or manually decompress %s" gz-file))
  (shell-command (format lsp-gunzip-script gz-file)))

;; VSCode marketplace

(defcustom lsp-vscode-ext-url
  "https://marketplace.visualstudio.com/_apis/public/gallery/publishers/%s/vsextensions/%s/%s/vspackage"
  "Vscode extension template url."
  :group 'lsp-mode
  :type 'string
  :package-version '(lsp-mode . "8.0.0"))

(defun lsp-vscode-extension-url (publisher name &optional version)
  "Return the URL to vscode extension.
PUBLISHER is the extension publisher.
NAME is the name of the extension.
VERSION is the version of the extension, defaults to `latest'"
  (format lsp-vscode-ext-url publisher name (or version "latest")))



;; Queueing prompts

(defvar lsp--question-queue nil
  "List of questions yet to be asked by `lsp-ask-question'.")

(defun lsp-ask-question (question options callback)
  "Prompt the user to answer the QUESTION with one of the OPTIONS from the
minibuffer. Once the user selects an option, the CALLBACK function will be
called, passing the selected option to it.

If the user is currently being shown a question, the question will be stored in
`lsp--question-queue', and will be asked once the user has answered the current
question."
  (add-to-list 'lsp--question-queue `(("question" . ,question)
                                      ("options" . ,options)
                                      ("callback" . ,callback)) t)
  (when (eq (length lsp--question-queue) 1)
    (lsp--process-question-queue)))

(defun lsp--process-question-queue ()
  "Take the first question from `lsp--question-queue', process it, then process
the next question until the queue is empty."
  (-let* (((&alist "question" "options" "callback") (car lsp--question-queue))
          (answer (completing-read question options nil t)))
    (pop lsp--question-queue)
    (funcall callback answer)
    (when lsp--question-queue
      (lsp--process-question-queue))))

(defun lsp--supports-buffer? (client)
  (and
   ;; both file and client remote or both local
   (eq (---truthy? (file-remote-p (buffer-file-name)))
       (---truthy? (lsp--client-remote? client)))

   ;; activation function or major-mode match.
   (if-let ((activation-fn (lsp--client-activation-fn client)))
       (funcall activation-fn (buffer-file-name) major-mode)
     (-contains? (lsp--client-major-modes client) major-mode))

   ;; check whether it is enabled if `lsp-enabled-clients' is not null
   (or (null lsp-enabled-clients)
       (or (member (lsp--client-server-id client) lsp-enabled-clients)
           (ignore (lsp--info "Client %s is not in lsp-enabled-clients"
                              (lsp--client-server-id client)))))

   ;; check whether it is not disabled.
   (not (lsp--client-disabled-p major-mode (lsp--client-server-id client)))))

(defun lsp--filter-clients (pred)
  (->> lsp-clients hash-table-values (-filter pred)))

(defun lsp--find-clients ()
  "Find clients which can handle BUFFER-MAJOR-MODE.
SESSION is the currently active session. The function will also
pick only remote enabled clients in case the FILE-NAME is on
remote machine and vice versa."
  (-when-let (matching-clients (lsp--filter-clients (-andfn #'lsp--supports-buffer?
                                                            #'lsp--server-binary-present?)))
    (lsp-log "Found the following clients for %s: %s"
             (buffer-file-name)
             (s-join ", "
                     (-map (lambda (client)
                             (format "(server-id %s, priority %s)"
                                     (lsp--client-server-id client)
                                     (lsp--client-priority client)))
                           matching-clients)))
    (-let* (((add-on-clients main-clients) (-separate #'lsp--client-add-on? matching-clients))
            (selected-clients (if-let ((main-client (and main-clients
                                                         (--max-by (> (lsp--client-priority it)
                                                                      (lsp--client-priority other))
                                                                   main-clients))))
                                  (cons main-client add-on-clients)
                                add-on-clients)))
      (lsp-log "The following clients were selected based on priority: %s"
               (s-join ", "
                       (-map (lambda (client)
                               (format "(server-id %s, priority %s)"
                                       (lsp--client-server-id client)
                                       (lsp--client-priority client)))
                             selected-clients)))
      selected-clients)))

(defun lsp-register-client (client)
  "Registers LSP client CLIENT."
  (cl-assert (symbolp (lsp--client-server-id client)) t)
  (cl-assert (or
              (functionp (lsp--client-activation-fn client))
              (and (listp (lsp--client-major-modes client))
                   (seq-every-p (apply-partially #'symbolp)
                                (lsp--client-major-modes client))))
             nil "Invalid activation-fn and/or major-modes.")
  (let ((client-id (lsp--client-server-id client)))
    (puthash client-id client lsp-clients)
    (setplist (intern (format "lsp-%s-after-open-hook" client-id))
              `( standard-value (nil) custom-type hook
                 custom-package-version (lsp-mode . "7.0.1")
                 variable-documentation ,(format "Hooks to run after `%s' server is run." client-id)
                 custom-requests nil))))

(defun lsp--create-initialization-options (_session client)
  "Create initialization-options from SESSION and CLIENT.
Add workspace folders depending on server being multiroot and
session workspace folder configuration for the server."
  (let* ((initialization-options-or-fn (lsp--client-initialization-options client)))
    (if (functionp initialization-options-or-fn)
        (funcall initialization-options-or-fn)
      initialization-options-or-fn)))

(defvar lsp-client-settings nil
  "For internal use, any external users please use
  `lsp-register-custom-settings' function instead")

(defun lsp--compare-setting-path (a b)
  (equal (car a) (car b)))

(defun lsp-register-custom-settings (props)
  "Register PROPS.
PROPS is list of triple (path value boolean?) where PATH is the path to the
property; VALUE can be a literal value, symbol to be evaluated, or either a
function or lambda function to be called without arguments; BOOLEAN? is an
optional flag that should be non-nil for boolean settings, when it is nil the
property will be ignored if the VALUE is nil.

Example: `(lsp-register-custom-settings '((\"foo.bar.buzz.enabled\" t t)))'
\(note the double parentheses)"
  (let ((-compare-fn #'lsp--compare-setting-path))
    (setq lsp-client-settings (-uniq (append props lsp-client-settings)))))

(defun lsp-region-text (region)
  "Get the text for REGION in current buffer."
  (-let (((start . end) (lsp--range-to-region region)))
    (buffer-substring-no-properties start end)))

(defun lsp-ht-set (tbl paths value)
  "Set nested hash table value.
TBL - a hash table, PATHS is the path to the nested VALUE."
  (pcase paths
    (`(,path) (ht-set! tbl path value))
    (`(,path . ,rst) (let ((nested-tbl (or (gethash path tbl)
                                           (let ((temp-tbl (ht)))
                                             (ht-set! tbl path temp-tbl)
                                             temp-tbl))))
                       (lsp-ht-set nested-tbl rst value)))))

;; sections

(defmacro defcustom-lsp (symbol standard doc &rest args)
  "Defines `lsp-mode' server property."
  (let ((path (plist-get args :lsp-path)))
    (cl-remf args :lsp-path)
    `(progn
       (lsp-register-custom-settings
        (quote ((,path ,symbol ,(equal ''boolean (plist-get args :type))))))

       (defcustom ,symbol ,standard ,doc
         :set (lambda (sym val)
                (lsp--set-custom-property sym val ,path))
         ,@args))))

(defun lsp--set-custom-property (sym val path)
  (set sym val)
  (let ((section (cl-first (s-split "\\." path))))
    (mapc (lambda (workspace)
            (when (-contains? (lsp--client-synchronize-sections (lsp--workspace-client workspace))
                              section)
              (with-lsp-workspace workspace
                (lsp--set-configuration (lsp-configuration-section section)))))
          (lsp--session-workspaces (lsp-session)))))

(defun lsp-configuration-section (section)
  "Get settings for SECTION."
  (let ((ret (ht-create)))
    (mapc (-lambda ((path variable boolean?))
            (when (s-matches? (concat (regexp-quote section) "\\..*") path)
              (let* ((symbol-value (-> variable
                                       lsp-resolve-value
                                       lsp-resolve-value))
                     (value (if (and boolean? (not symbol-value))
                                :json-false
                              symbol-value)))
                (when (or boolean? value)
                  (lsp-ht-set ret (s-split "\\." path) value)))))
          lsp-client-settings)
    ret))


(defun lsp--start-connection (session client project-root)
  "Initiates connection created from CLIENT for PROJECT-ROOT.
SESSION is the active session."
  (when (lsp--client-multi-root client)
    (cl-pushnew project-root (gethash (lsp--client-server-id client)
                                      (lsp-session-server-id->folders session))))
  (run-hook-with-args 'lsp-workspace-folders-changed-functions (list project-root) nil)

  (unwind-protect
      (lsp--start-workspace session client project-root (lsp--create-initialization-options session client))
    (lsp--spinner-stop)))

;; lsp-log-io-mode

(defvar lsp-log-io-mode-map
  (let ((map (make-sparse-keymap)))
    (define-key map (kbd "M-n") #'lsp-log-io-next)
    (define-key map (kbd "M-p") #'lsp-log-io-prev)
    (define-key map (kbd "k") #'lsp--erase-log-buffer)
    (define-key map (kbd "K") #'lsp--erase-session-log-buffers)
    map)
  "Keymap for lsp log buffer mode.")

(define-derived-mode lsp-log-io-mode special-mode "LspLogIo"
  "Special mode for viewing IO logs.")

(defun lsp-workspace-show-log (workspace)
  "Display the log buffer of WORKSPACE."
  (interactive
   (list (if lsp-log-io
             (if (eq (length (lsp-workspaces)) 1)
                 (cl-first (lsp-workspaces))
               (lsp--completing-read "Workspace: " (lsp-workspaces)
                                     #'lsp--workspace-print nil t))
           (user-error "IO logging is disabled"))))
  (pop-to-buffer (lsp--get-log-buffer-create workspace)))

(defalias 'lsp-switch-to-io-log-buffer 'lsp-workspace-show-log)

(defun lsp--get-log-buffer-create (workspace)
  "Return the lsp log buffer of WORKSPACE, creating a new one if needed."
  (let ((server-id (-> workspace lsp--workspace-client lsp--client-server-id symbol-name))
        (pid (format "%s" (process-id (lsp--workspace-cmd-proc workspace)))))
    (get-buffer-create (format "*lsp-log: %s:%s*" server-id pid))))

(defun lsp--erase-log-buffer (&optional all)
  "Delete contents of current lsp log buffer.
When ALL is t, erase all log buffers of the running session."
  (interactive)
  (let* ((workspaces (lsp--session-workspaces (lsp-session)))
         (current-log-buffer (current-buffer)))
    (dolist (w workspaces)
      (let ((b (lsp--get-log-buffer-create w)))
        (when (or all (eq b current-log-buffer))
          (with-current-buffer b
            (let ((inhibit-read-only t))
              (erase-buffer))))))))

(defun lsp--erase-session-log-buffers ()
  "Erase log buffers of the running session."
  (interactive)
  (lsp--erase-log-buffer t))

(defun lsp-log-io-next (arg)
  "Move to next log entry."
  (interactive "P")
  (ewoc-goto-next lsp--log-io-ewoc (or arg 1)))

(defun lsp-log-io-prev (arg)
  "Move to previous log entry."
  (interactive "P")
  (ewoc-goto-prev lsp--log-io-ewoc (or arg 1)))

(defun lsp--workspace-print (workspace)
  "Visual representation WORKSPACE."
  (let* ((proc (lsp--workspace-cmd-proc workspace))
         (status (lsp--workspace-status workspace))
         (server-id (-> workspace lsp--workspace-client lsp--client-server-id symbol-name))
         (pid (process-id proc)))

    (if (eq 'initialized status)
        (format "%s:%s" server-id pid)
      (format "%s:%s/%s" server-id pid status))))

(defun lsp--map-tree-widget (m)
  "Build `tree-widget' from a hash-table M."
  (when (hash-table-p m)
    (let (nodes)
      (maphash (lambda (k v)
                 (push `(tree-widget
                         :tag ,(if (hash-table-p v)
                                   (format "%s:" k)
                                 (format "%s: %s" k
                                         (propertize (format "%s" v)
                                                     'face
                                                     'font-lock-string-face)))
                         :open t
                         ,@(lsp--map-tree-widget v))
                       nodes))
               m)
      nodes)))

(defun lsp-buffer-name (buffer-id)
  (if-let ((buffer-name (plist-get buffer-id :buffer-name)))
      (funcall buffer-name buffer-id)
    (buffer-name buffer-id)))

(defun lsp--render-workspace (workspace)
  "Tree node representation of WORKSPACE."
  `(tree-widget :tag ,(lsp--workspace-print workspace)
                :open t
                (tree-widget :tag ,(propertize "Buffers" 'face 'font-lock-function-name-face)
                             :open t
                             ,@(->> workspace
                                    (lsp--workspace-buffers)
                                    (--map `(tree-widget
                                             :tag ,(when (lsp-buffer-live-p it)
                                                     (let ((buffer-name (lsp-buffer-name it)))
                                                       (if (lsp-with-current-buffer it buffer-read-only)
                                                           (propertize buffer-name 'face 'font-lock-constant-face)
                                                         buffer-name)))))))
                (tree-widget :tag ,(propertize "Capabilities" 'face 'font-lock-function-name-face)
                             ,@(-> workspace lsp--workspace-server-capabilities lsp--map-tree-widget))))

(define-derived-mode lsp-browser-mode special-mode "LspBrowser"
  "Define mode for displaying lsp sessions."
  (setq-local display-buffer-base-action '(nil . ((inhibit-same-window . t)))))

(defun lsp-describe-session ()
  "Describes current `lsp-session'."
  (interactive)
  (let ((session (lsp-session))
        (buf (get-buffer-create "*lsp session*")))
    (with-current-buffer buf
      (lsp-browser-mode)
      (let ((inhibit-read-only t))
        (erase-buffer)
        (--each (lsp-session-folders session)
          (widget-create
           `(tree-widget
             :tag ,(propertize it 'face 'font-lock-keyword-face)
             :open t
             ,@(->> session
                    (lsp-session-folder->servers)
                    (gethash it)
                    (-map 'lsp--render-workspace)))))))
    (pop-to-buffer buf)))

(defun lsp--session-workspaces (session)
  "Get all workspaces that are part of the SESSION."
  (-> session lsp-session-folder->servers hash-table-values -flatten -uniq))

(defun lsp--find-multiroot-workspace (session client project-root)
  "Look for a multiroot connection in SESSION created from CLIENT for PROJECT-ROOT and BUFFER-MAJOR-MODE."
  (when (lsp--client-multi-root client)
    (-when-let (multi-root-workspace (->> session
                                          (lsp--session-workspaces)
                                          (--first (eq (-> it lsp--workspace-client lsp--client-server-id)
                                                       (lsp--client-server-id client)))))
      (with-lsp-workspace multi-root-workspace
        (lsp-notify "workspace/didChangeWorkspaceFolders"
                    (lsp-make-did-change-workspace-folders-params
                     :event (lsp-make-workspace-folders-change-event
                             :added (vector (lsp-make-workspace-folder
                                             :uri (lsp--path-to-uri project-root)
                                             :name (f-filename project-root)))
                             :removed []))))

      (->> session (lsp-session-folder->servers) (gethash project-root) (cl-pushnew multi-root-workspace))
      (->> session (lsp-session-server-id->folders) (gethash (lsp--client-server-id client)) (cl-pushnew project-root))

      (lsp--persist-session session)

      (lsp--info "Opened folder %s in workspace %s" project-root (lsp--workspace-print multi-root-workspace))
      (lsp--open-in-workspace multi-root-workspace)

      multi-root-workspace)))

(defun lsp--ensure-lsp-servers (session clients project-root ignore-multi-folder)
  "Ensure that SESSION contain server CLIENTS created for PROJECT-ROOT.
IGNORE-MULTI-FOLDER to ignore multi folder server."
  (-map (lambda (client)
          (or
           (lsp--find-workspace session client project-root)
           (unless ignore-multi-folder
             (lsp--find-multiroot-workspace session client project-root))
           (lsp--start-connection session client project-root)))
        clients))

(defun lsp--spinner-stop ()
  "Stop the spinner in case all of the workspaces are started."
  (when (--all? (eq (lsp--workspace-status it) 'initialized)
                lsp--buffer-workspaces)
    (spinner-stop)))

(defun lsp--open-in-workspace (workspace)
  "Open in existing WORKSPACE."
  (if (eq 'initialized (lsp--workspace-status workspace))
      ;; when workspace is initialized just call document did open.
      (progn
        (with-lsp-workspace workspace
          (when-let ((before-document-open-fn (-> workspace
                                                  lsp--workspace-client
                                                  lsp--client-before-file-open-fn)))
            (funcall before-document-open-fn workspace))
          (lsp--text-document-did-open))
        (lsp--spinner-stop))
    ;; when it is not initialized
    (lsp--spinner-start)
    (cl-pushnew (lsp-current-buffer) (lsp--workspace-buffers workspace))))

(defun lsp--find-workspace (session client project-root)
  "Find server connection created with CLIENT in SESSION for PROJECT-ROOT."
  (when-let ((workspace (->> session
                             (lsp-session-folder->servers)
                             (gethash project-root)
                             (--first (eql (-> it lsp--workspace-client lsp--client-server-id)
                                           (lsp--client-server-id client))))))
    (lsp--open-in-workspace workspace)
    workspace))

(defun lsp--read-char (prompt &optional options)
  "Wrapper for `read-char-from-minibuffer' if Emacs +27.
Fallback to `read-key' otherwise.
PROMPT is the message and OPTIONS the available options."
  (if (fboundp 'read-char-from-minibuffer)
      (read-char-from-minibuffer prompt options)
    (read-key prompt)))

(defun lsp--find-root-interactively (session)
  "Find project interactively.
Returns nil if the project should not be added to the current SESSION."
  (condition-case nil
      (let* ((project-root-suggestion (or (lsp--suggest-project-root) default-directory))
             (action (lsp--read-char
                      (format
                       "%s is not part of any project.

%s ==> Import project root %s
%s ==> Import project by selecting root directory interactively
%s ==> Import project at current directory %s
%s ==> Do not ask again for the current project by adding %s to lsp-session-folders-blacklist
%s ==> Do not ask again for the current project by selecting ignore path interactively
%s ==> Do nothing: ask again when opening other files from the current project

Select action: "
                       (propertize (buffer-name) 'face 'bold)
                       (propertize "i" 'face 'success)
                       (propertize project-root-suggestion 'face 'bold)
                       (propertize "I" 'face 'success)
                       (propertize "." 'face 'success)
                       (propertize default-directory 'face 'bold)
                       (propertize "d" 'face 'warning)
                       (propertize project-root-suggestion 'face 'bold)
                       (propertize "D" 'face 'warning)
                       (propertize "n" 'face 'warning))
                      '(?i ?\r ?I ?. ?d ?D ?n))))
        (cl-case action
          (?i project-root-suggestion)
          (?\r project-root-suggestion)
          (?I (read-directory-name "Select workspace folder to add: "
                                   (or project-root-suggestion default-directory)
                                   nil
                                   t))
          (?. default-directory)
          (?d (push project-root-suggestion (lsp-session-folders-blacklist session))
              (lsp--persist-session session)
              nil)
          (?D (push (read-directory-name "Select folder to blacklist: "
                                         (or project-root-suggestion default-directory)
                                         nil
                                         t)
                    (lsp-session-folders-blacklist session))
              (lsp--persist-session session)
              nil)
          (t nil)))
    ('quit)))

(declare-function tramp-file-name-host "ext:tramp" (file) t)
(declare-function tramp-dissect-file-name "ext:tramp" (file &optional nodefault))

(defun lsp--files-same-host (f1 f2)
  "Predicate on whether or not two files are on the same host."
  (or (not (or (file-remote-p f1) (file-remote-p f2)))
      (and (file-remote-p f1)
           (file-remote-p f2)
           (progn (require 'tramp)
                  (equal (tramp-file-name-host (tramp-dissect-file-name f1))
                         (tramp-file-name-host (tramp-dissect-file-name f2)))))))

(defun lsp-find-session-folder (session file-name)
  "Look in the current SESSION for folder containing FILE-NAME."
  (let ((file-name-canonical (lsp-f-canonical file-name)))
    (->> session
         (lsp-session-folders)
         (--filter (and (lsp--files-same-host it file-name-canonical)
                        (or (lsp-f-same? it file-name-canonical)
                            (and (f-dir? it)
                                 (lsp-f-ancestor-of? it file-name-canonical)))))
         (--max-by (> (length it)
                      (length other))))))

(defun lsp-find-workspace (server-id &optional file-name)
  "Find workspace for SERVER-ID for FILE-NAME."
  (-when-let* ((session (lsp-session))
               (folder->servers (lsp-session-folder->servers session))
               (workspaces (if file-name
                               (gethash (lsp-find-session-folder session file-name) folder->servers)
                             (lsp--session-workspaces session))))

    (--first (eq (lsp--client-server-id (lsp--workspace-client it)) server-id) workspaces)))

(defun lsp--calculate-root (session file-name)
  "Calculate project root for FILE-NAME in SESSION."
  (and
   (->> session
        (lsp-session-folders-blacklist)
        (--first (and (lsp--files-same-host it file-name)
                      (lsp-f-ancestor-of? it file-name)
                      (prog1 t
                        (lsp--info "File %s is in blacklisted directory %s" file-name it))))
        not)
   (or
    (when lsp-auto-guess-root
      (lsp--suggest-project-root))
    (lsp-find-session-folder session file-name)
    (unless lsp-auto-guess-root
      (when-let ((root-folder (lsp--find-root-interactively session)))
        (if (or (not (f-equal? root-folder (expand-file-name "~/")))
                (yes-or-no-p
                 (concat
                  (propertize "[WARNING] " 'face 'warning)
                  "You are trying to import your home folder as project root. This may cause performance issue because some language servers (python, lua, etc) will try to scan all files under project root. To avoid that you may:

1. Use `I' option from the interactive project import to select subfolder(e. g. `~/foo/bar' instead of `~/').
2. If your file is under `~/' then create a subfolder and move that file in this folder.

Type `No' to go back to project selection.
Type `Yes' to confirm `HOME' as project root.
Type `C-g' to cancel project import process and stop `lsp'")))
            root-folder
          (lsp--calculate-root session file-name)))))))

(defun lsp--try-open-in-library-workspace ()
  "Try opening current file as library file in any of the active workspace.
The library folders are defined by each client for each of the active workspace."
  (when-let ((workspace (->> (lsp-session)
                             (lsp--session-workspaces)
                             ;; Sort the last active workspaces first as they are more likely to be
                             ;; the correct ones, especially when jumping to a definition.
                             (-sort (lambda (a _b)
                                      (-contains? lsp--last-active-workspaces a)))
                             (--first
                              (and (-> it lsp--workspace-client lsp--supports-buffer?)
                                   (when-let ((library-folders-fn
                                               (-> it lsp--workspace-client lsp--client-library-folders-fn)))
                                     (-first (lambda (library-folder)
                                               (lsp-f-ancestor-of? library-folder (buffer-file-name)))
                                             (funcall library-folders-fn it))))))))
    (lsp--open-in-workspace workspace)
    (view-mode t)
    (lsp--info "Opening read-only library file %s." (buffer-file-name))
    (list workspace)))

(defun lsp--persist-session (session)
  "Persist SESSION to `lsp-session-file'."
  (lsp--persist lsp-session-file (make-lsp-session
                                  :folders (lsp-session-folders session)
                                  :folders-blacklist (lsp-session-folders-blacklist session)
                                  :server-id->folders (lsp-session-server-id->folders session))))

(defun lsp--try-project-root-workspaces (ask-for-client ignore-multi-folder)
  "Try create opening file as a project file.
When IGNORE-MULTI-FOLDER is t the lsp mode will start new
language server even if there is language server which can handle
current language. When IGNORE-MULTI-FOLDER is nil current file
will be opened in multi folder language server if there is
such."
  (-let ((session (lsp-session)))
    (-if-let (clients (if ask-for-client
                          (list (lsp--completing-read "Select server to start: "
                                                      (ht-values lsp-clients)
                                                      (-compose 'symbol-name 'lsp--client-server-id) nil t))
                        (lsp--find-clients)))
        (-if-let (project-root (-some-> session
                                 (lsp--calculate-root (buffer-file-name))
                                 (lsp-f-canonical)))
            (progn
              ;; update project roots if needed and persist the lsp session
              (unless (-contains? (lsp-session-folders session) project-root)
                (cl-pushnew project-root (lsp-session-folders session))
                (lsp--persist-session session))
              (lsp--ensure-lsp-servers session clients project-root ignore-multi-folder))
          (lsp--warn "%s not in project or it is blacklisted." (buffer-name))
          nil)
      (lsp--warn "No LSP server for %s(check *lsp-log*)." major-mode)
      nil)))

(defun lsp-shutdown-workspace ()
  "Shutdown language server."
  (interactive)
  (--when-let (pcase (lsp-workspaces)
                (`nil (user-error "There are no active servers in the current buffer"))
                (`(,workspace) (when (y-or-n-p (format "Are you sure you want to stop the server %s?"
                                                       (lsp--workspace-print workspace)))
                                 workspace))
                (workspaces (lsp--completing-read "Select server: "
                                                  workspaces
                                                  'lsp--workspace-print nil t)))
    (lsp-workspace-shutdown it)))

(make-obsolete 'lsp-shutdown-workspace 'lsp-workspace-shutdown "lsp-mode 6.1")

(defcustom lsp-auto-select-workspace t
  "Shutdown or restart a single workspace.
If set and the current buffer has only a single workspace
associated with it, `lsp-shutdown-workspace' and
`lsp-restart-workspace' will act on it without asking."
  :type 'boolean
  :group 'lsp-mode)

(defun lsp--read-workspace ()
  "Ask the user to select a workspace.
Errors if there are none."
  (pcase (lsp-workspaces)
    (`nil (error "No workspaces associated with the current buffer"))
    ((and `(,workspace) (guard lsp-auto-select-workspace)) workspace)
    (workspaces (lsp--completing-read "Select workspace: " workspaces
                                      #'lsp--workspace-print nil t))))

(defun lsp-workspace-shutdown (workspace)
  "Shut the workspace WORKSPACE and the language server associated with it"
  (interactive (list (lsp--read-workspace)))
  (lsp--warn "Stopping %s" (lsp--workspace-print workspace))
  (with-lsp-workspace workspace (lsp--shutdown-workspace)))

(defun lsp-disconnect ()
  "Disconnect the buffer from the language server."
  (interactive)
  (lsp--text-document-did-close t)
  (lsp-managed-mode -1)
  (lsp-mode -1)
  (setq lsp--buffer-workspaces nil)
  (lsp--info "Disconnected"))

(defun lsp-restart-workspace ()
  (interactive)
  (--when-let (pcase (lsp-workspaces)
                (`nil (user-error "There are no active servers in the current buffer"))
                (`(,workspace) workspace)
                (workspaces (lsp--completing-read "Select server: "
                                                  workspaces
                                                  'lsp--workspace-print nil t)))
    (lsp-workspace-restart it)))

(make-obsolete 'lsp-restart-workspace 'lsp-workspace-restart "lsp-mode 6.1")

(defun lsp-workspace-restart (workspace)
  "Restart the workspace WORKSPACE and the language server associated with it"
  (interactive (list (lsp--read-workspace)))
  (lsp--warn "Restarting %s" (lsp--workspace-print workspace))
  (with-lsp-workspace workspace (lsp--shutdown-workspace t)))

;;;###autoload
(defun lsp (&optional arg)
  "Entry point for the server startup.
When ARG is t the lsp mode will start new language server even if
there is language server which can handle current language. When
ARG is nil current file will be opened in multi folder language
server if there is such. When `lsp' is called with prefix
argument ask the user to select which language server to start."
  (interactive "P")

  (lsp--require-packages)

  (when (buffer-file-name)
    (let (clients
          (matching-clients (lsp--filter-clients
                             (-andfn #'lsp--supports-buffer?
                                     #'lsp--server-binary-present?))))
      (cond
       (matching-clients
        (when (setq lsp--buffer-workspaces
                    (or (and
                         ;; Don't open as library file if file is part of a project.
                         (not (lsp-find-session-folder (lsp-session) (buffer-file-name)))
                         (lsp--try-open-in-library-workspace))
                        (lsp--try-project-root-workspaces (equal arg '(4))
                                                          (and arg (not (equal arg 1))))))
          (lsp-mode 1)
          (when lsp-auto-configure (lsp--auto-configure))
          (setq lsp-buffer-uri (lsp--buffer-uri))
          (lsp--info "Connected to %s."
                     (apply 'concat (--map (format "[%s]" (lsp--workspace-print it))
                                           lsp--buffer-workspaces)))))
       ;; look for servers which are currently being downloaded.
       ((setq clients (lsp--filter-clients (-andfn #'lsp--supports-buffer?
                                                   #'lsp--client-download-in-progress?)))
        (lsp--info "There are language server(%s) installation in progress.
The server(s) will be started in the buffer when it has finished."
                   (-map #'lsp--client-server-id clients))
        (seq-do (lambda (client)
                  (cl-pushnew (current-buffer) (lsp--client-buffers client)))
                clients))
       ;; look for servers to install
       ((setq clients (lsp--filter-clients
                       (-andfn #'lsp--supports-buffer?
                               (-const lsp-enable-suggest-server-download)
                               #'lsp--client-download-server-fn
                               (-not #'lsp--client-download-in-progress?))))
        (let ((client (lsp--completing-read
                       (concat "Unable to find installed server supporting this file. "
                               "The following servers could be installed automatically: ")
                       clients
                       (-compose #'symbol-name #'lsp--client-server-id)
                       nil
                       t)))
          (cl-pushnew (current-buffer) (lsp--client-buffers client))
          (lsp--install-server-internal client)))
       ;; automatic installation disabled
       ((setq clients (unless matching-clients
                        (lsp--filter-clients (-andfn #'lsp--supports-buffer?
                                                     #'lsp--client-download-server-fn
                                                     (-not (-const lsp-enable-suggest-server-download))
                                                     (-not #'lsp--server-binary-present?)))))
        (lsp--warn "The following servers support current file but automatic download is disabled: %s
\(If you have already installed the server check *lsp-log*)."
                   (mapconcat (lambda (client)
                                (symbol-name (lsp--client-server-id client)))
                              clients
                              " ")))
       ;; no clients present
       ((setq clients (unless matching-clients
                        (lsp--filter-clients (-andfn #'lsp--supports-buffer?
                                                     (-not #'lsp--server-binary-present?)))))
        (lsp--warn "The following servers support current file but do not have automatic installation: %s
You may find the installation instructions at https://emacs-lsp.github.io/lsp-mode/page/languages.
\(If you have already installed the server check *lsp-log*)."
                   (mapconcat (lambda (client)
                                (symbol-name (lsp--client-server-id client)))
                              clients
                              " ")))
       ;; no matches
       ((-> #'lsp--supports-buffer? lsp--filter-clients not)
        (lsp--error "There are no language servers supporting current mode `%s' registered with `lsp-mode'.
This issue might be caused by:
1. The language you are trying to use does not have built-in support in `lsp-mode'. You must install the required support manually. Examples of this are `lsp-java' or `lsp-metals'.
2. The language server that you expect to run is not configured to run for major mode `%s'. You may check that by checking the `:major-modes' that are passed to `lsp-register-client'.
3. `lsp-mode' doesn't have any integration for the language behind `%s'. Refer to https://emacs-lsp.github.io/lsp-mode/page/languages and https://langserver.org/ .
4. You are over `tramp'. In this case follow https://emacs-lsp.github.io/lsp-mode/page/remote/.
5. You have disabled the `lsp-mode' clients for that file. (Check `lsp-enabled-clients' and `lsp-disabled-clients')."
                    major-mode major-mode major-mode))))))

(defun lsp--buffer-visible-p ()
  "Return non nil if current buffer is visible."
  (or (buffer-modified-p) (get-buffer-window nil t)))

(defun lsp--init-if-visible ()
  "Run `lsp' for the current buffer if the buffer is visible.
Returns non nil if `lsp' was run for the buffer."
  (when (lsp--buffer-visible-p)
    (remove-hook 'window-configuration-change-hook #'lsp--init-if-visible t)
    (lsp)
    t))

;;;###autoload
(defun lsp-deferred ()
  "Entry point that defers server startup until buffer is visible.
`lsp-deferred' will wait until the buffer is visible before invoking `lsp'.
This avoids overloading the server with many files when starting Emacs."
  ;; Workspace may not be initialized yet. Use a buffer local variable to
  ;; remember that we deferred loading of this buffer.
  (setq lsp--buffer-deferred t)
  (let ((buffer (current-buffer)))
    ;; Avoid false positives as desktop-mode restores buffers by deferring
    ;; visibility check until the stack clears.
    (run-with-idle-timer 0 nil (lambda ()
                                 (when (buffer-live-p buffer)
                                   (with-current-buffer buffer
                                     (unless (lsp--init-if-visible)
                                       (add-hook 'window-configuration-change-hook #'lsp--init-if-visible nil t))))))))



(defvar lsp-file-truename-cache (ht))

(defmacro lsp-with-cached-filetrue-name (&rest body)
  "Executes BODY caching the `file-truename' calls."
  `(let ((old-fn (symbol-function 'file-truename)))
     (unwind-protect
         (progn
           (fset 'file-truename
                 (lambda (file-name &optional counter prev-dirs)
                   (or (gethash file-name lsp-file-truename-cache)
                       (puthash file-name (apply old-fn (list file-name counter prev-dirs))
                                lsp-file-truename-cache))))
           ,@body)
       (fset 'file-truename old-fn))))


(defun lsp-virtual-buffer-call (key &rest args)
  (when lsp--virtual-buffer
    (when-let ((fn (plist-get lsp--virtual-buffer key)))
      (apply fn args))))

(defun lsp-translate-column (column)
  "Translate COLUMN taking into account virtual buffers."
  (or (lsp-virtual-buffer-call :real->virtual-char column)
      column))

(defun lsp-translate-line (line)
  "Translate LINE taking into account virtual buffers."
  (or (lsp-virtual-buffer-call :real->virtual-line line)
      line))


;; lsp internal validation.

(defmacro lsp--doctor (&rest checks)
  `(-let [buf (current-buffer)]
     (with-current-buffer (get-buffer-create "*lsp-performance*")
       (with-help-window (current-buffer)
         ,@(-map (-lambda ((msg form))
                   `(insert (format "%s: %s\n" ,msg
                                    (let ((res (with-current-buffer buf
                                                 ,form)))
                                      (cond
                                       ((eq res :optional) (propertize "OPTIONAL" 'face 'warning))
                                       (res (propertize "OK" 'face 'success))
                                       (t (propertize "ERROR" 'face 'error)))))))
                 (-partition 2 checks))))))

(define-obsolete-function-alias 'lsp-diagnose
  'lsp-doctor "lsp-mode 8.0.0")

(defun lsp-doctor ()
  "Validate performance settings."
  (interactive)
  (lsp--doctor
   "Checking for Native JSON support" (functionp 'json-serialize)
   "Check emacs supports `read-process-output-max'" (boundp 'read-process-output-max)
   "Check `read-process-output-max' default has been changed from 4k"
   (and (boundp 'read-process-output-max)
        (> read-process-output-max 4096))
   "Byte compiled against Native JSON (recompile lsp-mode if failing when Native JSON available)"
   (condition-case _err
       (progn (lsp--make-message (list "a" "b"))
              nil)
     (error t))
   "`gc-cons-threshold' increased?" (> gc-cons-threshold 800000)
   "Using `plist' for deserialized objects? (refer to https://emacs-lsp.github.io/lsp-mode/page/performance/#use-plists-for-deserialization)" (or lsp-use-plists :optional)
   "Using emacs 28+ with native compilation?"
   (or (and (fboundp 'native-comp-available-p)
            (native-comp-available-p))
       :optional)))

(declare-function package-version-join "ext:package")
(declare-function package-desc-version "ext:package")
(declare-function package--alist "ext:package")

(defun lsp-version ()
  "Return string describing current version of `lsp-mode'."
  (interactive)
  (unless (featurep 'package)
    (require 'package))
  (let ((ver (format "lsp-mode %s, Emacs %s, %s"
                     (package-version-join
                      (package-desc-version
                       (car (alist-get 'lsp-mode (package--alist)))))
                     emacs-version
                     system-type)))
    (if (called-interactively-p 'interactive)
        (lsp--info "%s" ver)
      ver)))



;; org-mode/virtual-buffer

(declare-function org-babel-get-src-block-info "ext:ob-core")
(declare-function org-do-remove-indentation "ext:org-macs")
(declare-function org-src-get-lang-mode "ext:org-src")
(declare-function org-element-context "ext:org-element")

(defun lsp--virtual-buffer-update-position ()
  (-if-let (virtual-buffer (-first (-lambda ((&plist :in-range))
                                     (funcall in-range))
                                   lsp--virtual-buffer-connections))
      (unless (equal virtual-buffer lsp--virtual-buffer)
        (lsp-org))
    (when lsp-managed-mode
      (lsp-managed-mode -1)
      (lsp-mode -1)
      (setq lsp--buffer-workspaces nil)
      (setq lsp--virtual-buffer nil)
      (setq lsp-buffer-uri nil)

      ;; force refresh of diagnostics
      (run-hooks 'lsp-after-diagnostics-hook))))

(defun lsp-virtual-buffer-on-change (start end length)
  "Adjust on change event to be executed against the proper language server."
  (let ((max-point (max end
                        (or (plist-get lsp--before-change-vals :end) 0)
                        (+ start length))))
    (when-let ((virtual-buffer (-first (lambda (vb)
                                         (let ((lsp--virtual-buffer vb))
                                           (and (lsp-virtual-buffer-call :in-range start)
                                                (lsp-virtual-buffer-call :in-range max-point))))
                                       lsp--virtual-buffer-connections)))
      (lsp-with-current-buffer virtual-buffer
        (lsp-on-change start end length
                       (lambda (&rest _)
                         (list :range (lsp--range (list :character 0 :line 0)
                                                  lsp--virtual-buffer-point-max)
                               :text (lsp--buffer-content))))))))

(defun lsp-virtual-buffer-before-change (start _end)
  (when-let ((virtual-buffer (-first (lambda (vb)
                                       (lsp-with-current-buffer vb
                                         (lsp-virtual-buffer-call :in-range start)))
                                     lsp--virtual-buffer-connections)))
    (lsp-with-current-buffer virtual-buffer
      (setq lsp--virtual-buffer-point-max
            (lsp--point-to-position (lsp-virtual-buffer-call :last-point))))))

(defun lsp-patch-on-change-event ()
  (remove-hook 'after-change-functions #'lsp-on-change t)
  (add-hook 'after-change-functions #'lsp-virtual-buffer-on-change nil t)
  (add-hook 'before-change-functions #'lsp-virtual-buffer-before-change nil t))

(defun lsp-kill-virtual-buffers ()
  (mapc #'lsp-virtual-buffer-disconnect lsp--virtual-buffer-connections))

(defun lsp--move-point-in-indentation (point indentation)
  (save-excursion
    (goto-char point)
    (if (<= point (+ (point-at-bol) indentation))
        (point-at-bol)
      point)))

(declare-function flycheck-checker-supports-major-mode-p "ext:flycheck")
(declare-function flycheck-add-mode "ext:flycheck")
(declare-function lsp-diagnostics-lsp-checker-if-needed "lsp-diagnostics")

(defalias 'lsp-client-download-server-fn 'lsp--client-download-server-fn)

(defun lsp-flycheck-add-mode (mode)
  "Register flycheck support for MODE."
  (lsp-diagnostics-lsp-checker-if-needed)
  (unless (flycheck-checker-supports-major-mode-p 'lsp mode)
    (flycheck-add-mode 'lsp mode)))

(defun lsp-org ()
  (interactive)
  (-if-let ((virtual-buffer &as &plist :workspaces) (-first (-lambda ((&plist :in-range))
                                                              (funcall in-range))
                                                            lsp--virtual-buffer-connections))
      (unless (equal lsp--virtual-buffer virtual-buffer)
        (setq lsp--buffer-workspaces workspaces)
        (setq lsp--virtual-buffer virtual-buffer)
        (setq lsp-buffer-uri nil)
        (lsp-mode 1)
        (lsp-managed-mode 1)
        (lsp-patch-on-change-event))

    (save-excursion
      (-let* (virtual-buffer
              (wcb (lambda (f)
                     (with-current-buffer (plist-get virtual-buffer :buffer)
                       (-let* (((&plist :major-mode :buffer-file-name
                                        :goto-buffer :workspaces) virtual-buffer)
                               (lsp--virtual-buffer virtual-buffer)
                               (lsp--buffer-workspaces workspaces))
                         (save-excursion
                           (funcall goto-buffer)
                           (funcall f))))))
              ((&plist :begin :end :post-blank :language) (cl-second (org-element-context)))
              ((&alist :tangle file-name) (cl-third (org-babel-get-src-block-info 'light)))

              (file-name (if file-name
                             (f-expand file-name)
                           (user-error "You should specify file name in the src block header.")))
              (begin-marker (progn
                              (goto-char begin)
                              (forward-line)
                              (set-marker (make-marker) (point))))
              (end-marker (progn
                            (goto-char end)
                            (forward-line (1- (- post-blank)))
                            (set-marker (make-marker) (1+ (point)))))
              (buf (current-buffer))
              (src-block (buffer-substring-no-properties begin-marker
                                                         (1- end-marker)))
              (indentation (with-temp-buffer
                             (insert src-block)

                             (goto-char (point-min))
                             (let ((indentation (current-indentation)))
                               (plist-put lsp--virtual-buffer :indentation indentation)
                               (org-do-remove-indentation)
                               (goto-char (point-min))
                               (- indentation (current-indentation))))))
        (add-hook 'post-command-hook #'lsp--virtual-buffer-update-position nil t)

        (when (fboundp 'flycheck-add-mode)
          (lsp-flycheck-add-mode 'org-mode))

        (setq lsp--virtual-buffer
              (list
               :in-range (lambda (&optional point)
                           (<= begin-marker (or point (point)) (1- end-marker)))
               :goto-buffer (lambda () (goto-char begin-marker))
               :buffer-string
               (lambda ()
                 (let ((src-block (buffer-substring-no-properties
                                   begin-marker
                                   (1- end-marker))))
                   (with-temp-buffer
                     (insert src-block)

                     (goto-char (point-min))
                     (while (not (eobp))
                       (delete-region (point) (if (> (+ (point) indentation) (point-at-eol))
                                                  (point-at-eol)
                                                (+ (point) indentation)))
                       (forward-line))
                     (buffer-substring-no-properties (point-min)
                                                     (point-max)))))
               :buffer buf
               :begin begin-marker
               :end end-marker
               :indentation indentation
               :last-point (lambda () (1- end-marker))
               :cur-position (lambda ()
                               (lsp-save-restriction-and-excursion
                                 (list :line (- (lsp--cur-line)
                                                (lsp--cur-line begin-marker))
                                       :character (let ((character (- (point)
                                                                      (line-beginning-position)
                                                                      indentation)))
                                                    (if (< character 0)
                                                        0
                                                      character)))))
               :line/character->point (-lambda (line character)
                                        (-let [inhibit-field-text-motion t]
                                          (+ indentation
                                             (lsp-save-restriction-and-excursion
                                               (goto-char begin-marker)
                                               (forward-line line)
                                               (-let [line-end (line-end-position)]
                                                 (if (> character (- line-end (point)))
                                                     line-end
                                                   (forward-char character)
                                                   (point)))))))
               :major-mode (org-src-get-lang-mode language)
               :buffer-file-name file-name
               :buffer-uri (lsp--path-to-uri file-name)
               :with-current-buffer wcb
               :buffer-live? (lambda (_) (buffer-live-p buf))
               :buffer-name (lambda (_)
                              (propertize (format "%s(%s:%s)%s"
                                                  (buffer-name buf)
                                                  begin-marker
                                                  end-marker
                                                  language)
                                          'face 'italic))
               :real->virtual-line (lambda (line)
                                     (+ line (line-number-at-pos begin-marker) -1))
               :real->virtual-char (lambda (char) (+ char indentation))
               :cleanup (lambda ()
                          (set-marker begin-marker nil)
                          (set-marker end-marker nil))))
        (setf virtual-buffer lsp--virtual-buffer)
        (puthash file-name virtual-buffer lsp--virtual-buffer-mappings)
        (push virtual-buffer lsp--virtual-buffer-connections)

        ;; TODO: tangle only connected sections
        (add-hook 'after-save-hook 'org-babel-tangle nil t)
        (add-hook 'lsp-after-open-hook #'lsp-patch-on-change-event nil t)
        (add-hook 'kill-buffer-hook #'lsp-kill-virtual-buffers nil t)

        (setq lsp--buffer-workspaces
              (lsp-with-current-buffer virtual-buffer
                (lsp)
                (plist-put virtual-buffer :workspaces (lsp-workspaces))
                (lsp-workspaces)))))))

(defun lsp-virtual-buffer-disconnect (virtual-buffer)
  (interactive (list (or
                      lsp--virtual-buffer
                      (when lsp--virtual-buffer-connections
                        (lsp--completing-read "Select virtual buffer to disconnect: "
                                              lsp--virtual-buffer-connections
                                              (-lambda ((&plist :buffer-file-name))
                                                buffer-file-name))))))
  (-if-let ((&plist :buffer-file-name file-name :cleanup) virtual-buffer)
      (progn
        (lsp-with-current-buffer virtual-buffer
          (lsp--text-document-did-close))
        (setq lsp--virtual-buffer-connections (-remove-item virtual-buffer lsp--virtual-buffer-connections))
        (when (eq virtual-buffer lsp--virtual-buffer)
          (setf lsp--virtual-buffer nil))
        (when cleanup (funcall cleanup))
        (remhash file-name lsp--virtual-buffer-mappings)

        (lsp--virtual-buffer-update-position)
        (lsp--info "Disconnected from buffer %s" file-name))
    (lsp--error "Nothing to disconnect from?")))



;;;###autoload
(defun lsp-start-plain ()
  "Start `lsp-mode' using mininal configuration using the latest `melpa' version of the packages.

In case the major-mode that you are using for "
  (interactive)
  (let ((start-plain (make-temp-file "plain" nil ".el")))
    (url-copy-file "https://raw.githubusercontent.com/emacs-lsp/lsp-mode/master/scripts/lsp-start-plain.el"
                   start-plain t)
    (async-shell-command
     (format "%s -q -l %s %s"
             (expand-file-name invocation-name invocation-directory)
             start-plain
             (or (buffer-file-name) ""))
     (generate-new-buffer " *lsp-start-plain*"))))



(provide 'lsp-mode)
;;; lsp-mode.el ends here