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
|
/*************************************************************************/
/* gdscript_editor.cpp */
/*************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/*************************************************************************/
/* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
/* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/*************************************************************************/
#include "gdscript.h"
#include "core/engine.h"
#include "core/global_constants.h"
#include "core/os/file_access.h"
#include "gdscript_compiler.h"
#ifdef TOOLS_ENABLED
#include "editor/editor_file_system.h"
#include "editor/editor_settings.h"
#endif
void GDScriptLanguage::get_comment_delimiters(List<String> *p_delimiters) const {
p_delimiters->push_back("#");
}
void GDScriptLanguage::get_string_delimiters(List<String> *p_delimiters) const {
p_delimiters->push_back("\" \"");
p_delimiters->push_back("' '");
p_delimiters->push_back("\"\"\" \"\"\"");
}
String GDScriptLanguage::_get_processed_template(const String &p_template, const String &p_base_class_name) const {
String processed_template = p_template;
#ifdef TOOLS_ENABLED
if (EDITOR_DEF("text_editor/completion/add_type_hints", false)) {
processed_template = processed_template.replace("%INT_TYPE%", ": int");
processed_template = processed_template.replace("%STRING_TYPE%", ": String");
processed_template = processed_template.replace("%FLOAT_TYPE%", ": float");
processed_template = processed_template.replace("%VOID_RETURN%", " -> void");
} else {
processed_template = processed_template.replace("%INT_TYPE%", "");
processed_template = processed_template.replace("%STRING_TYPE%", "");
processed_template = processed_template.replace("%FLOAT_TYPE%", "");
processed_template = processed_template.replace("%VOID_RETURN%", "");
}
#else
processed_template = processed_template.replace("%INT_TYPE%", "");
processed_template = processed_template.replace("%STRING_TYPE%", "");
processed_template = processed_template.replace("%FLOAT_TYPE%", "");
processed_template = processed_template.replace("%VOID_RETURN%", "");
#endif
processed_template = processed_template.replace("%BASE%", p_base_class_name);
processed_template = processed_template.replace("%TS%", _get_indentation());
return processed_template;
}
Ref<Script> GDScriptLanguage::get_template(const String &p_class_name, const String &p_base_class_name) const {
String _template = "extends %BASE%\n"
"\n"
"# Declare member variables here. Examples:\n"
"# var a%INT_TYPE% = 2\n"
"# var b%STRING_TYPE% = \"text\"\n"
"\n"
"# Called when the node enters the scene tree for the first time.\n"
"func _ready()%VOID_RETURN%:\n"
"%TS%pass # Replace with function body.\n"
"\n"
"# Called every frame. 'delta' is the elapsed time since the previous frame.\n"
"#func _process(delta%FLOAT_TYPE%)%VOID_RETURN%:\n"
"#%TS%pass\n";
_template = _get_processed_template(_template, p_base_class_name);
Ref<GDScript> script;
script.instance();
script->set_source_code(_template);
return script;
}
bool GDScriptLanguage::is_using_templates() {
return true;
}
void GDScriptLanguage::make_template(const String &p_class_name, const String &p_base_class_name, Ref<Script> &p_script) {
String _template = _get_processed_template(p_script->get_source_code(), p_base_class_name);
p_script->set_source_code(_template);
}
bool GDScriptLanguage::validate(const String &p_script, int &r_line_error, int &r_col_error, String &r_test_error, const String &p_path, List<String> *r_functions, List<ScriptLanguage::Warning> *r_warnings, Set<int> *r_safe_lines) const {
GDScriptParser parser;
Error err = parser.parse(p_script, p_path.get_base_dir(), true, p_path, false, r_safe_lines);
#ifdef DEBUG_ENABLED
if (r_warnings) {
for (const List<GDScriptWarning>::Element *E = parser.get_warnings().front(); E; E = E->next()) {
const GDScriptWarning &warn = E->get();
ScriptLanguage::Warning w;
w.line = warn.line;
w.code = (int)warn.code;
w.string_code = GDScriptWarning::get_name_from_code(warn.code);
w.message = warn.get_message();
r_warnings->push_back(w);
}
}
#endif
if (err) {
r_line_error = parser.get_error_line();
r_col_error = parser.get_error_column();
r_test_error = parser.get_error();
return false;
} else {
const GDScriptParser::Node *root = parser.get_parse_tree();
ERR_FAIL_COND_V(root->type != GDScriptParser::Node::TYPE_CLASS, false);
const GDScriptParser::ClassNode *cl = static_cast<const GDScriptParser::ClassNode *>(root);
Map<int, String> funcs;
for (int i = 0; i < cl->functions.size(); i++) {
funcs[cl->functions[i]->line] = cl->functions[i]->name;
}
for (int i = 0; i < cl->static_functions.size(); i++) {
funcs[cl->static_functions[i]->line] = cl->static_functions[i]->name;
}
for (int i = 0; i < cl->subclasses.size(); i++) {
for (int j = 0; j < cl->subclasses[i]->functions.size(); j++) {
funcs[cl->subclasses[i]->functions[j]->line] = String(cl->subclasses[i]->name) + "." + String(cl->subclasses[i]->functions[j]->name);
}
}
for (Map<int, String>::Element *E = funcs.front(); E; E = E->next()) {
r_functions->push_back(E->get() + ":" + itos(E->key()));
}
}
return true;
}
bool GDScriptLanguage::has_named_classes() const {
return false;
}
bool GDScriptLanguage::supports_builtin_mode() const {
return true;
}
int GDScriptLanguage::find_function(const String &p_function, const String &p_code) const {
GDScriptTokenizerText tokenizer;
tokenizer.set_code(p_code);
int indent = 0;
while (tokenizer.get_token() != GDScriptTokenizer::TK_EOF && tokenizer.get_token() != GDScriptTokenizer::TK_ERROR) {
if (tokenizer.get_token() == GDScriptTokenizer::TK_NEWLINE) {
indent = tokenizer.get_token_line_indent();
}
if (indent == 0 && tokenizer.get_token() == GDScriptTokenizer::TK_PR_FUNCTION && tokenizer.get_token(1) == GDScriptTokenizer::TK_IDENTIFIER) {
String identifier = tokenizer.get_token_identifier(1);
if (identifier == p_function) {
return tokenizer.get_token_line();
}
}
tokenizer.advance();
}
return -1;
}
Script *GDScriptLanguage::create_script() const {
return memnew(GDScript);
}
/* DEBUGGER FUNCTIONS */
bool GDScriptLanguage::debug_break_parse(const String &p_file, int p_line, const String &p_error) {
//break because of parse error
if (ScriptDebugger::get_singleton() && Thread::get_caller_id() == Thread::get_main_id()) {
_debug_parse_err_line = p_line;
_debug_parse_err_file = p_file;
_debug_error = p_error;
ScriptDebugger::get_singleton()->debug(this, false);
return true;
} else {
return false;
}
}
bool GDScriptLanguage::debug_break(const String &p_error, bool p_allow_continue) {
if (ScriptDebugger::get_singleton() && Thread::get_caller_id() == Thread::get_main_id()) {
_debug_parse_err_line = -1;
_debug_parse_err_file = "";
_debug_error = p_error;
ScriptDebugger::get_singleton()->debug(this, p_allow_continue);
return true;
} else {
return false;
}
}
String GDScriptLanguage::debug_get_error() const {
return _debug_error;
}
int GDScriptLanguage::debug_get_stack_level_count() const {
if (_debug_parse_err_line >= 0)
return 1;
return _debug_call_stack_pos;
}
int GDScriptLanguage::debug_get_stack_level_line(int p_level) const {
if (_debug_parse_err_line >= 0)
return _debug_parse_err_line;
ERR_FAIL_INDEX_V(p_level, _debug_call_stack_pos, -1);
int l = _debug_call_stack_pos - p_level - 1;
return *(_call_stack[l].line);
}
String GDScriptLanguage::debug_get_stack_level_function(int p_level) const {
if (_debug_parse_err_line >= 0)
return "";
ERR_FAIL_INDEX_V(p_level, _debug_call_stack_pos, "");
int l = _debug_call_stack_pos - p_level - 1;
return _call_stack[l].function->get_name();
}
String GDScriptLanguage::debug_get_stack_level_source(int p_level) const {
if (_debug_parse_err_line >= 0)
return _debug_parse_err_file;
ERR_FAIL_INDEX_V(p_level, _debug_call_stack_pos, "");
int l = _debug_call_stack_pos - p_level - 1;
return _call_stack[l].function->get_source();
}
void GDScriptLanguage::debug_get_stack_level_locals(int p_level, List<String> *p_locals, List<Variant> *p_values, int p_max_subitems, int p_max_depth) {
if (_debug_parse_err_line >= 0)
return;
ERR_FAIL_INDEX(p_level, _debug_call_stack_pos);
int l = _debug_call_stack_pos - p_level - 1;
GDScriptFunction *f = _call_stack[l].function;
List<Pair<StringName, int> > locals;
f->debug_get_stack_member_state(*_call_stack[l].line, &locals);
for (List<Pair<StringName, int> >::Element *E = locals.front(); E; E = E->next()) {
p_locals->push_back(E->get().first);
p_values->push_back(_call_stack[l].stack[E->get().second]);
}
}
void GDScriptLanguage::debug_get_stack_level_members(int p_level, List<String> *p_members, List<Variant> *p_values, int p_max_subitems, int p_max_depth) {
if (_debug_parse_err_line >= 0)
return;
ERR_FAIL_INDEX(p_level, _debug_call_stack_pos);
int l = _debug_call_stack_pos - p_level - 1;
GDScriptInstance *instance = _call_stack[l].instance;
if (!instance)
return;
Ref<GDScript> script = instance->get_script();
ERR_FAIL_COND(script.is_null());
const Map<StringName, GDScript::MemberInfo> &mi = script->debug_get_member_indices();
for (const Map<StringName, GDScript::MemberInfo>::Element *E = mi.front(); E; E = E->next()) {
p_members->push_back(E->key());
p_values->push_back(instance->debug_get_member_by_index(E->get().index));
}
}
ScriptInstance *GDScriptLanguage::debug_get_stack_level_instance(int p_level) {
ERR_FAIL_COND_V(_debug_parse_err_line >= 0, NULL);
ERR_FAIL_INDEX_V(p_level, _debug_call_stack_pos, NULL);
int l = _debug_call_stack_pos - p_level - 1;
ScriptInstance *instance = _call_stack[l].instance;
return instance;
}
void GDScriptLanguage::debug_get_globals(List<String> *p_globals, List<Variant> *p_values, int p_max_subitems, int p_max_depth) {
const Map<StringName, int> &name_idx = GDScriptLanguage::get_singleton()->get_global_map();
const Variant *globals = GDScriptLanguage::get_singleton()->get_global_array();
List<Pair<String, Variant> > cinfo;
get_public_constants(&cinfo);
for (const Map<StringName, int>::Element *E = name_idx.front(); E; E = E->next()) {
if (ClassDB::class_exists(E->key()) || Engine::get_singleton()->has_singleton(E->key()))
continue;
bool is_script_constant = false;
for (List<Pair<String, Variant> >::Element *CE = cinfo.front(); CE; CE = CE->next()) {
if (CE->get().first == E->key()) {
is_script_constant = true;
break;
}
}
if (is_script_constant)
continue;
const Variant &var = globals[E->value()];
if (Object *obj = var) {
if (Object::cast_to<GDScriptNativeClass>(obj))
continue;
}
bool skip = false;
for (int i = 0; i < GlobalConstants::get_global_constant_count(); i++) {
if (E->key() == GlobalConstants::get_global_constant_name(i)) {
skip = true;
break;
}
}
if (skip)
continue;
p_globals->push_back(E->key());
p_values->push_back(var);
}
}
String GDScriptLanguage::debug_parse_stack_level_expression(int p_level, const String &p_expression, int p_max_subitems, int p_max_depth) {
if (_debug_parse_err_line >= 0)
return "";
return "";
}
void GDScriptLanguage::get_recognized_extensions(List<String> *p_extensions) const {
p_extensions->push_back("gd");
}
void GDScriptLanguage::get_public_functions(List<MethodInfo> *p_functions) const {
for (int i = 0; i < GDScriptFunctions::FUNC_MAX; i++) {
p_functions->push_back(GDScriptFunctions::get_info(GDScriptFunctions::Function(i)));
}
//not really "functions", but..
{
MethodInfo mi;
mi.name = "preload";
mi.arguments.push_back(PropertyInfo(Variant::STRING, "path"));
mi.return_val = PropertyInfo(Variant::OBJECT, "", PROPERTY_HINT_RESOURCE_TYPE, "Resource");
p_functions->push_back(mi);
}
{
MethodInfo mi;
mi.name = "yield";
mi.arguments.push_back(PropertyInfo(Variant::OBJECT, "object"));
mi.arguments.push_back(PropertyInfo(Variant::STRING, "signal"));
mi.default_arguments.push_back(Variant());
mi.default_arguments.push_back(String());
mi.return_val = PropertyInfo(Variant::OBJECT, "", PROPERTY_HINT_RESOURCE_TYPE, "GDScriptFunctionState");
p_functions->push_back(mi);
}
{
MethodInfo mi;
mi.name = "assert";
mi.return_val.type = Variant::NIL;
mi.arguments.push_back(PropertyInfo(Variant::BOOL, "condition"));
p_functions->push_back(mi);
}
}
void GDScriptLanguage::get_public_constants(List<Pair<String, Variant> > *p_constants) const {
Pair<String, Variant> pi;
pi.first = "PI";
pi.second = Math_PI;
p_constants->push_back(pi);
Pair<String, Variant> tau;
tau.first = "TAU";
tau.second = Math_TAU;
p_constants->push_back(tau);
Pair<String, Variant> infinity;
infinity.first = "INF";
infinity.second = Math_INF;
p_constants->push_back(infinity);
Pair<String, Variant> nan;
nan.first = "NAN";
nan.second = Math_NAN;
p_constants->push_back(nan);
}
String GDScriptLanguage::make_function(const String &p_class, const String &p_name, const PoolStringArray &p_args) const {
#ifdef TOOLS_ENABLED
bool th = EditorSettings::get_singleton()->get_setting("text_editor/completion/add_type_hints");
#else
bool th = false;
#endif
String s = "func " + p_name + "(";
if (p_args.size()) {
for (int i = 0; i < p_args.size(); i++) {
if (i > 0)
s += ", ";
s += p_args[i].get_slice(":", 0);
if (th) {
String type = p_args[i].get_slice(":", 1);
if (!type.empty() && type != "var") {
s += ": " + type;
}
}
}
}
s += String(")") + (th ? " -> void" : "") + ":\n" + _get_indentation() + "pass # Replace with function body.\n";
return s;
}
//////// COMPLETION //////////
#if defined(DEBUG_METHODS_ENABLED) && defined(TOOLS_ENABLED)
struct GDScriptCompletionContext {
const GDScriptParser::ClassNode *_class;
const GDScriptParser::FunctionNode *function;
const GDScriptParser::BlockNode *block;
Object *base;
String base_path;
int line;
uint32_t depth;
GDScriptCompletionContext() :
_class(NULL),
function(NULL),
block(NULL),
base(NULL),
line(0),
depth(0) {}
};
struct GDScriptCompletionIdentifier {
GDScriptParser::DataType type;
String enumeration;
Variant value;
const GDScriptParser::Node *assigned_expression;
GDScriptCompletionIdentifier() :
assigned_expression(NULL) {}
};
static void _get_directory_contents(EditorFileSystemDirectory *p_dir, Set<String> &r_list) {
const String quote_style = EDITOR_DEF("text_editor/completion/use_single_quotes", false) ? "'" : "\"";
for (int i = 0; i < p_dir->get_file_count(); i++) {
r_list.insert(quote_style + p_dir->get_file_path(i) + quote_style);
}
for (int i = 0; i < p_dir->get_subdir_count(); i++) {
_get_directory_contents(p_dir->get_subdir(i), r_list);
}
}
static String _get_visual_datatype(const PropertyInfo &p_info, bool p_isarg = true) {
if (p_info.usage & PROPERTY_USAGE_CLASS_IS_ENUM) {
String enum_name = p_info.class_name;
if (enum_name.find(".") == -1) {
return enum_name;
}
return enum_name.get_slice(".", 1);
}
String n = p_info.name;
int idx = n.find(":");
if (idx != -1) {
return n.substr(idx + 1, n.length());
}
if (p_info.type == Variant::OBJECT) {
if (p_info.hint == PROPERTY_HINT_RESOURCE_TYPE) {
return p_info.hint_string;
} else {
return p_info.class_name.operator String();
}
}
if (p_info.type == Variant::NIL) {
if (p_isarg || (p_info.usage & PROPERTY_USAGE_NIL_IS_VARIANT)) {
return "var";
} else {
return "void";
}
}
return Variant::get_type_name(p_info.type);
}
static GDScriptCompletionIdentifier _type_from_variant(const Variant &p_value) {
GDScriptCompletionIdentifier ci;
ci.value = p_value;
ci.type.is_constant = true;
ci.type.has_type = true;
ci.type.kind = GDScriptParser::DataType::BUILTIN;
ci.type.builtin_type = p_value.get_type();
if (ci.type.builtin_type == Variant::OBJECT) {
Object *obj = p_value.operator Object *();
if (!obj) {
return ci;
}
ci.type.native_type = obj->get_class_name();
Ref<Script> scr = p_value;
if (scr.is_valid()) {
ci.type.is_meta_type = true;
} else {
ci.type.is_meta_type = false;
scr = obj->get_script();
}
if (scr.is_valid()) {
ci.type.script_type = scr;
Ref<GDScript> gds = scr;
if (gds.is_valid()) {
ci.type.kind = GDScriptParser::DataType::GDSCRIPT;
} else {
ci.type.kind = GDScriptParser::DataType::SCRIPT;
}
ci.type.native_type = scr->get_instance_base_type();
} else {
ci.type.kind = GDScriptParser::DataType::NATIVE;
}
}
return ci;
}
static GDScriptCompletionIdentifier _type_from_property(const PropertyInfo &p_property) {
GDScriptCompletionIdentifier ci;
if (p_property.type == Variant::NIL) {
// Variant
return ci;
}
if (p_property.usage & PROPERTY_USAGE_CLASS_IS_ENUM) {
ci.enumeration = p_property.class_name;
}
ci.type.has_type = true;
ci.type.builtin_type = p_property.type;
if (p_property.type == Variant::OBJECT) {
ci.type.kind = GDScriptParser::DataType::NATIVE;
ci.type.native_type = p_property.class_name == StringName() ? "Object" : p_property.class_name;
} else {
ci.type.kind = GDScriptParser::DataType::BUILTIN;
}
return ci;
}
static GDScriptCompletionIdentifier _type_from_gdtype(const GDScriptDataType &p_gdtype) {
GDScriptCompletionIdentifier ci;
if (!p_gdtype.has_type) {
return ci;
}
ci.type.has_type = true;
ci.type.builtin_type = p_gdtype.builtin_type;
ci.type.native_type = p_gdtype.native_type;
ci.type.script_type = p_gdtype.script_type;
switch (p_gdtype.kind) {
case GDScriptDataType::UNINITIALIZED: {
ERR_EXPLAIN("Uninitialized completion. Please report a bug.");
} break;
case GDScriptDataType::BUILTIN: {
ci.type.kind = GDScriptParser::DataType::BUILTIN;
} break;
case GDScriptDataType::NATIVE: {
ci.type.kind = GDScriptParser::DataType::NATIVE;
} break;
case GDScriptDataType::GDSCRIPT: {
ci.type.kind = GDScriptParser::DataType::GDSCRIPT;
} break;
case GDScriptDataType::SCRIPT: {
ci.type.kind = GDScriptParser::DataType::SCRIPT;
} break;
}
return ci;
}
static bool _guess_identifier_type(GDScriptCompletionContext &p_context, const StringName &p_identifier, GDScriptCompletionIdentifier &r_type);
static bool _guess_identifier_type_from_base(GDScriptCompletionContext &p_context, const GDScriptCompletionIdentifier &p_base, const StringName &p_identifier, GDScriptCompletionIdentifier &r_type);
static bool _guess_method_return_type_from_base(GDScriptCompletionContext &p_context, const GDScriptCompletionIdentifier &p_base, const StringName &p_method, GDScriptCompletionIdentifier &r_type);
static bool _guess_expression_type(GDScriptCompletionContext &p_context, const GDScriptParser::Node *p_expression, GDScriptCompletionIdentifier &r_type) {
bool found = false;
if (++p_context.depth > 100) {
print_error("Maximum _guess_expression_type depth limit reached. Please file a bugreport.");
return false;
}
switch (p_expression->type) {
case GDScriptParser::Node::TYPE_CONSTANT: {
const GDScriptParser::ConstantNode *cn = static_cast<const GDScriptParser::ConstantNode *>(p_expression);
r_type = _type_from_variant(cn->value);
found = true;
} break;
case GDScriptParser::Node::TYPE_SELF: {
if (p_context._class) {
r_type.type.has_type = true;
r_type.type.kind = GDScriptParser::DataType::CLASS;
r_type.type.class_type = const_cast<GDScriptParser::ClassNode *>(p_context._class);
r_type.type.is_constant = true;
r_type.value = p_context.base;
found = true;
}
} break;
case GDScriptParser::Node::TYPE_IDENTIFIER: {
const GDScriptParser::IdentifierNode *id = static_cast<const GDScriptParser::IdentifierNode *>(p_expression);
found = _guess_identifier_type(p_context, id->name, r_type);
} break;
case GDScriptParser::Node::TYPE_DICTIONARY: {
// Try to recreate the dictionary
const GDScriptParser::DictionaryNode *dn = static_cast<const GDScriptParser::DictionaryNode *>(p_expression);
Dictionary d;
bool full = true;
for (int i = 0; i < dn->elements.size(); i++) {
GDScriptCompletionIdentifier key;
if (_guess_expression_type(p_context, dn->elements[i].key, key)) {
GDScriptCompletionIdentifier value;
if (_guess_expression_type(p_context, dn->elements[i].value, value)) {
if (!value.type.is_constant) {
full = false;
break;
}
d[key.value] = value.value;
} else {
full = false;
break;
}
} else {
full = false;
break;
}
}
if (full) {
// If not fully constant, setting this value is detrimental to the inference
r_type.value = d;
r_type.type.is_constant = true;
}
r_type.type.has_type = true;
r_type.type.kind = GDScriptParser::DataType::BUILTIN;
r_type.type.builtin_type = Variant::DICTIONARY;
} break;
case GDScriptParser::Node::TYPE_ARRAY: {
// Try to recreate the array
const GDScriptParser::ArrayNode *an = static_cast<const GDScriptParser::ArrayNode *>(p_expression);
Array a;
bool full = true;
a.resize(an->elements.size());
for (int i = 0; i < an->elements.size(); i++) {
GDScriptCompletionIdentifier value;
if (_guess_expression_type(p_context, an->elements[i], value)) {
a[i] = value.value;
} else {
full = false;
break;
}
}
if (full) {
// If not fully constant, setting this value is detrimental to the inference
r_type.value = a;
}
r_type.type.has_type = true;
r_type.type.kind = GDScriptParser::DataType::BUILTIN;
r_type.type.builtin_type = Variant::ARRAY;
} break;
case GDScriptParser::Node::TYPE_OPERATOR: {
const GDScriptParser::OperatorNode *op = static_cast<const GDScriptParser::OperatorNode *>(p_expression);
switch (op->op) {
case GDScriptParser::OperatorNode::OP_CALL: {
if (op->arguments[0]->type == GDScriptParser::Node::TYPE_TYPE) {
const GDScriptParser::TypeNode *tn = static_cast<const GDScriptParser::TypeNode *>(op->arguments[0]);
r_type.type.has_type = true;
r_type.type.kind = GDScriptParser::DataType::BUILTIN;
r_type.type.builtin_type = tn->vtype;
found = true;
break;
} else if (op->arguments[0]->type == GDScriptParser::Node::TYPE_BUILT_IN_FUNCTION) {
const GDScriptParser::BuiltInFunctionNode *bin = static_cast<const GDScriptParser::BuiltInFunctionNode *>(op->arguments[0]);
MethodInfo mi = GDScriptFunctions::get_info(bin->function);
r_type = _type_from_property(mi.return_val);
found = true;
break;
} else if (op->arguments.size() >= 2 && op->arguments[1]->type == GDScriptParser::Node::TYPE_IDENTIFIER) {
StringName id = static_cast<const GDScriptParser::IdentifierNode *>(op->arguments[1])->name;
GDScriptCompletionContext c = p_context;
c.line = op->line;
GDScriptCompletionIdentifier base;
if (!_guess_expression_type(c, op->arguments[0], base)) {
found = false;
break;
}
// Try call if constant methods with constant arguments
if (base.type.is_constant && base.value.get_type() == Variant::OBJECT) {
GDScriptParser::DataType native_type = base.type;
while (native_type.kind == GDScriptParser::DataType::CLASS) {
native_type = native_type.class_type->base_type;
}
while (native_type.kind == GDScriptParser::DataType::GDSCRIPT || native_type.kind == GDScriptParser::DataType::SCRIPT) {
if (native_type.script_type.is_valid()) {
Ref<Script> parent = native_type.script_type->get_base_script();
if (parent.is_valid()) {
native_type.script_type = parent;
} else {
native_type.kind = GDScriptParser::DataType::NATIVE;
native_type.native_type = native_type.script_type->get_instance_base_type();
if (!ClassDB::class_exists(native_type.native_type)) {
native_type.native_type = String("_") + native_type.native_type;
if (!ClassDB::class_exists(native_type.native_type)) {
native_type.has_type = false;
}
}
}
}
}
if (native_type.has_type && native_type.kind == GDScriptParser::DataType::NATIVE) {
MethodBind *mb = ClassDB::get_method(native_type.native_type, id);
if (mb && mb->is_const()) {
bool all_is_const = true;
Vector<Variant> args;
GDScriptCompletionContext c2 = p_context;
c2.line = op->line;
for (int i = 2; all_is_const && i < op->arguments.size(); i++) {
GDScriptCompletionIdentifier arg;
if (_guess_expression_type(c2, op->arguments[i], arg)) {
if (arg.type.has_type && arg.type.is_constant && arg.value.get_type() != Variant::OBJECT) {
args.push_back(arg.value);
} else {
all_is_const = false;
}
} else {
all_is_const = false;
}
}
Object *baseptr = base.value;
if (all_is_const && String(id) == "get_node" && ClassDB::is_parent_class(native_type.native_type, "Node") && args.size()) {
String arg1 = args[0];
if (arg1.begins_with("/root/")) {
String which = arg1.get_slice("/", 2);
if (which != "") {
// Try singletons first
if (GDScriptLanguage::get_singleton()->get_named_globals_map().has(which)) {
r_type = _type_from_variant(GDScriptLanguage::get_singleton()->get_named_globals_map()[which]);
found = true;
} else {
List<PropertyInfo> props;
ProjectSettings::get_singleton()->get_property_list(&props);
for (List<PropertyInfo>::Element *E = props.front(); E; E = E->next()) {
String s = E->get().name;
if (!s.begins_with("autoload/")) {
continue;
}
String name = s.get_slice("/", 1);
if (name == which) {
String script = ProjectSettings::get_singleton()->get(s);
if (script.begins_with("*")) {
script = script.right(1);
}
if (!script.begins_with("res://")) {
script = "res://" + script;
}
if (!script.ends_with(".gd")) {
//not a script, try find the script anyway,
//may have some success
script = script.get_basename() + ".gd";
}
if (FileAccess::exists(script)) {
Ref<Script> scr;
if (ScriptCodeCompletionCache::get_singleton()) {
scr = ScriptCodeCompletionCache::get_singleton()->get_cached_resource(script);
} else {
scr = ResourceLoader::load(script);
}
if (scr.is_valid()) {
r_type.type.has_type = true;
r_type.type.script_type = scr;
r_type.type.is_constant = false;
Ref<GDScript> gds = scr;
if (gds.is_valid()) {
r_type.type.kind = GDScriptParser::DataType::GDSCRIPT;
} else {
r_type.type.kind = GDScriptParser::DataType::SCRIPT;
}
r_type.value = Variant();
found = true;
}
}
break;
}
}
}
}
}
}
if (!found && all_is_const && baseptr) {
Vector<const Variant *> argptr;
for (int i = 0; i < args.size(); i++) {
argptr.push_back(&args[i]);
}
Variant::CallError ce;
Variant ret = mb->call(baseptr, (const Variant **)argptr.ptr(), argptr.size(), ce);
if (ce.error == Variant::CallError::CALL_OK && ret.get_type() != Variant::NIL) {
if (ret.get_type() != Variant::OBJECT || ret.operator Object *() != NULL) {
r_type = _type_from_variant(ret);
found = true;
}
}
}
}
}
}
if (!found) {
found = _guess_method_return_type_from_base(c, base, id, r_type);
}
}
} break;
case GDScriptParser::OperatorNode::OP_PARENT_CALL: {
if (!p_context._class || !op->arguments.size() || op->arguments[0]->type != GDScriptParser::Node::TYPE_IDENTIFIER) {
break;
}
StringName id = static_cast<const GDScriptParser::IdentifierNode *>(op->arguments[0])->name;
GDScriptCompletionIdentifier base;
base.value = p_context.base;
base.type = p_context._class->base_type;
GDScriptCompletionContext c = p_context;
c.line = op->line;
found = _guess_method_return_type_from_base(c, base, id, r_type);
} break;
case GDScriptParser::OperatorNode::OP_INDEX_NAMED: {
if (op->arguments.size() < 2 || op->arguments[1]->type != GDScriptParser::Node::TYPE_IDENTIFIER) {
found = false;
break;
}
const GDScriptParser::IdentifierNode *id = static_cast<const GDScriptParser::IdentifierNode *>(op->arguments[1]);
GDScriptCompletionContext c = p_context;
c.line = op->line;
GDScriptCompletionIdentifier base;
if (!_guess_expression_type(c, op->arguments[0], base)) {
found = false;
break;
}
if (base.value.get_type() == Variant::DICTIONARY && base.value.operator Dictionary().has(String(id->name))) {
Variant value = base.value.operator Dictionary()[String(id->name)];
r_type = _type_from_variant(value);
found = true;
break;
}
const GDScriptParser::DictionaryNode *dn = NULL;
if (op->arguments[0]->type == GDScriptParser::Node::TYPE_DICTIONARY) {
dn = static_cast<const GDScriptParser::DictionaryNode *>(op->arguments[0]);
} else if (base.assigned_expression && base.assigned_expression->type == GDScriptParser::Node::TYPE_DICTIONARY) {
dn = static_cast<const GDScriptParser::DictionaryNode *>(base.assigned_expression);
}
if (dn) {
for (int i = 0; i < dn->elements.size(); i++) {
GDScriptCompletionIdentifier key;
if (!_guess_expression_type(c, dn->elements[i].key, key)) {
continue;
}
if (key.value == String(id->name)) {
r_type.assigned_expression = dn->elements[i].value;
found = _guess_expression_type(c, dn->elements[i].value, r_type);
break;
}
}
}
if (!found) {
found = _guess_identifier_type_from_base(c, base, id->name, r_type);
}
} break;
case GDScriptParser::OperatorNode::OP_INDEX: {
if (op->arguments.size() < 2) {
found = false;
break;
}
GDScriptCompletionContext c = p_context;
c.line = op->line;
GDScriptCompletionIdentifier base;
if (!_guess_expression_type(c, op->arguments[0], base)) {
found = false;
break;
}
GDScriptCompletionIdentifier index;
if (!_guess_expression_type(c, op->arguments[1], index)) {
found = false;
break;
}
if (base.value.in(index.value)) {
Variant value = base.value.get(index.value);
r_type = _type_from_variant(value);
found = true;
break;
}
// Look if it is a dictionary node
const GDScriptParser::DictionaryNode *dn = NULL;
if (op->arguments[0]->type == GDScriptParser::Node::TYPE_DICTIONARY) {
dn = static_cast<const GDScriptParser::DictionaryNode *>(op->arguments[0]);
} else if (base.assigned_expression && base.assigned_expression->type == GDScriptParser::Node::TYPE_DICTIONARY) {
dn = static_cast<const GDScriptParser::DictionaryNode *>(base.assigned_expression);
}
if (dn) {
for (int i = 0; i < dn->elements.size(); i++) {
GDScriptCompletionIdentifier key;
if (!_guess_expression_type(c, dn->elements[i].key, key)) {
continue;
}
if (key.value == index.value) {
r_type.assigned_expression = dn->elements[i].value;
found = _guess_expression_type(p_context, dn->elements[i].value, r_type);
break;
}
}
}
// Look if it is an array node
if (!found && index.value.is_num()) {
int idx = index.value;
const GDScriptParser::ArrayNode *an = NULL;
if (op->arguments[0]->type == GDScriptParser::Node::TYPE_ARRAY) {
an = static_cast<const GDScriptParser::ArrayNode *>(op->arguments[0]);
} else if (base.assigned_expression && base.assigned_expression->type == GDScriptParser::Node::TYPE_ARRAY) {
an = static_cast<const GDScriptParser::ArrayNode *>(base.assigned_expression);
}
if (an && idx >= 0 && an->elements.size() > idx) {
r_type.assigned_expression = an->elements[idx];
found = _guess_expression_type(c, an->elements[idx], r_type);
break;
}
}
// Look for valid indexing in other types
if (!found && (index.value.get_type() == Variant::STRING || index.value.get_type() == Variant::NODE_PATH)) {
StringName id = index.value;
found = _guess_identifier_type_from_base(c, base, id, r_type);
} else if (!found && index.type.kind == GDScriptParser::DataType::BUILTIN) {
Variant::CallError err;
Variant base_val = Variant::construct(base.type.builtin_type, NULL, 0, err);
bool valid = false;
Variant res = base_val.get(index.value, &valid);
if (valid) {
r_type = _type_from_variant(res);
r_type.value = Variant();
r_type.type.is_constant = false;
found = true;
}
}
} break;
default: {
if (op->arguments.size() < 2) {
found = false;
break;
}
Variant::Operator vop = Variant::OP_MAX;
switch (op->op) {
case GDScriptParser::OperatorNode::OP_ADD: vop = Variant::OP_ADD; break;
case GDScriptParser::OperatorNode::OP_SUB: vop = Variant::OP_SUBTRACT; break;
case GDScriptParser::OperatorNode::OP_MUL: vop = Variant::OP_MULTIPLY; break;
case GDScriptParser::OperatorNode::OP_DIV: vop = Variant::OP_DIVIDE; break;
case GDScriptParser::OperatorNode::OP_MOD: vop = Variant::OP_MODULE; break;
case GDScriptParser::OperatorNode::OP_SHIFT_LEFT: vop = Variant::OP_SHIFT_LEFT; break;
case GDScriptParser::OperatorNode::OP_SHIFT_RIGHT: vop = Variant::OP_SHIFT_RIGHT; break;
case GDScriptParser::OperatorNode::OP_BIT_AND: vop = Variant::OP_BIT_AND; break;
case GDScriptParser::OperatorNode::OP_BIT_OR: vop = Variant::OP_BIT_OR; break;
case GDScriptParser::OperatorNode::OP_BIT_XOR: vop = Variant::OP_BIT_XOR; break;
default: {
}
}
if (vop == Variant::OP_MAX) {
break;
}
GDScriptCompletionContext context = p_context;
context.line = op->line;
GDScriptCompletionIdentifier p1;
GDScriptCompletionIdentifier p2;
if (!_guess_expression_type(context, op->arguments[0], p1)) {
found = false;
break;
}
if (!_guess_expression_type(context, op->arguments[1], p2)) {
found = false;
break;
}
Variant::CallError ce;
bool v1_use_value = p1.value.get_type() != Variant::NIL && p1.value.get_type() != Variant::OBJECT;
Variant v1 = (v1_use_value) ? p1.value : Variant::construct(p1.type.builtin_type, NULL, 0, ce);
bool v2_use_value = p2.value.get_type() != Variant::NIL && p2.value.get_type() != Variant::OBJECT;
Variant v2 = (v2_use_value) ? p2.value : Variant::construct(p2.type.builtin_type, NULL, 0, ce);
// avoid potential invalid ops
if ((vop == Variant::OP_DIVIDE || vop == Variant::OP_MODULE) && v2.get_type() == Variant::INT) {
v2 = 1;
v2_use_value = false;
}
if (vop == Variant::OP_DIVIDE && v2.get_type() == Variant::REAL) {
v2 = 1.0;
v2_use_value = false;
}
Variant res;
bool valid;
Variant::evaluate(vop, v1, v2, res, valid);
if (!valid) {
found = false;
break;
}
r_type = _type_from_variant(res);
if (!v1_use_value || !v2_use_value) {
r_type.value = Variant();
r_type.type.is_constant = false;
}
found = true;
} break;
}
} break;
default: {
}
}
// It may have found a null, but that's never useful
if (found && r_type.type.has_type && r_type.type.kind == GDScriptParser::DataType::BUILTIN && r_type.type.builtin_type == Variant::NIL) {
found = false;
}
// Check type hint last. For collections we want chance to get the actual value first
// This way we can detect types from the content of dictionaries and arrays
if (!found && p_expression->get_datatype().has_type) {
r_type.type = p_expression->get_datatype();
if (!r_type.assigned_expression) {
r_type.assigned_expression = p_expression;
}
found = true;
}
return found;
}
static bool _guess_identifier_type(GDScriptCompletionContext &p_context, const StringName &p_identifier, GDScriptCompletionIdentifier &r_type) {
// Look in blocks first
const GDScriptParser::BlockNode *blk = p_context.block;
int last_assign_line = -1;
const GDScriptParser::Node *last_assigned_expression = NULL;
GDScriptParser::DataType var_type;
while (blk) {
if (blk->variables.has(p_identifier)) {
if (blk->variables[p_identifier]->line > p_context.line) {
return false;
}
var_type = blk->variables[p_identifier]->datatype;
if (!last_assigned_expression && blk->variables[p_identifier]->assign && blk->variables[p_identifier]->assign->type == GDScriptParser::Node::TYPE_OPERATOR) {
const GDScriptParser::OperatorNode *op = static_cast<const GDScriptParser::OperatorNode *>(blk->variables[p_identifier]->assign);
if (op->op == GDScriptParser::OperatorNode::OP_ASSIGN && op->arguments.size() >= 2) {
last_assign_line = op->line;
last_assigned_expression = op->arguments[1];
}
}
}
for (const List<GDScriptParser::Node *>::Element *E = blk->statements.front(); E; E = E->next()) {
const GDScriptParser::Node *expr = E->get();
if (expr->line > p_context.line || expr->type != GDScriptParser::Node::TYPE_OPERATOR) {
continue;
}
const GDScriptParser::OperatorNode *op = static_cast<const GDScriptParser::OperatorNode *>(expr);
if (op->op != GDScriptParser::OperatorNode::OP_ASSIGN || op->line < last_assign_line) {
continue;
}
if (op->arguments.size() >= 2 && op->arguments[0]->type == GDScriptParser::Node::TYPE_IDENTIFIER) {
const GDScriptParser::IdentifierNode *id = static_cast<const GDScriptParser::IdentifierNode *>(op->arguments[0]);
if (id->name == p_identifier) {
last_assign_line = op->line;
last_assigned_expression = op->arguments[1];
}
}
}
if (blk->if_condition && blk->if_condition->type == GDScriptParser::Node::TYPE_OPERATOR && static_cast<const GDScriptParser::OperatorNode *>(blk->if_condition)->op == GDScriptParser::OperatorNode::OP_IS) {
//is used, check if identifier is in there! this helps resolve in blocks that are (if (identifier is value)): which are very common..
//super dirty hack, but very useful
//credit: Zylann
//TODO: this could be hacked to detect ANDed conditions too..
const GDScriptParser::OperatorNode *op = static_cast<const GDScriptParser::OperatorNode *>(blk->if_condition);
if (op->arguments[0]->type == GDScriptParser::Node::TYPE_IDENTIFIER && static_cast<const GDScriptParser::IdentifierNode *>(op->arguments[0])->name == p_identifier) {
//bingo
GDScriptCompletionContext c = p_context;
c.line = op->line;
c.block = blk;
if (_guess_expression_type(p_context, op->arguments[1], r_type)) {
r_type.type.is_meta_type = false; // Right-hand of `is` will be a meta type, but the left-hand value is not
// Not an assignment, it shouldn't carry any value
r_type.value = Variant();
r_type.assigned_expression = NULL;
return true;
}
}
}
blk = blk->parent_block;
}
if (last_assigned_expression && last_assign_line != p_context.line) {
GDScriptCompletionContext c = p_context;
c.line = last_assign_line;
r_type.assigned_expression = last_assigned_expression;
if (_guess_expression_type(c, last_assigned_expression, r_type)) {
return true;
}
}
if (var_type.has_type) {
r_type.type = var_type;
return true;
}
if (p_context.function) {
for (int i = 0; i < p_context.function->arguments.size(); i++) {
if (p_context.function->arguments[i] == p_identifier) {
if (p_context.function->argument_types[i].has_type) {
r_type.type = p_context.function->argument_types[i];
return true;
}
int def_from = p_context.function->arguments.size() - p_context.function->default_values.size();
if (i >= def_from) {
int def_idx = i - def_from;
if (p_context.function->default_values[def_idx]->type == GDScriptParser::Node::TYPE_OPERATOR) {
const GDScriptParser::OperatorNode *op = static_cast<const GDScriptParser::OperatorNode *>(p_context.function->default_values[def_idx]);
if (op->arguments.size() < 2) {
return false;
}
GDScriptCompletionContext c = p_context;
c.function = NULL;
c.block = NULL;
return _guess_expression_type(c, op->arguments[1], r_type);
}
}
break;
}
}
GDScriptParser::DataType base_type = p_context._class->base_type;
while (base_type.has_type) {
switch (base_type.kind) {
case GDScriptParser::DataType::GDSCRIPT: {
Ref<GDScript> gds = base_type.script_type;
if (gds.is_valid() && gds->has_method(p_context.function->name)) {
GDScriptFunction *func = gds->get_member_functions()[p_context.function->name];
if (func) {
for (int i = 0; i < func->get_argument_count(); i++) {
if (func->get_argument_name(i) == p_identifier) {
r_type = _type_from_gdtype(func->get_argument_type(i));
return true;
}
}
}
Ref<GDScript> base_gds = gds->get_base_script();
if (base_gds.is_valid()) {
base_type.kind = GDScriptParser::DataType::GDSCRIPT;
base_type.script_type = base_gds;
} else {
base_type.kind = GDScriptParser::DataType::NATIVE;
base_type.native_type = gds->get_instance_base_type();
}
} else {
base_type.kind = GDScriptParser::DataType::NATIVE;
base_type.native_type = gds->get_instance_base_type();
}
} break;
case GDScriptParser::DataType::NATIVE: {
List<MethodInfo> methods;
ClassDB::get_method_list(base_type.native_type, &methods);
ClassDB::get_virtual_methods(base_type.native_type, &methods);
for (List<MethodInfo>::Element *E = methods.front(); E; E = E->next()) {
if (E->get().name == p_context.function->name) {
MethodInfo &mi = E->get();
for (List<PropertyInfo>::Element *F = mi.arguments.front(); F; F = F->next()) {
if (F->get().name == p_identifier) {
r_type = _type_from_property(F->get());
return true;
}
}
}
}
base_type.has_type = false;
} break;
default: {
base_type.has_type = false;
} break;
}
}
}
// Check current class (including inheritance)
if (p_context._class) {
GDScriptCompletionIdentifier context_base;
context_base.value = p_context.base;
context_base.type.has_type = true;
context_base.type.kind = GDScriptParser::DataType::CLASS;
context_base.type.class_type = const_cast<GDScriptParser::ClassNode *>(p_context._class);
context_base.type.is_meta_type = p_context.function && p_context.function->_static;
if (_guess_identifier_type_from_base(p_context, context_base, p_identifier, r_type)) {
return true;
}
}
// Check named scripts
if (ScriptServer::is_global_class(p_identifier)) {
Ref<Script> scr = ResourceLoader::load(ScriptServer::get_global_class_path(p_identifier));
if (scr.is_valid()) {
r_type = _type_from_variant(scr);
r_type.type.is_meta_type = true;
return true;
}
return false;
}
for (int i = 0; i < 2; i++) {
StringName target_id;
switch (i) {
case 0:
// Check ClassDB
target_id = p_identifier;
break;
case 1:
// ClassDB again for underscore-prefixed classes
target_id = String("_") + p_identifier;
break;
}
if (ClassDB::class_exists(target_id)) {
r_type.type.has_type = true;
r_type.type.kind = GDScriptParser::DataType::NATIVE;
r_type.type.native_type = target_id;
if (Engine::get_singleton()->has_singleton(target_id)) {
r_type.type.is_meta_type = false;
r_type.value = Engine::get_singleton()->get_singleton_object(target_id);
} else {
r_type.type.is_meta_type = true;
const Map<StringName, int>::Element *target_elem = GDScriptLanguage::get_singleton()->get_global_map().find(target_id);
// Check because classes like EditorNode are in ClassDB by now, but unknown to GDScript
if (!target_elem) {
return false;
}
int idx = target_elem->get();
r_type.value = GDScriptLanguage::get_singleton()->get_global_array()[idx];
}
return true;
}
}
// Check autoload singletons
if (GDScriptLanguage::get_singleton()->get_named_globals_map().has(p_identifier)) {
r_type = _type_from_variant(GDScriptLanguage::get_singleton()->get_named_globals_map()[p_identifier]);
return true;
}
return false;
}
static bool _guess_identifier_type_from_base(GDScriptCompletionContext &p_context, const GDScriptCompletionIdentifier &p_base, const StringName &p_identifier, GDScriptCompletionIdentifier &r_type) {
GDScriptParser::DataType base_type = p_base.type;
bool _static = base_type.is_meta_type;
while (base_type.has_type) {
switch (base_type.kind) {
case GDScriptParser::DataType::CLASS: {
if (base_type.class_type->constant_expressions.has(p_identifier)) {
GDScriptParser::ClassNode::Constant c = base_type.class_type->constant_expressions[p_identifier];
r_type.type = c.type;
if (c.expression->type == GDScriptParser::Node::TYPE_CONSTANT) {
r_type.value = static_cast<const GDScriptParser::ConstantNode *>(c.expression)->value;
}
return true;
}
if (!_static) {
for (int i = 0; i < base_type.class_type->variables.size(); i++) {
GDScriptParser::ClassNode::Member m = base_type.class_type->variables[i];
if (m.identifier == p_identifier) {
if (m.expression) {
if (p_context.line == m.expression->line) {
// Variable used in the same expression
return false;
}
if (_guess_expression_type(p_context, m.expression, r_type)) {
return true;
}
if (m.expression->get_datatype().has_type) {
r_type.type = m.expression->get_datatype();
return true;
}
}
if (m.data_type.has_type) {
r_type.type = m.data_type;
return true;
}
return false;
}
}
}
base_type = base_type.class_type->base_type;
} break;
case GDScriptParser::DataType::GDSCRIPT: {
Ref<GDScript> gds = base_type.script_type;
if (gds.is_valid()) {
if (gds->get_constants().has(p_identifier)) {
r_type = _type_from_variant(gds->get_constants()[p_identifier]);
return true;
}
if (!_static) {
const Set<StringName>::Element *m = gds->get_members().find(p_identifier);
if (m) {
r_type = _type_from_gdtype(gds->get_member_type(p_identifier));
return true;
}
}
Ref<GDScript> parent = gds->get_base_script();
if (parent.is_valid()) {
base_type.script_type = parent;
} else {
base_type.kind = GDScriptParser::DataType::NATIVE;
base_type.native_type = gds->get_instance_base_type();
}
} else {
return false;
}
} break;
case GDScriptParser::DataType::SCRIPT: {
Ref<Script> scr = base_type.script_type;
if (scr.is_valid()) {
Map<StringName, Variant> constants;
scr->get_constants(&constants);
if (constants.has(p_identifier)) {
r_type = _type_from_variant(constants[p_identifier]);
return true;
}
if (!_static) {
List<PropertyInfo> members;
scr->get_script_property_list(&members);
for (const List<PropertyInfo>::Element *E = members.front(); E; E = E->next()) {
const PropertyInfo &prop = E->get();
if (prop.name == p_identifier) {
r_type = _type_from_property(prop);
return true;
}
}
}
Ref<Script> parent = scr->get_base_script();
if (parent.is_valid()) {
base_type.script_type = parent;
} else {
base_type.kind = GDScriptParser::DataType::NATIVE;
base_type.native_type = scr->get_instance_base_type();
}
} else {
return false;
}
} break;
case GDScriptParser::DataType::NATIVE: {
StringName class_name = base_type.native_type;
if (!ClassDB::class_exists(class_name)) {
class_name = String("_") + class_name;
if (!ClassDB::class_exists(class_name)) {
return false;
}
}
// Skip constants since they're all integers. Type does not matter because int has no members
List<PropertyInfo> props;
ClassDB::get_property_list(class_name, &props);
for (const List<PropertyInfo>::Element *E = props.front(); E; E = E->next()) {
const PropertyInfo &prop = E->get();
if (prop.name == p_identifier) {
StringName getter = ClassDB::get_property_getter(class_name, p_identifier);
if (getter != StringName()) {
MethodBind *g = ClassDB::get_method(class_name, getter);
if (g) {
r_type = _type_from_property(g->get_return_info());
return true;
}
} else {
r_type = _type_from_property(prop);
return true;
}
break;
}
}
return false;
} break;
case GDScriptParser::DataType::BUILTIN: {
Variant::CallError err;
Variant tmp = Variant::construct(base_type.builtin_type, NULL, 0, err);
if (err.error != Variant::CallError::CALL_OK) {
return false;
}
bool valid = false;
Variant res = tmp.get(p_identifier, &valid);
if (valid) {
r_type = _type_from_variant(res);
r_type.value = Variant();
r_type.type.is_constant = false;
return true;
}
return false;
} break;
default: {
return false;
} break;
}
}
return false;
}
static bool _find_last_return_in_block(const GDScriptCompletionContext &p_context, int &r_last_return_line, const GDScriptParser::Node **r_last_returned_value) {
if (!p_context.block) {
return false;
}
for (int i = 0; i < p_context.block->statements.size(); i++) {
if (p_context.block->statements[i]->line < r_last_return_line) {
continue;
}
if (p_context.block->statements[i]->type != GDScriptParser::Node::TYPE_CONTROL_FLOW) {
continue;
}
const GDScriptParser::ControlFlowNode *cf = static_cast<const GDScriptParser::ControlFlowNode *>(p_context.block->statements[i]);
if (cf->cf_type == GDScriptParser::ControlFlowNode::CF_RETURN && cf->arguments.size() > 0) {
if (cf->line > r_last_return_line) {
r_last_return_line = cf->line;
*r_last_returned_value = cf->arguments[0];
}
}
}
// Recurse into subblocks
for (int i = 0; i < p_context.block->sub_blocks.size(); i++) {
GDScriptCompletionContext c = p_context;
c.block = p_context.block->sub_blocks[i];
_find_last_return_in_block(c, r_last_return_line, r_last_returned_value);
}
return false;
}
static bool _guess_method_return_type_from_base(GDScriptCompletionContext &p_context, const GDScriptCompletionIdentifier &p_base, const StringName &p_method, GDScriptCompletionIdentifier &r_type) {
GDScriptParser::DataType base_type = p_base.type;
bool _static = base_type.is_meta_type;
if (_static && p_method == "new") {
r_type.type = base_type;
r_type.type.is_meta_type = false;
r_type.type.is_constant = false;
return true;
}
while (base_type.has_type) {
switch (base_type.kind) {
case GDScriptParser::DataType::CLASS: {
if (!base_type.class_type) {
base_type.has_type = false;
break;
}
for (int i = 0; i < base_type.class_type->static_functions.size(); i++) {
if (base_type.class_type->static_functions[i]->name == p_method) {
int last_return_line = -1;
const GDScriptParser::Node *last_returned_value = NULL;
GDScriptCompletionContext c = p_context;
c._class = base_type.class_type;
c.function = base_type.class_type->static_functions[i];
c.block = c.function->body;
_find_last_return_in_block(c, last_return_line, &last_returned_value);
if (last_returned_value) {
c.line = c.block->end_line;
return _guess_expression_type(c, last_returned_value, r_type);
}
}
}
if (!_static) {
for (int i = 0; i < base_type.class_type->functions.size(); i++) {
if (base_type.class_type->functions[i]->name == p_method) {
int last_return_line = -1;
const GDScriptParser::Node *last_returned_value = NULL;
GDScriptCompletionContext c = p_context;
c._class = base_type.class_type;
c.function = base_type.class_type->functions[i];
c.block = c.function->body;
_find_last_return_in_block(c, last_return_line, &last_returned_value);
if (last_returned_value) {
c.line = c.block->end_line;
return _guess_expression_type(c, last_returned_value, r_type);
}
}
}
}
base_type = base_type.class_type->base_type;
} break;
case GDScriptParser::DataType::GDSCRIPT: {
Ref<GDScript> gds = base_type.script_type;
if (gds.is_valid()) {
if (gds->get_member_functions().has(p_method)) {
r_type = _type_from_gdtype(gds->get_member_functions()[p_method]->get_return_type());
return true;
}
Ref<GDScript> base_script = gds->get_base_script();
if (base_script.is_valid()) {
base_type.script_type = base_script;
} else {
base_type.kind = GDScriptParser::DataType::NATIVE;
base_type.native_type = gds->get_instance_base_type();
}
} else {
return false;
}
} break;
case GDScriptParser::DataType::SCRIPT: {
Ref<Script> scr = base_type.script_type;
if (scr.is_valid()) {
List<MethodInfo> methods;
scr->get_script_method_list(&methods);
for (List<MethodInfo>::Element *E = methods.front(); E; E = E->next()) {
MethodInfo &mi = E->get();
if (mi.name == p_method) {
r_type = _type_from_property(mi.return_val);
return true;
}
}
Ref<Script> base_script = scr->get_base_script();
if (base_script.is_valid()) {
base_type.script_type = base_script;
} else {
base_type.kind = GDScriptParser::DataType::NATIVE;
base_type.native_type = scr->get_instance_base_type();
}
} else {
return false;
}
} break;
case GDScriptParser::DataType::NATIVE: {
StringName native = base_type.native_type;
if (!ClassDB::class_exists(native)) {
native = String("_") + native;
if (!ClassDB::class_exists(native)) {
return false;
}
}
MethodBind *mb = ClassDB::get_method(native, p_method);
if (mb) {
r_type = _type_from_property(mb->get_return_info());
return true;
}
return false;
} break;
case GDScriptParser::DataType::BUILTIN: {
Variant::CallError err;
Variant tmp = Variant::construct(base_type.builtin_type, NULL, 0, err);
if (err.error != Variant::CallError::CALL_OK) {
return false;
}
List<MethodInfo> methods;
tmp.get_method_list(&methods);
for (List<MethodInfo>::Element *E = methods.front(); E; E = E->next()) {
MethodInfo &mi = E->get();
if (mi.name == p_method) {
r_type = _type_from_property(mi.return_val);
return true;
}
}
return false;
} break;
default: {
return false;
}
}
}
return false;
}
static String _make_arguments_hint(const MethodInfo &p_info, int p_arg_idx) {
String arghint = _get_visual_datatype(p_info.return_val, false) + " " + p_info.name + "(";
int def_args = p_info.arguments.size() - p_info.default_arguments.size();
int i = 0;
for (const List<PropertyInfo>::Element *E = p_info.arguments.front(); E; E = E->next()) {
if (i > 0) {
arghint += ", ";
} else {
arghint += " ";
}
if (i == p_arg_idx) {
arghint += String::chr(0xFFFF);
}
arghint += _get_visual_datatype(E->get(), true) + " " + E->get().name;
if (i - def_args >= 0) {
arghint += String(" = ") + p_info.default_arguments[i - def_args].get_construct_string();
}
if (i == p_arg_idx) {
arghint += String::chr(0xFFFF);
}
i++;
}
if (p_info.flags & METHOD_FLAG_VARARG) {
if (p_info.arguments.size() > 0) {
arghint += ", ";
} else {
arghint += " ";
}
if (p_arg_idx >= p_info.arguments.size()) {
arghint += String::chr(0xFFFF);
}
arghint += "...";
if (p_arg_idx >= p_info.arguments.size()) {
arghint += String::chr(0xFFFF);
}
}
if (p_info.arguments.size() > 0 || (p_info.flags & METHOD_FLAG_VARARG)) {
arghint += " ";
}
arghint += ")";
return arghint;
}
static String _make_arguments_hint(const GDScriptParser::FunctionNode *p_function, int p_arg_idx) {
String arghint = p_function->return_type.to_string() + " " + p_function->name.operator String() + "(";
int def_args = p_function->arguments.size() - p_function->default_values.size();
for (int i = 0; i < p_function->arguments.size(); i++) {
if (i > 0) {
arghint += ", ";
} else {
arghint += " ";
}
if (i == p_arg_idx) {
arghint += String::chr(0xFFFF);
}
arghint += p_function->argument_types[i].to_string() + " " + p_function->arguments[i].operator String();
if (i - def_args >= 0) {
String def_val = "<unknown>";
if (p_function->default_values[i - def_args] && p_function->default_values[i - def_args]->type == GDScriptParser::Node::TYPE_OPERATOR) {
const GDScriptParser::OperatorNode *assign = static_cast<const GDScriptParser::OperatorNode *>(p_function->default_values[i - def_args]);
if (assign->arguments.size() >= 2) {
if (assign->arguments[1]->type == GDScriptParser::Node::TYPE_CONSTANT) {
const GDScriptParser::ConstantNode *cn = static_cast<const GDScriptParser::ConstantNode *>(assign->arguments[1]);
def_val = cn->value.get_construct_string();
} else if (assign->arguments[1]->type == GDScriptParser::Node::TYPE_IDENTIFIER) {
const GDScriptParser::IdentifierNode *id = static_cast<const GDScriptParser::IdentifierNode *>(assign->arguments[1]);
def_val = id->name.operator String();
}
}
}
arghint += " = " + def_val;
}
if (i == p_arg_idx) {
arghint += String::chr(0xFFFF);
}
}
if (p_function->arguments.size() > 0) {
arghint += " ";
}
arghint += ")";
return arghint;
}
static void _find_enumeration_candidates(const String p_enum_hint, Set<String> &r_result) {
if (p_enum_hint.find(".") == -1) {
// Global constant
StringName current_enum = p_enum_hint;
for (int i = 0; i < GlobalConstants::get_global_constant_count(); i++) {
if (GlobalConstants::get_global_constant_enum(i) == current_enum) {
r_result.insert(GlobalConstants::get_global_constant_name(i));
}
}
} else {
String class_name = p_enum_hint.get_slice(".", 0);
String enum_name = p_enum_hint.get_slice(".", 1);
if (!ClassDB::class_exists(class_name)) {
return;
}
List<StringName> enum_constants;
ClassDB::get_enum_constants(class_name, enum_name, &enum_constants);
for (List<StringName>::Element *E = enum_constants.front(); E; E = E->next()) {
String candidate = class_name + "." + E->get();
r_result.insert(candidate);
}
}
}
static void _find_identifiers_in_block(const GDScriptCompletionContext &p_context, Set<String> &r_result) {
for (Map<StringName, GDScriptParser::LocalVarNode *>::Element *E = p_context.block->variables.front(); E; E = E->next()) {
if (E->get()->line < p_context.line) {
r_result.insert(E->key().operator String());
}
}
if (p_context.block->parent_block) {
GDScriptCompletionContext c = p_context;
c.block = p_context.block->parent_block;
_find_identifiers_in_block(c, r_result);
}
}
static void _find_identifiers_in_base(const GDScriptCompletionContext &p_context, const GDScriptCompletionIdentifier &p_base, bool p_only_functions, Set<String> &r_result);
static void _find_identifiers_in_class(const GDScriptCompletionContext &p_context, bool p_static, bool p_only_functions, bool p_parent_only, Set<String> &r_result) {
if (!p_parent_only) {
if (!p_static && !p_only_functions) {
for (int i = 0; i < p_context._class->variables.size(); i++) {
r_result.insert(p_context._class->variables[i].identifier);
}
}
if (!p_only_functions) {
for (Map<StringName, GDScriptParser::ClassNode::Constant>::Element *E = p_context._class->constant_expressions.front(); E; E = E->next()) {
r_result.insert(E->key());
}
for (int i = 0; i < p_context._class->subclasses.size(); i++) {
r_result.insert(p_context._class->subclasses[i]->name);
}
}
for (int i = 0; i < p_context._class->static_functions.size(); i++) {
if (p_context._class->static_functions[i]->arguments.size()) {
r_result.insert(p_context._class->static_functions[i]->name.operator String() + "(");
} else {
r_result.insert(p_context._class->static_functions[i]->name.operator String() + "()");
}
}
if (!p_static) {
for (int i = 0; i < p_context._class->functions.size(); i++) {
if (p_context._class->functions[i]->arguments.size()) {
r_result.insert(p_context._class->functions[i]->name.operator String() + "(");
} else {
r_result.insert(p_context._class->functions[i]->name.operator String() + "()");
}
}
}
}
// Parents
GDScriptCompletionIdentifier base_type;
base_type.type = p_context._class->base_type;
base_type.type.is_meta_type = p_static;
base_type.value = p_context.base;
GDScriptCompletionContext c = p_context;
c.block = NULL;
c.function = NULL;
_find_identifiers_in_base(c, base_type, p_only_functions, r_result);
}
static void _find_identifiers_in_base(const GDScriptCompletionContext &p_context, const GDScriptCompletionIdentifier &p_base, bool p_only_functions, Set<String> &r_result) {
GDScriptParser::DataType base_type = p_base.type;
bool _static = base_type.is_meta_type;
if (_static && base_type.kind != GDScriptParser::DataType::BUILTIN) {
r_result.insert("new(");
}
while (base_type.has_type) {
switch (base_type.kind) {
case GDScriptParser::DataType::CLASS: {
GDScriptCompletionContext c = p_context;
c._class = base_type.class_type;
c.block = NULL;
c.function = NULL;
_find_identifiers_in_class(c, _static, p_only_functions, false, r_result);
base_type = base_type.class_type->base_type;
} break;
case GDScriptParser::DataType::GDSCRIPT: {
Ref<GDScript> script = base_type.script_type;
if (script.is_valid()) {
if (!_static && !p_only_functions) {
for (const Set<StringName>::Element *E = script->get_members().front(); E; E = E->next()) {
r_result.insert(E->get().operator String());
}
}
if (!p_only_functions) {
for (const Map<StringName, Variant>::Element *E = script->get_constants().front(); E; E = E->next()) {
r_result.insert(E->key().operator String());
}
}
for (const Map<StringName, GDScriptFunction *>::Element *E = script->get_member_functions().front(); E; E = E->next()) {
if (!_static || E->get()->is_static()) {
if (E->get()->get_argument_count()) {
r_result.insert(E->key().operator String() + "(");
} else {
r_result.insert(E->key().operator String() + "()");
}
}
}
if (!p_only_functions) {
for (const Map<StringName, Ref<GDScript> >::Element *E = script->get_subclasses().front(); E; E = E->next()) {
r_result.insert(E->key().operator String());
}
}
base_type = GDScriptParser::DataType();
if (script->get_base().is_valid()) {
base_type.has_type = true;
base_type.kind = GDScriptParser::DataType::GDSCRIPT;
base_type.script_type = script->get_base();
} else {
base_type.has_type = script->get_instance_base_type() != StringName();
base_type.kind = GDScriptParser::DataType::NATIVE;
base_type.script_type = script->get_instance_base_type();
}
} else {
return;
}
} break;
case GDScriptParser::DataType::SCRIPT: {
Ref<Script> scr = base_type.script_type;
if (scr.is_valid()) {
if (!_static && !p_only_functions) {
List<PropertyInfo> members;
scr->get_script_property_list(&members);
for (List<PropertyInfo>::Element *E = members.front(); E; E = E->next()) {
r_result.insert(E->get().name);
}
}
if (!p_only_functions) {
Map<StringName, Variant> constants;
scr->get_constants(&constants);
for (Map<StringName, Variant>::Element *E = constants.front(); E; E = E->next()) {
r_result.insert(E->key().operator String());
}
}
List<MethodInfo> methods;
scr->get_script_method_list(&methods);
for (List<MethodInfo>::Element *E = methods.front(); E; E = E->next()) {
if (E->get().arguments.size()) {
r_result.insert(E->get().name + "(");
} else {
r_result.insert(E->get().name + "()");
}
}
Ref<Script> base_script = scr->get_base_script();
if (base_script.is_valid()) {
base_type.script_type = base_script;
} else {
base_type.kind = GDScriptParser::DataType::NATIVE;
base_type.native_type = scr->get_instance_base_type();
}
} else {
return;
}
} break;
case GDScriptParser::DataType::NATIVE: {
StringName type = base_type.native_type;
if (!ClassDB::class_exists(type)) {
type = String("_") + type;
if (!ClassDB::class_exists(type)) {
return;
}
}
if (!p_only_functions) {
List<String> constants;
ClassDB::get_integer_constant_list(type, &constants);
for (List<String>::Element *E = constants.front(); E; E = E->next()) {
r_result.insert(E->get());
}
if (!_static) {
List<PropertyInfo> pinfo;
ClassDB::get_property_list(type, &pinfo);
for (List<PropertyInfo>::Element *E = pinfo.front(); E; E = E->next()) {
if (E->get().usage & (PROPERTY_USAGE_GROUP | PROPERTY_USAGE_CATEGORY)) {
continue;
}
if (E->get().name.find("/") != -1) {
continue;
}
r_result.insert(E->get().name);
}
}
}
if (!_static) {
List<MethodInfo> methods;
bool is_autocompleting_getters = GLOBAL_GET("debug/gdscript/completion/autocomplete_setters_and_getters").booleanize();
ClassDB::get_method_list(type, &methods, false, !is_autocompleting_getters);
for (List<MethodInfo>::Element *E = methods.front(); E; E = E->next()) {
if (E->get().name.begins_with("_")) {
continue;
}
if (E->get().arguments.size()) {
r_result.insert(E->get().name + "(");
} else {
r_result.insert(E->get().name + "()");
}
}
}
return;
} break;
case GDScriptParser::DataType::BUILTIN: {
Variant::CallError err;
Variant tmp = Variant::construct(base_type.builtin_type, NULL, 0, err);
if (err.error != Variant::CallError::CALL_OK) {
return;
}
if (!p_only_functions) {
List<PropertyInfo> members;
tmp.get_property_list(&members);
for (List<PropertyInfo>::Element *E = members.front(); E; E = E->next()) {
if (String(E->get().name).find("/") == -1) {
r_result.insert(E->get().name);
}
}
}
List<MethodInfo> methods;
tmp.get_method_list(&methods);
for (List<MethodInfo>::Element *E = methods.front(); E; E = E->next()) {
if (E->get().arguments.size()) {
r_result.insert(E->get().name + "(");
} else {
r_result.insert(E->get().name + "()");
}
}
return;
} break;
default: {
return;
} break;
}
}
}
static void _find_identifiers(const GDScriptCompletionContext &p_context, bool p_only_functions, Set<String> &r_result) {
const GDScriptParser::BlockNode *block = p_context.block;
if (p_context.function) {
const GDScriptParser::FunctionNode *f = p_context.function;
for (int i = 0; i < f->arguments.size(); i++) {
r_result.insert(f->arguments[i].operator String());
}
}
if (!p_only_functions && block) {
GDScriptCompletionContext c = p_context;
c.block = block;
_find_identifiers_in_block(c, r_result);
}
const GDScriptParser::ClassNode *clss = p_context._class;
bool _static = !p_context.function || p_context.function->_static;
while (clss) {
GDScriptCompletionContext c = p_context;
c._class = clss;
c.block = NULL;
c.function = NULL;
_find_identifiers_in_class(c, _static, p_only_functions, false, r_result);
_static = true;
clss = clss->owner;
}
for (int i = 0; i < GDScriptFunctions::FUNC_MAX; i++) {
MethodInfo mi = GDScriptFunctions::get_info(GDScriptFunctions::Function(i));
if (mi.arguments.size() || (mi.flags & METHOD_FLAG_VARARG)) {
r_result.insert(String(GDScriptFunctions::get_func_name(GDScriptFunctions::Function(i))) + "(");
} else {
r_result.insert(String(GDScriptFunctions::get_func_name(GDScriptFunctions::Function(i))) + "()");
}
}
static const char *_type_names[Variant::VARIANT_MAX] = {
"null", "bool", "int", "float", "String", "Vector2", "Rect2", "Vector3", "Transform2D", "Plane", "Quat", "AABB", "Basis", "Transform",
"Color", "NodePath", "RID", "Object", "Dictionary", "Array", "PoolByteArray", "PoolIntArray", "PoolRealArray", "PoolStringArray",
"PoolVector2Array", "PoolVector3Array", "PoolColorArray"
};
for (int i = 0; i < Variant::VARIANT_MAX; i++) {
r_result.insert(_type_names[i]);
}
static const char *_keywords[] = {
"and", "in", "not", "or", "false", "PI", "TAU", "INF", "NAN", "self", "true", "as", "assert",
"breakpoint", "class", "extends", "is", "func", "preload", "setget", "signal", "tool", "yield",
"const", "enum", "export", "onready", "static", "var", "break", "continue", "if", "elif",
"else", "for", "pass", "return", "match", "while", "remote", "sync", "master", "puppet", "slave",
"remotesync", "mastersync", "puppetsync",
0
};
const char **kw = _keywords;
while (*kw) {
r_result.insert(*kw);
kw++;
}
// Autoload singletons
List<PropertyInfo> props;
ProjectSettings::get_singleton()->get_property_list(&props);
for (List<PropertyInfo>::Element *E = props.front(); E; E = E->next()) {
String s = E->get().name;
if (!s.begins_with("autoload/")) {
continue;
}
String path = ProjectSettings::get_singleton()->get(s);
if (path.begins_with("*")) {
r_result.insert(s.get_slice("/", 1));
}
}
// Named scripts
List<StringName> named_scripts;
ScriptServer::get_global_class_list(&named_scripts);
for (List<StringName>::Element *E = named_scripts.front(); E; E = E->next()) {
r_result.insert(E->get().operator String());
}
// Native classes
for (const Map<StringName, int>::Element *E = GDScriptLanguage::get_singleton()->get_global_map().front(); E; E = E->next()) {
r_result.insert(E->key().operator String());
}
}
static void _find_call_arguments(const GDScriptCompletionContext &p_context, const GDScriptCompletionIdentifier &p_base, const StringName &p_method, int p_argidx, bool p_static, Set<String> &r_result, String &r_arghint) {
Variant base = p_base.value;
GDScriptParser::DataType base_type = p_base.type;
bool _static = false;
const String quote_style = EDITOR_DEF("text_editor/completion/use_single_quotes", false) ? "'" : "\"";
while (base_type.has_type) {
switch (base_type.kind) {
case GDScriptParser::DataType::CLASS: {
for (int i = 0; i < base_type.class_type->static_functions.size(); i++) {
if (base_type.class_type->static_functions[i]->name == p_method) {
r_arghint = _make_arguments_hint(base_type.class_type->static_functions[i], p_argidx);
return;
}
}
if (!_static) {
for (int i = 0; i < base_type.class_type->functions.size(); i++) {
if (base_type.class_type->functions[i]->name == p_method) {
r_arghint = _make_arguments_hint(base_type.class_type->functions[i], p_argidx);
return;
}
}
}
if ((p_method == "connect" || p_method == "emit_signal") && p_argidx == 0) {
for (int i = 0; i < base_type.class_type->_signals.size(); i++) {
r_result.insert(quote_style + base_type.class_type->_signals[i].name.operator String() + quote_style);
}
}
base_type = base_type.class_type->base_type;
} break;
case GDScriptParser::DataType::GDSCRIPT: {
Ref<GDScript> gds = base_type.script_type;
if (gds.is_valid()) {
if ((p_method == "connect" || p_method == "emit_signal") && p_argidx == 0) {
List<MethodInfo> signals;
gds->get_script_signal_list(&signals);
for (List<MethodInfo>::Element *E = signals.front(); E; E = E->next()) {
r_result.insert(quote_style + E->get().name + quote_style);
}
}
Ref<GDScript> base_script = gds->get_base_script();
if (base_script.is_valid()) {
base_type.script_type = base_script;
} else {
base_type.kind = GDScriptParser::DataType::NATIVE;
base_type.native_type = gds->get_instance_base_type();
}
} else {
return;
}
} break;
case GDScriptParser::DataType::NATIVE: {
StringName class_name = base_type.native_type;
if (!ClassDB::class_exists(class_name)) {
class_name = String("_") + class_name;
if (!ClassDB::class_exists(class_name)) {
base_type.has_type = false;
break;
}
}
List<MethodInfo> methods;
ClassDB::get_method_list(class_name, &methods);
ClassDB::get_virtual_methods(class_name, &methods);
int method_args = 0;
for (List<MethodInfo>::Element *E = methods.front(); E; E = E->next()) {
if (E->get().name == p_method) {
method_args = E->get().arguments.size();
if (base.get_type() == Variant::OBJECT) {
Object *obj = base.operator Object *();
if (obj) {
List<String> options;
obj->get_argument_options(p_method, p_argidx, &options);
for (List<String>::Element *F = options.front(); F; F = F->next()) {
r_result.insert(F->get());
}
}
}
if (p_argidx < method_args) {
PropertyInfo arg_info = E->get().arguments[p_argidx];
if (arg_info.usage & PROPERTY_USAGE_CLASS_IS_ENUM) {
_find_enumeration_candidates(arg_info.class_name, r_result);
}
}
r_arghint = _make_arguments_hint(E->get(), p_argidx);
break;
}
}
if ((p_method == "connect" || p_method == "emit_signal") && p_argidx == 0) {
List<MethodInfo> signals;
ClassDB::get_signal_list(class_name, &signals);
for (List<MethodInfo>::Element *E = signals.front(); E; E = E->next()) {
r_result.insert(quote_style + E->get().name + quote_style);
}
}
if (ClassDB::is_parent_class(class_name, "Node") && (p_method == "get_node" || p_method == "has_node") && p_argidx == 0) {
// Get autoloads
List<PropertyInfo> props;
ProjectSettings::get_singleton()->get_property_list(&props);
for (List<PropertyInfo>::Element *E = props.front(); E; E = E->next()) {
String s = E->get().name;
if (!s.begins_with("autoload/")) {
continue;
}
String name = s.get_slice("/", 1);
r_result.insert(quote_style + "/root/" + name + quote_style);
}
}
if (p_argidx == 0 && method_args > 0 && ClassDB::is_parent_class(class_name, "InputEvent") && p_method.operator String().find("action") != -1) {
// Get input actions
List<PropertyInfo> props;
ProjectSettings::get_singleton()->get_property_list(&props);
for (List<PropertyInfo>::Element *E = props.front(); E; E = E->next()) {
String s = E->get().name;
if (!s.begins_with("input/")) {
continue;
}
String name = s.get_slice("/", 1);
r_result.insert(quote_style + name + quote_style);
}
}
base_type.has_type = false;
} break;
case GDScriptParser::DataType::BUILTIN: {
if (base.get_type() == Variant::NIL) {
Variant::CallError err;
base = Variant::construct(base_type.builtin_type, NULL, 0, err);
if (err.error != Variant::CallError::CALL_OK) {
return;
}
}
List<MethodInfo> methods;
base.get_method_list(&methods);
for (List<MethodInfo>::Element *E = methods.front(); E; E = E->next()) {
if (E->get().name == p_method) {
r_arghint = _make_arguments_hint(E->get(), p_argidx);
return;
}
}
base_type.has_type = false;
} break;
default: {
base_type.has_type = false;
} break;
}
}
}
static void _find_call_arguments(GDScriptCompletionContext &p_context, const GDScriptParser::Node *p_node, int p_argidx, Set<String> &r_result, bool &r_forced, String &r_arghint) {
const String quote_style = EDITOR_DEF("text_editor/completion/use_single_quotes", false) ? "'" : "\"";
if (!p_node || p_node->type != GDScriptParser::Node::TYPE_OPERATOR) {
return;
}
Variant base;
GDScriptParser::DataType base_type;
StringName function;
bool _static = false;
const GDScriptParser::OperatorNode *op = static_cast<const GDScriptParser::OperatorNode *>(p_node);
GDScriptCompletionIdentifier connect_base;
if (op->op != GDScriptParser::OperatorNode::OP_CALL && op->op != GDScriptParser::OperatorNode::OP_PARENT_CALL) {
return;
}
if (!op->arguments.size()) {
return;
}
if (op->op == GDScriptParser::OperatorNode::OP_CALL) {
if (op->arguments[0]->type == GDScriptParser::Node::TYPE_BUILT_IN_FUNCTION) {
// Complete built-in function
const GDScriptParser::BuiltInFunctionNode *fn = static_cast<const GDScriptParser::BuiltInFunctionNode *>(op->arguments[0]);
MethodInfo mi = GDScriptFunctions::get_info(fn->function);
if ((mi.name == "load" || mi.name == "preload") && bool(EditorSettings::get_singleton()->get("text_editor/completion/complete_file_paths"))) {
_get_directory_contents(EditorFileSystem::get_singleton()->get_filesystem(), r_result);
}
r_arghint = _make_arguments_hint(mi, p_argidx);
return;
} else if (op->arguments[0]->type == GDScriptParser::Node::TYPE_TYPE) {
// Complete constructor
const GDScriptParser::TypeNode *tn = static_cast<const GDScriptParser::TypeNode *>(op->arguments[0]);
List<MethodInfo> constructors;
Variant::get_constructor_list(tn->vtype, &constructors);
int i = 0;
for (List<MethodInfo>::Element *E = constructors.front(); E; E = E->next()) {
if (p_argidx >= E->get().arguments.size()) {
continue;
}
if (i > 0) {
r_arghint += "\n";
}
r_arghint += _make_arguments_hint(E->get(), p_argidx);
i++;
}
return;
} else if (op->arguments[0]->type == GDScriptParser::Node::TYPE_SELF) {
if (op->arguments.size() < 2 || op->arguments[1]->type != GDScriptParser::Node::TYPE_IDENTIFIER) {
return;
}
base = p_context.base;
const GDScriptParser::IdentifierNode *id = static_cast<const GDScriptParser::IdentifierNode *>(op->arguments[1]);
function = id->name;
base_type.has_type = true;
base_type.kind = GDScriptParser::DataType::CLASS;
base_type.class_type = const_cast<GDScriptParser::ClassNode *>(p_context._class);
_static = p_context.function && p_context.function->_static;
if (function == "connect" && op->arguments.size() >= 4) {
_guess_expression_type(p_context, op->arguments[3], connect_base);
}
} else {
if (op->arguments.size() < 2 || op->arguments[1]->type != GDScriptParser::Node::TYPE_IDENTIFIER) {
return;
}
const GDScriptParser::IdentifierNode *id = static_cast<const GDScriptParser::IdentifierNode *>(op->arguments[1]);
function = id->name;
GDScriptCompletionIdentifier ci;
if (_guess_expression_type(p_context, op->arguments[0], ci)) {
base_type = ci.type;
base = ci.value;
} else {
return;
}
_static = ci.type.is_meta_type;
if (function == "connect" && op->arguments.size() >= 4) {
_guess_expression_type(p_context, op->arguments[3], connect_base);
}
}
} else {
if (!p_context._class || op->arguments.size() < 1 || op->arguments[0]->type != GDScriptParser::Node::TYPE_IDENTIFIER) {
return;
}
base_type.has_type = true;
base_type.kind = GDScriptParser::DataType::CLASS;
base_type.class_type = const_cast<GDScriptParser::ClassNode *>(p_context._class);
base_type.is_meta_type = p_context.function && p_context.function->_static;
base = p_context.base;
function = static_cast<const GDScriptParser::IdentifierNode *>(op->arguments[0])->name;
if (function == "connect" && op->arguments.size() >= 4) {
_guess_expression_type(p_context, op->arguments[3], connect_base);
}
}
GDScriptCompletionIdentifier ci;
ci.type = base_type;
ci.value = base;
_find_call_arguments(p_context, ci, function, p_argidx, _static, r_result, r_arghint);
if (function == "connect" && p_argidx == 2) {
Set<String> methods;
_find_identifiers_in_base(p_context, connect_base, true, methods);
for (Set<String>::Element *E = methods.front(); E; E = E->next()) {
r_result.insert(quote_style + E->get().replace("(", "").replace(")", "") + quote_style);
}
}
r_forced = r_result.size() > 0;
}
Error GDScriptLanguage::complete_code(const String &p_code, const String &p_base_path, Object *p_owner, List<String> *r_options, bool &r_forced, String &r_call_hint) {
const String quote_style = EDITOR_DEF("text_editor/completion/use_single_quotes", false) ? "'" : "\"";
GDScriptParser parser;
parser.parse(p_code, p_base_path, false, "", true);
r_forced = false;
Set<String> options;
GDScriptCompletionContext context;
context._class = parser.get_completion_class();
context.block = parser.get_completion_block();
context.function = parser.get_completion_function();
context.line = parser.get_completion_line();
if (!context._class || context._class->owner == NULL) {
context.base = p_owner;
context.base_path = p_base_path;
}
bool is_function = false;
switch (parser.get_completion_type()) {
case GDScriptParser::COMPLETION_NONE: {
} break;
case GDScriptParser::COMPLETION_BUILT_IN_TYPE_CONSTANT: {
List<StringName> constants;
Variant::get_constants_for_type(parser.get_completion_built_in_constant(), &constants);
for (List<StringName>::Element *E = constants.front(); E; E = E->next()) {
options.insert(E->get().operator String());
}
} break;
case GDScriptParser::COMPLETION_PARENT_FUNCTION: {
_find_identifiers_in_class(context, !context.function || context.function->_static, true, true, options);
} break;
case GDScriptParser::COMPLETION_FUNCTION: {
is_function = true;
} // fallthrough
case GDScriptParser::COMPLETION_IDENTIFIER: {
_find_identifiers(context, is_function, options);
} break;
case GDScriptParser::COMPLETION_GET_NODE: {
if (p_owner) {
List<String> opts;
p_owner->get_argument_options("get_node", 0, &opts);
for (List<String>::Element *E = opts.front(); E; E = E->next()) {
String opt = E->get().strip_edges();
if (opt.is_quoted()) {
r_forced = true;
String idopt = opt.unquote();
if (idopt.replace("/", "_").is_valid_identifier()) {
options.insert(idopt);
} else {
options.insert(opt);
}
}
}
// Get autoloads
List<PropertyInfo> props;
ProjectSettings::get_singleton()->get_property_list(&props);
for (List<PropertyInfo>::Element *E = props.front(); E; E = E->next()) {
String s = E->get().name;
if (!s.begins_with("autoload/")) {
continue;
}
String name = s.get_slice("/", 1);
options.insert(quote_style + "/root/" + name + quote_style);
}
}
} break;
case GDScriptParser::COMPLETION_METHOD: {
is_function = true;
} // fallthrough
case GDScriptParser::COMPLETION_INDEX: {
const GDScriptParser::Node *node = parser.get_completion_node();
if (node->type != GDScriptParser::Node::TYPE_OPERATOR) {
break;
}
const GDScriptParser::OperatorNode *op = static_cast<const GDScriptParser::OperatorNode *>(node);
if (op->arguments.size() < 1) {
break;
}
GDScriptCompletionIdentifier base;
if (!_guess_expression_type(context, op->arguments[0], base)) {
break;
}
GDScriptCompletionContext c = context;
c.function = NULL;
c.block = NULL;
c.base = base.value.get_type() == Variant::OBJECT ? base.value.operator Object *() : NULL;
if (base.type.kind == GDScriptParser::DataType::CLASS) {
c._class = base.type.class_type;
} else {
c._class = NULL;
}
_find_identifiers_in_base(c, base, is_function, options);
} break;
case GDScriptParser::COMPLETION_CALL_ARGUMENTS: {
_find_call_arguments(context, parser.get_completion_node(), parser.get_completion_argument_index(), options, r_forced, r_call_hint);
} break;
case GDScriptParser::COMPLETION_VIRTUAL_FUNC: {
GDScriptParser::DataType native_type = context._class->base_type;
while (native_type.has_type && native_type.kind != GDScriptParser::DataType::NATIVE) {
switch (native_type.kind) {
case GDScriptParser::DataType::CLASS: {
native_type = native_type.class_type->base_type;
} break;
case GDScriptParser::DataType::GDSCRIPT: {
Ref<GDScript> gds = native_type.script_type;
if (gds.is_valid()) {
Ref<GDScript> base = gds->get_base_script();
if (base.is_valid()) {
native_type.script_type = base;
} else {
native_type.native_type = gds->get_instance_base_type();
native_type.kind = GDScriptParser::DataType::NATIVE;
}
} else {
native_type.has_type = false;
}
} break;
default: {
native_type.has_type = false;
} break;
}
}
if (!native_type.has_type) {
break;
}
StringName class_name = native_type.native_type;
if (!ClassDB::class_exists(class_name)) {
class_name = String("_") + class_name;
if (!ClassDB::class_exists(class_name)) {
break;
}
}
bool use_type_hint = EditorSettings::get_singleton()->get_setting("text_editor/completion/add_type_hints").operator bool();
List<MethodInfo> virtual_methods;
ClassDB::get_virtual_methods(class_name, &virtual_methods);
for (List<MethodInfo>::Element *E = virtual_methods.front(); E; E = E->next()) {
MethodInfo &mi = E->get();
String method_hint = mi.name;
if (method_hint.find(":") != -1) {
method_hint = method_hint.get_slice(":", 0);
}
method_hint += "(";
if (mi.arguments.size()) {
for (int i = 0; i < mi.arguments.size(); i++) {
if (i > 0) {
method_hint += ", ";
}
String arg = mi.arguments[i].name;
if (arg.find(":") != -1) {
arg = arg.substr(0, arg.find(":"));
}
method_hint += arg;
if (use_type_hint && mi.arguments[i].type != Variant::NIL) {
method_hint += ": ";
if (mi.arguments[i].type == Variant::OBJECT && mi.arguments[i].class_name != StringName()) {
method_hint += mi.arguments[i].class_name.operator String();
} else {
method_hint += Variant::get_type_name(mi.arguments[i].type);
}
}
}
}
method_hint += ")";
if (use_type_hint && (mi.return_val.type != Variant::NIL || !(mi.return_val.usage & PROPERTY_USAGE_NIL_IS_VARIANT))) {
method_hint += " -> ";
if (mi.return_val.type == Variant::NIL) {
method_hint += "void";
} else if (mi.return_val.type == Variant::OBJECT && mi.return_val.class_name != StringName()) {
method_hint += mi.return_val.class_name.operator String();
} else {
method_hint += Variant::get_type_name(mi.return_val.type);
}
}
method_hint += ":";
options.insert(method_hint);
}
} break;
case GDScriptParser::COMPLETION_YIELD: {
const GDScriptParser::Node *node = parser.get_completion_node();
GDScriptCompletionContext c = context;
c.line = node->line;
GDScriptCompletionIdentifier type;
if (!_guess_expression_type(c, node, type)) {
break;
}
GDScriptParser::DataType base_type = type.type;
while (base_type.has_type) {
switch (base_type.kind) {
case GDScriptParser::DataType::CLASS: {
for (int i = 0; i < base_type.class_type->_signals.size(); i++) {
options.insert(quote_style + base_type.class_type->_signals[i].name.operator String() + quote_style);
}
base_type = base_type.class_type->base_type;
} break;
case GDScriptParser::DataType::SCRIPT:
case GDScriptParser::DataType::GDSCRIPT: {
Ref<Script> scr = base_type.script_type;
if (scr.is_valid()) {
List<MethodInfo> signals;
scr->get_script_signal_list(&signals);
for (List<MethodInfo>::Element *E = signals.front(); E; E = E->next()) {
options.insert(quote_style + E->get().name + quote_style);
}
Ref<Script> base_script = scr->get_base_script();
if (base_script.is_valid()) {
base_type.script_type = base_script;
} else {
base_type.kind = GDScriptParser::DataType::NATIVE;
base_type.native_type = scr->get_instance_base_type();
}
} else {
base_type.has_type = false;
}
} break;
case GDScriptParser::DataType::NATIVE: {
base_type.has_type = false;
StringName class_name = base_type.native_type;
if (!ClassDB::class_exists(class_name)) {
class_name = String("_") + class_name;
if (!ClassDB::class_exists(class_name)) {
break;
}
}
List<MethodInfo> signals;
ClassDB::get_signal_list(class_name, &signals);
for (List<MethodInfo>::Element *E = signals.front(); E; E = E->next()) {
options.insert(quote_style + E->get().name + quote_style);
}
} break;
default: {
base_type.has_type = false;
}
}
}
} break;
case GDScriptParser::COMPLETION_RESOURCE_PATH: {
if (EditorSettings::get_singleton()->get("text_editor/completion/complete_file_paths")) {
_get_directory_contents(EditorFileSystem::get_singleton()->get_filesystem(), options);
r_forced = true;
}
} break;
case GDScriptParser::COMPLETION_ASSIGN: {
GDScriptCompletionIdentifier type;
if (!_guess_expression_type(context, parser.get_completion_node(), type)) {
break;
}
if (!type.enumeration.empty()) {
_find_enumeration_candidates(type.enumeration, options);
r_forced = options.size() > 0;
}
} break;
case GDScriptParser::COMPLETION_TYPE_HINT: {
const GDScriptParser::ClassNode *clss = context._class;
while (clss) {
for (Map<StringName, GDScriptParser::ClassNode::Constant>::Element *E = clss->constant_expressions.front(); E; E = E->next()) {
GDScriptCompletionIdentifier constant;
GDScriptCompletionContext c = context;
c.function = NULL;
c.block = NULL;
c.line = E->value().expression->line;
if (_guess_expression_type(c, E->value().expression, constant)) {
if (constant.type.has_type && constant.type.is_meta_type) {
options.insert(E->key().operator String());
}
}
}
for (int i = 0; i < clss->subclasses.size(); i++) {
if (clss->subclasses[i]->name != StringName()) {
options.insert(clss->subclasses[i]->name.operator String());
}
}
clss = clss->owner;
for (int i = 0; i < Variant::VARIANT_MAX; i++) {
options.insert(Variant::get_type_name((Variant::Type)i));
}
}
List<StringName> native_classes;
ClassDB::get_class_list(&native_classes);
for (List<StringName>::Element *E = native_classes.front(); E; E = E->next()) {
String class_name = E->get().operator String();
if (class_name.begins_with("_")) {
class_name = class_name.right(1);
}
if (Engine::get_singleton()->has_singleton(class_name)) {
continue;
}
options.insert(class_name);
}
// Named scripts
List<StringName> named_scripts;
ScriptServer::get_global_class_list(&named_scripts);
for (List<StringName>::Element *E = named_scripts.front(); E; E = E->next()) {
options.insert(E->get().operator String());
}
if (parser.get_completion_identifier_is_function()) {
options.insert("void");
}
r_forced = true;
} break;
case GDScriptParser::COMPLETION_TYPE_HINT_INDEX: {
GDScriptCompletionIdentifier base;
String index = parser.get_completion_cursor().operator String();
if (!_guess_identifier_type(context, index.get_slice(".", 0), base)) {
break;
}
GDScriptCompletionContext c = context;
c._class = NULL;
c.function = NULL;
c.block = NULL;
bool finding = true;
index = index.right(index.find(".") + 1);
while (index.find(".") != -1) {
String id = index.get_slice(".", 0);
GDScriptCompletionIdentifier sub_base;
if (!_guess_identifier_type_from_base(c, base, id, sub_base)) {
finding = false;
break;
}
index = index.right(index.find(".") + 1);
base = sub_base;
}
if (!finding) {
break;
}
GDScriptParser::DataType base_type = base.type;
while (base_type.has_type) {
switch (base_type.kind) {
case GDScriptParser::DataType::CLASS: {
if (base_type.class_type) {
for (Map<StringName, GDScriptParser::ClassNode::Constant>::Element *E = base_type.class_type->constant_expressions.front(); E; E = E->next()) {
GDScriptCompletionIdentifier constant;
GDScriptCompletionContext c2 = context;
c2._class = base_type.class_type;
c2.function = NULL;
c2.block = NULL;
c2.line = E->value().expression->line;
if (_guess_expression_type(c2, E->value().expression, constant)) {
if (constant.type.has_type && constant.type.is_meta_type) {
options.insert(E->key().operator String());
}
}
}
for (int i = 0; i < base_type.class_type->subclasses.size(); i++) {
if (base_type.class_type->subclasses[i]->name != StringName()) {
options.insert(base_type.class_type->subclasses[i]->name.operator String());
}
}
base_type = base_type.class_type->base_type;
} else {
base_type.has_type = false;
}
} break;
case GDScriptParser::DataType::SCRIPT:
case GDScriptParser::DataType::GDSCRIPT: {
Ref<Script> scr = base_type.script_type;
if (scr.is_valid()) {
Map<StringName, Variant> constants;
scr->get_constants(&constants);
for (Map<StringName, Variant>::Element *E = constants.front(); E; E = E->next()) {
Ref<Script> const_scr = E->value();
if (const_scr.is_valid()) {
options.insert(E->key().operator String());
}
}
Ref<Script> base_script = scr->get_base_script();
if (base_script.is_valid()) {
base_type.script_type = base_script;
} else {
base_type.has_type = false;
}
} else {
base_type.has_type = false;
}
} break;
default: {
base_type.has_type = false;
} break;
}
}
r_forced = options.size() > 0;
} break;
}
for (Set<String>::Element *E = options.front(); E; E = E->next()) {
r_options->push_back(E->get());
}
return OK;
}
#else
Error GDScriptLanguage::complete_code(const String &p_code, const String &p_base_path, Object *p_owner, List<String> *r_options, bool &r_forced, String &r_call_hint) {
return OK;
}
#endif
//////// END COMPLETION //////////
String GDScriptLanguage::_get_indentation() const {
#ifdef TOOLS_ENABLED
if (Engine::get_singleton()->is_editor_hint()) {
bool use_space_indentation = EDITOR_DEF("text_editor/indent/type", false);
if (use_space_indentation) {
int indent_size = EDITOR_DEF("text_editor/indent/size", 4);
String space_indent = "";
for (int i = 0; i < indent_size; i++) {
space_indent += " ";
}
return space_indent;
}
}
#endif
return "\t";
}
void GDScriptLanguage::auto_indent_code(String &p_code, int p_from_line, int p_to_line) const {
String indent = _get_indentation();
Vector<String> lines = p_code.split("\n");
List<int> indent_stack;
for (int i = 0; i < lines.size(); i++) {
String l = lines[i];
int tc = 0;
for (int j = 0; j < l.length(); j++) {
if (l[j] == ' ' || l[j] == '\t') {
tc++;
} else {
break;
}
}
String st = l.substr(tc, l.length()).strip_edges();
if (st == "" || st.begins_with("#"))
continue; //ignore!
int ilevel = 0;
if (indent_stack.size()) {
ilevel = indent_stack.back()->get();
}
if (tc > ilevel) {
indent_stack.push_back(tc);
} else if (tc < ilevel) {
while (indent_stack.size() && indent_stack.back()->get() > tc) {
indent_stack.pop_back();
}
if (indent_stack.size() && indent_stack.back()->get() != tc)
indent_stack.push_back(tc); //this is not right but gets the job done
}
if (i >= p_from_line) {
l = "";
for (int j = 0; j < indent_stack.size(); j++) {
l += indent;
}
l += st;
} else if (i > p_to_line) {
break;
}
lines.write[i] = l;
}
p_code = "";
for (int i = 0; i < lines.size(); i++) {
if (i > 0)
p_code += "\n";
p_code += lines[i];
}
}
#ifdef TOOLS_ENABLED
static Error _lookup_symbol_from_base(const GDScriptParser::DataType &p_base, const String &p_symbol, bool p_is_function, GDScriptLanguage::LookupResult &r_result) {
GDScriptParser::DataType base_type = p_base;
while (base_type.has_type) {
switch (base_type.kind) {
case GDScriptParser::DataType::CLASS: {
if (base_type.class_type) {
if (p_is_function) {
for (int i = 0; i < base_type.class_type->functions.size(); i++) {
if (base_type.class_type->functions[i]->name == p_symbol) {
r_result.type = ScriptLanguage::LookupResult::RESULT_SCRIPT_LOCATION;
r_result.location = base_type.class_type->functions[i]->line;
return OK;
}
}
for (int i = 0; i < base_type.class_type->static_functions.size(); i++) {
if (base_type.class_type->static_functions[i]->name == p_symbol) {
r_result.type = ScriptLanguage::LookupResult::RESULT_SCRIPT_LOCATION;
r_result.location = base_type.class_type->static_functions[i]->line;
return OK;
}
}
} else {
if (base_type.class_type->constant_expressions.has(p_symbol)) {
r_result.type = ScriptLanguage::LookupResult::RESULT_SCRIPT_LOCATION;
r_result.location = base_type.class_type->constant_expressions[p_symbol].expression->line;
return OK;
}
for (int i = 0; i < base_type.class_type->variables.size(); i++) {
if (base_type.class_type->variables[i].identifier == p_symbol) {
r_result.type = ScriptLanguage::LookupResult::RESULT_SCRIPT_LOCATION;
r_result.location = base_type.class_type->variables[i].line;
return OK;
}
}
}
base_type = base_type.class_type->base_type;
}
} break;
case GDScriptParser::DataType::SCRIPT:
case GDScriptParser::DataType::GDSCRIPT: {
Ref<Script> scr = base_type.script_type;
if (scr.is_valid()) {
int line = scr->get_member_line(p_symbol);
if (line >= 0) {
r_result.type = ScriptLanguage::LookupResult::RESULT_SCRIPT_LOCATION;
r_result.location = line;
r_result.script = scr;
return OK;
}
Ref<Script> base_script = scr->get_base_script();
if (base_script.is_valid()) {
base_type.script_type = base_script;
} else {
base_type.kind = GDScriptParser::DataType::NATIVE;
base_type.native_type = scr->get_instance_base_type();
}
} else {
base_type.has_type = false;
}
} break;
case GDScriptParser::DataType::NATIVE: {
StringName class_name = base_type.native_type;
if (!ClassDB::class_exists(class_name)) {
class_name = String("_") + class_name;
if (!ClassDB::class_exists(class_name)) {
base_type.has_type = false;
break;
}
}
if (ClassDB::has_method(class_name, p_symbol, true)) {
r_result.type = ScriptLanguage::LookupResult::RESULT_CLASS_METHOD;
r_result.class_name = base_type.native_type;
r_result.class_member = p_symbol;
return OK;
}
List<MethodInfo> virtual_methods;
ClassDB::get_virtual_methods(class_name, &virtual_methods, true);
for (List<MethodInfo>::Element *E = virtual_methods.front(); E; E = E->next()) {
if (E->get().name == p_symbol) {
r_result.type = ScriptLanguage::LookupResult::RESULT_CLASS_METHOD;
r_result.class_name = base_type.native_type;
r_result.class_member = p_symbol;
return OK;
}
}
StringName enum_name = ClassDB::get_integer_constant_enum(class_name, p_symbol, true);
if (enum_name != StringName()) {
r_result.type = ScriptLanguage::LookupResult::RESULT_CLASS_ENUM;
r_result.class_name = base_type.native_type;
r_result.class_member = enum_name;
return OK;
}
List<String> constants;
ClassDB::get_integer_constant_list(class_name, &constants, true);
for (List<String>::Element *E = constants.front(); E; E = E->next()) {
if (E->get() == p_symbol) {
r_result.type = ScriptLanguage::LookupResult::RESULT_CLASS_CONSTANT;
r_result.class_name = base_type.native_type;
r_result.class_member = p_symbol;
return OK;
}
}
List<PropertyInfo> properties;
ClassDB::get_property_list(class_name, &properties, true);
for (List<PropertyInfo>::Element *E = properties.front(); E; E = E->next()) {
if (E->get().name == p_symbol) {
r_result.type = ScriptLanguage::LookupResult::RESULT_CLASS_PROPERTY;
r_result.class_name = base_type.native_type;
r_result.class_member = p_symbol;
return OK;
}
}
StringName parent = ClassDB::get_parent_class(class_name);
if (parent != StringName()) {
if (String(parent).begins_with("_")) {
base_type.native_type = String(parent).right(1);
} else {
base_type.native_type = parent;
}
} else {
base_type.has_type = false;
}
} break;
case GDScriptParser::DataType::BUILTIN: {
base_type.has_type = false;
if (Variant::has_constant(base_type.builtin_type, p_symbol)) {
r_result.type = ScriptLanguage::LookupResult::RESULT_CLASS_CONSTANT;
r_result.class_name = Variant::get_type_name(base_type.builtin_type);
r_result.class_member = p_symbol;
return OK;
}
Variant v;
REF v_ref;
if (base_type.builtin_type == Variant::OBJECT) {
v_ref.instance();
v = v_ref;
} else {
Variant::CallError err;
v = Variant::construct(base_type.builtin_type, NULL, 0, err);
if (err.error != Variant::CallError::CALL_OK) {
break;
}
}
if (v.has_method(p_symbol)) {
r_result.type = ScriptLanguage::LookupResult::RESULT_CLASS_METHOD;
r_result.class_name = Variant::get_type_name(base_type.builtin_type);
r_result.class_member = p_symbol;
return OK;
}
bool valid = false;
v.get(p_symbol, &valid);
if (valid) {
r_result.type = ScriptLanguage::LookupResult::RESULT_CLASS_PROPERTY;
r_result.class_name = Variant::get_type_name(base_type.builtin_type);
r_result.class_member = p_symbol;
return OK;
}
} break;
default: {
base_type.has_type = false;
} break;
}
}
return ERR_CANT_RESOLVE;
}
Error GDScriptLanguage::lookup_code(const String &p_code, const String &p_symbol, const String &p_base_path, Object *p_owner, LookupResult &r_result) {
//before parsing, try the usual stuff
if (ClassDB::class_exists(p_symbol)) {
r_result.type = ScriptLanguage::LookupResult::RESULT_CLASS;
r_result.class_name = p_symbol;
return OK;
} else {
String under_prefix = "_" + p_symbol;
if (ClassDB::class_exists(under_prefix)) {
r_result.type = ScriptLanguage::LookupResult::RESULT_CLASS;
r_result.class_name = p_symbol;
return OK;
}
}
for (int i = 0; i < Variant::VARIANT_MAX; i++) {
Variant::Type t = Variant::Type(i);
if (Variant::get_type_name(t) == p_symbol) {
r_result.type = ScriptLanguage::LookupResult::RESULT_CLASS;
r_result.class_name = Variant::get_type_name(t);
return OK;
}
}
for (int i = 0; i < GDScriptFunctions::FUNC_MAX; i++) {
if (GDScriptFunctions::get_func_name(GDScriptFunctions::Function(i)) == p_symbol) {
r_result.type = ScriptLanguage::LookupResult::RESULT_CLASS_METHOD;
r_result.class_name = "@GDScript";
r_result.class_member = p_symbol;
return OK;
}
}
if ("PI" == p_symbol || "TAU" == p_symbol || "INF" == p_symbol || "NAN" == p_symbol) {
r_result.type = ScriptLanguage::LookupResult::RESULT_CLASS_CONSTANT;
r_result.class_name = "@GDScript";
r_result.class_member = p_symbol;
return OK;
}
GDScriptParser parser;
parser.parse(p_code, p_base_path, false, "", true);
if (parser.get_completion_type() == GDScriptParser::COMPLETION_NONE) {
return ERR_CANT_RESOLVE;
}
GDScriptCompletionContext context;
context._class = parser.get_completion_class();
context.function = parser.get_completion_function();
context.block = parser.get_completion_block();
context.line = parser.get_completion_line();
context.base = p_owner;
context.base_path = p_base_path;
if (context._class && context._class->extends_class.size() > 0) {
bool success = false;
ClassDB::get_integer_constant(context._class->extends_class[0], p_symbol, &success);
if (success) {
r_result.type = ScriptLanguage::LookupResult::RESULT_CLASS_CONSTANT;
r_result.class_name = context._class->extends_class[0];
r_result.class_member = p_symbol;
return OK;
}
}
bool is_function = false;
switch (parser.get_completion_type()) {
case GDScriptParser::COMPLETION_BUILT_IN_TYPE_CONSTANT: {
r_result.type = ScriptLanguage::LookupResult::RESULT_CLASS_CONSTANT;
r_result.class_name = Variant::get_type_name(parser.get_completion_built_in_constant());
r_result.class_member = p_symbol;
return OK;
} break;
case GDScriptParser::COMPLETION_PARENT_FUNCTION:
case GDScriptParser::COMPLETION_FUNCTION: {
is_function = true;
} // fallthrough
case GDScriptParser::COMPLETION_IDENTIFIER: {
if (!is_function) {
is_function = parser.get_completion_identifier_is_function();
}
GDScriptParser::DataType base_type;
if (context._class) {
if (parser.get_completion_type() != GDScriptParser::COMPLETION_PARENT_FUNCTION) {
base_type.has_type = true;
base_type.kind = GDScriptParser::DataType::CLASS;
base_type.class_type = const_cast<GDScriptParser::ClassNode *>(context._class);
} else {
base_type = context._class->base_type;
}
} else {
break;
}
if (!is_function && context.block) {
// Lookup local variables
const GDScriptParser::BlockNode *block = context.block;
while (block) {
if (block->variables.has(p_symbol)) {
r_result.type = ScriptLanguage::LookupResult::RESULT_SCRIPT_LOCATION;
r_result.location = block->variables[p_symbol]->line;
return OK;
}
block = block->parent_block;
}
}
if (context.function && context.function->name != StringName()) {
// Lookup function arguments
for (int i = 0; i < context.function->arguments.size(); i++) {
if (context.function->arguments[i] == p_symbol) {
r_result.type = ScriptLanguage::LookupResult::RESULT_SCRIPT_LOCATION;
r_result.location = context.function->line;
return OK;
}
}
}
if (_lookup_symbol_from_base(base_type, p_symbol, is_function, r_result) == OK) {
return OK;
}
if (!is_function) {
// Guess in autoloads as singletons
List<PropertyInfo> props;
ProjectSettings::get_singleton()->get_property_list(&props);
for (List<PropertyInfo>::Element *E = props.front(); E; E = E->next()) {
String s = E->get().name;
if (!s.begins_with("autoload/"))
continue;
String name = s.get_slice("/", 1);
if (name == String(p_symbol)) {
String path = ProjectSettings::get_singleton()->get(s);
if (path.begins_with("*")) {
String script = path.substr(1, path.length());
if (!script.ends_with(".gd")) {
// Not a script, try find the script anyway,
// may have some success
script = script.get_basename() + ".gd";
}
if (FileAccess::exists(script)) {
r_result.type = ScriptLanguage::LookupResult::RESULT_SCRIPT_LOCATION;
r_result.location = 0;
r_result.script = ResourceLoader::load(script);
return OK;
}
}
}
}
// Global
Map<StringName, int> classes = GDScriptLanguage::get_singleton()->get_global_map();
if (classes.has(p_symbol)) {
Variant value = GDScriptLanguage::get_singleton()->get_global_array()[classes[p_symbol]];
if (value.get_type() == Variant::OBJECT) {
Object *obj = value;
if (obj) {
if (Object::cast_to<GDScriptNativeClass>(obj)) {
r_result.type = ScriptLanguage::LookupResult::RESULT_CLASS;
r_result.class_name = Object::cast_to<GDScriptNativeClass>(obj)->get_name();
} else {
r_result.type = ScriptLanguage::LookupResult::RESULT_CLASS;
r_result.class_name = obj->get_class();
}
// proxy class remove the underscore.
if (r_result.class_name.begins_with("_")) {
r_result.class_name = r_result.class_name.right(1);
}
return OK;
}
} else {
/*
// Because get_integer_constant_enum and get_integer_constant don't work on @GlobalScope
// We cannot determine the exact nature of the identifier here
// Otherwise these codes would work
StringName enumName = ClassDB::get_integer_constant_enum("@GlobalScope", p_symbol, true);
if (enumName != NULL) {
r_result.type = ScriptLanguage::LookupResult::RESULT_CLASS_ENUM;
r_result.class_name = "@GlobalScope";
r_result.class_member = enumName;
return OK;
}
else {
r_result.type = ScriptLanguage::LookupResult::RESULT_CLASS_CONSTANT;
r_result.class_name = "@GlobalScope";
r_result.class_member = p_symbol;
return OK;
}*/
r_result.type = ScriptLanguage::LookupResult::RESULT_CLASS_TBD_GLOBALSCOPE;
r_result.class_name = "@GlobalScope";
r_result.class_member = p_symbol;
return OK;
}
}
}
} break;
case GDScriptParser::COMPLETION_METHOD: {
is_function = true;
} // fallthrough
case GDScriptParser::COMPLETION_INDEX: {
const GDScriptParser::Node *node = parser.get_completion_node();
if (node->type != GDScriptParser::Node::TYPE_OPERATOR) {
break;
}
GDScriptCompletionIdentifier base;
if (!_guess_expression_type(context, static_cast<const GDScriptParser::OperatorNode *>(node)->arguments[0], base)) {
break;
}
if (_lookup_symbol_from_base(base.type, p_symbol, is_function, r_result) == OK) {
return OK;
}
} break;
case GDScriptParser::COMPLETION_VIRTUAL_FUNC: {
GDScriptParser::DataType base_type = context._class->base_type;
if (_lookup_symbol_from_base(base_type, p_symbol, true, r_result) == OK) {
return OK;
}
} break;
default: {
}
}
return ERR_CANT_RESOLVE;
}
#endif
|