glua_ls 1.0.27

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

    fn legacy_scope(pattern: &str) -> EmmyrcGmodScriptedClassScopeEntry {
        EmmyrcGmodScriptedClassScopeEntry::LegacyGlob(pattern.to_string())
    }

    fn dedent(input: &str) -> String {
        let lines: Vec<&str> = input.lines().collect();
        let mut min_indent = usize::MAX;
        for line in &lines {
            if line.trim().is_empty() {
                continue;
            }
            let indent = line.chars().take_while(|c| *c == ' ').count();
            min_indent = min_indent.min(indent);
        }
        if min_indent == usize::MAX {
            return String::new();
        }
        let mut out = String::new();
        for (i, line) in lines.iter().enumerate() {
            let trimmed = if line.len() >= min_indent {
                &line[min_indent..]
            } else {
                line
            };
            out.push_str(trimmed);
            if i + 1 < lines.len() {
                out.push('\n');
            }
        }
        out.trim_start_matches('\n').trim_end().to_string()
    }

    #[gtest]
    fn test_1() -> Result<()> {
        let mut ws = ProviderVirtualWorkspace::new();
        check!(ws.check_hover(
            r#"
                ---@class <??>A
                ---@field a number
                ---@field b string
                ---@field c boolean
            "#,
            VirtualHoverResult {
                value:
                    "```lua\n(class) A {\n    a: number,\n    b: string,\n    c: boolean,\n}\n```"
                        .to_string(),
            },
        ));
        Ok(())
    }

    #[gtest]
    fn test_right_to_left() -> Result<()> {
        let mut ws = ProviderVirtualWorkspace::new();
        // check!(ws.check_hover(
        //     r#"
        //         ---@class H4
        //         local m = {
        //             x = 1
        //         }

        //         ---@type H4
        //         local m1

        //         m1.x = {}
        //         m1.<??>x = {}
        //     "#,
        //     VirtualHoverResult {
        //         value: "```lua\n(field) x: integer = 1\n```".to_string(),
        //     },
        // ));

        check!(ws.check_hover(
            r#"
                ---@class Node
                ---@field x number
                ---@field right Node?

                ---@return Node
                local function createRBNode()
                end

                ---@type Node
                local node

                if node.right then
                else
                    node.<??>right = createRBNode()
                end
            "#,
            VirtualHoverResult {
                value: "```lua\n(field) right: Node\n```".to_string(),
            },
        ));

        check!(ws.check_hover(
            r#"
                 ---@class Node1
                ---@field x number

                ---@return Node1
                local function createRBNode()
                end

                ---@type Node1?
                local node

                if node then
                else
                    <??>node = createRBNode()
                end
            "#,
            VirtualHoverResult {
                value: "```lua\nlocal node: Node1 {\n    x: number,\n}\n```".to_string(),
            },
        ));
        Ok(())
    }

    #[gtest]
    fn test_hover_nil() -> Result<()> {
        let mut ws = ProviderVirtualWorkspace::new();
        check!(ws.check_hover(
            r#"
                ---@class A
                ---@field a? number

                ---@type A
                local a

                local d = a.<??>a
            "#,
            VirtualHoverResult {
                value: "```lua\n(field) a: number?\n```".to_string(),
            },
        ));
        Ok(())
    }

    #[gtest]
    fn test_hover_reassigned_field_initialized_local_stays_local_after_isvalid_guard() -> Result<()>
    {
        let mut ws = ProviderVirtualWorkspace::new_with_init_std_lib();
        let mut emmyrc = ws.get_emmyrc();
        emmyrc.gmod.enabled = true;
        ws.update_emmyrc(emmyrc);

        let (content, position) = ProviderVirtualWorkspace::handle_file_content(
            r#"
                ---@class Weapon
                ---@field activeVehicle nil

                ---@type Weapon
                local wep
                local veh = wep.activeVehicle
                if not IsValid(veh) then
                    veh = select(1, wep:GetTargetVehicle())
                end
                if not IsValid(veh) then return end
                local narrowed = ve<??>h
            "#,
        )?;
        let file_id = ws.def(&content);
        let hover = extract_hover_markdown(&ws, file_id, position);

        assert!(
            hover.contains("local veh: unknown"),
            "expected later local hover to stay on veh instead of source field, got: {}",
            hover
        );
        assert!(
            !hover.contains("(field) activeVehicle"),
            "later local hover must not bind back to the source field, got: {}",
            hover
        );
        Ok(())
    }

    #[gtest]
    fn test_hover_outparam_updates_trace_output_field() -> Result<()> {
        let mut ws = ProviderVirtualWorkspace::new();
        check!(ws.check_hover(
            r#"
                ---@class TraceResult
                ---@field Hit boolean
                local TraceResult = {}

                util = {}

                ---@outparam traceConfig.output TraceResult
                ---@param traceConfig table
                function util.TraceLine(traceConfig) end

                local ray = {}
                local traceData = {
                    output = ray,
                }

                util.TraceLine(traceData)

                local hit = traceData.<??>output.Hit
            "#,
            VirtualHoverResult {
                value: "```lua\n(field) output: TraceResult\n```".to_string(),
            },
        ));
        Ok(())
    }

    #[gtest]
    fn test_hover_decl_shows_inheritance_chain() -> Result<()> {
        let mut ws = ProviderVirtualWorkspace::new();
        check!(ws.check_hover(
            r#"
                ---@class BaseEntity
                ---@class Entity: BaseEntity
                ---@class Player: Entity

                ---@type Player
                local <??>ply
            "#,
            VirtualHoverResult {
                value: "```lua\nlocal ply: Player : Entity : BaseEntity\n```".to_string(),
            },
        ));
        Ok(())
    }

    #[gtest]
    fn test_hover_decl_shows_full_deep_inheritance_chain() -> Result<()> {
        let mut ws = ProviderVirtualWorkspace::new();
        check!(ws.check_hover(
            r#"
                ---@class A
                ---@class B: A
                ---@class C: B
                ---@class D: C
                ---@class E: D
                ---@class F: E

                ---@type F
                local <??>value
            "#,
            VirtualHoverResult {
                value: "```lua\nlocal value: F : E : D : C : B : A\n```".to_string(),
            },
        ));
        Ok(())
    }

    #[gtest]
    fn test_function_infer_return_val() -> Result<()> {
        let mut ws = ProviderVirtualWorkspace::new();
        check!(ws.check_hover(
            r#"
                local function <??>f(a, b)
                    a = 1
                end
            "#,
            VirtualHoverResult {
                value: "```lua\nlocal function f(a, b)\n```".to_string(),
            },
        ));
        Ok(())
    }

    #[gtest]
    fn test_hover_param_string() -> Result<()> {
        let mut ws = ProviderVirtualWorkspace::new();
        check!(ws.check_hover(
            r#"
                ---@param n string doc
                function foo(<??>n)
                end
            "#,
            VirtualHoverResult {
                value: dedent(
                    r#"
                    ```lua
                    local n: string
                    ```

                    ---

                    doc
                    "#
                )
            },
        ));
        Ok(())
    }

    #[gtest]
    fn test_hover_unannotated_param_with_gmod_name_hint() -> Result<()> {
        let mut ws = ProviderVirtualWorkspace::new();

        check!(ws.check_hover(
            r#"
                ---@class Entity

                local function foo(<??>ent)
                    local value = ent
                end
            "#,
            VirtualHoverResult {
                value: dedent(
                    r#"
                    ```lua
                    local ent: Entity
                    ```
                    "#
                )
            },
        ));
        Ok(())
    }

    #[gtest]
    fn test_hover_param_func() -> Result<()> {
        let mut ws = ProviderVirtualWorkspace::new();
        check!(ws.check_hover(
            r#"
                ---@param n fun():boolean doc
                function foo(<??>n)
                end
            "#,
            VirtualHoverResult {
                value: dedent(
                    r#"
                    ```lua
                    local function n() -> boolean
                    ```

                    ---

                    doc
                    "#
                )
            },
        ));
        Ok(())
    }

    #[gtest]
    fn test_hover_narrowed_function_type() -> Result<()> {
        let mut ws = ProviderVirtualWorkspace::new();
        check!(ws.check_hover(
            r#"
                ---@param n integer|fun():boolean
                function _G.foo(n)
                    local f = n
                    if type(f) ~= 'function' then
                        f = function()
                            return true
                        end
                    end
                    local _ = <??>f
                end
            "#,
            VirtualHoverResult {
                value: dedent(
                    r#"
                    ```lua
                    local function n() -> boolean
                    ```
                    "#
                ),
            },
        ));
        Ok(())
    }

    #[gtest]
    fn test_hover_undefined_global_isstring_guard_narrows_to_string() -> Result<()> {
        let mut ws = ProviderVirtualWorkspace::new_with_init_std_lib();
        ws.def(
            r#"
                ---Returns whether the given value is a string.
                ---@realm shared
                ---@realm menu
                ---@source https://wiki.facepunch.com/gmod/Global.isstring
                ---@param var any
                ---@return TypeGuard<string> isString # Whether the value is a string.
                function _G.isstring(var) end
            "#,
        );
        let (content, position) = ProviderVirtualWorkspace::handle_file_content(
            r#"
                if isstring(testVar2) then ---@diagnostic disable-line: undefined-global
                    print(<??>testVar2) ---@diagnostic disable-line: undefined-global
                end
            "#,
        )?;
        let file_id = ws.def(&content);
        let hover = crate::handlers::hover::hover(&ws.analysis, file_id, position, None)
            .ok_or("expected hover")
            .or_fail()?;

        let HoverContents::Markup(markup) = hover.contents else {
            return fail!("expected HoverContents::Markup");
        };

        assert!(
            markup.value.contains("string"),
            "expected hover to narrow to string, got: {}",
            markup.value
        );
        assert!(
            !markup.value.contains("any"),
            "expected hover to avoid any after narrowing, got: {}",
            markup.value
        );
        assert!(
            !markup.value.contains("unknown"),
            "expected hover to avoid unknown after narrowing, got: {}",
            markup.value
        );

        Ok(())
    }

    #[gtest]
    fn test_hover_undefined_global_field_access_shows_nil_not_any() -> Result<()> {
        let mut ws = ProviderVirtualWorkspace::new_with_init_std_lib();
        let (content, position) = ProviderVirtualWorkspace::handle_file_content(
            r#"
                print(test.<??>meow) ---@diagnostic disable-line: undefined-global
            "#,
        )?;
        let file_id = ws.def(&content);
        let hover = crate::handlers::hover::hover(&ws.analysis, file_id, position, None)
            .ok_or("expected hover")
            .or_fail()?;

        let HoverContents::Markup(markup) = hover.contents else {
            return fail!("expected HoverContents::Markup");
        };

        assert!(
            markup.value.contains("nil"),
            "expected hover to show nil for invalid field access, got: {}",
            markup.value
        );
        assert!(
            !markup.value.contains("any"),
            "expected hover to avoid any for invalid field access, got: {}",
            markup.value
        );
        assert!(
            !markup.value.contains("unknown"),
            "expected hover to avoid unknown for invalid field access, got: {}",
            markup.value
        );

        Ok(())
    }

    #[gtest]
    fn test_hover_invalid_boolean_field_access_shows_nil_not_any() -> Result<()> {
        let mut ws = ProviderVirtualWorkspace::new_with_init_std_lib();
        let (content, position) = ProviderVirtualWorkspace::handle_file_content(
            r#"
                local test = true
                print(test.<??>meow)
            "#,
        )?;
        let file_id = ws.def(&content);
        let hover = crate::handlers::hover::hover(&ws.analysis, file_id, position, None)
            .ok_or("expected hover")
            .or_fail()?;

        let HoverContents::Markup(markup) = hover.contents else {
            return fail!("expected HoverContents::Markup");
        };

        assert!(
            markup.value.contains("nil"),
            "expected hover to show nil for invalid field access, got: {}",
            markup.value
        );
        assert!(
            !markup.value.contains("any"),
            "expected hover to avoid any for invalid field access, got: {}",
            markup.value
        );
        assert!(
            !markup.value.contains("unknown"),
            "expected hover to avoid unknown for invalid field access, got: {}",
            markup.value
        );

        Ok(())
    }

    #[gtest]
    fn test_hover_plain_table_missing_field_shows_nil() -> Result<()> {
        let mut ws = ProviderVirtualWorkspace::new_with_init_std_lib();
        let (content, position) = ProviderVirtualWorkspace::handle_file_content(
            r#"
                local test = {}
                print(test.<??>meow)
            "#,
        )?;
        let file_id = ws.def(&content);
        let hover = crate::handlers::hover::hover(&ws.analysis, file_id, position, None)
            .ok_or("expected hover")
            .or_fail()?;

        let HoverContents::Markup(markup) = hover.contents else {
            return fail!("expected HoverContents::Markup");
        };

        assert!(
            markup.value.contains("nil"),
            "expected hover to show nil for unresolved plain table lookup, got: {}",
            markup.value
        );

        Ok(())
    }

    #[gtest]
    fn test_hover_dynamic_key_read_from_known_table_fields_stays_table() -> Result<()> {
        let mut ws = ProviderVirtualWorkspace::new_with_init_std_lib();
        let mut emmyrc = ws.get_emmyrc();
        emmyrc.gmod.enabled = true;
        emmyrc.gmod.infer_dynamic_fields = true;
        ws.update_emmyrc(emmyrc);

        let (content, position) = ProviderVirtualWorkspace::handle_file_content(
            r#"
                ---@class CSEnt
                local CSEnt = {}
                function CSEnt:Remove()
                end

                ---@param ent any
                ---@return boolean
                local function IsValid(ent)
                end

                ---@param modelPath string
                ---@param renderGroup any
                ---@return CSEnt
                local function ClientsideModel(modelPath, renderGroup)
                end

                local PREVIEW_RENDER_GROUP = 0
                Glide = {}
                local Editor = Glide.VehicleLayoutEditor or {}
                Glide.VehicleLayoutEditor = Editor

                Editor.previewModels = Editor.previewModels or {
                    seats = {},
                    wheels = {}
                }

                function Editor:GetPreviewEntity(kind, itemId, modelPath)
                    if not modelPath or modelPath == "" then return end
                    self.previewModels = self.previewModels or { seats = {}, wheels = {} }
                    local po<??>ol = self.previewModels[kind]
                    if not pool then return end

                    local entry = pool[itemId]
                    if not entry or not IsValid(entry.ent) or entry.model ~= modelPath then
                        if entry and IsValid(entry.ent) then
                            entry.ent:Remove()
                        end

                        local ent = ClientsideModel(modelPath, PREVIEW_RENDER_GROUP)
                        if not IsValid(ent) then
                            pool[itemId] = nil
                            return
                        end

                        entry = { ent = ent, model = modelPath }
                        pool[itemId] = entry
                    end

                    for _, value in pairs(pool) do
                    end

                    return entry.ent
                end
            "#,
        )?;
        let file_id = ws.def_file("lua/glide/client/vehicle_layout_editor.lua", &content);
        let value = extract_hover_markdown(&ws, file_id, position);

        assert!(
            value.contains("local pool: table"),
            "dynamic read of seats/wheels should hover as a table, got: {value}"
        );
        assert!(
            !value.contains("[unknown]"),
            "unknown dynamic write key must not become the displayed table shape, got: {value}"
        );
        Ok(())
    }

    #[gtest]
    fn test_decl_desc() -> Result<()> {
        let mut ws = ProviderVirtualWorkspace::new();
        check!(ws.check_hover(
            r#"
                ---@class Buff.AddData
                ---@field pulse? number 心跳周期

                ---@type Buff.AddData
                local data

                data.pu<??>lse
            "#,
            VirtualHoverResult {
                value: "```lua\n(field) pulse: number?\n```\n\n&nbsp;&nbsp;in class `Buff.AddData`\n\n---\n\n心跳周期".to_string(),
            },
        ));
        Ok(())
    }

    #[gtest]
    fn test_issue_535() -> Result<()> {
        let mut ws = ProviderVirtualWorkspace::new();
        check!(ws.check_hover(
            r#"
                ---@type table<string, number>
                local t

                ---@class T1
                local a

                function a:init(p)
                    self._c<??>fg = t[p]
                end
            "#,
            VirtualHoverResult {
                value: "```lua\n(field) _cfg: number\n```".to_string(),
            },
        ));

        check!(ws.check_hover(
            r#"
                ---@type table<string, number>
                local t = {
                }
                ---@class T2
                local a = {}

                function a:init(p)
                    self._cfg = t[p]
                end

                ---@param p T2
                function fun(p)
                    local x = p._c<??>fg
                end
            "#,
            VirtualHoverResult {
                value: "```lua\n(field) _cfg: number\n```".to_string(),
            },
        ));
        Ok(())
    }

    #[gtest]
    fn test_signature_desc() -> Result<()> {
        let mut ws = ProviderVirtualWorkspace::new();
        check!(ws.check_hover(
            r#"
                -- # A
                local function a<??>bc()
                end
            "#,
            VirtualHoverResult {
                value: "```lua\nlocal function abc()\n```\n\n---\n\n# A".to_string(),
            },
        ));
        Ok(())
    }

    #[gtest]
    fn test_class_desc() -> Result<()> {
        let mut ws = ProviderVirtualWorkspace::new();
        check!(ws.check_hover(
            r#"
                ---A1
                ---@class AB<??>C
                ---A2
            "#,
            VirtualHoverResult {
                value: "```lua\n(class) ABC\n```\n\n---\n\nA1".to_string(),
            },
        ));
        Ok(())
    }

    #[gtest]
    fn test_alias_desc() -> Result<()> {
        let mut ws = ProviderVirtualWorkspace::new();
        check!(ws.check_hover(
            r#"
                ---@alias Tes<??>Alias
                ---| 'A' # A1
                ---| 'B' # A2
            "#,
            VirtualHoverResult {
                value: "```lua\n(alias) TesAlias = (\"A\"|\"B\")\n    | \"A\" -- A1\n    | \"B\" -- A2\n\n```".to_string(),
            },
        ));
        Ok(())
    }

    #[gtest]
    fn test_type_desc() -> Result<()> {
        let mut ws = ProviderVirtualWorkspace::new();
        check!(ws.check_hover(
            r#"
                local export = {
                    ---@type number? activeSub
                    vvv = nil
                }

                export.v<??>vv
            "#,
            VirtualHoverResult {
                value: "```lua\n(field) vvv: number?\n```\n\n---\n\nactiveSub".to_string(),
            },
        ));
        Ok(())
    }

    #[gtest]
    fn test_table_escape_string_keys_hover_cleanly() -> Result<()> {
        let mut ws = ProviderVirtualWorkspace::new();
        check!(
            ws.check_hover_with_level(
                r#"
                local Escape<??>StringMap = {
                    ["\a"] = "\\a",
                    ["\b"] = "\\b",
                    ["\f"] = "\\f",
                    ["\n"] = "\\n",
                    ["\r"] = "\\r",
                    ["\t"] = "\\t",
                    ["\v"] = "\\v",
                    ["\\"] = "\\\\",
                    ["\""] = "\\\"",
                    ["\'"] = "\\\'"
                }
            "#,
                VirtualHoverResult {
                    value: r##"```lua
local EscapeStringMap: {
    ["\a"]: string = "\\a",
    ["\b"]: string = "\\b",
    ["\f"]: string = "\\f",
    ["\n"]: string = "\\n",
    ["\r"]: string = "\\r",
    ["\t"]: string = "\\t",
    ["\v"]: string = "\\v",
    ["\\"]: string = "\\\\",
    ["\""]: string = "\\\"",
    ["'"]: string = "\\'",
}
```"##
                        .to_string(),
                },
                Some(RenderLevel::DetailedCount(12)),
            )
        );
        Ok(())
    }

    #[gtest]
    fn test_field_key() -> Result<()> {
        let mut ws = ProviderVirtualWorkspace::new();
        ws.def(
            r#"
                ---@class ObserverParams
                ---@field next fun() # 测试

                ---@param params fun() | ObserverParams
                function test(params)
                end
            "#,
        );
        check!(ws.check_hover(
            r#"
                test({
                    <??>next = function()
                    end
                })
            "#,
            VirtualHoverResult {
                value: "```lua\n(field) ObserverParams.next()\n```\n\n---\n\n测试".to_string(),
            },
        ));
        Ok(())
    }

    #[gtest]
    fn test_field_key_for_generic() -> Result<()> {
        let mut ws = ProviderVirtualWorkspace::new();
        ws.def(
            r#"
                ---@class ObserverParams<T>
                ---@field next fun() # 测试

                ---@generic T
                ---@param params fun() | ObserverParams<T>
                function test(params)
                end
            "#,
        );
        check!(ws.check_hover(
            r#"
                test({
                    <??>next = function()
                    end
                })
            "#,
            VirtualHoverResult {
                value: "```lua\n(field) ObserverParams.next()\n```\n\n---\n\n测试".to_string(),
            },
        ));
        Ok(())
    }

    #[gtest]
    fn test_before_dot_returns_object_info() -> Result<()> {
        let mut ws = ProviderVirtualWorkspace::new();
        ws.def(
            r#"
                ---@class Node
                ---@field field number?
                ---@field method fun(self: Node)

                ---@type Node
                node = {}

                function node.method() end
            "#,
        );

        check!(ws.check_hover(
            r#"
                node<??>.field = nil
            "#,
            VirtualHoverResult {
                value: "```lua\n(global) node: Node {\n    field: number?,\n    method: function,\n}\n```".to_string(),
            },
        ));

        check!(ws.check_hover(
            r#"
                node<??>:method()
            "#,
            VirtualHoverResult {
                value: "```lua\n(global) node: Node {\n    field: number?,\n    method: function,\n}\n```".to_string(),
            },
        ));

        check!(ws.check_hover(
            r#"
                node<??>["key"] = "value"
            "#,
            VirtualHoverResult {
                value: "```lua\n(global) node: Node {\n    field: number?,\n    method: function,\n}\n```".to_string(),
            },
        ));

        check!(ws.check_hover(
            r#"
                node["key"<??>] = "value"
            "#,
            VirtualHoverResult {
                value: "\"key\"".to_string(),
            },
        ));

        Ok(())
    }

    #[gtest]
    fn test_see_tag() -> Result<()> {
        let mut ws = ProviderVirtualWorkspace::new();
        check!(ws.check_hover(
            r#"
                --- Description
                ---
                --- @see a.b.c
                local function te<??>st() end
            "#,
            VirtualHoverResult {
                value: "```lua\nlocal function test()\n```\n\n---\n\nDescription\n\n---\n\n@*see* a.b.c".to_string(),
            },
        ));

        check!(ws.check_hover(
            r#"
                --- Description
                ---
                --- @see a.b.c see description
                local function te<??>st() end
            "#,
            VirtualHoverResult {
                value: "```lua\nlocal function test()\n```\n\n---\n\nDescription\n\n---\n\n@*see* a.b.c see description".to_string(),
            },
        ));

        Ok(())
    }

    #[gtest]
    fn test_source_tag_renders_clickable_link() -> Result<()> {
        let mut ws = ProviderVirtualWorkspace::new();
        check!(ws.check_hover(
            r#"
                --- Description
                ---
                ---@source https://wiki.facepunch.com/gmod/Entity:SetPos
                local function te<??>st() end
            "#,
            VirtualHoverResult {
                value: "```lua\nlocal function test()\n```\n\n---\n\nDescription\n\n---\n\n**Source:** <https://wiki.facepunch.com/gmod/Entity:SetPos>".to_string(),
            },
        ));

        Ok(())
    }

    #[gtest]
    fn test_other_tag() -> Result<()> {
        let mut ws = ProviderVirtualWorkspace::new();
        check!(ws.check_hover(
            r#"
                --- Description
                ---
                --- @xyz content
                local function te<??>st() end
            "#,
            VirtualHoverResult {
                value: "```lua\nlocal function test()\n```\n\n---\n\nDescription\n\n---\n\n@*xyz* content".to_string(),
            },
        ));

        Ok(())
    }

    #[gtest]
    fn test_class_with_nil() -> Result<()> {
        let mut ws = ProviderVirtualWorkspace::new();
        ws.def(
            r#"
            ---@class A
            ---@field aAnnotation? string a标签

            ---@class B
            ---@field bAnnotation? string b标签
            "#,
        );
        check!(ws.check_hover(
            r#"
            ---@type A|B|nil
            local defaultOpt = {
                aAnnota<??>tion = "a",
            }
            "#,
            VirtualHoverResult {
                value:
                    "```lua\n(field) aAnnotation: string = \"a\"\n```\n\n---\n\na标签".to_string(),
            },
        ));

        Ok(())
    }

    #[gtest]
    fn test_hover_plugin_local_decl_uses_scoped_class_type() -> Result<()> {
        let mut ws = ProviderVirtualWorkspace::new();
        let mut emmyrc = ws.get_emmyrc();
        emmyrc.gmod.enabled = true;
        emmyrc.gmod.scripted_class_scopes.include = vec![legacy_scope("plugins/**")];
        emmyrc.gmod.hook_mappings.method_prefixes = vec!["PLUGIN".to_string()];
        ws.update_emmyrc(emmyrc);

        let (content, position) = ProviderVirtualWorkspace::handle_file_content(
            r#"
                local <??>PLUGIN = PLUGIN ---@diagnostic disable-line: undefined-global

                function PLUGIN:PlayerSpawn(client)
                end
            "#,
        )?;
        let file_id = ws.def_file("cityrp/plugins/vehicles/sh_plugin.lua", &content);
        let hover = crate::handlers::hover::hover(&ws.analysis, file_id, position, None)
            .ok_or("expected hover")
            .or_fail()?;

        let HoverContents::Markup(markup) = hover.contents else {
            return fail!("expected HoverContents::Markup");
        };

        assert!(
            markup.value.contains("vehicles"),
            "expected hover to include inferred plugin class 'vehicles', got: {}",
            markup.value
        );
        assert!(
            !markup.value.contains("unknown"),
            "expected hover to avoid unknown type, got: {}",
            markup.value
        );

        let (content, position) = ProviderVirtualWorkspace::handle_file_content(
            r#"
                local PLUGIN = <??>PLUGIN ---@diagnostic disable-line: undefined-global

                function PLUGIN:PlayerSpawn(client)
                end
            "#,
        )?;
        let file_id = ws.def_file("cityrp/plugins/vehicles/sh_plugin.lua", &content);
        let hover = crate::handlers::hover::hover(&ws.analysis, file_id, position, None)
            .ok_or("expected hover")
            .or_fail()?;

        let HoverContents::Markup(markup) = hover.contents else {
            return fail!("expected HoverContents::Markup");
        };

        assert!(
            markup.value.contains("vehicles"),
            "expected RHS PLUGIN hover to include inferred plugin class 'vehicles', got: {}",
            markup.value
        );
        assert!(
            !markup.value.contains("unknown"),
            "expected RHS PLUGIN hover to avoid unknown type, got: {}",
            markup.value
        );

        Ok(())
    }

    #[gtest]
    fn test_hover_entity_ent_uses_scoped_class_type() -> Result<()> {
        let mut ws = ProviderVirtualWorkspace::new();
        let mut emmyrc = ws.get_emmyrc();
        emmyrc.gmod.enabled = true;
        emmyrc.gmod.scripted_class_scopes.include = vec![legacy_scope("entities/**")];
        ws.update_emmyrc(emmyrc);

        let (content, position) = ProviderVirtualWorkspace::handle_file_content(
            r#"
                <??>ENT.Type = "anim"
                ENT.Base = "base_gmodentity"
            "#,
        )?;
        let file_id = ws.def_file(
            "cityrp/entities/entities/cityrp_money/sh_init.lua",
            &content,
        );
        let hover = crate::handlers::hover::hover(&ws.analysis, file_id, position, None)
            .ok_or("expected hover")
            .or_fail()?;

        let HoverContents::Markup(markup) = hover.contents else {
            return fail!("expected HoverContents::Markup");
        };

        assert!(
            markup.value.contains("cityrp_money"),
            "expected ENT hover to include scoped class 'cityrp_money', got: {}",
            markup.value
        );
        assert!(
            !markup.value.contains("ENT: ENT"),
            "expected ENT hover to avoid base global type ENT, got: {}",
            markup.value
        );

        Ok(())
    }

    #[gtest]
    fn test_hover_entity_ent_without_base_assignment_uses_scoped_class_type() -> Result<()> {
        let mut ws = ProviderVirtualWorkspace::new();
        let mut emmyrc = ws.get_emmyrc();
        emmyrc.gmod.enabled = true;
        emmyrc.gmod.scripted_class_scopes.include = vec![legacy_scope("entities/**")];
        ws.update_emmyrc(emmyrc);

        let (content, position) = ProviderVirtualWorkspace::handle_file_content(
            r#"
                function <??>ENT:Initialize()
                end
            "#,
        )?;
        let file_id = ws.def_file(
            "cityrp/entities/entities/cityrp_inventory/init.lua",
            &content,
        );
        let hover = crate::handlers::hover::hover(&ws.analysis, file_id, position, None)
            .ok_or("expected hover")
            .or_fail()?;

        let HoverContents::Markup(markup) = hover.contents else {
            return fail!("expected HoverContents::Markup");
        };

        assert!(
            markup.value.contains("cityrp_inventory"),
            "expected ENT hover to include scoped class 'cityrp_inventory', got: {}",
            markup.value
        );
        assert!(
            !markup.value.contains("(global) ENT"),
            "expected ENT hover to avoid plain global ENT declaration, got: {}",
            markup.value
        );

        Ok(())
    }

    #[gtest]
    fn test_hover_gm_hook_method_uses_sandbox_docs() -> Result<()> {
        let mut ws = ProviderVirtualWorkspace::new();
        let mut emmyrc = ws.get_emmyrc();
        emmyrc.gmod.enabled = true;
        ws.update_emmyrc(emmyrc);

        ws.def_file(
            "library/lua/includes/extensions/sandbox_hooks.lua",
            r#"
                ---@class GM
                ---@type GM
                GM = GM or {}

                ---@class SANDBOX
                ---@type SANDBOX
                SANDBOX = SANDBOX or {}

                ---Called when a player attempts to spawn a SENT.
                ---@param ply Player
                ---@param class string
                ---@return boolean
                function SANDBOX:PlayerSpawnSENT(ply, class)
                end
            "#,
        );

        let (content, position) = ProviderVirtualWorkspace::handle_file_content(
            r#"
                function GM:PlayerSpawnSE<??>NT(ply, class)
                    return true
                end
            "#,
        )?;
        let file_id = ws.def_file("gamemode/init.lua", &content);
        let hover = crate::handlers::hover::hover(&ws.analysis, file_id, position, None)
            .ok_or("expected hover")
            .or_fail()?;

        let HoverContents::Markup(markup) = hover.contents else {
            return fail!("expected HoverContents::Markup");
        };

        assert!(
            markup
                .value
                .contains("Called when a player attempts to spawn a SENT"),
            "expected hover to include SANDBOX hook docs, got: {}",
            markup.value
        );
        let has_inline_realm_badge = markup.value.contains(
            "![(Shared)](https://github.com/user-attachments/assets/a356f942-57d7-4915-a8cc-559870a980fc)",
        ) || markup.value.contains(
            "![(Server)](https://github.com/user-attachments/assets/d8fbe13a-6305-4e16-8698-5be874721ca1)",
        ) || markup.value.contains(
            "![(Client)](https://github.com/user-attachments/assets/a5f6ba64-374d-42f0-b2f4-50e5c964e808)",
        );
        assert!(
            has_inline_realm_badge,
            "expected hover to include a realm badge, got: {}",
            markup.value
        );
        assert!(
            markup.value.contains("**SHARED**")
                || markup.value.contains("**SERVER**")
                || markup.value.contains("**CLIENT**"),
            "expected hover to include explicit realm label text, got: {}",
            markup.value
        );
        assert!(
            markup.value.contains("```lua\n(method)"),
            "expected hover to keep syntax-highlighted lua signature, got: {}",
            markup.value
        );
        assert!(
            markup.value.contains("PlayerSpawnSENT"),
            "expected hover to include hook function signature, got: {}",
            markup.value
        );

        Ok(())
    }

    #[gtest]
    fn test_hover_hook_add_string_uses_registered_hook_docs() -> Result<()> {
        let mut ws = ProviderVirtualWorkspace::new();
        let mut emmyrc = ws.get_emmyrc();
        emmyrc.gmod.enabled = true;
        ws.update_emmyrc(emmyrc);

        ws.def_file(
            "library/lua/includes/extensions/sandbox_hooks.lua",
            r#"
                ---@class SANDBOX
                ---@type SANDBOX
                SANDBOX = SANDBOX or {}

                ---Called when a player attempts to spawn a SENT.
                ---@param ply Player
                ---@param class string
                ---@return boolean
                function SANDBOX:PlayerSpawnSENT(ply, class)
                end
            "#,
        );

        let (content, position) = ProviderVirtualWorkspace::handle_file_content(
            r#"
                hook.Add("PlayerSpawnSE<??>NT", "test", function() end)
            "#,
        )?;
        let file_id = ws.def_file("gamemode/init.lua", &content);
        let hover = crate::handlers::hover::hover(&ws.analysis, file_id, position, None)
            .ok_or("expected hover")
            .or_fail()?;

        let HoverContents::Markup(markup) = hover.contents else {
            return fail!("expected HoverContents::Markup");
        };

        assert!(
            markup.value.contains("(method) SANDBOX:PlayerSpawnSENT"),
            "expected hook.Add hook-name hover to include SANDBOX method signature, got: {}",
            markup.value
        );
        assert!(
            markup
                .value
                .contains("Called when a player attempts to spawn a SENT"),
            "expected hook.Add hook-name hover to include hook description, got: {}",
            markup.value
        );

        Ok(())
    }

    #[gtest]
    fn test_hover_hook_add_callback_parameter_usage_shows_inferred_type() -> Result<()> {
        let mut ws = ProviderVirtualWorkspace::new();
        let mut emmyrc = ws.get_emmyrc();
        emmyrc.gmod.enabled = true;
        ws.update_emmyrc(emmyrc);

        let (content, position) = ProviderVirtualWorkspace::handle_file_content(
            r#"
                ---@class Entity

                ---@class GM
                ---@type GM
                GM = GM or {}

                ---@param ent Entity
                ---@param input string
                ---@param activator Entity
                ---@param caller Entity
                ---@param value any
                ---@return boolean
                function GM:AcceptInput(ent, input, activator, caller, value) end

                hook = {}
                ---@param eventName string
                ---@param identifier any
                ---@param func function
                function hook.Add(eventName, identifier, func) end

                hook.Add("AcceptInput", "test", function(ent, input, activator, caller, value)
                    print(in<??>put)
                end)
            "#,
        )?;
        let file_id = ws.def_file("gamemode/init.lua", &content);
        let hover = crate::handlers::hover::hover(&ws.analysis, file_id, position, None)
            .ok_or("expected hover")
            .or_fail()?;

        let HoverContents::Markup(markup) = hover.contents else {
            return fail!("expected HoverContents::Markup");
        };

        assert!(
            markup.value.contains("input: string"),
            "expected inferred hook callback parameter type in hover, got: {}",
            markup.value
        );

        Ok(())
    }

    #[gtest]
    fn test_hover_gm_hook_method_shows_realm_badge_without_description() -> Result<()> {
        let mut ws = ProviderVirtualWorkspace::new();
        let mut emmyrc = ws.get_emmyrc();
        emmyrc.gmod.enabled = true;
        ws.update_emmyrc(emmyrc);

        ws.def_file(
            "library/lua/includes/extensions/sandbox_hooks.lua",
            r#"
                ---@class SANDBOX
                ---@type SANDBOX
                SANDBOX = SANDBOX or {}

                function SANDBOX:PlayerSpawnSENT(ply, class)
                end
            "#,
        );

        let (content, position) = ProviderVirtualWorkspace::handle_file_content(
            r#"
                function SANDBOX:PlayerSpawnSE<??>NT(ply, class)
                end
            "#,
        )?;
        let file_id = ws.def_file("gamemode/init.lua", &content);
        let hover = crate::handlers::hover::hover(&ws.analysis, file_id, position, None)
            .ok_or("expected hover")
            .or_fail()?;

        let HoverContents::Markup(markup) = hover.contents else {
            return fail!("expected HoverContents::Markup");
        };

        assert!(
            markup
                .value
                .contains("![(Shared)](https://github.com/user-attachments/assets/a356f942-57d7-4915-a8cc-559870a980fc)"),
            "expected hover to include shared realm badge without text description, got: {}",
            markup.value
        );
        assert!(
            markup.value.contains("**SHARED**"),
            "expected hover to include SHARED label text with realm badge, got: {}",
            markup.value
        );
        assert!(
            markup.value.contains("```lua\n(method)"),
            "expected hover to keep syntax-highlighted lua signature, got: {}",
            markup.value
        );

        Ok(())
    }

    #[gtest]
    fn test_hover_badge_prefers_annotation_realm_over_inferred_realm() -> Result<()> {
        let mut ws = ProviderVirtualWorkspace::new();
        let mut emmyrc = ws.get_emmyrc();
        emmyrc.gmod.enabled = true;
        ws.update_emmyrc(emmyrc);

        let (content, position) = ProviderVirtualWorkspace::handle_file_content(
            r#"
                ---@realm client

                ---@class SANDBOX
                ---@type SANDBOX
                SANDBOX = SANDBOX or {}

                ---Annotation should win for badge realm.
                if SERVER then
                    function SANDBOX:PlayerSpawnSE<??>NT(ply, class)
                    end
                end
            "#,
        )?;
        let file_id = ws.def_file("sv_badge_priority.lua", &content);
        let hover = crate::handlers::hover::hover(&ws.analysis, file_id, position, None)
            .ok_or("expected hover")
            .or_fail()?;

        let HoverContents::Markup(markup) = hover.contents else {
            return fail!("expected HoverContents::Markup");
        };

        assert!(
            markup
                .value
                .contains("![(Client)](https://github.com/user-attachments/assets/a5f6ba64-374d-42f0-b2f4-50e5c964e808)"),
            "expected client badge from annotation realm precedence, got: {}",
            markup.value
        );
        assert!(
            markup.value.contains("**CLIENT**"),
            "expected hover to include CLIENT label text with realm badge, got: {}",
            markup.value
        );
        assert!(
            markup.value.contains("```lua\n(method)"),
            "expected hover to keep syntax-highlighted lua signature, got: {}",
            markup.value
        );

        Ok(())
    }

    #[gtest]
    fn test_hover_gm_method_annotation_realm_overrides_shared_file_realm() -> Result<()> {
        let mut ws = ProviderVirtualWorkspace::new();
        let mut emmyrc = ws.get_emmyrc();
        emmyrc.gmod.enabled = true;
        ws.update_emmyrc(emmyrc);

        ws.def_file(
            "library/lua/includes/extensions/sh_meta_hooks.lua",
            r#"
                ---@class GM
                ---@type GM
                GM = GM or {}

                ---@realm client
                function GM:AnnotatedMetaHook(ply)
                end
            "#,
        );

        let (content, position) = ProviderVirtualWorkspace::handle_file_content(
            r#"
                function GM:AnnotatedMetaHo<??>ok(ply)
                end
            "#,
        )?;
        let file_id = ws.def_file("gamemode/shared.lua", &content);
        let hover = crate::handlers::hover::hover(&ws.analysis, file_id, position, None)
            .ok_or("expected hover")
            .or_fail()?;

        let HoverContents::Markup(markup) = hover.contents else {
            return fail!("expected HoverContents::Markup");
        };

        assert!(
            markup
                .value
                .contains("![(Client)](https://github.com/user-attachments/assets/a5f6ba64-374d-42f0-b2f4-50e5c964e808)"),
            "expected CLIENT badge from declaration annotation, got: {}",
            markup.value
        );
        assert!(
            !markup
                .value
                .contains("![(Shared)](https://github.com/user-attachments/assets/a356f942-57d7-4915-a8cc-559870a980fc)"),
            "did not expect SHARED badge when declaration has ---@realm client, got: {}",
            markup.value
        );
        assert!(
            markup.value.contains("**CLIENT**"),
            "expected CLIENT label text with realm badge, got: {}",
            markup.value
        );

        Ok(())
    }

    #[gtest]
    fn test_hover_table_method_annotation_realm_overrides_shared_file_realm() -> Result<()> {
        let mut ws = ProviderVirtualWorkspace::new();
        let mut emmyrc = ws.get_emmyrc();
        emmyrc.gmod.enabled = true;
        ws.update_emmyrc(emmyrc);

        let (content, position) = ProviderVirtualWorkspace::handle_file_content(
            r#"
                ---@class testTbl
                ---@type testTbl
                testTbl = testTbl or {}

                if SERVER then
                    ---@realm client
                    function testTbl:TestMe<??>thod()
                    end
                end
            "#,
        )?;
        let file_id = ws.def_file("sh_test_tbl.lua", &content);
        let hover = crate::handlers::hover::hover(&ws.analysis, file_id, position, None)
            .ok_or("expected hover")
            .or_fail()?;

        let HoverContents::Markup(markup) = hover.contents else {
            return fail!("expected HoverContents::Markup");
        };

        assert!(
            markup
                .value
                .contains("![(Client)](https://github.com/user-attachments/assets/a5f6ba64-374d-42f0-b2f4-50e5c964e808)"),
            "expected CLIENT badge for annotated table method, got: {}",
            markup.value
        );
        assert!(
            !markup
                .value
                .contains("![(Shared)](https://github.com/user-attachments/assets/a356f942-57d7-4915-a8cc-559870a980fc)"),
            "did not expect SHARED badge for annotated table method, got: {}",
            markup.value
        );
        assert!(
            !markup
                .value
                .contains("![(Server)](https://github.com/user-attachments/assets/d8fbe13a-6305-4e16-8698-5be874721ca1)"),
            "did not expect SERVER badge for annotated table method, got: {}",
            markup.value
        );

        Ok(())
    }

    #[gtest]
    fn test_hover_global_function_annotation_realm_overrides_shared_file_realm() -> Result<()> {
        let mut ws = ProviderVirtualWorkspace::new();
        let mut emmyrc = ws.get_emmyrc();
        emmyrc.gmod.enabled = true;
        ws.update_emmyrc(emmyrc);

        let (content, position) = ProviderVirtualWorkspace::handle_file_content(
            r#"
                if SERVER then
                    ---@realm client
                    function TestFun<??>ction()
                    end
                end
            "#,
        )?;
        let file_id = ws.def_file("sh_global_function.lua", &content);
        let hover = crate::handlers::hover::hover(&ws.analysis, file_id, position, None)
            .ok_or("expected hover")
            .or_fail()?;

        let HoverContents::Markup(markup) = hover.contents else {
            return fail!("expected HoverContents::Markup");
        };

        assert!(
            markup
                .value
                .contains("![(Client)](https://github.com/user-attachments/assets/a5f6ba64-374d-42f0-b2f4-50e5c964e808)"),
            "expected CLIENT badge for annotated global function, got: {}",
            markup.value
        );
        assert!(
            !markup
                .value
                .contains("![(Shared)](https://github.com/user-attachments/assets/a356f942-57d7-4915-a8cc-559870a980fc)"),
            "did not expect SHARED badge for annotated global function, got: {}",
            markup.value
        );
        assert!(
            !markup
                .value
                .contains("![(Server)](https://github.com/user-attachments/assets/d8fbe13a-6305-4e16-8698-5be874721ca1)"),
            "did not expect SERVER badge for annotated global function, got: {}",
            markup.value
        );

        Ok(())
    }

    #[gtest]
    fn test_hover_variable_with_comment_does_not_show_realm_badge() -> Result<()> {
        let mut ws = ProviderVirtualWorkspace::new();
        let mut emmyrc = ws.get_emmyrc();
        emmyrc.gmod.enabled = true;
        ws.update_emmyrc(emmyrc);

        let (content, position) = ProviderVirtualWorkspace::handle_file_content(
            r#"
                ---Variable docs should not gain realm badges.
                local testVa<??>r = 123
            "#,
        )?;
        let file_id = ws.def_file("gamemode/shared.lua", &content);
        let hover = crate::handlers::hover::hover(&ws.analysis, file_id, position, None)
            .ok_or("expected hover")
            .or_fail()?;

        let HoverContents::Markup(markup) = hover.contents else {
            return fail!("expected HoverContents::Markup");
        };

        assert!(
            markup
                .value
                .contains("Variable docs should not gain realm badges"),
            "expected hover to include variable description, got: {}",
            markup.value
        );
        assert!(
            !markup
                .value
                .contains("![(Shared)](https://github.com/user-attachments/assets/a356f942-57d7-4915-a8cc-559870a980fc)"),
            "did not expect SHARED badge for variable hover, got: {}",
            markup.value
        );
        assert!(
            !markup
                .value
                .contains("![(Server)](https://github.com/user-attachments/assets/d8fbe13a-6305-4e16-8698-5be874721ca1)"),
            "did not expect SERVER badge for variable hover, got: {}",
            markup.value
        );
        assert!(
            !markup
                .value
                .contains("![(Client)](https://github.com/user-attachments/assets/a5f6ba64-374d-42f0-b2f4-50e5c964e808)"),
            "did not expect CLIENT badge for variable hover, got: {}",
            markup.value
        );
        assert!(
            !markup.value.contains("**SHARED**")
                && !markup.value.contains("**SERVER**")
                && !markup.value.contains("**CLIENT**"),
            "did not expect realm label text for variable hover, got: {}",
            markup.value
        );

        Ok(())
    }

    #[gtest]
    fn test_hover_dynamic_field_uses_field_style_output() -> Result<()> {
        let mut ws = ProviderVirtualWorkspace::new();
        let mut emmyrc = ws.get_emmyrc();
        emmyrc.gmod.enabled = true;
        emmyrc.gmod.infer_dynamic_fields = true;
        ws.update_emmyrc(emmyrc);

        check!(ws.check_hover(
            r#"
                ---@class HoverDyn.Entity

                ---@type HoverDyn.Entity
                local ent
                ent.testVar = true

                local x = ent.te<??>stVar
            "#,
            VirtualHoverResult {
                value: "```lua\n(field) testVar: true\n```\n\n&nbsp;&nbsp;in class `HoverDyn.Entity`"
                    .to_string(),
            },
        ));

        Ok(())
    }

    #[gtest]
    fn test_hover_dynamic_field_for_metatable_instance() -> Result<()> {
        let mut ws = ProviderVirtualWorkspace::new();
        let mut emmyrc = ws.get_emmyrc();
        emmyrc.gmod.enabled = true;
        emmyrc.gmod.infer_dynamic_fields = true;
        ws.update_emmyrc(emmyrc);

        check!(ws.check_hover(
            r#"
                local LOCATION = {}
                LOCATION.__index = LOCATION

                function LOCATION:Init()
                    local instance = {}
                    setmetatable(instance, self)
                    instance._OriginalName = true
                    return instance
                end

                function LOCATION:GetOriginalName()
                    return self._Origi<??>nalName
                end
            "#,
            VirtualHoverResult {
                value: "```lua\n(infer) _OriginalName: true\n```".to_string(),
            },
        ));

        Ok(())
    }

    #[gtest]
    fn test_hover_dynamic_field_respects_file_scope() -> Result<()> {
        let mut ws = ProviderVirtualWorkspace::new();
        let mut emmyrc = ws.get_emmyrc();
        emmyrc.gmod.enabled = true;
        emmyrc.gmod.infer_dynamic_fields = true;
        emmyrc.gmod.dynamic_fields_global = false;
        ws.update_emmyrc(emmyrc);

        ws.def_file(
            "assign.lua",
            "---@class HoverDynScoped.Entity\n---@type HoverDynScoped.Entity\nlocal ent\nent.testVar = true\n",
        );
        let (content, position) = ProviderVirtualWorkspace::handle_file_content(
            r#"
                ---@type HoverDynScoped.Entity
                local ent2
                local x = ent2.te<??>stVar
            "#,
        )?;
        let file_id = ws.def_file("use.lua", &content);
        let hover = crate::handlers::hover::hover(&ws.analysis, file_id, position, None)
            .ok_or("expected hover")
            .or_fail()?;
        let HoverContents::Markup(markup) = hover.contents else {
            return fail!("expected HoverContents::Markup");
        };
        assert!(
            !markup.value.contains("(infer) testVar"),
            "dynamic field hover should not leak across files when dynamic_fields_global=false, got: {}",
            markup.value
        );

        Ok(())
    }

    #[gtest]
    fn test_hover_dynamic_field_for_tableof() -> Result<()> {
        let mut ws = ProviderVirtualWorkspace::new();
        let mut emmyrc = ws.get_emmyrc();
        emmyrc.gmod.enabled = true;
        emmyrc.gmod.infer_dynamic_fields = true;
        ws.update_emmyrc(emmyrc);

        let (content, position) = ProviderVirtualWorkspace::handle_file_content(
            r#"
                ---@class HoverTbl.Entity
                local HoverTbl = {}

                ---@return tableof<self>
                function HoverTbl:GetTable() end

                function HoverTbl:Init()
                    local tbl = self:GetTable()
                    tbl.customData = true
                    return tbl.cus<??>tomData
                end
            "#,
        )?;
        let file_id = ws.def(&content);
        let hover = crate::handlers::hover::hover(&ws.analysis, file_id, position, None)
            .ok_or("expected hover")
            .or_fail()?;
        let HoverContents::Markup(markup) = hover.contents else {
            return fail!("expected HoverContents::Markup");
        };
        assert!(
            markup.value.contains("customData"),
            "expected tableof dynamic field hover to resolve field name, got: {}",
            markup.value
        );
        assert!(
            markup.value.contains("HoverTbl.Entity"),
            "expected tableof dynamic field hover to stay on class context, got: {}",
            markup.value
        );

        Ok(())
    }

    #[gtest]
    fn test_hover_dynamic_table_field_assignment_prefers_concrete_string_over_any() -> Result<()> {
        let mut ws = ProviderVirtualWorkspace::new();

        let (content, position) = ProviderVirtualWorkspace::handle_file_content(
            r##"
                ---@return table|nil
                local function FromJSON()
                end

                ---@return table
                local function ReadTable()
                    return FromJSON() or {}
                end

                ---@param value string
                ---@return string
                local function FirstChar(value)
                    return value
                end

                ---@param phrase string
                ---@return string
                local function GetPhrase(phrase)
                    return phrase
                end

                local data = ReadTable()

                if FirstChar(data.text) == "#" then
                    data.te<??>xt = GetPhrase(data.text)
                end
            "##,
        )?;
        let file_id = ws.def(&content);
        let value = extract_hover_markdown(&ws, file_id, position);
        assert!(
            value.contains("text: string"),
            "expected concrete string hover for dynamic table field assignment, got: {}",
            value
        );
        assert!(
            !value.contains("any"),
            "expected hover to avoid retaining open-table any in the field type, got: {}",
            value
        );

        Ok(())
    }

    #[gtest]
    fn test_hover_dynamic_table_field_read_before_assignment_stays_open_table_any() -> Result<()> {
        let mut ws = ProviderVirtualWorkspace::new();

        let (content, position) = ProviderVirtualWorkspace::handle_file_content(
            r##"
                ---@return table|nil
                local function FromJSON()
                end

                ---@return table
                local function ReadTable()
                    return FromJSON() or {}
                end

                ---@param value string
                ---@return string
                local function FirstChar(value)
                    return value
                end

                ---@param phrase string
                ---@return string
                local function GetPhrase(phrase)
                    return phrase
                end

                local data = ReadTable()

                if FirstChar(data.text) == "#" then
                    data.text = GetPhrase(data.te<??>xt)
                end
            "##,
        )?;
        let file_id = ws.def(&content);
        let value = extract_hover_markdown(&ws, file_id, position);
        assert!(
            value.contains("any?"),
            "expected pre-assignment open-table field read to stay broad, got: {}",
            value
        );
        assert!(
            !value.contains("string"),
            "expected future assignment not to type a prior field read, got: {}",
            value
        );

        Ok(())
    }

    #[gtest]
    fn test_hover_dynamic_table_field_call_arg_before_assignment_stays_open_table_any() -> Result<()>
    {
        let mut ws = ProviderVirtualWorkspace::new();

        let (content, position) = ProviderVirtualWorkspace::handle_file_content(
            r##"
                util = {}

                ---@return table?
                function util.JSONToTable(s)
                end

                ---@return table
                local function ReadTable()
                    return util.JSONToTable("") or {}
                end

                ---@param value string
                ---@return string
                local function FirstChar(value)
                    return value
                end

                ---@param phrase string
                ---@return string
                local function GetPhrase(phrase)
                    return phrase
                end

                local data = ReadTable()

                if FirstChar(data.te<??>xt) == "#" then
                    data.text = GetPhrase(data.text)
                end
            "##,
        )?;
        let file_id = ws.def(&content);
        let value = extract_hover_markdown(&ws, file_id, position);
        assert!(
            value.contains("any?"),
            "expected call-site pre-assignment field read to stay broad, got: {}",
            value
        );
        assert!(
            !value.contains("string"),
            "expected future assignment not to type the earlier call argument, got: {}",
            value
        );

        Ok(())
    }

    #[gtest]
    fn test_hover_glide_readtable_field_call_arg_stays_open_table_any_after_reindex() -> Result<()>
    {
        let mut ws = ProviderVirtualWorkspace::new();
        let mut emmyrc = ws.get_emmyrc();
        emmyrc.gmod.enabled = true;
        emmyrc.gmod.infer_dynamic_fields = true;
        ws.update_emmyrc(emmyrc);

        let (network_content, position) = ProviderVirtualWorkspace::handle_file_content(
            r##"
                Glide.NetCommands = Glide.NetCommands or {}
                local commands = Glide.NetCommands

                commands[Glide.CMD_NOTIFY] = function()
                    local data = Glide.ReadTable()

                    if string.sub(data.te<??>xt, 1, 1) == "#" then
                        data.text = language.GetPhrase(data.text)
                    end
                end
            "##,
        )?;

        ws.def_files(vec![
            (
                "lua/autorun/sh_glide.lua",
                r#"
                ---@class Glide
                Glide = Glide or {}
                Glide.CMD_NOTIFY = 1

                function Glide.FromJSON(s)
                    if type(s) ~= "string" or s == "" then
                        return {}
                    end

                    return util.JSONToTable(s) or {}
                end
                "#,
            ),
            (
                "lua/glide/sh_network.lua",
                r#"
                function Glide.ReadTable()
                    local data = net.ReadData(1)
                    return Glide.FromJSON(data)
                end
                "#,
            ),
            (
                "lua/includes/util.lua",
                r#"
                util = {}

                ---@return table?
                function util.JSONToTable(json)
                end
                "#,
            ),
            (
                "lua/includes/net.lua",
                r#"
                net = {}

                ---@return string
                function net.ReadData(length)
                end
                "#,
            ),
            (
                "lua/includes/language.lua",
                r#"
                language = {}

                ---@param phrase string
                ---@return string
                function language.GetPhrase(phrase)
                end
                "#,
            ),
            (
                "lua/includes/string.lua",
                r#"
                string = {}

                ---@param s string
                ---@param i integer
                ---@param j integer?
                ---@return string
                function string.sub(s, i, j)
                end
                "#,
            ),
            ("lua/glide/client/network.lua", &network_content),
        ]);
        let uri = ws
            .virtual_url_generator
            .new_uri("lua/glide/client/network.lua");
        let file_id = ws
            .analysis
            .get_file_id(&uri)
            .expect("expected network.lua file id");

        let initial = extract_hover_markdown(&ws, file_id, position);
        assert!(
            initial.contains("any?"),
            "expected initial hover to stay broad, got: {}",
            initial
        );

        ws.analysis
            .update_file_text_only(&uri, format!("{network_content}\n"));

        ws.analysis.reindex_files(vec![file_id]);

        let after_reindex = extract_hover_markdown(&ws, file_id, position);
        assert!(
            after_reindex.contains("any?"),
            "expected post-reindex hover to stay broad, got: {}",
            after_reindex
        );
        assert!(
            !after_reindex.contains("string"),
            "expected future assignment not to type the earlier call argument after reindex, got: {}",
            after_reindex
        );

        Ok(())
    }

    #[gtest]
    fn test_hover_table_field_rhs_does_not_use_lhs_assignment_member() -> Result<()> {
        let mut ws = ProviderVirtualWorkspace::new();
        let mut emmyrc = ws.get_emmyrc();
        emmyrc.gmod.enabled = true;
        emmyrc.gmod.infer_dynamic_fields = true;
        ws.update_emmyrc(emmyrc);

        let (content, position) = ProviderVirtualWorkspace::handle_file_content(
            r##"
                ---@param phrase string
                ---@return string
                local function GetPhrase(phrase)
                    return phrase
                end

                ---@return table<any, any>
                local function ReadTable()
                    return {}
                end

                local data = ReadTable()
                data.text = GetPhrase(data.te<??>xt)
            "##,
        )?;
        let file_id = ws.def_file("lua/glide/client/network.lua", &content);

        let hover = extract_hover_markdown(&ws, file_id, position);
        assert!(
            hover.contains("any"),
            "expected RHS read not to use the LHS assignment member, got: {}",
            hover
        );
        assert!(
            !hover.contains("string"),
            "expected RHS read to remain broad before assignment value is applied, got: {}",
            hover
        );

        Ok(())
    }

    #[gtest]
    fn test_hover_class_field_read_does_not_use_later_same_file_assignment() -> Result<()> {
        let mut ws = ProviderVirtualWorkspace::new();
        let mut emmyrc = ws.get_emmyrc();
        emmyrc.gmod.enabled = true;
        emmyrc.gmod.infer_dynamic_fields = true;
        ws.update_emmyrc(emmyrc);

        let (content, position) = ProviderVirtualWorkspace::handle_file_content(
            r##"
                ---@class ReindexData

                ---@type ReindexData
                local data = {}

                local before = data.te<??>xt
                data.text = "later"
            "##,
        )?;
        let file_name = "lua/glide/client/network.lua";
        let file_id = ws.def_file(file_name, &content);

        let initial = extract_hover_markdown(&ws, file_id, position);
        assert!(
            !initial.contains("(field)") && !initial.contains("string"),
            "expected initial hover not to use the later assignment, got: {}",
            initial
        );

        let uri = ws.virtual_url_generator.new_uri(file_name);
        ws.analysis
            .update_file_text_only(&uri, format!("{content}\n"));
        ws.analysis.reindex_files(vec![file_id]);

        let after_reindex = extract_hover_markdown(&ws, file_id, position);
        assert!(
            !after_reindex.contains("(field)") && !after_reindex.contains("string"),
            "expected post-reindex hover not to use the later assignment, got: {}",
            after_reindex
        );

        Ok(())
    }

    #[gtest]
    fn test_hover_dynamic_table_field_read_after_assignment_uses_assigned_string() -> Result<()> {
        let mut ws = ProviderVirtualWorkspace::new();

        let (content, position) = ProviderVirtualWorkspace::handle_file_content(
            r##"
                ---@return table|nil
                local function FromJSON()
                end

                ---@return table
                local function ReadTable()
                    return FromJSON() or {}
                end

                ---@param value string
                ---@return string
                local function GetPhrase(value)
                    return value
                end

                local data = ReadTable()
                data.text = GetPhrase(data.text)
                local text = data.te<??>xt
            "##,
        )?;
        let file_id = ws.def(&content);
        let value = extract_hover_markdown(&ws, file_id, position);
        assert!(
            value.contains("string"),
            "expected post-assignment field read to use assigned string type, got: {}",
            value
        );
        assert!(
            !value.contains("any"),
            "expected post-assignment field read not to retain open-table any, got: {}",
            value
        );

        Ok(())
    }

    #[gtest]
    fn test_hover_branch_initialized_indexed_record_assignment() -> Result<()> {
        let mut ws = ProviderVirtualWorkspace::new();
        let mut emmyrc = ws.get_emmyrc();
        emmyrc.gmod.enabled = true;
        ws.update_emmyrc(emmyrc);

        ws.def_file(
            "lua/autorun/sh_glide.lua",
            r#"
            ---@class Glide
            Glide = Glide or {}
        "#,
        );
        ws.def_file(
            "lua/glide/sh_network.lua",
            r#"
            Glide.DebugNetwork = Glide.DebugNetwork or {}

            if CLIENT then
                local DebugNet = Glide.DebugNetwork

                function DebugNet.ReadSnapshot()
                    local entId = net.ReadUInt(16)
                    local vehicleId = nil
                    local fields = {}
                    return entId, vehicleId, fields
                end
            end
        "#,
        );

        let (content, position) = ProviderVirtualWorkspace::handle_file_content(
            r#"
            local DebugNet = Glide.DebugNetwork

            local function receive()
                local entId, vehicleId, fields = DebugNet.ReadSnapshot()
                if not entId then return end

                Glide.DebugSnapshots = Glide.DebugSnapshots or {}
                local rec = Glide.DebugSnapshots[entId]
                if not rec then
                    re<??>c = { data = {}, t = SysTime() }
                    Glide.DebugSnapshots[entId] = rec
                end

                local data = rec.data
                print(data, rec)
            end
        "#,
        )?;
        let file_id = ws.def_file("lua/glide/client/network.lua", &content);
        let value = extract_hover_markdown(&ws, file_id, position);
        assert!(
            value.contains("data"),
            "expected assignment hover to include record table shape, got: {}",
            value
        );
        assert!(
            !value.contains("any"),
            "expected assignment hover not to collapse record table to any, got: {}",
            value
        );

        Ok(())
    }

    #[gtest]
    fn test_hover_dynamic_key_table_shape_omits_unnamed_wildcard_value_row() -> Result<()> {
        let mut ws = ProviderVirtualWorkspace::new();
        let mut emmyrc = ws.get_emmyrc();
        emmyrc.gmod.enabled = true;
        emmyrc.gmod.infer_dynamic_fields = true;
        ws.update_emmyrc(emmyrc);

        let (content, position) = ProviderVirtualWorkspace::handle_file_content(
            r#"
                ---@class Vector
                local Vector = {}
                ---@return Vector
                function _G.Vector() end

                net = {}
                ---@return string
                function net.ReadString() end

                local function readValue()
                    if flag == 1 then
                        return Vector()
                    end

                    if flag == 2 then
                        return "text"
                    end

                    return 1
                end

                local rec = { data = {}, t = 0 }
                local data = rec.data
                data.vehicle = 1

                local key = net.ReadString()
                data[key] = readValue()

                local d = rec.da<??>ta
                print(d)
            "#,
        )?;
        let file_id = ws.def_file("lua/glide/client/network.lua", &content);
        let value = extract_hover_markdown(&ws, file_id, position);

        assert!(
            value.contains("vehicle"),
            "expected hover to retain concrete named fields, got: {value}"
        );
        assert!(
            !value.contains("\n    ("),
            "dynamic-key value type should not render as an unnamed table field, got: {value}"
        );
        assert!(
            value.contains("[string]"),
            "expected dynamic string-key values to render as an indexed table entry, got: {value}"
        );

        Ok(())
    }

    #[gtest]
    fn test_hover_dynamic_string_key_field_uses_index_value_type() -> Result<()> {
        let mut ws = ProviderVirtualWorkspace::new_with_init_std_lib();
        let mut emmyrc = ws.get_emmyrc();
        emmyrc.gmod.enabled = true;
        emmyrc.gmod.infer_dynamic_fields = true;
        ws.update_emmyrc(emmyrc);

        let (content, position) = ProviderVirtualWorkspace::handle_file_content(
            r#"
                ---@class Vector
                local Vector = {}
                ---@return Vector
                function _G.Vector() end

                net = {}
                ---@return string
                function net.ReadString() end

                Glide = {}

                local function readValue()
                    if flag == 1 then
                        return Vector()
                    end

                    if flag == 2 then
                        return true
                    end

                    if flag == 3 then
                        return {}
                    end

                    if flag == 4 then
                        return "text"
                    end

                    return 1
                end

                local fields = {}
                fields[net.ReadString()] = readValue()

                Glide.DebugSnapshots = Glide.DebugSnapshots or {}
                local rec = Glide.DebugSnapshots[1]
                if not rec then
                    rec = { data = {}, t = 0 }
                    Glide.DebugSnapshots[1] = rec
                end

                local data = rec.data
                data.vehicle = 1

                for key, value in pairs(fields) do
                    data[key] = value
                end

                local d = rec.data
                print(d.for<??>wardForce)
            "#,
        )?;
        let file_id = ws.def_file("lua/glide/client/debugging.lua", &content);
        let value = extract_hover_markdown(&ws, file_id, position);

        assert!(
            !value.contains(": nil"),
            "dynamic string-key field hover must not collapse to nil, got: {value}"
        );
        assert!(
            value.contains("Vector")
                && value.contains("true")
                && value.contains("1")
                && value.contains("\"text\"")
                && value.contains("table"),
            "expected field hover to use the dynamic string index value union, got: {value}"
        );

        Ok(())
    }

    #[gtest]
    fn test_hover_local_gettable_call_uses_scoped_receiver_type() -> Result<()> {
        let mut ws = ProviderVirtualWorkspace::new();
        let mut emmyrc = ws.get_emmyrc();
        emmyrc.gmod.enabled = true;
        emmyrc.gmod.scripted_class_scopes.include = vec![legacy_scope("entities/**")];
        ws.update_emmyrc(emmyrc);

        let (content, position) = ProviderVirtualWorkspace::handle_file_content(&dedent(
            r#"
                    ---@class Entity
                    local Entity = {}

                    ---@return tableof<self>
                    function Entity:GetTable() end

                    ---@generic T : table
                    ---@param metaName `T`
                    ---@return T
                    function FindMetaTable(metaName) end

                    local getTable = FindMetaTable("Entity").GetTable

                    function ENT:Think(selfTbl)
                        selfTbl = selfTbl or getTa<??>ble(self)
                    end
                "#,
        ))?;
        let file_id = ws.def_file("cityrp/entities/entities/glide_wheel/init.lua", &content);
        let hover = crate::handlers::hover::hover(&ws.analysis, file_id, position, None)
            .ok_or("expected hover")
            .or_fail()?;
        let HoverContents::Markup(markup) = hover.contents else {
            return fail!("expected HoverContents::Markup");
        };

        assert!(
            markup.value.contains("glide_wheel:GetTable"),
            "expected aliased GetTable hover to use scripted class receiver, got: {}",
            markup.value
        );
        assert!(
            markup.value.contains("tableof<glide_wheel>"),
            "expected aliased GetTable hover to specialize return type, got: {}",
            markup.value
        );
        assert!(
            !markup.value.contains("tableof<Entity>"),
            "expected aliased GetTable hover to avoid base Entity return type, got: {}",
            markup.value
        );

        Ok(())
    }

    #[gtest]
    fn test_hover_local_gettable_nested_state_field_keeps_declared_type() -> Result<()> {
        let mut ws = ProviderVirtualWorkspace::new();
        let mut emmyrc = ws.get_emmyrc();
        emmyrc.gmod.enabled = true;
        emmyrc.gmod.infer_dynamic_fields = true;
        emmyrc.gmod.scripted_class_scopes.include = vec![legacy_scope("entities/**")];
        ws.update_emmyrc(emmyrc);

        let (content, position) = ProviderVirtualWorkspace::handle_file_content(&dedent(
            r#"
                    ---@class Entity
                    local Entity = {}

                    ---@return tableof<self>
                    function Entity:GetTable() end

                    ---@generic T : table
                    ---@param metaName `T`
                    ---@return T
                    function FindMetaTable(metaName) end

                    ---@class GlideTraceData
                    ---@field start number

                    ---@class GlideWheelState
                    ---@field traceData GlideTraceData

                    local getTable = FindMetaTable("Entity").GetTable

                    function ENT:Initialize()
                        ---@type GlideWheelState
                        self.state = {
                            traceData = {
                                start = 1,
                            },
                        }
                    end

                    function ENT:Think(selfTbl)
                        selfTbl = selfTbl or getTable(self)
                        local traceData = selfTbl.state.traceData
                        return tra<??>ceData
                    end
                "#,
        ))?;
        let file_id = ws.def_file("cityrp/entities/entities/glide_wheel/init.lua", &content);
        let hover = crate::handlers::hover::hover(&ws.analysis, file_id, position, None)
            .ok_or("expected hover")
            .or_fail()?;
        let HoverContents::Markup(markup) = hover.contents else {
            return fail!("expected HoverContents::Markup");
        };

        assert!(
            markup.value.contains("GlideTraceData"),
            "expected nested traceData hover to keep declared type, got: {}",
            markup.value
        );
        assert!(
            !markup.value.contains("never"),
            "expected nested traceData hover to avoid never, got: {}",
            markup.value
        );
        assert!(
            !markup.value.contains(": nil"),
            "expected nested traceData hover to avoid nil, got: {}",
            markup.value
        );

        Ok(())
    }

    #[gtest]
    fn test_hover_local_gettable_state_field_keeps_typed_state() -> Result<()> {
        let mut ws = ProviderVirtualWorkspace::new();
        let mut emmyrc = ws.get_emmyrc();
        emmyrc.gmod.enabled = true;
        emmyrc.gmod.infer_dynamic_fields = true;
        emmyrc.gmod.scripted_class_scopes.include = vec![legacy_scope("entities/**")];
        ws.update_emmyrc(emmyrc);

        let (content, position) = ProviderVirtualWorkspace::handle_file_content(&dedent(
            r#"
                    ---@class Entity
                    local Entity = {}

                    ---@return tableof<self>
                    function Entity:GetTable() end

                    ---@generic T : table
                    ---@param metaName `T`
                    ---@return T
                    function FindMetaTable(metaName) end

                    ---@class GlideTraceData
                    ---@field start number

                    ---@class GlideWheelState
                    ---@field traceData GlideTraceData

                    local getTable = FindMetaTable("Entity").GetTable

                    function ENT:Initialize()
                        ---@type GlideWheelState
                        self.state = {
                            traceData = {
                                start = 1,
                            },
                        }
                    end

                    function ENT:Think(selfTbl)
                        selfTbl = selfTbl or getTable(self)
                        return selfTbl.sta<??>te
                    end
                "#,
        ))?;
        let file_id = ws.def_file("cityrp/entities/entities/glide_wheel/init.lua", &content);
        let hover = crate::handlers::hover::hover(&ws.analysis, file_id, position, None)
            .ok_or("expected hover")
            .or_fail()?;
        let HoverContents::Markup(markup) = hover.contents else {
            return fail!("expected HoverContents::Markup");
        };

        assert!(
            markup.value.contains("GlideWheelState"),
            "expected state hover to keep GlideWheelState, got: {}",
            markup.value
        );
        assert!(
            !markup.value.contains(": any"),
            "expected state hover to avoid any, got: {}",
            markup.value
        );

        Ok(())
    }

    /// Hovering the `function` keyword in a hook.Add callback should show the anonymous callback
    /// signature (e.g. `function(ply: Player, seat: Vehicle) -> boolean`) and NOT the generic
    /// "The function keyword is used to define a function..." keyword docs.
    #[gtest]
    fn test_hover_hook_add_callback_function_keyword_shows_hook_signature() -> Result<()> {
        let mut ws = ProviderVirtualWorkspace::new();
        let mut emmyrc = ws.get_emmyrc();
        emmyrc.gmod.enabled = true;
        ws.update_emmyrc(emmyrc);

        let (content, position) = ProviderVirtualWorkspace::handle_file_content(
            r#"
                ---@class Player
                ---@class Vehicle

                ---@class GM
                ---@type GM
                GM = GM or {}

                ---Called when a player tries to enter a vehicle.
                ---@param ply Player
                ---@param veh Vehicle
                ---@return boolean
                function GM:CanPlayerEnterVehicle(ply, veh) end

                hook = {}
                ---@param eventName string
                ---@param identifier any
                ---@param func function
                function hook.Add(eventName, identifier, func) end

                hook.Add("CanPlayerEnterVehicle", "test", fu<??>nction(ply, veh) end)
            "#,
        )?;
        let file_id = ws.def_file("gamemode/init.lua", &content);
        let hover = crate::handlers::hover::hover(&ws.analysis, file_id, position, None)
            .ok_or("expected hover")
            .or_fail()?;

        let HoverContents::Markup(markup) = hover.contents else {
            return fail!("expected HoverContents::Markup");
        };

        // Must NOT show keyword docs
        assert!(
            !markup.value.contains("The function keyword"),
            "hover should not show keyword docs for hook callback `function`, got: {}",
            markup.value
        );

        // Must show an anonymous callback signature with param types
        assert!(
            markup.value.contains("function("),
            "hover should show anonymous function signature, got: {}",
            markup.value
        );
        assert!(
            markup.value.contains("Player"),
            "hover should show Player param type in callback signature, got: {}",
            markup.value
        );
        assert!(
            markup.value.contains("Vehicle"),
            "hover should show Vehicle param type in callback signature, got: {}",
            markup.value
        );

        // Must show the hook description
        assert!(
            markup
                .value
                .contains("Called when a player tries to enter a vehicle"),
            "hover should include hook description, got: {}",
            markup.value
        );

        // Must show the return type from the hook's @return annotation
        assert!(
            markup.value.contains("-> boolean"),
            "hover should show `-> boolean` return type from hook annotation, got: {}",
            markup.value
        );

        Ok(())
    }

    /// Hovering the `function` keyword in a hook.Add callback should include the hook's return
    /// type in the anonymous signature (e.g. `function(...) -> boolean`).
    #[gtest]
    fn test_hover_hook_add_callback_function_keyword_includes_return_type() -> Result<()> {
        let mut ws = ProviderVirtualWorkspace::new();
        let mut emmyrc = ws.get_emmyrc();
        emmyrc.gmod.enabled = true;
        ws.update_emmyrc(emmyrc);

        let (content, position) = ProviderVirtualWorkspace::handle_file_content(
            r#"
                ---@class Player

                ---@class GM
                ---@type GM
                GM = GM or {}

                ---@param ply Player
                ---@return boolean
                function GM:PlayerConnect(ply) end

                hook = {}
                ---@param eventName string
                ---@param identifier any
                ---@param func function
                function hook.Add(eventName, identifier, func) end

                hook.Add("PlayerConnect", "test", fu<??>nction(ply) end)
            "#,
        )?;
        let file_id = ws.def_file("gamemode/init.lua", &content);
        let hover = crate::handlers::hover::hover(&ws.analysis, file_id, position, None)
            .ok_or("expected hover")
            .or_fail()?;

        let HoverContents::Markup(markup) = hover.contents else {
            return fail!("expected HoverContents::Markup");
        };

        assert!(
            markup.value.contains("-> boolean"),
            "hover should include return type `-> boolean` in anonymous callback signature, got: {}",
            markup.value
        );

        Ok(())
    }

    /// Regression: hovering the hook-name string in hook.Add should still work correctly.
    #[gtest]
    fn test_hover_hook_add_string_still_works_after_callback_hover_fix() -> Result<()> {
        let mut ws = ProviderVirtualWorkspace::new();
        let mut emmyrc = ws.get_emmyrc();
        emmyrc.gmod.enabled = true;
        ws.update_emmyrc(emmyrc);

        let (content, position) = ProviderVirtualWorkspace::handle_file_content(
            r#"
                ---@class GM
                ---@type GM
                GM = GM or {}

                ---@param ply Player
                ---@return boolean
                function GM:SomeHook(ply) end

                hook = {}
                ---@param eventName string
                ---@param identifier any
                ---@param func function
                function hook.Add(eventName, identifier, func) end

                hook.Add("SomeHo<??>ok", "test", function(ply) end)
            "#,
        )?;
        let file_id = ws.def_file("gamemode/init.lua", &content);
        let hover = crate::handlers::hover::hover(&ws.analysis, file_id, position, None)
            .ok_or("expected hover")
            .or_fail()?;

        let HoverContents::Markup(markup) = hover.contents else {
            return fail!("expected HoverContents::Markup");
        };

        assert!(
            markup.value.contains("SomeHook"),
            "hook-name hover should still show hook function signature, got: {}",
            markup.value
        );

        Ok(())
    }

    /// When `gmod.enabled` is false, hovering the `function` keyword in a hook.Add call
    /// should fall back to the generic keyword documentation.
    #[gtest]
    fn test_hover_hook_add_callback_function_keyword_fallback_when_gmod_disabled() -> Result<()> {
        let mut ws = ProviderVirtualWorkspace::new();
        let mut emmyrc = ws.get_emmyrc();
        emmyrc.gmod.enabled = false;
        ws.update_emmyrc(emmyrc);

        let (content, position) = ProviderVirtualWorkspace::handle_file_content(
            r#"
                ---@class GM
                ---@type GM
                GM = GM or {}

                ---@param ply Player
                ---@return boolean
                function GM:SomeHook(ply) end

                hook = {}
                ---@param eventName string
                ---@param identifier any
                ---@param func function
                function hook.Add(eventName, identifier, func) end

                hook.Add("SomeHook", "test", fu<??>nction(ply) end)
            "#,
        )?;
        let file_id = ws.def_file("gamemode/init.lua", &content);
        let hover = crate::handlers::hover::hover(&ws.analysis, file_id, position, None)
            .ok_or("expected hover")
            .or_fail()?;

        let HoverContents::Markup(markup) = hover.contents else {
            return fail!("expected HoverContents::Markup");
        };

        // With gmod disabled, should show generic keyword hover, not hook-specific hover.
        // The keyword docs contain the specific phrase from the locale file.
        assert!(
            markup
                .value
                .contains("The `function` keyword is used to define a function"),
            "expected generic keyword docs when gmod is disabled, got: {}",
            markup.value
        );
        // Should NOT show hook-specific anonymous signature (no param types)
        assert!(
            !markup.value.contains("Player"),
            "expected no hook-specific param types when gmod is disabled, got: {}",
            markup.value
        );

        Ok(())
    }

    /// A `function` keyword that is NOT inside a hook.Add callback (e.g. a standalone named
    /// function) should still show generic keyword docs even when `gmod.enabled = true`.
    #[gtest]
    fn test_hover_function_keyword_outside_hook_add_shows_keyword_docs() -> Result<()> {
        let mut ws = ProviderVirtualWorkspace::new();
        let mut emmyrc = ws.get_emmyrc();
        emmyrc.gmod.enabled = true;
        ws.update_emmyrc(emmyrc);

        let (content, position) = ProviderVirtualWorkspace::handle_file_content(
            r#"
                fu<??>nction standalone()
                end
            "#,
        )?;
        let file_id = ws.def_file("gamemode/init.lua", &content);
        let hover = crate::handlers::hover::hover(&ws.analysis, file_id, position, None)
            .ok_or("expected hover")
            .or_fail()?;

        let HoverContents::Markup(markup) = hover.contents else {
            return fail!("expected HoverContents::Markup");
        };

        assert!(
            markup
                .value
                .contains("The `function` keyword is used to define a function"),
            "standalone `function` keyword should show generic keyword docs, got: {}",
            markup.value
        );

        Ok(())
    }

    /// Hovering the `function` keyword in hook.Add where the hook name is not registered in any
    /// GM/GAMEMODE table should fall back to generic keyword docs (not crash or return no hover).
    #[gtest]
    fn test_hover_hook_add_callback_function_keyword_unregistered_hook_falls_back_to_keyword_docs()
    -> Result<()> {
        let mut ws = ProviderVirtualWorkspace::new();
        let mut emmyrc = ws.get_emmyrc();
        emmyrc.gmod.enabled = true;
        ws.update_emmyrc(emmyrc);

        let (content, position) = ProviderVirtualWorkspace::handle_file_content(
            r#"
                hook = {}
                function hook.Add(eventName, identifier, func) end

                -- "NonExistentHook" is not defined on GM/GAMEMODE, so no hook doc is available.
                hook.Add("NonExistentHook", "test", fu<??>nction(ply) end)
            "#,
        )?;
        let file_id = ws.def_file("gamemode/init.lua", &content);
        // When the hook is not registered, hover_gmod_hook_callback_function returns None and
        // the dispatch falls through to the generic keyword hover — always Some(...).
        let hover = crate::handlers::hover::hover(&ws.analysis, file_id, position, None)
            .ok_or("expected keyword fallback hover for unregistered hook")
            .or_fail()?;

        let HoverContents::Markup(markup) = hover.contents else {
            return fail!("expected HoverContents::Markup");
        };
        // Should show generic keyword docs, not hook-specific content.
        assert!(
            markup
                .value
                .contains("The `function` keyword is used to define a function"),
            "unregistered hook should show generic keyword docs, got: {}",
            markup.value
        );
        Ok(())
    }

    /// A hook declared with only `@return` and no `@param` annotations must still show
    /// the return type in the anonymous callback signature (e.g. `function() -> boolean`).
    /// Previously `filter_signature_type` would skip the signature when `param_docs` is empty,
    /// silently degrading return-only hooks to keyword docs.
    #[gtest]
    fn test_hover_hook_add_callback_function_keyword_return_only_hook_shows_return_type()
    -> Result<()> {
        let mut ws = ProviderVirtualWorkspace::new();
        let mut emmyrc = ws.get_emmyrc();
        emmyrc.gmod.enabled = true;
        ws.update_emmyrc(emmyrc);

        let (content, position) = ProviderVirtualWorkspace::handle_file_content(
            r#"
                ---@class GM
                ---@type GM
                GM = GM or {}

                ---@return boolean
                function GM:ReturnOnlyHook() end

                hook = {}
                ---@param eventName string
                ---@param identifier any
                ---@param func function
                function hook.Add(eventName, identifier, func) end

                hook.Add("ReturnOnlyHook", "test", fu<??>nction() end)
            "#,
        )?;
        let file_id = ws.def_file("gamemode/init.lua", &content);
        let hover = crate::handlers::hover::hover(&ws.analysis, file_id, position, None)
            .ok_or("expected hover for return-only hook")
            .or_fail()?;

        let HoverContents::Markup(markup) = hover.contents else {
            return fail!("expected HoverContents::Markup");
        };
        assert!(
            markup.value.contains("-> boolean"),
            "return-only hook should show `-> boolean` in anonymous callback signature, got: {}",
            markup.value
        );
        assert!(
            !markup
                .value
                .contains("The `function` keyword is used to define a function"),
            "should not fall back to generic keyword docs, got: {}",
            markup.value
        );
        Ok(())
    }

    fn enable_gmod_workspace() -> ProviderVirtualWorkspace {
        let mut ws = ProviderVirtualWorkspace::new();
        let mut emmyrc = ws.get_emmyrc();
        emmyrc.gmod.enabled = true;
        ws.update_emmyrc(emmyrc);
        ws
    }

    #[gtest]
    fn test_hover_color_decl_includes_preview() -> Result<()> {
        let mut ws = ProviderVirtualWorkspace::new();
        let (content, position) = ProviderVirtualWorkspace::handle_file_content(
            r#"
                ---@class Color
                ---@return Color
                function Color(r, g, b, a) end

                local HEADLIGHT = Color(255, 231, 176)
                <??>HEADLIGHT
            "#,
        )?;
        let file_id = ws.def(&content);
        let value = extract_hover_markdown(&ws, file_id, position);

        verify_that!(
            value,
            contains_substring("local HEADLIGHT: Color(255, 231, 176)")
        )?;
        // The description region now carries an inline SVG swatch image plus the
        // Color(...) text. The type line above remains plain text.
        verify_that!(value, contains_substring("data:image/svg+xml;base64,"))?;
        verify_that!(value, contains_substring("`Color(255, 231, 176)`"))?;
        Ok(())
    }

    #[gtest]
    fn test_hover_color_decl_swatch_suppressed_when_disabled() -> Result<()> {
        let mut ws = ProviderVirtualWorkspace::new();
        let mut emmyrc = ws.get_emmyrc();
        emmyrc.document_color.enable = false;
        ws.update_emmyrc(emmyrc);
        let (content, position) = ProviderVirtualWorkspace::handle_file_content(
            r#"
                ---@class Color
                ---@return Color
                function Color(r, g, b, a) end

                local HEADLIGHT = Color(255, 231, 176)
                <??>HEADLIGHT
            "#,
        )?;
        let file_id = ws.def(&content);
        let value = extract_hover_markdown(&ws, file_id, position);

        verify_that!(value, not(contains_substring("data:image/svg+xml;base64,")))?;
        Ok(())
    }

    #[gtest]
    fn test_hover_color_member_includes_preview_with_alpha() -> Result<()> {
        let mut ws = ProviderVirtualWorkspace::new();
        let (content, position) = ProviderVirtualWorkspace::handle_file_content(
            r#"
                ---@class Color
                ---@return Color
                function Color(r, g, b, a) end

                SKIN = {}
                SKIN.HeaderColor = Color(20, 40, 60, 128)
                SKIN.<??>HeaderColor
            "#,
        )?;
        let file_id = ws.def(&content);
        let value = extract_hover_markdown(&ws, file_id, position);

        verify_that!(
            value,
            contains_substring("(field) HeaderColor: Color(20, 40, 60, 128)")
        )?;
        verify_that!(value, contains_substring("data:image/svg+xml;base64,"))?;
        verify_that!(value, contains_substring("`Color(20, 40, 60, 128)`"))?;
        Ok(())
    }

    fn extract_hover_markdown(
        ws: &ProviderVirtualWorkspace,
        file_id: glua_code_analysis::FileId,
        position: lsp_types::Position,
    ) -> String {
        let hover = crate::handlers::hover::hover(&ws.analysis, file_id, position, None)
            .expect("expected hover");
        let HoverContents::Markup(markup) = hover.contents else {
            panic!("expected HoverContents::Markup");
        };
        markup.value
    }

    #[gtest]
    fn test_hover_guarded_global_table_field_when_child_indexes_first() -> Result<()> {
        let mut ws = ProviderVirtualWorkspace::new();
        let (child_content, position) = ProviderVirtualWorkspace::handle_file_content(
            r#"
                marauth = marauth or {}
                marauth.character = marauth.character or {}

                function marauth.character:Create()
                end

                local util = marauth.<??>util
            "#,
        )?;

        let file_ids = ws.def_files(vec![
            (
                "gamemodes/test/gamemode/sh_test1.lua",
                child_content.as_str(),
            ),
            (
                "gamemodes/test_base/gamemode/sh_test1.lua",
                r#"
                    marauth = marauth or {}
                    marauth.util = marauth.util or {}

                    function marauth.util:BaseFunction()
                    end
                "#,
            ),
        ]);
        let child_file = file_ids[0];
        let value = extract_hover_markdown(&ws, child_file, position);

        assert!(
            !value.contains("(field) util: nil"),
            "guarded global table field must not hover as nil, got: {value}"
        );
        assert!(
            value.contains("BaseFunction"),
            "expected hover to include the merged util table method, got: {value}"
        );

        Ok(())
    }

    #[gtest]
    fn test_hover_net_message_on_net_start_shows_send_and_receive_patterns() -> Result<()> {
        let mut ws = enable_gmod_workspace();

        ws.def_file(
            "lua/autorun/client/recv.lua",
            r#"
                net.Receive("MyMessage", function()
                    local id = net.ReadUInt(16)
                    local name = net.ReadString()
                end)
            "#,
        );

        let (content, position) = ProviderVirtualWorkspace::handle_file_content(
            r#"
                util.AddNetworkString("MyMessage")
                net.Start("MyMes<??>sage")
                net.WriteUInt(1, 16)
                net.WriteString("hello")
                net.Send(Entity(1))
            "#,
        )?;
        let file_id = ws.def_file("lua/autorun/server/send.lua", &content);
        let value = extract_hover_markdown(&ws, file_id, position);

        assert!(
            value.contains("(net) \"MyMessage\""),
            "expected typed header, got: {value}"
        );
        assert!(value.contains("**Senders**"), "got: {value}");
        assert!(value.contains("**Receivers**"), "got: {value}");
        assert!(value.contains("net.WriteUInt(1, 16)"), "got: {value}");
        assert!(value.contains("net.WriteString(\"hello\")"), "got: {value}");
        assert!(value.contains("net.ReadUInt(16)"), "got: {value}");
        assert!(value.contains("net.ReadString"), "got: {value}");
        // File names should appear as clickable links pointing to a line.
        assert!(
            value.contains("send.lua") && value.contains("recv.lua"),
            "expected file names in links, got: {value}"
        );
        assert!(
            value.contains("#L"),
            "expected #L<line> in clickable links, got: {value}"
        );
        Ok(())
    }

    #[gtest]
    fn test_hover_net_message_on_net_receive_shows_info() -> Result<()> {
        let mut ws = enable_gmod_workspace();

        ws.def_file(
            "lua/autorun/server/send.lua",
            r#"
                util.AddNetworkString("Ping")
                net.Start("Ping")
                net.WriteString("hi")
                net.Send(Entity(1))
            "#,
        );

        let (content, position) = ProviderVirtualWorkspace::handle_file_content(
            r#"
                net.Receive("Pin<??>g", function()
                    local s = net.ReadString()
                end)
            "#,
        )?;
        let file_id = ws.def_file("lua/autorun/client/recv.lua", &content);
        let value = extract_hover_markdown(&ws, file_id, position);

        assert!(
            value.contains("(net) \"Ping\""),
            "expected typed header, got: {value}"
        );
        assert!(value.contains("**Senders**"), "got: {value}");
        assert!(value.contains("**Receivers**"), "got: {value}");
        assert!(value.contains("net.WriteString"), "got: {value}");
        assert!(value.contains("net.ReadString"), "got: {value}");
        Ok(())
    }

    #[gtest]
    fn test_hover_net_message_on_util_add_network_string_shows_info() -> Result<()> {
        let mut ws = enable_gmod_workspace();

        ws.def_file(
            "lua/autorun/server/send.lua",
            r#"
                net.Start("Registered")
                net.WriteFloat(0.5)
                net.Send(Entity(1))
            "#,
        );

        let (content, position) = ProviderVirtualWorkspace::handle_file_content(
            r#"
                util.AddNetworkString("Regis<??>tered")
            "#,
        )?;
        let file_id = ws.def_file("lua/autorun/server/init.lua", &content);
        let value = extract_hover_markdown(&ws, file_id, position);

        assert!(
            value.contains("(net) \"Registered\""),
            "expected typed header, got: {value}"
        );
        assert!(value.contains("**Senders**"), "got: {value}");
        assert!(value.contains("net.WriteFloat"), "got: {value}");
        Ok(())
    }

    #[gtest]
    fn test_hover_net_message_groups_distinct_send_patterns() -> Result<()> {
        let mut ws = enable_gmod_workspace();

        ws.def_file(
            "lua/autorun/server/send_a.lua",
            r#"
                net.Start("MultiPattern")
                net.WriteUInt(1, 16)
                net.WriteString("a")
                net.Send(Entity(1))
            "#,
        );
        ws.def_file(
            "lua/autorun/server/send_b.lua",
            r#"
                net.Start("MultiPattern")
                net.WriteFloat(0.5)
                net.Send(Entity(1))
            "#,
        );

        let (content, position) = ProviderVirtualWorkspace::handle_file_content(
            r#"
                net.Receive("MultiPa<??>ttern", function() end)
            "#,
        )?;
        let file_id = ws.def_file("lua/autorun/client/recv.lua", &content);
        let value = extract_hover_markdown(&ws, file_id, position);

        assert!(
            value.contains("net.WriteUInt"),
            "expected first pattern entry, got: {value}"
        );
        assert!(
            value.contains("net.WriteString"),
            "expected first pattern entry, got: {value}"
        );
        assert!(
            value.contains("net.WriteFloat"),
            "expected second pattern entry, got: {value}"
        );
        // Two distinct send patterns should produce a multi-pattern Senders section.
        assert!(
            value.contains("across 2 patterns"),
            "expected multi-pattern label, got: {value}"
        );
        assert!(
            value.contains("Pattern A") && value.contains("Pattern B"),
            "expected Pattern A and B labels, got: {value}"
        );
        Ok(())
    }

    #[gtest]
    fn test_hover_net_message_marks_dynamic_writes() -> Result<()> {
        let mut ws = enable_gmod_workspace();

        let (content, position) = ProviderVirtualWorkspace::handle_file_content(
            r#"
                net.Start("DynLoop<??>")
                for _ = 1, 3 do
                    net.WriteString("x")
                end
                net.Send(Entity(1))
            "#,
        )?;
        let file_id = ws.def_file("lua/autorun/server/send.lua", &content);
        let value = extract_hover_markdown(&ws, file_id, position);

        assert!(
            value.contains("net.WriteString")
                && value.contains("for _ = 1, 3 do")
                && value.contains("end"),
            "expected WriteString nested under the `for ... do` source frame, got: {value}"
        );
        Ok(())
    }

    #[gtest]
    fn test_hover_net_message_nested_loops_via_named_callback() -> Result<()> {
        let mut ws = enable_gmod_workspace();

        let (content, position) = ProviderVirtualWorkspace::handle_file_content(
            r#"
                local function InitVars(len)
                    local plyCount = net.ReadUInt(8)
                    for i = 1, plyCount, 1 do
                        local userID = net.ReadUInt(16)
                        local varCount = net.ReadUInt(8)
                        for j = 1, varCount, 1 do
                            local v = net.ReadString()
                        end
                    end
                end
                net.Receive("NestedVars<??>", InitVars)
            "#,
        )?;
        let file_id = ws.def_file("lua/autorun/client/init.lua", &content);
        let value = extract_hover_markdown(&ws, file_id, position);

        assert!(
            value.contains("net.ReadUInt") && value.contains("net.ReadString"),
            "expected reads from both loop levels, got: {value}"
        );
        assert!(
            value.contains("for i = 1, plyCount, 1 do")
                && value.contains("for j = 1, varCount, 1 do"),
            "expected both for-loop headers, got: {value}"
        );
        Ok(())
    }

    #[gtest]
    fn test_hover_net_message_helper_call_inherits_outer_flow() -> Result<()> {
        let mut ws = enable_gmod_workspace();

        let (content, position) = ProviderVirtualWorkspace::handle_file_content(
            r#"
                local function readPair()
                    local k = net.ReadString()
                    local v = net.ReadString()
                end
                local function recv(len)
                    local n = net.ReadUInt(8)
                    for i = 1, n, 1 do
                        readPair()
                    end
                end
                net.Receive("HelperLoop<??>", recv)
            "#,
        )?;
        let file_id = ws.def_file("lua/autorun/client/helper.lua", &content);
        let value = extract_hover_markdown(&ws, file_id, position);

        // Reads inside readPair() should appear inside a Lua code fence that
        // follows a styled scope-open row for the outer for-loop — the
        // helper-recursion flow_path-prefix fix carries the call site's loop
        // into the helper body's reads.
        assert!(
            value.contains("net.ReadString") && value.contains("for i = 1, n, 1 do"),
            "expected ReadString and outer for-loop header both rendered, got: {value}"
        );
        let Some(header_idx) = value.find("for i = 1, n, 1 do") else {
            panic!("expected for-loop scope row, got: {value}");
        };
        let Some(read_idx) = value.find("net.ReadString") else {
            panic!("expected ReadString rendered, got: {value}");
        };
        assert!(
            read_idx > header_idx,
            "expected ReadString rendered after the for-loop scope-open row, got: {value}"
        );
        Ok(())
    }

    #[gtest]
    fn test_hover_net_message_no_counterpart_indexed_message() -> Result<()> {
        let mut ws = enable_gmod_workspace();

        let (content, position) = ProviderVirtualWorkspace::handle_file_content(
            r#"
                util.AddNetworkString("Lonely<??>")
            "#,
        )?;
        let file_id = ws.def_file("lua/autorun/server/init.lua", &content);
        let value = extract_hover_markdown(&ws, file_id, position);

        assert!(
            value.contains("(net) \"Lonely\""),
            "expected typed header even with no usages, got: {value}"
        );
        assert!(
            value.contains("no recorded usages") || value.contains("No payload patterns indexed"),
            "expected an empty-state hint, got: {value}"
        );
        Ok(())
    }

    #[gtest]
    fn test_hover_net_message_does_not_trigger_for_unrelated_string() -> Result<()> {
        let mut ws = enable_gmod_workspace();

        let (content, position) = ProviderVirtualWorkspace::handle_file_content(
            r#"
                local x = "Hel<??>lo"
            "#,
        )?;
        let file_id = ws.def_file("lua/autorun/server/init.lua", &content);
        let hover = crate::handlers::hover::hover(&ws.analysis, file_id, position, None);
        if let Some(hover) = hover {
            let HoverContents::Markup(markup) = hover.contents else {
                return Ok(());
            };
            assert!(
                !markup.value.contains("(net)"),
                "unrelated string hover should not show net-message info, got: {}",
                markup.value
            );
        }
        Ok(())
    }

    #[gtest]
    fn test_hover_branched_dynamic_field_unions_vector_real_shape() -> Result<()> {
        // Repro of cityrp-vehicle-base/init.lua bug:
        //   if exitPos then
        //       seat.GlideExitPos = Vector(...)
        //   else
        //       seat.GlideExitPos = nil
        //   end
        // Reading `seat.GlideExitPos` later must hover as `Vector?` (i.e. Vector|nil),
        // NOT bare `nil`. Pre-fix, `retain_only_member_for_owner_key` dropped the
        // Vector-branch member because the `= nil` branch ran later.
        let mut ws = enable_gmod_workspace();
        let mut emmyrc = ws.get_emmyrc();
        emmyrc.gmod.infer_dynamic_fields = true;
        emmyrc.gmod.scripted_class_scopes.include = vec![legacy_scope("entities/**")];
        ws.update_emmyrc(emmyrc);

        let (content, position) = ProviderVirtualWorkspace::handle_file_content(
            r#"
                ---@class Vector
                local Vector = {}
                ---@param x? number
                ---@param y? number
                ---@param z? number
                ---@return Vector
                function _G.Vector(x, y, z) end

                ---@param ent any
                ---@return boolean
                function _G.IsValid(ent) end

                ---@class NULL
                NULL = {}

                _G.ents = {}
                ---@generic T : Entity
                ---@param class `T`
                ---@return T|NULL
                function _G.ents.Create(class) end

                ---@class Entity
                local Entity = {}

                function ENT:CreateSeat(exitPos)
                    local seat = ents.Create("prop_vehicle_prisoner_pod")
                    if not IsValid(seat) then return end
                    if exitPos then
                        seat.GlideExitPos = Vector(exitPos[1], exitPos[2], exitPos[3])
                    else
                        seat.GlideExitPos = nil
                    end
                end

                function ENT:ReadSeat()
                    local seat = ents.Create("prop_vehicle_prisoner_pod")
                    if not IsValid(seat) then return end
                    local v = seat.Glide<??>ExitPos
                    return v
                end
            "#,
        )?;
        let file_id = ws.def_file("lua/entities/base_glide/init.lua", &content);
        let value = extract_hover_markdown(&ws, file_id, position);

        let has_vector = value.contains("Vector");
        let has_nil_marker = value.contains("nil") || value.contains('?');
        assert!(
            has_vector && has_nil_marker,
            "expected hover on read site to include Vector + nil marker, got: {value}"
        );
        Ok(())
    }

    /// Hover at the LHS of `seat.GlideExitPos = nil` (the offending line the
    /// user is hovering on). Pre-fix this displayed `(field) GlideExitPos: nil`
    /// because `get_hover_type` rewrote the displayed type to the RHS being
    /// assigned. Post-fix, `lhs_indexed_member_union_type` recovers the
    /// field's full union from the branched assignments so the hover correctly
    /// shows `Vector?` (i.e. `Vector | nil`).
    #[gtest]
    fn test_hover_branched_dynamic_field_lhs_assign_does_not_collapse_field_type() -> Result<()> {
        let mut ws = enable_gmod_workspace();
        let mut emmyrc = ws.get_emmyrc();
        emmyrc.gmod.infer_dynamic_fields = true;
        emmyrc.gmod.scripted_class_scopes.include = vec![legacy_scope("entities/**")];
        ws.update_emmyrc(emmyrc);

        // Hover directly on the `GlideExitPos` token in the `= nil` branch.
        let (content, position) = ProviderVirtualWorkspace::handle_file_content(
            r#"
                ---@class Vector
                local Vector = {}
                ---@param x? number
                ---@param y? number
                ---@param z? number
                ---@return Vector
                function _G.Vector(x, y, z) end

                ---@param ent any
                ---@return boolean
                function _G.IsValid(ent) end

                ---@class NULL
                NULL = {}

                _G.ents = {}
                ---@generic T : Entity
                ---@param class `T`
                ---@return T|NULL
                function _G.ents.Create(class) end

                ---@class Entity
                local Entity = {}

                function ENT:CreateSeat(exitPos)
                    local seat = ents.Create("prop_vehicle_prisoner_pod")
                    if not IsValid(seat) then return end
                    if exitPos then
                        seat.GlideExitPos = Vector(exitPos[1], exitPos[2], exitPos[3])
                    else
                        seat.Glide<??>ExitPos = nil
                    end
                end
            "#,
        )?;
        let file_id = ws.def_file("lua/entities/base_glide/init.lua", &content);
        let value = extract_hover_markdown(&ws, file_id, position);

        // Post-fix the LHS hover must show the field's full union, not bare
        // `nil` or `never`. Accept either `Vector?` rendering or explicit
        // `Vector | nil` / `Vector|nil`.
        assert!(
            value.contains("GlideExitPos"),
            "hover should include field name, got: {value}"
        );
        assert!(
            value.contains("Vector"),
            "LHS hover must include `Vector` from the other branch, got: {value}"
        );
        let has_nil_marker = value.contains("Vector?")
            || value.contains("Vector | nil")
            || value.contains("Vector|nil")
            || value.contains("nil");
        assert!(
            has_nil_marker,
            "LHS hover must include a nil marker (`?` or `| nil`), got: {value}"
        );
        assert!(
            !value.contains("GlideExitPos: nil") && !value.contains("GlideExitPos: never"),
            "LHS hover must not collapse to bare `nil` or `never`, got: {value}"
        );
        Ok(())
    }

    /// Read-site hover where the entity local comes from `self.seats[index]`
    /// in a different method than the branched assignment. Mirrors
    /// `cityrp-vehicle-base/init.lua:559` (`local seat = self.seats[index]`
    /// then `seat.GlideExitPos[1]`). Failing red test pre-fix produced bare
    /// `nil` or `never` because the branched dynamic-field assignment
    /// collapsed to the last `= nil` branch.
    #[gtest]
    fn test_hover_branched_dynamic_field_read_via_self_seats_array() -> Result<()> {
        let mut ws = enable_gmod_workspace();
        let mut emmyrc = ws.get_emmyrc();
        emmyrc.gmod.infer_dynamic_fields = true;
        emmyrc.gmod.scripted_class_scopes.include = vec![legacy_scope("entities/**")];
        ws.update_emmyrc(emmyrc);

        let (content, position) = ProviderVirtualWorkspace::handle_file_content(
            r#"
                ---@class Vector
                local Vector = {}
                ---@param x? number
                ---@param y? number
                ---@param z? number
                ---@return Vector
                function _G.Vector(x, y, z) end

                ---@param ent any
                ---@return boolean
                function _G.IsValid(ent) end

                ---@class NULL
                NULL = {}

                _G.ents = {}
                ---@generic T : Entity
                ---@param class `T`
                ---@return T|NULL
                function _G.ents.Create(class) end

                ---@class Entity
                local Entity = {}

                ---@class base_glide
                ---@field seats prop_vehicle_prisoner_pod[]
                local ENTCLASS = {}

                function ENT:CreateSeat(exitPos, index)
                    local seat = ents.Create("prop_vehicle_prisoner_pod")
                    if not IsValid(seat) then return end
                    if exitPos then
                        seat.GlideExitPos = Vector(exitPos[1], exitPos[2], exitPos[3])
                    else
                        seat.GlideExitPos = nil
                    end
                    self.seats[index] = seat
                end

                function ENT:GetSeatExitPos(index)
                    local seat = self.seats[index]
                    if not IsValid(seat) then return end
                    return seat.Glide<??>ExitPos
                end
            "#,
        )?;
        let file_id = ws.def_file("lua/entities/base_glide/init.lua", &content);
        let value = extract_hover_markdown(&ws, file_id, position);

        let has_vector = value.contains("Vector");
        let has_nil_marker = value.contains("nil") || value.contains('?');
        assert!(
            has_vector && has_nil_marker,
            "real-shape read via self.seats[i] must hover Vector|nil, got: {value}"
        );
        Ok(())
    }
}