1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181 4182 4183 4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207 4208 4209 4210 4211 4212 4213 4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228 4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254 4255 4256 4257 4258 4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292 4293 4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304 4305 4306 4307 4308 4309 4310 4311 4312 4313 4314 4315 4316 4317 4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331 4332 4333 4334 4335 4336 4337 4338 4339 4340 4341 4342 4343 4344 4345 4346 4347 4348 4349 4350 4351 4352 4353 4354 4355 4356 4357 4358 4359 4360 4361 4362 4363 4364 4365 4366 4367 4368 4369 4370 4371 4372 4373 4374 4375 4376 4377 4378 4379 4380 4381 4382 4383 4384 4385 4386 4387 4388 4389 4390 4391 4392 4393 4394 4395 4396 4397 4398 4399 4400 4401 4402 4403 4404 4405 4406 4407 4408 4409 4410 4411 4412 4413 4414 4415 4416 4417 4418 4419 4420 4421 4422 4423 4424 4425 4426 4427 4428 4429 4430 4431 4432 4433 4434 4435 4436 4437 4438 4439 4440 4441 4442 4443 4444 4445 4446 4447 4448 4449 4450 4451 4452 4453 4454 4455 4456 4457 4458 4459 4460 4461 4462 4463 4464 4465 4466 4467 4468 4469 4470 4471 4472 4473 4474 4475 4476 4477 4478 4479 4480 4481 4482 4483 4484 4485 4486 4487 4488 4489 4490 4491 4492 4493 4494 4495 4496 4497 4498 4499 4500 4501 4502 4503 4504 4505 4506 4507 4508 4509 4510 4511 4512 4513 4514 4515 4516 4517 4518 4519 4520 4521 4522 4523 4524 4525 4526 4527 4528 4529 4530 4531 4532 4533 4534 4535 4536 4537 4538 4539 4540 4541 4542 4543 4544 4545 4546 4547 4548 4549 4550 4551 4552 4553 4554 4555 4556 4557 4558 4559 4560 4561 4562 4563 4564 4565 4566 4567 4568 4569 4570 4571 4572 4573 4574 4575 4576 4577 4578 4579 4580 4581 4582 4583 4584 4585 4586 4587 4588 4589 4590 4591 4592 4593 4594 4595 4596 4597 4598 4599 4600 4601 4602 4603 4604 4605 4606 4607 4608 4609 4610 4611 4612 4613 4614 4615 4616 4617 4618 4619 4620 4621 4622 4623 4624 4625 4626 4627 4628 4629 4630 4631 4632 4633 4634 4635 4636 4637 4638 4639 4640 4641 4642 4643 4644 4645 4646 4647 4648 4649 4650 4651 4652 4653 4654 4655 4656 4657 4658 4659 4660 4661 4662 4663 4664 4665 4666 4667 4668 4669 4670 4671 4672 4673 4674 4675 4676 4677 4678 4679 4680 4681 4682 4683 4684 4685 4686 4687 4688 4689 4690 4691 4692 4693 4694 4695 4696 4697 4698 4699 4700 4701 4702 4703 4704 4705 4706 4707 4708 4709 4710 4711 4712 4713 4714 4715 4716 4717 4718 4719 4720 4721 4722 4723 4724 4725 4726 4727 4728 4729 4730 4731 4732 4733 4734 4735 4736 4737 4738 4739 4740 4741 4742 4743 4744 4745 4746 4747 4748 4749 4750 4751 4752 4753 4754 4755 4756 4757 4758 4759 4760 4761 4762 4763 4764 4765 4766 4767 4768 4769 4770 4771 4772 4773 4774 4775 4776 4777 4778 4779 4780 4781 4782 4783 4784 4785 4786 4787 4788 4789 4790 4791 4792 4793 4794 4795 4796 4797 4798 4799 4800 4801 4802 4803 4804 4805 4806 4807 4808 4809 4810 4811 4812 4813 4814 4815 4816 4817 4818 4819 4820 4821 4822 4823 4824 4825 4826 4827 4828 4829 4830 4831 4832 4833 4834 4835 4836 4837 4838 4839 4840 4841 4842 4843 4844 4845 4846 4847 4848 4849 4850 4851 4852 4853 4854 4855 4856 4857 4858 4859 4860 4861 4862 4863 4864 4865 4866 4867 4868 4869 4870 4871 4872 4873 4874 4875 4876 4877 4878 4879 4880 4881 4882 4883 4884 4885 4886 4887 4888 4889 4890 4891 4892 4893 4894 4895 4896 4897 4898 4899 4900 4901 4902 4903 4904 4905 4906 4907 4908 4909 4910 4911 4912 4913 4914 4915 4916 4917 4918 4919 4920 4921 4922 4923 4924 4925 4926 4927 4928 4929 4930 4931 4932 4933 4934 4935 4936 4937 4938 4939 4940 4941 4942 4943 4944 4945 4946 4947 4948 4949 4950 4951 4952 4953 4954 4955 4956 4957 4958 4959 4960 4961 4962 4963 4964 4965 4966 4967 4968 4969 4970 4971 4972 4973 4974 4975 4976 4977 4978 4979 4980 4981 4982 4983 4984 4985 4986 4987 4988 4989 4990 4991 4992 4993 4994 4995 4996 4997 4998 4999 5000 5001 5002 5003 5004 5005 5006 5007 5008 5009 5010 5011 5012 5013 5014 5015 5016 5017 5018 5019 5020 5021 5022 5023 5024 5025 5026 5027 5028 5029 5030 5031 5032 5033 5034 5035 5036 5037 5038 5039 5040 5041 5042 5043 5044 5045 5046 5047 5048 5049 5050 5051 5052 5053 5054 5055 5056 5057 5058 5059 5060 5061 5062 5063 5064 5065 5066 5067 5068 5069 5070 5071 5072 5073 5074 5075 5076 5077 5078 5079 5080 5081 5082 5083 5084 5085 5086 5087 5088 5089 5090 5091 5092 5093 5094 5095 5096 5097 5098 5099 5100 5101 5102 5103 5104 5105 5106 5107 5108 5109 5110 5111 5112 5113 5114 5115 5116 5117 5118 5119 5120 5121 5122 5123 5124 5125 5126 5127 5128 5129 5130 5131 5132 5133 5134 5135 5136 5137 5138 5139 5140 5141 5142 5143 5144 5145 5146 5147 5148 5149 5150 5151 5152 5153 5154 5155 5156 5157 5158 5159 5160 5161 5162 5163 5164 5165 5166 5167 5168 5169 5170 5171 5172 5173 5174 5175 5176 5177 5178 5179 5180 5181 5182 5183 5184 5185 5186 5187 5188 5189 5190 5191 5192 5193 5194 5195 5196 5197 5198 5199 5200 5201 5202 5203 5204 5205 5206 5207 5208 5209 5210 5211 5212 5213 5214 5215 5216 5217 5218 5219 5220 5221 5222 5223 5224 5225 5226 5227 5228 5229 5230 5231 5232 5233 5234 5235 5236 5237 5238 5239 5240 5241 5242 5243 5244 5245 5246 5247 5248 5249 5250 5251 5252 5253 5254 5255 5256 5257 5258 5259 5260 5261 5262 5263 5264 5265 5266 5267 5268 5269 5270 5271 5272 5273 5274 5275 5276 5277 5278 5279 5280 5281 5282 5283 5284 5285 5286 5287 5288 5289 5290 5291 5292 5293 5294 5295 5296 5297 5298 5299 5300 5301 5302 5303 5304 5305 5306 5307 5308 5309 5310 5311 5312 5313 5314 5315 5316 5317 5318 5319 5320 5321 5322 5323 5324 5325 5326 5327 5328 5329 5330 5331 5332 5333 5334 5335 5336 5337 5338 5339 5340 5341 5342 5343 5344 5345 5346 5347 5348 5349 5350 5351 5352 5353 5354 5355 5356 5357 5358 5359 5360 5361 5362 5363 5364 5365 5366 5367 5368 5369 5370 5371 5372 5373 5374 5375 5376 5377 5378 5379 5380 5381 5382 5383 5384 5385 5386 5387 5388 5389 5390 5391 5392 5393 5394 5395 5396 5397 5398 5399 5400 5401 5402 5403 5404 5405 5406 5407 5408 5409 5410 5411 5412 5413 5414 5415 5416 5417 5418 5419 5420 5421 5422 5423 5424 5425 5426 5427 5428 5429 5430 5431 5432 5433 5434 5435 5436 5437 5438 5439 5440 5441 5442 5443 5444 5445 5446 5447 5448 5449 5450 5451 5452 5453 5454 5455 5456 5457 5458 5459 5460 5461 5462 5463 5464 5465 5466 5467 5468 5469 5470 5471 5472 5473 5474 5475 5476 5477 5478 5479 5480 5481 5482 5483 5484 5485 5486 5487 5488 5489 5490 5491 5492 5493 5494 5495 5496 5497 5498 5499 5500 5501 5502 5503 5504 5505 5506 5507 5508 5509 5510 5511 5512 5513 5514 5515 5516 5517 5518 5519 5520 5521 5522 5523 5524 5525 5526 5527 5528 5529 5530 5531 5532 5533 5534 5535 5536 5537 5538 5539 5540 5541 5542 5543 5544 5545 5546 5547 5548 5549 5550 5551 5552 5553 5554 5555 5556 5557 5558 5559 5560 5561 5562 5563 5564 5565 5566 5567 5568 5569 5570 5571 5572 5573 5574 5575 5576 5577 5578 5579 5580 5581 5582 5583 5584 5585 5586 5587 5588 5589 5590 5591 5592 5593 5594 5595 5596 5597 5598 5599 5600 5601 5602 5603 5604 5605 5606 5607 5608 5609 5610 5611 5612 5613 5614 5615 5616 5617 5618 5619 5620 5621 5622 5623 5624 5625 5626 5627 5628 5629 5630 5631 5632 5633 5634 5635 5636 5637 5638 5639 5640 5641 5642 5643 5644 5645 5646 5647 5648 5649 5650 5651 5652 5653 5654 5655 5656 5657 5658 5659 5660 5661 5662 5663 5664 5665 5666 5667 5668 5669 5670 5671 5672 5673 5674 5675 5676 5677 5678 5679 5680 5681 5682 5683 5684 5685 5686 5687 5688 5689 5690 5691 5692 5693 5694 5695 5696 5697 5698 5699 5700 5701 5702 5703 5704 5705 5706 5707 5708 5709 5710 5711 5712 5713 5714 5715 5716 5717 5718 5719 5720 5721 5722 5723 5724 5725 5726 5727 5728 5729 5730 5731 5732 5733 5734 5735 5736 5737 5738 5739 5740 5741 5742 5743 5744 5745 5746 5747 5748 5749 5750 5751 5752 5753 5754 5755 5756 5757 5758 5759 5760 5761 5762 5763 5764 5765 5766 5767 5768 5769 5770 5771 5772 5773 5774 5775 5776 5777 5778 5779 5780 5781 5782 5783 5784 5785 5786 5787 5788 5789 5790 5791 5792 5793 5794 5795 5796 5797 5798 5799 5800 5801 5802 5803 5804 5805 5806 5807 5808 5809 5810 5811 5812 5813 5814 5815 5816 5817 5818 5819 5820 5821 5822 5823 5824 5825 5826 5827 5828 5829 5830 5831 5832 5833 5834 5835 5836 5837 5838 5839 5840 5841 5842 5843 5844 5845 5846 5847 5848 5849 5850 5851 5852 5853 5854 5855 5856 5857 5858 5859 5860 5861 5862 5863 5864 5865 5866 5867 5868 5869 5870 5871 5872 5873 5874 5875 5876 5877 5878 5879 5880 5881 5882 5883 5884 5885 5886 5887 5888 5889 5890 5891 5892 5893 5894 5895 5896 5897 5898 5899 5900 5901 5902 5903 5904 5905 5906 5907 5908 5909 5910 5911 5912 5913 5914 5915 5916 5917 5918 5919 5920 5921 5922 5923 5924 5925 5926 5927 5928 5929 5930 5931 5932 5933 5934 5935 5936 5937 5938 5939 5940 5941 5942 5943 5944 5945 5946 5947 5948 5949 5950 5951 5952 5953 5954 5955 5956 5957 5958 5959 5960 5961 5962 5963 5964 5965 5966 5967 5968 5969 5970 5971 5972 5973 5974 5975 5976 5977 5978 5979 5980 5981 5982 5983 5984 5985 5986 5987 5988 5989 5990 5991 5992 5993 5994 5995 5996 5997 5998 5999 6000 6001 6002 6003 6004 6005 6006 6007 6008 6009 6010 6011 6012 6013 6014 6015 6016 6017 6018 6019 6020 6021 6022 6023 6024 6025 6026 6027 6028 6029 6030 6031 6032 6033 6034 6035 6036 6037 6038 6039 6040 6041 6042 6043 6044 6045 6046 6047 6048 6049 6050 6051 6052 6053 6054 6055 6056 6057 6058 6059 6060 6061 6062 6063 6064 6065 6066 6067 6068 6069 6070 6071 6072 6073 6074 6075 6076 6077 6078 6079 6080 6081 6082 6083 6084 6085 6086 6087 6088 6089 6090 6091 6092 6093 6094 6095 6096 6097 6098 6099 6100 6101 6102 6103 6104 6105 6106 6107 6108 6109 6110 6111 6112 6113 6114 6115 6116 6117 6118 6119 6120 6121 6122 6123 6124 6125 6126 6127 6128 6129 6130 6131 6132 6133 6134 6135 6136 6137 6138 6139 6140 6141 6142 6143 6144 6145 6146 6147 6148 6149 6150 6151 6152 6153 6154 6155 6156 6157 6158 6159 6160 6161 6162 6163 6164 6165 6166 6167 6168 6169 6170 6171 6172 6173 6174 6175 6176 6177 6178 6179 6180 6181 6182 6183 6184 6185 6186 6187 6188 6189 6190 6191 6192 6193 6194 6195 6196 6197 6198 6199 6200 6201 6202 6203 6204 6205 6206 6207 6208 6209 6210 6211 6212 6213 6214 6215 6216 6217 6218 6219 6220 6221 6222 6223 6224 6225 6226 6227 6228 6229 6230 6231 6232 6233 6234 6235 6236 6237 6238 6239 6240 6241 6242 6243 6244 6245 6246 6247 6248 6249 6250 6251 6252 6253 6254 6255 6256 6257 6258 6259 6260 6261 6262 6263 6264 6265 6266 6267 6268 6269 6270 6271 6272 6273 6274 6275 6276 6277 6278 6279 6280 6281 6282 6283 6284 6285 6286 6287 6288 6289 6290 6291 6292 6293 6294 6295 6296 6297 6298 6299 6300 6301 6302 6303 6304 6305 6306 6307 6308 6309 6310 6311 6312 6313 6314 6315 6316 6317 6318 6319 6320 6321 6322 6323 6324 6325 6326 6327 6328 6329 6330 6331 6332 6333 6334 6335 6336 6337 6338 6339 6340 6341 6342 6343 6344 6345 6346 6347 6348 6349 6350 6351 6352 6353 6354 6355 6356 6357 6358 6359 6360 6361 6362 6363 6364 6365 6366 6367 6368 6369 6370 6371 6372 6373 6374 6375 6376 6377 6378 6379 6380 6381 6382 6383 6384 6385 6386 6387 6388 6389 6390 6391 6392 6393 6394 6395 6396 6397 6398 6399 6400 6401 6402 6403 6404 6405 6406 6407 6408 6409 6410 6411 6412 6413 6414 6415 6416 6417 6418 6419 6420 6421 6422 6423 6424 6425 6426 6427 6428 6429 6430 6431 6432 6433 6434 6435 6436 6437 6438 6439 6440 6441 6442 6443 6444 6445 6446 6447 6448 6449 6450 6451 6452 6453 6454 6455 6456 6457 6458 6459 6460 6461 6462 6463 6464 6465 6466 6467 6468 6469 6470 6471 6472 6473 6474 6475 6476 6477 6478 6479 6480 6481 6482 6483 6484 6485 6486 6487 6488 6489 6490 6491 6492 6493 6494 6495 6496 6497 6498 6499 6500 6501 6502 6503 6504 6505 6506 6507 6508 6509 6510 6511 6512 6513 6514 6515 6516 6517 6518 6519 6520 6521 6522 6523 6524 6525 6526 6527 6528 6529 6530 6531 6532 6533 6534 6535 6536 6537 6538 6539 6540 6541 6542 6543 6544 6545 6546 6547 6548 6549 6550 6551 6552 6553 6554 6555 6556 6557 6558 6559 6560 6561 6562 6563 6564 6565 6566 6567 6568 6569 6570 6571 6572 6573 6574 6575 6576 6577 6578 6579 6580 6581 6582 6583 6584 6585 6586 6587 6588 6589 6590 6591 6592 6593 6594 6595 6596 6597 6598 6599 6600 6601 6602 6603 6604 6605 6606 6607
|
{
***************************************************************************
* *
* This source is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This code is distributed in the hope that it will be useful, but *
* WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
* General Public License for more details. *
* *
* A copy of the GNU General Public License is available on the World *
* Wide Web at <http://www.gnu.org/copyleft/gpl.html>. You can also *
* obtain it by writing to the Free Software Foundation, *
* Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1335, USA. *
* *
***************************************************************************
Author: Mattias Gaertner
Abstract:
TCodeToolManager gathers all tools in one single Object
to easily use the code tools in a program.
}
unit CodeToolManager;
{$ifdef fpc}{$mode objfpc}{$endif}{$H+}
interface
{$I codetools.inc}
{.$DEFINE CTDEBUG}
{ $DEFINE DoNotHandleFindDeclException}
uses
{$IFDEF MEM_CHECK}
MemCheck,
{$ENDIF}
Classes, SysUtils, contnrs, TypInfo, types, Laz_AVL_Tree,
// LazUtils
LazFileUtils, LazFileCache, LazMethodList, LazDbgLog, AvgLvlTree,
LazStringUtils,
// Codetools
FileProcs, BasicCodeTools, CodeToolsStrConsts,
EventCodeTool, CodeTree, CodeAtom, SourceChanger, DefineTemplates, CodeCache,
ExprEval, LinkScanner, KeywordFuncLists, FindOverloads, CodeBeautifier,
FindDeclarationCache, DirectoryCacher,
PPUCodeTools, LFMTrees, DirectivesTree, CodeCompletionTemplater,
PascalParserTool, CodeToolsConfig, CustomCodeTool, FindDeclarationTool,
IdentCompletionTool, StdCodeTools, ResourceCodeTool, CodeToolsStructs,
CTUnitGraph, ExtractProcTool;
type
TCodeToolManager = class;
TCodeTool = TEventsCodeTool;
TDirectivesTool = TCompilerDirectivesTree;
TOnBeforeApplyCTChanges = procedure(Manager: TCodeToolManager;
var Abort: boolean) of object;
TOnAfterApplyCTChanges = procedure(Manager: TCodeToolManager) of object;
TOnGatherExternalChanges = procedure(Manager: TCodeToolManager;
var Abort: boolean) of object;
TOnSearchUsedUnit = function(const SrcFilename: string;
const TheUnitName, TheUnitInFilename: string
): TCodeBuffer of object;
TOnCodeToolCheckAbort = function: boolean of object;
TOnFindDefineProperty = procedure(Sender: TObject;
const PersistentClassName, AncestorClassName, Identifier: string;
var IsDefined: boolean) of object;
TOnFindFPCMangledSource = procedure(Sender: TObject; SrcType: TCodeTreeNodeDesc;
const SrcName: string; out SrcFilename: string) of object;
{ ECodeToolManagerError }
ECodeToolManagerError = class(Exception)
public
Id: int64;
constructor Create(TheID: int64; const Msg: string);
constructor CreateFmt(TheID: int64; const Msg: string; const Args: array of const);
end;
TCodeToolManagerHandler = (
ctmOnToolTreeChanging
);
TCodeToolManagerHandlers = set of TCodeToolManagerHandler;
TOnToolTreeChanging = TCodeTreeChangeEvent;
TOnScannerInit = procedure(Self: TCodeToolManager; Scanner: TLinkScanner) of object;
{ TCodeToolManager }
TCodeToolManager = class(TPersistent)
private
FAbortable: boolean;
FAddInheritedCodeToOverrideMethod: boolean;
FAdjustTopLineDueToComment: boolean;
FCatchExceptions: boolean;
FChangeStep: integer;
FCheckFilesOnDisk: boolean;
FCodeCompletionTemplateFileName: String;
FCodeNodeTreeChangeStep: integer;
FCompleteProperties: boolean;
FCurCodeTool: TCodeTool; // current codetool
FCurDirectivesTool: TDirectivesTool;
FCursorBeyondEOL: boolean;
FDirectivesTools: TAVLTree; // tree of TDirectivesTool sorted for Code (TCodeBuffer)
FErrorCode: TCodeBuffer;
FErrorColumn: integer;
FErrorId: int64;
FErrorLine: integer;
FErrorMsg: string;
FErrorTopLine: integer;
FCodeTreeNodesDeletedStep: integer;
FIndentSize: integer;
FJumpSingleLinePos: integer;
FJumpCodeBlockPos: integer;
FIdentifierListUpdating: boolean;
FOnAfterApplyChanges: TOnAfterApplyCTChanges;
FOnBeforeApplyChanges: TOnBeforeApplyCTChanges;
FOnCheckAbort: TOnCodeToolCheckAbort;
FOnFindFPCMangledSource: TOnFindFPCMangledSource;
FOnGatherExternalChanges: TOnGatherExternalChanges;
FOnFindDefinePropertyForContext: TOnFindDefinePropertyForContext;
FOnFindDefineProperty: TOnFindDefineProperty;
FOnGatherUserIdentifiers: TOnGatherUserIdentifiers;
FOnGetIndenterExamples: TOnGetFABExamples;
FOnGetMethodName: TOnGetMethodname;
FOnRescanFPCDirectoryCache: TNotifyEvent;
FOnScannerInit: TOnScannerInit;
FOnSearchUsedUnit: TOnSearchUsedUnit;
FResourceTool: TResourceCodeTool;
FSetPropertyVariablename: string;
FSetPropertyVariableIsPrefix: Boolean;
FSetPropertyVariableUseConst: Boolean;
FSourceExtensions: string; // default is '.pp;.pas;.lpr;.dpr;.dpk'
FPascalTools: TAVLTree; // tree of TCustomCodeTool sorted TCustomCodeTool(Data).Scanner.MainCode
FTabWidth: integer;
FUseTabs: boolean;
FVisibleEditorLines: integer;
FWriteExceptions: boolean;
FWriteLockCount: integer;// Set/Unset counter
FWriteLockStep: integer; // current write lock ID
FHandlers: array[TCodeToolManagerHandler] of TMethodList;
FErrorDbgMsg: string;
procedure DoOnGatherUserIdentifiers(Sender: TIdentCompletionTool;
const ContextFlags: TIdentifierListContextFlags);
procedure DoOnRescanFPCDirectoryCache(Sender: TObject);
function GetBeautifier: TBeautifyCodeOptions; inline;
function DoOnScannerGetInitValues(Scanner: TLinkScanner; Code: Pointer;
out AChangeStep: integer): TExpressionEvaluator;
procedure DoOnDefineTreeReadValue(Sender: TObject; const VariableName: string;
var Value: string; var Handled: boolean);
procedure DoOnGlobalValuesChanged;
function DoOnFindUsedUnit(SrcTool: TFindDeclarationTool; const TheUnitName,
TheUnitInFilename: string): TCodeBuffer;
function DoOnGetSrcPathForCompiledUnit(Sender: TObject;
const AFilename: string): string;
function DoOnInternalGetMethodName(const AMethod: TMethod;
CheckOwner: TObject): string;
function FindCodeOfMainUnitHint(Code: TCodeBuffer): TCodeBuffer;
procedure CreateScanner(Code: TCodeBuffer);
procedure SetAbortable(const AValue: boolean);
procedure SetAddInheritedCodeToOverrideMethod(const AValue: boolean);
procedure SetCheckFilesOnDisk(NewValue: boolean);
procedure SetCodeCompletionTemplateFileName(AValue: String);
procedure SetCompleteProperties(const AValue: boolean);
procedure SetIndentSize(NewValue: integer);
procedure SetSetPropertyVariableIsPrefix(aValue: Boolean);
procedure SetSetPropertyVariablename(AValue: string);
procedure SetSetPropertyVariableUseConst(aValue: Boolean);
procedure SetTabWidth(const AValue: integer);
procedure SetUseTabs(AValue: boolean);
procedure SetVisibleEditorLines(NewValue: integer);
procedure SetJumpSingleLinePos(NewValue: integer);
procedure SetJumpCodeBlockPos(NewValue: integer);
procedure SetCursorBeyondEOL(NewValue: boolean);
procedure BeforeApplyingChanges(var Abort: boolean);
procedure AfterApplyingChanges;
procedure AdjustErrorTopLine;
procedure WriteError;
procedure DoOnFABGetNestedComments(Sender: TObject; Code: TCodeBuffer; out
NestedComments: boolean);
procedure DoOnFABGetExamples(Sender: TObject; Code: TCodeBuffer;
Step: integer; var CodeBuffers: TFPList; var ExpandedFilenames: TStrings);
procedure DoOnLoadFileForTool(Sender: TObject; const ExpandedFilename: string;
out Code: TCodeBuffer; var {%H-}Abort: boolean);
function DoOnGetCodeToolForBuffer(Sender: TObject;
Code: TCodeBuffer; GoToMainCode: boolean): TFindDeclarationTool;
function DoOnGetDirectoryCache(const ADirectory: string): TCTDirectoryCache;
procedure DoOnToolSetWriteLock(Lock: boolean);
procedure DoOnToolGetChangeSteps(out SourcesChangeStep, FilesChangeStep: int64;
out InitValuesChangeStep: integer);
function DoOnParserProgress({%H-}Tool: TCustomCodeTool): boolean;
procedure DoOnToolTreeChange(Tool: TCustomCodeTool; NodesDeleting: boolean);
function DoOnScannerProgress(Sender: TLinkScanner): boolean;
function GetResourceTool: TResourceCodeTool;
function GetOwnerForCodeTreeNode(ANode: TCodeTreeNode): TObject;
function DirectoryCachePoolGetString(const ADirectory: string;
const AStringType: TCTDirCacheString): string;
function DirectoryCachePoolFindVirtualFile(const Filename: string): string;
function DirectoryCachePoolGetUnitFromSet(const UnitSet, AnUnitName: string;
SrcSearchRequiresPPU: boolean): string;
function DirectoryCachePoolGetCompiledUnitFromSet(
const UnitSet, AnUnitName: string): string;
procedure DirectoryCachePoolIterateFPCUnitsFromSet(const UnitSet: string;
const Iterate: TCTOnIterateFile);
procedure AddHandler(HandlerType: TCodeToolManagerHandler; const Handler: TMethod);
procedure RemoveHandler(HandlerType: TCodeToolManagerHandler; const Handler: TMethod);
public
DefinePool: TDefinePool; // definition templates (rules)
DefineTree: TDefineTree; // cache for defines (e.g. initial compiler values)
SourceCache: TCodeCache; // cache for source (units, include files, ...)
SourceChangeCache: TSourceChangeCache; // cache for write accesses
PPUCache: TPPUTools;
GlobalValues: TExpressionEvaluator;
DirectoryCachePool: TCTDirectoryCachePool;
CompilerDefinesCache: TCompilerDefinesCache;
IdentifierList: TIdentifierList;
IdentifierHistory: TIdentifierHistoryList;
Positions: TCodeXYPositions;
Indenter: TFullyAutomaticBeautifier;
property FPCDefinesCache: TCompilerDefinesCache read CompilerDefinesCache; deprecated 'use CompilerDefinesCache'; // 1.9
property Beautifier: TBeautifyCodeOptions read GetBeautifier;
constructor Create;
destructor Destroy; override;
procedure Init(Config: TCodeToolsOptions);
procedure SimpleInit(const ConfigFilename: string);
procedure ActivateWriteLock;
procedure DeactivateWriteLock;
property ChangeStep: integer read FChangeStep; // code changes
procedure IncreaseChangeStep;
property CodeNodeTreeChangeStep: integer read FCodeNodeTreeChangeStep;// nodes altered, added, deleted
property CodeTreeNodesDeletedStep: integer read FCodeTreeNodesDeletedStep;// nodes deleted
procedure GetCodeTreeNodesDeletedStep(out NodesDeletedStep: integer);// use this for events
procedure AddHandlerToolTreeChanging(const OnToolTreeChanging: TOnToolTreeChanging);
procedure RemoveHandlerToolTreeChanging(const OnToolTreeChanging: TOnToolTreeChanging);
// file handling
property SourceExtensions: string
read FSourceExtensions write FSourceExtensions;
function FindFile(const ExpandedFilename: string): TCodeBuffer;
function LoadFile(const ExpandedFilename: string;
UpdateFromDisk, Revert: boolean): TCodeBuffer;
function CreateFile(const AFilename: string): TCodeBuffer;
function CreateTempFile(const AFilename: string): TCodeBuffer;
procedure ReleaseTempFile(Buffer: TCodeBuffer);
function SaveBufferAs(OldBuffer: TCodeBuffer; const ExpandedFilename: string;
out NewBuffer: TCodeBuffer): boolean;
function FilenameHasSourceExt(const AFilename: string): boolean;
function GetMainCode(Code: TCodeBuffer): TCodeBuffer;
function GetIncludeCodeChain(Code: TCodeBuffer;
RemoveFirstCodesWithoutTool: boolean;
out ListOfCodeBuffer: TFPList): boolean;
property OnSearchUsedUnit: TOnSearchUsedUnit
read FOnSearchUsedUnit write FOnSearchUsedUnit;
property OnRescanFPCDirectoryCache: TNotifyEvent read FOnRescanFPCDirectoryCache write FOnRescanFPCDirectoryCache;
// initializing single scanner
property OnScannerInit: TOnScannerInit read FOnScannerInit write FOnScannerInit;
// initializing single codetool
function GetCodeToolForSource(Code: TCodeBuffer;
GoToMainCode, ExceptionOnError: boolean): TCustomCodeTool;
function FindCodeToolForSource(Code: TCodeBuffer): TCustomCodeTool;
property CurCodeTool: TCodeTool read FCurCodeTool;
procedure ClearCurCodeTool;
function InitCurCodeTool(Code: TCodeBuffer): boolean;
function InitResourceTool: boolean;
procedure ClearPositions;
// initializing single compilerdirectivestree
function GetDirectivesToolForSource(Code: TCodeBuffer;
ExceptionOnError: boolean): TCompilerDirectivesTree;
property CurDirectivesTool: TDirectivesTool read FCurDirectivesTool;
procedure ClearCurDirectivesTool;
function InitCurDirectivesTool(Code: TCodeBuffer): boolean;
function FindDirectivesToolForSource(Code: TCodeBuffer): TDirectivesTool;
// exception handling
procedure ClearError;
function HandleException(AnException: Exception): boolean;
procedure SetError(Id: int64; Code: TCodeBuffer; Line, Column: integer;
const TheMessage: string);
property CatchExceptions: boolean
read FCatchExceptions write FCatchExceptions;
property WriteExceptions: boolean
read FWriteExceptions write FWriteExceptions;
property ErrorCode: TCodeBuffer read fErrorCode;
property ErrorColumn: integer read fErrorColumn;
property ErrorLine: integer read fErrorLine;
property ErrorMessage: string read fErrorMsg;
property ErrorId: int64 read FErrorId;
property ErrorTopLine: integer read fErrorTopLine;
property ErrorDbgMsg: string read FErrorDbgMsg;
property Abortable: boolean read FAbortable write SetAbortable;
property OnCheckAbort: TOnCodeToolCheckAbort
read FOnCheckAbort write FOnCheckAbort;
// tool settings
property AdjustTopLineDueToComment: boolean read FAdjustTopLineDueToComment
write FAdjustTopLineDueToComment;
property CheckFilesOnDisk: boolean read FCheckFilesOnDisk
write SetCheckFilesOnDisk;
property CursorBeyondEOL: boolean read FCursorBeyondEOL
write SetCursorBeyondEOL;
property IndentSize: integer read FIndentSize write SetIndentSize;
property JumpSingleLinePos: integer read FJumpSingleLinePos write SetJumpSingleLinePos;
property JumpCodeBlockPos: integer read FJumpCodeBlockPos write SetJumpCodeBlockPos;
property SetPropertyVariablename: string
read FSetPropertyVariablename write SetSetPropertyVariablename;
property SetPropertyVariableIsPrefix: Boolean
read FSetPropertyVariableIsPrefix write SetSetPropertyVariableIsPrefix;
property SetPropertyVariableUseConst: Boolean
read FSetPropertyVariableUseConst write SetSetPropertyVariableUseConst;
property VisibleEditorLines: integer
read FVisibleEditorLines write SetVisibleEditorLines;
property TabWidth: integer read FTabWidth write SetTabWidth;
property UseTabs: boolean read FUseTabs write SetUseTabs;
property CompleteProperties: boolean
read FCompleteProperties write SetCompleteProperties;
property AddInheritedCodeToOverrideMethod: boolean
read FAddInheritedCodeToOverrideMethod
write SetAddInheritedCodeToOverrideMethod;
// code completion templates
property CodeCompletionTemplateFileName : String read FCodeCompletionTemplateFileName
write SetCodeCompletionTemplateFileName;
// source changing
procedure BeginUpdate;
function EndUpdate: boolean;
function GatherExternalChanges: boolean;
property OnGatherExternalChanges: TOnGatherExternalChanges
read FOnGatherExternalChanges write FOnGatherExternalChanges;
function ApplyChanges: boolean;
property OnBeforeApplyChanges: TOnBeforeApplyCTChanges
read FOnBeforeApplyChanges write FOnBeforeApplyChanges;
property OnAfterApplyChanges: TOnAfterApplyCTChanges
read FOnAfterApplyChanges write FOnAfterApplyChanges;
// defines
function SetGlobalValue(const VariableName, VariableValue: string): boolean;
function GetUnitPathForDirectory(const Directory: string;
UseCache: boolean = true): string;
function GetIncludePathForDirectory(const Directory: string;
UseCache: boolean = true): string;
function GetSrcPathForDirectory(const Directory: string;
UseCache: boolean = true): string;
function GetCompleteSrcPathForDirectory(const Directory: string;
UseCache: boolean = true): string;
function GetPPUSrcPathForDirectory(const Directory: string): string;
function GetDCUSrcPathForDirectory(const Directory: string): string;
function GetCompiledSrcPathForDirectory(const Directory: string;
{%H-}UseCache: boolean = true): string;
function GetNestedCommentsFlagForFile(const Filename: string): boolean;
function GetPascalCompilerForDirectory(const Directory: string): TPascalCompiler;
function GetCompilerModeForDirectory(const Directory: string): TCompilerMode;
function GetCompiledSrcExtForDirectory(const {%H-}Directory: string): string;
function FindUnitInUnitLinks(const Directory, AUnitName: string): string;
function GetUnitLinksForDirectory(const Directory: string;
UseCache: boolean = false): string;
function FindUnitInUnitSet(const Directory, AUnitName: string): string;
function GetUnitSetIDForDirectory(const Directory: string;
UseCache: boolean = true): string;
function GetUnitSetForDirectory(const Directory: string): TFPCUnitSetCache;
function GetFPCUnitPathForDirectory(const Directory: string;
UseCache: boolean = true): string;// value of macro #FPCUnitPath
procedure GetFPCVersionForDirectory(const Directory: string;
out FPCVersion, FPCRelease, FPCPatch: integer);
function GetPCVersionForDirectory(const Directory: string): integer;
function GetNamespacesForDirectory(const Directory: string;
UseCache: boolean = true): string;// value of macro #Namespaces
// miscellaneous
property OnGetMethodName: TOnGetMethodname read FOnGetMethodName
write FOnGetMethodName;
property OnGetIndenterExamples: TOnGetFABExamples
read FOnGetIndenterExamples write FOnGetIndenterExamples;
property OnGatherUserIdentifiers: TOnGatherUserIdentifiers
read FOnGatherUserIdentifiers write FOnGatherUserIdentifiers;
// data function
procedure FreeListOfPCodeXYPosition(var List: TFPList);
procedure FreeTreeOfPCodeXYPosition(var Tree: TAVLTree);
function CreateTreeOfPCodeXYPosition: TAVLTree;
procedure AddListToTreeOfPCodeXYPosition(SrcList: TFPList;
DestTree: TAVLTree; ClearList, CreateCopies: boolean);
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// code exploring
function Explore(Code: TCodeBuffer; out ACodeTool: TCodeTool;
WithStatements: boolean; OnlyInterface: boolean = false): boolean;
function CheckSyntax(Code: TCodeBuffer; out NewCode: TCodeBuffer;
out NewX, NewY, NewTopLine: integer; out ErrorMsg: string): boolean;
function ExploreDirectives(Code: TCodeBuffer;
out ADirectivesTool: TDirectivesTool): boolean;
function ExploreUnitDirectives(Code: TCodeBuffer;
out aScanner: TLinkScanner): boolean;
// compiler directives
function GuessMisplacedIfdefEndif(Code: TCodeBuffer; X,Y: integer;
out NewCode: TCodeBuffer;
out NewX, NewY, NewTopLine: integer): boolean;
// find include directive of include file at position X,Y
function FindEnclosingIncludeDirective(Code: TCodeBuffer; X,Y: integer;
out NewCode: TCodeBuffer;
out NewX, NewY, NewTopLine: integer): boolean;
function FindResourceDirective(Code: TCodeBuffer; StartX, StartY: integer;
out NewCode: TCodeBuffer; out NewX, NewY, NewTopLine: integer;
const Filename: string = ''; SearchInCleanSrc: boolean = true): boolean;
function AddResourceDirective(Code: TCodeBuffer; const Filename: string;
SearchInCleanSrc: boolean = true; const NewSrc: string = ''): boolean;
function FindIncludeDirective(Code: TCodeBuffer; StartX, StartY: integer;
out NewCode: TCodeBuffer; out NewX, NewY, NewTopLine: integer;
const Filename: string = ''; SearchInCleanSrc: boolean = true): boolean;
function AddIncludeDirectiveForInit(Code: TCodeBuffer; const Filename: string;
const NewSrc: string = ''): boolean;
function AddUnitWarnDirective(Code: TCodeBuffer; WarnID, Comment: string;
TurnOn: boolean): boolean;
function RemoveDirective(Code: TCodeBuffer; NewX, NewY: integer;
RemoveEmptyIFs: boolean): boolean;
function FixIncludeFilenames(Code: TCodeBuffer; Recursive: boolean;
out MissingIncludeFilesCodeXYPos: TFPList): boolean;
function FixMissingH2PasDirectives(Code: TCodeBuffer;
var Changed: boolean): boolean;
function ReduceCompilerDirectives(Code: TCodeBuffer;
Undefines, Defines: TStrings; var Changed: boolean): boolean;
// keywords and comments
function IsKeyword(Code: TCodeBuffer; const KeyWord: string): boolean;
function ExtractCodeWithoutComments(Code: TCodeBuffer;
KeepDirectives: boolean = false;
KeepVerbosityDirectives: boolean = false): string;
function GetPasDocComments(Code: TCodeBuffer; X, Y: integer;
out ListOfPCodeXYPosition: TFPList): boolean;
// blocks (e.g. begin..end, case..end, try..finally..end, repeat..until)
function FindBlockCounterPart(Code: TCodeBuffer; X,Y: integer;
out NewCode: TCodeBuffer;
out NewX, NewY, NewTopLine: integer): boolean;
function FindBlockStart(Code: TCodeBuffer; X,Y: integer;
out NewCode: TCodeBuffer;
out NewX, NewY, NewTopLine: integer;
SkipStart: boolean = false): boolean;
function GuessUnclosedBlock(Code: TCodeBuffer; X,Y: integer;
out NewCode: TCodeBuffer;
out NewX, NewY, NewTopLine: integer): boolean;
function CompleteBlock(Code: TCodeBuffer; X,Y: integer;
OnlyIfCursorBlockIndented: boolean): boolean;
function CompleteBlock(Code: TCodeBuffer; X,Y: integer;
OnlyIfCursorBlockIndented: boolean;
out NewCode: TCodeBuffer;
out NewX, NewY, NewTopLine: integer): boolean;
// method jumping
function JumpToMethod(Code: TCodeBuffer; X,Y: integer;
out NewCode: TCodeBuffer;
out NewX, NewY, NewTopLine, BlockTopLine, BlockBottomLine: integer;
out RevertableJump: boolean): boolean;
function FindProcDeclaration(Code: TCodeBuffer; CleanDef: string;
out Tool: TCodeTool; out Node: TCodeTreeNode;
Attr: TProcHeadAttributes = [phpWithoutSemicolon]): boolean;
// find declaration
function FindDeclaration(Code: TCodeBuffer; X,Y: integer;
out NewCode: TCodeBuffer;
out NewX, NewY, NewTopLine, BlockTopLine, BlockBottomLine: integer;
Flags: TFindSmartFlags = DefaultFindSmartFlags): boolean;
function FindDeclarationOfIdentifier(Code: TCodeBuffer; X,Y: integer;
Identifier: PChar;
out NewCode: TCodeBuffer;
out NewX, NewY, NewTopLine: integer): boolean;
function FindSmartHint(Code: TCodeBuffer; X,Y: integer;
Flags: TFindSmartFlags = DefaultFindSmartHintFlags): string;
function FindDeclarationInInterface(Code: TCodeBuffer;
const Identifier: string; out NewCode: TCodeBuffer;
out NewX, NewY, NewTopLine: integer): boolean;
function FindDeclarationInInterface(Code: TCodeBuffer;
const Identifier: string; out NewCode: TCodeBuffer;
out NewX, NewY, NewTopLine, BlockTopLine, BlockBottomLine: integer): boolean;
function FindDeclarationWithMainUsesSection(Code: TCodeBuffer;
const Identifier: string;
out NewCode: TCodeBuffer;
out NewX, NewY, NewTopLine: integer): Boolean;
function FindDeclarationAndOverload(Code: TCodeBuffer; X,Y: integer;
out ListOfPCodeXYPosition: TFPList;
Flags: TFindDeclarationListFlags): boolean;
function FindMainDeclaration(Code: TCodeBuffer; X,Y: integer;
out NewCode: TCodeBuffer;
out NewX, NewY, NewTopLine: integer): boolean;
function FindDeclarationOfPropertyPath(Code: TCodeBuffer;
const PropertyPath: string; out NewCode: TCodeBuffer;
out NewX, NewY, NewTopLine: integer): Boolean;
function FindFileAtCursor(Code: TCodeBuffer; X,Y: integer;
out Found: TFindFileAtCursorFlag; out FoundFilename: string;
Allowed: TFindFileAtCursorFlags = DefaultFindFileAtCursorAllowed;
StartPos: PCodeXYPosition = nil): boolean;
// get code context (aka parameter hints)
function FindCodeContext(Code: TCodeBuffer; X,Y: integer;
out CodeContexts: TCodeContextInfo): boolean;
function ExtractProcedureHeader(Code: TCodeBuffer; X,Y: integer;
Attributes: TProcHeadAttributes; out ProcHead: string): boolean;
function HasInterfaceRegisterProc(Code: TCodeBuffer;
out HasRegisterProc: boolean): boolean;
// gather identifiers (i.e. all visible)
function GatherUnitNames(Code: TCodeBuffer): Boolean;
function GatherIdentifiers(Code: TCodeBuffer; X,Y: integer): boolean;
function GetIdentifierAt(Code: TCodeBuffer; X,Y: integer;
out Identifier: string): boolean;
function IdentItemCheckHasChilds(IdentItem: TIdentifierListItem): boolean;
function FindAbstractMethods(Code: TCodeBuffer; X,Y: integer;
out ListOfPCodeXYPosition: TFPList;
SkipAbstractsInStartClass: boolean = false): boolean;
function GetValuesOfCaseVariable(Code: TCodeBuffer; X,Y: integer;
List: TStrings; WithTypeDefIfScoped: boolean = true): boolean;
function GatherOverloads(Code: TCodeBuffer; X,Y: integer;
out Graph: TDeclarationOverloadsGraph): boolean;
// find references, rename identifier, remove identifier
function FindReferences(IdentifierCode: TCodeBuffer;
X, Y: integer; SearchInCode: TCodeBuffer; SkipComments: boolean;
var ListOfPCodeXYPosition: TFPList;
var Cache: TFindIdentifierReferenceCache // you must free Cache
): boolean;
function FindUnitReferences(UnitCode, TargetCode: TCodeBuffer;
SkipComments: boolean; var ListOfPCodeXYPosition: TFPList): boolean;
function FindUsedUnitReferences(Code: TCodeBuffer; X, Y: integer;
SkipComments: boolean; out UsedUnitFilename: string;
var ListOfPCodeXYPosition: TFPList): boolean;
function RenameIdentifier(TreeOfPCodeXYPosition: TAVLTree;
const OldIdentifier, NewIdentifier: string;
DeclarationCode: TCodeBuffer = nil; DeclarationCaretXY: PPoint = nil): boolean;
function ReplaceWord(Code: TCodeBuffer; const OldWord, NewWord: string;
ChangeStrings: boolean): boolean;
function RemoveIdentifierDefinition(Code: TCodeBuffer; X, Y: integer
): boolean; // e.g. remove the variable definition at X,Y
function RemoveWithBlock(Code: TCodeBuffer; X, Y: integer): boolean;
function AddWithBlock(Code: TCodeBuffer; X1, Y1, X2, Y2: integer;
const WithExpr: string; // if empty: collect Candidates
Candidates: TStrings): boolean;
function ChangeParamList(Code: TCodeBuffer;
Changes: TObjectList; // list of TChangeParamListItem
var ProcPos: TCodeXYPosition; // if it is in this unit the proc declaration is changed and this position is cleared
TreeOfPCodeXYPosition: TAVLTree // positions in this unit are processed and removed from the tree
): boolean;
// resourcestring sections
function GatherResourceStringSections(
Code: TCodeBuffer; X,Y: integer;
CodePositions: TCodeXYPositions): boolean;
function IdentifierExistsInResourceStringSection(Code: TCodeBuffer;
X,Y: integer; const ResStrIdentifier: string): boolean;
function CreateIdentifierFromStringConst(
StartCode: TCodeBuffer; StartX, StartY: integer;
EndCode: TCodeBuffer; EndX, EndY: integer;
out Identifier: string; MaxLen: integer): boolean;
function StringConstToFormatString(
StartCode: TCodeBuffer; StartX, StartY: integer;
EndCode: TCodeBuffer; EndX, EndY: integer;
out FormatStringConstant, FormatParameters: string;
out StartInStringConst, EndInStringConst: boolean): boolean;
function GatherResourceStringsWithValue(SectionCode: TCodeBuffer;
SectionX, SectionY: integer; const StringValue: string;
CodePositions: TCodeXYPositions): boolean;
function AddResourcestring(CursorCode: TCodeBuffer; X,Y: integer;
SectionCode: TCodeBuffer; SectionX, SectionY: integer;
const NewIdentifier, NewValue: string;
InsertPolicy: TResourcestringInsertPolicy): boolean;
// expressions
function GetStringConstBounds(Code: TCodeBuffer; X,Y: integer;
out StartCode: TCodeBuffer; out StartX, StartY: integer;
out EndCode: TCodeBuffer; out EndX, EndY: integer;
ResolveComments: boolean): boolean;
procedure ImproveStringConstantStart(const ACode: string; var StartPos: integer);
procedure ImproveStringConstantEnd(const ACode: string; var EndPos: integer);
function ExtractOperand(Code: TCodeBuffer; X,Y: integer;
out Operand: string; WithPostTokens, WithAsOperator,
WithoutTrailingPoints: boolean): boolean;
function GetExpandedOperand(Code: TCodeBuffer; X, Y: Integer;
out Operand: string; ResolveProperty: Boolean): Boolean;
// code completion = auto class completion, auto forward proc completion,
// (local) var assignment completion, event assignment completion
function CompleteCode(Code: TCodeBuffer; X,Y,TopLine: integer;
out NewCode: TCodeBuffer;
out NewX, NewY, NewTopLine, BlockTopLine, BlockBottomLine: integer;Interactive: Boolean): boolean;
function CreateVariableForIdentifier(Code: TCodeBuffer; X,Y,TopLine: integer;
out NewCode: TCodeBuffer;
out NewX, NewY, NewTopLine: integer; Interactive: Boolean): boolean;
function AddMethods(Code: TCodeBuffer; X,Y, TopLine: integer;
ListOfPCodeXYPosition: TFPList;
const VirtualToOverride: boolean;
out NewCode: TCodeBuffer;
out NewX, NewY, NewTopLine, BlockTopLine, BlockBottomLine: integer): boolean;
function GuessTypeOfIdentifier(Code: TCodeBuffer; X,Y: integer;
out ItsAKeyword, IsSubIdentifier: boolean;
out ExistingDefinition: TFindContext; // next existing definition
out ListOfPFindContext: TFPList; // possible classes
out NewExprType: TExpressionType; out NewType: string): boolean; // false = not at an identifier or syntax error
function GetPossibleInitsForVariable(Code: TCodeBuffer; X,Y: integer;
out Statements: TStrings; out InsertPositions: TObjectList // e.g. [use unit1, unit2;]i:=0;
): boolean;
function DeclareVariableNearBy(Code: TCodeBuffer; X,Y: integer;
const VariableName, NewType, NewUnitName: string;
Visibility: TCodeTreeNodeDesc;
LvlPosCode: TCodeBuffer = nil; LvlPosX: integer = 0; LvlPosY: integer = 0
): boolean;
function DeclareVariableAt(Code: TCodeBuffer; X,Y: integer;
const VariableName, NewType, NewUnitName: string): boolean;
// simplifications
function FindRedefinitions(Code: TCodeBuffer;
out TreeOfCodeTreeNodeExt: TAVLTree; WithEnums: boolean): boolean;
function RemoveRedefinitions(Code: TCodeBuffer;
TreeOfCodeTreeNodeExt: TAVLTree): boolean;
function RemoveAllRedefinitions(Code: TCodeBuffer): boolean;
function FindAliasDefinitions(Code: TCodeBuffer;
out TreeOfCodeTreeNodeExt: TAVLTree; OnlyWrongType: boolean): boolean;
function FixAliasDefinitions(Code: TCodeBuffer;
TreeOfCodeTreeNodeExt: TAVLTree): boolean;
function FixAllAliasDefinitions(Code: TCodeBuffer): boolean;
function FindConstFunctions(Code: TCodeBuffer;
out TreeOfCodeTreeNodeExt: TAVLTree): boolean;
function ReplaceConstFunctions(Code: TCodeBuffer;
TreeOfCodeTreeNodeExt: TAVLTree): boolean;
function ReplaceAllConstFunctions(Code: TCodeBuffer): boolean;
function FindTypeCastFunctions(Code: TCodeBuffer;
out TreeOfCodeTreeNodeExt: TAVLTree): boolean;
function ReplaceTypeCastFunctions(Code: TCodeBuffer;
TreeOfCodeTreeNodeExt: TAVLTree): boolean;
function ReplaceAllTypeCastFunctions(Code: TCodeBuffer): boolean;
function FixForwardDefinitions(Code: TCodeBuffer): boolean;
function FindEmptyMethods(Code: TCodeBuffer;
const AClassName: string; // can be ''
X,Y: integer;
const Sections: TPascalClassSections;
ListOfPCodeXYPosition: TFPList;
out AllEmpty: boolean): boolean;
function RemoveEmptyMethods(Code: TCodeBuffer;
const AClassName: string; X,Y: integer;
const Sections: TPascalClassSections;
out AllRemoved: boolean;
const Attr: TProcHeadAttributes;
out RemovedProcHeads: TStrings): boolean;
// custom class completion
function InitClassCompletion(Code: TCodeBuffer;
const AClassName: string; out CodeTool: TCodeTool): boolean;
// insert/replace
function InsertStatements(InsertPos: TInsertStatementPosDescription;
const Statements: string): boolean;
// alter proc
function AddProcModifier(Code: TCodeBuffer; X, Y: integer;
const aModifier: string): boolean;
// extract proc (creates a new procedure from code in selection)
function CheckExtractProc(Code: TCodeBuffer;
const StartPoint, EndPoint: TPoint;
out MethodPossible, SubProcPossible, SubProcSameLvlPossible: boolean;
out MissingIdentifiers: TAVLTree; // tree of PCodeXYPosition
VarTree: TAVLTree = nil // tree of TExtractedProcVariable
): boolean;
function ExtractProc(Code: TCodeBuffer; const StartPoint, EndPoint: TPoint;
ProcType: TExtractProcType; const ProcName: string;
IgnoreIdentifiers: TAVLTree; // tree of PCodeXYPosition
var NewCode: TCodeBuffer; var NewX, NewY, NewTopLine, BlockTopLine, BlockBottomLine: integer;
FunctionResultVariableStartPos: integer = 0
): boolean;
// 'Assign' method
function FindAssignMethod(Code: TCodeBuffer; X, Y: integer;
out Tool: TCodeTool; out ClassNode: TCodeTreeNode;
out AssignDeclNode: TCodeTreeNode;
var MemberNodeExts: TAVLTree; // tree of TCodeTreeNodeExtension, Node=var or property, Data=write property
out AssignBodyNode: TCodeTreeNode;
out InheritedDeclContext: TFindContext;
ProcName: string = '' // default: Assign
): boolean;
// source name e.g. 'unit AUnitName;'
function GetSourceName(Code: TCodeBuffer; SearchMainCode: boolean): string;
function GetCachedSourceName(Code: TCodeBuffer): string;
function RenameSource(Code: TCodeBuffer; const NewName: string): boolean;
function GetSourceType(Code: TCodeBuffer; SearchMainCode: boolean): string;
// uses sections
function FindUnitInAllUsesSections(Code: TCodeBuffer;
const AnUnitName: string; out NamePos, InPos: integer;
const IgnoreMissingIncludeFiles: Boolean = False): boolean;
function RenameUsedUnit(Code: TCodeBuffer;
const OldUnitName, NewUnitName, NewUnitInFile: string): boolean;
function ReplaceUsedUnits(Code: TCodeBuffer;
UnitNamePairs: TStringToStringTree): boolean;
function AddUnitToMainUsesSection(Code: TCodeBuffer;
const NewUnitName, NewUnitInFile: string;
AsLast: boolean = false; CheckSpecialUnits: boolean = true): boolean;
function AddUnitToMainUsesSectionIfNeeded(Code: TCodeBuffer;
const NewUnitName, NewUnitInFile: string;
AsLast: boolean = false; CheckSpecialUnits: boolean = true): boolean;
function AddUnitToImplementationUsesSection(Code: TCodeBuffer;
const NewUnitName, NewUnitInFile: string;
AsLast: boolean = false; CheckSpecialUnits: boolean = true): boolean;
function RemoveUnitFromAllUsesSections(Code: TCodeBuffer;
const AnUnitName: string): boolean;
function FindUsedUnitFiles(Code: TCodeBuffer; var MainUsesSection: TStrings
): boolean; // only main uses section, if unit not found, returns "unitname" or "unitname in 'filename'"
function FindUsedUnitFiles(Code: TCodeBuffer; var MainUsesSection,
ImplementationUsesSection: TStrings): boolean;
function FindUsedUnitNames(Code: TCodeBuffer; var MainUsesSection,
ImplementationUsesSection: TStrings): boolean; // ignoring 'in'
function FindMissingUnits(Code: TCodeBuffer; var MissingUnits: TStrings;
FixCase: boolean = false; SearchImplementation: boolean = true): boolean;
function FindDelphiProjectUnits(Code: TCodeBuffer;
out FoundInUnits, MissingInUnits, NormalUnits: TStrings;
IgnoreNormalUnits: boolean = false): boolean;
function FindDelphiPackageUnits(Code: TCodeBuffer;
var FoundInUnits, MissingInUnits, NormalUnits: TStrings;
IgnoreNormalUnits: boolean = false): boolean;
function CommentUnitsInUsesSections(Code: TCodeBuffer;
MissingUnits: TStrings): boolean;
function FindUnitCaseInsensitive(Code: TCodeBuffer;
var AnUnitName, AnUnitInFilename: string): string;
function FindUnitSource(Code: TCodeBuffer;
const AnUnitName, AnUnitInFilename: string): TCodeBuffer;
function CreateUsesGraph: TUsesGraph;
function FindUnusedUnits(Code: TCodeBuffer; Units: TStrings): boolean;
// resources
property OnFindDefinePropertyForContext: TOnFindDefinePropertyForContext
read FOnFindDefinePropertyForContext
write FOnFindDefinePropertyForContext;
property OnFindDefineProperty: TOnFindDefineProperty
read FOnFindDefineProperty
write FOnFindDefineProperty;
function FindLFMFileName(Code: TCodeBuffer): string;
function CheckLFM(UnitCode, LFMBuf: TCodeBuffer; out LFMTree: TLFMTree;
RootMustBeClassInUnit, RootMustBeClassInIntf,
ObjectsMustExist: boolean): boolean;
function FindNextResourceFile(Code: TCodeBuffer;
var LinkIndex: integer): TCodeBuffer;
function AddLazarusResourceHeaderComment(Code: TCodeBuffer;
const CommentText: string): boolean;
function FindLazarusResource(Code: TCodeBuffer;
const ResourceName: string): TAtomPosition;
function AddLazarusResource(Code: TCodeBuffer;
const ResourceName, ResourceData: string): boolean;
function RemoveLazarusResource(Code: TCodeBuffer;
const ResourceName: string): boolean;
function RenameMainInclude(Code: TCodeBuffer; const NewFilename: string;
KeepPath: boolean): boolean;
function RenameIncludeDirective(Code: TCodeBuffer; LinkIndex: integer;
const NewFilename: string; KeepPath: boolean): boolean;// in cleaned source
procedure DefaultFindDefinePropertyForContext(Sender: TObject;
const ClassContext, AncestorClassContext: TFindContext;
{%H-}LFMNode: TLFMTreeNode;
const IdentName: string; var IsDefined: boolean);
// Delphi to Lazarus conversion
function ConvertDelphiToLazarusSource(Code: TCodeBuffer;
AddLRSCode: boolean): boolean;
// Application.Createform(ClassName,VarName) statements in program source
function FindCreateFormStatement(Code: TCodeBuffer; StartPos: integer;
const AClassName, AVarName: string;
out Position: integer): integer; // 0=found, -1=not found, 1=found, but wrong classname
function AddCreateFormStatement(Code: TCodeBuffer;
const AClassName, AVarName: string): boolean;
function RemoveCreateFormStatement(Code: TCodeBuffer;
const AVarName: string): boolean;
function ChangeCreateFormStatement(Code: TCodeBuffer;
const OldClassName, OldVarName: string;
const NewClassName, NewVarName: string;
OnlyIfExists: boolean): boolean;
function ListAllCreateFormStatements(Code: TCodeBuffer): TStrings;
function SetAllCreateFromStatements(Code: TCodeBuffer;
List: TStrings): boolean;
// Application.Title:= statements in program source
function GetApplicationTitleStatement(Code: TCodeBuffer;
var Title: string): boolean;
function SetApplicationTitleStatement(Code: TCodeBuffer;
const NewTitle: string): boolean;
function RemoveApplicationTitleStatement(Code: TCodeBuffer): boolean;
// Application.Scaled:= statements in program source
function GetApplicationScaledStatement(Code: TCodeBuffer;
var AScaled: Boolean): boolean;
function SetApplicationScaledStatement(Code: TCodeBuffer;
const NewScaled: Boolean): boolean;
function RemoveApplicationScaledStatement(Code: TCodeBuffer): boolean;
// forms
// Hint: to find the class use FindDeclarationInInterface
function RenameForm(Code: TCodeBuffer;
const OldFormName, OldFormClassName: string;
const NewFormName, NewFormClassName: string): boolean;
function FindFormAncestor(Code: TCodeBuffer; const FormClassName: string;
var AncestorClassName: string; DirtySearch: boolean): boolean;
// form components
function CompleteComponent(Code: TCodeBuffer;
AComponent, AncestorComponent: TComponent): boolean;
function PublishedVariableExists(Code: TCodeBuffer;
const AClassName, AVarName: string;
ErrorOnClassNotFound: boolean): boolean;
function AddPublishedVariable(Code: TCodeBuffer;
const AClassName,VarName, VarType: string): boolean;
function RemovePublishedVariable(Code: TCodeBuffer;
const AClassName, AVarName: string;
ErrorOnClassNotFound: boolean): boolean;
function RenamePublishedVariable(Code: TCodeBuffer;
const AClassName, OldVariableName, NewVarName,
VarType: shortstring; ErrorOnClassNotFound: boolean): boolean;
function RetypeClassVariables(Code: TCodeBuffer; const AClassName: string;
ListOfReTypes: TStringToStringTree;
ErrorOnClassNotFound: boolean;
SearchImplementationToo: boolean = false): boolean;
function FindDanglingComponentEvents(Code: TCodeBuffer;
const AClassName: string;
RootComponent: TComponent; ExceptionOnClassNotFound,
SearchInAncestors: boolean;
out ListOfPInstancePropInfo: TFPList;
const OverrideGetMethodName: TOnGetMethodname = nil): boolean;
// utilities for the object inspector
function GetCompatiblePublishedMethods(Code: TCodeBuffer;
const AClassName: string;
PropInstance: TPersistent; const PropName: string;
const Proc: TGetStrProc): boolean;
function GetCompatiblePublishedMethods(Code: TCodeBuffer;
const AClassName: string; TypeData: PTypeData;
const Proc: TGetStrProc): boolean;
function PublishedMethodExists(Code:TCodeBuffer;
const AClassName, AMethodName: string;
PropInstance: TPersistent; const PropName: string;
out MethodIsCompatible, MethodIsPublished, IdentIsMethod: boolean
): boolean;
function PublishedMethodExists(Code:TCodeBuffer; const AClassName,
AMethodName: string; TypeData: PTypeData;
out MethodIsCompatible, MethodIsPublished, IdentIsMethod: boolean
): boolean;
function JumpToPublishedMethodBody(Code: TCodeBuffer;
const AClassName, AMethodName: string;
out NewCode: TCodeBuffer;
out NewX, NewY, NewTopLine, BlockTopLine, BlockBottomLine: integer): boolean;
function RenamePublishedMethod(Code: TCodeBuffer;
const AClassName, OldMethodName,
NewMethodName: string): boolean;
function CreatePublishedMethod(Code: TCodeBuffer; const AClassName,
NewMethodName: string; ATypeInfo: PTypeInfo;
UseTypeInfoForParameters: boolean = false;
const APropertyUnitName: string = ''; const APropertyPath: string = '';
const CallAncestorMethod: string = ''; AddOverride: boolean = false
): boolean;
// private class parts
function CreatePrivateMethod(Code: TCodeBuffer; const AClassName,
NewMethodName: string; ATypeInfo: PTypeInfo;
UseTypeInfoForParameters: boolean = false;
const APropertyUnitName: string = '';
const APropertyPath: string = ''): boolean;
// IDE % directives
function GetIDEDirectives(Code: TCodeBuffer; DirectiveList: TStrings;
const Filter: TOnIDEDirectiveFilter = nil): boolean;
function SetIDEDirectives(Code: TCodeBuffer; DirectiveList: TStrings;
const Filter: TOnIDEDirectiveFilter = nil): boolean;
// linker jumping
function JumpToLinkerIdentifier(Code: TCodeBuffer;
const SourceFilename: string; SourceLine: integer;
const MangledFunction, Identifier: string;
out NewCode: TCodeBuffer;
out NewX, NewY, NewTopLine: integer): boolean;
// gdb stacktraces
function FindFPCMangledIdentifier(GDBIdentifier: string; out aComplete: boolean;
out aMessage: string; const OnFindSource: TOnFindFPCMangledSource;
out NewCode: TCodeBuffer;
out NewX, NewY, NewTopLine: integer): boolean;
property OnFindFPCMangledSource: TOnFindFPCMangledSource
read FOnFindFPCMangledSource write FOnFindFPCMangledSource;
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
procedure ConsistencyCheck;
procedure WriteDebugReport(WriteTool,
WriteDefPool, WriteDefTree, WriteCache, WriteGlobalValues,
WriteMemStats: boolean);
procedure WriteMemoryStats;
end;
var CodeToolBoss: TCodeToolManager;
function CreateDefinesForFPCMode(const Name: string;
CompilerMode: TCompilerMode): TDefineTemplate;
implementation
function CompareCodeToolMainSources(Data1, Data2: Pointer): integer;
var
Src1, Src2: Pointer;
begin
Src1:=TCustomCodeTool(Data1).Scanner.MainCode;
Src2:=TCustomCodeTool(Data2).Scanner.MainCode;
if Src1<Src2 then
Result:=-1
else if Src1>Src2 then
Result:=+1
else
Result:=0;
end;
function CompareDirectivesTreeSources(Data1, Data2: Pointer): integer;
var
Src1, Src2: Pointer;
begin
Src1:=TCompilerDirectivesTree(Data1).Code;
Src2:=TCompilerDirectivesTree(Data2).Code;
if Src1<Src2 then
Result:=-1
else if Src1>Src2 then
Result:=+1
else
Result:=0;
end;
function GetOwnerForCodeTreeNode(ANode: TCodeTreeNode): TObject;
begin
Result:=CodeToolBoss.GetOwnerForCodeTreeNode(ANode);
end;
procedure DumpExceptionBackTrace;
var
FrameCount: integer;
Frames: PPointer;
FrameNumber:Integer;
begin
DebugLn('Codetools Stack trace:');
DebugLn(BackTraceStrFunc(ExceptAddr));
FrameCount:=ExceptFrameCount;
Frames:=ExceptFrames;
for FrameNumber := 0 to FrameCount-1 do
DebugLn(BackTraceStrFunc(Frames[FrameNumber]));
end;
function CreateDefinesForFPCMode(const Name: string; CompilerMode: TCompilerMode
): TDefineTemplate;
var
cm: TCompilerMode;
NewMode: String;
begin
Result:=TDefineTemplate.Create(Name,'set FPC compiler mode',
'','',da_Block);
for cm:=Low(TCompilerMode) to High(TCompilerMode) do begin
Result.AddChild(TDefineTemplate.Create(CompilerModeVars[cm],
CompilerModeVars[cm],CompilerModeVars[cm],'',da_Undefine));
end;
NewMode:=CompilerModeVars[CompilerMode];
Result.AddChild(TDefineTemplate.Create(NewMode,
NewMode,NewMode,'1',da_Define));
end;
{ ECodeToolManagerError }
constructor ECodeToolManagerError.Create(TheID: int64; const Msg: string);
begin
Id:=TheID;
inherited Create(Msg);
end;
constructor ECodeToolManagerError.CreateFmt(TheID: int64; const Msg: string;
const Args: array of const);
begin
Id:=TheID;
inherited CreateFmt(Msg,Args);
end;
{ TCodeToolManager }
// inline
function TCodeToolManager.GetBeautifier: TBeautifyCodeOptions;
begin
Result:=SourceChangeCache.BeautifyCodeOptions;
end;
constructor TCodeToolManager.Create;
begin
inherited Create;
FCheckFilesOnDisk:=true;
FOnFindDefinePropertyForContext:=@DefaultFindDefinePropertyForContext;
DirectoryCachePool:=TCTDirectoryCachePool.Create;
DirectoryCachePool.OnGetString:=@DirectoryCachePoolGetString;
DirectoryCachePool.OnFindVirtualFile:=@DirectoryCachePoolFindVirtualFile;
DirectoryCachePool.OnGetUnitFromSet:=@DirectoryCachePoolGetUnitFromSet;
DirectoryCachePool.OnGetCompiledUnitFromSet:=@DirectoryCachePoolGetCompiledUnitFromSet;
DirectoryCachePool.OnIterateFPCUnitsFromSet:=@DirectoryCachePoolIterateFPCUnitsFromSet;
DefineTree:=TDefineTree.Create;
DefineTree.OnReadValue:=@DoOnDefineTreeReadValue;
DefinePool:=TDefinePool.Create;
SourceCache:=TCodeCache.Create;
SourceCache.DirectoryCachePool:=DirectoryCachePool;
if DefaultConfigCodeCache=nil then
DefaultConfigCodeCache:=SourceCache;
SourceChangeCache:=TSourceChangeCache.Create;
SourceChangeCache.OnBeforeApplyChanges:=@BeforeApplyingChanges;
SourceChangeCache.OnAfterApplyChanges:=@AfterApplyingChanges;
Indenter:=TFullyAutomaticBeautifier.Create;
Indenter.OnGetNestedComments:=@DoOnFABGetNestedComments;
Indenter.OnGetExamples:=@DoOnFABGetExamples;
Indenter.OnLoadFile:=@DoOnLoadFileForTool;
GlobalValues:=TExpressionEvaluator.Create;
OnFileExistsCached:=@DirectoryCachePool.FileExists;
OnFileAgeCached:=@DirectoryCachePool.FileAge;
DefineTree.DirectoryCachePool:=DirectoryCachePool;
CompilerDefinesCache:=TCompilerDefinesCache.Create(nil);
PPUCache:=TPPUTools.Create;
FAddInheritedCodeToOverrideMethod:=true;
FAdjustTopLineDueToComment:=true;
FCatchExceptions:=true;
FCompleteProperties:=true;
FSetPropertyVariablename:='AValue';
FSetPropertyVariableIsPrefix := false;
FSetPropertyVariableUseConst := false;
FAddInheritedCodeToOverrideMethod := true;
FCursorBeyondEOL:=true;
FIndentSize:=2;
FJumpSingleLinePos:=50;
FJumpSingleLinePos:=0;
FSourceExtensions:='.pp;.pas;.p;.lpr;.lpk;.dpr;.dpk';
FVisibleEditorLines:=20;
FWriteExceptions:=true;
FPascalTools:=TAVLTree.Create(@CompareCodeToolMainSources);
FDirectivesTools:=TAVLTree.Create(@CompareDirectivesTreeSources);
IdentifierList:=TIdentifierList.Create;
IdentifierHistory:=TIdentifierHistoryList.Create;
IdentifierList.History:=IdentifierHistory;
DefaultLFMTrees:=TLFMTrees.Create;
end;
destructor TCodeToolManager.Destroy;
var
e: TCodeToolManagerHandler;
begin
{$IFDEF CTDEBUG}
DebugLn('[TCodeToolManager.Destroy] A');
{$ENDIF}
FreeAndNil(GlobalValues);
{$IFDEF CTDEBUG}
DebugLn('[TCodeToolManager.Destroy] B');
{$ENDIF}
FreeAndNil(DefaultLFMTrees);
FreeAndNil(Positions);
FreeAndNil(IdentifierHistory);
FreeAndNil(IdentifierList);
FPascalTools.FreeAndClear;
FreeAndNil(FPascalTools);
FDirectivesTools.FreeAndClear;
FreeAndNil(FDirectivesTools);
FreeAndNil(PPUCache);
FreeAndNil(FResourceTool);
{$IFDEF CTDEBUG}
DebugLn('[TCodeToolManager.Destroy] C');
{$ENDIF}
FreeAndNil(DefineTree);
FreeAndNil(DefinePool);
{$IFDEF CTDEBUG}
DebugLn('[TCodeToolManager.Destroy] D');
{$ENDIF}
FreeAndNil(Indenter);
FreeAndNil(SourceChangeCache);
{$IFDEF CTDEBUG}
DebugLn('[TCodeToolManager.Destroy] E');
{$ENDIF}
if DefaultConfigCodeCache=SourceCache then
DefaultConfigCodeCache:=nil;
FreeAndNil(SourceCache);
if OnFileExistsCached=@DirectoryCachePool.FileExists then
OnFileExistsCached:=nil;
if OnFileAgeCached=@DirectoryCachePool.FileAge then
OnFileAgeCached:=nil;
FreeAndNil(DirectoryCachePool);
FreeAndNil(CompilerDefinesCache);
for e:=low(FHandlers) to high(FHandlers) do
FreeAndNil(FHandlers[e]);
{$IFDEF CTDEBUG}
DebugLn('[TCodeToolManager.Destroy] F');
{$ENDIF}
inherited Destroy;
{$IFDEF CTDEBUG}
DebugLn('[TCodeToolManager.Destroy] END');
{$ENDIF}
{$IFDEF MEM_CHECK}
CheckHeap('TCodeToolManager.Destroy END');
{$ENDIF}
end;
procedure TCodeToolManager.Init(Config: TCodeToolsOptions);
var
FPCDefines: TDefineTemplate;
FPCSrcDefines: TDefineTemplate;
LazarusSrcDefines: TDefineTemplate;
CurFPCOptions: String;
UnitSetCache: TFPCUnitSetCache;
//CfgCache: TPCTargetConfigCache;
procedure AddFPCOption(s: string);
begin
if s='' then exit;
if CurFPCOptions<>'' then
CurFPCOptions:=CurFPCOptions+' ';
CurFPCOptions:=CurFPCOptions+s;
end;
begin
// set global values
with GlobalValues do begin
Variables[ExternalMacroStart+'LazarusSrcDir']:=Config.LazarusSrcDir;
Variables[ExternalMacroStart+'FPCSrcDir']:=Config.FPCSrcDir;
Variables[ExternalMacroStart+'LCLWidgetType']:=Config.LCLWidgetType;
Variables[ExternalMacroStart+'ProjectDir']:=Config.ProjectDir;
end;
CompilerDefinesCache.ConfigCaches.Assign(Config.ConfigCaches);
CompilerDefinesCache.SourceCaches.Assign(Config.SourceCaches);
CompilerDefinesCache.TestFilename:=Config.TestPascalFile;
if CompilerDefinesCache.TestFilename='' then
CompilerDefinesCache.TestFilename:=GetTempFilename('fpctest.pas','');
UnitSetCache:=CompilerDefinesCache.FindUnitSet(Config.FPCPath,
Config.TargetOS,Config.TargetProcessor,Config.FPCOptions,Config.FPCSrcDir,
true);
// parse compiler settings, fpc sources
UnitSetCache.Init;
//CfgCache:=UnitSetCache.GetConfigCache(false);
//if CfgCache.TargetOS<>CfgCache.RealTargetOS then
// debugln(['TCodeToolManager.Init TargetOS=',CfgCache.TargetOS,' RealTargetOS=',CfgCache.RealTargetOS]);
//if CfgCache.TargetCPU<>CfgCache.RealTargetCPU then
// debugln(['TCodeToolManager.Init TargetCPU=',CfgCache.TargetCPU,' RealTargetCPU=',CfgCache.RealTargetCPU]);
// save
Config.ConfigCaches.Assign(CompilerDefinesCache.ConfigCaches);
Config.SourceCaches.Assign(CompilerDefinesCache.SourceCaches);
// create template for FPC settings
FPCDefines:=CreateFPCTemplate(UnitSetCache,nil);
DefineTree.Add(FPCDefines);
// create template for FPC source directory
FPCSrcDefines:=CreateFPCSourceTemplate(UnitSetCache,nil);
DefineTree.Add(FPCSrcDefines);
// create template for lazarus source directory
LazarusSrcDefines:=DefinePool.CreateLazarusSrcTemplate('$(#LazarusSrcDir)',
'$(#LCLWidgetType)',Config.LazarusSrcOptions,nil);
DefineTree.Add(LazarusSrcDefines);
// create template for LCL project
DefineTree.Add(DefinePool.CreateLCLProjectTemplate(
'$(#LazarusSrcDir)','$(#LCLWidgetType)','$(#ProjectDir)',nil));
//debugln(['TCodeToolManager.Init defines: ',DefineTree.GetDefinesForVirtualDirectory.AsString]);
//debugln(['TCodeToolManager.Init inc path rtl/system: ',GetIncludePathForDirectory(UnitSetCache.FPCSourceDirectory+'/rtl/bsd')]);
end;
procedure TCodeToolManager.SimpleInit(const ConfigFilename: string);
var
Options: TCodeToolsOptions;
begin
// setup the Options
Options:=TCodeToolsOptions.Create;
try
// To not parse the FPC sources every time, the options are saved to a file.
DebugLn(['TCodeToolManager.SimpleInit Config=',ConfigFilename]);
if FileExistsUTF8(ConfigFilename) then
Options.LoadFromFile(ConfigFilename);
// use environment variables
Options.InitWithEnvironmentVariables;
// apply defaults
if Options.FPCSrcDir='' then
Options.FPCSrcDir:=ExpandFileNameUTF8('~/freepascal/fpc');
if Options.LazarusSrcDir='' then
Options.LazarusSrcDir:=ExpandFileNameUTF8('~/pascal/lazarus');
DebugLn(['TCodeToolManager.SimpleInit PP=',Options.FPCPath,' FPCDIR=',Options.FPCSrcDir,' LAZARUSDIR=',Options.LazarusSrcDir,' FPCTARGET=',Options.TargetOS]);
// init the codetools
if not Options.UnitLinkListValid then
debugln('Scanning FPC sources may take a while ...');
Init(Options);
// save the options and the FPC unit links results.
Options.SaveToFile(ConfigFilename);
finally
Options.Free;
end;
end;
procedure TCodeToolManager.BeginUpdate;
begin
SourceChangeCache.BeginUpdate;
end;
function TCodeToolManager.EndUpdate: boolean;
begin
Result:=SourceChangeCache.EndUpdate;
end;
function TCodeToolManager.GatherExternalChanges: boolean;
var
Abort: Boolean;
begin
Result:=true;
if Assigned(OnGatherExternalChanges) then begin
Abort:=false;
OnGatherExternalChanges(Self,Abort);
Result:=not Abort;
end;
end;
function TCodeToolManager.FindFile(const ExpandedFilename: string): TCodeBuffer;
begin
Result:=SourceCache.FindFile(ExpandedFilename);
end;
function TCodeToolManager.LoadFile(const ExpandedFilename: string;
UpdateFromDisk, Revert: boolean): TCodeBuffer;
begin
{$IFDEF CTDEBUG}
DebugLn('>>>>>> [TCodeToolManager.LoadFile] ',ExpandedFilename,' Update=',dbgs(UpdateFromDisk),' Revert=',dbgs(Revert));
{$ENDIF}
if (not UpdateFromDisk) and (not Revert) then begin
Result:=SourceCache.FindFile(ExpandedFilename);
if (Result<>nil) and (not Result.IsDeleted) then exit;
end;
Result:=SourceCache.LoadFile(ExpandedFilename);
if Result<>nil then begin
if Revert then begin
if not Result.Revert then
Result:=nil;
end else if UpdateFromDisk and Result.AutoRevertFromDisk
and Result.FileNeedsUpdate then begin
//debugln(['TCodeToolManager.LoadFile ',ExpandedFilename,' AutoRevert=',Result.AutoRevertFromDisk,' Modified=',Result.Modified,' NeedLoad=',Result.FileNeedsUpdate]);
Result.Reload;
end;
end;
end;
function TCodeToolManager.CreateFile(const AFilename: string): TCodeBuffer;
begin
Result:=SourceCache.CreateFile(AFilename);
DirectoryCachePool.IncreaseFileTimeStamp;
{$IFDEF CTDEBUG}
DebugLn('****** TCodeToolManager.CreateFile "',AFilename,'" ',dbgs(Result<>nil));
{$ENDIF}
end;
function TCodeToolManager.CreateTempFile(const AFilename: string): TCodeBuffer;
var
i: Integer;
TempFilename: string;
CurName: String;
CurExt: String;
begin
TempFilename:=VirtualTempDir+PathDelim+AFilename;
Result:=FindFile(TempFilename);
if (Result<>nil) and (Result.ReferenceCount=0) then exit;
CurName:=ExtractFileNameOnly(AFilename);
CurExt:=ExtractFileExt(AFilename);
i:=1;
repeat
TempFilename:=VirtualTempDir+PathDelim+CurName+IntToStr(i)+CurExt;
Result:=FindFile(TempFilename);
if (Result<>nil) and (Result.ReferenceCount=0) then exit;
inc(i);
until Result=nil;
Result:=SourceCache.CreateFile(TempFilename);
Result.IncrementRefCount;
end;
procedure TCodeToolManager.ReleaseTempFile(Buffer: TCodeBuffer);
begin
Buffer.ReleaseRefCount;
end;
function TCodeToolManager.SaveBufferAs(OldBuffer: TCodeBuffer;
const ExpandedFilename: string; out NewBuffer: TCodeBuffer): boolean;
begin
Result:=SourceCache.SaveBufferAs(OldBuffer,ExpandedFilename,NewBuffer);
end;
function TCodeToolManager.FilenameHasSourceExt(
const AFilename: string): boolean;
var i, CurExtStart, CurExtEnd, ExtStart, ExtLen: integer;
begin
ExtStart:=length(AFilename);
while (ExtStart>0) and (AFilename[ExtStart]<>'.')
and (AFilename[ExtStart]<>PathDelim) do
dec(ExtStart);
if (ExtStart<1) or (AFilename[ExtStart]<>'.') then begin
Result:=false;
exit;
end;
ExtLen:=length(AFilename)-ExtStart+1;
CurExtStart:=1;
CurExtEnd:=CurExtStart;
while CurExtEnd<=length(FSourceExtensions)+1 do begin
if (CurExtEnd>length(FSourceExtensions))
or (FSourceExtensions[CurExtEnd] in [':',';']) then begin
// compare current extension with filename-extension
if ExtLen=CurExtEnd-CurExtStart then begin
i:=0;
while (i<ExtLen)
and (UpChars[AFilename[i+ExtStart]]
=UpChars[FSourceExtensions[CurExtStart+i]]) do
inc(i);
if i=ExtLen then begin
Result:=true;
exit;
end;
end;
inc(CurExtEnd);
CurExtStart:=CurExtEnd;
end else
inc(CurExtEnd);
end;
Result:=false;
end;
function TCodeToolManager.GetMainCode(Code: TCodeBuffer): TCodeBuffer;
var
NewFile: TCodeBuffer;
begin
// find MainCode (= the start source, e.g. a unit/program/package source)
Result:=Code;
if Result=nil then exit;
// if this is an include file, find the top level source
while (Result.LastIncludedByFile<>'') do begin
NewFile:=SourceCache.LoadFile(Result.LastIncludedByFile);
if (NewFile=nil) then begin
Result.LastIncludedByFile:='';
break;
end;
Result:=NewFile;
end;
if (not FilenameHasSourceExt(Result.Filename)) then begin
NewFile:=FindCodeOfMainUnitHint(Result);
if NewFile<>nil then Result:=NewFile;
end;
CreateScanner(Result);
end;
function TCodeToolManager.GetIncludeCodeChain(Code: TCodeBuffer;
RemoveFirstCodesWithoutTool: boolean; out ListOfCodeBuffer: TFPList): boolean;
var
OldCode: TCodeBuffer;
NewCode: TCodeBuffer;
begin
// find MainCode (= the start source, e.g. a unit/program/package source)
Result:=false;
ListOfCodeBuffer:=nil;
if Code=nil then exit;
Result:=true;
ListOfCodeBuffer:=TFPList.Create;
ListOfCodeBuffer.Add(Code);
// if this is an include file, find the top level source
while (Code.LastIncludedByFile<>'') do begin
NewCode:=SourceCache.LoadFile(Code.LastIncludedByFile);
if NewCode=nil then begin
NewCode.LastIncludedByFile:='';
break;
end;
Code:=NewCode;
ListOfCodeBuffer.Insert(0,Code);
end;
if (not FilenameHasSourceExt(Code.Filename)) then begin
OldCode:=Code;
Code:=FindCodeOfMainUnitHint(OldCode);
if Code<>OldCode then
ListOfCodeBuffer.Insert(0,Code);
end;
if RemoveFirstCodesWithoutTool then begin
while ListOfCodeBuffer.Count>0 do begin
Code:=TCodeBuffer(ListOfCodeBuffer[0]);
if FindCodeToolForSource(Code)<>nil then break;
ListOfCodeBuffer.Delete(0);
end;
if ListOfCodeBuffer.Count=0 then begin
ListOfCodeBuffer.Free;
ListOfCodeBuffer:=nil;
Result:=false;
exit;
end;
end;
end;
function TCodeToolManager.FindCodeOfMainUnitHint(Code: TCodeBuffer
): TCodeBuffer;
var
MainUnitFilename: string;
begin
Result:=nil;
if Code=nil then exit;
//DebugLn('TCodeToolManager.FindCodeOfMainUnitHint ',Code.Filename);
if not FindMainUnitHint(Code.Source,MainUnitFilename) then exit;
if MainUnitFilename='' then exit;
MainUnitFilename:=TrimFilename(MainUnitFilename);
if (not FilenameIsAbsolute(MainUnitFilename))
and (not Code.IsVirtual) then
MainUnitFilename:=TrimFilename(ExtractFilePath(Code.Filename)+PathDelim
+MainUnitFilename);
//DebugLn('TCodeToolManager.FindCodeOfMainUnitHint B ');
Result:=SourceCache.LoadFile(MainUnitFilename);
end;
procedure TCodeToolManager.CreateScanner(Code: TCodeBuffer);
begin
if FilenameHasSourceExt(Code.Filename) and (Code.Scanner=nil) then begin
// create a scanner for the unit/program
Code.Scanner:=TLinkScanner.Create;
Code.Scanner.OnGetInitValues:=@DoOnScannerGetInitValues;
Code.Scanner.OnSetGlobalWriteLock:=@DoOnToolSetWriteLock;
Code.Scanner.OnGetGlobalChangeSteps:=@DoOnToolGetChangeSteps;
Code.Scanner.OnProgress:=@DoOnScannerProgress;
end;
end;
procedure TCodeToolManager.ClearError;
begin
fErrorMsg:='';
fErrorCode:=nil;
fErrorLine:=-1;
fErrorTopLine:=0;
FErrorId:=0;
FErrorMsg := '';
FErrorDbgMsg := '';
end;
procedure TCodeToolManager.ClearCurCodeTool;
begin
ClearError;
FCurCodeTool:=nil;
end;
function TCodeToolManager.ApplyChanges: boolean;
begin
Result:=SourceChangeCache.Apply;
end;
function TCodeToolManager.SetGlobalValue(const VariableName,
VariableValue: string): boolean;
var
OldValue: string;
begin
OldValue:=GlobalValues[VariableName];
Result:=(OldValue<>VariableValue);
if not Result then exit;
GlobalValues[VariableName]:=VariableValue;
DefineTree.ClearCache;
end;
function TCodeToolManager.GetUnitPathForDirectory(const Directory: string;
UseCache: boolean): string;
begin
if UseCache then
Result:=DirectoryCachePool.GetString(Directory,ctdcsUnitPath,true)
else
Result:=DefineTree.GetUnitPathForDirectory(Directory);
end;
function TCodeToolManager.GetIncludePathForDirectory(const Directory: string;
UseCache: boolean): string;
begin
if UseCache then
Result:=DirectoryCachePool.GetString(Directory,ctdcsIncludePath,true)
else
Result:=DefineTree.GetIncludePathForDirectory(Directory);
end;
function TCodeToolManager.GetSrcPathForDirectory(const Directory: string;
UseCache: boolean): string;
begin
if UseCache then
Result:=DirectoryCachePool.GetString(Directory,ctdcsSrcPath,true)
else
Result:=DefineTree.GetSrcPathForDirectory(Directory);
end;
function TCodeToolManager.GetCompleteSrcPathForDirectory(
const Directory: string; UseCache: boolean): string;
// returns the SrcPath + UnitPath + any CompiledSrcPath
var
CurUnitPath: String;
StartPos: Integer;
EndPos: LongInt;
CurSrcPath: String;
CurUnitDir: String;
CurCompiledSrcPath: String;
begin
if UseCache then
Result:=DirectoryCachePool.GetString(Directory,ctdcsCompleteSrcPath,true)
else begin
CurUnitPath:='.;'+GetUnitPathForDirectory(Directory);
CurSrcPath:=GetSrcPathForDirectory(Directory);
// for every unit path, get the CompiledSrcPath
StartPos:=1;
while StartPos<=length(CurUnitPath) do begin
EndPos:=StartPos;
while (EndPos<=length(CurUnitPath)) and (CurUnitPath[EndPos]<>';') do
inc(EndPos);
if EndPos>StartPos then begin
CurUnitDir:=TrimFilename(copy(CurUnitPath,StartPos,EndPos-StartPos));
if not FilenameIsAbsolute(CurUnitDir) then
CurUnitDir:=TrimFilename(AppendPathDelim(Directory)+CurUnitDir);
CurCompiledSrcPath:=CreateAbsoluteSearchPath(
GetCompiledSrcPathForDirectory(CurUnitDir),CurUnitDir);
if CurCompiledSrcPath<>'' then
CurSrcPath:=CurSrcPath+';'+CurCompiledSrcPath;
end;
StartPos:=EndPos+1;
end;
// combine unit, src and compiledsrc search path
Result:=CurUnitPath+';'+CurSrcPath;
// make it absolute, so the user need less string concatenations
if FilenameIsAbsolute(Directory) then
Result:=CreateAbsoluteSearchPath(Result,Directory);
// trim the paths, remove doubles and empty paths
Result:=MinimizeSearchPath(Result);
end;
end;
function TCodeToolManager.GetPPUSrcPathForDirectory(const Directory: string
): string;
begin
Result:=DefineTree.GetPPUSrcPathForDirectory(Directory);
end;
function TCodeToolManager.GetDCUSrcPathForDirectory(const Directory: string
): string;
begin
Result:=DefineTree.GetDCUSrcPathForDirectory(Directory);
end;
function TCodeToolManager.GetCompiledSrcPathForDirectory(
const Directory: string; UseCache: boolean): string;
begin
Result:=DefineTree.GetCompiledSrcPathForDirectory(Directory);
end;
function TCodeToolManager.GetNestedCommentsFlagForFile(
const Filename: string): boolean;
var
Directory: String;
begin
Result:=false;
Directory:=ExtractFilePath(Filename);
// check pascal compiler is FPC and mode is FPC or OBJFPC
if GetPascalCompilerForDirectory(Directory)<>pcFPC then exit;
if not (GetCompilerModeForDirectory(Directory) in [cmFPC,cmOBJFPC]) then exit;
Result:=true;
end;
function TCodeToolManager.GetPascalCompilerForDirectory(const Directory: string
): TPascalCompiler;
var
Evaluator: TExpressionEvaluator;
PascalCompiler: string;
pc: TPascalCompiler;
begin
Result:=pcFPC;
Evaluator:=DefineTree.GetDefinesForDirectory(Directory,true);
if Evaluator=nil then exit;
PascalCompiler:=Evaluator.Variables[PascalCompilerDefine];
for pc:=Low(TPascalCompiler) to High(TPascalCompiler) do
if (PascalCompiler=PascalCompilerNames[pc]) then
Result:=pc;
end;
function TCodeToolManager.GetCompilerModeForDirectory(const Directory: string
): TCompilerMode;
var
Evaluator: TExpressionEvaluator;
cm: TCompilerMode;
begin
Result:=cmFPC;
Evaluator:=DefineTree.GetDefinesForDirectory(Directory,true);
if Evaluator=nil then exit;
for cm:=Succ(Low(TCompilerMode)) to High(TCompilerMode) do
if Evaluator.IsDefined(CompilerModeVars[cm]) then
Result:=cm;
end;
function TCodeToolManager.GetCompiledSrcExtForDirectory(const Directory: string
): string;
begin
Result:='.ppu';
end;
function TCodeToolManager.FindUnitInUnitLinks(const Directory, AUnitName: string
): string;
begin
Result:=DirectoryCachePool.FindUnitInUnitLinks(Directory,AUnitName);
end;
function TCodeToolManager.GetUnitLinksForDirectory(const Directory: string;
UseCache: boolean): string;
var
Evaluator: TExpressionEvaluator;
begin
if UseCache then begin
Result:=DirectoryCachePool.GetString(Directory,ctdcsUnitLinks,true)
end else begin
Result:='';
Evaluator:=DefineTree.GetDefinesForDirectory(Directory,true);
if Evaluator=nil then exit;
Result:=Evaluator[UnitLinksMacroName];
end;
end;
function TCodeToolManager.FindUnitInUnitSet(const Directory, AUnitName: string
): string;
begin
Result:=DirectoryCachePool.FindUnitInUnitSet(Directory,AUnitName);
end;
function TCodeToolManager.GetUnitSetIDForDirectory(const Directory: string;
UseCache: boolean): string;
var
Evaluator: TExpressionEvaluator;
begin
if UseCache then begin
Result:=DirectoryCachePool.GetString(Directory,ctdcsUnitSet,true)
end else begin
Result:='';
Evaluator:=DefineTree.GetDefinesForDirectory(Directory,true);
if Evaluator=nil then exit;
Result:=Evaluator[UnitSetMacroName];
end;
end;
function TCodeToolManager.GetUnitSetForDirectory(const Directory: string
): TFPCUnitSetCache;
var
ID: String;
Changed: boolean;
begin
Result:=nil;
ID:=GetUnitSetIDForDirectory(Directory,true);
if ID='' then exit;
Changed:=false;
Result:=CompilerDefinesCache.FindUnitSetWithID(ID,Changed,false);
if Changed then Result:=nil;
end;
function TCodeToolManager.GetFPCUnitPathForDirectory(const Directory: string;
UseCache: boolean): string;
var
Evaluator: TExpressionEvaluator;
begin
if UseCache then begin
Result:=DirectoryCachePool.GetString(Directory,ctdcsFPCUnitPath,true)
end else begin
Result:='';
Evaluator:=DefineTree.GetDefinesForDirectory(Directory,true);
if Evaluator=nil then exit;
Result:=Evaluator[FPCUnitPathMacroName];
end;
end;
procedure TCodeToolManager.GetFPCVersionForDirectory(const Directory: string;
out FPCVersion, FPCRelease, FPCPatch: integer);
var
Evaluator: TExpressionEvaluator;
FPCFullVersion: LongInt;
begin
FPCVersion:=0;
FPCRelease:=0;
FPCPatch:=0;
Evaluator:=DefineTree.GetDefinesForDirectory(Directory,true);
if Evaluator=nil then exit;
FPCFullVersion:=StrToIntDef(Evaluator['FPC_FULLVERSION'],0);
FPCVersion:=FPCFullVersion div 10000;
FPCRelease:=(FPCFullVersion div 100) mod 100;
FPCPatch:=FPCFullVersion mod 100;
end;
function TCodeToolManager.GetPCVersionForDirectory(const Directory: string
): integer;
var
Evaluator: TExpressionEvaluator;
s: String;
begin
Result:=0;
Evaluator:=DefineTree.GetDefinesForDirectory(Directory,true);
if Evaluator=nil then exit;
s:=Evaluator['FPC_FULLVERSION'];
if s<>'' then
exit(StrToIntDef(s,0));
s:=Evaluator['PAS2JS_FULLVERSION'];
if s<>'' then
exit(StrToIntDef(s,0));
end;
function TCodeToolManager.GetNamespacesForDirectory(const Directory: string;
UseCache: boolean): string;
var
Evaluator: TExpressionEvaluator;
FPCFullVersion: LongInt;
UnitSet: TFPCUnitSetCache;
begin
if UseCache then begin
Result:=DirectoryCachePool.GetString(Directory,ctdcsNamespaces,true)
end else begin
Result:='';
Evaluator:=DefineTree.GetDefinesForDirectory(Directory,true);
if Evaluator=nil then exit;
if Evaluator.IsDefined('PAS2JS') then
Result:=Evaluator[NamespacesMacroName]
else begin
FPCFullVersion:=StrToIntDef(Evaluator['FPC_FULLVERSION'],0);
if FPCFullVersion>=30101 then
Result:=Evaluator[NamespacesMacroName];
end;
// add default unit scopes from compiler cfg
UnitSet:=GetUnitSetForDirectory(Directory);
if UnitSet<>nil then
Result:=MergeWithDelimiter(Result,UnitSet.GetUnitScopes,';');
end;
end;
procedure TCodeToolManager.FreeListOfPCodeXYPosition(var List: TFPList);
begin
CodeCache.FreeListOfPCodeXYPosition(List);
List:=nil;
end;
procedure TCodeToolManager.FreeTreeOfPCodeXYPosition(var Tree: TAVLTree);
begin
CodeCache.FreeTreeOfPCodeXYPosition(Tree);
Tree:=nil;
end;
function TCodeToolManager.CreateTreeOfPCodeXYPosition: TAVLTree;
begin
Result:=CodeCache.CreateTreeOfPCodeXYPosition;
end;
procedure TCodeToolManager.AddListToTreeOfPCodeXYPosition(SrcList: TFPList;
DestTree: TAVLTree; ClearList, CreateCopies: boolean);
begin
CodeCache.AddListToTreeOfPCodeXYPosition(SrcList,DestTree,ClearList,CreateCopies);
end;
function TCodeToolManager.Explore(Code: TCodeBuffer;
out ACodeTool: TCodeTool; WithStatements: boolean; OnlyInterface: boolean
): boolean;
begin
Result:=false;
ACodeTool:=nil;
try
if InitCurCodeTool(Code) then begin
ACodeTool:=FCurCodeTool;
FCurCodeTool.Explore(WithStatements,OnlyInterface);
Result:=true;
end;
except
on e: Exception do Result:=HandleException(e);
end;
end;
function TCodeToolManager.InitCurCodeTool(Code: TCodeBuffer): boolean;
var MainCode: TCodeBuffer;
begin
Result:=false;
ClearCurCodeTool;
MainCode:=GetMainCode(Code);
if MainCode=nil then begin
ClearError;
FErrorLine:=1;
FErrorColumn:=1;
fErrorCode:=Code;
if Code = nil then
begin
fErrorMsg:='TCodeToolManager.InitCurCodeTool Code=nil'
end
else begin
fErrorMsg:='unit of include file is not known (hint: open and explore unit first)';
end;
exit;
end;
if MainCode.Scanner=nil then begin
FErrorMsg:=Format(ctsNoScannerFound,[MainCode.Filename]);
exit;
end;
FCurCodeTool:=TCodeTool(GetCodeToolForSource(MainCode,false,true));
FCurCodeTool.ErrorPosition.Code:=nil;
{$IFDEF CTDEBUG}
DebugLn('[TCodeToolManager.InitCurCodeTool] ',Code.Filename,' ',dbgs(Code.SourceLength));
{$ENDIF}
Result:=(FCurCodeTool.Scanner<>nil);
if not Result then begin
fErrorCode:=MainCode;
fErrorMsg:=ctsNoScannerAvailable;
end;
end;
function TCodeToolManager.InitResourceTool: boolean;
begin
ClearError;
Result:=true;
end;
procedure TCodeToolManager.ClearPositions;
begin
if Positions=nil then
Positions:=TCodeXYPositions.Create
else
Positions.Clear;
end;
function TCodeToolManager.HandleException(AnException: Exception): boolean;
var
ErrorSrcTool: TCustomCodeTool;
DirtyPos: Integer;
ErrorDirTool: TCompilerDirectivesTree;
begin
ClearError;
fErrorMsg:=AnException.Message;
if (AnException is ELinkScannerError) then begin
// link scanner error
FErrorId:=ELinkScannerError(AnException).Id;
if AnException is ELinkScannerConsistency then
DumpExceptionBackTrace;
DirtyPos:=0;
if AnException is ELinkScannerEditError then begin
fErrorCode:=TCodeBuffer(ELinkScannerEditError(AnException).Buffer);
if fErrorCode<>nil then
DirtyPos:=ELinkScannerEditError(AnException).BufferPos;
end else begin
fErrorCode:=TCodeBuffer(ELinkScannerError(AnException).Sender.Code);
DirtyPos:=ELinkScannerError(AnException).Sender.SrcPos;
end;
if (fErrorCode<>nil) and (DirtyPos>0) then begin
fErrorCode.AbsoluteToLineCol(DirtyPos,fErrorLine,fErrorColumn);
end;
end else if (AnException is ECodeToolError) then begin
// codetool error
ErrorSrcTool:=ECodeToolError(AnException).Sender;
FErrorId:=ECodeToolError(AnException).Id;
if ErrorSrcTool.ErrorNicePosition.Code<>nil then begin
fErrorCode:=ErrorSrcTool.ErrorNicePosition.Code;
fErrorColumn:=ErrorSrcTool.ErrorNicePosition.X;
fErrorLine:=ErrorSrcTool.ErrorNicePosition.Y;
end else begin
fErrorCode:=ErrorSrcTool.ErrorPosition.Code;
fErrorColumn:=ErrorSrcTool.ErrorPosition.X;
fErrorLine:=ErrorSrcTool.ErrorPosition.Y;
end;
end else if (AnException is ECDirectiveParserException) then begin
// Compiler directive parser error
FErrorId:=ECDirectiveParserException(AnException).Id;
ErrorDirTool:=ECDirectiveParserException(AnException).Sender;
fErrorCode:=ErrorDirTool.Code;
end else if (AnException is ESourceChangeCacheError) then begin
// SourceChangeCache error
FErrorId:=ESourceChangeCacheError(AnException).Id;
end else if (AnException is ECodeToolManagerError) then begin
// CodeToolManager error
FErrorId:=ECodeToolManagerError(AnException).Id;
end else begin
// unknown exception
DumpExceptionBackTrace;
FErrorMsg:=AnException.ClassName+': '+FErrorMsg;
if FCurCodeTool<>nil then begin
fErrorCode:=FCurCodeTool.ErrorPosition.Code;
fErrorColumn:=FCurCodeTool.ErrorPosition.X;
fErrorLine:=FCurCodeTool.ErrorPosition.Y;
end;
FErrorId:=20170421202914;
end;
SourceChangeCache.Clear;
// adjust error topline
AdjustErrorTopLine;
// write error
WriteError;
// raise or catch
if not FCatchExceptions then raise AnException;
Result:=false;
end;
procedure TCodeToolManager.AdjustErrorTopLine;
begin
// adjust error topline
if (fErrorCode<>nil) and (fErrorTopLine<1) then begin
fErrorTopLine:=fErrorLine;
if (fErrorTopLine>0) and (JumpSingleLinePos>0) then begin
dec(fErrorTopLine,VisibleEditorLines*JumpSingleLinePos div 100);
if fErrorTopLine<1 then fErrorTopLine:=1;
end;
end;
end;
procedure TCodeToolManager.WriteError;
begin
if FWriteExceptions then begin
FErrorDbgMsg:='### TCodeToolManager.HandleException: ['+IntToStr(FErrorId)+'] "'+ErrorMessage+'"';
if ErrorLine>0 then FErrorDbgMsg+=' at Line='+DbgS(ErrorLine);
if ErrorColumn>0 then FErrorDbgMsg+=' Col='+DbgS(ErrorColumn);
if ErrorCode<>nil then FErrorDbgMsg+=' in "'+ErrorCode.Filename+'"';
Debugln(FErrorDbgMsg);
{$IFDEF CTDEBUG}
WriteDebugReport(true,false,false,false,false,false);
{$ENDIF}
end;
end;
function TCodeToolManager.CheckSyntax(Code: TCodeBuffer;
out NewCode: TCodeBuffer; out NewX, NewY, NewTopLine: integer;
out ErrorMsg: string): boolean;
// returns true on syntax correct
var
ACodeTool: TCodeTool;
begin
Result:=Explore(Code,ACodeTool,true);
if ACodeTool=nil then ;
NewCode:=ErrorCode;
NewX:=ErrorColumn;
NewY:=ErrorLine;
NewTopLine:=ErrorTopLine;
ErrorMsg:=ErrorMessage;
end;
function TCodeToolManager.ExploreDirectives(Code: TCodeBuffer; out
ADirectivesTool: TDirectivesTool): boolean;
begin
Result:=false;
ADirectivesTool:=nil;
try
if InitCurDirectivesTool(Code) then begin
ADirectivesTool:=FCurDirectivesTool;
FCurDirectivesTool.Parse;
Result:=true;
end;
except
on e: Exception do Result:=HandleException(e);
end;
end;
function TCodeToolManager.ExploreUnitDirectives(Code: TCodeBuffer; out
aScanner: TLinkScanner): boolean;
begin
Result:=false;
if not InitCurCodeTool(Code) then exit;
{$IFDEF CTDEBUG}
DebugLn('TCodeToolManager.ExploreUnitDirectives A ',dbgs(FCurCodeTool.Scanner<>nil));
{$ENDIF}
try
aScanner:=FCurCodeTool.Scanner;
if not aScanner.StoreDirectives then
aScanner.DemandStoreDirectives;
aScanner.Scan(lsrEnd,true);
Result:=true;
except
on e: Exception do Result:=HandleException(e);
end;
{$IFDEF CTDEBUG}
DebugLn('TCodeToolManager.ExploreUnitDirectives END ');
{$ENDIF}
end;
function TCodeToolManager.JumpToMethod(Code: TCodeBuffer; X, Y: integer; out
NewCode: TCodeBuffer; out NewX, NewY, NewTopLine, BlockTopLine,
BlockBottomLine: integer; out RevertableJump: boolean): boolean;
var
CursorPos: TCodeXYPosition;
NewPos: TCodeXYPosition;
begin
Result:=false;
{$IFDEF CTDEBUG}
DebugLn('TCodeToolManager.JumpToMethod A ',Code.Filename,' x=',dbgs(x),' y=',dbgs(y));
{$ENDIF}
if not InitCurCodeTool(Code) then exit;
CursorPos.X:=X;
CursorPos.Y:=Y;
CursorPos.Code:=Code;
{$IFDEF CTDEBUG}
DebugLn('TCodeToolManager.JumpToMethod B ',dbgs(FCurCodeTool.Scanner<>nil));
{$ENDIF}
try
Result:=FCurCodeTool.FindJumpPoint(CursorPos,NewPos,NewTopLine,
BlockTopLine,BlockBottomLine,RevertableJump);
if Result then begin
NewX:=NewPos.X;
NewY:=NewPos.Y;
NewCode:=NewPos.Code;
end;
except
on e: Exception do Result:=HandleException(e);
end;
{$IFDEF CTDEBUG}
DebugLn('TCodeToolManager.JumpToMethod END ');
{$ENDIF}
end;
function TCodeToolManager.FindProcDeclaration(Code: TCodeBuffer;
CleanDef: string; out Tool: TCodeTool; out Node: TCodeTreeNode;
Attr: TProcHeadAttributes): boolean;
var
Paths: TStringList;
begin
Result:=false;
{$IFDEF CTDEBUG}
DebugLn(['TCodeToolManager.FindProcDeclaration A ',Code.Filename,' CleanDef=',CleanDef]);
{$ENDIF}
Tool:=nil;
Node:=nil;
if not InitCurCodeTool(Code) then exit;
Tool:=FCurCodeTool;
Paths:=TStringList.Create;
try
Paths.Add(CleanDef);
try
FCurCodeTool.BuildTree(lsrInitializationStart);
Node:=FCurCodeTool.FindSubProcPath(Paths,Attr,false);
Result:=Node<>nil;
except
on e: Exception do Result:=HandleException(e);
end;
finally
Paths.Free;
end;
{$IFDEF CTDEBUG}
DebugLn('TCodeToolManager.FindProcDeclaration END ');
{$ENDIF}
end;
function TCodeToolManager.FindDeclaration(Code: TCodeBuffer; X, Y: integer; out
NewCode: TCodeBuffer; out NewX, NewY, NewTopLine, BlockTopLine,
BlockBottomLine: integer; Flags: TFindSmartFlags): boolean;
var
CursorPos: TCodeXYPosition;
NewPos: TCodeXYPosition;
NewTool: TFindDeclarationTool;
NewNode: TCodeTreeNode;
begin
Result:=false;
{$IFDEF CTDEBUG}
DebugLn(['TCodeToolManager.FindDeclaration A ',Code.Filename,' x=',x,' y=',y]);
{$ENDIF}
if not InitCurCodeTool(Code) then begin
{$IFDEF VerboseFindDeclarationFail}
debugln(['TCodeToolManager.FindDeclaration InitCurCodeTool failed']);
{$ENDIF}
exit;
end;
CursorPos.X:=X;
CursorPos.Y:=Y;
CursorPos.Code:=Code;
{$IFDEF CTDEBUG}
DebugLn('TCodeToolManager.FindDeclaration B ',dbgs(FCurCodeTool.Scanner<>nil));
{$ENDIF}
try
{$IFDEF DoNotHandleFindDeclException}
DebugLn('TCodeToolManager.FindDeclaration NOT HANDLING EXCEPTIONS');
RaiseUnhandableExceptions:=true;
{$ENDIF}
Result:=FCurCodeTool.FindDeclaration(CursorPos,Flags,NewTool,NewNode,
NewPos,NewTopLine,BlockTopLine,BlockBottomLine);
if Result then begin
NewX:=NewPos.X;
NewY:=NewPos.Y;
NewCode:=NewPos.Code;
if (NewTool=nil) and (NewNode<>nil) then ;
{$IFDEF CTDEBUG}
debugln(['TCodeToolManager.FindDeclaration ',Dbgs(NewPos)]);
{$ENDIF}
end;
{$IFDEF DoNotHandleFindDeclException}
finally
RaiseUnhandableExceptions:=false;
end;
{$ELSE}
except
on e: Exception do begin
Result:=HandleException(e);
{$IFDEF VerboseFindDeclarationFail}
if not Result then
debugln(['TCodeToolManager.FindDeclaration Exception=',e.Message]);
{$ENDIF}
end;
end;
{$ENDIF}
{$IFDEF VerboseFindDeclarationFail}
if not Result then begin
debugln(['TCodeToolManager.FindDeclaration FAILED at ',dbgs(CursorPos)]);
end;
{$ENDIF}
{$IFDEF CTDEBUG}
DebugLn('TCodeToolManager.FindDeclaration END ');
{$ENDIF}
end;
function TCodeToolManager.FindDeclarationOfIdentifier(Code: TCodeBuffer;
X,Y: integer; Identifier: PChar; out NewCode: TCodeBuffer; out NewX, NewY,
NewTopLine: integer): boolean;
var
CursorPos: TCodeXYPosition;
NewPos: TCodeXYPosition;
begin
Result:=false;
{$IFDEF CTDEBUG}
DebugLn(['TCodeToolManager.FindDeclarationOfIdentifier A ',Code.Filename,' x=',x,' y=',y,' Identifier=',GetIdentifier(Identifier)]);
{$ENDIF}
if not InitCurCodeTool(Code) then exit;
CursorPos.X:=X;
CursorPos.Y:=Y;
CursorPos.Code:=Code;
{$IFDEF CTDEBUG}
DebugLn('TCodeToolManager.FindDeclarationOfIdentifier B ',dbgs(FCurCodeTool.Scanner<>nil));
{$ENDIF}
try
{$IFDEF DoNotHandleFindDeclException}
DebugLn('TCodeToolManager.FindDeclarationOfIdentifier NOT HANDLING EXCEPTIONS');
RaiseUnhandableExceptions:=true;
{$ENDIF}
Result:=FCurCodeTool.FindDeclarationOfIdentifier(CursorPos,Identifier,NewPos,NewTopLine);
if Result then begin
NewX:=NewPos.X;
NewY:=NewPos.Y;
NewCode:=NewPos.Code;
end;
{$IFDEF DoNotHandleFindDeclException}
finally
RaiseUnhandableExceptions:=false;
end;
{$ELSE}
except
on e: Exception do Result:=HandleException(e);
end;
{$ENDIF}
{$IFDEF CTDEBUG}
DebugLn('TCodeToolManager.FindDeclarationOfIdentifier END ');
{$ENDIF}
end;
function TCodeToolManager.FindSmartHint(Code: TCodeBuffer; X, Y: integer;
Flags: TFindSmartFlags): string;
var
CursorPos: TCodeXYPosition;
begin
Result:='';
{$IFDEF CTDEBUG}
DebugLn('TCodeToolManager.FindSmartHint A ',Code.Filename,' x=',dbgs(x),' y=',dbgs(y));
{$ENDIF}
if not InitCurCodeTool(Code) then exit;
CursorPos.X:=X;
CursorPos.Y:=Y;
CursorPos.Code:=Code;
{$IFDEF CTDEBUG}
DebugLn('TCodeToolManager.FindSmartHint B ',dbgs(FCurCodeTool.Scanner<>nil));
{$ENDIF}
try
Result:=FCurCodeTool.FindSmartHint(CursorPos,Flags);
except
on e: Exception do HandleException(e);
end;
{$IFDEF CTDEBUG}
DebugLn('TCodeToolManager.FindSmartHint END ');
{$ENDIF}
end;
function TCodeToolManager.FindDeclarationInInterface(Code: TCodeBuffer;
const Identifier: string; out NewCode: TCodeBuffer; out NewX, NewY,
NewTopLine, BlockTopLine, BlockBottomLine: integer): boolean;
var
NewPos: TCodeXYPosition;
begin
Result:=false;
{$IFDEF CTDEBUG}
DebugLn('TCodeToolManager.FindDeclarationInInterface A ',Code.Filename,' Identifier=',Identifier);
{$ENDIF}
if not InitCurCodeTool(Code) then exit;
{$IFDEF CTDEBUG}
DebugLn('TCodeToolManager.FindDeclarationInInterface B ',dbgs(FCurCodeTool.Scanner<>nil));
{$ENDIF}
try
Result:=FCurCodeTool.FindDeclarationInInterface(Identifier,NewPos,
NewTopLine,BlockTopLine,BlockBottomLine);
if Result then begin
NewX:=NewPos.X;
NewY:=NewPos.Y;
NewCode:=NewPos.Code;
end;
except
on e: Exception do HandleException(e);
end;
{$IFDEF CTDEBUG}
DebugLn('TCodeToolManager.FindDeclarationInInterface END ');
{$ENDIF}
end;
function TCodeToolManager.FindDeclarationInInterface(Code: TCodeBuffer;
const Identifier: string; out NewCode: TCodeBuffer; out NewX, NewY,
NewTopLine: integer): boolean;
var
BlockTopLine, BlockBottomLine: integer;
begin
Result := FindDeclarationInInterface(Code, Identifier, NewCode, NewX, NewY, NewTopLine,
BlockTopLine, BlockBottomLine);
end;
function TCodeToolManager.FindDeclarationWithMainUsesSection(Code: TCodeBuffer;
const Identifier: string; out NewCode: TCodeBuffer;
out NewX, NewY, NewTopLine: integer): Boolean;
var
NewPos: TCodeXYPosition;
begin
Result:=false;
{$IFDEF CTDEBUG}
DebugLn('TCodeToolManager.FindDeclarationWithMainUsesSection A ',Code.Filename,' Identifier=',Identifier);
{$ENDIF}
if not InitCurCodeTool(Code) then exit;
try
Result:=FCurCodeTool.FindDeclarationWithMainUsesSection(Identifier,NewPos,
NewTopLine);
if Result then begin
NewX:=NewPos.X;
NewY:=NewPos.Y;
NewCode:=NewPos.Code;
end;
except
on e: Exception do HandleException(e);
end;
{$IFDEF CTDEBUG}
DebugLn('TCodeToolManager.FindDeclarationInInterface END ');
{$ENDIF}
end;
function TCodeToolManager.FindDeclarationAndOverload(Code: TCodeBuffer; X,
Y: integer; out ListOfPCodeXYPosition: TFPList;
Flags: TFindDeclarationListFlags): boolean;
var
CursorPos: TCodeXYPosition;
begin
Result:=false;
{$IFDEF CTDEBUG}
DebugLn('TCodeToolManager.FindDeclarationAndOverload A ',Code.Filename,' x=',dbgs(x),' y=',dbgs(y));
{$ENDIF}
ListOfPCodeXYPosition:=nil;
if not InitCurCodeTool(Code) then exit;
CursorPos.X:=X;
CursorPos.Y:=Y;
CursorPos.Code:=Code;
{$IFDEF CTDEBUG}
DebugLn('TCodeToolManager.FindDeclarationAndOverload B ',dbgs(FCurCodeTool.Scanner<>nil));
{$ENDIF}
try
Result:=FCurCodeTool.FindDeclarationAndOverload(CursorPos,
ListOfPCodeXYPosition,Flags);
except
on e: Exception do Result:=HandleException(e);
end;
{$IFDEF CTDEBUG}
DebugLn('TCodeToolManager.FindDeclarationAndOverload END ');
{$ENDIF}
end;
function TCodeToolManager.FindMainDeclaration(Code: TCodeBuffer; X, Y: integer;
out NewCode: TCodeBuffer; out NewX, NewY, NewTopLine: integer): boolean;
var
CursorPos: TCodeXYPosition;
NewPos: TCodeXYPosition;
begin
Result:=false;
{$IFDEF CTDEBUG}
DebugLn('TCodeToolManager.FindMainDeclaration A ',Code.Filename,' x=',dbgs(x),' y=',dbgs(y));
{$ENDIF}
if not InitCurCodeTool(Code) then exit;
CursorPos.X:=X;
CursorPos.Y:=Y;
CursorPos.Code:=Code;
try
Result:=FCurCodeTool.FindMainDeclaration(CursorPos,NewPos,NewTopLine);
if Result then begin
NewX:=NewPos.X;
NewY:=NewPos.Y;
NewCode:=NewPos.Code;
end;
except
on e: Exception do Result:=HandleException(e);
end;
{$IFDEF CTDEBUG}
DebugLn('TCodeToolManager.FindMainDeclaration END ');
{$ENDIF}
end;
function TCodeToolManager.FindDeclarationOfPropertyPath(Code: TCodeBuffer;
const PropertyPath: string; out NewCode: TCodeBuffer; out NewX, NewY,
NewTopLine: integer): Boolean;
var
NewPos: TCodeXYPosition;
begin
Result:=false;
{$IFDEF CTDEBUG}
DebugLn('TCodeToolManager.FindDeclarationOfPropertyPath A ',Code.Filename,' Path="',PropertyPath,'"');
{$ENDIF}
if not InitCurCodeTool(Code) then exit;
try
Result:=FCurCodeTool.FindDeclarationOfPropertyPath(PropertyPath,
NewPos,NewTopLine);
if Result then begin
NewX:=NewPos.X;
NewY:=NewPos.Y;
NewCode:=NewPos.Code;
end;
except
on e: Exception do Result:=HandleException(e);
end;
{$IFDEF CTDEBUG}
DebugLn('TCodeToolManager.FindDeclarationOfPropertyPath END ');
{$ENDIF}
end;
function TCodeToolManager.FindFileAtCursor(Code: TCodeBuffer; X, Y: integer;
out Found: TFindFileAtCursorFlag; out FoundFilename: string;
Allowed: TFindFileAtCursorFlags; StartPos: PCodeXYPosition): boolean;
var
CursorPos: TCodeXYPosition;
begin
Result:=false;
{$IFDEF CTDEBUG}
DebugLn('TCodeToolManager.FindFileAtCursor A ',Code.Filename,' x=',dbgs(x),' y=',dbgs(y));
{$ENDIF}
if not InitCurCodeTool(Code) then exit;
CursorPos.X:=X;
CursorPos.Y:=Y;
CursorPos.Code:=Code;
try
Result:=FCurCodeTool.FindFileAtCursor(CursorPos,Found,FoundFilename,
Allowed,StartPos);
except
on e: Exception do HandleException(e);
end;
{$IFDEF CTDEBUG}
DebugLn('TCodeToolManager.FindFileAtCursor END ');
{$ENDIF}
end;
function TCodeToolManager.FindCodeContext(Code: TCodeBuffer; X, Y: integer; out
CodeContexts: TCodeContextInfo): boolean;
var
CursorPos: TCodeXYPosition;
begin
Result:=false;
CodeContexts:=nil;
{$IFDEF CTDEBUG}
DebugLn('TCodeToolManager.FindCodeContext A ',Code.Filename,' x=',dbgs(x),' y=',dbgs(y));
{$ENDIF}
if not InitCurCodeTool(Code) then exit;
CursorPos.X:=X;
CursorPos.Y:=Y;
CursorPos.Code:=Code;
try
Result:=FCurCodeTool.FindCodeContext(CursorPos,CodeContexts);
except
on e: Exception do HandleException(e);
end;
{$IFDEF CTDEBUG}
DebugLn('TCodeToolManager.FindCodeContext END ');
{$ENDIF}
end;
function TCodeToolManager.ExtractProcedureHeader(Code: TCodeBuffer; X,
Y: integer; Attributes: TProcHeadAttributes; out ProcHead: string): boolean;
var
CursorPos: TCodeXYPosition;
begin
Result:=false;
{$IFDEF CTDEBUG}
DebugLn('TCodeToolManager.ExtractProcedureHeader A ',Code.Filename,' x=',dbgs(x),' y=',dbgs(y));
{$ENDIF}
if not InitCurCodeTool(Code) then exit;
CursorPos.X:=X;
CursorPos.Y:=Y;
CursorPos.Code:=Code;
try
Result:=FCurCodeTool.ExtractProcedureHeader(CursorPos,Attributes,ProcHead);
except
on e: Exception do HandleException(e);
end;
{$IFDEF CTDEBUG}
DebugLn('TCodeToolManager.ExtractProcedureHeader END ');
{$ENDIF}
end;
function TCodeToolManager.HasInterfaceRegisterProc(Code: TCodeBuffer;
out HasRegisterProc: boolean): boolean;
begin
Result:=false;
HasRegisterProc:=false;
{$IFDEF CTDEBUG}
DebugLn('TCodeToolManager.HasInterfaceRegisterProc A ',Code.Filename);
{$ENDIF}
if not InitCurCodeTool(Code) then exit;
try
Result:=FCurCodeTool.HasInterfaceRegisterProc(HasRegisterProc);
except
on e: Exception do Result:=HandleException(e);
end;
end;
function TCodeToolManager.GatherUnitNames(Code: TCodeBuffer): Boolean;
var
CursorPos: TCodeXYPosition;
begin
Result := False;
if not InitCurCodeTool(Code) then exit;
if IdentifierList<>nil then IdentifierList.Clear;
CursorPos.X := 0;
CursorPos.Y := 0;
CursorPos.Code := Code;
try
Result := FCurCodeTool.GatherAvailableUnitNames(CursorPos, IdentifierList);
except
on e: Exception do HandleException(e);
end;
end;
function TCodeToolManager.GetApplicationScaledStatement(Code: TCodeBuffer;
var AScaled: Boolean): boolean;
var
StartPos, BooleanConstStartPos, EndPos: integer;
begin
Result:=false;
{$IFDEF CTDEBUG}
DebugLn('TCodeToolManager.GetApplicationScaledStatement A ',Code.Filename);
{$ENDIF}
if not InitCurCodeTool(Code) then exit;
try
Result:=FCurCodeTool.FindApplicationScaledStatement(StartPos,
BooleanConstStartPos,EndPos);
Result:=FCurCodeTool.GetApplicationScaledStatement(BooleanConstStartPos,
EndPos,AScaled);
except
on e: Exception do Result:=HandleException(e);
end;
end;
function TCodeToolManager.GatherIdentifiers(Code: TCodeBuffer; X, Y: integer
): boolean;
var
CursorPos: TCodeXYPosition;
begin
Result:=false;
{$IFDEF CTDEBUG}
DebugLn('TCodeToolManager.GatherIdentifiers A ',Code.Filename,' x=',dbgs(x),' y=',dbgs(y));
{$ENDIF}
if not InitCurCodeTool(Code) then exit;
if IdentifierList<>nil then IdentifierList.Clear;
CursorPos.X:=X;
CursorPos.Y:=Y;
CursorPos.Code:=Code;
{$IFDEF CTDEBUG}
DebugLn('TCodeToolManager.GatherIdentifiers B ',dbgs(FCurCodeTool.Scanner<>nil));
{$ENDIF}
try
FIdentifierListUpdating:=true;
try
Result:=FCurCodeTool.GatherIdentifiers(CursorPos,IdentifierList);
finally
FIdentifierListUpdating:=false;
end;
except
on e: Exception do HandleException(e);
end;
{$IFDEF CTDEBUG}
DebugLn('TCodeToolManager.GatherIdentifiers END ');
{$ENDIF}
end;
function TCodeToolManager.GetIdentifierAt(Code: TCodeBuffer; X, Y: integer; out
Identifier: string): boolean;
var
CleanPos: integer;
begin
Result:=false;
{$IFDEF CTDEBUG}
DebugLn('TCodeToolManager.GetIdentifierAt A ',Code.Filename,' x=',dbgs(x),' y=',dbgs(y));
{$ENDIF}
Code.LineColToPosition(Y,X,CleanPos);
if (CleanPos>0) and (CleanPos<=Code.SourceLength) then begin
Identifier:=GetIdentifier(@Code.Source[CleanPos]);
Result:=true;
end else begin
Identifier:='';
Result:=false;
end;
end;
function TCodeToolManager.IdentItemCheckHasChilds(IdentItem: TIdentifierListItem
): boolean;
begin
Result:=false;
{$IFDEF CTDEBUG}
DebugLn('TCodeToolManager.IdentItemCheckHasChilds A ');
{$ENDIF}
try
IdentItem.CheckHasChilds;
Result:=true;
except
on e: Exception do HandleException(e);
end;
end;
function TCodeToolManager.FindAbstractMethods(Code: TCodeBuffer; X, Y: integer;
out ListOfPCodeXYPosition: TFPList; SkipAbstractsInStartClass: boolean): boolean;
var
CursorPos: TCodeXYPosition;
begin
{$IFDEF CTDEBUG}
DebugLn('TCodeToolManager.FindRedefinitions A ',Code.Filename);
{$ENDIF}
Result:=false;
ListOfPCodeXYPosition:=nil;
if not InitCurCodeTool(Code) then exit;
CursorPos.X:=X;
CursorPos.Y:=Y;
CursorPos.Code:=Code;
try
Result:=FCurCodeTool.FindAbstractMethods(CursorPos,ListOfPCodeXYPosition,
SkipAbstractsInStartClass);
except
on e: Exception do Result:=HandleException(e);
end;
end;
function TCodeToolManager.GetValuesOfCaseVariable(Code: TCodeBuffer; X,
Y: integer; List: TStrings; WithTypeDefIfScoped: boolean): boolean;
var
CursorPos: TCodeXYPosition;
begin
{$IFDEF CTDEBUG}
DebugLn('TCodeToolManager.GetValuesOfCaseVariable A ',Code.Filename);
{$ENDIF}
Result:=false;
if not InitCurCodeTool(Code) then exit;
CursorPos.X:=X;
CursorPos.Y:=Y;
CursorPos.Code:=Code;
try
Result:=FCurCodeTool.GetValuesOfCaseVariable(CursorPos,List,WithTypeDefIfScoped);
except
on e: Exception do Result:=HandleException(e);
end;
end;
function TCodeToolManager.GatherOverloads(Code: TCodeBuffer; X, Y: integer; out
Graph: TDeclarationOverloadsGraph): boolean;
var
NewCode: TCodeBuffer;
NewX, NewY, NewTopLine: integer;
begin
{$IFDEF CTDEBUG}
DebugLn('TCodeToolManager.GatherOverloads A ',Code.Filename);
{$ENDIF}
Result:=false;
Graph:=nil;
if not FindMainDeclaration(Code,X,Y,NewCode,NewX,NewY,NewTopLine)
then begin
DebugLn('TCodeToolManager.GatherOverloads unable to FindMainDeclaration ',Code.Filename,' x=',dbgs(x),' y=',dbgs(y));
exit;
end;
if NewTopLine=0 then ;
if not InitCurCodeTool(Code) then exit;
try
Graph:=TDeclarationOverloadsGraph.Create;
Graph.OnGetCodeToolForBuffer:=@DoOnGetCodeToolForBuffer;
Result:=Graph.Init(NewCode,NewX,NewY);
except
on e: Exception do Result:=HandleException(e);
end;
end;
function TCodeToolManager.FindReferences(IdentifierCode: TCodeBuffer; X,
Y: integer; SearchInCode: TCodeBuffer; SkipComments: boolean;
var ListOfPCodeXYPosition: TFPList; var Cache: TFindIdentifierReferenceCache
): boolean;
var
CursorPos: TCodeXYPosition;
NewTopLine: integer;
ImplementationNode: TCodeTreeNode;
begin
Result:=false;
{$IFDEF CTDEBUG}
if Cache=nil then
DebugLn('TCodeToolManager.FindReferences A ',IdentifierCode.Filename,' x=',dbgs(x),' y=',dbgs(y),' SearchInCode=',SearchInCode.Filename)
else
debugln(['TCodeToolManager.FindReferences A SearchInCode=',SearchInCode.Filename]);
{$ENDIF}
ListOfPCodeXYPosition:=nil;
if Cache=nil then
Cache:=TFindIdentifierReferenceCache.Create;
if (Cache.SourcesChangeStep=SourceCache.ChangeStamp)
and (Cache.SourcesChangeStep<>CTInvalidChangeStamp64)
and (Cache.FilesChangeStep=FileStateCache.TimeStamp)
and (Cache.FilesChangeStep<>CTInvalidChangeStamp64)
and (Cache.InitValuesChangeStep=DefineTree.ChangeStep)
and (Cache.InitValuesChangeStep<>CTInvalidChangeStamp)
and (Cache.IdentifierCode=IdentifierCode) and (Cache.X=X) and (Cache.Y=Y)
then begin
//debugln(['TCodeToolManager.FindReferences cache valid']);
// all sources and values are the same => use cache
Result:=true;
end else begin
//debugln(['TCodeToolManager.FindReferences cache not valid']);
{debugln(['TCodeToolManager.FindReferences IdentifierCode=',Cache.IdentifierCode=IdentifierCode,
' X=',Cache.X=X,' Y=',Cache.Y=Y,
' SourcesChangeStep=',Cache.SourcesChangeStep=SourceCache.ChangeStamp,',',Cache.SourcesChangeStep=CTInvalidChangeStamp64,
' FilesChangeStep=',Cache.FilesChangeStep=FileStateCache.TimeStamp,',',Cache.FilesChangeStep=CTInvalidChangeStamp64,
' InitValuesChangeStep=',Cache.InitValuesChangeStep=DefineTree.ChangeStep,',',Cache.InitValuesChangeStep=CTInvalidChangeStamp,
'']);}
Cache.Clear;
Cache.IdentifierCode:=IdentifierCode;
Cache.X:=X;
Cache.Y:=Y;
Cache.SourcesChangeStep:=SourceCache.ChangeStamp;
Cache.FilesChangeStep:=FileStateCache.TimeStamp;
Cache.InitValuesChangeStep:=DefineTree.ChangeStep;
if not InitCurCodeTool(IdentifierCode) then exit;
CursorPos.X:=X;
CursorPos.Y:=Y;
CursorPos.Code:=IdentifierCode;
try
Result:=FCurCodeTool.FindDeclaration(CursorPos,[fsfFindMainDeclaration],
Cache.NewTool,Cache.NewNode,Cache.NewPos,NewTopLine);
except
on e: Exception do HandleException(e);
end;
if not Result then begin
debugln(['TCodeToolManager.FindReferences FCurCodeTool.FindDeclaration failed']);
exit;
end;
// check if scope can be limited
if Cache.NewTool<>nil then begin
Cache.IsPrivate:=(Cache.NewTool.GetSourceType in [ctnLibrary,ctnProgram]);
if not Cache.IsPrivate then begin
ImplementationNode:=Cache.NewTool.FindImplementationNode;
if (ImplementationNode<>nil)
and (Cache.NewNode.StartPos>=ImplementationNode.StartPos) then
Cache.IsPrivate:=true;
end;
if not Cache.IsPrivate then begin
if (Cache.NewNode.GetNodeOfTypes([ctnParameterList,ctnClassPrivate])<>nil) then
Cache.IsPrivate:=true;
end;
end;
end;
if (not Result) or (Cache.NewNode=nil) then begin
DebugLn('TCodeToolManager.FindReferences unable to FindDeclaration ',IdentifierCode.Filename,' x=',dbgs(x),' y=',dbgs(y));
exit;
end;
Result:=true;
if NewTopLine=0 then ;
if not InitCurCodeTool(SearchInCode) then exit;
if Cache.IsPrivate and (FCurCodeTool<>Cache.NewTool) then begin
//debugln(['TCodeToolManager.FindReferences identifier is not reachable from this unit => skipping search']);
exit(true);
end;
CursorPos:=Cache.NewPos;
{$IFDEF CTDEBUG}
DebugLn('TCodeToolManager.FindReferences Searching ',dbgs(FCurCodeTool.Scanner<>nil),' for reference to x=',dbgs(CursorPos.X),' y=',dbgs(CursorPos.Y),' ',CursorPos.Code.Filename);
{$ENDIF}
try
Result:=FCurCodeTool.FindReferences(CursorPos,SkipComments,
ListOfPCodeXYPosition);
except
on e: Exception do HandleException(e);
end;
{$IFDEF CTDEBUG}
DebugLn(['TCodeToolManager.FindReferences END ',Result]);
{$ENDIF}
end;
function TCodeToolManager.FindUnitReferences(UnitCode, TargetCode: TCodeBuffer;
SkipComments: boolean; var ListOfPCodeXYPosition: TFPList): boolean;
// finds unit name of UnitCode in unit of TargetCode
begin
Result:=false;
{$IFDEF CTDEBUG}
DebugLn('TCodeToolManager.FindUnitReferences A ',UnitCode.Filename,' Target=',TargetCode.Filename);
{$ENDIF}
ListOfPCodeXYPosition:=nil;
if not InitCurCodeTool(TargetCode) then exit;
try
Result:=FCurCodeTool.FindUnitReferences(UnitCode,SkipComments,
ListOfPCodeXYPosition);
except
on e: Exception do HandleException(e);
end;
{$IFDEF CTDEBUG}
DebugLn('TCodeToolManager.FindUnitReferences END ');
{$ENDIF}
end;
function TCodeToolManager.FindUsedUnitReferences(Code: TCodeBuffer; X,
Y: integer; SkipComments: boolean; out UsedUnitFilename: string;
var ListOfPCodeXYPosition: TFPList): boolean;
// finds in unit of Code all references of the unit at the uses clause at X,Y
var
CursorPos: TCodeXYPosition;
begin
Result:=false;
{$IFDEF CTDEBUG}
DebugLn('TCodeToolManager.FindUsedUnitReferences A ',Code.Filename,' X=',X,' Y=',Y,' SkipComments=',SkipComments);
{$ENDIF}
ListOfPCodeXYPosition:=nil;
if not InitCurCodeTool(Code) then exit;
CursorPos.X:=X;
CursorPos.Y:=Y;
CursorPos.Code:=Code;
try
FCurCodeTool.FindUsedUnitReferences(CursorPos,SkipComments,UsedUnitFilename,
ListOfPCodeXYPosition);
Result:=true;
except
on e: Exception do HandleException(e);
end;
{$IFDEF CTDEBUG}
DebugLn('TCodeToolManager.FindUnitReferences END ');
{$ENDIF}
end;
function TCodeToolManager.RenameIdentifier(TreeOfPCodeXYPosition: TAVLTree;
const OldIdentifier, NewIdentifier: string; DeclarationCode: TCodeBuffer;
DeclarationCaretXY: PPoint): boolean;
var
ANode, ANode2: TAVLTreeNode;
CurCodePos, LastCodePos: PCodeXYPosition;
IdentStartPos: integer;
IdentLen, IdentLenDiff: Integer;
SameLineCount: Integer;
i: Integer;
Code: TCodeBuffer;
begin
Result:=false;
{$IFDEF CTDEBUG}
DebugLn('TCodeToolManager.RenameIdentifier A Old=',OldIdentifier,' New=',NewIdentifier,' ',dbgs(TreeOfPCodeXYPosition<>nil));
{$ENDIF}
if TreeOfPCodeXYPosition=nil then begin
Result:=true;
exit;
end;
if not IsValidIdent(NewIdentifier) then exit;
ClearCurCodeTool;
SourceChangeCache.Clear;
CurCodePos := nil;
LastCodePos := nil;
SameLineCount := 0;
IdentLen:=length(OldIdentifier);
IdentLenDiff := length(NewIdentifier) - IdentLen;
if DeclarationCode = nil then
DeclarationCaretXY := nil;;
if DeclarationCaretXY = nil then
DeclarationCode := nil;;
// the tree is sorted for descending line code positions
// -> go from end of source to start of source, so that replacing does not
// change any CodeXYPosition not yet processed
ANode:=TreeOfPCodeXYPosition.FindLowest;
while ANode<>nil do begin
// next position
CurCodePos:=PCodeXYPosition(ANode.Data);
Code:=CurCodePos^.Code;
Code.LineColToPosition(CurCodePos^.Y,CurCodePos^.X,IdentStartPos);
DebugLn('TCodeToolManager.RenameIdentifier File ',Code.Filename,' Line=',dbgs(CurCodePos^.Y),' Col=',dbgs(CurCodePos^.X),' Identifier=',GetIdentifier(@Code.Source[IdentStartPos]));
// search absolute position in source
if IdentStartPos<1 then begin
SetError(20170421203205,Code, CurCodePos^.Y, CurCodePos^.X, ctsPositionNotInSource);
exit;
end;
// check if old identifier is there
if CompareIdentifiers(@Code.Source[IdentStartPos],PChar(Pointer(OldIdentifier)))<>0
then begin
debugln(['TCodeToolManager.RenameIdentifier CONSISTENCY ERROR ',Dbgs(CurCodePos^),' ']);
SetError(20170421203210,CurCodePos^.Code,CurCodePos^.Y,CurCodePos^.X,
Format(ctsStrExpectedButAtomFound,[OldIdentifier,
GetIdentifier(@Code.Source[IdentStartPos])])
);
exit;
end;
// change if needed
if CompareIdentifiersCaseSensitive(@Code.Source[IdentStartPos],
PChar(Pointer(NewIdentifier)))<>0
then begin
DebugLn('TCodeToolManager.RenameIdentifier Change ');
SourceChangeCache.ReplaceEx(gtNone,gtNone,1,1,Code,
IdentStartPos,IdentStartPos+IdentLen,NewIdentifier);
if (DeclarationCode = Code) and (CurCodePos^.Y = DeclarationCaretXY^.Y) and
(CurCodePos^.X < DeclarationCaretXY^.X)
then
DeclarationCaretXY^.X := DeclarationCaretXY^.X + IdentLenDiff;
if (LastCodePos <> nil) and (CurCodePos^.Y = LastCodePos^.Y) and
(CurCodePos^.Code = LastCodePos^.Code)
then
inc(SameLineCount);
end else begin
DebugLn('TCodeToolManager.RenameIdentifier KEPT ',GetIdentifier(@Code.Source[IdentStartPos]));
end;
LastCodePos := CurCodePos;
ANode2 := ANode;
ANode:=TreeOfPCodeXYPosition.FindSuccessor(ANode);
if (ANode = nil) or (PCodeXYPosition(ANode.Data)^.Code <> LastCodePos^.Code) or
(PCodeXYPosition(ANode.Data)^.Y <> LastCodePos^.Y)
then begin
if (SameLineCount > 0) then begin
for i := 1 to SameLineCount do begin
ANode2 := TreeOfPCodeXYPosition.FindPrecessor(ANode2);
PCodeXYPosition(ANode2.Data)^.X := PCodeXYPosition(ANode2.Data)^.X + i * IdentLenDiff;
end;
end;
SameLineCount := 0;
end;
end;
// apply
DebugLn('TCodeToolManager.RenameIdentifier Apply');
if not SourceChangeCache.Apply then exit;
//DebugLn('TCodeToolManager.RenameIdentifier Success');
Result:=true;
{$IFDEF CTDEBUG}
DebugLn('TCodeToolManager.RenameIdentifier END ');
{$ENDIF}
end;
function TCodeToolManager.ReplaceWord(Code: TCodeBuffer; const OldWord,
NewWord: string; ChangeStrings: boolean): boolean;
begin
Result:=false;
{$IFDEF CTDEBUG}
DebugLn('TCodeToolManager.ReplaceWord A ',Code.Filename,' OldWord="',OldWord,'" NewWord="',NewWord,'"');
{$ENDIF}
if not InitCurCodeTool(Code) then exit;
try
Result:=FCurCodeTool.ReplaceWord(OldWord, NewWord, ChangeStrings,
SourceChangeCache);
except
on e: Exception do HandleException(e);
end;
end;
function TCodeToolManager.RemoveIdentifierDefinition(Code: TCodeBuffer; X,
Y: integer): boolean;
var
CursorPos: TCodeXYPosition;
begin
Result:=false;
{$IFDEF CTDEBUG}
DebugLn(['TCodeToolManager.RemoveIdentifierDefinition A ',Code.Filename,' X=',X,' Y=',Y]);
{$ENDIF}
if not InitCurCodeTool(Code) then exit;
CursorPos.X:=X;
CursorPos.Y:=Y;
CursorPos.Code:=Code;
try
Result:=FCurCodeTool.RemoveIdentifierDefinition(CursorPos,SourceChangeCache);
except
on e: Exception do HandleException(e);
end;
end;
function TCodeToolManager.RemoveWithBlock(Code: TCodeBuffer; X, Y: integer
): boolean;
var
CursorPos: TCodeXYPosition;
begin
Result:=false;
{$IFDEF CTDEBUG}
DebugLn(['TCodeToolManager.RemoveWithBlock A ',Code.Filename,' X=',X,' Y=',Y]);
{$ENDIF}
if not InitCurCodeTool(Code) then exit;
CursorPos.X:=X;
CursorPos.Y:=Y;
CursorPos.Code:=Code;
try
Result:=FCurCodeTool.RemoveWithBlock(CursorPos,SourceChangeCache);
except
on e: Exception do HandleException(e);
end;
end;
function TCodeToolManager.AddWithBlock(Code: TCodeBuffer; X1, Y1, X2,
Y2: integer; const WithExpr: string; Candidates: TStrings): boolean;
var
StartPos, EndPos: TCodeXYPosition;
begin
Result:=false;
{$IFDEF CTDEBUG}
DebugLn(['TCodeToolManager.AddWithBlock A ',Code.Filename,' X1=',X1,' Y1=',Y1,' X2=',X2,' Y2=',Y2,' WithExpr="',WithExpr,'"']);
{$ENDIF}
if not InitCurCodeTool(Code) then exit;
StartPos.X:=X1;
StartPos.Y:=Y1;
StartPos.Code:=Code;
EndPos.X:=X2;
EndPos.Y:=Y2;
EndPos.Code:=Code;
try
Result:=FCurCodeTool.AddWithBlock(StartPos,EndPos,WithExpr,Candidates,
SourceChangeCache);
except
on e: Exception do HandleException(e);
end;
end;
function TCodeToolManager.ChangeParamList(Code: TCodeBuffer;
Changes: TObjectList; var ProcPos: TCodeXYPosition;
TreeOfPCodeXYPosition: TAVLTree): boolean;
begin
Result:=false;
{$IFDEF CTDEBUG}
DebugLn('TCodeToolManager.ChangeParamList A ',Code.Filename);
{$ENDIF}
if not InitCurCodeTool(Code) then exit;
try
Result:=FCurCodeTool.ChangeParamList(Changes,ProcPos,TreeOfPCodeXYPosition,
SourceChangeCache);
except
on e: Exception do HandleException(e);
end;
end;
function TCodeToolManager.GatherResourceStringSections(Code: TCodeBuffer;
X, Y: integer; CodePositions: TCodeXYPositions): boolean;
var
CursorPos: TCodeXYPosition;
begin
Result:=false;
{$IFDEF CTDEBUG}
DebugLn('TCodeToolManager.GatherResourceStringSections A ',Code.Filename,' x=',dbgs(x),' y=',dbgs(y));
{$ENDIF}
if not InitCurCodeTool(Code) then exit;
CursorPos.X:=X;
CursorPos.Y:=Y;
CursorPos.Code:=Code;
if CodePositions=nil then begin
ClearPositions;
CodePositions:=Positions;
end;
try
Result:=FCurCodeTool.GatherResourceStringSections(CursorPos,CodePositions);
except
on e: Exception do HandleException(e);
end;
end;
function TCodeToolManager.IdentifierExistsInResourceStringSection(
Code: TCodeBuffer; X, Y: integer; const ResStrIdentifier: string): boolean;
var
CursorPos: TCodeXYPosition;
begin
Result:=false;
{$IFDEF CTDEBUG}
DebugLn('TCodeToolManager.IdentifierExistsInResourceStringSection A ',Code.Filename,' x=',dbgs(x),' y=',dbgs(y));
{$ENDIF}
if not InitCurCodeTool(Code) then exit;
CursorPos.X:=X;
CursorPos.Y:=Y;
CursorPos.Code:=Code;
try
Result:=FCurCodeTool.IdentifierExistsInResourceStringSection(CursorPos,
ResStrIdentifier);
except
on e: Exception do HandleException(e);
end;
end;
function TCodeToolManager.CreateIdentifierFromStringConst(
StartCode: TCodeBuffer; StartX, StartY: integer;
EndCode: TCodeBuffer; EndX, EndY: integer;
out Identifier: string; MaxLen: integer): boolean;
var
StartCursorPos, EndCursorPos: TCodeXYPosition;
begin
Result:=false;
{$IFDEF CTDEBUG}
DebugLn('TCodeToolManager.CreateIdentifierFromStringConst A ',StartCode.Filename,' x=',dbgs(StartX),' y=',dbgs(StartY));
{$ENDIF}
if not InitCurCodeTool(StartCode) then exit;
StartCursorPos.X:=StartX;
StartCursorPos.Y:=StartY;
StartCursorPos.Code:=StartCode;
EndCursorPos.X:=EndX;
EndCursorPos.Y:=EndY;
EndCursorPos.Code:=EndCode;
Identifier:='';
try
Result:=FCurCodeTool.CreateIdentifierFromStringConst(
StartCursorPos,EndCursorPos,Identifier,MaxLen);
except
on e: Exception do HandleException(e);
end;
end;
function TCodeToolManager.StringConstToFormatString(
StartCode: TCodeBuffer; StartX, StartY: integer;
EndCode: TCodeBuffer; EndX, EndY: integer;
out FormatStringConstant, FormatParameters: string;
out StartInStringConst, EndInStringConst: boolean): boolean;
var
StartCursorPos, EndCursorPos: TCodeXYPosition;
begin
Result:=false;
{$IFDEF CTDEBUG}
DebugLn('TCodeToolManager.StringConstToFormatString A ',StartCode.Filename,' x=',dbgs(StartX),' y=',dbgs(StartY));
{$ENDIF}
if not InitCurCodeTool(StartCode) then exit;
StartCursorPos.X:=StartX;
StartCursorPos.Y:=StartY;
StartCursorPos.Code:=StartCode;
EndCursorPos.X:=EndX;
EndCursorPos.Y:=EndY;
EndCursorPos.Code:=EndCode;
try
Result:=FCurCodeTool.StringConstToFormatString(
StartCursorPos,EndCursorPos,FormatStringConstant,FormatParameters,
StartInStringConst,EndInStringConst);
except
on e: Exception do HandleException(e);
end;
end;
function TCodeToolManager.GatherResourceStringsWithValue(
SectionCode: TCodeBuffer; SectionX, SectionY: integer;
const StringValue: string; CodePositions: TCodeXYPositions): boolean;
var
CursorPos: TCodeXYPosition;
begin
Result:=false;
{$IFDEF CTDEBUG}
DebugLn('TCodeToolManager.GatherResourceStringsWithValue A ',SectionCode.Filename,' x=',dbgs(SectionX),' y=',dbgs(SectionY));
{$ENDIF}
if not InitCurCodeTool(SectionCode) then exit;
CursorPos.X:=SectionX;
CursorPos.Y:=SectionY;
CursorPos.Code:=SectionCode;
if CodePositions=nil then begin
ClearPositions;
CodePositions:=Positions;
end;
try
Result:=FCurCodeTool.GatherResourceStringsWithValue(CursorPos,StringValue,
CodePositions);
except
on e: Exception do HandleException(e);
end;
end;
function TCodeToolManager.AddResourcestring(
CursorCode: TCodeBuffer; X,Y: integer;
SectionCode: TCodeBuffer; SectionX, SectionY: integer;
const NewIdentifier, NewValue: string;
InsertPolicy: TResourcestringInsertPolicy): boolean;
var
CursorPos, SectionPos, NearestPos: TCodeXYPosition;
begin
Result:=false;
{$IFDEF CTDEBUG}
DebugLn('TCodeToolManager.AddResourcestring A ',SectionCode.Filename,' x=',dbgs(SectionX),' y=',dbgs(SectionY));
{$ENDIF}
if not InitCurCodeTool(SectionCode) then exit;
SectionPos.X:=SectionX;
SectionPos.Y:=SectionY;
SectionPos.Code:=SectionCode;
try
NearestPos.Code:=nil;
if InsertPolicy=rsipContext then begin
CursorPos.X:=X;
CursorPos.Y:=Y;
CursorPos.Code:=CursorCode;
Result:=FCurCodeTool.FindNearestResourceString(CursorPos, SectionPos,
NearestPos);
if not Result then exit;
end;
Result:=FCurCodeTool.AddResourcestring(SectionPos, NewIdentifier, NewValue,
InsertPolicy,NearestPos,SourceChangeCache);
except
on e: Exception do HandleException(e);
end;
end;
procedure TCodeToolManager.ImproveStringConstantStart(const ACode: string;
var StartPos: integer);
begin
BasicCodeTools.ImproveStringConstantStart(ACode,StartPos);
end;
procedure TCodeToolManager.ImproveStringConstantEnd(const ACode: string;
var EndPos: integer);
begin
BasicCodeTools.ImproveStringConstantEnd(ACode,EndPos);
end;
function TCodeToolManager.GetStringConstBounds(Code: TCodeBuffer; X,
Y: integer; out StartCode: TCodeBuffer; out StartX, StartY: integer; out
EndCode: TCodeBuffer; out EndX, EndY: integer; ResolveComments: boolean
): boolean;
var
CursorPos, StartPos, EndPos: TCodeXYPosition;
begin
Result:=false;
{$IFDEF CTDEBUG}
DebugLn('TCodeToolManager.GetStringConstBounds A ',Code.Filename);
{$ENDIF}
if not InitCurCodeTool(Code) then exit;
CursorPos.X:=X;
CursorPos.Y:=Y;
CursorPos.Code:=Code;
try
Result:=FCurCodeTool.GetStringConstBounds(CursorPos,StartPos,EndPos,
ResolveComments);
if Result then begin
StartCode:=StartPos.Code;
StartX:=StartPos.X;
StartY:=StartPos.Y;
EndCode:=EndPos.Code;
EndX:=EndPos.X;
EndY:=EndPos.Y;
end;
except
on e: Exception do Result:=HandleException(e);
end;
end;
function TCodeToolManager.InsertStatements(
InsertPos: TInsertStatementPosDescription; const Statements: string): boolean;
begin
Result:=false;
{$IFDEF CTDEBUG}
DebugLn('TCodeToolManager.InsertStatements A ',Code.Filename,' Line=',Y,',Col=',X);
{$ENDIF}
if not InitCurCodeTool(InsertPos.CodeXYPos.Code) then exit;
try
Result:=FCurCodeTool.InsertStatements(InsertPos,Statements,SourceChangeCache);
except
on e: Exception do HandleException(e);
end;
end;
function TCodeToolManager.AddProcModifier(Code: TCodeBuffer; X, Y: integer;
const aModifier: string): boolean;
var
CursorPos: TCodeXYPosition;
begin
Result:=false;
{$IFDEF CTDEBUG}
DebugLn('TCodeToolManager.ExtractOperand A ',Code.Filename);
{$ENDIF}
if not InitCurCodeTool(Code) then exit;
CursorPos.X:=X;
CursorPos.Y:=Y;
CursorPos.Code:=Code;
try
Result:=FCurCodeTool.AddProcModifier(CursorPos,aModifier,SourceChangeCache);
except
on e: Exception do HandleException(e);
end;
end;
function TCodeToolManager.ExtractOperand(Code: TCodeBuffer; X, Y: integer; out
Operand: string; WithPostTokens, WithAsOperator,
WithoutTrailingPoints: boolean): boolean;
var
CursorPos: TCodeXYPosition;
begin
Result:=false;
{$IFDEF CTDEBUG}
DebugLn('TCodeToolManager.ExtractOperand A ',Code.Filename);
{$ENDIF}
Operand:='';
if not InitCurCodeTool(Code) then exit;
CursorPos.X:=X;
CursorPos.Y:=Y;
CursorPos.Code:=Code;
try
Result:=FCurCodeTool.ExtractOperand(CursorPos,Operand,
WithPostTokens,WithAsOperator,WithoutTrailingPoints);
except
on e: Exception do HandleException(e);
end;
end;
function TCodeToolManager.GetExpandedOperand(Code: TCodeBuffer; X, Y: Integer;
out Operand: string; ResolveProperty: Boolean): Boolean;
var
CursorPos: TCodeXYPosition;
begin
Result := False;
Operand := '';
if not InitCurCodeTool(Code) then Exit;
CursorPos.X := X;
CursorPos.Y := Y;
CursorPos.Code := Code;
try
Result := FCurCodeTool.GetExpandedOperand(CursorPos, Operand, ResolveProperty);
except
on e: Exception do HandleException(e);
end;
end;
function TCodeToolManager.GuessMisplacedIfdefEndif(Code: TCodeBuffer; X,
Y: integer; out NewCode: TCodeBuffer; out NewX, NewY, NewTopLine: integer
): boolean;
var
CursorPos: TCodeXYPosition;
NewPos: TCodeXYPosition;
begin
Result:=false;
{$IFDEF CTDEBUG}
DebugLn('TCodeToolManager.GuessMisplacedIfdefEndif A ',Code.Filename);
{$ENDIF}
if not InitCurCodeTool(Code) then exit;
CursorPos.X:=X;
CursorPos.Y:=Y;
CursorPos.Code:=Code;
try
Result:=FCurCodeTool.GuessMisplacedIfdefEndif(CursorPos,NewPos,NewTopLine);
if Result then begin
NewX:=NewPos.X;
NewY:=NewPos.Y;
NewCode:=NewPos.Code;
end;
except
on e: Exception do Result:=HandleException(e);
end;
end;
function TCodeToolManager.FindEnclosingIncludeDirective(Code: TCodeBuffer; X,
Y: integer; out NewCode: TCodeBuffer; out NewX, NewY, NewTopLine: integer
): boolean;
var
CursorPos: TCodeXYPosition;
NewPos: TCodeXYPosition;
begin
Result:=false;
{$IFDEF CTDEBUG}
DebugLn('TCodeToolManager.FindEnclosingIncludeDirective A ',Code.Filename);
{$ENDIF}
if not InitCurCodeTool(Code) then exit;
CursorPos.X:=X;
CursorPos.Y:=Y;
CursorPos.Code:=Code;
try
Result:=FCurCodeTool.FindEnclosingIncludeDirective(CursorPos,
NewPos,NewTopLine);
if Result then begin
NewX:=NewPos.X;
NewY:=NewPos.Y;
NewCode:=NewPos.Code;
end;
except
on e: Exception do Result:=HandleException(e);
end;
end;
function TCodeToolManager.FindResourceDirective(Code: TCodeBuffer; StartX,
StartY: integer;
out NewCode: TCodeBuffer; out NewX, NewY, NewTopLine: integer;
const Filename: string; SearchInCleanSrc: boolean): boolean;
var
CursorPos: TCodeXYPosition;
NewPos: TCodeXYPosition;
p: integer;
ADirectivesTool: TDirectivesTool;
begin
Result:=false;
{$IFDEF CTDEBUG}
DebugLn('TCodeToolManager.FindResourceDirective A ',Code.Filename);
{$ENDIF}
NewCode:=nil;
NewX:=0;
NewY:=0;
NewTopLine:=0;
if SearchInCleanSrc then begin
if not InitCurCodeTool(Code) then exit;
CursorPos.X:=StartX;
CursorPos.Y:=StartY;
CursorPos.Code:=Code;
try
Result:=FCurCodeTool.FindResourceDirective(CursorPos,NewPos,NewTopLine,
Filename);
if Result then begin
NewX:=NewPos.X;
NewY:=NewPos.Y;
NewCode:=NewPos.Code;
end;
except
on e: Exception do Result:=HandleException(e);
end;
end else begin
try
if not InitCurDirectivesTool(Code) then exit;
ADirectivesTool:=FCurDirectivesTool;
FCurDirectivesTool.Parse;
Code.LineColToPosition(StartY,StartX,p);
Result:=ADirectivesTool.NodeStartToCodePos(
ADirectivesTool.FindResourceDirective(Filename,p),
CursorPos);
NewCode:=CursorPos.Code;
NewX:=CursorPos.X;
NewY:=CursorPos.Y;
NewTopLine:=NewY;
except
on e: Exception do Result:=HandleException(e);
end;
end;
end;
function TCodeToolManager.AddResourceDirective(Code: TCodeBuffer;
const Filename: string; SearchInCleanSrc: boolean; const NewSrc: string
): boolean;
var
Tree: TCompilerDirectivesTree;
Node: TCodeTreeNode;
begin
Result:=false;
{$IFDEF CTDEBUG}
DebugLn('TCodeToolManager.AddResourceDirective A ',Code.Filename,' Filename=',Filename);
{$ENDIF}
if SearchInCleanSrc then begin
if not InitCurCodeTool(Code) then exit;
try
Result:=FCurCodeTool.AddResourceDirective(Filename,SourceChangeCache,NewSrc);
except
on e: Exception do Result:=HandleException(e);
end;
end else begin
try
Tree:=TCompilerDirectivesTree.Create;
try
Tree.Parse(Code,GetNestedCommentsFlagForFile(Code.Filename));
Node:=Tree.FindResourceDirective(Filename);
if Node=nil then
Result:=AddResourceDirective(Code,Filename,true,NewSrc)
else
Result:=true;
finally
Tree.Free;
end;
except
on e: Exception do Result:=HandleException(e);
end;
end;
end;
function TCodeToolManager.FindIncludeDirective(Code: TCodeBuffer; StartX,
StartY: integer; out NewCode: TCodeBuffer; out NewX, NewY,
NewTopLine: integer; const Filename: string; SearchInCleanSrc: boolean
): boolean;
var
CursorPos: TCodeXYPosition;
NewPos: TCodeXYPosition;
Tree: TCompilerDirectivesTree;
p: integer;
begin
Result:=false;
{$IFDEF CTDEBUG}
DebugLn('TCodeToolManager.FindIncludeDirective A ',Code.Filename);
{$ENDIF}
NewCode:=nil;
NewX:=0;
NewY:=0;
NewTopLine:=0;
if SearchInCleanSrc then begin
if not InitCurCodeTool(Code) then exit;
CursorPos.X:=StartX;
CursorPos.Y:=StartY;
CursorPos.Code:=Code;
try
Result:=FCurCodeTool.FindIncludeDirective(CursorPos,NewPos,NewTopLine,
Filename);
if Result then begin
NewX:=NewPos.X;
NewY:=NewPos.Y;
NewCode:=NewPos.Code;
end;
except
on e: Exception do Result:=HandleException(e);
end;
end else begin
try
Tree:=TCompilerDirectivesTree.Create;
try
Tree.Parse(Code,GetNestedCommentsFlagForFile(Code.Filename));
Code.LineColToPosition(StartY,StartX,p);
Result:=Tree.NodeStartToCodePos(Tree.FindIncludeDirective(Filename,p),
CursorPos);
NewCode:=CursorPos.Code;
NewX:=CursorPos.X;
NewY:=CursorPos.Y;
NewTopLine:=NewY;
finally
Tree.Free;
end;
except
on e: Exception do Result:=HandleException(e);
end;
end;
end;
function TCodeToolManager.AddIncludeDirectiveForInit(Code: TCodeBuffer;
const Filename: string; const NewSrc: string): boolean;
begin
Result:=false;
{$IFDEF CTDEBUG}
DebugLn('TCodeToolManager.AddIncludeDirectiveForInit A ',Code.Filename,' Filename=',Filename);
{$ENDIF}
if not InitCurCodeTool(Code) then exit;
try
Result:=FCurCodeTool.AddIncludeDirectiveForInit(Filename,SourceChangeCache,NewSrc);
except
on e: Exception do Result:=HandleException(e);
end;
end;
function TCodeToolManager.AddUnitWarnDirective(Code: TCodeBuffer; WarnID,
Comment: string; TurnOn: boolean): boolean;
begin
Result:=false;
{$IFDEF CTDEBUG}
DebugLn(['TCodeToolManager.AddUnitWarnDirective A ',Code.Filename,' aParam="',aParam,'" TurnOn=',TurnOn]);
{$ENDIF}
if not InitCurCodeTool(Code) then exit;
try
Result:=FCurCodeTool.AddUnitWarnDirective(WarnID,Comment,TurnOn,SourceChangeCache);
except
on e: Exception do Result:=HandleException(e);
end;
end;
function TCodeToolManager.RemoveDirective(Code: TCodeBuffer; NewX,
NewY: integer; RemoveEmptyIFs: boolean): boolean;
var
Tree: TCompilerDirectivesTree;
p: integer;
Node: TCodeTreeNode;
Changed: boolean;
ParentNode: TCodeTreeNode;
begin
Result:=false;
{$IFDEF CTDEBUG}
DebugLn('TCodeToolManager.RemoveDirective A ',Code.Filename);
{$ENDIF}
try
Code.LineColToPosition(NewY,NewX,p);
if (p<1) or (p>Code.SourceLength) then exit;
Tree:=TCompilerDirectivesTree.Create;
try
Tree.Parse(Code,GetNestedCommentsFlagForFile(Code.Filename));
Node:=Tree.FindNodeAtPos(p);
if Node=nil then exit;
ParentNode:=Node.Parent;
Changed:=false;
Tree.DisableNode(Node,Changed,true);
if RemoveEmptyIFs and (ParentNode<>nil) and Tree.NodeIsEmpty(ParentNode) then
Tree.DisableNode(ParentNode,Changed,true);
Result:=Changed;
finally
Tree.Free;
end;
except
on e: Exception do Result:=HandleException(e);
end;
end;
function TCodeToolManager.FixIncludeFilenames(Code: TCodeBuffer;
Recursive: boolean; out MissingIncludeFilesCodeXYPos: TFPList): boolean;
procedure CreateErrorForMissingIncludeFile;
var
CodePos: PCodeXYPosition;
begin
ClearError;
CodePos:=PCodeXYPosition(MissingIncludeFilesCodeXYPos[0]);
fErrorCode:=CodePos^.Code;
fErrorLine:=CodePos^.Y;
fErrorColumn:=CodePos^.X;
FErrorId:=20170421202903;
FErrorMsg:='missing include file';
end;
var
FoundIncludeFiles: TStrings;
i: Integer;
AFilename: string;
ToFixIncludeFiles: TStringList;
FixedIncludeFiles: TStringList;
begin
Result:=false;
{$IFDEF CTDEBUG}
DebugLn('TCodeToolManager.FixIncludeFilenames A ',Code.Filename,' Recursive=', DbgS(Recursive));
{$ENDIF}
MissingIncludeFilesCodeXYPos:=nil;
if not InitCurCodeTool(Code) then exit;
try
FixedIncludeFiles:=nil;
ToFixIncludeFiles:=TStringList.Create;
try
ToFixIncludeFiles.Add(Code.Filename);
while ToFixIncludeFiles.Count>0 do begin
// get next include file
AFilename:=ToFixIncludeFiles[ToFixIncludeFiles.Count-1];
ToFixIncludeFiles.Delete(ToFixIncludeFiles.Count-1);
Code:=LoadFile(AFilename,false,false);
if Code=nil then begin
raise ECodeToolError.Create(FCurCodeTool,20170421202139,
'unable to read file "'+AFilename+'"');
end;
// fix file
FoundIncludeFiles:=nil;
try
Result:=FCurCodeTool.FixIncludeFilenames(Code,SourceChangeCache,
FoundIncludeFiles,MissingIncludeFilesCodeXYPos);
if (MissingIncludeFilesCodeXYPos<>nil)
and (MissingIncludeFilesCodeXYPos.Count>0) then begin
DebugLn('TCodeToolManager.FixIncludeFilenames Missing: ',dbgs(MissingIncludeFilesCodeXYPos.Count));
Result:=false;
CreateErrorForMissingIncludeFile;
exit;
end;
if not Recursive then begin
// check only main file -> stop
exit;
end;
// remember, that the file has been fixed to avoid cycles
if FixedIncludeFiles=nil then
FixedIncludeFiles:=TStringList.Create;
FixedIncludeFiles.Add(Code.Filename);
// add new include files to stack
if FoundIncludeFiles<>nil then begin
for i:=0 to FoundIncludeFiles.Count-1 do begin
AFilename:=FoundIncludeFiles[i];
if ((FixedIncludeFiles=nil)
or (FixedIncludeFiles.IndexOf(AFilename)<0))
and (ToFixIncludeFiles.IndexOf(AFilename)<0) then begin
ToFixIncludeFiles.Add(AFilename);
end;
end;
end;
//DebugLn('TCodeToolManager.FixIncludeFilenames FixedIncludeFiles=',FixedIncludeFiles.Text,' ToFixIncludeFiles=',ToFixIncludeFiles.Text);
finally
FoundIncludeFiles.Free;
end;
end;
finally
FixedIncludeFiles.Free;
ToFixIncludeFiles.Free;
end;
except
on e: Exception do Result:=HandleException(e);
end;
end;
function TCodeToolManager.FixMissingH2PasDirectives(Code: TCodeBuffer;
var Changed: boolean): boolean;
begin
Result:=false;
try
if InitCurDirectivesTool(Code) then begin
FCurDirectivesTool.Parse;
FCurDirectivesTool.FixMissingH2PasDirectives(Changed);
Result:=true;
end;
except
on e: Exception do Result:=HandleException(e);
end;
end;
function TCodeToolManager.ReduceCompilerDirectives(Code: TCodeBuffer;
Undefines, Defines: TStrings; var Changed: boolean): boolean;
begin
Result:=false;
try
if InitCurDirectivesTool(Code) then begin
FCurDirectivesTool.Parse;
FCurDirectivesTool.ReduceCompilerDirectives(Undefines,Defines,Changed);
Result:=true;
end;
except
on e: Exception do Result:=HandleException(e);
end;
end;
function TCodeToolManager.IsKeyword(Code: TCodeBuffer; const KeyWord: string
): boolean;
begin
Result:=false;
{$IFDEF CTDEBUG}
DebugLn('TCodeToolManager.IsKeyword A ',Code.Filename,' Keyword=',KeyWord);
{$ENDIF}
if not InitCurCodeTool(Code) then exit;
try
Result:=FCurCodeTool.StringIsKeyWord(KeyWord);
except
on e: Exception do Result:=HandleException(e);
end;
end;
function TCodeToolManager.ExtractCodeWithoutComments(Code: TCodeBuffer;
KeepDirectives: boolean; KeepVerbosityDirectives: boolean): string;
begin
Result:=CleanCodeFromComments(Code.Source,
GetNestedCommentsFlagForFile(Code.Filename),KeepDirectives,
KeepVerbosityDirectives);
end;
function TCodeToolManager.GetPasDocComments(Code: TCodeBuffer; X, Y: integer;
out ListOfPCodeXYPosition: TFPList): boolean;
var
CursorPos: TCodeXYPosition;
begin
Result:=false;
{$IFDEF CTDEBUG}
DebugLn('TCodeToolManager.GetPasDocComments A ',Code.Filename);
{$ENDIF}
ListOfPCodeXYPosition:=nil;
if not InitCurCodeTool(Code) then exit;
CursorPos.X:=X;
CursorPos.Y:=Y;
CursorPos.Code:=Code;
{$IFDEF CTDEBUG}
DebugLn('TCodeToolManager.GetPasDocComments B ',dbgs(FCurCodeTool.Scanner<>nil));
{$ENDIF}
try
Result:=FCurCodeTool.GetPasDocComments(CursorPos,true,ListOfPCodeXYPosition);
except
on e: Exception do Result:=HandleException(e);
end;
{$IFDEF CTDEBUG}
DebugLn('TCodeToolManager.GetPasDocComments END ');
{$ENDIF}
end;
function TCodeToolManager.FindBlockCounterPart(Code: TCodeBuffer;
X, Y: integer; out NewCode: TCodeBuffer; out NewX, NewY, NewTopLine: integer
): boolean;
var
CursorPos: TCodeXYPosition;
NewPos: TCodeXYPosition;
begin
Result:=false;
{$IFDEF CTDEBUG}
DebugLn('TCodeToolManager.FindBlockCounterPart A ',Code.Filename);
{$ENDIF}
NewCode:=nil;
if not InitCurCodeTool(Code) then exit;
CursorPos.X:=X;
CursorPos.Y:=Y;
CursorPos.Code:=Code;
{$IFDEF CTDEBUG}
DebugLn('TCodeToolManager.FindBlockCounterPart B ',dbgs(FCurCodeTool.Scanner<>nil));
{$ENDIF}
try
Result:=FCurCodeTool.FindBlockCounterPart(CursorPos,NewPos,NewTopLine);
if Result then begin
NewX:=NewPos.X;
NewY:=NewPos.Y;
NewCode:=NewPos.Code;
end;
except
on e: Exception do Result:=HandleException(e);
end;
{$IFDEF CTDEBUG}
DebugLn('TCodeToolManager.FindBlockCounterPart END ');
{$ENDIF}
end;
function TCodeToolManager.FindBlockStart(Code: TCodeBuffer; X, Y: integer; out
NewCode: TCodeBuffer; out NewX, NewY, NewTopLine: integer; SkipStart: boolean
): boolean;
var
CursorPos: TCodeXYPosition;
NewPos: TCodeXYPosition;
begin
Result:=false;
{$IFDEF CTDEBUG}
DebugLn('TCodeToolManager.FindBlockStart A ',Code.Filename);
{$ENDIF}
if not InitCurCodeTool(Code) then exit;
CursorPos.X:=X;
CursorPos.Y:=Y;
CursorPos.Code:=Code;
{$IFDEF CTDEBUG}
DebugLn('TCodeToolManager.FindBlockStart B ',dbgs(FCurCodeTool.Scanner<>nil));
{$ENDIF}
try
Result:=FCurCodeTool.FindBlockStart(CursorPos,NewPos,NewTopLine,SkipStart);
if Result then begin
NewX:=NewPos.X;
NewY:=NewPos.Y;
NewCode:=NewPos.Code;
end;
except
on e: Exception do Result:=HandleException(e);
end;
{$IFDEF CTDEBUG}
DebugLn('TCodeToolManager.FindBlockStart END ');
{$ENDIF}
end;
function TCodeToolManager.GuessUnclosedBlock(Code: TCodeBuffer; X, Y: integer;
out NewCode: TCodeBuffer; out NewX, NewY, NewTopLine: integer): boolean;
var
CursorPos: TCodeXYPosition;
NewPos: TCodeXYPosition;
begin
Result:=false;
{$IFDEF CTDEBUG}
DebugLn('TCodeToolManager.GuessUnclosedBlock A ',Code.Filename);
{$ENDIF}
if not InitCurCodeTool(Code) then exit;
CursorPos.X:=X;
CursorPos.Y:=Y;
CursorPos.Code:=Code;
{$IFDEF CTDEBUG}
DebugLn('TCodeToolManager.GuessUnclosedBlock B ',dbgs(FCurCodeTool.Scanner<>nil));
{$ENDIF}
try
Result:=FCurCodeTool.GuessUnclosedBlock(CursorPos,NewPos,NewTopLine);
if Result then begin
NewX:=NewPos.X;
NewY:=NewPos.Y;
NewCode:=NewPos.Code;
end;
except
on e: Exception do Result:=HandleException(e);
end;
{$IFDEF CTDEBUG}
DebugLn('TCodeToolManager.GuessUnclosedBlock END ');
{$ENDIF}
end;
function TCodeToolManager.CompleteBlock(Code: TCodeBuffer; X, Y: integer;
OnlyIfCursorBlockIndented: boolean): boolean;
var
NewCode: TCodeBuffer;
NewX, NewY, NewTopLine: integer;
begin
Result:=CompleteBlock(Code,X,Y,OnlyIfCursorBlockIndented,
NewCode,NewX,NewY,NewTopLine);
if (NewCode=nil) and (NewX<0) and (NewY<0) and (NewTopLine<1) then ;
end;
function TCodeToolManager.CompleteBlock(Code: TCodeBuffer; X, Y: integer;
OnlyIfCursorBlockIndented: boolean;
out NewCode: TCodeBuffer; out NewX, NewY, NewTopLine: integer): boolean;
var
CursorPos, NewPos: TCodeXYPosition;
begin
Result:=false;
NewCode:=Code;
NewX:=X;
NewY:=Y;
NewTopLine:=-1;
{$IFDEF CTDEBUG}
DebugLn('TCodeToolManager.CompleteBlock A ',Code.Filename,' x=',dbgs(X),' y=',dbgs(Y));
{$ENDIF}
if not InitCurCodeTool(Code) then exit;
CursorPos.X:=X;
CursorPos.Y:=Y;
CursorPos.Code:=Code;
try
Result:=FCurCodeTool.CompleteBlock(CursorPos,SourceChangeCache,
OnlyIfCursorBlockIndented,NewPos,NewTopLine);
if Result then begin
NewCode:=NewPos.Code;
NewX:=NewPos.X;
NewY:=NewPos.Y;
end;
except
on e: Exception do HandleException(e);
end;
end;
function TCodeToolManager.GetCompatiblePublishedMethods(Code: TCodeBuffer;
const AClassName: string; PropInstance: TPersistent; const PropName: string;
const Proc: TGetStrProc): boolean;
begin
{$IFDEF CTDEBUG}
DebugLn(['TCodeToolManager.GetCompatiblePublishedMethods A ',Code.Filename,' Classname=',AClassname,' Instance=',DbgSName(PropInstance),' PropName=',PropName]);
{$ENDIF}
Result:=false;
if not InitCurCodeTool(Code) then exit;
try
Result:=FCurCodeTool.GetCompatiblePublishedMethods(AClassName,
PropInstance,PropName,Proc);
except
on e: Exception do Result:=HandleException(e);
end;
end;
function TCodeToolManager.GetCompatiblePublishedMethods(Code: TCodeBuffer;
const AClassName: string; TypeData: PTypeData; const Proc: TGetStrProc): boolean;
begin
{$IFDEF CTDEBUG}
DebugLn('TCodeToolManager.GetCompatiblePublishedMethods A ',Code.Filename,' Classname=',AClassname);
{$ENDIF}
Result:=false;
if not InitCurCodeTool(Code) then exit;
try
Result:=FCurCodeTool.GetCompatiblePublishedMethods(AClassName,TypeData,Proc);
except
on e: Exception do Result:=HandleException(e);
end;
end;
function TCodeToolManager.PublishedMethodExists(Code: TCodeBuffer;
const AClassName, AMethodName: string; PropInstance: TPersistent;
const PropName: string; out MethodIsCompatible, MethodIsPublished,
IdentIsMethod: boolean): boolean;
begin
{$IFDEF CTDEBUG}
DebugLn(['TCodeToolManager.PublishedMethodExists A ',Code.Filename,' ',AClassName,':',AMethodName,' Porperty=',DbgSName(PropInstance),'.',PropName]);
{$ENDIF}
Result:=InitCurCodeTool(Code);
if not Result then exit;
try
Result:=FCurCodeTool.PublishedMethodExists(AClassName,
AMethodName,PropInstance,PropName,
MethodIsCompatible,MethodIsPublished,IdentIsMethod);
except
on e: Exception do Result:=HandleException(e);
end;
end;
function TCodeToolManager.PublishedMethodExists(Code:TCodeBuffer;
const AClassName, AMethodName: string; TypeData: PTypeData;
out MethodIsCompatible, MethodIsPublished, IdentIsMethod: boolean): boolean;
begin
{$IFDEF CTDEBUG}
DebugLn('TCodeToolManager.PublishedMethodExists A ',Code.Filename,' ',AClassName,':',AMethodName);
{$ENDIF}
Result:=InitCurCodeTool(Code);
if not Result then exit;
try
Result:=FCurCodeTool.PublishedMethodExists(AClassName,
AMethodName,TypeData,
MethodIsCompatible,MethodIsPublished,IdentIsMethod);
except
on e: Exception do Result:=HandleException(e);
end;
end;
function TCodeToolManager.JumpToPublishedMethodBody(Code: TCodeBuffer;
const AClassName, AMethodName: string; out NewCode: TCodeBuffer; out NewX,
NewY, NewTopLine, BlockTopLine, BlockBottomLine: integer): boolean;
var NewPos: TCodeXYPosition;
begin
{$IFDEF CTDEBUG}
DebugLn('TCodeToolManager.JumpToPublishedMethodBody A ',Code.Filename,' ',AClassName,':',AMethodName);
{$ENDIF}
Result:=InitCurCodeTool(Code);
if not Result then exit;
try
Result:=FCurCodeTool.JumpToPublishedMethodBody(AClassName,
AMethodName,NewPos,NewTopLine,BlockTopLine,BlockBottomLine,true);
if Result then begin
NewCode:=NewPos.Code;
NewX:=NewPos.X;
NewY:=NewPos.Y;
end;
except
on e: Exception do Result:=HandleException(e);
end;
end;
function TCodeToolManager.RenamePublishedMethod(Code: TCodeBuffer;
const AClassName, OldMethodName, NewMethodName: string): boolean;
begin
{$IFDEF CTDEBUG}
DebugLn('TCodeToolManager.RenamePublishedMethod A');
{$ENDIF}
Result:=InitCurCodeTool(Code);
if not Result then exit;
try
SourceChangeCache.Clear;
Result:=FCurCodeTool.RenamePublishedMethod(AClassName,
OldMethodName,NewMethodName,SourceChangeCache);
except
on e: Exception do Result:=HandleException(e);
end;
end;
function TCodeToolManager.CreatePublishedMethod(Code: TCodeBuffer;
const AClassName, NewMethodName: string; ATypeInfo: PTypeInfo;
UseTypeInfoForParameters: boolean; const APropertyUnitName: string;
const APropertyPath: string; const CallAncestorMethod: string;
AddOverride: boolean): boolean;
begin
{$IFDEF CTDEBUG}
DebugLn('TCodeToolManager.CreatePublishedMethod A');
{$ENDIF}
Result:=InitCurCodeTool(Code);
if not Result then exit;
try
SourceChangeCache.Clear;
Result:=FCurCodeTool.CreateMethod(AClassName,
NewMethodName,ATypeInfo,APropertyUnitName,APropertyPath,
SourceChangeCache,UseTypeInfoForParameters,pcsPublished,
CallAncestorMethod,AddOverride);
except
on e: Exception do Result:=HandleException(e);
end;
end;
function TCodeToolManager.CreatePrivateMethod(Code: TCodeBuffer;
const AClassName, NewMethodName: string; ATypeInfo: PTypeInfo;
UseTypeInfoForParameters: boolean; const APropertyUnitName: string;
const APropertyPath: string): boolean;
begin
{$IFDEF CTDEBUG}
DebugLn('TCodeToolManager.CreatePrivateMethod A');
{$ENDIF}
Result:=InitCurCodeTool(Code);
if not Result then exit;
try
SourceChangeCache.Clear;
Result:=FCurCodeTool.CreateMethod(AClassName,
NewMethodName,ATypeInfo,APropertyUnitName,APropertyPath,
SourceChangeCache,UseTypeInfoForParameters,pcsPrivate);
except
on e: Exception do Result:=HandleException(e);
end;
end;
function TCodeToolManager.GetIDEDirectives(Code: TCodeBuffer;
DirectiveList: TStrings; const Filter: TOnIDEDirectiveFilter): boolean;
begin
{$IFDEF CTDEBUG}
DebugLn('TCodeToolManager.GetIDEDirectives A ',Code.Filename);
{$ENDIF}
Result:=false;
if not InitCurCodeTool(Code) then exit;
try
Result:=FCurCodeTool.GetIDEDirectives(DirectiveList,Filter);
except
on e: Exception do Result:=HandleException(e);
end;
end;
function TCodeToolManager.SetIDEDirectives(Code: TCodeBuffer;
DirectiveList: TStrings; const Filter: TOnIDEDirectiveFilter): boolean;
begin
{$IFDEF CTDEBUG}
DebugLn('TCodeToolManager.GetIDEDirectives A ',Code.Filename);
{$ENDIF}
Result:=false;
if not InitCurCodeTool(Code) then exit;
try
Result:=FCurCodeTool.SetIDEDirectives(DirectiveList,SourceChangeCache,Filter);
except
on e: Exception do Result:=HandleException(e);
end;
end;
function TCodeToolManager.JumpToLinkerIdentifier(Code: TCodeBuffer;
const SourceFilename: string; SourceLine: integer;
const MangledFunction, Identifier: string; out NewCode: TCodeBuffer; out
NewX, NewY, NewTopLine: integer): boolean;
var
NewPos: TCodeXYPosition;
begin
{$IFDEF CTDEBUG}
DebugLn('TCodeToolManager.JumpToLinkerIdentifier A ',Code.Filename);
{$ENDIF}
Result:=false;
if not InitCurCodeTool(Code) then exit;
try
Result:=FCurCodeTool.FindJumpPointForLinkerPos(
SourceFilename, SourceLine, MangledFunction, Identifier,
NewPos,NewTopLine);
if Result then begin
NewX:=NewPos.X;
NewY:=NewPos.Y;
NewCode:=NewPos.Code;
end;
except
on e: Exception do Result:=HandleException(e);
end;
end;
function TCodeToolManager.FindFPCMangledIdentifier(GDBIdentifier: string; out
aComplete: boolean; out aMessage: string;
const OnFindSource: TOnFindFPCMangledSource; out NewCode: TCodeBuffer; out NewX,
NewY, NewTopLine: integer): boolean;
{ Examples:
compiler built-in
fpc_raiseexception
??
PASCALMAIN
SYSTEM_FPC_SYSTEMMAIN$LONGINT$PPCHAR$PPCHAR
unit:
procedure
SYSUTILS_RUNERRORTOEXCEPT$LONGINT$POINTER$POINTER
SYSTEM_HANDLEERRORADDRFRAME$LONGINT$POINTER$POINTER
method
EXTTOOLEDITDLG_TEXTERNALTOOLMENUITEMS_$__LOAD$TCONFIGSTORAGE$$TMODALRESULT
EXTTOOLEDITDLG_TEXTERNALTOOLMENUITEMS_$__LOAD$TCONFIGSTORAGE$ANSISTRING$$TMODALRESULT
ENVIRONMENTOPTS_TENVIRONMENTOPTIONS_$__LOAD$BOOLEAN
MAIN_TMAINIDE_$__LOADGLOBALOPTIONS
MAIN_TMAINIDE_$__CREATE$TCOMPONENT$$TMAINIDE
program:
P$TESTPROJECT1_DOTEST
P$TESTPROJECT1_DOTEST_SUBTEST
P$TESTPROJECT1_DOTEST$CHAR_SUBTEST$LONGINT
P$TESTSTACKTRACE1_TMAINCLASS_$_TSUBCLASS_$__RAISESOMETHING$ANSISTRING
}
var
p: PChar;
TheSrcName: string;
Code: TCodeBuffer;
CurIdentifier: string;
Tool: TCodeTool;
Node: TCodeTreeNode;
SubNode: TCodeTreeNode;
ClassNode: TCodeTreeNode;
ProcNode: TCodeTreeNode;
SectionNode: TCodeTreeNode;
SrcFilename: string;
NewPos: TCodeXYPosition;
procedure ReadIdentifier(out Identifier: string);
var
StartP: PChar;
begin
StartP:=p;
while p^ in ['A'..'Z','0'..'9'] do inc(p);
Identifier:=copy(GDBIdentifier,StartP-PChar(GDBIdentifier)+1,p-StartP);
end;
procedure ReadParamList;
begin
if p^='$' then begin
// parameter list => skip
while (p^ in ['$','A'..'Z','0'..'9']) do inc(p);
end;
end;
function FindUnit(TheUnitName: string; out aFilename: string): boolean;
var
InFilename: string;
begin
// search in main search path
InFilename:='';
aFilename:=CodeToolBoss.DirectoryCachePool.FindUnitSourceInCompletePath(
'',TheUnitName,InFilename,true);
if aFilename='' then begin
// user search
if Assigned(OnFindSource) then
OnFindSource(Self,ctnUnit,TheUnitName,aFilename)
else if Assigned(OnFindFPCMangledSource) then
OnFindFPCMangledSource(Self,ctnUnit,TheUnitName,aFilename)
end;
Result:=aFilename<>'';
end;
function FindProgram(TheSrcName: string; out aFilename: string): boolean;
begin
aFilename:='';
// user search
if Assigned(OnFindSource) then begin
OnFindSource(Self,ctnProgram,TheSrcName,aFilename);
end;
Result:=aFilename<>'';
end;
begin
Result:=false;
aComplete:=false;
aMessage:='';
NewCode:=nil;
NewTopLine:=-1;
NewX:=-1;
NewY:=-1;
if GDBIdentifier='' then begin
aMessage:='missing identifier';
exit;
end;
p:=PChar(GDBIdentifier);
if p^ in ['a'..'z'] then begin
// lower case unit name means compiler built in function
aMessage:='the function "'+GDBIdentifier+'" is a compiler special function without source';
exit;
end;
TheSrcName:='';
if p^ in ['A'..'Z'] then begin
ReadIdentifier(TheSrcName);
//debugln(['TCodeToolManager.FindGBDIdentifier first identifier=',TheSrcName,' ...']);
if (TheSrcName='P') and (p^='$') then begin
// P$programname
inc(p);
if IsIdentStartChar[p^] then
ReadIdentifier(TheSrcName);
//debugln(['TCodeToolManager.FindGBDIdentifier search source of program "',TheSrcName,'" ...']);
if not FindProgram(TheSrcName,SrcFilename) then begin
aMessage:='can''t find program "'+TheSrcName+'"';
exit;
end;
end else if p^='_' then begin
// a unit name
// => search unit
if not FindUnit(TheSrcName,SrcFilename) then begin
aMessage:='can''t find unit '+TheSrcName;
exit;
end;
end else if p^<>'_' then begin
// only one uppercase identifier, e.g. PASCALMAIN
aMessage:='compiler built in function "'+GDBIdentifier+'"';
exit;
end;
// load unit source
Code:=LoadFile(SrcFilename,true,false);
if Code=nil then begin
aMessage:='unable to read file "'+SrcFilename+'"';
exit;
end;
inc(p);
if p^ in ['A'..'Z'] then begin
ReadIdentifier(CurIdentifier);
//debugln(['TCodeToolManager.FindGBDIdentifier Identifier="',CurIdentifier,'"']);
if not Explore(Code,Tool,false,true) then begin
//debugln(['TCodeToolManager.FindGBDIdentifier parse error']);
aMessage:=CodeToolBoss.ErrorMessage;
exit;
end;
ReadParamList;
Node:=nil;
if Tool.GetSourceType=ctnUnit then begin
// a unit => first search in interface, then in implementation
SectionNode:=Tool.FindInterfaceNode;
if SectionNode<>nil then begin
Node:=Tool.FindSubDeclaration(CurIdentifier,SectionNode);
end;
if Node=nil then begin
// search in implementation
try
Node:=Tool.FindDeclarationNodeInImplementation(CurIdentifier,true);
except
on E: Exception do begin
HandleException(E);
//debugln(['TCodeToolManager.FindGBDIdentifier FindDeclarationNodeInImplementation parse error in "',Code.Filename,'": ',E.Message]);
aMessage:=ErrorMessage;
exit;
end;
end;
end;
end else begin
// not a unit, e.g. a program
SectionNode:=Tool.Tree.Root;
if SectionNode<>nil then begin
Node:=Tool.FindSubDeclaration(CurIdentifier,SectionNode);
end;
end;
if Node=nil then begin
// identifier not found => use only SrcFilename
//debugln(['TCodeToolManager.FindGBDIdentifier identifier "',CurIdentifier,'" not found in "',Code.Filename,'"']);
aMessage:='identifier "'+CurIdentifier+'" not found in "'+Code.Filename+'"';
exit;
end;
repeat
if (p^='_') and (p[1]='$') and (p[2]='_') and (p[3]='_') then begin
// sub identifier is method or member
inc(p,4);
end else if (p^='_') and (p[1] in ['A'..'Z']) then begin
// sub identifier is proc
inc(p);
end else
break;
if not (p^ in ['A'..'Z']) then begin
break;
end;
// _$__identifier => sub identifier
ReadIdentifier(CurIdentifier);
ReadParamList;
// find sub identifier
SubNode:=Tool.FindSubDeclaration(CurIdentifier,Node);
if SubNode=nil then begin
//debugln(['TCodeToolManager.FindGBDIdentifier SubIdentifier="',CurIdentifier,'" not found']);
break;
end;
//debugln(['TCodeToolManager.FindGBDIdentifier SubIdentifier="',CurIdentifier,'" found']);
Node:=SubNode;
until false;
if Node.Desc=ctnProcedure then begin
// proc node => find body
ClassNode:=Tool.FindClassOrInterfaceNode(Node);
if ClassNode<>nil then begin
try
Tool.BuildTree(lsrInitializationStart);
except
on E: Exception do begin
// ignore
end;
end;
ProcNode:=Tool.FindCorrespondingProcNode(Node,[phpAddClassName]);
if ProcNode<>nil then
Node:=ProcNode;
end;
end;
aComplete:=p^ in [#0,#9,#10,#13,' '];
Result:=Tool.JumpToCleanPos(Node.StartPos,-1,-1,NewPos,NewTopLine,false);
NewCode:=NewPos.Code;
NewX:=NewPos.X;
NewY:=NewPos.Y;
end;
// unknown operator => use only SrcFilename
//debugln(['TCodeToolManager.FindGBDIdentifier operator not yet supported: ',dbgstr(p^)]);
aMessage:='operator not supported: '+dbgstr(p^);
exit;
end else begin
// example: ??
end;
aMessage:='unknown identifier "'+GDBIdentifier+'"';
end;
function TCodeToolManager.CompleteCode(Code: TCodeBuffer; X, Y,
TopLine: integer; out NewCode: TCodeBuffer; out NewX, NewY, NewTopLine,
BlockTopLine, BlockBottomLine: integer; Interactive: Boolean): boolean;
var
CursorPos: TCodeXYPosition;
NewPos: TCodeXYPosition;
begin
{$IFDEF CTDEBUG}
DebugLn('TCodeToolManager.CompleteCode A ',Code.Filename);
{$ENDIF}
Result:=false;
NewX := 0;
NewY := 0;
NewTopLine := 0;
NewCode := NIL;
if not InitCurCodeTool(Code) then exit;
CursorPos.X:=X;
CursorPos.Y:=Y;
CursorPos.Code:=Code;
try
Result:=FCurCodeTool.CompleteCode(CursorPos,TopLine,
NewPos,NewTopLine, BlockTopLine, BlockBottomLine,SourceChangeCache,Interactive);
if Result then begin
NewX:=NewPos.X;
NewY:=NewPos.Y;
NewCode:=NewPos.Code;
end;
except
on e: Exception do Result:=HandleException(e);
end;
end;
function TCodeToolManager.CreateVariableForIdentifier(Code: TCodeBuffer; X, Y,
TopLine: integer; out NewCode: TCodeBuffer; out NewX, NewY,
NewTopLine: integer; Interactive: Boolean): boolean;
var
CursorPos: TCodeXYPosition;
NewPos: TCodeXYPosition;
begin
{$IFDEF CTDEBUG}
DebugLn('TCodeToolManager.CreateVariableForIdentifier A ',Code.Filename);
{$ENDIF}
Result:=false;
if not InitCurCodeTool(Code) then exit;
CursorPos.X:=X;
CursorPos.Y:=Y;
CursorPos.Code:=Code;
try
Result:=FCurCodeTool.CreateVariableForIdentifier(CursorPos,TopLine,
NewPos,NewTopLine,SourceChangeCache,Interactive);
if Result then begin
NewX:=NewPos.X;
NewY:=NewPos.Y;
NewCode:=NewPos.Code;
end;
except
on e: Exception do Result:=HandleException(e);
end;
end;
function TCodeToolManager.AddMethods(Code: TCodeBuffer; X, Y, TopLine: integer;
ListOfPCodeXYPosition: TFPList; const VirtualToOverride: boolean; out
NewCode: TCodeBuffer; out NewX, NewY, NewTopLine, BlockTopLine,
BlockBottomLine: integer): boolean;
var
CursorPos, NewPos: TCodeXYPosition;
begin
{$IFDEF CTDEBUG}
DebugLn('TCodeToolManager.AddMethods A ',Code.Filename);
{$ENDIF}
Result:=false;
NewCode:=nil;
if not InitCurCodeTool(Code) then exit;
CursorPos.X:=X;
CursorPos.Y:=Y;
CursorPos.Code:=Code;
try
Result:=FCurCodeTool.AddMethods(CursorPos,TopLine,ListOfPCodeXYPosition,
VirtualToOverride,NewPos,NewTopLine,BlockTopLine,BlockBottomLine,SourceChangeCache);
NewCode:=NewPos.Code;
NewX:=NewPos.X;
NewY:=NewPos.Y;
except
on e: Exception do Result:=HandleException(e);
end;
end;
function TCodeToolManager.GuessTypeOfIdentifier(Code: TCodeBuffer; X,
Y: integer; out ItsAKeyword, IsSubIdentifier: boolean;
out ExistingDefinition: TFindContext;
out ListOfPFindContext: TFPList; out NewExprType: TExpressionType;
out NewType: string): boolean;
var
CursorPos: TCodeXYPosition;
begin
{$IFDEF CTDEBUG}
DebugLn(['TCodeToolManager.GuessTypeOfIdentifier A ',Code.Filename,' X=',X,' Y=',Y]);
{$ENDIF}
Result:=false;
if not InitCurCodeTool(Code) then exit;
CursorPos.X:=X;
CursorPos.Y:=Y;
CursorPos.Code:=Code;
try
Result:=FCurCodeTool.GuessTypeOfIdentifier(CursorPos,ItsAKeyword,
IsSubIdentifier,ExistingDefinition,ListOfPFindContext,
NewExprType,NewType);
except
on e: Exception do Result:=HandleException(e);
end;
end;
function TCodeToolManager.GetPossibleInitsForVariable(Code: TCodeBuffer; X,
Y: integer; out Statements: TStrings; out InsertPositions: TObjectList
): boolean;
var
CursorPos: TCodeXYPosition;
begin
{$IFDEF CTDEBUG}
DebugLn(['TCodeToolManager.GetPossibleInitsForVariable A ',Code.Filename,' X=',X,' Y=',Y]);
{$ENDIF}
Result:=false;
if not InitCurCodeTool(Code) then exit;
CursorPos.Code:=Code;
CursorPos.X:=X;
CursorPos.Y:=Y;
try
Result:=FCurCodeTool.GetPossibleInitsForVariable(CursorPos,Statements,
InsertPositions,SourceChangeCache);
except
on e: Exception do Result:=HandleException(e);
end;
end;
function TCodeToolManager.DeclareVariableNearBy(Code: TCodeBuffer; X,
Y: integer; const VariableName, NewType, NewUnitName: string;
Visibility: TCodeTreeNodeDesc; LvlPosCode: TCodeBuffer; LvlPosX: integer;
LvlPosY: integer): boolean;
var
CursorPos, LvlPos: TCodeXYPosition;
begin
{$IFDEF CTDEBUG}
DebugLn(['TCodeToolManager.DeclareVariableNearBy A ',Code.Filename,' X=',X,' Y=',Y]);
{$ENDIF}
Result:=false;
if not InitCurCodeTool(Code) then exit;
CursorPos.Code:=Code;
CursorPos.X:=X;
CursorPos.Y:=Y;
LvlPos.Code:=LvlPosCode;
LvlPos.X:=LvlPosX;
LvlPos.Y:=LvlPosY;
try
Result:=FCurCodeTool.DeclareVariableNearBy(CursorPos,VariableName,
NewType,NewUnitName,Visibility,SourceChangeCache,LvlPos);
except
on e: Exception do Result:=HandleException(e);
end;
end;
function TCodeToolManager.DeclareVariableAt(Code: TCodeBuffer; X, Y: integer;
const VariableName, NewType, NewUnitName: string): boolean;
var
CursorPos: TCodeXYPosition;
begin
{$IFDEF CTDEBUG}
DebugLn(['TCodeToolManager.DeclareVariableNearBy A ',Code.Filename,' X=',X,' Y=',Y]);
{$ENDIF}
Result:=false;
if not InitCurCodeTool(Code) then exit;
CursorPos.Code:=Code;
CursorPos.X:=X;
CursorPos.Y:=Y;
try
Result:=FCurCodeTool.DeclareVariableAt(CursorPos,VariableName,
NewType,NewUnitName,SourceChangeCache);
except
on e: Exception do Result:=HandleException(e);
end;
end;
function TCodeToolManager.FindRedefinitions(Code: TCodeBuffer; out
TreeOfCodeTreeNodeExt: TAVLTree; WithEnums: boolean): boolean;
begin
{$IFDEF CTDEBUG}
DebugLn('TCodeToolManager.FindRedefinitions A ',Code.Filename);
{$ENDIF}
Result:=false;
TreeOfCodeTreeNodeExt:=nil;
if not InitCurCodeTool(Code) then exit;
try
Result:=FCurCodeTool.FindRedefinitions(TreeOfCodeTreeNodeExt,WithEnums);
except
on e: Exception do Result:=HandleException(e);
end;
end;
function TCodeToolManager.RemoveRedefinitions(Code: TCodeBuffer;
TreeOfCodeTreeNodeExt: TAVLTree): boolean;
begin
{$IFDEF CTDEBUG}
DebugLn('TCodeToolManager.RemoveRedefinitions A ',Code.Filename);
{$ENDIF}
Result:=false;
if not InitCurCodeTool(Code) then exit;
try
Result:=FCurCodeTool.RemoveRedefinitions(TreeOfCodeTreeNodeExt,
SourceChangeCache);
except
on e: Exception do Result:=HandleException(e);
end;
end;
function TCodeToolManager.RemoveAllRedefinitions(Code: TCodeBuffer): boolean;
var
TreeOfCodeTreeNodeExt: TAVLTree;
begin
{$IFDEF CTDEBUG}
DebugLn('TCodeToolManager.RemoveAllRedefinitions A ',Code.Filename);
{$ENDIF}
Result:=false;
TreeOfCodeTreeNodeExt:=nil;
try
TreeOfCodeTreeNodeExt:=nil;
if not InitCurCodeTool(Code) then exit;
try
Result:=FCurCodeTool.FindRedefinitions(TreeOfCodeTreeNodeExt,false);
if not Result then exit;
Result:=FCurCodeTool.RemoveRedefinitions(TreeOfCodeTreeNodeExt,
SourceChangeCache);
except
on e: Exception do Result:=HandleException(e);
end;
finally
DisposeAVLTree(TreeOfCodeTreeNodeExt);
end;
end;
function TCodeToolManager.RemoveApplicationScaledStatement(Code: TCodeBuffer
): boolean;
begin
Result:=false;
{$IFDEF CTDEBUG}
DebugLn('TCodeToolManager.RemoveApplicationScaledStatement A ',Code.Filename);
{$ENDIF}
if not InitCurCodeTool(Code) then exit;
try
Result:=FCurCodeTool.RemoveApplicationScaledStatement(SourceChangeCache);
except
on e: Exception do Result:=HandleException(e);
end;
end;
function TCodeToolManager.FindAliasDefinitions(Code: TCodeBuffer; out
TreeOfCodeTreeNodeExt: TAVLTree; OnlyWrongType: boolean): boolean;
begin
{$IFDEF CTDEBUG}
DebugLn('TCodeToolManager.FindAliasDefinitions A ',Code.Filename);
{$ENDIF}
Result:=false;
TreeOfCodeTreeNodeExt:=nil;
if not InitCurCodeTool(Code) then exit;
try
Result:=FCurCodeTool.FindAliasDefinitions(TreeOfCodeTreeNodeExt,
OnlyWrongType);
except
on e: Exception do Result:=HandleException(e);
end;
end;
function TCodeToolManager.FixAliasDefinitions(Code: TCodeBuffer;
TreeOfCodeTreeNodeExt: TAVLTree): boolean;
begin
{$IFDEF CTDEBUG}
DebugLn('TCodeToolManager.FixAliasDefinitions A ',Code.Filename);
{$ENDIF}
Result:=false;
if not InitCurCodeTool(Code) then exit;
try
Result:=FCurCodeTool.FixAliasDefinitions(TreeOfCodeTreeNodeExt,
SourceChangeCache);
except
on e: Exception do Result:=HandleException(e);
end;
end;
function TCodeToolManager.FixAllAliasDefinitions(Code: TCodeBuffer): boolean;
var
TreeOfCodeTreeNodeExt: TAVLTree;
begin
{$IFDEF CTDEBUG}
DebugLn('TCodeToolManager.FixAllAliasDefinitions A ',Code.Filename);
{$ENDIF}
Result:=false;
TreeOfCodeTreeNodeExt:=nil;
try
TreeOfCodeTreeNodeExt:=nil;
if not InitCurCodeTool(Code) then exit;
try
Result:=FCurCodeTool.FindAliasDefinitions(TreeOfCodeTreeNodeExt,true);
if not Result then exit;
Result:=FCurCodeTool.FixAliasDefinitions(TreeOfCodeTreeNodeExt,
SourceChangeCache);
except
on e: Exception do Result:=HandleException(e);
end;
finally
DisposeAVLTree(TreeOfCodeTreeNodeExt);
end;
end;
function TCodeToolManager.FindConstFunctions(Code: TCodeBuffer; out
TreeOfCodeTreeNodeExt: TAVLTree): boolean;
begin
{$IFDEF CTDEBUG}
DebugLn('TCodeToolManager.FindConstFunctions A ',Code.Filename);
{$ENDIF}
Result:=false;
TreeOfCodeTreeNodeExt:=nil;
if not InitCurCodeTool(Code) then exit;
try
Result:=FCurCodeTool.FindConstFunctions(TreeOfCodeTreeNodeExt);
except
on e: Exception do Result:=HandleException(e);
end;
end;
function TCodeToolManager.ReplaceConstFunctions(Code: TCodeBuffer;
TreeOfCodeTreeNodeExt: TAVLTree): boolean;
begin
{$IFDEF CTDEBUG}
DebugLn('TCodeToolManager.ReplaceConstFunctions A ',Code.Filename);
{$ENDIF}
Result:=false;
if not InitCurCodeTool(Code) then exit;
try
Result:=FCurCodeTool.ReplaceConstFunctions(TreeOfCodeTreeNodeExt,
SourceChangeCache);
except
on e: Exception do Result:=HandleException(e);
end;
end;
function TCodeToolManager.ReplaceAllConstFunctions(Code: TCodeBuffer): boolean;
var
TreeOfCodeTreeNodeExt: TAVLTree;
begin
{$IFDEF CTDEBUG}
DebugLn('TCodeToolManager.ReplaceAllConstFunctions A ',Code.Filename);
{$ENDIF}
Result:=false;
if not InitCurCodeTool(Code) then exit;
try
repeat
TreeOfCodeTreeNodeExt:=nil;
try
Result:=FCurCodeTool.FindConstFunctions(TreeOfCodeTreeNodeExt);
if (not Result) or (TreeOfCodeTreeNodeExt=nil)
or (TreeOfCodeTreeNodeExt.Count=0) then
break;
Result:=FCurCodeTool.ReplaceConstFunctions(TreeOfCodeTreeNodeExt,
SourceChangeCache);
finally
DisposeAVLTree(TreeOfCodeTreeNodeExt);
end;
until not Result;
except
on e: Exception do Result:=HandleException(e);
end;
end;
function TCodeToolManager.FindTypeCastFunctions(Code: TCodeBuffer; out
TreeOfCodeTreeNodeExt: TAVLTree): boolean;
begin
{$IFDEF CTDEBUG}
DebugLn('TCodeToolManager.FindTypeCastFunctions A ',Code.Filename);
{$ENDIF}
Result:=false;
TreeOfCodeTreeNodeExt:=nil;
if not InitCurCodeTool(Code) then exit;
try
Result:=FCurCodeTool.FindTypeCastFunctions(TreeOfCodeTreeNodeExt);
except
on e: Exception do Result:=HandleException(e);
end;
end;
function TCodeToolManager.ReplaceTypeCastFunctions(Code: TCodeBuffer;
TreeOfCodeTreeNodeExt: TAVLTree): boolean;
begin
{$IFDEF CTDEBUG}
DebugLn('TCodeToolManager.ReplaceTypeCastFunctions A ',Code.Filename);
{$ENDIF}
Result:=false;
if not InitCurCodeTool(Code) then exit;
try
Result:=FCurCodeTool.ReplaceTypeCastFunctions(TreeOfCodeTreeNodeExt,
SourceChangeCache);
except
on e: Exception do Result:=HandleException(e);
end;
end;
function TCodeToolManager.ReplaceAllTypeCastFunctions(Code: TCodeBuffer
): boolean;
var
TreeOfCodeTreeNodeExt: TAVLTree;
begin
{$IFDEF CTDEBUG}
DebugLn('TCodeToolManager.ReplaceAllTypeCastFunctions A ',Code.Filename);
{$ENDIF}
Result:=false;
if not InitCurCodeTool(Code) then exit;
try
repeat
TreeOfCodeTreeNodeExt:=nil;
try
Result:=FCurCodeTool.FindTypeCastFunctions(TreeOfCodeTreeNodeExt);
if (not Result) or (TreeOfCodeTreeNodeExt=nil)
or (TreeOfCodeTreeNodeExt.Count=0) then
break;
Result:=FCurCodeTool.ReplaceTypeCastFunctions(TreeOfCodeTreeNodeExt,
SourceChangeCache);
finally
DisposeAVLTree(TreeOfCodeTreeNodeExt);
end;
until not Result;
except
on e: Exception do Result:=HandleException(e);
end;
end;
function TCodeToolManager.FixForwardDefinitions(Code: TCodeBuffer): boolean;
begin
{$IFDEF CTDEBUG}
DebugLn('TCodeToolManager.FixForwardDefinitions A ',Code.Filename);
{$ENDIF}
Result:=false;
if not InitCurCodeTool(Code) then exit;
try
Result:=FCurCodeTool.FixForwardDefinitions(SourceChangeCache);
except
on e: Exception do Result:=HandleException(e);
end;
end;
function TCodeToolManager.FindEmptyMethods(Code: TCodeBuffer;
const AClassName: string; X, Y: integer;
const Sections: TPascalClassSections; ListOfPCodeXYPosition: TFPList;
out AllEmpty: boolean): boolean;
var
CursorPos: TCodeXYPosition;
begin
{$IFDEF CTDEBUG}
DebugLn('TCodeToolManager.FindEmptyMethods A ',Code.Filename);
{$ENDIF}
Result:=false;
if not InitCurCodeTool(Code) then exit;
CursorPos.X:=X;
CursorPos.Y:=Y;
CursorPos.Code:=Code;
try
Result:=FCurCodeTool.FindEmptyMethods(CursorPos,AClassName,Sections,
ListOfPCodeXYPosition,AllEmpty);
except
on e: Exception do Result:=HandleException(e);
end;
end;
function TCodeToolManager.RemoveEmptyMethods(Code: TCodeBuffer;
const AClassName: string; X,Y: integer;
const Sections: TPascalClassSections; out AllRemoved: boolean;
const Attr: TProcHeadAttributes; out RemovedProcHeads: TStrings): boolean;
var
CursorPos: TCodeXYPosition;
begin
{$IFDEF CTDEBUG}
DebugLn('TCodeToolManager.RemoveEmptyMethods A ',Code.Filename);
{$ENDIF}
Result:=false;
if not InitCurCodeTool(Code) then exit;
CursorPos.X:=X;
CursorPos.Y:=Y;
CursorPos.Code:=Code;
try
Result:=FCurCodeTool.RemoveEmptyMethods(CursorPos,AClassName,Sections,
SourceChangeCache,AllRemoved,Attr,RemovedProcHeads);
except
on e: Exception do Result:=HandleException(e);
end;
end;
function TCodeToolManager.FindUnusedUnits(Code: TCodeBuffer; Units: TStrings
): boolean;
begin
{$IFDEF CTDEBUG}
DebugLn('TCodeToolManager.FindEmptyMethods A ',Code.Filename);
{$ENDIF}
Result:=false;
if not InitCurCodeTool(Code) then exit;
try
Result:=FCurCodeTool.FindUnusedUnits(Units);
except
on e: Exception do Result:=HandleException(e);
end;
end;
function TCodeToolManager.InitClassCompletion(Code: TCodeBuffer;
const AClassName: string; out CodeTool: TCodeTool): boolean;
begin
{$IFDEF CTDEBUG}
DebugLn('TCodeToolManager.InitClassCompletion A ',Code.Filename);
{$ENDIF}
Result:=false;
CodeTool:=nil;
if not InitCurCodeTool(Code) then exit;
try
Result:=FCurCodeTool.InitClassCompletion(AClassName,SourceChangeCache);
CodeTool:=FCurCodeTool;
except
on e: Exception do Result:=HandleException(e);
end;
end;
function TCodeToolManager.CheckExtractProc(Code: TCodeBuffer; const StartPoint,
EndPoint: TPoint; out MethodPossible, SubProcPossible,
SubProcSameLvlPossible: boolean; out MissingIdentifiers: TAVLTree;
VarTree: TAVLTree): boolean;
var
StartPos, EndPos: TCodeXYPosition;
begin
{$IFDEF CTDEBUG}
DebugLn('TCodeToolManager.CheckExtractProc A ',Code.Filename);
{$ENDIF}
Result:=false;
if not InitCurCodeTool(Code) then exit;
StartPos.X:=StartPoint.X;
StartPos.Y:=StartPoint.Y;
StartPos.Code:=Code;
EndPos.X:=EndPoint.X;
EndPos.Y:=EndPoint.Y;
EndPos.Code:=Code;
try
Result:=FCurCodeTool.CheckExtractProc(StartPos,EndPos,MethodPossible,
SubProcPossible,SubProcSameLvlPossible,MissingIdentifiers,
VarTree);
except
on e: Exception do Result:=HandleException(e);
end;
end;
function TCodeToolManager.ExtractProc(Code: TCodeBuffer; const StartPoint,
EndPoint: TPoint; ProcType: TExtractProcType; const ProcName: string;
IgnoreIdentifiers: TAVLTree; var NewCode: TCodeBuffer; var NewX, NewY,
NewTopLine, BlockTopLine, BlockBottomLine: integer;
FunctionResultVariableStartPos: integer): boolean;
var
StartPos, EndPos: TCodeXYPosition;
NewPos: TCodeXYPosition;
begin
{$IFDEF CTDEBUG}
DebugLn('TCodeToolManager.ExtractProc A ',Code.Filename);
{$ENDIF}
Result:=false;
if not InitCurCodeTool(Code) then exit;
StartPos.X:=StartPoint.X;
StartPos.Y:=StartPoint.Y;
StartPos.Code:=Code;
EndPos.X:=EndPoint.X;
EndPos.Y:=EndPoint.Y;
EndPos.Code:=Code;
try
Result:=FCurCodeTool.ExtractProc(StartPos,EndPos,ProcType,ProcName,
IgnoreIdentifiers,NewPos,NewTopLine,BlockTopLine,BlockBottomLine,SourceChangeCache,
FunctionResultVariableStartPos);
if Result then begin
NewX:=NewPos.X;
NewY:=NewPos.Y;
NewCode:=NewPos.Code;
end;
except
on e: Exception do Result:=HandleException(e);
end;
end;
function TCodeToolManager.FindAssignMethod(Code: TCodeBuffer; X, Y: integer;
out Tool: TCodeTool; out ClassNode: TCodeTreeNode;
out AssignDeclNode: TCodeTreeNode; var MemberNodeExts: TAVLTree;
out AssignBodyNode: TCodeTreeNode; out InheritedDeclContext: TFindContext;
ProcName: string): boolean;
var
CodePos: TCodeXYPosition;
begin
{$IFDEF CTDEBUG}
DebugLn('TCodeToolManager.FindAssignMethod A ',Code.Filename);
{$ENDIF}
Result:=false;
AssignDeclNode:=nil;
AssignBodyNode:=nil;
if not InitCurCodeTool(Code) then exit;
Tool:=FCurCodeTool;
CodePos.X:=X;
CodePos.Y:=Y;
CodePos.Code:=Code;
try
Result:=FCurCodeTool.FindAssignMethod(CodePos,ClassNode,
AssignDeclNode,MemberNodeExts,AssignBodyNode,
InheritedDeclContext,ProcName);
except
on e: Exception do Result:=HandleException(e);
end;
end;
function TCodeToolManager.GetSourceName(Code: TCodeBuffer;
SearchMainCode: boolean): string;
begin
Result:='';
if (Code=nil)
or ((not SearchMainCode) and (Code.LastIncludedByFile<>'')) then exit;
{$IFDEF CTDEBUG}
DebugLn('TCodeToolManager.GetSourceName A ',Code.Filename,' ',dbgs(Code.SourceLength));
{$ENDIF}
{$IFDEF MEM_CHECK}
CheckHeap(IntToStr(MemCheck_GetMem_Cnt));
{$ENDIF}
if not InitCurCodeTool(Code) then exit;
try
Result:=FCurCodeTool.GetSourceName;
except
on e: Exception do begin
Result:=FCurCodeTool.ExtractSourceName;
HandleException(e);
end;
end;
{$IFDEF CTDEBUG}
DebugLn('TCodeToolManager.GetSourceName B ',Code.Filename,' ',dbgs(Code.SourceLength));
{$IFDEF MEM_CHECK}
CheckHeap(IntToStr(MemCheck_GetMem_Cnt));
{$ENDIF}
DebugLn('SourceName=',Result);
{$ENDIF}
end;
function TCodeToolManager.GetCachedSourceName(Code: TCodeBuffer): string;
begin
Result:='';
if (Code=nil)
or (Code.LastIncludedByFile<>'') then exit;
{$IFDEF CTDEBUG}
DebugLn('TCodeToolManager.GetCachedSourceName A ',Code.Filename,' ',dbgs(Code.SourceLength));
{$ENDIF}
{$IFDEF MEM_CHECK}
CheckHeap(IntToStr(MemCheck_GetMem_Cnt));
{$ENDIF}
if not InitCurCodeTool(Code) then exit;
try
Result:=FCurCodeTool.GetCachedSourceName;
except
on e: Exception do HandleException(e);
end;
{$IFDEF CTDEBUG}
DebugLn('TCodeToolManager.GetCachedSourceName B ',Code.Filename,' ',dbgs(Code.SourceLength));
{$IFDEF MEM_CHECK}
CheckHeap(IntToStr(MemCheck_GetMem_Cnt));
{$ENDIF}
DebugLn('SourceName=',Result);
{$ENDIF}
end;
function TCodeToolManager.GetSourceType(Code: TCodeBuffer;
SearchMainCode: boolean): string;
begin
Result:='';
if (Code=nil)
or ((not SearchMainCode) and (Code.LastIncludedByFile<>'')) then exit;
{$IFDEF CTDEBUG}
DebugLn('TCodeToolManager.GetSourceType A ',Code.Filename,' ',dbgs(Code.SourceLength));
{$ENDIF}
if not InitCurCodeTool(Code) then exit;
try
// GetSourceType does not parse the code -> parse it with GetSourceName
FCurCodeTool.GetSourceName;
case FCurCodeTool.GetSourceType of
ctnProgram: Result:='PROGRAM';
ctnPackage: Result:='PACKAGE';
ctnLibrary: Result:='LIBRARY';
ctnUnit: Result:='UNIT';
else
Result:='';
end;
except
on e: Exception do HandleException(e);
end;
{$IFDEF CTDEBUG}
DebugLn('TCodeToolManager.GetSourceType END ',Code.Filename,',',dbgs(Code.SourceLength));
{$IFDEF MEM_CHECK}
CheckHeap(IntToStr(MemCheck_GetMem_Cnt));
{$ENDIF}
DebugLn('SourceType=',Result);
{$ENDIF}
end;
function TCodeToolManager.RenameSource(Code: TCodeBuffer;
const NewName: string): boolean;
begin
Result:=false;
{$IFDEF CTDEBUG}
DebugLn('TCodeToolManager.RenameSource A ',Code.Filename,' NewName=',NewName);
{$ENDIF}
if not InitCurCodeTool(Code) then exit;
try
Result:=FCurCodeTool.RenameSource(NewName,SourceChangeCache);
except
on e: Exception do Result:=HandleException(e);
end;
end;
function TCodeToolManager.FindUnitInAllUsesSections(Code: TCodeBuffer;
const AnUnitName: string; out NamePos, InPos: integer;
const IgnoreMissingIncludeFiles: Boolean = False): boolean;
var
NameAtomPos, InAtomPos: TAtomPosition;
OldIgnoreMissingIncludeFiles: Boolean;
begin
Result:=false;
{$IFDEF CTDEBUG}
DebugLn('TCodeToolManager.FindUnitInAllUsesSections A ',Code.Filename,' AUnitName=',AnUnitName);
{$ENDIF}
if not InitCurCodeTool(Code) then exit;
{$IFDEF CTDEBUG}
DebugLn('TCodeToolManager.FindUnitInAllUsesSections B ',Code.Filename,' AUnitName=',AnUnitName);
{$ENDIF}
OldIgnoreMissingIncludeFiles := FCurCodeTool.Scanner.IgnoreMissingIncludeFiles;
try
FCurCodeTool.Scanner.IgnoreMissingIncludeFiles := IgnoreMissingIncludeFiles;
Result:=FCurCodeTool.FindUnitInAllUsesSections(AnUnitName,
NameAtomPos, InAtomPos);
if Result then begin
NamePos:=NameAtomPos.StartPos;
InPos:=InAtomPos.StartPos;
end;
except
on e: Exception do Result:=HandleException(e);
end;
FCurCodeTool.Scanner.IgnoreMissingIncludeFiles := OldIgnoreMissingIncludeFiles;
end;
function TCodeToolManager.RenameUsedUnit(Code: TCodeBuffer;
const OldUnitName, NewUnitName, NewUnitInFile: string): boolean;
begin
Result:=false;
{$IFDEF CTDEBUG}
DebugLn('TCodeToolManager.RenameUsedUnit A ',Code.Filename,' Old=',OldUnitName,' New=',NewUnitName);
{$ENDIF}
if not InitCurCodeTool(Code) then exit;
try
Result:=FCurCodeTool.RenameUsedUnit(OldUnitName,NewUnitName,
NewUnitInFile,SourceChangeCache);
except
on e: Exception do Result:=HandleException(e);
end;
end;
function TCodeToolManager.ReplaceUsedUnits(Code: TCodeBuffer;
UnitNamePairs: TStringToStringTree): boolean;
begin
Result:=false;
{$IFDEF CTDEBUG}
DebugLn('TCodeToolManager.ReplaceUsedUnits A ',Code.Filename);
{$ENDIF}
if not InitCurCodeTool(Code) then exit;
try
Result:=FCurCodeTool.ReplaceUsedUnits(UnitNamePairs,SourceChangeCache);
except
on e: Exception do Result:=HandleException(e);
end;
end;
function TCodeToolManager.AddUnitToMainUsesSection(Code: TCodeBuffer;
const NewUnitName, NewUnitInFile: string; AsLast: boolean;
CheckSpecialUnits: boolean = true): boolean;
begin
Result:=false;
{$IFDEF CTDEBUG}
DebugLn('TCodeToolManager.AddUnitToMainUsesSection A ',Code.Filename,' NewUnitName=',NewUnitName);
{$ENDIF}
if not InitCurCodeTool(Code) then exit;
try
Result:=FCurCodeTool.AddUnitToMainUsesSection(NewUnitName, NewUnitInFile,
SourceChangeCache,AsLast,CheckSpecialUnits);
except
on e: Exception do Result:=HandleException(e);
end;
end;
function TCodeToolManager.AddUnitToMainUsesSectionIfNeeded(Code: TCodeBuffer;
const NewUnitName, NewUnitInFile: string; AsLast: boolean;
CheckSpecialUnits: boolean): boolean;
var
NamePos, InPos: TAtomPosition;
begin
Result:=false;
{$IFDEF CTDEBUG}
DebugLn('TCodeToolManager.AddUnitToMainUsesSectionIfNeeded A ',Code.Filename,' NewUnitName=',NewUnitName);
{$ENDIF}
if not InitCurCodeTool(Code) then exit;
try
if not FCurCodeTool.FindUnitInAllUsesSections(NewUnitName,NamePos,InPos) then
Result:=FCurCodeTool.AddUnitToMainUsesSection(NewUnitName, NewUnitInFile,
SourceChangeCache,AsLast,CheckSpecialUnits);
except
on e: Exception do Result:=HandleException(e);
end;
end;
function TCodeToolManager.AddUnitToImplementationUsesSection(Code: TCodeBuffer;
const NewUnitName, NewUnitInFile: string; AsLast: boolean;
CheckSpecialUnits: boolean): boolean;
begin
Result:=false;
{$IFDEF CTDEBUG}
DebugLn('TCodeToolManager.AddUnitToImplementationUsesSection A ',Code.Filename,' NewUnitName=',NewUnitName);
{$ENDIF}
if not InitCurCodeTool(Code) then exit;
try
Result:=FCurCodeTool.AddUnitToImplementationUsesSection(
NewUnitName, NewUnitInFile,
SourceChangeCache,AsLast,CheckSpecialUnits);
except
on e: Exception do Result:=HandleException(e);
end;
end;
function TCodeToolManager.RemoveUnitFromAllUsesSections(Code: TCodeBuffer;
const AnUnitName: string): boolean;
begin
Result:=false;
{$IFDEF CTDEBUG}
DebugLn('TCodeToolManager.RemoveUnitFromAllUsesSections A ',Code.Filename,' AUnitName=',AnUnitName);
{$ENDIF}
if not InitCurCodeTool(Code) then exit;
try
Result:=FCurCodeTool.RemoveUnitFromAllUsesSections(AnUnitName,
SourceChangeCache);
except
on e: Exception do Result:=HandleException(e);
end;
end;
function TCodeToolManager.FindUsedUnitFiles(Code: TCodeBuffer;
var MainUsesSection: TStrings): boolean;
begin
Result:=false;
{$IFDEF CTDEBUG}
DebugLn('TCodeToolManager.FindUsedUnitFiles A ',Code.Filename);
{$ENDIF}
if not InitCurCodeTool(Code) then exit;
try
Result:=FCurCodeTool.FindUsedUnitFiles(MainUsesSection);
except
on e: Exception do Result:=HandleException(e);
end;
end;
function TCodeToolManager.FindUsedUnitFiles(Code: TCodeBuffer;
var MainUsesSection, ImplementationUsesSection: TStrings): boolean;
begin
Result:=false;
{$IFDEF CTDEBUG}
DebugLn('TCodeToolManager.FindUsedUnitFiles A ',Code.Filename);
{$ENDIF}
if not InitCurCodeTool(Code) then exit;
try
Result:=FCurCodeTool.FindUsedUnitFiles(MainUsesSection,
ImplementationUsesSection);
except
on e: Exception do Result:=HandleException(e);
end;
end;
function TCodeToolManager.FindUsedUnitNames(Code: TCodeBuffer;
var MainUsesSection, ImplementationUsesSection: TStrings): boolean;
begin
Result:=false;
{$IFDEF CTDEBUG}
DebugLn('TCodeToolManager.FindUsedUnitNames A ',Code.Filename);
{$ENDIF}
if not InitCurCodeTool(Code) then exit;
try
Result:=FCurCodeTool.FindUsedUnitNames(MainUsesSection,
ImplementationUsesSection);
except
on e: Exception do Result:=HandleException(e);
end;
end;
function TCodeToolManager.FindMissingUnits(Code: TCodeBuffer;
var MissingUnits: TStrings; FixCase: boolean;
SearchImplementation: boolean): boolean;
begin
Result:=false;
{$IFDEF CTDEBUG}
DebugLn('TCodeToolManager.FindMissingUnits A ',Code.Filename);
{$ENDIF}
if not InitCurCodeTool(Code) then exit;
try
Result:=FCurCodeTool.FindMissingUnits(MissingUnits,FixCase,
SearchImplementation,SourceChangeCache);
except
on e: Exception do Result:=HandleException(e);
end;
end;
function TCodeToolManager.FindDelphiProjectUnits(Code: TCodeBuffer;
out FoundInUnits, MissingInUnits, NormalUnits: TStrings;
IgnoreNormalUnits: boolean): boolean;
begin
Result:=false;
{$IFDEF CTDEBUG}
DebugLn('TCodeToolManager.FindDelphiProjectUnits A ',Code.Filename);
{$ENDIF}
if not InitCurCodeTool(Code) then exit;
try
Result:=FCurCodeTool.FindDelphiProjectUnits(FoundInUnits, MissingInUnits,
NormalUnits, false, IgnoreNormalUnits);
except
on e: Exception do Result:=HandleException(e);
end;
end;
function TCodeToolManager.FindDelphiPackageUnits(Code: TCodeBuffer;
var FoundInUnits, MissingInUnits, NormalUnits: TStrings;
IgnoreNormalUnits: boolean): boolean;
begin
Result:=false;
{$IFDEF CTDEBUG}
DebugLn('TCodeToolManager.FindDelphiPackageUnits A ',Code.Filename);
{$ENDIF}
if not InitCurCodeTool(Code) then exit;
try
Result:=FCurCodeTool.FindDelphiProjectUnits(FoundInUnits,
MissingInUnits,NormalUnits,true,IgnoreNormalUnits);
except
on e: Exception do Result:=HandleException(e);
end;
end;
function TCodeToolManager.CommentUnitsInUsesSections(Code: TCodeBuffer;
MissingUnits: TStrings): boolean;
begin
Result:=false;
{$IFDEF CTDEBUG}
DebugLn('TCodeToolManager.CommentUnitsInUsesSections A ',Code.Filename);
{$ENDIF}
if not InitCurCodeTool(Code) then exit;
try
Result:=FCurCodeTool.CommentUnitsInUsesSections(MissingUnits,
SourceChangeCache);
except
on e: Exception do Result:=HandleException(e);
end;
end;
function TCodeToolManager.FindUnitCaseInsensitive(Code: TCodeBuffer;
var AnUnitName, AnUnitInFilename: string): string;
begin
Result:='';
{$IFDEF CTDEBUG}
DebugLn('TCodeToolManager.FindUnitCaseInsensitive A ',Code.Filename,' AnUnitName="',AnUnitName,'"',' AnUnitInFilename="',AnUnitInFilename,'"');
{$ENDIF}
if not InitCurCodeTool(Code) then exit;
try
Result:=FCurCodeTool.FindUnitCaseInsensitive(AnUnitName,AnUnitInFilename);
except
on e: Exception do HandleException(e);
end;
end;
function TCodeToolManager.FindUnitSource(Code: TCodeBuffer; const AnUnitName,
AnUnitInFilename: string): TCodeBuffer;
begin
Result:=nil;
{$IFDEF CTDEBUG}
DebugLn('TCodeToolManager.FindUnit A ',Code.Filename,' AnUnitName="',AnUnitName,'" AnUnitInFilename="',AnUnitInFilename,'"');
{$ENDIF}
if not InitCurCodeTool(Code) then exit;
try
Result:=FCurCodeTool.FindUnitSource(AnUnitName,AnUnitInFilename,false);
except
on e: Exception do HandleException(e);
end;
end;
function TCodeToolManager.CreateUsesGraph: TUsesGraph;
begin
Result:=TUsesGraph.Create;
Result.DirectoryCachePool:=DirectoryCachePool;
Result.OnGetCodeToolForBuffer:=@DoOnGetCodeToolForBuffer;
Result.OnLoadFile:=@DoOnLoadFileForTool;
end;
function TCodeToolManager.FindLFMFileName(Code: TCodeBuffer): string;
var LinkIndex: integer;
CurCode: TCodeBuffer;
Ext: string;
begin
Result:='';
{$IFDEF CTDEBUG}
DebugLn('TCodeToolManager.FindLFMFileName A ',Code.Filename);
{$ENDIF}
if not InitCurCodeTool(Code) then exit;
try
LinkIndex:=-1;
CurCode:=FCurCodeTool.FindNextIncludeInInitialization(LinkIndex);
while (CurCode<>nil) do begin
if UpperCaseStr(ExtractFileExt(CurCode.Filename))='.LRS' then begin
Result:=CurCode.Filename;
Ext:=ExtractFileExt(Result);
Result:=copy(Result,1,length(Result)-length(Ext))+'.lfm';
exit;
end;
CurCode:=FCurCodeTool.FindNextIncludeInInitialization(LinkIndex);
end;
except
on e: Exception do HandleException(e);
end;
end;
function TCodeToolManager.CheckLFM(UnitCode, LFMBuf: TCodeBuffer;
out LFMTree: TLFMTree; RootMustBeClassInUnit, RootMustBeClassInIntf,
ObjectsMustExist: boolean): boolean;
begin
Result:=false;
{$IFDEF CTDEBUG}
DebugLn('TCodeToolManager.CheckLFM A ',UnitCode.Filename,' ',LFMBuf.Filename);
{$ENDIF}
if not InitCurCodeTool(UnitCode) then exit;
try
Result:=FCurCodeTool.CheckLFM(LFMBuf,LFMTree,OnFindDefinePropertyForContext,
RootMustBeClassInUnit,RootMustBeClassInIntf,ObjectsMustExist);
except
on e: Exception do HandleException(e);
end;
end;
function TCodeToolManager.FindNextResourceFile(Code: TCodeBuffer;
var LinkIndex: integer): TCodeBuffer;
begin
Result:=nil;
{$IFDEF CTDEBUG}
DebugLn('TCodeToolManager.FindNextResourceFile A ',Code.Filename);
{$ENDIF}
if not InitCurCodeTool(Code) then exit;
try
Result:=FCurCodeTool.FindNextIncludeInInitialization(LinkIndex);
except
on e: Exception do HandleException(e);
end;
end;
function TCodeToolManager.AddLazarusResourceHeaderComment(Code: TCodeBuffer;
const CommentText: string): boolean;
begin
Result:=false;
if not InitResourceTool then exit;
{$IFDEF CTDEBUG}
DebugLn('TCodeToolManager.AddLazarusResourceHeaderComment A ',Code.Filename,' CommentText=',CommentText);
{$ENDIF}
try
Result:=GetResourceTool.AddLazarusResourceHeaderComment(Code,
'{ '+CommentText+' }'+SourceChangeCache.BeautifyCodeOptions.LineEnd
+SourceChangeCache.BeautifyCodeOptions.LineEnd);
except
on e: Exception do HandleException(e);
end;
end;
function TCodeToolManager.FindLazarusResource(Code: TCodeBuffer;
const ResourceName: string): TAtomPosition;
begin
Result.StartPos:=-1;
if not InitResourceTool then exit;
{$IFDEF CTDEBUG}
DebugLn('TCodeToolManager.FindLazarusResource A ',Code.Filename,' ResourceName=',ResourceName);
{$ENDIF}
try
Result:=GetResourceTool.FindLazarusResource(Code,ResourceName,-1);
except
on e: Exception do HandleException(e);
end;
end;
function TCodeToolManager.AddLazarusResource(Code: TCodeBuffer;
const ResourceName, ResourceData: string): boolean;
begin
Result:=false;
{$IFDEF CTDEBUG}
DebugLn('TCodeToolManager.AddLazarusResource A ',Code.Filename,' ResourceName=',ResourceName,' ',dbgs(length(ResourceData)));
{$ENDIF}
if not InitResourceTool then exit;
{$IFDEF CTDEBUG}
DebugLn('TCodeToolManager.AddLazarusResource B ');
{$ENDIF}
try
Result:=GetResourceTool.AddLazarusResource(Code,ResourceName,ResourceData);
except
on e: Exception do Result:=HandleException(e);
end;
end;
function TCodeToolManager.RemoveLazarusResource(Code: TCodeBuffer;
const ResourceName: string): boolean;
begin
Result:=false;
{$IFDEF CTDEBUG}
DebugLn('TCodeToolManager.RemoveLazarusResource A ',Code.Filename,' ResourceName=',ResourceName);
{$ENDIF}
if not InitResourceTool then exit;
try
Result:=GetResourceTool.RemoveLazarusResource(Code,ResourceName);
except
on e: Exception do Result:=HandleException(e);
end;
end;
function TCodeToolManager.RenameMainInclude(Code: TCodeBuffer;
const NewFilename: string; KeepPath: boolean): boolean;
var
LinkIndex: integer;
OldIgnoreMissingIncludeFiles: boolean;
begin
Result:=false;
{$IFDEF CTDEBUG}
DebugLn('TCodeToolManager.RenameMainInclude A ',Code.Filename,' NewFilename=',NewFilename,' KeepPath=',dbgs(KeepPath));
{$ENDIF}
if not InitCurCodeTool(Code) then exit;
try
OldIgnoreMissingIncludeFiles:=
FCurCodeTool.Scanner.IgnoreMissingIncludeFiles;
FCurCodeTool.Scanner.IgnoreMissingIncludeFiles:=true;
LinkIndex:=-1;
if FCurCodeTool.FindNextIncludeInInitialization(LinkIndex)=nil then exit;
Result:=FCurCodeTool.RenameInclude(LinkIndex,NewFilename,KeepPath,
SourceChangeCache);
FCurCodeTool.Scanner.IgnoreMissingIncludeFiles:=
OldIgnoreMissingIncludeFiles;
except
on e: Exception do Result:=HandleException(e);
end;
end;
function TCodeToolManager.RenameIncludeDirective(Code: TCodeBuffer;
LinkIndex: integer; const NewFilename: string; KeepPath: boolean): boolean;
begin
Result:=false;
{$IFDEF CTDEBUG}
DebugLn('TCodeToolManager.RenameIncludeDirective A ',Code.Filename,' NewFilename=',NewFilename,' KeepPath=',dbgs(KeepPath));
{$ENDIF}
if not InitCurCodeTool(Code) then exit;
try
Result:=FCurCodeTool.RenameInclude(LinkIndex,NewFilename,KeepPath,
SourceChangeCache);
except
on e: Exception do Result:=HandleException(e);
end;
end;
procedure TCodeToolManager.DefaultFindDefinePropertyForContext(Sender: TObject;
const ClassContext, AncestorClassContext: TFindContext; LFMNode: TLFMTreeNode;
const IdentName: string; var IsDefined: boolean);
var
PersistentClassName: String;
AncestorClassName: String;
begin
if Assigned(OnFindDefineProperty) then begin
PersistentClassName:=ClassContext.Tool.ExtractClassName(
ClassContext.Node,false);
AncestorClassName:='';
if AncestorClassContext.Tool<>nil then
AncestorClassName:=AncestorClassContext.Tool.ExtractClassName(
AncestorClassContext.Node,false);
OnFindDefineProperty(ClassContext.Tool,
PersistentClassName,AncestorClassName,IdentName,
IsDefined);
end;
end;
function TCodeToolManager.FindCreateFormStatement(Code: TCodeBuffer;
StartPos: integer;
const AClassName, AVarName: string;
out Position: integer): integer;
// 0=found, -1=not found, 1=found, but wrong classname
var PosAtom: TAtomPosition;
begin
Result:=-1;
{$IFDEF CTDEBUG}
DebugLn('TCodeToolManager.FindCreateFormStatement A ',Code.Filename,' StartPos=',dbgs(StartPos),' ',AClassName,':',AVarName);
{$ENDIF}
if not InitCurCodeTool(Code) then exit;
try
Result:=FCurCodeTool.FindCreateFormStatement(StartPos,AClassName,
AVarName,PosAtom);
if Result<>-1 then
Position:=PosAtom.StartPos;
except
on e: Exception do HandleException(e);
end;
end;
function TCodeToolManager.AddCreateFormStatement(Code: TCodeBuffer;
const AClassName, AVarName: string): boolean;
begin
Result:=false;
{$IFDEF CTDEBUG}
DebugLn('TCodeToolManager.AddCreateFormStatement A ',Code.Filename,' ',AClassName,':',AVarName);
{$ENDIF}
if not InitCurCodeTool(Code) then exit;
try
Result:=FCurCodeTool.AddCreateFormStatement(AClassName,AVarName,
SourceChangeCache);
except
on e: Exception do Result:=HandleException(e);
end;
end;
function TCodeToolManager.RemoveCreateFormStatement(Code: TCodeBuffer;
const AVarName: string): boolean;
begin
Result:=false;
{$IFDEF CTDEBUG}
DebugLn('TCodeToolManager.RemoveCreateFormStatement A ',Code.Filename,' ',AVarName);
{$ENDIF}
if not InitCurCodeTool(Code) then exit;
try
Result:=FCurCodeTool.RemoveCreateFormStatement(AVarName,SourceChangeCache);
except
on e: Exception do Result:=HandleException(e);
end;
end;
function TCodeToolManager.ChangeCreateFormStatement(Code: TCodeBuffer;
const OldClassName, OldVarName: string; const NewClassName,
NewVarName: string; OnlyIfExists: boolean): boolean;
begin
Result:=false;
{$IFDEF CTDEBUG}
DebugLn('TCodeToolManager.ChangeCreateFormStatement A ',Code.Filename,
' ',OldVarName+':',OldClassName,' -> ',NewVarName+':',NewClassName,
' OnlyIfExists=',dbgs(OnlyIfExists));
{$ENDIF}
if not InitCurCodeTool(Code) then exit;
try
Result:=FCurCodeTool.ChangeCreateFormStatement(-1,OldClassName,OldVarName,
NewClassName,NewVarName,OnlyIfExists,
SourceChangeCache);
except
on e: Exception do Result:=HandleException(e);
end;
end;
function TCodeToolManager.ListAllCreateFormStatements(
Code: TCodeBuffer): TStrings;
begin
Result:=nil;
{$IFDEF CTDEBUG}
DebugLn('TCodeToolManager.ListAllCreateFormStatements A ',Code.Filename);
{$ENDIF}
if not InitCurCodeTool(Code) then exit;
try
Result:=FCurCodeTool.ListAllCreateFormStatements;
except
on e: Exception do HandleException(e);
end;
end;
function TCodeToolManager.SetAllCreateFromStatements(Code: TCodeBuffer;
List: TStrings): boolean;
begin
Result:=false;
{$IFDEF CTDEBUG}
DebugLn('TCodeToolManager.SetAllCreateFromStatements A ',Code.Filename);
{$ENDIF}
if not InitCurCodeTool(Code) then exit;
try
Result:=FCurCodeTool.SetAllCreateFromStatements(List,SourceChangeCache);
except
on e: Exception do Result:=HandleException(e);
end;
end;
function TCodeToolManager.SetApplicationScaledStatement(Code: TCodeBuffer;
const NewScaled: Boolean): boolean;
begin
Result:=false;
{$IFDEF CTDEBUG}
DebugLn('TCodeToolManager.SetApplicationScaledStatement A ',Code.Filename);
{$ENDIF}
if not InitCurCodeTool(Code) then exit;
try
Result:=FCurCodeTool.SetApplicationScaledStatement(NewScaled,
SourceChangeCache);
except
on e: Exception do Result:=HandleException(e);
end;
end;
function TCodeToolManager.GetApplicationTitleStatement(Code: TCodeBuffer;
var Title: string): boolean;
var
StartPos, StringConstStartPos, EndPos: integer;
begin
Result:=false;
{$IFDEF CTDEBUG}
DebugLn('TCodeToolManager.GetApplicationTitleStatement A ',Code.Filename);
{$ENDIF}
if not InitCurCodeTool(Code) then exit;
try
Result:=FCurCodeTool.FindApplicationTitleStatement(StartPos,
StringConstStartPos,EndPos);
if StartPos=0 then ;
Result:=FCurCodeTool.GetApplicationTitleStatement(StringConstStartPos,
EndPos,Title);
except
on e: Exception do Result:=HandleException(e);
end;
end;
function TCodeToolManager.SetApplicationTitleStatement(Code: TCodeBuffer;
const NewTitle: string): boolean;
begin
Result:=false;
{$IFDEF CTDEBUG}
DebugLn('TCodeToolManager.SetApplicationTitleStatement A ',Code.Filename);
{$ENDIF}
if not InitCurCodeTool(Code) then exit;
try
Result:=FCurCodeTool.SetApplicationTitleStatement(NewTitle,
SourceChangeCache);
except
on e: Exception do Result:=HandleException(e);
end;
end;
function TCodeToolManager.RemoveApplicationTitleStatement(Code: TCodeBuffer
): boolean;
begin
Result:=false;
{$IFDEF CTDEBUG}
DebugLn('TCodeToolManager.RemoveApplicationTitleStatement A ',Code.Filename);
{$ENDIF}
if not InitCurCodeTool(Code) then exit;
try
Result:=FCurCodeTool.RemoveApplicationTitleStatement(SourceChangeCache);
except
on e: Exception do Result:=HandleException(e);
end;
end;
function TCodeToolManager.RenameForm(Code: TCodeBuffer; const OldFormName,
OldFormClassName: string; const NewFormName, NewFormClassName: string
): boolean;
begin
Result:=false;
{$IFDEF CTDEBUG}
DebugLn('TCodeToolManager.RenameForm A ',Code.Filename,
' OldFormName=',OldFormName,' OldFormClassName=',OldFormClassName,
' NewFormName=',NewFormName,' NewFormClassName=',NewFormClassName);
{$ENDIF}
if not InitCurCodeTool(Code) then exit;
try
Result:=FCurCodeTool.RenameForm(OldFormName,OldFormClassName,
NewFormName,NewFormClassName,SourceChangeCache);
except
on e: Exception do Result:=HandleException(e);
end;
end;
function TCodeToolManager.FindFormAncestor(Code: TCodeBuffer;
const FormClassName: string; var AncestorClassName: string;
DirtySearch: boolean): boolean;
begin
Result:=false;
{$IFDEF CTDEBUG}
DebugLn('TCodeToolManager.FindFormAncestor A ',Code.Filename,' ',FormClassName);
{$ENDIF}
AncestorClassName:='';
if not InitCurCodeTool(Code) then exit;
try
Result:=FCurCodeTool.FindFormAncestor(FormClassName,AncestorClassName);
except
on e: Exception do Result:=HandleException(e);
end;
if (not Result) and DirtySearch then begin
AncestorClassName:=FindClassAncestorName(Code.Source,FormClassName);
Result:=AncestorClassName<>'';
end;
end;
function TCodeToolManager.CompleteComponent(Code: TCodeBuffer;
AComponent, AncestorComponent: TComponent): boolean;
begin
Result:=false;
{$IFDEF CTDEBUG}
DebugLn('TCodeToolManager.CompleteComponent A ',Code.Filename,' ',AComponent.Name,':',AComponent.ClassName,' ',dbgsName(AncestorComponent));
{$ENDIF}
if not InitCurCodeTool(Code) then exit;
try
Result:=FCurCodeTool.CompleteComponent(AComponent,AncestorComponent,
SourceChangeCache);
except
on e: Exception do Result:=HandleException(e);
end;
end;
function TCodeToolManager.PublishedVariableExists(Code: TCodeBuffer;
const AClassName, AVarName: string; ErrorOnClassNotFound: boolean): boolean;
begin
Result:=false;
{$IFDEF CTDEBUG}
DebugLn('TCodeToolManager.PublishedVariableExists A ',Code.Filename,' ',AClassName,':',AVarName);
{$ENDIF}
if not InitCurCodeTool(Code) then exit;
try
Result:=FCurCodeTool.FindPublishedVariable(AClassName,
AVarName,ErrorOnClassNotFound)<>nil;
except
on e: Exception do Result:=HandleException(e);
end;
end;
function TCodeToolManager.AddPublishedVariable(Code: TCodeBuffer;
const AClassName, VarName, VarType: string): boolean;
begin
Result:=false;
{$IFDEF CTDEBUG}
DebugLn('TCodeToolManager.AddPublishedVariable A ',Code.Filename,' ',AClassName,':',VarName);
{$ENDIF}
if not InitCurCodeTool(Code) then exit;
try
Result:=FCurCodeTool.AddPublishedVariable(AClassName,
VarName,VarType,SourceChangeCache);
except
on e: Exception do Result:=HandleException(e);
end;
end;
function TCodeToolManager.RemovePublishedVariable(Code: TCodeBuffer;
const AClassName, AVarName: string; ErrorOnClassNotFound: boolean): boolean;
begin
Result:=false;
{$IFDEF CTDEBUG}
DebugLn('TCodeToolManager.RemovePublishedVariable A ',Code.Filename,' ',AClassName,':',AVarName);
{$ENDIF}
if not InitCurCodeTool(Code) then exit;
try
Result:=FCurCodeTool.RemovePublishedVariable(AClassName,
AVarName,ErrorOnClassNotFound,SourceChangeCache);
except
on e: Exception do Result:=HandleException(e);
end;
end;
function TCodeToolManager.RenamePublishedVariable(Code: TCodeBuffer;
const AClassName, OldVariableName, NewVarName, VarType: shortstring;
ErrorOnClassNotFound: boolean): boolean;
begin
Result:=false;
{$IFDEF CTDEBUG}
DebugLn('TCodeToolManager.RenamePublishedVariable A ',Code.Filename,' ',AClassName,' OldVar=',OldVariableName,' NewVar=',NewVarName);
{$ENDIF}
if not InitCurCodeTool(Code) then exit;
try
Result:=FCurCodeTool.RenamePublishedVariable(AClassName,
OldVariableName,NewVarName,VarType,
ErrorOnClassNotFound,SourceChangeCache);
except
on e: Exception do Result:=HandleException(e);
end;
end;
function TCodeToolManager.RetypeClassVariables(Code: TCodeBuffer;
const AClassName: string; ListOfReTypes: TStringToStringTree;
ErrorOnClassNotFound: boolean; SearchImplementationToo: boolean): boolean;
begin
Result:=false;
{$IFDEF CTDEBUG}
DebugLn('TCodeToolManager.RetypeClassVariables A ',Code.Filename,' ',AClassName);
{$ENDIF}
if not InitCurCodeTool(Code) then exit;
try
Result:=FCurCodeTool.RetypeClassVariables(AClassName,ListOfReTypes,
ErrorOnClassNotFound,SourceChangeCache,SearchImplementationToo);
except
on e: Exception do Result:=HandleException(e);
end;
end;
function TCodeToolManager.FindDanglingComponentEvents(Code: TCodeBuffer;
const AClassName: string; RootComponent: TComponent;
ExceptionOnClassNotFound, SearchInAncestors: boolean; out
ListOfPInstancePropInfo: TFPList;
const OverrideGetMethodName: TOnGetMethodname): boolean;
begin
Result:=false;
{$IFDEF CTDEBUG}
DebugLn('TCodeToolManager.FindDanglingComponentEvents A ',Code.Filename,' ',AClassName);
{$ENDIF}
ListOfPInstancePropInfo:=nil;
if not InitCurCodeTool(Code) then exit;
try
Result:=FCurCodeTool.FindDanglingComponentEvents(AClassName,RootComponent,
ExceptionOnClassNotFound,SearchInAncestors,
ListOfPInstancePropInfo,OverrideGetMethodName);
except
on e: Exception do Result:=HandleException(e);
end;
end;
function TCodeToolManager.ConvertDelphiToLazarusSource(Code: TCodeBuffer;
AddLRSCode: boolean): boolean;
begin
Result:=false;
{$IFDEF CTDEBUG}
DebugLn('TCodeToolManager.ConvertDelphiToLazarusSource A ',Code.Filename);
{$ENDIF}
if not InitCurCodeTool(Code) then exit;
try
Result:=FCurCodeTool.ConvertDelphiToLazarusSource(AddLRSCode,
SourceChangeCache);
except
on e: Exception do Result:=HandleException(e);
end;
end;
function TCodeToolManager.DoOnFindUsedUnit(SrcTool: TFindDeclarationTool;
const TheUnitName, TheUnitInFilename: string): TCodeBuffer;
begin
if Assigned(OnSearchUsedUnit) then
Result:=OnSearchUsedUnit(SrcTool.MainFilename,
TheUnitName,TheUnitInFilename)
else
Result:=nil;
end;
procedure TCodeToolManager.DoOnGatherUserIdentifiers(
Sender: TIdentCompletionTool; const ContextFlags: TIdentifierListContextFlags
);
begin
if Assigned(FOnGatherUserIdentifiers) then
FOnGatherUserIdentifiers(Sender, ContextFlags);
end;
function TCodeToolManager.DoOnGetSrcPathForCompiledUnit(Sender: TObject;
const AFilename: string): string;
begin
if CompareFileExt(AFilename,'.ppu',false)=0 then
Result:=GetPPUSrcPathForDirectory(ExtractFilePath(AFilename))
else if CompareFileExt(AFilename,'.dcu',false)=0 then
Result:=GetDCUSrcPathForDirectory(ExtractFilePath(AFilename))
else
Result:='';
if Result='' then
Result:=GetCompiledSrcPathForDirectory(ExtractFilePath(AFilename));
end;
function TCodeToolManager.DoOnInternalGetMethodName(const AMethod: TMethod;
CheckOwner: TObject): string;
begin
if Assigned(OnGetMethodName) then
Result:=OnGetMethodName(AMethod,CheckOwner)
else if (AMethod.Data=nil) or (AMethod.Code=nil) then
Result:=''
else if (CheckOwner<>nil) and (TObject(AMethod.Data)<>CheckOwner) then
Result:=''
else
Result:=TObject(AMethod.Data).MethodName(AMethod.Code);
end;
function TCodeToolManager.DoOnParserProgress(Tool: TCustomCodeTool): boolean;
begin
Result:=true;
if not FAbortable then exit;
if not Assigned(OnCheckAbort) then exit;
Result:=not OnCheckAbort();
end;
procedure TCodeToolManager.DoOnRescanFPCDirectoryCache(Sender: TObject);
begin
if Assigned(FOnRescanFPCDirectoryCache) then
FOnRescanFPCDirectoryCache(Sender);
end;
procedure TCodeToolManager.DoOnToolTreeChange(Tool: TCustomCodeTool;
NodesDeleting: boolean);
var
i: Integer;
begin
CTIncreaseChangeStamp(FCodeNodeTreeChangeStep);
if NodesDeleting then begin
CTIncreaseChangeStamp(FCodeTreeNodesDeletedStep);
// Note: IdentifierList nodes do not need to be cleared, because Node
// is accessed via GetNode, which checks if nodes were deleted
end;
//debugln(['TCodeToolManager.OnToolTreeChange ',FHandlers[ctmOnToolTreeChanging].Count]);
i:=FHandlers[ctmOnToolTreeChanging].Count;
while FHandlers[ctmOnToolTreeChanging].NextDownIndex(i) do
TOnToolTreeChanging(FHandlers[ctmOnToolTreeChanging][i])(Tool,NodesDeleting);
end;
function TCodeToolManager.DoOnScannerProgress(Sender: TLinkScanner): boolean;
begin
Result:=true;
if not FAbortable then exit;
if not Assigned(OnCheckAbort) then exit;
Result:=not OnCheckAbort();
end;
procedure TCodeToolManager.DoOnFABGetNestedComments(Sender: TObject;
Code: TCodeBuffer; out NestedComments: boolean);
begin
NestedComments:=GetNestedCommentsFlagForFile(Code.Filename);
end;
procedure TCodeToolManager.DoOnFABGetExamples(Sender: TObject; Code: TCodeBuffer;
Step: integer; var CodeBuffers: TFPList; var ExpandedFilenames: TStrings);
begin
if Assigned(OnGetIndenterExamples) then
OnGetIndenterExamples(Sender,Code,Step,CodeBuffers,ExpandedFilenames);
end;
procedure TCodeToolManager.DoOnLoadFileForTool(Sender: TObject;
const ExpandedFilename: string; out Code: TCodeBuffer; var Abort: boolean);
begin
Code:=LoadFile(ExpandedFilename,true,false);
end;
function TCodeToolManager.DoOnScannerGetInitValues(Scanner: TLinkScanner;
Code: Pointer; out AChangeStep: integer): TExpressionEvaluator;
begin
Result:=nil;
AChangeStep:=DefineTree.ChangeStep;
if Code=nil then exit;
//DefineTree.WriteDebugReport;
if not TCodeBuffer(Code).IsVirtual then
Result:=DefineTree.GetDefinesForDirectory(
ExtractFilePath(TCodeBuffer(Code).Filename),false)
else
Result:=DefineTree.GetDefinesForVirtualDirectory;
if Assigned(OnScannerInit) then
OnScannerInit(Self,Scanner);
end;
procedure TCodeToolManager.DoOnDefineTreeReadValue(Sender: TObject;
const VariableName: string; var Value: string; var Handled: boolean);
begin
Handled:=GlobalValues.IsDefined(VariableName);
if Handled then
Value:=GlobalValues[VariableName];
//DebugLn('[TCodeToolManager.OnDefineTreeReadValue] Name="',VariableName,'" = "',Value,'"');
end;
procedure TCodeToolManager.DoOnGlobalValuesChanged;
begin
DefineTree.ClearCache;
end;
procedure TCodeToolManager.SetCheckFilesOnDisk(NewValue: boolean);
begin
if NewValue=FCheckFilesOnDisk then exit;
FCheckFilesOnDisk:=NewValue;
if FCurCodeTool<>nil then
FCurCodeTool.CheckFilesOnDisk:=NewValue;
end;
procedure TCodeToolManager.SetCodeCompletionTemplateFileName(AValue: String);
var
OldValue: String;
Code: TCodeBuffer;
begin
AValue:=CleanAndExpandFilename(AValue);
if FCodeCompletionTemplateFileName=AValue then Exit;
OldValue:=FCodeCompletionTemplateFileName;
FCodeCompletionTemplateFileName:=AValue;
if CompareFilenames(FCodeCompletionTemplateFileName,OldValue)=0 then exit;
if (FCodeCompletionTemplateFileName<>'') then
Code:=LoadFile(FCodeCompletionTemplateFileName,true,false)
else
Code:=nil;
if Code<>nil then begin
if CTTemplateExpander=nil then
CTTemplateExpander:=TTemplateExpander.Create;
CTTemplateExpander.Code:=Code;
end else begin
FreeAndNil(CTTemplateExpander);
end;
end;
procedure TCodeToolManager.SetCompleteProperties(const AValue: boolean);
begin
if CompleteProperties=AValue then exit;
FCompleteProperties:=AValue;
if FCurCodeTool<>nil then
FCurCodeTool.CompleteProperties:=AValue;
end;
procedure TCodeToolManager.SetIndentSize(NewValue: integer);
begin
if NewValue=FIndentSize then exit;
FIndentSize:=NewValue;
if FCurCodeTool<>nil then
FCurCodeTool.IndentSize:=NewValue;
SourceChangeCache.BeautifyCodeOptions.Indent:=NewValue;
end;
procedure TCodeToolManager.SetTabWidth(const AValue: integer);
begin
if FTabWidth=AValue then exit;
FTabWidth:=AValue;
SourceChangeCache.BeautifyCodeOptions.TabWidth:=AValue;
Indenter.DefaultTabWidth:=AValue;
end;
procedure TCodeToolManager.SetUseTabs(AValue: boolean);
begin
if FUseTabs=AValue then Exit;
FUseTabs:=AValue;
SourceChangeCache.BeautifyCodeOptions.UseTabs:=UseTabs;
end;
procedure TCodeToolManager.SetVisibleEditorLines(NewValue: integer);
begin
if NewValue=FVisibleEditorLines then exit;
FVisibleEditorLines:=NewValue;
if FCurCodeTool<>nil then
FCurCodeTool.VisibleEditorLines:=NewValue;
end;
procedure TCodeToolManager.SetJumpSingleLinePos(NewValue: integer);
begin
if NewValue=FJumpSingleLinePos then exit;
FJumpSingleLinePos:=NewValue;
if FCurCodeTool<>nil then
FCurCodeTool.JumpSingleLinePos:=NewValue;
end;
procedure TCodeToolManager.SetJumpCodeBlockPos(NewValue: integer);
begin
if NewValue=FJumpCodeBlockPos then exit;
FJumpCodeBlockPos:=NewValue;
if FCurCodeTool<>nil then
FCurCodeTool.JumpCodeBlockPos:=NewValue;
end;
procedure TCodeToolManager.SetSetPropertyVariableIsPrefix(aValue: Boolean);
begin
if FSetPropertyVariableIsPrefix = aValue then Exit;
FSetPropertyVariableIsPrefix := aValue;
end;
procedure TCodeToolManager.SetSetPropertyVariablename(AValue: string);
begin
if FSetPropertyVariablename=aValue then Exit;
FSetPropertyVariablename:=aValue;
end;
procedure TCodeToolManager.SetSetPropertyVariableUseConst(aValue: Boolean);
begin
if FSetPropertyVariableUseConst = aValue then Exit;
FSetPropertyVariableUseConst := aValue;
end;
procedure TCodeToolManager.SetCursorBeyondEOL(NewValue: boolean);
begin
if NewValue=FCursorBeyondEOL then exit;
FCursorBeyondEOL:=NewValue;
if FCurCodeTool<>nil then
FCurCodeTool.CursorBeyondEOL:=NewValue;
end;
procedure TCodeToolManager.BeforeApplyingChanges(var Abort: boolean);
begin
IncreaseChangeStep;
if Assigned(FOnBeforeApplyChanges) then
FOnBeforeApplyChanges(Self,Abort);
end;
procedure TCodeToolManager.AfterApplyingChanges;
begin
// clear all codetrees of changed buffers
if FCurCodeTool<>nil then
FCurCodeTool.Clear;
// user callback
if Assigned(FOnAfterApplyChanges) then
FOnAfterApplyChanges(Self);
end;
function TCodeToolManager.FindCodeToolForSource(Code: TCodeBuffer
): TCustomCodeTool;
var
ANode: TAVLTreeNode;
CurSrc, SearchedSrc: Pointer;
begin
ANode:=FPascalTools.Root;
SearchedSrc:=Pointer(Code);
while (ANode<>nil) do begin
CurSrc:=Pointer(TCustomCodeTool(ANode.Data).Scanner.MainCode);
if CurSrc>SearchedSrc then
ANode:=ANode.Left
else if CurSrc<SearchedSrc then
ANode:=ANode.Right
else begin
Result:=TCustomCodeTool(ANode.Data);
exit;
end;
end;
Result:=nil;
end;
procedure TCodeToolManager.SetError(Id: int64; Code: TCodeBuffer; Line,
Column: integer; const TheMessage: string);
begin
FErrorId:=Id;
FErrorMsg:=TheMessage;
FErrorCode:=Code;
FErrorLine:=Line;
FErrorColumn:=Column;
FErrorTopLine:=FErrorLine;
AdjustErrorTopLine;
WriteError;
end;
function TCodeToolManager.GetCodeToolForSource(Code: TCodeBuffer;
GoToMainCode, ExceptionOnError: boolean): TCustomCodeTool;
// return a codetool for the source
begin
Result:=nil;
if Code=nil then begin
if ExceptionOnError then
raise Exception.Create('TCodeToolManager.GetCodeToolForSource '
+'internal error: Code=nil');
exit;
end;
if GoToMainCode then
Code:=GetMainCode(Code);
Result:=FindCodeToolForSource(Code);
if Result=nil then begin
CreateScanner(Code);
if Code.Scanner=nil then begin
if ExceptionOnError then
raise ECodeToolManagerError.CreateFmt(20170422131430,ctsNoScannerFound,[Code.Filename]);
exit;
end;
Result:=TCodeTool.Create;
Result.Scanner:=Code.Scanner;
FPascalTools.Add(Result);
TCodeTool(Result).Beautifier:=SourceChangeCache.BeautifyCodeOptions;
TCodeTool(Result).OnGetCodeToolForBuffer:=@DoOnGetCodeToolForBuffer;
TCodeTool(Result).OnGetDirectoryCache:=@DoOnGetDirectoryCache;
TCodeTool(Result).OnFindUsedUnit:=@DoOnFindUsedUnit;
TCodeTool(Result).OnGetSrcPathForCompiledUnit:=@DoOnGetSrcPathForCompiledUnit;
TCodeTool(Result).OnGetMethodName:=@DoOnInternalGetMethodName;
TCodeTool(Result).OnRescanFPCDirectoryCache:=@DoOnRescanFPCDirectoryCache;
TCodeTool(Result).OnGatherUserIdentifiers:=@DoOnGatherUserIdentifiers;
TCodeTool(Result).DirectoryCache:=
DirectoryCachePool.GetCache(ExtractFilePath(Code.Filename),
true,true);
Result.OnSetGlobalWriteLock:=@DoOnToolSetWriteLock;
Result.OnTreeChange:=@DoOnToolTreeChange;
TCodeTool(Result).OnParserProgress:=@DoOnParserProgress;
end;
with TCodeTool(Result) do begin
AdjustTopLineDueToComment:=Self.AdjustTopLineDueToComment;
AddInheritedCodeToOverrideMethod:=Self.AddInheritedCodeToOverrideMethod;
CompleteProperties:=Self.CompleteProperties;
SetPropertyVariablename:=Self.SetPropertyVariablename;
SetPropertyVariableIsPrefix:=Self.SetPropertyVariableIsPrefix;
SetPropertyVariableUseConst:=Self.SetPropertyVariableUseConst;
end;
Result.CheckFilesOnDisk:=FCheckFilesOnDisk;
Result.IndentSize:=FIndentSize;
Result.VisibleEditorLines:=FVisibleEditorLines;
Result.JumpSingleLinePos:=FJumpSingleLinePos;
Result.JumpCodeBlockPos:=FJumpCodeBlockPos;
Result.CursorBeyondEOL:=FCursorBeyondEOL;
end;
function TCodeToolManager.FindDirectivesToolForSource(Code: TCodeBuffer
): TDirectivesTool;
var
ANode: TAVLTreeNode;
CurSrc, SearchedSrc: Pointer;
begin
ANode:=FDirectivesTools.Root;
SearchedSrc:=Pointer(Code);
while (ANode<>nil) do begin
CurSrc:=Pointer(TDirectivesTool(ANode.Data).Code);
if CurSrc>SearchedSrc then
ANode:=ANode.Left
else if CurSrc<SearchedSrc then
ANode:=ANode.Right
else begin
Result:=TDirectivesTool(ANode.Data);
exit;
end;
end;
Result:=nil;
end;
procedure TCodeToolManager.ClearCurDirectivesTool;
begin
ClearError;
FCurDirectivesTool:=nil;
end;
function TCodeToolManager.InitCurDirectivesTool(Code: TCodeBuffer): boolean;
begin
Result:=false;
ClearCurDirectivesTool;
FCurDirectivesTool:=TDirectivesTool(GetDirectivesToolForSource(Code,true));
{$IFDEF CTDEBUG}
DebugLn('[TCodeToolManager.InitCurDirectivesTool] ',Code.Filename,' ',dbgs(Code.SourceLength));
{$ENDIF}
Result:=true;
end;
function TCodeToolManager.GetDirectivesToolForSource(Code: TCodeBuffer;
ExceptionOnError: boolean): TCompilerDirectivesTree;
begin
if ExceptionOnError then ;
Result:=FindDirectivesToolForSource(Code);
if Result=nil then begin
Result:=TDirectivesTool.Create;
Result.Code:=Code;
FDirectivesTools.Add(Result);
end;
Result.NestedComments:=GetNestedCommentsFlagForFile(Code.Filename);
end;
procedure TCodeToolManager.SetAbortable(const AValue: boolean);
begin
if FAbortable=AValue then exit;
FAbortable:=AValue;
end;
procedure TCodeToolManager.SetAddInheritedCodeToOverrideMethod(
const AValue: boolean);
begin
if FAddInheritedCodeToOverrideMethod=AValue then exit;
FAddInheritedCodeToOverrideMethod:=AValue;
if FCurCodeTool<>nil then
FCurCodeTool.AddInheritedCodeToOverrideMethod:=AValue;
end;
function TCodeToolManager.DoOnGetCodeToolForBuffer(Sender: TObject;
Code: TCodeBuffer; GoToMainCode: boolean): TFindDeclarationTool;
begin
{$IFDEF CTDEBUG}
DbgOut('[TCodeToolManager.OnGetCodeToolForBuffer]');
if Sender is TCustomCodeTool then
DbgOut(' Sender=',TCustomCodeTool(Sender).MainFilename);
debugln(' Code=',Code.Filename);
{$ENDIF}
Result:=TFindDeclarationTool(GetCodeToolForSource(Code,GoToMainCode,true));
end;
function TCodeToolManager.DoOnGetDirectoryCache(const ADirectory: string
): TCTDirectoryCache;
begin
Result:=DirectoryCachePool.GetCache(ADirectory,true,true);
end;
procedure TCodeToolManager.ActivateWriteLock;
begin
if FWriteLockCount=0 then begin
// start a new write lock
if FWriteLockStep<>$7fffffff then
inc(FWriteLockStep)
else
FWriteLockStep:=-$7fffffff;
SourceCache.GlobalWriteLockIsSet:=true;
SourceCache.GlobalWriteLockStep:=FWriteLockStep;
end;
inc(FWriteLockCount);
{$IFDEF CTDEBUG}
DebugLn('[TCodeToolManager.ActivateWriteLock] FWriteLockCount=',dbgs(FWriteLockCount),' FWriteLockStep=',dbgs(FWriteLockStep));
{$ENDIF}
end;
procedure TCodeToolManager.DeactivateWriteLock;
begin
if FWriteLockCount>0 then begin
dec(FWriteLockCount);
if FWriteLockCount=0 then begin
// end the write lock
if FWriteLockStep<>$7fffffff then
inc(FWriteLockStep)
else
FWriteLockStep:=-$7fffffff;
SourceCache.GlobalWriteLockIsSet:=false;
SourceCache.GlobalWriteLockStep:=FWriteLockStep;
end;
end;
{$IFDEF CTDEBUG}
DebugLn('[TCodeToolManager.DeactivateWriteLock] FWriteLockCount=',dbgs(FWriteLockCount),' FWriteLockStep=',dbgs(FWriteLockStep));
{$ENDIF}
end;
procedure TCodeToolManager.IncreaseChangeStep;
begin
if FChangeStep<>High(Integer) then
inc(FChangeStep)
else
FChangeStep:=Low(Integer);
end;
procedure TCodeToolManager.GetCodeTreeNodesDeletedStep(out
NodesDeletedStep: integer);
begin
NodesDeletedStep:=FCodeTreeNodesDeletedStep;
end;
procedure TCodeToolManager.AddHandlerToolTreeChanging(
const OnToolTreeChanging: TOnToolTreeChanging);
begin
AddHandler(ctmOnToolTreeChanging,TMethod(OnToolTreeChanging));
end;
procedure TCodeToolManager.RemoveHandlerToolTreeChanging(
const OnToolTreeChanging: TOnToolTreeChanging);
begin
RemoveHandler(ctmOnToolTreeChanging,TMethod(OnToolTreeChanging));
end;
function TCodeToolManager.GetResourceTool: TResourceCodeTool;
begin
if FResourceTool=nil then FResourceTool:=TResourceCodeTool.Create;
Result:=FResourceTool;
end;
function TCodeToolManager.GetOwnerForCodeTreeNode(ANode: TCodeTreeNode
): TObject;
var
AToolNode: TAVLTreeNode;
CurTool: TCustomCodeTool;
RootCodeTreeNode: TCodeTreeNode;
CurDirTool: TCompilerDirectivesTree;
begin
Result:=nil;
if ANode=nil then exit;
RootCodeTreeNode:=ANode.GetRoot;
// search in codetools
AToolNode:=FPascalTools.FindLowest;
while (AToolNode<>nil) do begin
CurTool:=TCustomCodeTool(AToolNode.Data);
if (CurTool.Tree<>nil) and (CurTool.Tree.Root=RootCodeTreeNode) then begin
Result:=CurTool;
exit;
end;
AToolNode:=FPascalTools.FindSuccessor(AToolNode);
end;
// search in directivestools
AToolNode:=FDirectivesTools.FindLowest;
while (AToolNode<>nil) do begin
CurDirTool:=TCompilerDirectivesTree(AToolNode.Data);
if (CurDirTool.Tree<>nil) and (CurDirTool.Tree.Root=RootCodeTreeNode) then
begin
Result:=CurDirTool;
exit;
end;
AToolNode:=FDirectivesTools.FindSuccessor(AToolNode);
end;
end;
function TCodeToolManager.DirectoryCachePoolGetString(const ADirectory: string;
const AStringType: TCTDirCacheString): string;
begin
case AStringType of
ctdcsUnitPath: Result:=GetUnitPathForDirectory(ADirectory,false);
ctdcsSrcPath: Result:=GetSrcPathForDirectory(ADirectory,false);
ctdcsIncludePath: Result:=GetIncludePathForDirectory(ADirectory,false);
ctdcsCompleteSrcPath: Result:=GetCompleteSrcPathForDirectory(ADirectory,false);
ctdcsUnitLinks: Result:=GetUnitLinksForDirectory(ADirectory,false);
ctdcsUnitSet: Result:=GetUnitSetIDForDirectory(ADirectory,false);
ctdcsFPCUnitPath: Result:=GetFPCUnitPathForDirectory(ADirectory,false);
ctdcsNamespaces: Result:=GetNamespacesForDirectory(ADirectory,false);
else RaiseCatchableException('');
end;
end;
function TCodeToolManager.DirectoryCachePoolFindVirtualFile(
const Filename: string): string;
var
Code: TCodeBuffer;
begin
Result:='';
if (Filename='') or (System.Pos(PathDelim,Filename)>0) then
exit;
Code:=FindFile(Filename);
if Code<>nil then
Result:=Code.Filename;
end;
function TCodeToolManager.DirectoryCachePoolGetUnitFromSet(const UnitSet,
AnUnitName: string; SrcSearchRequiresPPU: boolean): string;
var
Changed: boolean;
UnitSetCache: TFPCUnitSetCache;
begin
Result:='';
UnitSetCache:=CompilerDefinesCache.FindUnitSetWithID(UnitSet,Changed,false);
if UnitSetCache=nil then begin
debugln(['TCodeToolManager.DirectoryCachePoolGetUnitFromSet invalid UnitSet="',dbgstr(UnitSet),'"']);
exit;
end;
if Changed then begin
debugln(['TCodeToolManager.DirectoryCachePoolGetUnitFromSet outdated UnitSet="',dbgstr(UnitSet),'"']);
exit;
end;
Result:=UnitSetCache.GetUnitSrcFile(AnUnitName,SrcSearchRequiresPPU);
end;
function TCodeToolManager.DirectoryCachePoolGetCompiledUnitFromSet(
const UnitSet, AnUnitName: string): string;
var
Changed: boolean;
UnitSetCache: TFPCUnitSetCache;
begin
Result:='';
UnitSetCache:=CompilerDefinesCache.FindUnitSetWithID(UnitSet,Changed,false);
if UnitSetCache=nil then begin
debugln(['TCodeToolManager.DirectoryCachePoolGetCompiledUnitFromSet invalid UnitSet="',dbgstr(UnitSet),'"']);
exit;
end;
if Changed then begin
debugln(['TCodeToolManager.DirectoryCachePoolGetCompiledUnitFromSet outdated UnitSet="',dbgstr(UnitSet),'"']);
exit;
end;
Result:=UnitSetCache.GetCompiledUnitFile(AnUnitName);
end;
procedure TCodeToolManager.DirectoryCachePoolIterateFPCUnitsFromSet(
const UnitSet: string; const Iterate: TCTOnIterateFile);
var
Changed: boolean;
UnitSetCache: TFPCUnitSetCache;
aConfigCache: TPCTargetConfigCache;
Node: TAVLTreeNode;
Item: PStringToStringItem;
begin
UnitSetCache:=CompilerDefinesCache.FindUnitSetWithID(UnitSet,Changed,false);
if UnitSetCache=nil then begin
debugln(['TCodeToolManager.DirectoryCachePoolIterateFPCUnitsFromSet invalid UnitSet="',dbgstr(UnitSet),'"']);
exit;
end;
if Changed then begin
debugln(['TCodeToolManager.DirectoryCachePoolIterateFPCUnitsFromSet outdated UnitSet="',dbgstr(UnitSet),'"']);
exit;
end;
aConfigCache:=UnitSetCache.GetConfigCache(false);
if (aConfigCache=nil) or (aConfigCache.Units=nil) then exit;
Node:=aConfigCache.Units.Tree.FindLowest;
while Node<>nil do begin
Item:=PStringToStringItem(Node.Data);
Iterate(Item^.Value);
Node:=aConfigCache.Units.Tree.FindSuccessor(Node);
end;
end;
procedure TCodeToolManager.AddHandler(HandlerType: TCodeToolManagerHandler;
const Handler: TMethod);
begin
if Handler.Code=nil then RaiseCatchableException('TCodeToolManager.AddHandler');
if FHandlers[HandlerType]=nil then
FHandlers[HandlerType]:=TMethodList.Create;
FHandlers[HandlerType].Add(Handler);
end;
procedure TCodeToolManager.RemoveHandler(HandlerType: TCodeToolManagerHandler;
const Handler: TMethod);
begin
FHandlers[HandlerType].Remove(Handler);
end;
procedure TCodeToolManager.DoOnToolSetWriteLock(Lock: boolean);
begin
if Lock then ActivateWriteLock else DeactivateWriteLock;
end;
procedure TCodeToolManager.DoOnToolGetChangeSteps(out SourcesChangeStep,
FilesChangeStep: int64; out InitValuesChangeStep: integer);
begin
SourcesChangeStep:=SourceCache.ChangeStamp;
FilesChangeStep:=FileStateCache.TimeStamp;
InitValuesChangeStep:=DefineTree.ChangeStep;
end;
procedure TCodeToolManager.ConsistencyCheck;
begin
if FCurCodeTool<>nil then begin
FCurCodeTool.ConsistencyCheck;
end;
DefinePool.ConsistencyCheck;
DefineTree.ConsistencyCheck;
SourceCache.ConsistencyCheck;
GlobalValues.ConsistencyCheck;
SourceChangeCache.ConsistencyCheck;
FPascalTools.ConsistencyCheck;
FDirectivesTools.ConsistencyCheck;
end;
procedure TCodeToolManager.WriteDebugReport(WriteTool,
WriteDefPool, WriteDefTree, WriteCache, WriteGlobalValues,
WriteMemStats: boolean);
begin
DebugLn('[TCodeToolManager.WriteDebugReport]');
if FCurCodeTool<>nil then begin
if WriteTool then begin
FCurCodeTool.WriteDebugTreeReport;
if FCurCodeTool.Scanner<>nil then
FCurCodeTool.Scanner.WriteDebugReport;
end;
end;
if WriteDefPool then
DefinePool.WriteDebugReport
else
DefinePool.ConsistencyCheck;
if WriteDefTree then
DefineTree.WriteDebugReport
else
DefineTree.ConsistencyCheck;
if WriteCache then
SourceCache.WriteDebugReport
else
SourceCache.ConsistencyCheck;
if WriteGlobalValues then
GlobalValues.WriteDebugReport
else
GlobalValues.ConsistencyCheck;
if WriteMemStats then WriteMemoryStats;
ConsistencyCheck;
end;
procedure TCodeToolManager.WriteMemoryStats;
var
Node: TAVLTreeNode;
ATool: TEventsCodeTool;
Stats: TCTMemStats;
begin
DebugLn(['Memory stats: ']);
Stats:=TCTMemStats.Create;
// boss
Stats.Add('Boss',
PtrUInt(InstanceSize)
+MemSizeString(FErrorMsg)
+MemSizeString(FSetPropertyVariablename)
+PtrUInt(SizeOf(FSetPropertyVariableIsPrefix))
+PtrUInt(SizeOf(FSetPropertyVariableUseConst))
+MemSizeString(FSourceExtensions)
);
if DefinePool<>nil then
DefinePool.CalcMemSize(Stats);
if DefineTree<>nil then
DefineTree.CalcMemSize(Stats);
if SourceCache<>nil then
SourceCache.CalcMemSize(Stats);
if SourceChangeCache<>nil then
SourceChangeCache.CalcMemSize(Stats);
if GlobalValues<>nil then
Stats.Add('GlobalValues',GlobalValues.CalcMemSize);
if DirectoryCachePool<>nil then
DirectoryCachePool.CalcMemSize(Stats);
if IdentifierList<>nil then
Stats.Add('IdentifierList',IdentifierList.CalcMemSize);
if IdentifierHistory<>nil then
Stats.Add('IdentifierHistory',IdentifierHistory.CalcMemSize);
if Positions<>nil then
Stats.Add('Positions',Positions.CalcMemSize);
if FDirectivesTools<>nil then begin
Stats.Add('FDirectivesTools.Count',FDirectivesTools.Count);
// ToDo
end;
if FPascalTools<>nil then begin
Stats.Add('PascalTools.Count',FPascalTools.Count);
Stats.Add('PascalTools',PtrUInt(FPascalTools.Count)*SizeOf(Node));
Node:=FPascalTools.FindLowest;
while Node<>nil do begin
ATool:=TCodeTool(Node.Data);
ATool.CalcMemSize(Stats);
Node:=FPascalTools.FindSuccessor(Node);
end;
end;
Stats.Add('KeywordFuncLists.Global',KeywordFuncLists.CalcMemSize);
Stats.Add('FileStateCache',FileStateCache.CalcMemSize);
Stats.Add('GlobalIdentifierTree',GlobalIdentifierTree.CalcMemSize);
Stats.WriteReport;
Stats.Free;
end;
//-----------------------------------------------------------------------------
function FindIncFileInCfgCache(const Name: string; out ExpFilename: string): boolean;
var
CfgCache: TPCTargetConfigCache;
UnitSet: TFPCUnitSetCache;
begin
// search the include file in directories defines in fpc.cfg (by -Fi option)
UnitSet:=CodeToolBoss.GetUnitSetForDirectory('');
if UnitSet<>nil then begin
CfgCache:=UnitSet.GetConfigCache(false);
Result:=Assigned(CfgCache) and Assigned(CfgCache.Includes)
and CfgCache.Includes.GetString(Name,ExpFilename);
end
else
Result:=False;
end;
initialization
CodeToolBoss:=TCodeToolManager.Create;
OnFindOwnerOfCodeTreeNode:=@GetOwnerForCodeTreeNode;
BasicCodeTools.FindIncFileInCfgCache:=@FindIncFileInCfgCache;
finalization
{$IFDEF CTDEBUG}
DebugLn('codetoolmanager.pas - finalization');
{$ENDIF}
OnFindOwnerOfCodeTreeNode:=nil;
CodeToolBoss.Free;
CodeToolBoss:=nil;
FreeAndNil(CTTemplateExpander);
{$IFDEF CTDEBUG}
DebugLn('codetoolmanager.pas - finalization finished');
{$ENDIF}
end.
|