gdstyle 0.1.7

A fast, opinionated linter and formatter for GDScript (Godot 4.x)
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
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
use gdstyle::ast::ClassMember;
use gdstyle::config::Config;
use gdstyle::fixer;
use gdstyle::formatter;
use gdstyle::lexer::Lexer;
use gdstyle::linter;
use gdstyle::parser::Parser as GdParser;
use std::path::Path;

fn parse_members_for_test(source: &str) -> Vec<ClassMember> {
    let mut lexer = Lexer::new(source);
    let tokens = lexer.tokenize();
    GdParser::new(&tokens).parse()
}

fn default_config() -> Config {
    Config::default()
}

fn fixture_path(name: &str) -> std::path::PathBuf {
    Path::new(env!("CARGO_MANIFEST_DIR"))
        .join("tests")
        .join("fixtures")
        .join(name)
}

// --- End-to-end integration tests ---

#[test]
fn clean_script_produces_no_diagnostics() {
    let config = default_config();
    let diagnostics = linter::lint_file(&fixture_path("clean_script.gd"), &config).unwrap();
    assert!(
        diagnostics.is_empty(),
        "clean_script.gd should produce no diagnostics, got:\n{}",
        diagnostics
            .iter()
            .map(|d| format!("  line {}: [{}] {}", d.span.line, d.rule, d.message))
            .collect::<Vec<_>>()
            .join("\n")
    );
}

#[test]
fn bad_naming_detects_all_violations() {
    let config = default_config();
    let diagnostics = linter::lint_file(&fixture_path("bad_naming.gd"), &config).unwrap();

    let rule_names: Vec<&str> = diagnostics.iter().map(|d| d.rule.as_str()).collect();

    assert!(
        rule_names.contains(&"naming/class-name-pascal-case"),
        "should detect bad class name"
    );
    assert!(
        rule_names.contains(&"naming/signal-name-snake-case"),
        "should detect bad signal name"
    );
    assert!(
        rule_names.contains(&"naming/enum-name-pascal-case"),
        "should detect bad enum name"
    );
    assert!(
        rule_names.contains(&"naming/enum-member-screaming-case"),
        "should detect bad enum members"
    );
    assert!(
        rule_names.contains(&"naming/constant-name-screaming-case"),
        "should detect bad constant name"
    );
    assert!(
        rule_names.contains(&"naming/variable-name-snake-case"),
        "should detect bad variable name"
    );
    assert!(
        rule_names.contains(&"naming/function-name-snake-case"),
        "should detect bad function name"
    );
}

#[test]
fn bad_formatting_detects_violations() {
    let config = default_config();
    let diagnostics = linter::lint_file(&fixture_path("bad_formatting.gd"), &config).unwrap();

    let rule_names: Vec<&str> = diagnostics.iter().map(|d| d.rule.as_str()).collect();

    assert!(
        rule_names.contains(&"format/number-literals"),
        "should detect uppercase hex"
    );
    assert!(
        rule_names.contains(&"format/boolean-operators"),
        "should detect && and ||"
    );
    assert!(
        rule_names.contains(&"format/double-quotes"),
        "should detect single quotes"
    );
    assert!(
        rule_names.contains(&"format/one-statement-per-line"),
        "should detect semicolon"
    );
}

#[test]
fn bad_ordering_detects_violations() {
    let config = default_config();
    let diagnostics = linter::lint_file(&fixture_path("bad_ordering.gd"), &config).unwrap();

    let rule_names: Vec<&str> = diagnostics.iter().map(|d| d.rule.as_str()).collect();

    assert!(
        rule_names.contains(&"order/class-member-order"),
        "should detect ordering violations"
    );
}

#[test]
fn suppression_comments_work() {
    let config = default_config();
    let diagnostics = linter::lint_file(&fixture_path("suppressed.gd"), &config).unwrap();

    let variable_name_diags: Vec<_> = diagnostics
        .iter()
        .filter(|d| d.rule == "naming/variable-name-snake-case")
        .collect();

    assert_eq!(
        variable_name_diags.len(),
        1,
        "only YetAnother should trigger naming rule, got: {:?}",
        variable_name_diags
    );

    assert!(
        variable_name_diags[0].message.contains("YetAnother"),
        "the one diagnostic should be about YetAnother"
    );
}

#[test]
fn config_overrides_work() {
    let mut config = default_config();
    config.rules.insert(
        "naming/class-name-pascal-case".to_string(),
        gdstyle::config::RuleSeverityConfig::Off,
    );

    let diagnostics = linter::lint_file(&fixture_path("bad_naming.gd"), &config).unwrap();
    let class_name_diags: Vec<_> = diagnostics
        .iter()
        .filter(|d| d.rule == "naming/class-name-pascal-case")
        .collect();
    assert!(
        class_name_diags.is_empty(),
        "disabled rule should not produce diagnostics"
    );
}

#[test]
fn custom_line_length_config() {
    let mut config = default_config();
    config.max_line_length = 200;

    let source = format!("var x: String = \"{}\"\n", "a".repeat(150));
    let diagnostics = linter::lint_source(&source, "test.gd", &config);
    let line_length_diags: Vec<_> = diagnostics
        .iter()
        .filter(|d| d.rule == "format/max-line-length")
        .collect();
    assert!(line_length_diags.is_empty());
}

#[test]
fn json_output_format() {
    let config = default_config();
    let diagnostics = linter::lint_file(&fixture_path("bad_naming.gd"), &config).unwrap();

    let json = gdstyle::reporter::format_json(&diagnostics);
    let parsed: serde_json::Value = serde_json::from_str(&json).unwrap();
    assert!(parsed.is_array());
    assert!(!parsed.as_array().unwrap().is_empty());

    let first = &parsed[0];
    assert!(first.get("rule").is_some());
    assert!(first.get("message").is_some());
    assert!(first.get("severity").is_some());
    assert!(first.get("span").is_some());
    assert!(first.get("file").is_some());
}

#[test]
fn file_name_check_on_pascal_case_file() {
    let config = default_config();
    let source = "extends Node\n";
    let diagnostics = linter::lint_source(source, "PlayerController.gd", &config);
    assert!(
        diagnostics
            .iter()
            .any(|d| d.rule == "naming/file-name-snake-case"),
        "should flag PascalCase filename"
    );
}

#[test]
fn lint_multiple_files_independently() {
    let config = default_config();

    let clean = linter::lint_file(&fixture_path("clean_script.gd"), &config).unwrap();
    let bad = linter::lint_file(&fixture_path("bad_naming.gd"), &config).unwrap();

    assert!(clean.is_empty());
    assert!(!bad.is_empty());
}

// --- Regression tests ---

#[test]
fn blank_lines_not_counted_inside_function_bodies() {
    // Regression: check_blank_lines used the start line of the previous member
    // instead of its end line, causing it to count blank lines inside function
    // bodies as if they were between top-level members.
    let source = r#"extends Node

func long_function():
	var a = 1

	var b = 2

	var c = 3

	var d = 4

	var e = 5

	return a + b + c + d + e

func next_function():
	pass
"#;
    let config = default_config();
    let diagnostics = linter::lint_source(source, "test.gd", &config);
    let blank_line_diags: Vec<_> = diagnostics
        .iter()
        .filter(|d| d.rule == "format/blank-lines")
        .collect();
    assert!(
        blank_line_diags.is_empty(),
        "blank lines inside function bodies should not trigger format/blank-lines, got: {:?}",
        blank_line_diags
            .iter()
            .map(|d| format!("line {}: {}", d.span.line, d.message))
            .collect::<Vec<_>>()
    );
}

#[test]
fn blank_lines_between_functions_still_detected() {
    // Ensure the blank-lines rule still catches actual violations between
    // top-level members (3+ blank lines).
    let source = "extends Node\n\n\nvar x = 1\n\n\n\n\nvar y = 2\n";
    let config = default_config();
    let diagnostics = linter::lint_source(source, "test.gd", &config);
    assert!(
        diagnostics.iter().any(|d| d.rule == "format/blank-lines"),
        "should still detect too many blank lines between top-level members"
    );
}

#[test]
fn operator_spacing_correct_after_multibyte_utf8() {
    // Regression: the lexer tracked character index instead of byte offset,
    // so after a multi-byte character (e.g., em dash U+2014, 3 bytes) all
    // subsequent span offsets were wrong, causing false operator-spacing warnings.
    let source = "extends Node\n\n# Comment with em dash \u{2014} here\n\nfunc test(a: bool):\n\tif a and true != null:\n\t\tpass\n";
    let config = default_config();
    let diagnostics = linter::lint_source(source, "test.gd", &config);
    let spacing_diags: Vec<_> = diagnostics
        .iter()
        .filter(|d| d.rule == "format/operator-spacing")
        .collect();
    assert!(
        spacing_diags.is_empty(),
        "correctly spaced operators after multi-byte UTF-8 should not trigger warnings, got: {:?}",
        spacing_diags
            .iter()
            .map(|d| format!("line {}:{}: {}", d.span.line, d.span.column, d.message))
            .collect::<Vec<_>>()
    );
}

#[test]
fn no_panic_on_string_after_multibyte_utf8() {
    // Regression: source slicing in read_string used char index instead of
    // byte offset, causing a panic when a string literal appeared after a
    // multi-byte UTF-8 character (e.g., em dash).
    let source = "extends Node\n\n# Em dash \u{2014} here\n\nvar x = \"hello\"\n";
    let config = default_config();
    // Should not panic.
    let _diagnostics = linter::lint_source(source, "test.gd", &config);
}

#[test]
fn operator_spacing_still_detected_after_multibyte_utf8() {
    // Ensure operator-spacing still catches real violations after multi-byte chars.
    let source = "extends Node\n\n# Comment with em dash \u{2014} here\n\nfunc test(a: int, b: int):\n\tvar x = a+b\n";
    let config = default_config();
    let diagnostics = linter::lint_source(source, "test.gd", &config);
    assert!(
        diagnostics
            .iter()
            .any(|d| d.rule == "format/operator-spacing"),
        "should still detect missing spaces around + after multi-byte UTF-8"
    );
}

#[test]
fn static_var_screaming_case_accepted() {
    // Regression: static var with SCREAMING_SNAKE_CASE was flagged as a naming
    // violation and the auto-fix corrupted the line.
    let source = "static var THOUGHT_POIGNANCY: int = 5\nstatic var MY_CONSTANT: String = \"x\"\n";
    let config = default_config();
    let diagnostics = linter::lint_source(source, "test.gd", &config);
    let naming_diags: Vec<_> = diagnostics
        .iter()
        .filter(|d| d.rule == "naming/variable-name-snake-case")
        .collect();
    assert!(
        naming_diags.is_empty(),
        "static var with SCREAMING_SNAKE_CASE should be accepted, got: {:?}",
        naming_diags.iter().map(|d| &d.message).collect::<Vec<_>>()
    );
}

#[test]
fn to_snake_case_no_double_underscores() {
    // Regression: to_snake_case("THOUGHT_POIGNANCY") produced "thought__poignancy".
    let source = "var MyPascalVar: int = 1\n";
    let config = default_config();
    let diagnostics = linter::lint_source(source, "test.gd", &config);
    let fix_diag = diagnostics
        .iter()
        .find(|d| d.rule == "naming/variable-name-snake-case");
    assert!(fix_diag.is_some());
    let msg = &fix_diag.unwrap().message;
    assert!(
        !msg.contains("__"),
        "suggested name should not contain double underscores, got: {}",
        msg
    );
}

#[test]
fn max_line_length_counts_tabs_at_visual_width() {
    // Tabs should be counted at 4-column width, not 1 byte.
    // A tab + 97 chars = 101 visual columns (exceeds 100).
    // A tab + 96 chars = 100 visual columns (exactly at limit).
    let config = default_config();

    let at_limit = format!("\t{}\n", "a".repeat(96));
    let diags = linter::lint_source(&at_limit, "test.gd", &config);
    assert!(
        !diags.iter().any(|d| d.rule == "format/max-line-length"),
        "tab + 96 chars = 100 visual columns, should not trigger"
    );

    let over_limit = format!("\t{}\n", "a".repeat(97));
    let diags = linter::lint_source(&over_limit, "test.gd", &config);
    assert!(
        diags.iter().any(|d| d.rule == "format/max-line-length"),
        "tab + 97 chars = 101 visual columns, should trigger"
    );
}

// --- Fix tests ---

#[test]
fn fix_trailing_whitespace() {
    let source = "var x = 5   \n";
    let config = default_config();
    let diagnostics = linter::lint_source(source, "test.gd", &config);
    let fixed = fixer::apply_fixes(source, &diagnostics, true);
    assert_eq!(fixed, "var x = 5\n");
}

#[test]
fn fix_boolean_operators() {
    let source = "if a && b:\n\tpass\n";
    let config = default_config();
    let diagnostics = linter::lint_source(source, "test.gd", &config);
    let fixed = fixer::apply_fixes(source, &diagnostics, true);
    assert!(fixed.contains("and"), "should replace && with and");
    assert!(!fixed.contains("&&"), "should not contain &&");
}

#[test]
fn fix_double_quotes() {
    let source = "var x = 'hello'\n";
    let config = default_config();
    let diagnostics = linter::lint_source(source, "test.gd", &config);
    let fixed = fixer::apply_fixes(source, &diagnostics, true);
    assert!(
        fixed.contains("\"hello\""),
        "should replace single with double quotes"
    );
}

#[test]
fn fix_uppercase_hex() {
    let source = "var x = 0xFF\n";
    let config = default_config();
    let diagnostics = linter::lint_source(source, "test.gd", &config);
    let fixed = fixer::apply_fixes(source, &diagnostics, true);
    assert!(fixed.contains("0xff"), "should lowercase hex digits");
}

#[test]
fn fix_trailing_newline() {
    let source = "var x = 5";
    let config = default_config();
    let diagnostics = linter::lint_source(source, "test.gd", &config);
    let fixed = fixer::apply_fixes(source, &diagnostics, true);
    assert!(fixed.ends_with('\n'), "should add trailing newline");
}

#[test]
fn fix_preserves_safe_only() {
    let source = "signal health_change\n";
    let config = default_config();
    let diagnostics = linter::lint_source(source, "test.gd", &config);

    // Safe-only should NOT fix signal-past-tense (unsafe fix).
    let safe_fixed = fixer::apply_fixes(source, &diagnostics, true);
    assert!(
        safe_fixed.contains("health_change"),
        "safe-only should not rename signal"
    );

    // Unsafe mode should fix it (past tense: health_change -> health_changed).
    let unsafe_fixed = fixer::apply_fixes(source, &diagnostics, false);
    assert!(
        unsafe_fixed.contains("health_changed"),
        "unsafe fix should rename signal to past tense, got: {}",
        unsafe_fixed
    );
}

// --- Formatter tests ---

#[test]
fn formatter_clean_script_idempotent() {
    let source = std::fs::read_to_string(fixture_path("clean_script.gd")).unwrap();
    let config = default_config();
    let first = formatter::format_source(&source, &config);
    let second = formatter::format_source(&first, &config);
    assert_eq!(
        first, second,
        "formatter must be idempotent on clean script"
    );
}

#[test]
fn formatter_normalizes_quotes() {
    let source = "var x = 'hello'\n";
    let config = default_config();
    let formatted = formatter::format_source(source, &config);
    assert!(
        formatted.contains("\"hello\""),
        "formatter should normalize quotes"
    );
}

#[test]
fn formatter_normalizes_boolean_operators() {
    let source = "if a && b || !c:\n\tpass\n";
    let config = default_config();
    let formatted = formatter::format_source(source, &config);
    assert!(!formatted.contains("&&"), "should not contain &&");
    assert!(!formatted.contains("||"), "should not contain ||");
    assert!(formatted.contains("and"), "should contain 'and'");
    assert!(formatted.contains("or"), "should contain 'or'");
}

#[test]
fn formatter_strips_trailing_whitespace() {
    let source = "var x = 5   \nvar y = 10\t\n";
    let config = default_config();
    let formatted = formatter::format_source(source, &config);
    for line in formatted.lines() {
        assert!(
            !line.ends_with(' ') && !line.ends_with('\t'),
            "line should not have trailing whitespace: '{}'",
            line
        );
    }
}

#[test]
fn formatter_ensures_trailing_newline() {
    let source = "var x = 5";
    let config = default_config();
    let formatted = formatter::format_source(source, &config);
    assert!(formatted.ends_with('\n'), "should end with newline");
}

#[test]
fn formatter_collapses_blank_lines() {
    let source = "var x = 5\n\n\n\n\nvar y = 10\n";
    let config = default_config();
    let formatted = formatter::format_source(source, &config);
    // Should have at most 2 consecutive blank lines.
    assert!(
        !formatted.contains("\n\n\n\n"),
        "should collapse 4+ blank lines"
    );
}

#[test]
fn formatter_normalizes_hex() {
    let source = "var x = 0xFF\n";
    let config = default_config();
    let formatted = formatter::format_source(source, &config);
    assert!(formatted.contains("0xff"), "should lowercase hex digits");
}

// --- Regression tests for bugs found during GARP project testing ---

#[test]
fn formatter_does_not_rename_variables() {
    // Regression: naming fixes were marked is_safe=true and the formatter renamed
    // variables at their declaration site without updating all references, corrupting code.
    // Examples: `var CONFIG` -> `config CONFIG`, `var DialogueBoxClass` -> `dialogue_box_class DialogueBoxClass`
    let source = "@onready var CONFIG: ConfigFile = load(\"config\")\n";
    let config = default_config();
    let formatted = formatter::format_source(source, &config);
    assert!(
        formatted.contains("var CONFIG"),
        "formatter must not rename variable identifiers, got: {}",
        formatted.trim()
    );
}

#[test]
fn formatter_does_not_rename_static_var_pascal_case() {
    // Regression: `static var TogglePauseCommand` became
    // `toggle_pause_command toggle_pause_command TogglePauseCommand` (duplicate snake_case).
    let source = "static var TogglePauseCommand: String = \"toggle_pause\"\n";
    let config = default_config();
    let formatted = formatter::format_source(source, &config);
    assert!(
        formatted.contains("static var TogglePauseCommand"),
        "formatter must not rename static var identifiers, got: {}",
        formatted.trim()
    );
}

#[test]
fn formatter_does_not_rename_constants() {
    // Regression: `const event_tag = "event:"` became `EVENT_TAG event_tag = "event:"`
    let source = "const event_tag = \"event:\"\nconst data_tag = \"data:\"\n";
    let config = default_config();
    let formatted = formatter::format_source(source, &config);
    assert!(
        formatted.contains("const event_tag"),
        "formatter must not rename constant identifiers, got: {}",
        formatted.trim()
    );
}

#[test]
fn formatter_does_not_rename_enum_members() {
    // Regression: enum members like `None` were renamed to `NONE`,
    // `AIInControl` to `AIIN_CONTROL`, breaking all references.
    let source = "enum MovementMode {\n\tNone,\n\tAIInControl,\n\tPlayerInControl,\n}\n";
    let config = default_config();
    let formatted = formatter::format_source(source, &config);
    assert!(
        formatted.contains("None"),
        "formatter must not rename enum members, got: {}",
        formatted.trim()
    );
    assert!(
        formatted.contains("AIInControl"),
        "formatter must not rename enum members, got: {}",
        formatted.trim()
    );
}

#[test]
fn formatter_does_not_rename_functions() {
    // Regression: `func pullArray():` became `pull_array pullArray():`
    let source = "func pullArray():\n\tpass\n";
    let config = default_config();
    let formatted = formatter::format_source(source, &config);
    assert!(
        formatted.contains("func pullArray()"),
        "formatter must not rename function identifiers, got: {}",
        formatted.trim()
    );
}

#[test]
fn formatter_does_not_rename_class_name() {
    // Regression: `class_name TV extends InteractableObject` became `Tv TV extends...`
    let source = "class_name TV extends Node\n";
    let config = default_config();
    let formatted = formatter::format_source(source, &config);
    assert!(
        formatted.contains("class_name TV"),
        "formatter must not rename class_name identifiers, got: {}",
        formatted.trim()
    );
}

#[test]
fn formatter_does_not_rename_export_var() {
    // Regression: `@export var CHATTING_RADIUS: int = 300` became
    // `@export chatting_radius CHATTING_RADIUS: int = 300`
    let source = "@export var CHATTING_RADIUS: int = 300\n";
    let config = default_config();
    let formatted = formatter::format_source(source, &config);
    assert!(
        formatted.contains("@export var CHATTING_RADIUS"),
        "formatter must not rename @export var identifiers, got: {}",
        formatted.trim()
    );
}

#[test]
fn formatter_preserves_walrus_operator() {
    // Regression: `:=` was split into `: =` by operator spacing rule.
    // `var exit_thread := false` became `var exit_thread : = false`
    let source =
        "var exit_thread := false\n@onready var my_node := $MyNode\nconst THRESHOLD := 20.0\n";
    let config = default_config();
    let formatted = formatter::format_source(source, &config);
    assert!(
        !formatted.contains(": ="),
        "formatter must not insert space in :=, got: {}",
        formatted
    );
    assert!(
        formatted.contains(":="),
        "formatter must preserve := operator, got: {}",
        formatted
    );
}

#[test]
fn formatter_preserves_node_paths() {
    // Regression: `%InteractionMarkers/Marker2D_D` became `%InteractionMarkers / Marker2D_D`
    // because the `/` was treated as a division operator.
    let source = "@onready var marker := %InteractionMarkers/Marker2D_D\n@onready var sprite := $Sprites/Sprite2D_D\n";
    let config = default_config();
    let formatted = formatter::format_source(source, &config);
    assert!(
        !formatted.contains("/ Marker2D"),
        "formatter must not add spaces in node paths, got: {}",
        formatted
    );
    assert!(
        !formatted.contains("/ Sprite2D"),
        "formatter must not add spaces in node paths, got: {}",
        formatted
    );
}

#[test]
fn formatter_preserves_multiline_if_parens() {
    // Regression: multi-line `if (\n...\n):` had parens stripped, producing invalid
    // GDScript `if\n...\n:` which is a syntax error.
    let source = "func test():\n\tif (\n\t\ta != null\n\t\tand b != null\n\t):\n\t\tpass\n";
    let config = default_config();
    let formatted = formatter::format_source(source, &config);
    assert!(
        formatted.contains("if ("),
        "formatter must preserve parens on multi-line if conditions, got: {}",
        formatted
    );
    assert!(
        formatted.contains("):"),
        "formatter must preserve closing paren+colon on multi-line if conditions, got: {}",
        formatted
    );
}

#[test]
fn formatter_removes_single_line_unnecessary_parens() {
    // Single-line `if (x):` should still be fixed to `if x:`.
    let source = "func test():\n\tif (x == null):\n\t\tpass\n";
    let config = default_config();
    let formatted = formatter::format_source(source, &config);
    assert!(
        formatted.contains("if x == null:"),
        "formatter should remove unnecessary parens on single-line if, got: {}",
        formatted
    );
}

#[test]
fn formatter_preserves_comment_separator_lines() {
    // Regression: `##### MAIN FUNCTION CALLED...` became `## ### MAIN FUNCTION CALLED...`
    // because the doc-comment spacing rule inserted a space after `##`.
    let source = "##### MAIN FUNCTION CALLED TO GENERATE HOURLY PLAN #####\n";
    let config = default_config();
    let formatted = formatter::format_source(source, &config);
    assert_eq!(
        formatted.trim(),
        "##### MAIN FUNCTION CALLED TO GENERATE HOURLY PLAN #####",
        "formatter must not mangle ##### comment separators"
    );
}

#[test]
fn formatter_preserves_hash_section_headers() {
    // Regression: `### HANDLE CHAT FUNCTIONS...` became `## # HANDLE CHAT FUNCTIONS...`
    let source = "### HANDLE CHAT FUNCTIONS AND LOGIC INSIDE PERSONA ###\n";
    let config = default_config();
    let formatted = formatter::format_source(source, &config);
    assert_eq!(
        formatted.trim(),
        "### HANDLE CHAT FUNCTIONS AND LOGIC INSIDE PERSONA ###",
        "formatter must not mangle ### section headers"
    );
}

#[test]
fn formatter_trailing_comma_skips_comments() {
    // Regression: trailing comma was inserted into comments instead of after the
    // actual last element. Multi-pass caused repeated comma insertion: `# comment,,,,,`
    let source = "var arr = [\n\ta,\n\tb # last item comment\n]\n";
    let config = default_config();
    let formatted = formatter::format_source(source, &config);
    assert!(
        !formatted.contains(",,,"),
        "formatter must not insert multiple commas into comments, got: {}",
        formatted
    );
    // The comma should be after `b`, not inside the comment.
    assert!(
        formatted.contains("b, # last item comment")
            || formatted.contains("b,\t# last item comment"),
        "trailing comma should be inserted before the comment, got: {}",
        formatted
    );
}

#[test]
fn formatter_blank_lines_does_not_delete_code() {
    // Regression: the format/blank-lines rule used parser body_line_count which
    // could be wrong for complex functions, causing the rule to delete real code
    // when it thought there were "too many blank lines between members".
    let source = r#"static func plan(
	start_time = 0,
	end_time = 1440,
	chunk = 60):
	var result = []
	for i in range(10):
		var x = i * chunk

		if i > 5:
			var node = {"activity": "test", "duration": chunk}

			result.append(node)

			var prev = "prev"
		else:
			result[-1].duration += chunk

	return result

static func other():
	pass
"#;
    let config = default_config();
    let formatted = formatter::format_source(source, &config);
    // All the code should still be present.
    assert!(
        formatted.contains("result.append(node)"),
        "formatter must not delete code inside function bodies, got: {}",
        formatted
    );
    assert!(
        formatted.contains("result[-1].duration += chunk"),
        "formatter must not delete else-branch code, got: {}",
        formatted
    );
    assert!(
        formatted.contains("var prev ="),
        "formatter must not delete variable declarations, got: {}",
        formatted
    );
}

#[test]
fn formatter_negative_number_spacing() {
    // Regression: `-1` in contexts like `!= -1` became `!= - 1` (space inserted
    // between unary minus and operand).
    let source = "func test():\n\tif x != -1:\n\t\tpass\n\tvar y = [-1, -2]\n";
    let config = default_config();
    let formatted = formatter::format_source(source, &config);
    assert!(
        formatted.contains("!= -1"),
        "formatter must not add space in negative literals after comparison, got: {}",
        formatted
    );
}

#[test]
fn formatter_idempotent_on_complex_script() {
    // Ensure formatter is idempotent (running twice produces the same output)
    // on a complex script with many features.
    let source = r#"class_name MyClass
extends Node

##### SECTION HEADER #####

const MAX_VALUE := 100
const event_tag = "event:"

@export var RADIUS: int = 300
@onready var node := $Path/To/Node

var _private_var := false

enum State {
	None,
	Active,
	Idle,
}

func _ready():
	if (self.node != null):
		pass

func complex_func(a: int, b: int = -1):
	var result := a + b
	var arr = [
		"item1",
		"item2" # comment
	]
	if (
		a > 0
		and b > 0
	):
		return result
	return -1
"#;
    let config = default_config();
    let first = formatter::format_source(source, &config);
    let second = formatter::format_source(&first, &config);
    assert_eq!(
        first, second,
        "formatter must be idempotent on complex scripts.\nFirst pass:\n{}\nSecond pass:\n{}",
        first, second
    );
}

// --- Naming fix regression tests ---

#[test]
fn fix_signal_name_replaces_name_not_keyword() {
    // Regression: naming fixes replaced the keyword (e.g. "signal") instead of the
    // identifier name, producing "wave_started waveStarted(...)" instead of
    // "signal wave_started(...)".
    let source = "signal waveStarted(wave: int)\n";
    let config = default_config();
    let diagnostics = linter::lint_source(source, "test.gd", &config);
    let fixed = fixer::apply_fixes(source, &diagnostics, false);
    assert!(
        fixed.starts_with("signal "),
        "fix must preserve the 'signal' keyword, got: {}",
        fixed
    );
    assert!(
        fixed.contains("wave_started"),
        "fix must rename to snake_case, got: {}",
        fixed
    );
}

#[test]
fn fix_var_name_replaces_name_not_keyword() {
    // Regression: variable naming fix replaced "var" with the new name.
    let source = "var DamageMultiplier: float = 1.0\n";
    let config = default_config();
    let diagnostics = linter::lint_source(source, "test.gd", &config);
    let fixed = fixer::apply_fixes(source, &diagnostics, false);
    assert!(
        fixed.starts_with("var "),
        "fix must preserve the 'var' keyword, got: {}",
        fixed
    );
    assert!(
        fixed.contains("damage_multiplier"),
        "fix must rename to snake_case, got: {}",
        fixed
    );
}

#[test]
fn fix_const_name_replaces_name_not_keyword() {
    // Regression: constant naming fix replaced "const" with the new name.
    let source = "const maxSpeed = 400.0\n";
    let config = default_config();
    let diagnostics = linter::lint_source(source, "test.gd", &config);
    let fixed = fixer::apply_fixes(source, &diagnostics, false);
    assert!(
        fixed.starts_with("const "),
        "fix must preserve the 'const' keyword, got: {}",
        fixed
    );
    assert!(
        fixed.contains("MAX_SPEED"),
        "fix must rename to SCREAMING_SNAKE_CASE, got: {}",
        fixed
    );
}

#[test]
fn fix_func_name_replaces_name_not_keyword() {
    // Regression: function naming fix replaced "func" with the new name.
    let source = "func HasState(name: StringName) -> bool:\n\treturn true\n";
    let config = default_config();
    let diagnostics = linter::lint_source(source, "test.gd", &config);
    let fixed = fixer::apply_fixes(source, &diagnostics, false);
    assert!(
        fixed.starts_with("func "),
        "fix must preserve the 'func' keyword, got: {}",
        fixed
    );
    assert!(
        fixed.contains("has_state"),
        "fix must rename to snake_case, got: {}",
        fixed
    );
}

#[test]
fn fix_enum_name_replaces_name_not_keyword() {
    // Regression: enum naming fix replaced "enum" with the new name.
    let source = "enum item_rarity { COMMON, RARE }\n";
    let config = default_config();
    let diagnostics = linter::lint_source(source, "test.gd", &config);
    let fixed = fixer::apply_fixes(source, &diagnostics, false);
    assert!(
        fixed.starts_with("enum "),
        "fix must preserve the 'enum' keyword, got: {}",
        fixed
    );
    assert!(
        fixed.contains("ItemRarity"),
        "fix must rename to PascalCase, got: {}",
        fixed
    );
}

#[test]
fn fix_signal_past_tense_inflects_correctly() {
    // Regression: past tense fix naively appended "_changed" instead of inflecting.
    let cases = vec![
        ("signal wave_complete\n", "wave_completed"),
        ("signal enemy_die\n", "enemy_died"),
        ("signal player_retry\n", "player_retried"),
        ("signal item_add\n", "item_added"),
    ];

    let config = default_config();
    for (source, expected_name) in cases {
        let diagnostics = linter::lint_source(source, "test.gd", &config);
        let fixed = fixer::apply_fixes(source, &diagnostics, false);
        assert!(
            fixed.contains(expected_name),
            "past tense fix for '{}' should produce '{}', got: {}",
            source.trim(),
            expected_name,
            fixed.trim()
        );
    }
}

#[test]
fn ordering_no_false_positives_for_local_variables() {
    // S4: local variables inside function bodies should NOT generate
    // order/class-member-order warnings.
    let source = "extends Node\n\nvar speed: float = 10.0\n\nfunc _ready():\n\tvar timer = Timer.new()\n\ttimer.wait_time = 1.0\n\tadd_child(timer)\n\nfunc process_data(items: Array):\n\tfor item in items:\n\t\tvar result = 1\n\t\tif result > 0:\n\t\t\tvar label = Label.new()\n\t\t\tlabel.text = str(result)\n";
    let config = default_config();
    let diagnostics = linter::lint_source(source, "test.gd", &config);
    let ordering_diags: Vec<_> = diagnostics
        .iter()
        .filter(|d| d.rule == "order/class-member-order")
        .collect();
    assert!(
        ordering_diags.is_empty(),
        "local variables inside functions should not generate ordering warnings, got:\n{}",
        ordering_diags
            .iter()
            .map(|d| format!("  {}:{} {}", d.span.line, d.span.column, d.message))
            .collect::<Vec<_>>()
            .join("\n")
    );
}

#[test]
fn ordering_no_false_positives_for_local_vars_after_comment() {
    // S4: comments inside function bodies caused the parser to break out of
    // the body, making subsequent local variables appear as class-level members.
    let source =
        "extends Node\n\nfunc _ready():\n\tpass\n\nfunc bar():\n\t# comment\n\tvar x = 1\n";
    let config = default_config();
    let diagnostics = linter::lint_source(source, "test.gd", &config);
    let ordering_diags: Vec<_> = diagnostics
        .iter()
        .filter(|d| d.rule == "order/class-member-order")
        .collect();
    assert!(
        ordering_diags.is_empty(),
        "local variables after comments in function bodies should not generate ordering warnings, got:\n{}",
        ordering_diags
            .iter()
            .map(|d| format!("  {}:{} {}", d.span.line, d.span.column, d.message))
            .collect::<Vec<_>>()
            .join("\n")
    );
}

#[test]
fn ordering_no_false_positives_complex_function_body() {
    // S4: reproduces the GARP active_cognition.gd pattern with deep nesting.
    let source = r#"class_name ActiveCognition
extends Node

@export var persona_name: String
@onready var state_chart: Node = %StateChart

func _ready():
	pass

func _choose_event_to_react():
	# Some logic
	var persona_events = []
	var regular_events = []

	for event in []:
		var e = event
		if e:
			var data = {}
			data["key"] = "value"
"#;
    let config = default_config();
    let diagnostics = linter::lint_source(source, "test.gd", &config);
    let ordering_diags: Vec<_> = diagnostics
        .iter()
        .filter(|d| d.rule == "order/class-member-order")
        .collect();
    assert!(
        ordering_diags.is_empty(),
        "complex function bodies with comments and local vars should not generate ordering warnings, got:\n{}",
        ordering_diags
            .iter()
            .map(|d| format!("  {}:{} {}", d.span.line, d.span.column, d.message))
            .collect::<Vec<_>>()
            .join("\n")
    );
}

#[test]
fn fix_collapses_excess_blank_lines() {
    // S5: format/blank-lines should be fixable by --fix
    let source = "extends Node\n\nvar x = 5\n\n\n\n\nvar y = 10\n";
    let config = default_config();
    let diagnostics = linter::lint_source(source, "test.gd", &config);

    // Should have a blank-lines diagnostic
    let blank_diags: Vec<_> = diagnostics
        .iter()
        .filter(|d| d.rule == "format/blank-lines")
        .collect();
    assert!(!blank_diags.is_empty(), "should detect excess blank lines");
    assert!(
        blank_diags[0].fix.is_some(),
        "blank-lines diagnostic should have a fix"
    );
    assert!(
        blank_diags[0].fix.as_ref().unwrap().is_safe,
        "blank-lines fix should be safe"
    );

    // Apply safe fix
    let fixed = fixer::apply_fixes(source, &diagnostics, true);
    // Should have exactly 2 blank lines between members
    assert!(
        !fixed.contains("\n\n\n\n"),
        "should collapse 4+ blank lines, got:\n{}",
        fixed
    );
    // Should still have 2 blank lines
    assert!(
        fixed.contains("\n\n\n"),
        "should preserve 2 blank lines between members, got:\n{}",
        fixed
    );

    // Verify no more blank-lines warnings
    let recheck = linter::lint_source(&fixed, "test.gd", &config);
    let remaining: Vec<_> = recheck
        .iter()
        .filter(|d| d.rule == "format/blank-lines")
        .collect();
    assert!(
        remaining.is_empty(),
        "after fix, should have no blank-lines warnings, got:\n{}",
        remaining
            .iter()
            .map(|d| d.message.as_str())
            .collect::<Vec<_>>()
            .join(", ")
    );
}

#[test]
fn fix_collapses_six_blank_lines_to_two() {
    // S5: GARP example with 6 blank lines
    let source = "func foo():\n\tpass\n\n\n\n\n\n\nfunc bar():\n\tpass\n";
    let config = default_config();
    let diagnostics = linter::lint_source(source, "test.gd", &config);
    let fixed = fixer::apply_fixes(source, &diagnostics, true);

    // Count blank lines between functions
    let lines: Vec<&str> = fixed.split('\n').collect();
    let mut max_consecutive = 0;
    let mut consecutive = 0;
    for line in &lines {
        if line.trim().is_empty() {
            consecutive += 1;
        } else {
            if consecutive > max_consecutive {
                max_consecutive = consecutive;
            }
            consecutive = 0;
        }
    }
    if consecutive > max_consecutive {
        max_consecutive = consecutive;
    }
    assert!(
        max_consecutive <= 2,
        "should have at most 2 consecutive blank lines, got {}: {}",
        max_consecutive,
        fixed
    );
}

// S9: --unsafe-fix should update same-file references

#[test]
fn unsafe_fix_updates_same_file_variable_references() {
    // S9: renaming a var declaration should also update self.OLD_NAME and bare OLD_NAME
    let source = "var DEFAULT_SEED = 42\n\nfunc _ready():\n\tself.DEFAULT_SEED = 100\n\tprint(DEFAULT_SEED)\n";
    let config = default_config();
    let diagnostics = linter::lint_source(source, "test.gd", &config);
    let fixed = fixer::apply_fixes(source, &diagnostics, false);

    assert!(
        !fixed.contains("DEFAULT_SEED"),
        "all references to DEFAULT_SEED should be renamed, got:\n{}",
        fixed
    );
    assert!(
        fixed.contains("var default_seed"),
        "declaration should be renamed to snake_case, got:\n{}",
        fixed
    );
    // self.default_seed and bare default_seed should appear
    assert!(
        fixed.contains("self.default_seed"),
        "self-qualified reference should be updated, got:\n{}",
        fixed
    );
}

#[test]
fn unsafe_fix_updates_same_file_enum_member_references() {
    // S9: renaming enum members should update references in the same file
    let source = "enum State {\n\tAIInControl,\n\tPlayerInControl,\n}\n\nfunc get_state():\n\treturn State.AIInControl\n";
    let config = default_config();
    let diagnostics = linter::lint_source(source, "test.gd", &config);
    let fixed = fixer::apply_fixes(source, &diagnostics, false);

    // The renamed member should be AI_IN_CONTROL (with S7 acronym fix)
    assert!(
        fixed.contains("AI_IN_CONTROL"),
        "enum member should be renamed to SCREAMING_CASE, got:\n{}",
        fixed
    );
    // The reference should also be updated
    assert!(
        !fixed.contains("AIInControl"),
        "reference to old enum member name should be updated, got:\n{}",
        fixed
    );
}

#[test]
fn unsafe_fix_updates_same_file_function_references() {
    // S9: renaming a function should update calls in the same file
    let source = "func HasState(name: String) -> bool:\n\treturn true\n\nfunc _ready():\n\tif HasState(\"idle\"):\n\t\tpass\n";
    let config = default_config();
    let diagnostics = linter::lint_source(source, "test.gd", &config);
    let fixed = fixer::apply_fixes(source, &diagnostics, false);

    assert!(
        fixed.contains("func has_state"),
        "function declaration should be renamed, got:\n{}",
        fixed
    );
    assert!(
        !fixed.contains("HasState"),
        "all references to old function name should be updated, got:\n{}",
        fixed
    );
}

#[test]
fn unsafe_fix_updates_multiple_declarations_same_name() {
    // S9: two declarations of the same variable name in the same file
    let source = "func foo():\n\tvar DialogueCtrl = 1\n\tprint(DialogueCtrl)\n\nfunc bar():\n\tvar DialogueCtrl = 2\n\tprint(DialogueCtrl)\n";
    let config = default_config();
    let diagnostics = linter::lint_source(source, "test.gd", &config);
    let fixed = fixer::apply_fixes(source, &diagnostics, false);

    // Note: local vars inside functions may not be detected by the parser as
    // class members, so they may not have naming diagnostics. But if they do,
    // all references should be updated.
    // In this case the parser may not see these as class members, so this test
    // primarily verifies the fixer doesn't crash.
    assert!(!fixed.is_empty());
}

// S1: cross-file reference tracking

#[test]
fn extract_renames_from_diagnostics() {
    // S1: extract_renames should identify naming renames from diagnostics
    let source = "var CONFIG: ConfigFile = load(\"cfg\")\n";
    let config = default_config();
    let diagnostics = linter::lint_source(source, "config_manager.gd", &config);

    let members = parse_members_for_test(source);
    let renames = fixer::extract_renames(source, &diagnostics, "config_manager.gd", &members);

    // Should find the CONFIG → config rename
    let config_rename = renames.iter().find(|r| r.old_name == "CONFIG");
    assert!(
        config_rename.is_some(),
        "should extract CONFIG rename, got: {:?}",
        renames
            .iter()
            .map(|r| format!("{} -> {}", r.old_name, r.new_name))
            .collect::<Vec<_>>()
    );
    assert_eq!(config_rename.unwrap().new_name, "config");
}

#[test]
fn find_cross_file_references_detects_old_names() {
    // S1: find_cross_file_references should detect references to renamed identifiers
    let renames = vec![fixer::AppliedRename {
        old_name: "CONFIG".to_string(),
        new_name: "config".to_string(),
        source_file: "config_manager.gd".to_string(),
        source_class_name: Some("ConfigManager".to_string()),
        kind: fixer::RenameKind::Constant,
        is_instance_member: false,
    }];

    let other_source = "func _ready():\n\tvar cfg = ConfigManager.CONFIG\n\tprint(cfg)\n";
    let refs = fixer::find_cross_file_references(other_source, "engine.gd", &renames);

    assert!(
        !refs.is_empty(),
        "should find reference to CONFIG in other file"
    );
    assert_eq!(refs[0].old_name, "CONFIG");
    assert_eq!(refs[0].new_name, "config");
    assert_eq!(refs[0].file, "engine.gd");
}

#[test]
fn find_cross_file_references_skips_same_file() {
    // S1: should not report references in the same file (those are handled by S9)
    let renames = vec![fixer::AppliedRename {
        old_name: "CONFIG".to_string(),
        new_name: "config".to_string(),
        source_file: "config_manager.gd".to_string(),
        source_class_name: Some("ConfigManager".to_string()),
        kind: fixer::RenameKind::Constant,
        is_instance_member: false,
    }];

    let source = "var config = 1\nprint(CONFIG)\n";
    let refs = fixer::find_cross_file_references(source, "config_manager.gd", &renames);

    assert!(
        refs.is_empty(),
        "should not report references in the same file"
    );
}

#[test]
fn apply_cross_file_fixes_updates_references() {
    // S1: apply_cross_file_fixes should replace old names with new names
    let refs = vec![fixer::CrossFileReference {
        file: "engine.gd".to_string(),
        line: 2,
        column: 25,
        old_name: "CONFIG".to_string(),
        new_name: "config".to_string(),
        source_file: "config_manager.gd".to_string(),
        offset: 40,
        length: 6,
    }];

    let source = "func _ready():\n\tvar cfg = ConfigManager.CONFIG\n\tprint(cfg)\n";
    let fixed = fixer::apply_cross_file_fixes(source, &refs);

    assert!(
        fixed.contains("ConfigManager.config"),
        "should replace CONFIG with config, got:\n{}",
        fixed
    );
    assert!(
        !fixed.contains("CONFIG"),
        "should not contain old name CONFIG, got:\n{}",
        fixed
    );
}

#[test]
fn apply_scene_renames_rewrites_signal_and_method_connections() {
    // A signal rename and a function rename should both update the matching
    // `[connection]` attributes in a .tscn; otherwise the editor-wired
    // connection silently breaks at runtime.
    let renames = vec![
        fixer::AppliedRename {
            old_name: "health_change".to_string(),
            new_name: "health_changed".to_string(),
            source_file: "player.gd".to_string(),
            source_class_name: Some("Player".to_string()),
            kind: fixer::RenameKind::Signal,
            is_instance_member: true,
        },
        fixer::AppliedRename {
            old_name: "onHealthChange".to_string(),
            new_name: "on_health_change".to_string(),
            source_file: "hud.gd".to_string(),
            source_class_name: Some("Hud".to_string()),
            kind: fixer::RenameKind::Function,
            is_instance_member: true,
        },
    ];
    let scene = "[gd_scene format=3]\n\n[connection signal=\"health_change\" from=\".\" to=\"HUD\" method=\"onHealthChange\"]\n";
    let (rewritten, applied) = fixer::apply_scene_renames(scene, &renames);

    assert!(
        rewritten.contains("signal=\"health_changed\""),
        "signal rewritten: {}",
        rewritten
    );
    assert!(
        rewritten.contains("method=\"on_health_change\""),
        "method rewritten: {}",
        rewritten
    );
    assert!(
        !rewritten.contains("\"health_change\""),
        "old signal gone: {}",
        rewritten
    );
    assert!(
        !rewritten.contains("\"onHealthChange\""),
        "old method gone: {}",
        rewritten
    );
    assert_eq!(applied.len(), 2, "both connections reported");
}

#[test]
fn apply_scene_renames_ignores_unrelated_kinds() {
    // A Variable / Constant / Class rename must not touch scene connection
    // attributes; only Signal (signal=) and Function (method=) do.
    let renames = vec![fixer::AppliedRename {
        old_name: "health_change".to_string(),
        new_name: "HEALTH_CHANGE".to_string(),
        source_file: "x.gd".to_string(),
        source_class_name: None,
        kind: fixer::RenameKind::Constant,
        is_instance_member: false,
    }];
    let scene = "[connection signal=\"health_change\" method=\"foo\"]\n";
    let (rewritten, applied) = fixer::apply_scene_renames(scene, &renames);
    assert_eq!(rewritten, scene, "constant rename must not touch scene");
    assert!(applied.is_empty());
}

// S6: enum-one-per-line auto-fix

// S8: member ordering in fmt

#[test]
fn fmt_reorders_exports_before_onready() {
    let source = "extends Node\n\n@onready var chart: Node = %Chart\n@onready var memory: Node = %Memory\n@export var name: String\n@onready var active: Node = %Active\n@export var prob: float = 1.0\n";
    let config = default_config();
    let formatted = formatter::format_source(source, &config);

    let export_pos = formatted.find("@export var name");
    let onready_pos = formatted.find("@onready var chart");
    assert!(
        export_pos.unwrap() < onready_pos.unwrap(),
        "@export should appear before @onready after formatting, got:\n{}",
        formatted
    );
}

#[test]
fn fmt_reorders_signals_before_enums() {
    let source = "extends Node\n\nenum State { IDLE, RUN }\nsignal done()\nsignal moved()\n";
    let config = default_config();
    let formatted = formatter::format_source(source, &config);

    let signal_pos = formatted.find("signal done()");
    let enum_pos = formatted.find("enum State");
    assert!(
        signal_pos.unwrap() < enum_pos.unwrap(),
        "signal should appear before enum, got:\n{}",
        formatted
    );
}

#[test]
fn fmt_reorders_constants_before_vars() {
    let source = "extends Node\n\nvar _font: Font = null\nconst C_TAN = 0.784\n";
    let config = default_config();
    let formatted = formatter::format_source(source, &config);

    let const_pos = formatted.find("const C_TAN");
    let var_pos = formatted.find("var _font");
    assert!(
        const_pos.unwrap() < var_pos.unwrap(),
        "const should appear before var, got:\n{}",
        formatted
    );
}

#[test]
fn fmt_reorder_idempotent() {
    let source =
        "extends Node\n\nfunc _ready():\n\tpass\n\nvar speed: float = 10.0\n\nsignal done()\n";
    let config = default_config();
    let first = formatter::format_source(source, &config);
    let second = formatter::format_source(&first, &config);
    assert_eq!(
        first, second,
        "must be idempotent.\nFirst:\n{}\nSecond:\n{}",
        first, second
    );
}

#[test]
fn fmt_reorder_preserves_correct_order() {
    let source = "extends Node\n\nsignal done()\n\nconst MAX = 100\n\nvar speed: float = 10.0\n\nfunc _ready():\n\tpass\n";
    let config = default_config();
    let formatted = formatter::format_source(source, &config);
    let second = formatter::format_source(&formatted, &config);
    assert_eq!(
        formatted, second,
        "formatter must be idempotent on correctly ordered script"
    );
}

#[test]
fn fix_enum_one_per_line() {
    // S6: single-line enum should be reformatted to multi-line by --fix
    let source = "enum Direction {DOWN, RIGHT, UP, LEFT}\n";
    let config = default_config();
    let diagnostics = linter::lint_source(source, "test.gd", &config);

    let enum_diags: Vec<_> = diagnostics
        .iter()
        .filter(|d| d.rule == "format/enum-one-per-line")
        .collect();
    assert!(!enum_diags.is_empty(), "should detect single-line enum");
    assert!(
        enum_diags[0].fix.is_some(),
        "enum-one-per-line should have an auto-fix"
    );

    let fixed = fixer::apply_fixes(source, &diagnostics, true);

    // Should be multi-line now
    assert!(
        fixed.contains("enum Direction {"),
        "should have opening brace on enum line, got:\n{}",
        fixed
    );
    assert!(
        fixed.contains("\tDOWN,"),
        "each member should be on its own indented line, got:\n{}",
        fixed
    );
    assert!(
        fixed.contains("\tRIGHT,"),
        "each member should be on its own indented line, got:\n{}",
        fixed
    );
    assert!(
        fixed.contains("\tUP,"),
        "each member should be on its own indented line, got:\n{}",
        fixed
    );
    assert!(
        fixed.contains("\tLEFT,"),
        "each member should be on its own indented line with trailing comma, got:\n{}",
        fixed
    );

    // Should have no more enum-one-per-line warnings
    let recheck = linter::lint_source(&fixed, "test.gd", &config);
    let remaining: Vec<_> = recheck
        .iter()
        .filter(|d| d.rule == "format/enum-one-per-line")
        .collect();
    assert!(
        remaining.is_empty(),
        "after fix, should have no enum-one-per-line warnings"
    );
}

#[test]
fn fix_enum_one_per_line_state() {
    // S6: another GARP example
    let source = "enum State {IDLE, RUNNING, JUMPING, FALLING, DEAD}\n";
    let config = default_config();
    let diagnostics = linter::lint_source(source, "test.gd", &config);
    let fixed = fixer::apply_fixes(source, &diagnostics, true);

    assert!(
        fixed.contains("\tIDLE,"),
        "IDLE should be on its own line, got:\n{}",
        fixed
    );
    assert!(
        fixed.contains("\tDEAD,"),
        "DEAD should have trailing comma, got:\n{}",
        fixed
    );
}

#[test]
fn fmt_does_not_duplicate_class_level_abstract_annotation() {
    // Regression for issue #4: an `@abstract` annotation between blank
    // lines above `class_name`/`extends` plus a function later in the
    // file blew up to ~30 copies of `@abstract\nclass_name Pickup\n`
    // when formatted. The parser was holding `@abstract` in
    // `pending_annotations` past `class_name` and `extends` and
    // attaching it to the function; the function unit's start then
    // covered line 1, which the formatter re-emitted on every pass.
    //
    // Fix: flush pending unknown annotations as standalone
    // `ClassAnnotation` members when we hit `class_name`/`extends`, so
    // they sort with the other class-level annotations (category 0)
    // and don't dangle.
    let source = "@abstract\n\nclass_name Pickup\nextends Node\n\n\n\nfunc apply_to(target: Node3D) -> void:\n\tpass\n";
    let config = default_config();
    let formatted = formatter::format_source(source, &config);
    let abstract_count = formatted.matches("@abstract").count();
    let class_name_count = formatted.matches("class_name Pickup").count();
    assert_eq!(
        abstract_count, 1,
        "@abstract must not be duplicated, got {} copies:\n{}",
        abstract_count, formatted
    );
    assert_eq!(
        class_name_count, 1,
        "class_name must not be duplicated, got {} copies:\n{}",
        class_name_count, formatted
    );
    // And the resulting file should look like canonical Godot:
    // `@abstract` directly above `class_name`, then `extends`, then
    // two blank lines before the function.
    assert!(
        formatted.contains("@abstract\nclass_name Pickup\nextends Node\n"),
        "expected canonical class header, got:\n{}",
        formatted
    );
    assert!(
        formatted.contains("\n\n\nfunc apply_to(target: Node3D) -> void:\n\tpass\n"),
        "expected 2 blank lines between class header and func, got:\n{}",
        formatted
    );
}

#[test]
fn fmt_class_annotation_before_signal_not_duplicated() {
    // Same family of bug as issue #4: a stray top-level annotation
    // (`@abstract` here) immediately before a `signal` declaration
    // used to ride past the signal in `pending_annotations` and
    // attach to a function later in the file, blowing up the output.
    // Signal declarations don't take a leading annotation, so the
    // parser now flushes pending entries as class-level annotations
    // at that point.
    let source = "@abstract\nclass_name BaseEntity\nextends Node\n\n\nsignal damaged(amount: int)\n\n\nfunc take_damage(amount: int) -> void:\n\tdamaged.emit(amount)\n";
    let config = default_config();
    let formatted = formatter::format_source(source, &config);
    assert_eq!(
        formatted.matches("@abstract").count(),
        1,
        "@abstract must not be duplicated, got:\n{}",
        formatted
    );
    assert!(
        formatted.contains("@abstract\nclass_name BaseEntity\nextends Node"),
        "@abstract must land at the canonical class-header position, got:\n{}",
        formatted
    );
    assert!(
        formatted.contains("signal damaged(amount: int)"),
        "signal declaration must be preserved, got:\n{}",
        formatted
    );
}

#[test]
fn fmt_class_annotation_before_enum_not_duplicated() {
    // Counterpart to the signal case: an `@abstract` immediately
    // before an `enum` declaration used to ride forward to the next
    // function and trigger the same multi-line duplication. `enum`
    // doesn't accept a leading annotation, so we flush.
    let source = "@abstract\nclass_name Item\nextends Resource\n\n\nenum Rarity { COMMON, RARE, EPIC }\n\n\nfunc describe() -> String:\n\treturn \"\"\n";
    let config = default_config();
    let formatted = formatter::format_source(source, &config);
    assert_eq!(
        formatted.matches("@abstract").count(),
        1,
        "@abstract must not be duplicated, got:\n{}",
        formatted
    );
    assert!(
        formatted.contains("@abstract\nclass_name Item\nextends Resource"),
        "expected canonical class header, got:\n{}",
        formatted
    );
    assert!(
        formatted.contains("enum Rarity"),
        "enum declaration must be preserved, got:\n{}",
        formatted
    );
}

#[test]
fn fmt_class_annotation_before_inner_class_not_duplicated() {
    // Inner class declarations have no AST slot for annotations, so
    // we treat a stray `@abstract` above one as a class-level
    // annotation of the OUTER class. That's the deliberate
    // trade-off: the alternative — leaving it pending — would
    // attach it to the next top-level function and cause the
    // duplication bug. If Godot ever wires inner-class annotations
    // through the AST, revisit `parse_inner_class`.
    let source = "class_name Outer\nextends Node\n\n\n@abstract\nclass Inner:\n\tvar x: int = 5\n\n\nfunc use_inner() -> void:\n\tvar i = Inner.new()\n";
    let config = default_config();
    let formatted = formatter::format_source(source, &config);
    assert_eq!(
        formatted.matches("@abstract").count(),
        1,
        "@abstract must not be duplicated, got:\n{}",
        formatted
    );
    assert!(
        formatted.contains("class Inner:"),
        "inner class must be preserved, got:\n{}",
        formatted
    );
    assert!(
        formatted.contains("func use_inner()"),
        "outer function must be preserved and not eaten by the annotation flush, got:\n{}",
        formatted
    );
}

#[test]
fn fmt_trailing_unknown_annotation_at_eof_not_dropped() {
    // A trailing top-level annotation with no following declaration
    // used to be silently dropped by the parser (pending_annotations
    // was never flushed at EOF). Now it survives as a class-level
    // annotation so a round-trip through `gdstyle fmt` doesn't lose
    // content.
    let source = "class_name Foo\nextends Node\n\n\nfunc bar() -> void:\n\tpass\n\n\n@abstract\n";
    let config = default_config();
    let formatted = formatter::format_source(source, &config);
    assert_eq!(
        formatted.matches("@abstract").count(),
        1,
        "trailing @abstract must survive the round-trip, got:\n{}",
        formatted
    );
}

#[test]
fn fmt_class_annotation_with_args_preserves_args() {
    // The parsed ClassAnnotation only stores the annotation NAME (no
    // parens, no args), but the formatter emits via line-based source
    // slicing, so the raw `@x(a, b)` text round-trips verbatim. Lock
    // that in: an AST-based emitter introduced later would silently
    // drop the args, and we want a test that catches it.
    let source = "@some_unknown(42, \"arg\")\n\nclass_name Foo\nextends Node\n\n\nfunc bar() -> void:\n\tpass\n";
    let config = default_config();
    let formatted = formatter::format_source(source, &config);
    assert!(
        formatted.contains("@some_unknown(42, \"arg\")"),
        "annotation args must round-trip verbatim, got:\n{}",
        formatted
    );
    assert_eq!(
        formatted.matches("@some_unknown").count(),
        1,
        "annotation with args must not duplicate, got:\n{}",
        formatted
    );
}

#[test]
fn fmt_stacked_class_annotations_preserve_source_order() {
    // Multiple unknown annotations above class_name must be emitted in
    // the order the user wrote them — the flush iterates a Vec, but
    // a future switch to a Set or HashMap would silently scramble.
    let source = "@abstract\n@experimental\n@deprecated\n\nclass_name Foo\nextends Node\n\n\nfunc bar() -> void:\n\tpass\n";
    let config = default_config();
    let formatted = formatter::format_source(source, &config);
    let abs = formatted.find("@abstract").expect("missing @abstract");
    let exp = formatted.find("@experimental").expect("missing @experimental");
    let dep = formatted.find("@deprecated").expect("missing @deprecated");
    assert!(
        abs < exp && exp < dep,
        "stacked annotations must keep source order @abstract → @experimental → @deprecated, got positions abs={} exp={} dep={} in:\n{}",
        abs, exp, dep, formatted
    );
}

#[test]
fn fmt_class_and_function_level_abstract_both_survive() {
    // Class-level @abstract above class_name AND function-level
    // @abstract above a method must both survive. Guards against an
    // over-eager flush that promotes EVERY @abstract to class-level.
    let source = "@abstract\nclass_name Pickup\nextends Node\n\n\n@abstract\nfunc to_implement() -> void:\n\tpass\n\n\nfunc concrete() -> void:\n\tpass\n";
    let config = default_config();
    let formatted = formatter::format_source(source, &config);
    assert_eq!(
        formatted.matches("@abstract").count(),
        2,
        "both @abstract occurrences must survive, got:\n{}",
        formatted
    );
    assert!(
        formatted.contains("@abstract\nclass_name Pickup"),
        "class-level @abstract must sit above class_name, got:\n{}",
        formatted
    );
    assert!(
        formatted.contains("@abstract\nfunc to_implement("),
        "function-level @abstract must stay attached to its function, got:\n{}",
        formatted
    );
}

#[test]
fn fmt_class_annotation_is_idempotent() {
    // Formatting twice must produce the identical output. The original
    // bug was a multi-pass duplication loop; this is the direct
    // assertion that we no longer oscillate.
    let source = "@abstract\n\nclass_name Pickup\nextends Node\n\n\n\nfunc apply_to(target: Node3D) -> void:\n\tpass\n";
    let config = default_config();
    let once = formatter::format_source(source, &config);
    let twice = formatter::format_source(&once, &config);
    assert_eq!(
        once, twice,
        "formatter must be idempotent; pass 1 vs pass 2 differ:\n--- pass 1 ---\n{}\n--- pass 2 ---\n{}",
        once, twice
    );
}

#[test]
fn fmt_class_annotation_with_docstring_between() {
    // `@abstract\n## docstring\nclass_name Foo` — the docstring is
    // logically class-level and the reorder pass moves it to after
    // class_name/extends (canonical Godot style). @abstract must end
    // up above class_name, the docstring after extends, neither
    // duplicated.
    let source = "@abstract\n## Top-level docstring for this abstract class.\nclass_name Foo\nextends Node\n\n\nfunc bar() -> void:\n\tpass\n";
    let config = default_config();
    let formatted = formatter::format_source(source, &config);
    assert_eq!(
        formatted.matches("@abstract").count(),
        1,
        "@abstract must not duplicate, got:\n{}",
        formatted
    );
    assert_eq!(
        formatted.matches("## Top-level docstring").count(),
        1,
        "docstring must not duplicate, got:\n{}",
        formatted
    );
    let abs_pos = formatted.find("@abstract").unwrap();
    let cls_pos = formatted.find("class_name Foo").unwrap();
    let doc_pos = formatted.find("## Top-level docstring").unwrap();
    assert!(
        abs_pos < cls_pos,
        "@abstract must come before class_name, got:\n{}",
        formatted
    );
    assert!(
        cls_pos < doc_pos,
        "class_name must come before the docstring (canonical Godot order), got:\n{}",
        formatted
    );
}

#[test]
fn fmt_preserves_function_level_abstract_annotation() {
    // The function-level form of `@abstract` (declaring an abstract
    // method, distinct from declaring an abstract class) must continue
    // to attach to the function it sits above, not get hoisted to the
    // top of the file as a class-level annotation. Guards against an
    // over-eager rewrite of the class-level fix.
    let source = "class_name Pickup\nextends Node\n\n\n@abstract\nfunc to_implement() -> void:\n\tpass\n\n\nfunc concrete() -> void:\n\tpass\n";
    let config = default_config();
    let formatted = formatter::format_source(source, &config);
    assert!(
        formatted.contains("@abstract\nfunc to_implement("),
        "@abstract must stay attached to its function, got:\n{}",
        formatted
    );
    // And there should still be exactly one @abstract — no duplication.
    assert_eq!(
        formatted.matches("@abstract").count(),
        1,
        "@abstract must not be duplicated, got:\n{}",
        formatted
    );
}

#[test]
fn fmt_keeps_doc_attached_on_user_reported_artifact_resolver() {
    // Verbatim shape of the file the issue reporter posted: a function
    // whose body ends with a top-level `if` after a `match` (so the body
    // closes at depth 2), followed by a single-line `## doc` for the
    // next function. Before the lexer fix, the docstring was detached
    // by two blank lines from `get_active_artifacts`, breaking the
    // Godot editor tooltip and the class-docs export. Kept as a
    // separate test from the minimal repro so a future regression on
    // this exact reported shape would be immediately recognisable.
    let source = "extends Object
class_name ArtifactSkillResolver

## Resolves all artifact skills matching the given trigger.
## Executes the resolution via signals.
static func resolve_skills(trigger: int) -> void:
\tvar activated = []
\tfor resolution in activated:
\t\t_execute_resolution(resolution, trigger)

static func _execute_resolution(resolution: int, trigger: int) -> void:
\tmatch resolution:
\t\t1:
\t\t\tprint(\"one\")
\t\t2:
\t\t\tprint(\"two\")
\tif trigger != 0:
\t\tresolve_skills(0)

## Filters equipped artifacts to only those whose prerequisite is also equipped.
static func get_active_artifacts(equipped) -> Array:
\treturn equipped
";
    let config = default_config();
    let formatted = formatter::format_source(source, &config);
    assert!(
        formatted.contains(
            "## Filters equipped artifacts to only those whose prerequisite is also equipped.\nstatic func get_active_artifacts("
        ),
        "docstring detached from get_active_artifacts, got:\n{}",
        formatted
    );
    // The multi-line docstring for `resolve_skills` must also stay
    // attached (regression guard against breaking the working case
    // while fixing the broken one).
    assert!(
        formatted.contains(
            "## Executes the resolution via signals.\nstatic func resolve_skills("
        ),
        "multi-line docstring on resolve_skills was detached, got:\n{}",
        formatted
    );
}

#[test]
fn fmt_keeps_doc_attached_when_prev_body_ends_with_nested_block() {
    // Regression: when a function body ended with a nested indented block
    // (e.g. a top-level `if` after a `match`, leaving two open indent
    // levels at the body's tail), a single-line `## doc` for the NEXT
    // function got detached. The lexer was swallowing the doc line without
    // emitting the dedents that close the previous body, so the parser
    // ate the doc as part of the previous function. The formatter then
    // inserted its canonical between-functions blank-line gap between the
    // orphaned doc and the function it actually documents. Reported by
    // a user against gdstyle 0.1.1 with a real Godot project file.
    let source = "extends Node

static func a() -> void:
\tmatch 1:
\t\t1:
\t\t\tpass
\tif true:
\t\tpass

## doc for b
static func b() -> void:
\tpass
";
    let config = default_config();
    let formatted = formatter::format_source(source, &config);
    assert!(
        formatted.contains("## doc for b\nstatic func b("),
        "doc comment was detached from its function, got:\n{}",
        formatted
    );
}

#[test]
fn fmt_preserves_function_bodies_with_doc_comments() {
    let source = r#"## Module doc

class_name MyClass
extends Node

func _ready():
	var data = {}
	data["key"] = "value"
	print(data)

func get_info() -> String:
	var result = "info"
	return result
"#;
    let config = default_config();
    let formatted = formatter::format_source(source, &config);

    assert!(
        formatted.contains("data[\"key\"] = \"value\""),
        "function body line missing, got:\n{}",
        formatted
    );
    assert!(
        formatted.contains("print(data)"),
        "function body line missing, got:\n{}",
        formatted
    );
    assert!(
        formatted.contains("var result = \"info\""),
        "function body line missing, got:\n{}",
        formatted
    );
    assert!(
        formatted.contains("return result"),
        "function body line missing, got:\n{}",
        formatted
    );

    let second = formatter::format_source(&formatted, &config);
    assert_eq!(formatted, second, "must be idempotent");
}

#[test]
fn fmt_preserves_multiline_expressions() {
    let source = "var tokens = ConfigManager.get_instance().config.get_value(\n\t\"setup\", \"MAX_TOKENS\"\n)\n";
    let config = default_config();
    let formatted = formatter::format_source(source, &config);

    assert!(
        formatted.contains("\"setup\", \"MAX_TOKENS\""),
        "multi-line args should be preserved, got:\n{}",
        formatted
    );
    assert!(
        formatted.contains(")"),
        "closing paren should be preserved, got:\n{}",
        formatted
    );
}

#[test]
fn fmt_does_not_duplicate_class_name() {
    let source = "class_name MyClass extends Node\n\nvar x = 10\n\nfunc _ready():\n\tpass\n";
    let config = default_config();
    let formatted = formatter::format_source(source, &config);

    let count = formatted.matches("class_name").count();
    assert_eq!(
        count, 1,
        "class_name should appear exactly once, got {} times in:\n{}",
        count, formatted
    );
}

#[test]
fn fmt_does_not_duplicate_class_name_with_many_members() {
    let source = r#"class_name ActiveCognition extends Node2D

static var THOUGHT_POIGNANCY: int = 5
static var THOUGHT_PREDICATE: String = "plan"

@export var name: String
@onready var chart: Node = %Chart

signal done()
const MAX = 100
var x = 10

func _ready():
	pass

func _process(delta: float):
	pass

func custom():
	pass
"#;
    let config = default_config();
    let formatted = formatter::format_source(source, &config);

    let count = formatted.matches("class_name").count();
    assert_eq!(
        count, 1,
        "class_name should appear exactly once, got {} times in:\n{}",
        count, formatted
    );
    assert!(
        formatted.contains("static var THOUGHT_POIGNANCY"),
        "static var missing"
    );
    assert!(formatted.contains("func custom()"), "custom func missing");
    assert!(formatted.contains("signal done()"), "signal missing");
}

#[test]
fn fmt_preserves_class_name_extends_same_line() {
    let source = "class_name Persona extends CharacterBody2D\n\nfunc _ready():\n\tpass\n";
    let config = default_config();
    let formatted = formatter::format_source(source, &config);

    assert!(
        formatted.contains("class_name Persona extends CharacterBody2D"),
        "class_name+extends on same line should be preserved, got:\n{}",
        formatted
    );
}

#[test]
fn fmt_preserves_multiline_var_declarations() {
    let source = "var default_max_tokens = ConfigManager.get_instance().config.get_value(\n\t\"setup\", \"DEFAULT_MAX_TOKENS\"\n)\nvar default_temperature = ConfigManager.get_instance().config.get_value(\n\t\"setup\", \"DEFAULT_TEMPERATURE\"\n)\n";
    let config = default_config();
    let formatted = formatter::format_source(source, &config);

    assert!(
        formatted.contains("\"DEFAULT_MAX_TOKENS\""),
        "first multi-line var args missing, got:\n{}",
        formatted
    );
    assert!(
        formatted.contains("\"DEFAULT_TEMPERATURE\""),
        "second multi-line var args missing, got:\n{}",
        formatted
    );
    let paren_count = formatted.matches(')').count();
    assert!(
        paren_count >= 4,
        "closing parens missing (need >=4, got {}), got:\n{}",
        paren_count,
        formatted
    );
}

#[test]
fn fmt_preserves_inner_class_boundary() {
    let source = "class_name MyScript\nextends Node\n\nfunc parse_response() -> String:\n\tvar result = \"default\"\n\treturn result\n\nclass InnerHelper:\n\tvar data: Dictionary\n\tvar name: String\n\tfunc _init(n: String):\n\t\tself.name = n\n\t\tself.data = {}\n";
    let config = default_config();
    let formatted = formatter::format_source(source, &config);

    assert!(
        formatted.contains("class InnerHelper:"),
        "inner class declaration missing"
    );
    assert!(
        formatted.contains("self.name = n"),
        "inner class body missing"
    );
    assert!(
        formatted.contains("self.data = {}"),
        "inner class body missing"
    );
    assert!(
        formatted.contains("var result = \"default\""),
        "function body before inner class missing"
    );
    assert!(
        formatted.contains("return result"),
        "return statement missing"
    );

    let second = formatter::format_source(&formatted, &config);
    assert_eq!(formatted, second, "must be idempotent");
}

#[test]
fn fmt_preserves_conditional_function_body() {
    let source = "func _object_out_of_range(area: Area2D):\n\tif area is Area2D:\n\t\t_untrack_object(area)\n";
    let config = default_config();
    let formatted = formatter::format_source(source, &config);

    assert!(
        formatted.contains("_untrack_object(area)"),
        "function body should be preserved, got:\n{}",
        formatted
    );
}

#[test]
fn fmt_reorder_with_enum_and_doc_comments() {
    // Regression: enum body was eaten when doc comments appeared before class_name
    // because multi-pass loop re-ran reorder after enum expansion.
    let source = "## Doc\n\nclass_name Test\nextends Node\n\n@onready var db: Node = %DB\n\nenum Mode { A, B, C }\n\nsignal done()\n";
    let config = default_config();
    let formatted = formatter::format_source(source, &config);

    // Enum should be fully expanded
    assert!(
        formatted.contains("\tA,"),
        "enum member A missing, got:\n{}",
        formatted
    );
    assert!(
        formatted.contains("\tB,"),
        "enum member B missing, got:\n{}",
        formatted
    );
    assert!(
        formatted.contains("\tC,"),
        "enum member C missing, got:\n{}",
        formatted
    );
    // Signal before enum
    let signal_pos = formatted.find("signal done()");
    let enum_pos = formatted.find("enum Mode");
    assert!(
        signal_pos.unwrap() < enum_pos.unwrap(),
        "signal should be before enum, got:\n{}",
        formatted
    );

    let second = formatter::format_source(&formatted, &config);
    assert_eq!(formatted, second, "must be idempotent");
}

// Fix A: doc comments attached to next declaration are not ordering violations

#[test]
fn ordering_inner_class_comment_first_body_line_not_flagged() {
    // Regression for issue #5: an inner class whose first body line is
    // a `##` doc comment (or any comment-only line) had its members
    // misclassified as siblings of the outer class. The lexer wasn't
    // emitting an Indent before the leading comment, so the parser's
    // `parse_indented_block` saw no Indent, returned an empty body,
    // and the `var a` token fell back to the outer parse loop —
    // landing AFTER the `class Bar` node and tripping
    // `order/class-member-order` on every inner-class member.
    let source = "class_name Foo extends Object\n## doc.\nclass Bar extends RefCounted:\n\t## doc for a.\n\tvar a: int\n";
    let config = default_config();
    let diagnostics = linter::lint_source(source, "test.gd", &config);
    let ordering: Vec<_> = diagnostics
        .iter()
        .filter(|d| d.rule == "order/class-member-order")
        .collect();
    assert!(
        ordering.is_empty(),
        "inner-class members with a leading comment must not trigger ordering, got:\n{}",
        ordering
            .iter()
            .map(|d| format!("  {}:{} {}", d.span.line, d.span.column, d.message))
            .collect::<Vec<_>>()
            .join("\n")
    );
}

#[test]
fn ordering_inner_class_multiple_leading_comments_not_flagged() {
    // Same family: multiple consecutive comment-only lines (mix of `#`
    // and `##`) at the start of an inner class body. Both the parser
    // and the formatter should treat them as part of the inner body.
    let source = "class_name Foo extends Object\n\n\nclass Bar extends RefCounted:\n\t# plain comment\n\t## doc comment\n\tvar a: int\n\tvar b: int\n";
    let config = default_config();
    let diagnostics = linter::lint_source(source, "test.gd", &config);
    let ordering: Vec<_> = diagnostics
        .iter()
        .filter(|d| d.rule == "order/class-member-order")
        .collect();
    assert!(
        ordering.is_empty(),
        "stacked leading comments in an inner class body must not trip ordering, got:\n{}",
        ordering
            .iter()
            .map(|d| format!("  {}:{} {}", d.span.line, d.span.column, d.message))
            .collect::<Vec<_>>()
            .join("\n")
    );
}

#[test]
fn ordering_doc_comment_before_static_func_not_flagged() {
    let source = "var x = 1\n\n## Docs for foo\n## More docs\nstatic func foo():\n\tpass\n";
    let config = default_config();
    let diagnostics = linter::lint_source(source, "test.gd", &config);
    let ordering: Vec<_> = diagnostics
        .iter()
        .filter(|d| d.rule == "order/class-member-order")
        .collect();
    assert!(
        ordering.is_empty(),
        "doc comment attached to static func should not cause ordering warning, got:\n{}",
        ordering
            .iter()
            .map(|d| format!("  {}:{} {}", d.span.line, d.span.column, d.message))
            .collect::<Vec<_>>()
            .join("\n")
    );
}

// Fix B: class_name/extends before doc comments at file top

#[test]
fn fmt_moves_class_name_before_doc_comments() {
    let source = "## Class documentation.\n## More docs.\nclass_name TestClass\nextends Node\n\nvar x = 10\n";
    let config = default_config();
    let formatted = formatter::format_source(source, &config);

    let cn_pos = formatted.find("class_name TestClass");
    let doc_pos = formatted.find("## Class documentation.");
    assert!(
        cn_pos.unwrap() < doc_pos.unwrap(),
        "class_name should appear before ## doc comments, got:\n{}",
        formatted
    );
    let ext_pos = formatted.find("extends Node");
    assert!(
        ext_pos.unwrap() < doc_pos.unwrap(),
        "extends should appear before ## doc comments, got:\n{}",
        formatted
    );

    let second = formatter::format_source(&formatted, &config);
    assert_eq!(
        formatted, second,
        "must be idempotent after doc comment reorder"
    );
}

#[test]
fn fmt_doc_comments_before_extends_only() {
    let source =
        "## Lightweight overlay.\n## Shows FPS.\nextends CanvasLayer\n\nfunc _ready():\n\tpass\n";
    let config = default_config();
    let formatted = formatter::format_source(source, &config);

    let ext_pos = formatted.find("extends CanvasLayer");
    let doc_pos = formatted.find("## Lightweight overlay.");
    assert!(
        ext_pos.unwrap() < doc_pos.unwrap(),
        "extends should appear before ## doc comments, got:\n{}",
        formatted
    );

    let second = formatter::format_source(&formatted, &config);
    assert_eq!(
        formatted, second,
        "must be idempotent after extends reorder"
    );
}

// Fix C: inner class member reordering

#[test]
fn fmt_reorders_inner_class_members() {
    let source = "class_name MyScript\nextends Node\n\nclass DialogueLine:\n\tvar speaker: String\n\tvar dialogue: String\n\tfunc _init(s: String, d: String):\n\t\tself.speaker = s\n\t\tself.dialogue = d\n\n\tstatic func from_dict(d: Dictionary) -> DialogueLine:\n\t\treturn DialogueLine.new(d.speaker, d.line)\n\n\tfunc _to_string():\n\t\treturn speaker\n";
    let config = default_config();
    let formatted = formatter::format_source(source, &config);

    let static_pos = formatted.find("static func from_dict");
    let init_pos = formatted.find("func _init");
    let tostr_pos = formatted.find("func _to_string");
    // Virtual methods (`_init`) come before all other methods; a `static`
    // method is just a regular method per Godot's style guide, so it sorts
    // after the virtuals, keeping its source position relative to the
    // other regular method `_to_string`.
    assert!(
        init_pos.unwrap() < static_pos.unwrap(),
        "_init (virtual) should be before static func in inner class, got:\n{}",
        formatted
    );
    assert!(
        static_pos.unwrap() < tostr_pos.unwrap(),
        "static func should keep its source order before _to_string, got:\n{}",
        formatted
    );

    let diagnostics = linter::lint_source(&formatted, "test.gd", &config);
    let ordering: Vec<_> = diagnostics
        .iter()
        .filter(|d| d.rule == "order/class-member-order")
        .collect();
    assert!(
        ordering.is_empty(),
        "no ordering warnings after inner class reorder, got:\n{}",
        ordering
            .iter()
            .map(|d| format!("  {}:{} {}", d.span.line, d.span.column, d.message))
            .collect::<Vec<_>>()
            .join("\n")
    );

    let second = formatter::format_source(&formatted, &config);
    assert_eq!(
        formatted, second,
        "must be idempotent after inner class reorder"
    );
}

#[test]
fn ordering_multiple_doc_comments_before_func_not_flagged() {
    // Multiple ## doc comment lines between vars and a static func should
    // not produce ordering warnings; they document the function.
    let source = "var x = 1\nvar y = 2\n\n## Normalize data for strict mode.\n## Ensures all required fields present.\n## Removes deprecated properties.\nstatic func normalize(data: Dictionary) -> Dictionary:\n\treturn data\n";
    let config = default_config();
    let diagnostics = linter::lint_source(source, "test.gd", &config);
    let ordering: Vec<_> = diagnostics
        .iter()
        .filter(|d| d.rule == "order/class-member-order")
        .collect();
    assert!(
        ordering.is_empty(),
        "multi-line doc comment block before static func should not trigger ordering, got:\n{}",
        ordering
            .iter()
            .map(|d| format!("  {}:{} {}", d.span.line, d.span.column, d.message))
            .collect::<Vec<_>>()
            .join("\n")
    );
}

#[test]
fn fmt_full_reorder_with_doc_comments_and_multiline_vars() {
    // Comprehensive test: reorder with doc comments, multi-line vars, inner class.
    let source = r#"## Quest system docs.
## More details.
class_name QuestSystem
extends Node

@onready var ui: Control = %UI
@export var max_quests: int = 10

var _cache: Dictionary = {}
var reward_xp = GameSettings.load_value(
	"quests", "REWARD_XP"
)

const MAX_HISTORY = 500

signal quest_accept

## Normalize quest data.
static func normalize(data: Dictionary) -> Dictionary:
	return data

func _ready():
	pass

class Objective:
	var desc: String
	var target: int
	func _init(d: String, t: int):
		self.desc = d
		self.target = t

	static func from_dict(d: Dictionary) -> Objective:
		return Objective.new(d.desc, d.target)

	func is_done() -> bool:
		return false
"#;
    let config = default_config();
    let formatted = formatter::format_source(source, &config);

    // class_name before docs
    assert!(
        formatted.find("class_name").unwrap() < formatted.find("## Quest system").unwrap(),
        "class_name before docs, got:\n{}",
        formatted
    );
    // extends before docs
    assert!(
        formatted.find("extends Node").unwrap() < formatted.find("## Quest system").unwrap(),
        "extends before docs, got:\n{}",
        formatted
    );
    // signal before enum/const
    assert!(
        formatted.find("signal quest_accept").unwrap()
            < formatted.find("const MAX_HISTORY").unwrap(),
        "signal before const, got:\n{}",
        formatted
    );
    // Multi-line var preserved
    assert!(
        formatted.contains("\"quests\", \"REWARD_XP\""),
        "multi-line var args preserved, got:\n{}",
        formatted
    );
    // Doc comment stays with static func
    assert!(
        formatted.contains("## Normalize quest data.\nstatic func normalize"),
        "doc comment attached to static func, got:\n{}",
        formatted
    );
    // Inner class: virtual `_init` comes before the static method.
    assert!(
        formatted.find("func _init").unwrap() < formatted.find("static func from_dict").unwrap(),
        "inner class _init (virtual) before static func, got:\n{}",
        formatted
    );
    // No ordering warnings
    let diagnostics = linter::lint_source(&formatted, "test.gd", &config);
    let ordering: Vec<_> = diagnostics
        .iter()
        .filter(|d| d.rule == "order/class-member-order")
        .collect();
    assert!(
        ordering.is_empty(),
        "no ordering warnings after full reorder, got:\n{}",
        ordering
            .iter()
            .map(|d| format!("  {}:{} {}", d.span.line, d.span.column, d.message))
            .collect::<Vec<_>>()
            .join("\n")
    );
    // Idempotent
    let second = formatter::format_source(&formatted, &config);
    assert_eq!(formatted, second, "must be idempotent");
}

#[test]
fn fmt_inner_class_preserves_multiline_body() {
    // Inner class with multi-line function bodies should not be corrupted.
    let source = "class_name Game\nextends Node\n\nclass Stats:\n\tvar hp: int\n\tvar mp: int\n\n\tfunc _init(h: int, m: int):\n\t\tself.hp = h\n\t\tself.mp = m\n\n\tstatic func default() -> Stats:\n\t\treturn Stats.new(100, 50)\n\n\tfunc to_dict() -> Dictionary:\n\t\treturn {\"hp\": hp, \"mp\": mp}\n";
    let config = default_config();
    let formatted = formatter::format_source(source, &config);

    // All bodies preserved
    assert!(formatted.contains("self.hp = h"), "init body preserved");
    assert!(formatted.contains("self.mp = m"), "init body preserved");
    assert!(
        formatted.contains("Stats.new(100, 50)"),
        "static func body preserved"
    );
    assert!(
        formatted.contains("{\"hp\": hp, \"mp\": mp}"),
        "to_dict body preserved"
    );
    // Virtual `_init` comes before the static method.
    assert!(
        formatted.find("func _init").unwrap() < formatted.find("static func default").unwrap(),
        "_init (virtual) before static func in inner class"
    );
    // Idempotent
    let second = formatter::format_source(&formatted, &config);
    assert_eq!(formatted, second, "must be idempotent");
}

// Line breaking tests

#[test]
fn fmt_breaks_long_function_signature() {
    let source = "func take_damage(amount: int, source: Node, damage_type: String, is_critical: bool, knockback: float, effect: String) -> void:\n\tpass\n";
    let config = default_config();
    let formatted = formatter::format_source(source, &config);

    // Should be broken into multiple lines
    assert!(
        formatted.contains("func take_damage(\n"),
        "should break after opening paren, got:\n{}",
        formatted
    );
    assert!(
        formatted.contains("\tamount: int,"),
        "params should be on separate lines, got:\n{}",
        formatted
    );
    assert!(
        formatted.contains("\teffect: String,"),
        "last param should have trailing comma, got:\n{}",
        formatted
    );
    assert!(
        formatted.contains(") -> void:"),
        "return type on closing line, got:\n{}",
        formatted
    );
    assert!(
        formatted.contains("\tpass"),
        "body preserved, got:\n{}",
        formatted
    );

    let second = formatter::format_source(&formatted, &config);
    assert_eq!(formatted, second, "must be idempotent");
}

#[test]
fn fmt_breaks_long_function_call() {
    let source = "func _ready():\n\tvar result = some_really_long_function_name(argument_one, argument_two, argument_three, argument_four)\n";
    let config = default_config();
    let formatted = formatter::format_source(source, &config);

    assert!(
        formatted.contains("some_really_long_function_name(\n"),
        "should break call, got:\n{}",
        formatted
    );
    assert!(
        formatted.contains("\t\targument_one,"),
        "args indented, got:\n{}",
        formatted
    );
    assert!(
        formatted.contains("\t)"),
        "closing paren indented to call level, got:\n{}",
        formatted
    );

    let second = formatter::format_source(&formatted, &config);
    assert_eq!(formatted, second, "must be idempotent");
}

#[test]
fn fmt_breaks_long_dictionary_literal() {
    let source = "var data = {\"key_one\": value_one, \"key_two\": value_two, \"key_three\": value_three, \"key_four\": value_four, \"key_five\": value_five}\n";
    let config = default_config();
    let formatted = formatter::format_source(source, &config);

    assert!(
        formatted.contains("var data = {\n"),
        "should break dict, got:\n{}",
        formatted
    );
    assert!(
        formatted.contains("\t\"key_one\": value_one,"),
        "entries on own lines, got:\n{}",
        formatted
    );
    assert!(
        formatted.contains("}"),
        "closing brace present, got:\n{}",
        formatted
    );

    let second = formatter::format_source(&formatted, &config);
    assert_eq!(formatted, second, "must be idempotent");
}

#[test]
fn fmt_breaks_long_array_literal() {
    let source = "var items = [item_one, item_two, item_three, item_four, item_five, item_six, item_seven, item_eight, item_nine]\n";
    let config = default_config();
    let formatted = formatter::format_source(source, &config);

    assert!(
        formatted.contains("var items = [\n"),
        "should break array, got:\n{}",
        formatted
    );
    assert!(
        formatted.contains("\titem_one,"),
        "elements on own lines, got:\n{}",
        formatted
    );
    assert!(
        formatted.contains("]"),
        "closing bracket present, got:\n{}",
        formatted
    );

    let second = formatter::format_source(&formatted, &config);
    assert_eq!(formatted, second, "must be idempotent");
}

#[test]
fn fmt_does_not_break_short_lines() {
    let source = "func foo(a: int, b: int) -> void:\n\tvar x = bar(1, 2)\n\tvar y = [1, 2, 3]\n";
    let config = default_config();
    let formatted = formatter::format_source(source, &config);

    // These lines are under 100 chars, should not be broken
    assert!(
        formatted.contains("func foo(a: int, b: int) -> void:"),
        "short sig should not break, got:\n{}",
        formatted
    );
    assert!(
        formatted.contains("var x = bar(1, 2)"),
        "short call should not break, got:\n{}",
        formatted
    );
}

#[test]
fn fmt_does_not_break_long_string_without_delimiters() {
    let source = "func _ready():\n\tprint(\"This is a very long string that exceeds the line length limit but cannot be broken because it is a single argument\")\n";
    let config = default_config();
    let formatted = formatter::format_source(source, &config);

    // Single argument in parens; breaking wouldn't help
    assert!(
        !formatted.contains("print(\n"),
        "single-arg call should not break, got:\n{}",
        formatted
    );
}

#[test]
fn fmt_preserves_nested_calls_during_break() {
    let source = "func _ready():\n\tvar result = outer_func(inner_one(a, b), inner_two(c, d), inner_three(e, f), inner_four(g, h))\n";
    let config = default_config();
    let formatted = formatter::format_source(source, &config);

    // Should break at top-level commas, not inside inner calls
    assert!(
        formatted.contains("inner_one(a, b),"),
        "nested call preserved intact, got:\n{}",
        formatted
    );
    assert!(
        formatted.contains("inner_two(c, d),"),
        "nested call preserved intact, got:\n{}",
        formatted
    );

    let second = formatter::format_source(&formatted, &config);
    assert_eq!(formatted, second, "must be idempotent");
}

// Quality rule suppression tests

#[test]
fn quality_max_function_length_suppressible() {
    // Build a function with 55 lines (over default limit of 50)
    let mut source =
        String::from("# gdstyle:ignore=quality/max-function-length\nfunc long_func():\n");
    for i in 0..55 {
        source.push_str(&format!("\tvar v{} = {}\n", i, i));
    }
    let config = default_config();
    let diagnostics = linter::lint_source(&source, "test.gd", &config);
    let quality: Vec<_> = diagnostics
        .iter()
        .filter(|d| d.rule == "quality/max-function-length")
        .collect();
    assert!(
        quality.is_empty(),
        "max-function-length should be suppressible with inline comment"
    );
}

#[test]
fn quality_max_parameters_suppressible() {
    let source = "func many_params(a: int, b: int, c: int, d: int, e: int, f: int, g: int):  # gdstyle:ignore=quality/max-parameters\n\tpass\n";
    let config = default_config();
    let diagnostics = linter::lint_source(source, "test.gd", &config);
    let quality: Vec<_> = diagnostics
        .iter()
        .filter(|d| d.rule == "quality/max-parameters")
        .collect();
    assert!(
        quality.is_empty(),
        "max-parameters should be suppressible with same-line comment"
    );
}

// === Corner case tests ===

// Line breaker: escaped backslash before closing quote
#[test]
fn fmt_line_break_escaped_backslash_in_string() {
    // "path\\" ends with a literal backslash. The line breaker must not
    // think the closing quote is escaped.
    let source = "func _ready():\n\tvar result = some_func(\"path\\\\\", \"other\\\\\", \"third\\\\\", \"fourth\\\\\")\n";
    let config = default_config();
    let formatted = formatter::format_source(source, &config);
    // All string args must survive intact
    assert!(
        formatted.contains("\"path\\\\\""),
        "escaped backslash string must survive, got:\n{}",
        formatted
    );
    assert!(
        formatted.contains("\"fourth\\\\\""),
        "last arg must survive, got:\n{}",
        formatted
    );
    let second = formatter::format_source(&formatted, &config);
    assert_eq!(formatted, second, "must be idempotent");
}

// Line breaker: commas inside strings should NOT be split points
#[test]
fn fmt_line_break_commas_inside_strings() {
    let source = "func _ready():\n\tvar result = make_query(\"hello, world\", \"a, b, c\", \"x, y\", \"final\")\n";
    let config = default_config();
    let formatted = formatter::format_source(source, &config);
    // The commas inside strings must NOT be split points
    assert!(
        formatted.contains("\"hello, world\","),
        "string with commas must be intact, got:\n{}",
        formatted
    );
    assert!(
        formatted.contains("\"a, b, c\","),
        "string with commas must be intact, got:\n{}",
        formatted
    );
    let second = formatter::format_source(&formatted, &config);
    assert_eq!(formatted, second, "must be idempotent");
}

// Line breaker: empty parameter list should not break
#[test]
fn fmt_line_break_empty_params() {
    let source = "func a_very_long_function_name_that_alone_exceeds_the_line_length_limit_without_any_parameters() -> Dictionary:\n\tpass\n";
    let config = default_config();
    let formatted = formatter::format_source(source, &config);
    // Should NOT break inside empty ()
    assert!(
        !formatted.contains("(\n)"),
        "should not break empty params, got:\n{}",
        formatted
    );
}

// Line breaker: trailing comma already present
#[test]
fn fmt_line_break_trailing_comma_already_present() {
    let source = "func _ready():\n\tvar result = some_func(argument_one, argument_two, argument_three, argument_four, argument_five,)\n";
    let config = default_config();
    let formatted = formatter::format_source(source, &config);
    // Should not produce double commas
    assert!(
        !formatted.contains(",,"),
        "must not produce double commas, got:\n{}",
        formatted
    );
    let second = formatter::format_source(&formatted, &config);
    assert_eq!(formatted, second, "must be idempotent");
}

// Line breaker: inline comment after closing paren
#[test]
fn fmt_line_break_inline_comment() {
    let source = "func attack(target: Node, damage: int, crit: bool, knockback: float, effect: String) -> void: # important\n\tpass\n";
    let config = default_config();
    let formatted = formatter::format_source(source, &config);
    // Comment should be preserved on the closing line
    assert!(
        formatted.contains(") -> void: # important"),
        "inline comment must be on closing line, got:\n{}",
        formatted
    );
    assert!(
        formatted.contains("\ttarget: Node,"),
        "params should be broken, got:\n{}",
        formatted
    );
}

// Line breaker: typed arrays with nested brackets
#[test]
fn fmt_line_break_typed_arrays() {
    let source = "func process(items: Array[Dictionary[String, Variant]], callbacks: Array[Callable], options: Dictionary, flags: int) -> void:\n\tpass\n";
    let config = default_config();
    let formatted = formatter::format_source(source, &config);
    // Nested brackets should NOT be split
    assert!(
        formatted.contains("items: Array[Dictionary[String, Variant]],"),
        "typed array must stay intact, got:\n{}",
        formatted
    );
    let second = formatter::format_source(&formatted, &config);
    assert_eq!(formatted, second, "must be idempotent");
}

// Naming: digit-to-uppercase transitions
#[test]
fn to_snake_case_digit_uppercase() {
    // Names like Vector2D, Area2D are common in GDScript
    let source = "func _is_valid_hitbox_area_2D(node):\n\tpass\n";
    let config = default_config();
    let diagnostics = linter::lint_source(source, "test.gd", &config);
    let naming: Vec<_> = diagnostics
        .iter()
        .filter(|d| d.rule == "naming/function-name-snake-case")
        .collect();
    // Should suggest something reasonable (not panic or produce empty string)
    assert!(
        !naming.is_empty(),
        "should flag non-snake-case function name"
    );
    if let Some(fix) = &naming[0].fix {
        let suggested = &fix.replacements[0].new_text;
        assert!(!suggested.is_empty(), "suggestion should not be empty");
        assert!(
            !suggested.contains("__"),
            "suggestion should not have double underscores, got: {}",
            suggested
        );
    }
}

// Naming: single-letter class names
#[test]
fn naming_single_letter_class_name() {
    let source = "class_name A\nextends Node\n";
    let config = default_config();
    let diagnostics = linter::lint_source(source, "test.gd", &config);
    // 'A' is a valid PascalCase-ish name (debatable) but should not crash
    // The rule might flag it since is_pascal_case requires lowercase chars
    // Just ensure no panic
    assert!(diagnostics.iter().all(|d| !d.message.is_empty()));
}

// Reorder: empty file
#[test]
fn fmt_reorder_empty_file() {
    let config = default_config();
    let formatted = formatter::format_source("", &config);
    // Should not panic on empty input
    assert!(formatted.is_empty() || formatted == "\n");
}

// Reorder: file with only comments
#[test]
fn fmt_reorder_only_comments() {
    let source = "# Just a comment\n# Another one\n";
    let config = default_config();
    let formatted = formatter::format_source(source, &config);
    assert!(
        formatted.contains("# Just a comment"),
        "comments should be preserved"
    );
    let second = formatter::format_source(&formatted, &config);
    assert_eq!(formatted, second, "must be idempotent");
}

// Reorder: @tool file
#[test]
fn fmt_reorder_tool_file() {
    let source = "@tool\nclass_name MyPlugin\nextends EditorPlugin\n\nfunc _ready():\n\tpass\n\nvar x = 10\n";
    let config = default_config();
    let formatted = formatter::format_source(source, &config);
    // @tool should stay first
    assert!(
        formatted.starts_with("@tool"),
        "@tool must be first, got:\n{}",
        formatted
    );
    // var should be before func after reorder
    let var_pos = formatted.find("var x");
    let func_pos = formatted.find("func _ready");
    assert!(
        var_pos.unwrap() < func_pos.unwrap(),
        "var before func, got:\n{}",
        formatted
    );
    let second = formatter::format_source(&formatted, &config);
    assert_eq!(formatted, second, "must be idempotent");
}

// Reorder: file with only inner classes
#[test]
fn fmt_reorder_only_inner_classes() {
    let source = "class StateIdle:\n\tfunc enter():\n\t\tpass\n\nclass StateRunning:\n\tfunc enter():\n\t\tpass\n";
    let config = default_config();
    let formatted = formatter::format_source(source, &config);
    // Both classes should be preserved, order unchanged (same category)
    assert!(
        formatted.contains("class StateIdle:"),
        "first class preserved"
    );
    assert!(
        formatted.contains("class StateRunning:"),
        "second class preserved"
    );
    assert!(
        formatted.find("StateIdle").unwrap() < formatted.find("StateRunning").unwrap(),
        "order preserved"
    );
    let second = formatter::format_source(&formatted, &config);
    assert_eq!(formatted, second, "must be idempotent");
}

// Enum fix: enum with value assignments should preserve them
#[test]
fn fmt_enum_with_value_assignments_already_multiline() {
    let source = "enum State {\n\tIDLE = 0,\n\tRUNNING = 1,\n\tJUMPING = 2,\n}\n";
    let config = default_config();
    let formatted = formatter::format_source(source, &config);
    assert!(
        formatted.contains("IDLE = 0"),
        "value assignment must be preserved, got:\n{}",
        formatted
    );
    assert!(
        formatted.contains("RUNNING = 1"),
        "value assignment must be preserved, got:\n{}",
        formatted
    );
}

#[test]
fn fix_enum_value_assignments_preserved_during_expansion() {
    // Single-line enum with value assignments must preserve them when expanded.
    let source = "enum State { IDLE = 0, RUNNING = 1, JUMPING = 2 }\n";
    let config = default_config();
    let diagnostics = linter::lint_source(source, "test.gd", &config);
    let fixed = fixer::apply_fixes(source, &diagnostics, true);
    assert!(
        fixed.contains("IDLE = 0,"),
        "value assignment must be preserved during expansion, got:\n{}",
        fixed
    );
    assert!(
        fixed.contains("RUNNING = 1,"),
        "value assignment must be preserved, got:\n{}",
        fixed
    );
    assert!(
        fixed.contains("JUMPING = 2,"),
        "value assignment must be preserved, got:\n{}",
        fixed
    );
}

// Blank lines: at end of file (edge case - only caught by trailing newline normalizer)
#[test]
fn fmt_blank_lines_at_end_of_file() {
    let source = "extends Node\n\n\n\n";
    let config = default_config();
    let formatted = formatter::format_source(source, &config);
    // Should end with exactly one newline
    assert!(formatted.ends_with('\n'), "should end with newline");
    assert!(
        !formatted.ends_with("\n\n"),
        "should not end with double newline, got:\n{:?}",
        formatted
    );
}

#[test]
fn fmt_normalises_spacing_in_class_header() {
    // The file starts with `@tool` / `class_name` / `extends` separated by
    // doubled blank lines. The canonical Godot layout clusters these tightly.
    // Regression test for the original report on GARP/log-stream.gd.
    let source = "@tool\n\n\nclass_name LogStream\n\n\nextends Node\n\n\n## Class doc.\n\n\nsignal log_message\n\n\nsignal log_warning\n";
    let config = default_config();
    let formatted = formatter::format_source(source, &config);
    assert!(
        formatted.starts_with("@tool\nclass_name LogStream\nextends Node\n## Class doc.\n"),
        "header items should cluster tight, got:\n{}",
        formatted
    );
    assert!(
        formatted.contains("signal log_message\nsignal log_warning"),
        "consecutive signals should be tight, got:\n{}",
        formatted
    );
}

#[test]
fn fmt_parses_var_with_computed_property_getset() {
    // `var foo:` followed by an indented `get:` / `set(...)` block is a
    // GDScript computed property. The parser must consume that whole block
    // as part of the variable, and the formatter must leave it intact.
    let source = "extends Node\n\n## Property doc.\nvar value:\n\tget:\n\t\treturn _value\n\tset(v):\n\t\t_value = v\n\nfunc _ready():\n\tpass\n";
    let config = default_config();
    let formatted = formatter::format_source(source, &config);
    assert!(
        formatted.contains("## Property doc.\nvar value:"),
        "doc must stay attached to computed-property var, got:\n{}",
        formatted
    );
    assert!(
        formatted.contains("\tget:") && formatted.contains("\tset(v):"),
        "get/set bodies must be preserved, got:\n{}",
        formatted
    );
}

#[test]
fn fmt_doc_comment_between_two_functions_is_preserved() {
    // After a function body, a `##` doc comment that documents the next
    // function must remain visible to the outer parser (not be swallowed
    // by the body line counter) and tight against the function it documents.
    let source = "extends Node\n\nfunc _ready():\n\tpass\n\n## Normalize quest data.\nstatic func normalize() -> void:\n\tpass\n";
    let config = default_config();
    let formatted = formatter::format_source(source, &config);
    assert!(
        formatted.contains("## Normalize quest data.\nstatic func normalize"),
        "doc must stay attached to the next function, got:\n{}",
        formatted
    );
}

// --- Member-spacing regression suite -----------------------------------------
// The original report from `GARP/log-stream.gd` was "lots of wasted blank
// space at the top". The cases below cover each pair of adjacent
// declarations the spacing pass has to handle.

#[test]
fn fmt_member_spacing_tightens_header_block() {
    let source = "@tool\n\n\nclass_name Foo\n\n\nextends Node\n";
    let formatted = formatter::format_source(source, &default_config());
    assert_eq!(
        formatted, "@tool\nclass_name Foo\nextends Node\n",
        "@tool/class_name/extends must cluster tight, got:\n{}",
        formatted
    );
}

#[test]
fn fmt_member_spacing_standalone_class_doc_after_extends() {
    // A `##` block separated from the next member by a blank is a
    // free-standing class docstring; it must sit tight under `extends`
    // and get a single blank line before the first member.
    let source = "class_name Foo\nextends Node\n\n\n## Class doc.\n\n\nsignal x\n";
    let formatted = formatter::format_source(source, &default_config());
    assert!(
        formatted.starts_with("class_name Foo\nextends Node\n## Class doc.\n\nsignal x\n"),
        "standalone class doc tight under extends, got:\n{}",
        formatted
    );
}

#[test]
fn fmt_member_spacing_collapses_doubled_blanks_between_signals() {
    let source = "extends Node\n\nsignal a\n\n\nsignal b\n\n\nsignal c\n";
    let formatted = formatter::format_source(source, &default_config());
    assert!(
        formatted.contains("signal a\nsignal b\nsignal c"),
        "consecutive signals must be tight, got:\n{}",
        formatted
    );
}

#[test]
fn fmt_member_spacing_two_blanks_around_each_function() {
    let source = "extends Node\n\nvar x = 1\n\nfunc a():\n\tpass\n\nfunc b():\n\tpass\n";
    let formatted = formatter::format_source(source, &default_config());
    assert!(
        formatted.contains("var x = 1\n\n\nfunc a():"),
        "var -> func should have 2 blank lines, got:\n{}",
        formatted
    );
    assert!(
        formatted.contains("\tpass\n\n\nfunc b():"),
        "func -> func should have 2 blank lines, got:\n{}",
        formatted
    );
}

#[test]
fn fmt_member_spacing_one_blank_between_member_categories() {
    let source = "extends Node\n\nsignal s\n\n\n\n\nconst K = 1\n\n\n\nvar x = 2\n";
    let formatted = formatter::format_source(source, &default_config());
    assert!(
        formatted.contains("signal s\n\nconst K = 1\n\nvar x = 2"),
        "different categories must be separated by exactly one blank, got:\n{}",
        formatted
    );
}

// --- Computed-property var regression suite ----------------------------------

#[test]
fn fmt_computed_property_with_export_annotation() {
    // A `var name:` that opens a get/set block, with `@export` above it,
    // must be parsed as a single variable, body preserved, doc attached.
    let source = "extends Node\n\n## The thing.\n@export var thing:\n\tget:\n\t\treturn _thing\n\tset(v):\n\t\t_thing = v\n\nfunc _ready():\n\tpass\n";
    let formatted = formatter::format_source(source, &default_config());
    assert!(
        formatted.contains("## The thing.\n@export var thing:"),
        "doc + annotation must stay attached to the var, got:\n{}",
        formatted
    );
    assert!(
        formatted.contains("\tget:") && formatted.contains("\tset(v):"),
        "get/set block must survive intact, got:\n{}",
        formatted
    );
}

#[test]
fn fmt_computed_property_with_explicit_type_hint() {
    // `var name: int:` (typed computed property) is also valid syntax;
    // the trailing `:` opens the get/set block.
    let source = "extends Node\n\nvar counter: int:\n\tget:\n\t\treturn _counter\n\tset(v):\n\t\t_counter = v\n\nfunc _ready():\n\tpass\n";
    let formatted = formatter::format_source(source, &default_config());
    assert!(
        formatted.contains("var counter: int:\n\tget:"),
        "typed computed property must parse correctly, got:\n{}",
        formatted
    );
}

#[test]
fn fmt_regular_var_with_value_unaffected_by_computed_property_handling() {
    // A regular `var name = value` must NOT trigger any get/set block
    // consumption (regression guard for the parser change).
    let source = "extends Node\n\nvar a = 1\nvar b = 2\nvar c = 3\n\nfunc _ready():\n\tpass\n";
    let formatted = formatter::format_source(source, &default_config());
    assert!(
        formatted.contains("var a = 1\nvar b = 2\nvar c = 3"),
        "consecutive plain vars must survive intact, got:\n{}",
        formatted
    );
}

// --- Colon-spacing regression suite ------------------------------------------
// Style guide: no space before `:`, one space after (except `:=` and `\n`).

#[test]
fn fmt_colon_spacing_in_type_hints() {
    let source = "extends Node\n\nconst X:float = 1.0\nvar y:int = 0\n@export var z:Dictionary = {}\n";
    let formatted = formatter::format_source(source, &default_config());
    assert!(
        formatted.contains("const X: float = 1.0"),
        "const type hint: missing space after ':', got:\n{}",
        formatted
    );
    assert!(
        formatted.contains("var y: int = 0"),
        "var type hint: missing space after ':', got:\n{}",
        formatted
    );
    assert!(
        formatted.contains("@export var z: Dictionary"),
        "annotated var type hint: missing space after ':', got:\n{}",
        formatted
    );
}

#[test]
fn fmt_colon_spacing_in_function_parameters() {
    let source = "extends Node\n\nfunc f(a:int, b:String = \"x\") -> bool:\n\treturn true\n";
    let formatted = formatter::format_source(source, &default_config());
    assert!(
        formatted.contains("func f(a: int, b: String = \"x\")"),
        "function parameter type hints: missing space after ':', got:\n{}",
        formatted
    );
}

#[test]
fn fmt_colon_spacing_strips_space_before_block_colon() {
    let source = "extends Node\n\nfunc _ready() :\n\tif true :\n\t\tpass\n\twhile false :\n\t\tpass\n";
    let formatted = formatter::format_source(source, &default_config());
    assert!(
        formatted.contains("func _ready():"),
        "func signature: no space before ':', got:\n{}",
        formatted
    );
    assert!(
        formatted.contains("if true:"),
        "if: no space before ':', got:\n{}",
        formatted
    );
    assert!(
        formatted.contains("while false:"),
        "while: no space before ':', got:\n{}",
        formatted
    );
}

#[test]
fn fmt_colon_spacing_preserves_walrus_inferred_type() {
    // `:=` must not have its space-before stripped (`var x := 1` is the
    // canonical form, not `var x:= 1`).
    let source = "extends Node\n\nfunc f() -> void:\n\tvar x := 1\n\tvar y := \"two\"\n";
    let formatted = formatter::format_source(source, &default_config());
    assert!(
        formatted.contains("var x := 1"),
        ":= must keep its leading space, got:\n{}",
        formatted
    );
    assert!(
        formatted.contains("var y := \"two\""),
        ":= must keep its leading space, got:\n{}",
        formatted
    );
}

#[test]
fn fmt_colon_spacing_in_dict_keys() {
    let source = "extends Node\n\nvar d = {\"a\":1, \"b\":2}\n";
    let formatted = formatter::format_source(source, &default_config());
    assert!(
        formatted.contains("{\"a\": 1, \"b\": 2}"),
        "dict keys: missing space after ':', got:\n{}",
        formatted
    );
}

// --- Comma-spacing regression suite ------------------------------------------
// Style guide: no space before `,`, one space after (except trailing).

#[test]
fn fmt_comma_spacing_in_function_call_args() {
    let source = "extends Node\n\nfunc f() -> void:\n\tInput.get_axis(\"a\",\"b\",\"c\")\n";
    let formatted = formatter::format_source(source, &default_config());
    assert!(
        formatted.contains("Input.get_axis(\"a\", \"b\", \"c\")"),
        "function args: missing space after ',', got:\n{}",
        formatted
    );
}

#[test]
fn fmt_comma_spacing_in_array_and_dict_literals() {
    let source = "extends Node\n\nvar arr = [1,2,3,4]\nvar d = {\"a\":1,\"b\":2,\"c\":3}\n";
    let formatted = formatter::format_source(source, &default_config());
    assert!(
        formatted.contains("[1, 2, 3, 4]"),
        "array literal: missing space after ',', got:\n{}",
        formatted
    );
    assert!(
        formatted.contains("{\"a\": 1, \"b\": 2, \"c\": 3}"),
        "dict literal: missing space after ',', got:\n{}",
        formatted
    );
}

#[test]
fn fmt_comma_spacing_strips_space_before_comma() {
    let source = "extends Node\n\nfunc f() -> void:\n\tvar a = [1 ,2 ,3]\n\tfn(x ,y ,z)\n";
    let formatted = formatter::format_source(source, &default_config());
    assert!(
        formatted.contains("[1, 2, 3]"),
        "array: must strip stray space before ',', got:\n{}",
        formatted
    );
    assert!(
        formatted.contains("fn(x, y, z)"),
        "call: must strip stray space before ',', got:\n{}",
        formatted
    );
}

#[test]
fn fmt_comma_spacing_preserves_trailing_comma() {
    // A comma followed by `\n` (multi-line trailing comma) or by a closing
    // delimiter must not trip the "space after" check.
    let source = "extends Node\n\nfunc f() -> void:\n\tvar arr = [\n\t\t1,\n\t\t2,\n\t]\n";
    let formatted = formatter::format_source(source, &default_config());
    assert!(
        formatted.contains(",\n\t\t2,\n\t]"),
        "trailing commas must not be flagged, got:\n{}",
        formatted
    );
}

#[test]
fn fmt_combined_colon_and_comma_fixes_match_canonical_godot_form() {
    // End-to-end regression covering A1+A2+A3 together against a realistic
    // function signature and body.
    let source = "extends Node\n\nfunc add_item(item_id:String,count:int = 1,rarity:int = 0) -> bool :\n\tvar pairs = [{\"a\":1,\"b\":2}, {\"c\":3,\"d\":4}]\n\treturn true\n";
    let formatted = formatter::format_source(source, &default_config());
    assert!(
        formatted.contains("func add_item(item_id: String, count: int = 1, rarity: int = 0) -> bool:"),
        "combined signature spacing, got:\n{}",
        formatted
    );
    assert!(
        formatted.contains("{\"a\": 1, \"b\": 2}") && formatted.contains("{\"c\": 3, \"d\": 4}"),
        "combined dict spacing, got:\n{}",
        formatted
    );
}

// Fixer: rename doesn't affect string literals
#[test]
fn fixer_rename_does_not_affect_strings() {
    let source =
        "var CONFIG = 1\nfunc _ready():\n\tprint(\"CONFIG is important\")\n\tprint(CONFIG)\n";
    let config = default_config();
    let diagnostics = linter::lint_source(source, "test.gd", &config);
    let fixed = fixer::apply_fixes(source, &diagnostics, false);
    // The string "CONFIG is important" must NOT be changed
    assert!(
        fixed.contains("\"CONFIG is important\""),
        "string literal should not be renamed, got:\n{}",
        fixed
    );
    // But the identifier reference should be renamed
    assert!(
        fixed.contains("print(config)"),
        "identifier should be renamed, got:\n{}",
        fixed
    );
}

// Formatter: multiline string should not be broken
#[test]
fn fmt_does_not_break_multiline_string() {
    let source = "var text = \"This is a very long string with commas, periods, and other punctuation that exceeds the line length limit but should not be broken\"\n";
    let config = default_config();
    let formatted = formatter::format_source(source, &config);
    // Single-argument call with long string; breaking wouldn't help
    // The string content must remain intact
    assert!(
        formatted.contains("commas, periods, and other punctuation"),
        "string must not be split, got:\n{}",
        formatted
    );
}

// Formatter: GDScript string prefixes
#[test]
fn fmt_handles_string_prefix_in_line_break() {
    let source = "func _ready():\n\tvar result = make_node(&\"StringName1\", &\"StringName2\", &\"StringName3\", &\"StringName4\", &\"StringName5\")\n";
    let config = default_config();
    let formatted = formatter::format_source(source, &config);
    // &"..." strings should survive line breaking intact
    assert!(
        formatted.contains("&\"StringName1\""),
        "string name prefix must survive, got:\n{}",
        formatted
    );
    assert!(
        formatted.contains("&\"StringName5\""),
        "last string name must survive, got:\n{}",
        formatted
    );
    let second = formatter::format_source(&formatted, &config);
    assert_eq!(formatted, second, "must be idempotent");
}

// Signal past-tense: single-word signals
#[test]
fn signal_past_tense_single_word() {
    let source = "signal connect\n";
    let config = default_config();
    let diagnostics = linter::lint_source(source, "test.gd", &config);
    let past: Vec<_> = diagnostics
        .iter()
        .filter(|d| d.rule == "naming/signal-past-tense")
        .collect();
    // "connect" is not past tense and not a noun
    assert!(
        !past.is_empty(),
        "single-word verb signal should be flagged"
    );
}

// Reorder: annotations attached to members move with them
#[test]
fn fmt_reorder_annotations_move_with_members() {
    let source =
        "extends Node\n\nfunc _ready():\n\tpass\n\n@export_range(0, 100)\nvar health: int = 100\n";
    let config = default_config();
    let formatted = formatter::format_source(source, &config);
    // @export_range should stay attached to var health and appear before func
    assert!(
        formatted.contains("@export_range(0, 100)\nvar health"),
        "@export_range must stay with var, got:\n{}",
        formatted
    );
    let export_pos = formatted.find("@export_range");
    let func_pos = formatted.find("func _ready");
    assert!(
        export_pos.unwrap() < func_pos.unwrap(),
        "@export var before func, got:\n{}",
        formatted
    );
}

// Reorder: blank lines between doc comments and class_name with gap
#[test]
fn fmt_doc_comments_with_blank_line_before_class_name() {
    let source = "## Class docstring\n\nclass_name MyClass\nextends Node\n";
    let config = default_config();
    let formatted = formatter::format_source(source, &config);
    let cn_pos = formatted.find("class_name");
    let doc_pos = formatted.find("## Class docstring");
    assert!(
        cn_pos.unwrap() < doc_pos.unwrap(),
        "class_name before doc, got:\n{}",
        formatted
    );
    let second = formatter::format_source(&formatted, &config);
    assert_eq!(formatted, second, "must be idempotent");
}

// Unicode identifier support
#[test]
fn lexer_handles_unicode_identifiers() {
    let source = "var café = 1\nvar naïve = 2\nvar über = 3\n";
    let config = default_config();
    let diagnostics = linter::lint_source(source, "test.gd", &config);
    // Should not crash and should parse identifiers
    // The naming rules may flag them but should not panic
    for d in &diagnostics {
        assert!(!d.message.is_empty());
    }
}

// Windows line endings
#[test]
fn lexer_handles_crlf_line_endings() {
    let source = "extends Node\r\n\r\nvar x = 1\r\nvar y = 2\r\n";
    let config = default_config();
    let diagnostics = linter::lint_source(source, "test.gd", &config);
    // Should not crash or produce bogus diagnostics from \r characters
    // The \r should be silently skipped
    for d in &diagnostics {
        assert!(
            !d.message.contains('\r'),
            "diagnostic should not contain \\r"
        );
    }
}

#[test]
fn formatter_handles_crlf_input() {
    let source = "var x = 'hello'\r\nvar y = 'world'\r\n";
    let config = default_config();
    let formatted = formatter::format_source(source, &config);
    // Should normalize to LF and convert quotes
    assert!(
        formatted.contains("\"hello\""),
        "quotes should be normalized, got:\n{}",
        formatted
    );
    assert!(
        !formatted.contains('\r'),
        "\\r should be stripped from output, got:\n{:?}",
        formatted
    );
}

// Naming: Vector2D, Area2D, Node3D patterns
#[test]
fn naming_suggests_correct_snake_case_for_2d_3d_names() {
    let source = "func _is_valid_hitbox_area_2D(node):\n\tpass\n";
    let config = default_config();
    let diagnostics = linter::lint_source(source, "test.gd", &config);
    let naming: Vec<_> = diagnostics
        .iter()
        .filter(|d| d.rule == "naming/function-name-snake-case")
        .collect();
    assert!(
        !naming.is_empty(),
        "should flag non-snake-case function name"
    );
    let fix = naming[0].fix.as_ref().unwrap();
    let suggested = &fix.replacements[0].new_text;
    assert_eq!(
        suggested, "_is_valid_hitbox_area_2d",
        "should suggest area_2d not area_2_d, got: {}",
        suggested
    );
}

// Naming: single letter class names accepted as PascalCase
#[test]
fn naming_single_letter_class_name_not_flagged() {
    let source = "class_name A\nextends Node\n";
    let config = default_config();
    let diagnostics = linter::lint_source(source, "test.gd", &config);
    let naming: Vec<_> = diagnostics
        .iter()
        .filter(|d| d.rule == "naming/class-name-pascal-case")
        .collect();
    assert!(
        naming.is_empty(),
        "single-letter class name 'A' should be valid PascalCase, got: {:?}",
        naming.iter().map(|d| &d.message).collect::<Vec<_>>()
    );
}

// Bare comma continuation lines are NOT broken (no enclosing delimiter
// context means we can't safely determine if splitting is valid GDScript).
#[test]
fn fmt_does_not_break_bare_comma_continuation() {
    let source = "func _ready():\n\tvar aff = Affordance.new(\n\t\t\"order_coffee\", \"is\", \"ordering\", \"ordering coffee at the counter\", \"desc\", \"result\", \"interact\"\n\t)\n";
    let config = default_config();
    let formatted = formatter::format_source(source, &config);
    // The continuation line should NOT be split (no enclosing delimiter on this line)
    assert!(
        formatted.contains("\"order_coffee\", \"is\""),
        "bare comma line should not be split, got:\n{}",
        formatted
    );
}

// Regression: % string formatting with multi-line argument arrays must not be corrupted.
#[test]
fn fmt_preserves_percent_format_multiline_args() {
    let source = "func log_it():\n\tLog.info(\"Controller::%s at %s: activity='%s', count=%d\"\n\t\t% [self.name, time.strftime(\"%H:%M\"),\n\t\t\tactivity,\n\t\t\tcount,])\n";
    let config = default_config();
    let formatted = formatter::format_source(source, &config);
    // The % formatting args must remain intact
    assert!(
        formatted.contains("% [self.name"),
        "% format array must survive, got:\n{}",
        formatted
    );
    assert!(
        formatted.contains("activity,"),
        "args must survive, got:\n{}",
        formatted
    );
    assert!(
        formatted.contains("count,]"),
        "trailing arg must survive, got:\n{}",
        formatted
    );
    let second = formatter::format_source(&formatted, &config);
    assert_eq!(formatted, second, "must be idempotent");
}

#[test]
fn fmt_preserves_already_broken_function_call() {
    // Function call already broken across lines with trailing comma must not be re-broken.
    let source = "func check(target, items):\n\tif not Controller._is_in_list(\n\t\tself.current_target,\n\t\titems,\n\t):\n\t\treturn false\n\treturn true\n";
    let config = default_config();
    let formatted = formatter::format_source(source, &config);
    assert!(
        formatted.contains("self.current_target,"),
        "already-broken args preserved, got:\n{}",
        formatted
    );
    assert!(
        formatted.contains("\t):"),
        "closing paren preserved, got:\n{}",
        formatted
    );
    let second = formatter::format_source(&formatted, &config);
    assert_eq!(formatted, second, "must be idempotent");
}

#[test]
fn fmt_does_not_corrupt_long_lines_fixture() {
    // Run formatter on the edge case fixture and verify no data loss.
    let source = std::fs::read_to_string(fixture_path("long_lines_edge_cases.gd")).unwrap();
    let config = default_config();
    let formatted = formatter::format_source(&source, &config);

    // Key content that must survive formatting:
    assert!(
        formatted.contains("curr_time.strftime(\"%H:%M\")"),
        "format arg must survive"
    );
    assert!(
        formatted.contains("start_time.strftime(\"%H:%M\") if start_time"),
        "conditional arg must survive"
    );
    assert!(
        formatted.contains("action_name if action_name else \"null\""),
        "ternary in format args must survive"
    );
    assert!(
        formatted.contains("Controller._is_in_list("),
        "function call must survive"
    );
    assert!(
        formatted.contains("curr_selected_activity"),
        "property chain must survive"
    );
    assert!(
        formatted.contains("using static fallback plan"),
        "long string must survive"
    );

    // Must be idempotent
    let second = formatter::format_source(&formatted, &config);
    assert_eq!(formatted, second, "must be idempotent on edge case fixture");
}

// Long comment wrapping
#[test]
fn fmt_wraps_long_comment() {
    let source = "func _ready():\n\t# TODO: remove this auxiliary dictionary and make all_affordances a dict. Not doing it now because it requires refactoring all callers\n\tpass\n";
    let config = default_config();
    let formatted = formatter::format_source(source, &config);
    // Comment should be wrapped into multiple lines
    let comment_lines: Vec<_> = formatted
        .lines()
        .filter(|l| l.trim().starts_with("# TODO"))
        .collect();
    assert!(
        !comment_lines.is_empty(),
        "wrapped comment should exist, got:\n{}",
        formatted
    );
    // Each line should be <= 100 visual chars
    for cl in &comment_lines {
        let vlen = cl
            .chars()
            .map(|c| if c == '\t' { 4 } else { 1 })
            .sum::<usize>();
        assert!(
            vlen <= 100,
            "wrapped comment line too long ({} chars): {}",
            vlen,
            cl
        );
    }
    let second = formatter::format_source(&formatted, &config);
    assert_eq!(formatted, second, "must be idempotent");
}

// Short comments not wrapped
#[test]
fn fmt_does_not_wrap_short_comment() {
    let source = "# Short comment\n";
    let config = default_config();
    let formatted = formatter::format_source(source, &config);
    assert_eq!(
        formatted.matches("# ").count(),
        1,
        "short comment should not be split"
    );
}

// Long if condition breaking
#[test]
fn fmt_breaks_long_if_condition() {
    let source = "func check(event):\n\tif event is InputEventMouseButton and event.button_index == MOUSE_BUTTON_LEFT and event.is_pressed() and event.double_click:\n\t\tpass\n";
    let config = default_config();
    let formatted = formatter::format_source(source, &config);
    assert!(
        formatted.contains("if (event is InputEventMouseButton"),
        "should wrap in parens, got:\n{}",
        formatted
    );
    assert!(
        formatted.contains("and event.button_index"),
        "should break at and, got:\n{}",
        formatted
    );
    assert!(
        formatted.contains("):"),
        "should end with ):, got:\n{}",
        formatted
    );
    let second = formatter::format_source(&formatted, &config);
    assert_eq!(formatted, second, "must be idempotent");
}

// Short if not broken
#[test]
fn fmt_does_not_break_short_if() {
    let source = "func test():\n\tif a and b:\n\t\tpass\n";
    let config = default_config();
    let formatted = formatter::format_source(source, &config);
    assert!(
        formatted.contains("if a and b:"),
        "short if should not be broken"
    );
}

// ===========================================================================
// Quality rules: integration tests
// ===========================================================================

/// Helper: create a config with specific quality rules enabled (they may be
/// off-by-default) and all other rules still at defaults.
fn quality_config(enable: &[&str]) -> Config {
    let mut config = default_config();
    for rule in enable {
        config
            .rules
            .insert(rule.to_string(), gdstyle::config::RuleSeverityConfig::Warn);
    }
    config
}

#[test]
fn quality_bad_quality_fixture_detects_all_rules() {
    let config = default_config();
    let diagnostics = linter::lint_file(&fixture_path("bad_quality.gd"), &config).unwrap();

    let rules: Vec<&str> = diagnostics.iter().map(|d| d.rule.as_str()).collect();

    assert!(
        rules.contains(&"quality/self-comparison"),
        "should detect self-comparison"
    );
    assert!(
        rules.contains(&"quality/no-self-assign"),
        "should detect no-self-assign"
    );
    assert!(
        rules.contains(&"quality/duplicate-dict-key"),
        "should detect duplicate-dict-key"
    );
    assert!(
        rules.contains(&"quality/duplicated-load"),
        "should detect duplicated-load"
    );
    assert!(
        rules.contains(&"quality/no-else-return"),
        "should detect no-else-return"
    );
    assert!(
        rules.contains(&"quality/unreachable-code"),
        "should detect unreachable-code"
    );
    assert!(
        rules.contains(&"quality/await-in-loop"),
        "should detect await-in-loop"
    );
    assert!(
        rules.contains(&"quality/allocation-in-loop"),
        "should detect allocation-in-loop"
    );
    assert!(
        rules.contains(&"quality/process-get-node"),
        "should detect process-get-node"
    );
    assert!(
        rules.contains(&"quality/unnecessary-pass"),
        "should detect unnecessary-pass"
    );
    assert!(
        rules.contains(&"quality/max-nesting-depth"),
        "should detect max-nesting-depth"
    );
    assert!(
        rules.contains(&"quality/max-returns"),
        "should detect max-returns"
    );
    assert!(
        rules.contains(&"quality/max-branches"),
        "should detect max-branches"
    );
    assert!(
        rules.contains(&"quality/max-local-variables"),
        "should detect max-local-variables"
    );
}

// --- no-debug-print (off by default, must be explicitly enabled) ---

#[test]
fn quality_no_debug_print_off_by_default() {
    let config = default_config();
    let source = "func foo() -> void:\n\tprint(\"hello\")\n";
    let diagnostics = linter::lint_source(source, "test.gd", &config);
    assert!(
        !diagnostics
            .iter()
            .any(|d| d.rule == "quality/no-debug-print"),
        "no-debug-print should be off by default"
    );
}

#[test]
fn quality_no_debug_print_when_enabled() {
    let config = quality_config(&["quality/no-debug-print"]);
    let source =
        "func foo() -> void:\n\tprint(\"hello\")\n\tprints(\"a\", \"b\")\n\tprinterr(\"err\")\n";
    let diagnostics = linter::lint_source(source, "test.gd", &config);
    let hits: Vec<_> = diagnostics
        .iter()
        .filter(|d| d.rule == "quality/no-debug-print")
        .collect();
    assert_eq!(hits.len(), 3, "should detect 3 debug print calls");
}

#[test]
fn quality_no_debug_print_ignores_custom_funcs() {
    let config = quality_config(&["quality/no-debug-print"]);
    let source = "func foo() -> void:\n\tprint_score(100)\n";
    let diagnostics = linter::lint_source(source, "test.gd", &config);
    assert!(
        !diagnostics
            .iter()
            .any(|d| d.rule == "quality/no-debug-print"),
        "should not flag print_score"
    );
}

// --- self-comparison ---

#[test]
fn quality_self_comparison_all_operators() {
    let config = default_config();
    let source = "\
func test() -> void:
\tvar a: int = 1
\tif a == a:
\t\tpass
\tif a != a:
\t\tpass
\tif a > a:
\t\tpass
\tif a >= a:
\t\tpass
\tif a < a:
\t\tpass
\tif a <= a:
\t\tpass
";
    let diagnostics = linter::lint_source(source, "test.gd", &config);
    let hits: Vec<_> = diagnostics
        .iter()
        .filter(|d| d.rule == "quality/self-comparison")
        .collect();
    assert_eq!(
        hits.len(),
        6,
        "should detect all 6 self-comparisons, got {}",
        hits.len()
    );
}

#[test]
fn quality_self_comparison_different_vars_ok() {
    let config = default_config();
    let source = "func test() -> void:\n\tif a == b:\n\t\tpass\n";
    let diagnostics = linter::lint_source(source, "test.gd", &config);
    assert!(
        !diagnostics
            .iter()
            .any(|d| d.rule == "quality/self-comparison"),
        "different variables should not trigger"
    );
}

// --- no-self-assign ---

#[test]
fn quality_no_self_assign_simple() {
    let config = default_config();
    let source = "func test() -> void:\n\tvar x: int = 5\n\tx = x\n";
    let diagnostics = linter::lint_source(source, "test.gd", &config);
    assert!(diagnostics
        .iter()
        .any(|d| d.rule == "quality/no-self-assign"));
}

#[test]
fn quality_no_self_assign_dot_access_ok() {
    let config = default_config();
    let source = "func test() -> void:\n\tx = x.normalized()\n";
    let diagnostics = linter::lint_source(source, "test.gd", &config);
    assert!(
        !diagnostics
            .iter()
            .any(|d| d.rule == "quality/no-self-assign"),
        "x = x.method() should not trigger"
    );
}

#[test]
fn quality_no_self_assign_lhs_property_rhs_local_does_not_trigger() {
    // Regression for a false positive reported in the wild:
    // `moon.size = size * (.08 + 0.035 * _moon_value)` was being flagged
    // because the rule used a 3-token sliding window and only saw matching
    // `size` identifiers on both sides of `=`, ignoring that the LHS is
    // actually the property access `moon.size`.
    let config = default_config();
    let source = "func test() -> void:\n\tmoon.size = size * 0.5\n";
    let diagnostics = linter::lint_source(source, "test.gd", &config);
    assert!(
        !diagnostics
            .iter()
            .any(|d| d.rule == "quality/no-self-assign"),
        "moon.size = size * 0.5 must not trigger (different paths)"
    );
}

#[test]
fn quality_no_self_assign_uses_lhs_in_rhs_expression_does_not_trigger() {
    // `x = x + 1` is not a self-assignment, it reads x and writes a new
    // value. The rule must only flag pure x = x no-ops.
    let config = default_config();
    let source = "func test() -> void:\n\tvar x: int = 5\n\tx = x + 1\n";
    let diagnostics = linter::lint_source(source, "test.gd", &config);
    assert!(
        !diagnostics
            .iter()
            .any(|d| d.rule == "quality/no-self-assign"),
        "x = x + 1 must not trigger (uses x in expression)"
    );
}

#[test]
fn quality_no_self_assign_self_qualified_different_path_does_not_trigger() {
    // self.position and position are different paths even when the trailing
    // segment matches.
    let config = default_config();
    let source = "func test() -> void:\n\tself.position = position\n";
    let diagnostics = linter::lint_source(source, "test.gd", &config);
    assert!(
        !diagnostics
            .iter()
            .any(|d| d.rule == "quality/no-self-assign"),
        "self.position = position must not trigger (different paths)"
    );
}

#[test]
fn quality_no_self_assign_matching_dot_chain_triggers() {
    // `obj.foo = obj.foo` IS a real self-assign and should still be flagged.
    let config = default_config();
    let source = "func test() -> void:\n\tobj.foo = obj.foo\n";
    let diagnostics = linter::lint_source(source, "test.gd", &config);
    assert!(
        diagnostics
            .iter()
            .any(|d| d.rule == "quality/no-self-assign"),
        "obj.foo = obj.foo must still trigger (same path on both sides)"
    );
}

#[test]
fn quality_no_self_assign_deep_chain_triggers() {
    // Multi-level chains should also work both ways.
    let config = default_config();
    let source = "func test() -> void:\n\tself.player.health = self.player.health\n";
    let diagnostics = linter::lint_source(source, "test.gd", &config);
    assert!(
        diagnostics
            .iter()
            .any(|d| d.rule == "quality/no-self-assign"),
        "self.player.health = self.player.health must trigger"
    );
}

#[test]
fn quality_no_self_assign_indexed_does_not_trigger() {
    // arr[i] = arr[i] is an array element assignment; LHS chain isn't a
    // pure identifier dotted path, so we conservatively don't flag it.
    let config = default_config();
    let source = "func test() -> void:\n\tarr[i] = arr[i]\n";
    let diagnostics = linter::lint_source(source, "test.gd", &config);
    assert!(
        !diagnostics
            .iter()
            .any(|d| d.rule == "quality/no-self-assign"),
        "arr[i] = arr[i] must not trigger (subscript, not dotted chain)"
    );
}

#[test]
fn quality_no_self_assign_walrus_does_not_trigger() {
    // `var x := x` would have `Identifier(x) Colon Assign Identifier(x)` —
    // the LHS chain walk from `=` lands on Colon and bails, so we do not
    // flag inferred-type declarations like `var x := some_value`.
    let config = default_config();
    let source = "func test() -> void:\n\tvar x := 5\n";
    let diagnostics = linter::lint_source(source, "test.gd", &config);
    assert!(
        !diagnostics
            .iter()
            .any(|d| d.rule == "quality/no-self-assign"),
        ":= inferred declaration must not trigger"
    );
}

// --- duplicate-dict-key ---

#[test]
fn quality_duplicate_dict_key_detected() {
    let config = default_config();
    let source = "func test() -> Dictionary:\n\treturn {\"a\": 1, \"b\": 2, \"a\": 3}\n";
    let diagnostics = linter::lint_source(source, "test.gd", &config);
    assert!(diagnostics
        .iter()
        .any(|d| d.rule == "quality/duplicate-dict-key"));
}

#[test]
fn quality_duplicate_dict_key_nested_ok() {
    let config = default_config();
    let source = "\
func test() -> Dictionary:
\treturn {\"a\": {\"a\": 1}, \"b\": 2}
";
    let diagnostics = linter::lint_source(source, "test.gd", &config);
    assert!(
        !diagnostics
            .iter()
            .any(|d| d.rule == "quality/duplicate-dict-key"),
        "same key in nested dict should not trigger on outer"
    );
}

// --- duplicated-load ---

#[test]
fn quality_duplicated_load_detected() {
    let config = default_config();
    let source = "\
var a: PackedScene = preload(\"res://scene.tscn\")
var b: PackedScene = preload(\"res://scene.tscn\")
";
    let diagnostics = linter::lint_source(source, "test.gd", &config);
    assert!(diagnostics
        .iter()
        .any(|d| d.rule == "quality/duplicated-load"));
}

#[test]
fn quality_duplicated_load_different_paths_ok() {
    let config = default_config();
    let source = "\
var a: PackedScene = preload(\"res://scene_a.tscn\")
var b: PackedScene = preload(\"res://scene_b.tscn\")
";
    let diagnostics = linter::lint_source(source, "test.gd", &config);
    assert!(
        !diagnostics
            .iter()
            .any(|d| d.rule == "quality/duplicated-load"),
        "different paths should not trigger"
    );
}

// --- no-else-return ---

#[test]
fn quality_no_else_return_simple() {
    let config = default_config();
    let source = "\
func foo(x: int) -> int:
\tif x > 0:
\t\treturn 1
\telse:
\t\treturn -1
";
    let diagnostics = linter::lint_source(source, "test.gd", &config);
    assert!(diagnostics
        .iter()
        .any(|d| d.rule == "quality/no-else-return"));
}

#[test]
fn quality_no_else_return_elif_chain() {
    let config = default_config();
    let source = "\
func foo(x: int) -> String:
\tif x > 100:
\t\treturn \"high\"
\telif x > 50:
\t\treturn \"medium\"
\telse:
\t\treturn \"low\"
";
    let diagnostics = linter::lint_source(source, "test.gd", &config);
    let hits: Vec<_> = diagnostics
        .iter()
        .filter(|d| d.rule == "quality/no-else-return")
        .collect();
    // Both `elif` and `else` should be flagged
    assert!(
        hits.len() >= 2,
        "should detect elif and else after return, got {}",
        hits.len()
    );
}

#[test]
fn quality_no_else_return_no_return_ok() {
    let config = default_config();
    let source = "\
func foo(x: int) -> void:
\tif x > 0:
\t\tvar y: int = x
\t\ty += 1
\telse:
\t\tvar z: int = -x
\t\tz += 1
";
    let diagnostics = linter::lint_source(source, "test.gd", &config);
    assert!(
        !diagnostics
            .iter()
            .any(|d| d.rule == "quality/no-else-return"),
        "no return in if block, else is fine"
    );
}

// --- unreachable-code ---

#[test]
fn quality_unreachable_after_return() {
    let config = default_config();
    let source = "\
func foo() -> int:
\treturn 42
\tvar x: int = 0
";
    let diagnostics = linter::lint_source(source, "test.gd", &config);
    assert!(diagnostics
        .iter()
        .any(|d| d.rule == "quality/unreachable-code"));
}

#[test]
fn quality_unreachable_after_break() {
    let config = default_config();
    let source = "\
func foo() -> void:
\tfor i: int in range(10):
\t\tbreak
\t\tvar x: int = i
";
    let diagnostics = linter::lint_source(source, "test.gd", &config);
    assert!(diagnostics
        .iter()
        .any(|d| d.rule == "quality/unreachable-code"));
}

#[test]
fn quality_unreachable_after_continue() {
    let config = default_config();
    let source = "\
func foo() -> void:
\tfor i: int in range(10):
\t\tcontinue
\t\tvar x: int = i
";
    let diagnostics = linter::lint_source(source, "test.gd", &config);
    assert!(diagnostics
        .iter()
        .any(|d| d.rule == "quality/unreachable-code"));
}

#[test]
fn quality_unreachable_else_not_flagged() {
    let config = default_config();
    let source = "\
func foo(x: int) -> int:
\tif x > 0:
\t\treturn 1
\telse:
\t\treturn -1
";
    let diagnostics = linter::lint_source(source, "test.gd", &config);
    assert!(
        !diagnostics
            .iter()
            .any(|d| d.rule == "quality/unreachable-code"),
        "else after return is not unreachable code"
    );
}

// --- await-in-loop ---

#[test]
fn quality_await_in_for_loop() {
    let config = default_config();
    let source = "\
func foo() -> void:
\tfor i: int in range(10):
\t\tawait get_tree().create_timer(1.0).timeout
";
    let diagnostics = linter::lint_source(source, "test.gd", &config);
    assert!(diagnostics
        .iter()
        .any(|d| d.rule == "quality/await-in-loop"));
}

#[test]
fn quality_await_in_while_loop() {
    let config = default_config();
    let source = "\
func foo() -> void:
\twhile true:
\t\tawait get_tree().process_frame
";
    let diagnostics = linter::lint_source(source, "test.gd", &config);
    assert!(diagnostics
        .iter()
        .any(|d| d.rule == "quality/await-in-loop"));
}

#[test]
fn quality_await_in_nested_loop() {
    let config = default_config();
    let source = "\
func foo() -> void:
\tfor i: int in range(10):
\t\tfor j: int in range(10):
\t\t\tawait get_tree().create_timer(0.1).timeout
";
    let diagnostics = linter::lint_source(source, "test.gd", &config);
    assert!(diagnostics
        .iter()
        .any(|d| d.rule == "quality/await-in-loop"));
}

#[test]
fn quality_await_outside_loop_ok() {
    let config = default_config();
    let source = "\
func foo() -> void:
\tawait get_tree().create_timer(1.0).timeout
";
    let diagnostics = linter::lint_source(source, "test.gd", &config);
    assert!(
        !diagnostics
            .iter()
            .any(|d| d.rule == "quality/await-in-loop"),
        "await outside loop should not trigger"
    );
}

// --- allocation-in-loop ---

#[test]
fn quality_allocation_in_loop_detected() {
    let config = default_config();
    let source = "\
func foo() -> void:
\tfor i: int in range(10):
\t\tvar n: Node = Node.new()
\t\tadd_child(n)
";
    let diagnostics = linter::lint_source(source, "test.gd", &config);
    assert!(diagnostics
        .iter()
        .any(|d| d.rule == "quality/allocation-in-loop"));
}

#[test]
fn quality_allocation_outside_loop_ok() {
    let config = default_config();
    let source = "\
func foo() -> void:
\tvar n: Node = Node.new()
\tadd_child(n)
";
    let diagnostics = linter::lint_source(source, "test.gd", &config);
    assert!(
        !diagnostics
            .iter()
            .any(|d| d.rule == "quality/allocation-in-loop"),
        "allocation outside loop should not trigger"
    );
}

// --- process-get-node ---

#[test]
fn quality_process_get_node_dollar() {
    let config = default_config();
    let source = "\
func _process(delta: float) -> void:
\tvar label: Label = $HUD/Label
\tlabel.text = str(delta)
";
    let diagnostics = linter::lint_source(source, "test.gd", &config);
    assert!(diagnostics
        .iter()
        .any(|d| d.rule == "quality/process-get-node"));
}

#[test]
fn quality_process_get_node_call() {
    let config = default_config();
    let source = "\
func _physics_process(delta: float) -> void:
\tvar body: Node = get_node(\"Body\")
\tbody.position.x += delta
";
    let diagnostics = linter::lint_source(source, "test.gd", &config);
    assert!(diagnostics
        .iter()
        .any(|d| d.rule == "quality/process-get-node"));
}

#[test]
fn quality_process_get_node_ready_ok() {
    let config = default_config();
    let source = "\
func _ready() -> void:
\tvar label: Label = $HUD/Label
\tlabel.text = \"ready\"
";
    let diagnostics = linter::lint_source(source, "test.gd", &config);
    assert!(
        !diagnostics
            .iter()
            .any(|d| d.rule == "quality/process-get-node"),
        "get_node in _ready should not trigger"
    );
}

#[test]
fn quality_process_get_node_modulo_not_flagged() {
    // Regression: `a % b` modulo inside _process was misread as a
    // `%UniqueNode` reference because the check looked at the char before
    // `%`. A unique-node ref is `%` followed by an identifier/quote.
    let config = default_config();
    let source = "\
func _process(delta: float) -> void:
\tvar phase: int = frame % 60
\tposition.x = phase * delta
";
    let diagnostics = linter::lint_source(source, "test.gd", &config);
    assert!(
        !diagnostics
            .iter()
            .any(|d| d.rule == "quality/process-get-node"),
        "modulo in _process must not be flagged as a node lookup"
    );
}

#[test]
fn quality_process_get_node_unique_node_flagged() {
    // A genuine `%UniqueName` lookup in _process should still be flagged.
    let config = default_config();
    let source = "\
func _process(_delta: float) -> void:
\tvar bar = %HealthBar
\tbar.value += 1
";
    let diagnostics = linter::lint_source(source, "test.gd", &config);
    assert!(
        diagnostics
            .iter()
            .any(|d| d.rule == "quality/process-get-node"),
        "%UniqueName lookup in _process should be flagged"
    );
}

// --- unnecessary-pass ---

#[test]
fn quality_unnecessary_pass_with_other_code() {
    let config = default_config();
    let source = "\
func foo() -> void:
\tvar x: int = 5
\tx += 1
\tpass
";
    let diagnostics = linter::lint_source(source, "test.gd", &config);
    assert!(diagnostics
        .iter()
        .any(|d| d.rule == "quality/unnecessary-pass"));
}

#[test]
fn quality_unnecessary_pass_alone_ok() {
    let config = default_config();
    let source = "func foo() -> void:\n\tpass\n";
    let diagnostics = linter::lint_source(source, "test.gd", &config);
    assert!(
        !diagnostics
            .iter()
            .any(|d| d.rule == "quality/unnecessary-pass"),
        "lone pass should not trigger"
    );
}

#[test]
fn quality_unnecessary_pass_in_match_arm_ok() {
    // A `pass` that is the sole body of a match arm is required: a match
    // arm cannot be empty. It must not be flagged even though the function
    // has other statements.
    let config = default_config();
    let source = "\
func handle(state: int) -> void:
\tmatch state:
\t\t0:
\t\t\tpass
\t\t1:
\t\t\tdo_something()
\t\t_:
\t\t\tpass
";
    let diagnostics = linter::lint_source(source, "test.gd", &config);
    assert!(
        !diagnostics
            .iter()
            .any(|d| d.rule == "quality/unnecessary-pass"),
        "pass as a lone match-arm body must not be flagged; got: {:?}",
        diagnostics
            .iter()
            .filter(|d| d.rule == "quality/unnecessary-pass")
            .map(|d| d.span.line)
            .collect::<Vec<_>>()
    );
}

#[test]
fn quality_unnecessary_pass_in_empty_if_body_ok() {
    // `pass` as the only statement of an if/else branch is required.
    let config = default_config();
    let source = "\
func foo(flag: bool) -> void:
\tif flag:
\t\tpass
\telse:
\t\tprint(\"no\")
";
    let diagnostics = linter::lint_source(source, "test.gd", &config);
    assert!(
        !diagnostics
            .iter()
            .any(|d| d.rule == "quality/unnecessary-pass"),
        "pass as a lone if-branch body must not be flagged"
    );
}

// --- type-hint (off by default) ---

#[test]
fn quality_type_hint_off_by_default() {
    let config = default_config();
    let source = "var speed = 10.0\n";
    let diagnostics = linter::lint_source(source, "test.gd", &config);
    assert!(
        !diagnostics.iter().any(|d| d.rule == "quality/type-hint"),
        "type-hint should be off by default"
    );
}

#[test]
fn quality_type_hint_when_enabled() {
    let config = quality_config(&["quality/type-hint"]);
    let source = "\
var speed = 10.0
var health: int = 100
func foo(x, y: int):
\tpass
";
    let diagnostics = linter::lint_source(source, "test.gd", &config);
    let hits: Vec<_> = diagnostics
        .iter()
        .filter(|d| d.rule == "quality/type-hint")
        .collect();
    // speed (no type), foo return type, param x (no type) = 3
    assert!(
        hits.len() >= 3,
        "should detect at least 3 missing type hints, got {}",
        hits.len()
    );
}

// --- empty-function (off by default) ---

#[test]
fn quality_empty_function_off_by_default() {
    let config = default_config();
    let source = "func foo() -> void:\n\tpass\n";
    let diagnostics = linter::lint_source(source, "test.gd", &config);
    assert!(
        !diagnostics
            .iter()
            .any(|d| d.rule == "quality/empty-function"),
        "empty-function should be off by default"
    );
}

#[test]
fn quality_empty_function_when_enabled() {
    let config = quality_config(&["quality/empty-function"]);
    let source = "func foo() -> void:\n\tpass\n";
    let diagnostics = linter::lint_source(source, "test.gd", &config);
    assert!(diagnostics
        .iter()
        .any(|d| d.rule == "quality/empty-function"));
}

// --- max-class-variables ---

#[test]
fn quality_max_class_variables_exceeded() {
    let config = default_config();
    let mut source = String::from("class_name BigClass\nextends Node\n\n");
    for i in 0..20 {
        source.push_str(&format!("var v_{}: int = {}\n", i, i));
    }
    source.push_str("\nfunc _ready() -> void:\n\tpass\n");
    let diagnostics = linter::lint_source(&source, "test.gd", &config);
    assert!(diagnostics
        .iter()
        .any(|d| d.rule == "quality/max-class-variables"));
}

// --- max-public-methods ---

#[test]
fn quality_max_public_methods_exceeded() {
    let config = default_config();
    let mut source = String::from("class_name BigApi\nextends Node\n\n");
    for i in 0..25 {
        source.push_str(&format!("func method_{}() -> void:\n\tpass\n\n", i));
    }
    let diagnostics = linter::lint_source(&source, "test.gd", &config);
    assert!(diagnostics
        .iter()
        .any(|d| d.rule == "quality/max-public-methods"));
}

// --- max-inner-classes ---

#[test]
fn quality_max_inner_classes_exceeded() {
    let config = default_config();
    let mut source = String::from("class_name Outer\nextends Node\n\n");
    for i in 0..8 {
        source.push_str(&format!("class Inner{} extends RefCounted:\n\tpass\n\n", i));
    }
    let diagnostics = linter::lint_source(&source, "test.gd", &config);
    assert!(diagnostics
        .iter()
        .any(|d| d.rule == "quality/max-inner-classes"));
}

// --- max-nesting-depth ---

#[test]
fn quality_max_nesting_depth_within_limit_ok() {
    let config = default_config();
    let source = "\
func foo() -> void:
\tif true:
\t\tif true:
\t\t\tpass
";
    let diagnostics = linter::lint_source(source, "test.gd", &config);
    assert!(
        !diagnostics
            .iter()
            .any(|d| d.rule == "quality/max-nesting-depth"),
        "depth 2 should be within default limit of 4"
    );
}

// --- max-returns ---

#[test]
fn quality_max_returns_within_limit_ok() {
    let config = default_config();
    let source = "\
func foo(x: int) -> int:
\tif x > 0:
\t\treturn 1
\treturn 0
";
    let diagnostics = linter::lint_source(source, "test.gd", &config);
    assert!(
        !diagnostics.iter().any(|d| d.rule == "quality/max-returns"),
        "2 returns should be within default limit of 6"
    );
}

// --- max-branches ---

#[test]
fn quality_max_branches_within_limit_ok() {
    let config = default_config();
    let source = "\
func foo(x: int) -> void:
\tif x == 1:
\t\tpass
\tif x == 2:
\t\tpass
";
    let diagnostics = linter::lint_source(source, "test.gd", &config);
    assert!(
        !diagnostics.iter().any(|d| d.rule == "quality/max-branches"),
        "2 branches should be within default limit of 8"
    );
}

// --- max-local-variables ---

#[test]
fn quality_max_local_variables_within_limit_ok() {
    let config = default_config();
    let source = "\
func foo() -> void:
\tvar a: int = 1
\tvar b: int = 2
\tvar c: int = 3
";
    let diagnostics = linter::lint_source(source, "test.gd", &config);
    assert!(
        !diagnostics
            .iter()
            .any(|d| d.rule == "quality/max-local-variables"),
        "3 locals should be within default limit of 10"
    );
}

// --- Clean script still clean with new rules ---

#[test]
fn quality_clean_script_still_clean() {
    let config = default_config();
    let diagnostics = linter::lint_file(&fixture_path("clean_script.gd"), &config).unwrap();
    assert!(
        diagnostics.is_empty(),
        "clean_script.gd should still produce no diagnostics, got:\n{}",
        diagnostics
            .iter()
            .map(|d| format!("  line {}: [{}] {}", d.span.line, d.rule, d.message))
            .collect::<Vec<_>>()
            .join("\n")
    );
}

// =============================================================================
// Regression tests for the 9 bugs surfaced by the ai-battleground / multi-
// project survey (see docs/dev-notes/lessons-ai-battleground-autofix.md).
// One test per bug, asserting the specific failure mode is gone.
// =============================================================================

#[test]
fn regression_bug1_comment_spacing_skips_doc_comments() {
    // Bug 1: rule was rewriting `##var p2p_session` → `## var p2p_session`,
    // which (a) changes how a reader interprets a "double-commented-out"
    // line, and (b) shifts the doc-comment marker position. The fix is to
    // leave any DocComment token (anything beginning with `##`) untouched.
    let source = "##var p2p_session: P2PSession = null\n";
    let config = default_config();
    let diagnostics = linter::lint_source(source, "test.gd", &config);
    let comment_spacing: Vec<_> = diagnostics
        .iter()
        .filter(|d| d.rule == "format/comment-spacing")
        .collect();
    assert!(
        comment_spacing.is_empty(),
        "comment-spacing must not fire on `##` doc comments; got: {:?}",
        comment_spacing
            .iter()
            .map(|d| &d.message)
            .collect::<Vec<_>>()
    );

    // It should still fire on a plain `#` comment whose first char is not
    // alphabetic. The pre-existing rule deliberately skips `#abc` style
    // (treats it as conventional one-char shorthand), but does flag
    // `#0`, `#$`, `#-` etc.
    let plain = "var x = 1\n#0 LevelFilter::Off\n";
    let plain_diags = linter::lint_source(plain, "test.gd", &config);
    assert!(
        plain_diags
            .iter()
            .any(|d| d.rule == "format/comment-spacing"),
        "comment-spacing should still fire on `#0` (single-hash, non-text follow-up)"
    );
}

#[test]
fn regression_bug2_operator_spacing_unary_after_else() {
    // Bug 2: `X if cond else -1` was getting an unwanted space inserted into
    // `else - 1`, because the unary skip-list didn't include `else`. The fix
    // now uses an `is_operand_end` predicate that returns false for any
    // keyword/operator/opening-bracket, making `-`/`+` unary in those
    // positions.
    let cases = [
        "var x = 1 if y else -1\n",
        "return v if v > 0 else -1.0\n",
        "var d = 1.0 if is_local else -1.0\n",
        "var a = (b if c else -2)\n",
    ];
    let config = default_config();
    for source in &cases {
        let diagnostics = linter::lint_source(source, "test.gd", &config);
        let bad: Vec<_> = diagnostics
            .iter()
            .filter(|d| d.rule == "format/operator-spacing")
            .collect();
        assert!(
            bad.is_empty(),
            "unary `-` after `else` should not trigger operator-spacing in `{}`; got: {:?}",
            source.trim(),
            bad.iter().map(|d| &d.message).collect::<Vec<_>>()
        );
    }
}

#[test]
fn regression_bug3_constant_accepts_private_pascalcase_preload() {
    // Bug 3: the rule documented "(or PascalCase for preloads)" but its
    // PascalCase check rejected names with a leading underscore, so
    // `_ButtonNormalTex` was being rewritten to `_BUTTON_NORMAL_TEX`. The
    // fix strips leading underscores before the PascalCase check.
    let source = "\
const _ButtonNormalTex := preload(\"res://x.png\")
const _ButtonPressedTex := preload(\"res://y.png\")
";
    let config = default_config();
    let diagnostics = linter::lint_source(source, "test.gd", &config);
    let bad: Vec<_> = diagnostics
        .iter()
        .filter(|d| d.rule == "naming/constant-name-screaming-case")
        .collect();
    assert!(
        bad.is_empty(),
        "private-PascalCase preloads should be accepted as conforming; got: {:?}",
        bad.iter().map(|d| &d.message).collect::<Vec<_>>()
    );

    // But snake_case privates are still flagged (they're neither SCREAMING
    // nor PascalCase).
    let snake = "const _emoji_font := preload(\"res://x.ttf\")\n";
    let snake_diags = linter::lint_source(snake, "test.gd", &config);
    assert!(
        snake_diags
            .iter()
            .any(|d| d.rule == "naming/constant-name-screaming-case"),
        "snake_case private const should still fire the rule"
    );
}

#[test]
fn regression_bug4_signal_past_tense_skips_non_verbs() {
    // Bug 4: the rule used to append "ed"/"d" to the last word regardless of
    // whether it was actually a verb, producing nonsense like `launch_gamed`
    // and `return_to_lobbied`. The fix gates inflection on a curated regular-
    // verb dictionary plus the existing irregular dictionary.
    let nonsense_inputs = [
        "signal launch_game\n",
        "signal return_to_lobby\n",
        "signal before_ui_action\n",
        "signal reset_app\n",
        "signal stream_chunk\n",
    ];
    let config = default_config();
    for source in &nonsense_inputs {
        let diagnostics = linter::lint_source(source, "test.gd", &config);
        let fixed = fixer::apply_fixes(source, &diagnostics, false);
        // The original signal name should still be present: no nonsense
        // rename applied.
        assert_eq!(
            fixed.trim(),
            source.trim(),
            "non-verb signal `{}` should not be auto-inflected",
            source.trim()
        );
    }

    // Real verbs (regular and irregular) should still be inflected.
    let verb_cases = [
        // The inflector handles regular `+ed` plus the `e → d` and
        // `consonant+y → ied` rules; consonant-doubling is a separate
        // (currently unimplemented) refinement, so we exercise verbs that
        // do not need doubling.
        ("signal player_jump\n", "player_jumped"),
        ("signal request_complete\n", "request_completed"),
        ("signal connection_lose\n", "connection_lost"), // irregular
        ("signal config_apply\n", "config_applied"),     // y → ied
    ];
    for (source, expected) in &verb_cases {
        let diagnostics = linter::lint_source(source, "test.gd", &config);
        let fixed = fixer::apply_fixes(source, &diagnostics, false);
        assert!(
            fixed.contains(expected),
            "verb signal `{}` should inflect to contain `{}`; got `{}`",
            source.trim(),
            expected,
            fixed.trim()
        );
    }
}

#[test]
fn regression_bug5_static_function_rename_is_not_collateral_to_enum_member() {
    // Bug 5: renaming `static func NONE() -> ChaosProfile` on class
    // `ChaosProfile` used to also rewrite the unrelated `DeviceProfile.NONE`
    // enum member in other files, because the rewriter matched bare
    // identifier tokens. The context-aware rewriter requires
    // `<source_class_name>.<old>(` for static function renames; the
    // `DeviceProfile.NONE` access (no parens, different qualifier) is left
    // alone.
    let source_file = "\
class_name ChaosProfile

static func NONE() -> ChaosProfile:
\treturn null
";
    let target_file = "\
extends Node

enum DeviceProfile { NONE, IPHONE_15_PRO }

var current: int = DeviceProfile.NONE

func _ready():
\tChaosProfile.NONE()
";
    let config = default_config();
    let source_diags = linter::lint_source(source_file, "chaos_profile.gd", &config);
    let source_members = parse_members_for_test(source_file);
    let renames = fixer::extract_renames(
        source_file,
        &source_diags,
        "chaos_profile.gd",
        &source_members,
    );
    let none_rename = renames
        .iter()
        .find(|r| r.old_name == "NONE")
        .expect("expected NONE rename");
    assert_eq!(none_rename.new_name, "none");
    assert!(matches!(none_rename.kind, fixer::RenameKind::Function));
    assert_eq!(
        none_rename.source_class_name.as_deref(),
        Some("ChaosProfile")
    );
    assert!(
        !none_rename.is_instance_member,
        "static function should NOT be marked instance"
    );

    let refs = fixer::find_cross_file_references(target_file, "device_sim.gd", &renames);
    let texts: Vec<_> = refs
        .iter()
        .map(|r| {
            let s = &target_file[r.offset..r.offset + r.length];
            (r.old_name.clone(), s.to_string(), r.offset)
        })
        .collect();

    // The `ChaosProfile.NONE()` call site SHOULD be matched.
    assert!(
        texts.iter().any(|(_, _, off)| {
            let lhs_window = &target_file[off.saturating_sub(13)..*off];
            lhs_window.ends_with("ChaosProfile.")
        }),
        "ChaosProfile.NONE() call should be picked up; got: {:?}",
        texts
    );

    // The `DeviceProfile.NONE` enum member access must NOT be matched.
    assert!(
        !texts.iter().any(|(_, _, off)| {
            let lhs_window = &target_file[off.saturating_sub(14)..*off];
            lhs_window.ends_with("DeviceProfile.")
        }),
        "DeviceProfile.NONE (different class qualifier) must not be rewritten; got: {:?}",
        texts
    );
}

#[test]
fn regression_bug6_formatter_preserves_declarations() {
    // Bug 6: the formatter's reorder pass could produce output that lost or
    // mutated declarations in subtle ways, breaking the file at runtime. The
    // safety guards now (a) skip reorder when a module-level initializer
    // depends on another module-level identifier (because GDScript runs
    // initialisers in source order), and (b) re-parse the output and verify
    // the set of declared symbols is unchanged.
    //
    // Cross-referencing initialisers must NOT be reordered.
    let cross_ref = "\
extends Node

const A := 1
const B := A + 2
const C := B * 3

func _ready():
\tprint(C)
";
    let formatted = formatter::format_source(cross_ref, &default_config());
    // Find positions of A, B, C decls; they must appear in source order.
    let pos_a = formatted.find("const A").expect("A missing");
    let pos_b = formatted.find("const B").expect("B missing");
    let pos_c = formatted.find("const C").expect("C missing");
    assert!(
        pos_a < pos_b && pos_b < pos_c,
        "cross-referencing const declarations must keep source order; got A={} B={} C={}\n{}",
        pos_a,
        pos_b,
        pos_c,
        formatted
    );

    // Sanity check: a clean file with independent decls + multiple kinds
    // round-trips without dropping members. This protects against the
    // formatter silently losing an inner class etc.
    let multi = "\
class_name Demo
extends Node

class Inner1:
\tvar x: int = 0

class Inner2:
\tvar y: int = 0

signal a_signal
const C := 1
var v: int = 0

func _ready():
\tpass

static func helper():
\tpass
";
    let formatted_multi = formatter::format_source(multi, &default_config());
    for needle in [
        "class Inner1",
        "class Inner2",
        "signal a_signal",
        "const C",
        "var v",
        "func _ready",
        "static func helper",
    ] {
        assert!(
            formatted_multi.contains(needle),
            "formatter dropped `{}` from output:\n{}",
            needle,
            formatted_multi
        );
    }
}

#[test]
fn regression_bug7_no_unnecessary_parens_preserves_space() {
    // Bug 7: `if(is_mouse):` was rewritten to `ifis_mouse:`. The rule
    // deleted both parens but didn't replace the opening one with a space
    // when the keyword and `(` were touching. Fix: detect the touching case
    // and emit a single space in place of the open paren.
    let source = "\
func foo():
\tif(is_mouse):
\t\tpass
";
    let config = default_config();
    let diagnostics = linter::lint_source(source, "test.gd", &config);
    let fixed = fixer::apply_fixes(source, &diagnostics, false);
    assert!(
        fixed.contains("if is_mouse:"),
        "`if(is_mouse):` must become `if is_mouse:`, got:\n{}",
        fixed
    );
    assert!(
        !fixed.contains("ifis_mouse"),
        "must not produce `ifis_mouse` (no separator), got:\n{}",
        fixed
    );

    // When the original already had a space (`if (is_mouse):`), the fixed
    // output should not gain an extra space (`if  is_mouse:`).
    let with_space = "\
func bar():
\tif (is_mouse):
\t\tpass
";
    let with_space_fixed = fixer::apply_fixes(
        with_space,
        &linter::lint_source(with_space, "test.gd", &config),
        false,
    );
    assert!(
        with_space_fixed.contains("if is_mouse:") && !with_space_fixed.contains("if  is_mouse"),
        "`if (is_mouse):` must produce single-space `if is_mouse:`, got:\n{}",
        with_space_fixed
    );
}

#[test]
fn regression_bug8_instance_member_rename_rewrites_dot_access() {
    // Bug 8: a `var current_A: int` on `class_name ModelState` is accessed
    // cross-file as `model.current_A` (instance), not `ModelState.current_A`
    // (static). The previous rewriter required the qualifier to equal the
    // source class name and so left every instance access untouched. The
    // fix marks the rename as `is_instance_member: true` and accepts any
    // `.<name>` member access (including `arr[0].name`, `func().name`, etc.).
    let source_file = "\
class_name ModelState

var current_A: int = 1
var current_T: int = 3
";
    let target_file = "\
func calc(model: ModelState, models: Array) -> void:
\tvar a := model.current_A
\tvar t := models[0].current_T
\tvar wrap := models.front().current_A
";
    let config = default_config();
    let source_diags = linter::lint_source(source_file, "model_state.gd", &config);
    let source_members = parse_members_for_test(source_file);
    let renames = fixer::extract_renames(
        source_file,
        &source_diags,
        "model_state.gd",
        &source_members,
    );

    let cur_a = renames
        .iter()
        .find(|r| r.old_name == "current_A")
        .expect("current_A rename");
    assert!(
        cur_a.is_instance_member,
        "non-static `var current_A` must be marked instance member"
    );

    let refs = fixer::find_cross_file_references(target_file, "consumer.gd", &renames);
    // Should pick up:
    //   model.current_A
    //   models[0].current_T
    //   models.front().current_A
    let names: Vec<&str> = refs.iter().map(|r| r.old_name.as_str()).collect();
    let count_a = names.iter().filter(|n| **n == "current_A").count();
    let count_t = names.iter().filter(|n| **n == "current_T").count();
    assert_eq!(
        count_a, 2,
        "should match both `model.current_A` and `models.front().current_A`; got refs: {:?}",
        names
    );
    assert_eq!(
        count_t, 1,
        "should match `models[0].current_T`; got refs: {:?}",
        names
    );

    // Verify the fix actually rewrites the source text.
    let fixed = fixer::apply_cross_file_fixes(target_file, &refs);
    assert!(
        fixed.contains("model.current_a")
            && fixed.contains("models[0].current_t")
            && fixed.contains("models.front().current_a"),
        "all instance accesses must be renamed; got:\n{}",
        fixed
    );
}

#[test]
fn regression_bug9_replacement_ordering_insertion_after_replacement() {
    // Bug 9: when `format/operator-spacing` (insert space, length 0) and
    // `format/double-quotes` (replace `' in '` length 6 with `" in "`)
    // landed at the same byte offset, applying the insertion first shifted
    // the replacement span and left a stray closing `'`, producing the
    // syntactically invalid `+" in "'`. Fix: secondary sort by length
    // descending so span replacements run before zero-length insertions at
    // the same offset.
    let source = "\
class_name X
func foo() -> String:
\treturn \"a\" +' in ' + \"b\"
";
    let config = default_config();
    let diagnostics = linter::lint_source(source, "test.gd", &config);
    let fixed = fixer::apply_fixes(source, &diagnostics, false);
    assert!(
        fixed.contains("\"a\" + \" in \" + \"b\""),
        "expected `\"a\" + \" in \" + \"b\"` after combined fix; got:\n{}",
        fixed
    );
    assert!(
        !fixed.contains("\" in \"'"),
        "must not leave an orphan single quote after the converted string; got:\n{}",
        fixed
    );

    // GARP_old's actual case: a second, mixed-quote string elsewhere on the
    // line that should be left alone (single-quoted, contains `"`).
    let mixed = "\
class_name Y
func bar(p: String) -> String:
\treturn \"x\" +' in ' + p + ' has \"q\" inside'
";
    let mixed_diags = linter::lint_source(mixed, "test.gd", &config);
    let mixed_fixed = fixer::apply_fixes(mixed, &mixed_diags, false);
    assert!(
        mixed_fixed.contains("\"x\" + \" in \" + p + ' has \"q\" inside'"),
        "second string (with embedded `\"`) must stay single-quoted intact; got:\n{}",
        mixed_fixed
    );
}

#[test]
fn regression_bug12_formatter_does_not_drop_inner_class_body() {
    // Bug 12: when an inner class with a body and methods got reordered, its
    // body was being lifted out as top-level vars/funcs while the class
    // header remained empty. The safety guard now compares scope-qualified
    // member signatures: moving a member out of its inner class scope
    // changes the signature and the formatter rejects the change.
    let source = "\
class_name ChatTree
## doc

class Position:
\tvar key := \"\"
\tvar index := 0
\tfunc _to_string() -> String:
\t\treturn key

const FOO := \"x\"
";
    let formatted = formatter::format_source(source, &default_config());
    // Members of class Position must remain inside its body.
    let pos_idx = formatted
        .find("class Position")
        .expect("class Position lost");
    let key_idx = formatted.find("var key").expect("var key lost");
    let to_str_idx = formatted.find("func _to_string").expect("_to_string lost");
    assert!(
        pos_idx < key_idx && pos_idx < to_str_idx,
        "var key / _to_string must stay AFTER class Position, not lifted to top level. Got:\n{}",
        formatted
    );

    // The inner class body must still be indented inside the class block.
    let after_pos = &formatted[pos_idx..];
    assert!(
        after_pos.contains("\tvar key"),
        "var key must remain tab-indented inside class Position; got:\n{}",
        formatted
    );
}

#[test]
fn regression_bug10_trailing_comma_skips_subscripts() {
    // Bug 10: format/trailing-comma was treating every multi-line `[...]` as
    // an array literal and inserting a trailing comma. But GDScript does not
    // allow trailing commas inside subscripts (`arr[i,]` is invalid). When
    // godot-open-rts had a multi-line `CONSTANTS[\n\tkey,\n]` it broke parsing.
    let source = "\
extends Node

const TABLE := { \"a\": 1, \"b\": 2 }

func has(v) -> bool:
\treturn TABLE[
\t\tv
\t] != null
";
    let config = default_config();
    let diagnostics = linter::lint_source(source, "test.gd", &config);
    let bad: Vec<_> = diagnostics
        .iter()
        .filter(|d| d.rule == "format/trailing-comma")
        .collect();
    assert!(
        bad.is_empty(),
        "trailing-comma must NOT fire on a multi-line subscript `TABLE[v,]`; got: {:?}",
        bad.iter()
            .map(|d| (d.span.line, &d.message))
            .collect::<Vec<_>>()
    );

    // Sanity check: a real multi-line array literal still gets the comma.
    let array_source = "\
extends Node

var xs := [
\t1,
\t2
]
";
    let array_diags = linter::lint_source(array_source, "test.gd", &config);
    assert!(
        array_diags
            .iter()
            .any(|d| d.rule == "format/trailing-comma"),
        "trailing-comma should still fire on multi-line array literals"
    );
}

#[test]
fn regression_bug11_class_rename_does_not_touch_unrelated_enum_members() {
    // Bug 11: jdungeon had `class_name NPC` (which fails is_pascal_case
    // because it's all-uppercase, no lowercase letters). The rule renamed
    // it to `Npc`. The cross-file rewriter then matched every standalone
    // `NPC` token in the project (including unrelated `enum EntityType {
    // PLAYER, ENEMY, ITEM, NPC }`), turning the enum member into `Npc`
    // and silently re-introducing a different naming violation. The fix
    // restricts Class rewriting to actual type / call / static-access
    // positions.
    // Use a non-acronym class so the rename actually fires (acronyms like
    // `NPC` are now preserved by `to_pascal_case`, see bug 1.14 / pascal_word).
    let source_file = "class_name myShape\n";
    let target_file = "\
extends Node

enum EntityType { PLAYER, ENEMY, ITEM, myShape }

var current = EntityType.myShape

# A genuine class reference that SHOULD be rewritten:
var avatar: myShape = myShape.new()
";
    let config = default_config();
    let source_diags = linter::lint_source(source_file, "shape.gd", &config);
    let source_members = parse_members_for_test(source_file);
    let renames = fixer::extract_renames(source_file, &source_diags, "shape.gd", &source_members);
    let class_rename = renames
        .iter()
        .find(|r| r.old_name == "myShape")
        .expect("expected myShape class rename");
    assert!(matches!(class_rename.kind, fixer::RenameKind::Class));
    assert_eq!(class_rename.new_name, "MyShape");

    let refs = fixer::find_cross_file_references(target_file, "consumer.gd", &renames);

    // The enum member `myShape` (bare in `enum { ..., myShape }`) and the
    // access `EntityType.myShape` must NOT be rewritten.
    for r in &refs {
        let preceding = &target_file[r.offset.saturating_sub(20)..r.offset];
        assert!(
            !preceding.ends_with("EntityType."),
            "Class rename must not touch `EntityType.myShape`; ref preceded by `{}`",
            preceding
        );
        let line_start = target_file[..r.offset]
            .rfind('\n')
            .map(|p| p + 1)
            .unwrap_or(0);
        let line = &target_file[line_start
            ..target_file[r.offset..]
                .find('\n')
                .map(|n| r.offset + n)
                .unwrap_or(target_file.len())];
        assert!(
            !line.starts_with("enum "),
            "Class rename must not touch enum-member declaration; line: {}",
            line
        );
    }

    // The genuine type reference `var avatar: myShape` and the constructor
    // `myShape.new()` SHOULD be rewritten.
    let texts: Vec<&str> = refs
        .iter()
        .map(|r| &target_file[r.offset..r.offset + r.length])
        .collect();
    assert!(
        texts.contains(&"myShape"),
        "type-position `: myShape` reference should be picked up; got refs at offsets {:?}",
        refs.iter().map(|r| r.offset).collect::<Vec<_>>()
    );
}

#[test]
fn pascal_word_preserves_acronyms() {
    // Bug 1.14: `to_pascal_case("HTTPRequest")` used to return
    // `"Httprequest"`, mangling the acronym. Single-word acronyms now
    // round-trip; mixed acronym+CamelCase preserves the leading uppercase
    // run.
    use gdstyle::rules::naming;
    assert_eq!(naming::to_pascal_case("HTTPRequest"), "HTTPRequest");
    assert_eq!(naming::to_pascal_case("XMLParser"), "XMLParser");
    assert_eq!(naming::to_pascal_case("NPC"), "NPC");
    assert_eq!(naming::to_pascal_case("URL"), "URL");
    // Plain camelCase / snake_case / lowercase still works as before.
    assert_eq!(naming::to_pascal_case("myShape"), "MyShape");
    assert_eq!(naming::to_pascal_case("snake_case_name"), "SnakeCaseName");
    assert_eq!(naming::to_pascal_case("foo"), "Foo");
}

#[test]
fn regression_bug13_typed_collections_not_wrapped_or_comma_inserted() {
    // Bug 13: bread-adventure had a long line with a typed dictionary
    // `var x: Dictionary[int, ExploreStatus] = {} as Dictionary[...]`.
    // The formatter wrapped it across lines, added a trailing comma, and
    // GDScript's parser rejected the multi-line typed-collection bracket
    // entirely. The fix is to leave subscripts and typed-collection
    // brackets as a single line (and never insert a trailing comma in
    // them), letting the format/max-line-length warning stand.
    let mut config = default_config();
    config.max_line_length = 60;

    let long_source = "\
extends Node

var explored: Dictionary[int, Constant.ExploreStatus] = {} as Dictionary[int, Constant.ExploreStatus]
";
    let formatted = formatter::format_source(long_source, &config);
    // The typed-dict bracket `Dictionary[int, X]` must remain single-line.
    assert!(
        formatted.contains("Dictionary[int, Constant.ExploreStatus]"),
        "typed dictionary must not be wrapped across lines; got:\n{}",
        formatted
    );
    assert!(
        !formatted.contains("Dictionary[\n"),
        "typed dictionary must not be split with a leading `[`; got:\n{}",
        formatted
    );

    // And no trailing comma should appear inside the type spec, even if
    // someone manually wrote it across lines.
    let manual = "\
extends Node

var explored: Dictionary[
\tint,
\tConstant.ExploreStatus
] = {}
";
    let manual_diags = linter::lint_source(manual, "test.gd", &config);
    let bad: Vec<_> = manual_diags
        .iter()
        .filter(|d| d.rule == "format/trailing-comma")
        .collect();
    assert!(
        bad.is_empty(),
        "trailing-comma must not fire inside Dictionary[...] type spec; got: {:?}",
        bad.iter()
            .map(|d| (d.span.line, &d.message))
            .collect::<Vec<_>>()
    );
}

#[test]
fn regression_bug14_function_callable_reference_rewritten() {
    // Bug 14: Pixelorama declared `_on_Autosave_timeout` and connected the
    // signal via `autosave_timer.timeout.connect(_on_Autosave_timeout)`.
    // The same-file rewriter only rewrote `<token>(` call sites and missed
    // bare callable references (passing the function as a Callable). The
    // fix accepts bare references when there's no name collision in the
    // file.
    let source = "\
class_name AutosaveDriver
extends Node

@onready var autosave_timer: Timer = $Timer

func _ready() -> void:
\tautosave_timer.timeout.connect(_on_Autosave_timeout)

func _on_Autosave_timeout() -> void:
\tprint(\"saving\")
";
    let config = default_config();
    let diagnostics = linter::lint_source(source, "autosave.gd", &config);
    let fixed = fixer::apply_fixes(source, &diagnostics, false);
    assert!(
        fixed.contains("connect(_on_autosave_timeout)"),
        "callable reference passed to .connect() should be renamed; got:\n{}",
        fixed
    );
    assert!(
        !fixed.contains("_on_Autosave_timeout"),
        "no occurrence of the old name should remain; got:\n{}",
        fixed
    );
}

#[test]
fn regression_bug15_one_statement_per_line_skips_match_arms() {
    // Bug 15: GodSVG had `match int(hp):\n\t\t0: r1 = c; g1 = x` (a match
    // arm with `;`-joined body). The autofix split the `;` into a new line
    // at the same indent, orphaning `g1 = x` from the arm. Match arms
    // (`<pattern>: stmt1; stmt2`) must be left alone: the arm body needs
    // to become an indented block, which we don't synthesise.
    let source = "\
extends Node

func decode(c: int, x: int) -> Array:
\tvar r1 := 0.0
\tvar g1 := 0.0
\tvar b1 := 0.0
\tmatch c:
\t\t0: r1 = x; g1 = c
\t\t1: r1 = c; g1 = x
\treturn [r1, g1, b1]
";
    let config = default_config();
    let diagnostics = linter::lint_source(source, "test.gd", &config);
    let bad: Vec<_> = diagnostics
        .iter()
        .filter(|d| d.rule == "format/one-statement-per-line")
        .collect();
    assert!(
        bad.is_empty(),
        "match-arm `;` separators must not be flagged; got: {:?}",
        bad.iter()
            .map(|d| (d.span.line, &d.message))
            .collect::<Vec<_>>()
    );

    // Sanity: a regular `;` in a normal function body still gets flagged.
    let normal = "\
extends Node

func foo() -> void:
\tvar a = 1; var b = 2
";
    let normal_diags = linter::lint_source(normal, "test.gd", &config);
    assert!(
        normal_diags
            .iter()
            .any(|d| d.rule == "format/one-statement-per-line"),
        "ordinary `;` between statements should still be flagged"
    );
}

#[test]
fn regression_bug16_rename_suppressed_when_new_name_collides() {
    // Bug 16: GodSVG defined `const e = E`, `const pi = PI` (lowercase
    // mathematical aliases). The constant-name-screaming-case rule renamed
    // them to `const E = E` (duplicate) and `const PI = PI` (cyclic, since
    // `PI` is a Godot built-in). The fix suppresses any naming rule rewrite
    // when the proposed new name already exists as a declared name OR
    // appears anywhere in the source as an identifier (built-in, autoload,
    // imported class).
    let source = "\
class_name MathConsts extends Object

const E = 2.71828
const PHI = 1.618
const e = E
const pi = PI
const tau = TAU
";
    let config = default_config();
    let diagnostics = linter::lint_source(source, "math.gd", &config);
    let fixed = fixer::apply_fixes(source, &diagnostics, false);

    // None of `e`, `pi`, `tau` should be renamed to their uppercase
    // collision targets.
    assert!(
        fixed.contains("const e = E"),
        "`const e = E` should NOT be rewritten (would duplicate `const E`); got:\n{}",
        fixed
    );
    assert!(
        fixed.contains("const pi = PI"),
        "`const pi = PI` should NOT be rewritten (would self-reference Godot built-in `PI`); got:\n{}",
        fixed
    );
    assert!(
        fixed.contains("const tau = TAU"),
        "`const tau = TAU` should NOT be rewritten; got:\n{}",
        fixed
    );

    // A plain non-colliding rename should still apply.
    let normal = "\
class_name X extends Object
const myConst = 1
";
    let normal_diags = linter::lint_source(normal, "x.gd", &config);
    let normal_fixed = fixer::apply_fixes(normal, &normal_diags, false);
    assert!(
        normal_fixed.contains("const MY_CONST = 1"),
        "non-colliding camelCase const should still get renamed to SCREAMING_CASE; got:\n{}",
        normal_fixed
    );
}