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
|
{ Copyright (C) 2006
*****************************************************************************
See the file COPYING.modifiedLGPL.txt, included in this distribution,
for details about the license.
*****************************************************************************
Author: Mattias Gaertner
}
unit IDEExternToolIntf;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, Math, Laz_AVL_Tree,
// LazUtils
UTF8Process, LazFileUtils, LazFileCache, LazMethodList, LazLoggerBase,
// IdeIntf
ObjInspStrConsts;
const
SubToolFPC = 'FPC';
SubToolFPCPriority = 100;
SubToolFPCLinker = 'FPCLinker';
SubToolFPCRes = 'FPCRes';
SubToolFPCWindRes = 'FPCWindRes';
SubToolFPCAssembler = 'FPCAssembler';
SubToolPas2js = 'Pas2JS';
SubToolPas2jsPriority = 99;
SubToolMake = 'make';
SubToolMakePriority = 1000; // higher than FPC
SubToolDefault = 'Show all'; // this parser simply writes all output to Messages window
SubToolDefaultPriority = 0;
AbortedExitCode = 12321;
MsgAttrDiskFilename = 'DiskFile';
const
IDEToolCompilePackage = 'Package';
IDEToolCompileProject = 'Project'; // the active project
IDEToolCompileIDE = 'IDE';
type
{ TIDEExternalToolData
When the IDE compiles a package or a project it creates an instance and sets
TAbstractExternalTool.Data. }
TIDEExternalToolData = class
public
Kind: string; // e.g. IDEToolCompilePackage or IDEToolCompileProject
ModuleName: string; // e.g. the package name
Filename: string; // e.g. the lpi or lpk filename
Flags: TStrings;
constructor Create(aKind, aModuleName, aFilename: string);
destructor Destroy; override;
end;
type
TETShareStringEvent = procedure(var s: string) of object;
TMessageLineUrgency = (
mluNone,
mluProgress, // time and statistics about the run
mluDebug, // extreme verbosity, only useful for tool authors
mluVerbose3, // all infos
mluVerbose2, // almost all infos
mluVerbose, // extra infos
mluHint, // tool found something unusual
mluNote, // maybe wrong or unnecessary
mluWarning, // probably something is wrong
mluImportant, // message has no urgency level, but should be shown
mluError, // tool could not finish, some tools can still continue
mluFatal, // critical error in input, tool had to abort
mluPanic // bug in tool
);
TMessageLineUrgencies = set of TMessageLineUrgency;
const
MessageLineUrgencyNames: array[TMessageLineUrgency] of string = (
'None',
'Progress',
'Debug',
'Verbose',
'Verbose',
'Verbose',
'Hint',
'Note',
'Warning',
'Misc',
'Error',
'Fatal',
'Panic'
);
type
TMessageLines = class;
TAbstractExternalTool = class;
TMessageLineFlag = (
mlfLeftToken, // position is about left token, otherwise right token
mlfFixed, // reason for the messages was resolved, e.g. quick fixed
mlfHiddenByIDEDirective,
mlfHiddenByIDEDirectiveValid,
mlfTestBuildFile // Filename is not absolute, the test filename on disk is in Attributes[MsgAttrDiskFilename]
);
TMessageLineFlags = set of TMessageLineFlag;
{ TMessageLine }
TMessageLine = packed class
private
// pointers
FLines: TMessageLines; // owner
FMsg: string; // fixed/improved message
FFilename: string;
FOriginalLine: string;
FSubTool: string;
FTranslatedMsg: string; // translated message
fAttributes: TStrings;
// native types
FSubType: PtrUInt;
FChangeStamp: int64;
// special types
FOutputIndex: integer;
FColumn: integer;
FIndex: integer; // 0-based, position in Lines.Items
FLine: integer;
FMsgID: integer;
FUrgency: TMessageLineUrgency;
FFlags: TMessageLineFlags;
function GetAttribute(const Identifier: string): string;
procedure SetAttribute(const Identifier: string; const AValue: string);
procedure SetColumn(const AValue: integer);
procedure SetFilename(AValue: string);
procedure SetFlags(AValue: TMessageLineFlags);
procedure SetLine(const AValue: integer);
procedure SetMsg(AValue: string);
procedure SetMsgID(AValue: integer);
procedure SetSubTool(AValue: string);
procedure SetSubType(AValue: PtrUInt);
procedure SetTranslatedMsg(AValue: string);
procedure SetUrgency(AValue: TMessageLineUrgency);
procedure SortedSrcPosBind;
procedure SortedSrcPosUnbind;
protected
procedure SetLines(AValue: TMessageLines);
public
constructor Create; virtual;
destructor Destroy; override;
procedure Assign(Source: TMessageLine);
function Equals(Obj: TObject): boolean; override;
procedure Clear;
function GetShortFilename: string; inline;
function GetRelativeFilename: string;
function GetFullFilename: string; inline;
procedure ShareStrings(const ShareStringEvent: TETShareStringEvent); virtual;
procedure SetSourcePosition(NewFilename: string; NewLine, NewColumn: integer);
procedure IncreaseChangeStamp;
procedure MarkFixed;
function HasSourcePosition: boolean;
procedure GetAttributes(List: TStrings);
function GetToolData: TIDEExternalToolData; virtual;
public
property Index: integer read FIndex; // index in Lines (Note: Lines can have more or less items than the raw output has text lines)
property Urgency: TMessageLineUrgency read FUrgency write SetUrgency;
property SubTool: string read FSubTool write SetSubTool; // e.g. SubToolFPC, SubToolMake, SubToolFPCLinker
property SubType: PtrUInt read FSubType write SetSubType; // depends on SubTool
property Msg: string read FMsg write SetMsg; // improved message without filename, line, column, setting it clears TranslatedMsg
property MsgID: integer read FMsgID write SetMsgID; // message id (depends on parser, e.g. fpc writes them with -vq, MsgID<>0 if valid)
property TranslatedMsg: string read FTranslatedMsg write SetTranslatedMsg; // translated Msg, set this after Msg
property Filename: string read FFilename write SetFilename; // full file name, relative if not found or not yet searched
property Line: integer read FLine write SetLine; // valid if >0
property Column: integer read FColumn write SetColumn; // valid if >0
property Flags: TMessageLineFlags read FFlags write SetFlags;
property Attribute[const Identifier: string]: string read GetAttribute write SetAttribute; default; // arbitrary attributes
property ChangeStamp: int64 read FChangeStamp;
property OutputIndex: integer read FOutputIndex; // index in raw Output, there can be only one message per output line
property Lines: TMessageLines read FLines write SetLines;
property OriginalLine: string read FOriginalLine;
end;
TMessageLineClass = class of TMessageLine;
{ TMessageLineEnumerator }
TMessageLineEnumerator = class
private
FFilename: string;
FMaxLine: integer;
FMinLine: integer;
protected
FTree: TAvlTree;
FCurrent: TAvlTreeNode;
function GetCurrent: TMessageLine; inline;
public
constructor Create(Tree: TAvlTree; const aFilename: string;
aMinLine, aMaxLine: integer);
function GetEnumerator: TMessageLineEnumerator;
function MoveNext: boolean;
property Current: TMessageLine read GetCurrent;
property Filename: string read FFilename;
property MinLine: integer read FMinLine;
property MaxLine: integer read FMaxLine;
end;
{ TMessageLines }
TETMarksFixedEvent = procedure(ListOfTMessageLine: TFPList) of object;
TMessageLines = class
private
FChangeStamp: int64;
FCritSec: TRTLCriticalSection;
FBaseDirectory: string;
fItems: TFPList; // list of TMessageLine
FMessageLineClass: TMessageLineClass;
FOnMarksFixed: TETMarksFixedEvent;
FOwner: TObject;
FSortedForSrcPos: TAvlTree; // tree of TMessageLine sorted for Filename, Line, Column, OutputIndex, Index
FUpdateSortedSrcPos: boolean;
fChangedHandler: TMethodList;
fMarkedFixed: TAvlTree; // list of TMessageLine
function GetItems(Index: integer): TMessageLine;
procedure SetBaseDirectory(const AValue: string);
procedure LineChanged(Line: TMessageLine);
public
UrgencyCounts: array[TMessageLineUrgency] of integer;
constructor Create(aOwner: TObject; aMsgLineClass: TMessageLineClass);
destructor Destroy; override;
property Owner: TObject read FOwner;
procedure EnterCriticalSection; virtual; // always use before access
procedure LeaveCriticalSection; virtual;
function Count: integer; inline;
procedure Clear;
property Items[Index: integer]: TMessageLine read GetItems; default;
function GetLastLine: TMessageLine;
property BaseDirectory: string read FBaseDirectory write SetBaseDirectory; // always trimmed and with trailing /
function CreateLine(OutputIndex: integer): TMessageLine; // create, but do not yet add it
procedure Add(MsgLine: TMessageLine);
procedure Remove(MsgLine: TMessageLine);
procedure Delete(MsgLine: TMessageLine);
procedure MarkFixed(MsgLine: TMessageLine); // (main thread) request to add mlfFixed, will be applied in ApplyFixedMarks
procedure ApplyFixedMarks; virtual; // (main thread) apply mlfFixed to all messages added via MarkFixed
procedure FetchAll(SrcLines: TMessageLines);
procedure SourceLinesInserted(Filename: string; Line, InsertedCount: integer);
procedure SourceLinesDeleted(Filename: string; FirstLine, DeletedCount: integer);
property MessageLineClass: TMessageLineClass read FMessageLineClass;
property OnMarksFixed: TETMarksFixedEvent read FOnMarksFixed write FOnMarksFixed;
property ChangeStamp: int64 read FChangeStamp;
procedure IncreaseChangeStamp; inline;
function IndexOfOutputIndex(OutputIndex: integer): integer;
function EnumerateFile(aFilename: string;
MinLine: integer = 0; MaxLine: integer = High(integer)): TMessageLineEnumerator;
property UpdateSortedSrcPos: boolean // disable this while updating many Filename,Line,Col without changes the order
read FUpdateSortedSrcPos write FUpdateSortedSrcPos;
procedure AddChangedHandler(const OnLineChanged: TNotifyEvent;
AsFirst: boolean = false);
procedure RemoveChangedHandler(const OnLineChanged: TNotifyEvent);
procedure ConsistencyCheck;
end;
{ The output is parsed in chunks (multiple lines) at a time.
First every output line is passed to each parser via ReadLine.
After the whole chunk was processed via ReadLine, each parser is called
with ImproveMessages up to three times. During ImproveMessages the Tool
is locked via its critical section.
After ImproveMessages the chunk is passed to the Views.
}
TExtToolParserSyncPhase = (
etpspAfterReadLine, { (worker thread) after lines were created by parsers
via ReadLine and added to Tool.WorkerMessages. In this phase parsers can
look what other parsers have created and/or to decide if they need some
data from the IDE (NeedSynchronize:=true) }
etpspSynchronized, { (main thread) parsers can collect data from the IDE.
If the data need processing set NeedAfterSync:=true }
etpspAfterSync { (worker thread) use the collected IDE data }
);
TExtToolParserSyncPhases = set of TExtToolParserSyncPhase;
{ TExtToolParser
Read the output of a tool, for example the output of the Free Pascal compiler.
It does not filter. Some parsers can work together, for example make and fpc.
Usage: Tool.AddParsers('SubTool') or Tool.AddParser(ParseClass);
}
TExtToolParser = class(TComponent)
private
FNeedAfterSync: boolean;
FNeedSynchronize: boolean;
FTool: TAbstractExternalTool;
public
destructor Destroy; override; // (main thread)
procedure Init; virtual; // called after macros resolved, before starting thread (main thread)
procedure InitReading; virtual; // called if process started, before first line (worker thread)
procedure Done; virtual; // called after process stopped (worker thread)
procedure ReadLine(Line: string; OutputIndex: integer; var Handled: boolean); virtual; abstract; // (worker thread)
function CreateMsgLine(OutputIndex: integer): TMessageLine; // (worker thread)
procedure AddMsgLine(MsgLine: TMessageLine); virtual; // (worker thread)
property Tool: TAbstractExternalTool read FTool;// set when added to a tool
property NeedSynchronize: boolean read FNeedSynchronize write FNeedSynchronize; // set this in ImproveMessages phase etpspAfterReadLine
property NeedAfterSync: boolean read FNeedAfterSync write FNeedAfterSync; // set this in ImproveMessages phase etpspSynchronized
procedure ImproveMessages({%H-}aPhase: TExtToolParserSyncPhase); virtual; // Tool.WorkerMessages, Tool is in Critical section
procedure ConsistencyCheck; virtual;
class function GetParserName: string; virtual;
class function GetLocalizedParserName: string; virtual;
class function IsSubTool(const SubTool: string): boolean; deprecated 'use CanParseSubTool';
class function CanParseSubTool(const SubTool: string): boolean; virtual;
class function GetMsgPattern({%H-}SubTool: string; {%H-}MsgID: integer;
out Urgency: TMessageLineUrgency): string; virtual;
class function GetMsgHint({%H-}SubTool: string; {%H-}MsgID: integer): string; virtual;
class function GetMsgParser(Msg: TMessageLine; ParserClass: TClass): TExtToolParser;
class function DefaultSubTool: string; virtual; abstract;
class function Priority: integer; virtual; // higher comes first
end;
TExtToolParserClass = class of TExtToolParser;
{ TFPCParser - standard parser for Free Pascal messages, implemented by IDE }
TFPCParser = class(TExtToolParser)
private
FHideHintsSenderNotUsed: boolean;
FHideHintsUnitNotUsedInMainSource: boolean;
FShowLinesCompiled: boolean;
protected
FFilesToIgnoreUnitNotUsed: TStrings;
public
class function GetFPCParser(Msg: TMessageLine): TFPCParser;
function GetFPCMsgIDPattern(MsgID: integer): string; virtual; abstract;
class function MsgLineIsId(Msg: TMessageLine; MsgId: integer;
out Value1, Value2: string): boolean; virtual; abstract;
class function GetFPCMsgPattern(Msg: TMessageLine): string; virtual; abstract;
class function GetFPCMsgValue1(Msg: TMessageLine): string; virtual; abstract;
class function GetFPCMsgValues(Msg: TMessageLine; out Value1, Value2: string): boolean; virtual; abstract;
property FilesToIgnoreUnitNotUsed: TStrings read FFilesToIgnoreUnitNotUsed
write FFilesToIgnoreUnitNotUsed;
property ShowLinesCompiled: boolean read FShowLinesCompiled
write FShowLinesCompiled default false;
property HideHintsSenderNotUsed: boolean read FHideHintsSenderNotUsed
write FHideHintsSenderNotUsed default true;
property HideHintsUnitNotUsedInMainSource: boolean
read FHideHintsUnitNotUsedInMainSource
write FHideHintsUnitNotUsedInMainSource default true;
end;
TFPCParserClass = class of TFPCParser;
var
IDEFPCParser: TFPCParserClass = nil;
IDEPas2jsParser: TFPCParserClass = nil;
type
{ TMakeParser - standard parser for 'make' messages, implemented by IDE }
TMakeParser = class(TExtToolParser)
end;
{ TDefaultParser - simple parser for simple text output, no filtering }
TDefaultParser = class(TExtToolParser)
public
procedure ReadLine(Line: string; OutputIndex: integer; var Handled: boolean
); override;
class function DefaultSubTool: string; override;
class function GetLocalizedParserName: string; override;
class function Priority: integer; override;
end;
const
DefaultETViewMinUrgency = mluHint;
type
{ TExtToolView
Implemented by the IDE.
When a tool with a scanner but no View is started the IDE automatically
creates a View.
You can create a View with IDEMessagesWindow.CreateView(Title) }
TExtToolView = class(TComponent)
private
FCaption: string;
FExitCode: integer;
FExitStatus: integer;
FLines: TMessageLines;
FMinUrgency: TMessageLineUrgency;
FOnChanged: TNotifyEvent;
FPendingLines: TMessageLines;
FPendingProgressLine: TMessageLine;
FProgressLine: TMessageLine;
FRunning: boolean;
FSummaryMsg: string;
FTool: TAbstractExternalTool;
protected
FLastWorkerMessageCount: integer;
FMessageLineClass: TMessageLineClass;
procedure CreateLines; virtual;
procedure FetchAllPending; virtual; // (main thread)
procedure ToolExited; virtual; // (main thread) called by InputClosed
procedure QueueAsyncOnChanged; virtual; // (worker thread)
procedure RemoveAsyncOnChanged; virtual; // (main or worker thread)
public
constructor Create(AOwner: TComponent); override; // (main thread)
destructor Destroy; override; // (main thread)
procedure ProcessNewMessages({%H-}AThread: TThread); virtual; // (worker thread, Tool is in Critical section)
procedure ClearLines; // (main thread)
function ApplyPending: boolean; virtual; // true if something changed (main thread)
procedure InputClosed; virtual; // called by Tool when source closed (main thread)
function LineFits(Line: TMessageLine): boolean; virtual; // called by ProcessNewMessages (worker thread)
procedure EnterCriticalSection; virtual; // Note: when using Tool and View: always lock Tool before View
procedure LeaveCriticalSection; virtual;
procedure ConsistencyCheck; virtual;
public
property Running: boolean read FRunning write FRunning;
property SummaryMsg: string read FSummaryMsg write FSummaryMsg;
property Tool: TAbstractExternalTool read FTool;
property Caption: string read FCaption write FCaption;
property OnChanged: TNotifyEvent read FOnChanged write FOnChanged; // called in main thread
property ExitCode: integer read FExitCode write FExitCode;
property ExitStatus: integer read FExitStatus write FExitStatus;
property MinUrgency: TMessageLineUrgency read FMinUrgency write FMinUrgency default DefaultETViewMinUrgency; // hide messages below this
property MessageLineClass: TMessageLineClass read FMessageLineClass;
function HasFinished: boolean; virtual; // not running, no pending messages
public
// needs critical section
property PendingLines: TMessageLines read FPendingLines write FPendingLines;
property PendingProgressLine: TMessageLine read FPendingProgressLine write FPendingProgressLine;
property LastWorkerMessageCount: integer read FLastWorkerMessageCount;
public
// only main thread
property Lines: TMessageLines read FLines;
property ProgressLine: TMessageLine read FProgressLine; // valid if ProgressLine.Msg<>''
end;
TExtToolViewClass = class of TExtToolView;
TExternalToolStage = (
etsInit, // just created, set your parameters, then call Execute
etsInitializing, // set in Execute, during resolving macros
etsWaitingForStart, // waiting for a process slot
etsStarting, // creating the thread and process
etsRunning, // process started
etsWaitingForStop, // waiting for process to stop
etsStopped, // process has stopped
etsDestroying // during destructor
);
TExternalToolStages = set of TExternalToolStage;
TExternalToolNewOutputEvent = procedure(Sender: TObject;
FirstNewMsgLine: integer) of object;
TExternalToolHandler = (
ethNewOutput,
ethStopped
);
TExternalToolsBase = class;
TExternalToolGroup = class;
{ TAbstractExternalTool
Implemented by the IDE.
Create one with ExternalToolList.Add or AddDummy.
Access needs Tool.Enter/LeaveCriticalSection. }
TAbstractExternalTool = class(TComponent)
private
FData: TObject;
FEnvironmentOverrides: TStrings;
FEstimatedLoad: int64;
FExitCode: integer;
FExitStatus: integer;
FFreeData: boolean;
FGroup: TExternalToolGroup;
FHint: string;
FReadStdOutBeforeErr: boolean;
FResolveMacrosOnExecute: boolean;
FThread: TThread;
FWorkerDirectory: string;
FWorkerMessages: TMessageLines;
FParsers: TFPList; // list of TExtToolParser
FReferences: TStringList;
FTitle: string;
FTools: TExternalToolsBase;
FViews: TFPList; // list of TExtToolView
FCurrentDirectoryIsTestDir: boolean;
function GetCmdLineParams: string;
function GetParserCount: integer;
function GetParsers(Index: integer): TExtToolParser;
function GetReferences(Index: integer): string;
function GetViews(Index: integer): TExtToolView;
procedure SetCmdLineParams(aParams: string);
procedure SetEnvironmentOverrides(AValue: TStrings);
procedure SetGroup(AValue: TExternalToolGroup);
procedure SetTitle(const AValue: string);
procedure AddHandler(HandlerType: TExternalToolHandler;
const AMethod: TMethod; AsFirst: boolean = true);
procedure RemoveHandler(HandlerType: TExternalToolHandler;
const AMethod: TMethod);
protected
FErrorMessage: string;
FTerminated: boolean;
FHandlers: array[TExternalToolHandler] of TMethodList;
FStage: TExternalToolStage;
FWorkerOutput: TStrings;
FProcess: TProcessUTF8;
FWorkerMessagesClass: TMessageLineClass;
procedure DoCallNotifyHandler(HandlerType: TExternalToolHandler);
function GetExecuteAfter(Index: integer): TAbstractExternalTool; virtual; abstract;
function GetExecuteBefore(Index: integer): TAbstractExternalTool; virtual; abstract;
procedure DoExecute; virtual; abstract; // starts thread, returns immediately
procedure Notification(AComponent: TComponent; Operation: TOperation);
override;
function CanFree: boolean; virtual;
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
procedure EnterCriticalSection; virtual; // always use before access, when using Tool and View: always lock Tool before View
procedure LeaveCriticalSection; virtual;
property Thread: TThread read FThread write FThread;
procedure ConsistencyCheck; virtual;
procedure AutoFree; // (only main thread) free if not in use
property Title: string read FTitle write SetTitle;
property Hint: string read FHint write FHint; // this hint is shown in About dialog
property Data: TObject read FData write FData; // free for user, e.g. the IDE uses TIDEExternalToolData
property FreeData: boolean read FFreeData write FFreeData default false; // true = auto free Data on destroy
property Tools: TExternalToolsBase read FTools;
property Group: TExternalToolGroup read FGroup write SetGroup;
property EstimatedLoad: int64 read FEstimatedLoad write FEstimatedLoad default 1; // used for deciding which tool to run next
// handlers
procedure RemoveAllHandlersOfObject(AnObject: TObject);
procedure AddHandlerOnNewOutput(const OnNewOutput: TExternalToolNewOutputEvent;
AsFirst: boolean = true); // called in main thread, Sender=Tool
procedure RemoveHandlerOnNewOutput(const OnNewOutput: TExternalToolNewOutputEvent);
procedure AddHandlerOnStopped(const OnStopped: TNotifyEvent;
AsFirst: boolean = true); // called in main thread, Sender=Tool
procedure RemoveHandlerOnStopped(const OnStopped: TNotifyEvent);
procedure Reference(Thing: TObject; const Note: string); // add a reference to delay auto freeing, use Release for free
procedure Release(Thing: TObject);
property References[Index: integer]: string read GetReferences;
function ReferenceCount: integer;
// process
property Process: TProcessUTF8 read FProcess;
property EnvironmentOverrides: TStrings read FEnvironmentOverrides
write SetEnvironmentOverrides; // if not empty, then this and IDE's environment will be merged and replace Process.Environment
property CmdLineParams: string read GetCmdLineParams write SetCmdLineParams;
function ResolveMacros: boolean; virtual; abstract; // resolve macros in Process.Executable, Process.CurrentDirectory, Process.Params, Process.Environment on Execute
property ResolveMacrosOnExecute: boolean read FResolveMacrosOnExecute
write FResolveMacrosOnExecute;
property CurrentDirectoryIsTestDir: boolean read FCurrentDirectoryIsTestDir
write FCurrentDirectoryIsTestDir; // virtual files were written to test directory, parsers should reverse source filenames
property Stage: TExternalToolStage read FStage;
procedure Execute; virtual; abstract;
procedure Terminate; virtual; abstract;
procedure WaitForExit; virtual; abstract;
property Terminated: boolean read FTerminated;
property ExitCode: integer read FExitCode write FExitCode;
property ExitStatus: integer read FExitStatus write FExitStatus;
property ErrorMessage: string read FErrorMessage write FErrorMessage; // error executing tool
property ReadStdOutBeforeErr: boolean read FReadStdOutBeforeErr write FReadStdOutBeforeErr;
// output
property WorkerOutput: TStrings read FWorkerOutput; // the raw output
property WorkerDirectory: string read FWorkerDirectory write FWorkerDirectory; // changed by parsers, initialized from Process.CurrentDirectory
property WorkerMessages: TMessageLines read FWorkerMessages; // created by parsers
property WorkerMessagesClass: TMessageLineClass read FWorkerMessagesClass;
// parsers
property ParserCount: integer read GetParserCount;
property Parsers[Index: integer]: TExtToolParser read GetParsers; // sorted for Priority
function AddParsers(const SubTool: string): TExtToolParser; // will be freed on Destroy
function AddParser(ParserClass: TExtToolParserClass): TExtToolParser; // will be freed on Destroy
function AddParserByName(const ParserName: string): TExtToolParser; // will be freed on Destroy
procedure DeleteParser(Parser: TExtToolParser); // disconnect and free
procedure RemoveParser(Parser: TExtToolParser); // disconnect without free
function IndexOfParser(Parser: TExtToolParser): integer;
procedure ClearParsers(Delete: boolean = true);
function FindParser(aParserClass: TExtToolParserClass): TExtToolParser;
function FindParser(const SubTool: string): TExtToolParser;
// viewers
function ViewCount: integer;
property Views[Index: integer]: TExtToolView read GetViews;
function AddView(View: TExtToolView): integer; // (main thread) will *not* be freed on destroy
procedure DeleteView(View: TExtToolView); // (main thread) disconnect and free, this might free the tool
procedure RemoveView(View: TExtToolView); // (main thread) disconnect without free, this might free the tool
function IndexOfView(View: TExtToolView): integer;
procedure ClearViews(Delete: boolean = false); // (main thread), this might free the tool
function FindUnfinishedView: TExtToolView;
// dependencies
procedure AddExecuteBefore(Tool: TAbstractExternalTool); virtual; abstract;
function IsExecutedBefore(Tool: TAbstractExternalTool): Boolean; virtual; abstract;// search recursively
procedure RemoveExecuteBefore(Tool: TAbstractExternalTool); virtual; abstract;
function ExecuteBeforeCount: integer; virtual; abstract;
property ExecuteBefore[Index: integer]: TAbstractExternalTool read GetExecuteBefore;
function ExecuteAfterCount: integer; virtual; abstract;
property ExecuteAfter[Index: integer]: TAbstractExternalTool read GetExecuteAfter;
end;
{ TExternalToolGroup
Hint: Add tools by setting Tool.Group:=Group.
You can create your own descendant classes. }
TExternalToolGroup = class(TComponent)
private
FAbortIfOneFails: boolean;
FErrorMessage: string;
FItems: TFPList; // list of TAbstractExternalTool
function GetItems(Index: integer): TAbstractExternalTool;
procedure InternalRemove(Tool: TAbstractExternalTool); virtual;
procedure InternallAdd(Tool: TAbstractExternalTool); virtual;
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
procedure Clear; virtual;
function Count: integer;
procedure Execute; virtual;
procedure WaitForExit; virtual;
procedure Terminate; virtual;
function AllStopped: boolean;
property Items[Index: integer]: TAbstractExternalTool read GetItems; default;
property AbortIfOneFails: boolean read FAbortIfOneFails write FAbortIfOneFails;
procedure ToolExited(Tool: TAbstractExternalTool); virtual;
property ErrorMessage: string read FErrorMessage write FErrorMessage;
end;
{ TExternalToolsBase
Implemented by an application or the IDE. }
TExternalToolsBase = class(TComponent)
private
function GetItems(Index: integer): TAbstractExternalTool; inline;
protected
fItems: TFPList; // list of TAbstractExternalTool
function GetParsers(Index: integer): TExtToolParserClass; virtual; abstract; // (main thread)
public
constructor Create(aOwner: TComponent); override;
destructor Destroy; override;
function Count: integer; inline;
procedure TerminateAll; virtual; abstract;
procedure Clear; virtual; abstract; // terminate + free all tools
property Items[Index: integer]: TAbstractExternalTool read GetItems; default;
function Add(Title: string): TAbstractExternalTool; virtual; abstract;
function AddDummy(Title: string): TAbstractExternalTool; // a tool, not run, usable for dependencies
function IndexOf(Tool: TAbstractExternalTool): integer; virtual; abstract;
procedure ConsistencyCheck; virtual;
procedure EnterCriticalSection; virtual; abstract;
procedure LeaveCriticalSection; virtual; abstract;
function GetIDEObject(ToolData: TIDEExternalToolData): TObject; virtual; abstract;
procedure HandleMesages; virtual; abstract;
// parsers
procedure RegisterParser(Parser: TExtToolParserClass); virtual; abstract; // (main thread)
procedure UnregisterParser(Parser: TExtToolParserClass); virtual; abstract; // (main thread)
function FindParserForTool(const SubTool: string): TExtToolParserClass; virtual; abstract; // (main thread)
function FindParserWithName(const ParserName: string): TExtToolParserClass; virtual; abstract; // (main thread)
function ParserCount: integer; virtual; abstract; // (main thread)
property Parsers[Index: integer]: TExtToolParserClass read GetParsers; // (main thread)
function GetMsgPattern(SubTool: string; MsgID: integer; out Urgency: TMessageLineUrgency): string; virtual; // (main thread)
function GetMsgHint(SubTool: string; MsgID: integer): string; virtual; // (main thread)
function GetMsgTool(Msg: TMessageLine): TAbstractExternalTool; virtual; abstract;
end;
var
ExternalToolList: TExternalToolsBase = nil; // will be set by the IDE
type
{ TIDEExternalToolOptions }
TETMacroFunction = function(var aValue: string): boolean of object;
TIDEExternalToolOptions = class
private
fCmdLineParams: string;
FCustomMacroFunction: TETMacroFunction;
FEnvironmentOverrides: TStringList;
FExecutable: string;
FHideWindow: boolean;
FHint: string;
FQuiet: boolean;
FResolveMacros: boolean;
FParsers: TStrings;
FShowConsole: boolean;
fTitle: string;
fWorkingDirectory: string;
procedure SetEnvironmentOverrides(AValue: TStringList);
procedure SetParsers(AValue: TStrings);
public
constructor Create;
destructor Destroy; override;
procedure Assign(Source: TIDEExternalToolOptions);
function Equals(Obj: TObject): boolean; override;
procedure Clear;
property Title: string read fTitle write fTitle;
property Hint: string read FHint write FHint;
property Executable: string read FExecutable write FExecutable;
property Filename: string read FExecutable write FExecutable; deprecated; // use Executable instead
property CmdLineParams: string read fCmdLineParams write fCmdLineParams;
property WorkingDirectory: string read fWorkingDirectory write fWorkingDirectory;
property EnvironmentOverrides: TStringList read FEnvironmentOverrides write SetEnvironmentOverrides;
property Scanners: TStrings read FParsers; deprecated 'use Parsers';
property Parsers: TStrings read FParsers write SetParsers;
property ShowConsole: boolean read FShowConsole write FShowConsole default false; // sets poNoConsole/poNewConsole, works only on MSWindows
property HideWindow: boolean read FHideWindow write FHideWindow default true; // sets/unsets swoHide/swoShow, works only on MSWindows
property ResolveMacros: boolean read FResolveMacros write FResolveMacros default true;
property CustomMacroFunction: TETMacroFunction read FCustomMacroFunction write FCustomMacroFunction;
property Quiet: boolean read FQuiet write FQuiet; // no user dialogs about errors
end;
type
TRunExternalTool = function(Tool: TIDEExternalToolOptions): boolean of object;
var
RunExternalTool: TRunExternalTool = nil;// set by the IDE
DefaultMaxProcessCount: integer = 2;// set by the IDE
function CompareMsgLinesSrcPos(MsgLine1, MsgLine2: Pointer): integer;
function StrToMsgLineUrgency(const s: string): TMessageLineUrgency;
function dbgs(u: TMessageLineUrgency): string; overload;
function dbgs(f: TMessageLineFlag): string; overload;
function dbgs(Flags: TMessageLineFlags): string; overload;
function dbgs(s: TExternalToolStage): string; overload;
implementation
function CompareMsgLinesSrcPos(MsgLine1, MsgLine2: Pointer): integer;
var
Line1: TMessageLine absolute MsgLine1;
Line2: TMessageLine absolute MsgLine2;
begin
Result:=CompareFilenames(Line1.Filename,Line2.Filename);
if Result<>0 then exit;
if Line1.Line<Line2.Line then exit(-1)
else if Line1.Line>Line2.Line then exit(1);
if Line1.Column<Line2.Column then exit(-1)
else if Line1.Column>Line2.Column then exit(1);
if Line1.OutputIndex<Line2.OutputIndex then exit(-1)
else if Line1.OutputIndex>Line2.OutputIndex then exit(1);
if Line1.Index<Line2.Index then exit(-1)
else if Line1.Index>Line2.Index then exit(1);
Result:=0;
end;
function StrToMsgLineUrgency(const s: string): TMessageLineUrgency;
begin
for Result:=Low(TMessageLineUrgency) to high(TMessageLineUrgency) do
if SysUtils.CompareText(s,MessageLineUrgencyNames[Result])=0 then exit;
Result:=mluNone;
end;
function dbgs(u: TMessageLineUrgency): string;
begin
Result:='';
WriteStr(Result,u);
end;
function dbgs(f: TMessageLineFlag): string;
begin
Result:='';
WriteStr(Result,f);
end;
function dbgs(Flags: TMessageLineFlags): string;
var
f: TMessageLineFlag;
begin
Result:='';
for f in Flags do begin
if Result<>'' then Result+=',';
Result+=dbgs(f);
end;
Result:='['+Result+']';
end;
function dbgs(s: TExternalToolStage): string;
begin
Result:='';
WriteStr(Result,s);
end;
{ TIDEExternalToolData }
constructor TIDEExternalToolData.Create(aKind, aModuleName, aFilename: string);
begin
Kind:=aKind;
ModuleName:=aModuleName;
Filename:=aFilename;
Flags:=TStringList.Create;
end;
destructor TIDEExternalToolData.Destroy;
begin
FreeAndNil(Flags);
inherited Destroy;
end;
{ TFPCParser }
class function TFPCParser.GetFPCParser(Msg: TMessageLine): TFPCParser;
begin
Result:=TFPCParser(GetMsgParser(Msg,TFPCParser));
end;
{ TIDEExternalToolOptions }
procedure TIDEExternalToolOptions.SetEnvironmentOverrides(AValue: TStringList);
begin
if FEnvironmentOverrides.Equals(AValue) then Exit;
FEnvironmentOverrides.Assign(AValue);
end;
procedure TIDEExternalToolOptions.SetParsers(AValue: TStrings);
begin
if FParsers.Equals(AValue) then Exit;
FParsers.Assign(AValue);
end;
constructor TIDEExternalToolOptions.Create;
begin
ResolveMacros:=true;
FEnvironmentOverrides:=TStringList.Create;
FParsers:=TStringList.Create;
FHideWindow:=true;
end;
destructor TIDEExternalToolOptions.Destroy;
begin
FreeAndNil(FEnvironmentOverrides);
FreeAndNil(FParsers);
inherited Destroy;
end;
procedure TIDEExternalToolOptions.Assign(Source: TIDEExternalToolOptions);
begin
Title:=Source.Title;
Executable:=Source.Executable;
CmdLineParams:=Source.CmdLineParams;
WorkingDirectory:=Source.WorkingDirectory;
EnvironmentOverrides:=Source.EnvironmentOverrides;
Parsers:=Source.Parsers;
ShowConsole:=Source.ShowConsole;
HideWindow:=Source.HideWindow;
ResolveMacros:=Source.ResolveMacros;
CustomMacroFunction:=Source.CustomMacroFunction;
Quiet:=Source.Quiet;
end;
function TIDEExternalToolOptions.Equals(Obj: TObject): boolean;
var
Source: TIDEExternalToolOptions;
begin
if Obj=Self then exit(true);
if Obj is TIDEExternalToolOptions then begin
Source:=TIDEExternalToolOptions(Obj);
Result:=(Title=Source.Title)
and (Executable=Source.Executable)
and (CmdLineParams=Source.CmdLineParams)
and (WorkingDirectory=Source.WorkingDirectory)
and EnvironmentOverrides.Equals(Source.EnvironmentOverrides)
and Parsers.Equals(Source.Parsers)
and (ShowConsole=Source.ShowConsole)
and (HideWindow=Source.HideWindow)
and (ResolveMacros=Source.ResolveMacros)
and CompareMethods(TMethod(CustomMacroFunction),TMethod(Source.CustomMacroFunction))
and (Quiet=Source.Quiet);
end else
Result:=inherited Equals(Obj);
end;
procedure TIDEExternalToolOptions.Clear;
begin
fCmdLineParams:='';
FCustomMacroFunction:=nil;
FEnvironmentOverrides.Clear;
FExecutable:='';
FShowConsole:=false;
FHideWindow:=true;
FResolveMacros:=true;
FParsers.Clear;
fTitle:='';
fWorkingDirectory:='';
FQuiet:=false;
end;
{ TExternalToolGroup }
function TExternalToolGroup.GetItems(Index: integer): TAbstractExternalTool;
begin
Result:=TAbstractExternalTool(FItems[Index]);
end;
constructor TExternalToolGroup.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
FItems:=TFPList.Create;
FAbortIfOneFails:=true;
end;
destructor TExternalToolGroup.Destroy;
begin
Clear;
FreeAndNil(FItems);
inherited Destroy;
end;
procedure TExternalToolGroup.Clear;
var
i: Integer;
begin
for i:=Count-1 downto 0 do
Items[i].Group:=nil;
end;
function TExternalToolGroup.Count: integer;
begin
Result:=FItems.Count;
end;
procedure TExternalToolGroup.Execute;
var
i: Integer;
Tool: TAbstractExternalTool;
begin
for i:=0 to Count-1 do begin
Tool:=Items[i];
if Tool.Terminated then continue;
//debugln(['TExternalToolGroup.Execute ',Tool.Title]);
Tool.Execute;
end;
end;
procedure TExternalToolGroup.WaitForExit;
begin
repeat
ExternalToolList.HandleMesages;
if AllStopped then exit;
Sleep(20);
//debugln(['TExternalToolGroup.WaitForExit ',Now,'==========================']);
//for i:=0 to Count-1 do
// debugln([' Stage=',dbgs(Items[i].Stage),' "',Items[i].Title,'"']);
until false;
end;
procedure TExternalToolGroup.Terminate;
var
i: Integer;
begin
for i:=Count-1 downto 0 do
if i<Count then
Items[i].Terminate;
end;
function TExternalToolGroup.AllStopped: boolean;
var
i: Integer;
begin
for i:=0 to Count-1 do
if ord(Items[i].Stage)<ord(etsStopped) then exit(false);
Result:=true;
end;
procedure TExternalToolGroup.InternalRemove(Tool: TAbstractExternalTool);
begin
FItems.Remove(Tool);
if FItems.Count>0 then exit;
if csDestroying in ComponentState then exit;
Free;
end;
procedure TExternalToolGroup.InternallAdd(Tool: TAbstractExternalTool);
begin
if FItems.IndexOf(Tool)>=0 then
raise Exception.Create('already in group');
FItems.Add(Tool);
end;
procedure TExternalToolGroup.ToolExited(Tool: TAbstractExternalTool);
begin
//debugln(['TExternalToolGroup.ToolExited START ',Tool.Title,' Error=',Tool.ErrorMessage,' AbortIfOneFails=',AbortIfOneFails]);
if (Tool.ErrorMessage<>'') then begin
if ErrorMessage='' then
ErrorMessage:=Tool.ErrorMessage;
if AbortIfOneFails then
Terminate;
end;
end;
{ TDefaultParser }
procedure TDefaultParser.ReadLine(Line: string; OutputIndex: integer;
var Handled: boolean);
var
MsgLine: TMessageLine;
begin
Handled:=true;
//debugln(['TDefaultParser.ReadLine ',Line]);
MsgLine:=CreateMsgLine(OutputIndex);
MsgLine.Msg:=Line;
MsgLine.Urgency:=mluImportant;
AddMsgLine(MsgLine);
end;
class function TDefaultParser.DefaultSubTool: string;
begin
Result:=SubToolDefault;
end;
class function TDefaultParser.GetLocalizedParserName: string;
begin
Result:=oisShowAllOutputLines;
end;
class function TDefaultParser.Priority: integer;
begin
Result:=SubToolDefaultPriority;
end;
{ TMessageLineEnumerator }
function TMessageLineEnumerator.GetCurrent: TMessageLine;
begin
Result:=TMessageLine(FCurrent.Data);
end;
constructor TMessageLineEnumerator.Create(Tree: TAvlTree;
const aFilename: string; aMinLine, aMaxLine: integer);
begin
FTree:=Tree;
FFilename:=aFilename;
fMinLine:=aMinLine;
FMaxLine:=aMaxLine;
end;
function TMessageLineEnumerator.GetEnumerator: TMessageLineEnumerator;
begin
Result:=Self;
end;
function TMessageLineEnumerator.MoveNext: boolean;
var
Line: TMessageLine;
CmpLine: TMessageLine;
begin
Result:=false;
if FCurrent=nil then begin
CmpLine:=TMessageLine.Create;
try
CmpLine.Filename:=FFilename;
CmpLine.Line:=MinLine;
CmpLine.Column:=Low(Integer);
FCurrent:=FTree.FindNearest(CmpLine);
if FCurrent=nil then exit;
if FTree.Compare(FCurrent.Data,CmpLine)<0 then
FCurrent:=FCurrent.Successor;
finally
CmpLine.Free;
end;
end else begin
FCurrent:=FCurrent.Successor;
end;
if FCurrent=nil then
exit;
Line:=Current;
if CompareFilenames(Line.Filename,FFilename)<>0 then exit;
if Line.Line>MaxLine then exit;
Result:=true;
end;
{ TAbstractExternalTool }
function TAbstractExternalTool.GetParserCount: integer;
begin
Result:=FParsers.Count;
end;
function TAbstractExternalTool.GetCmdLineParams: string;
begin
Result:=MergeCmdLineParams(Process.Parameters);
end;
function TAbstractExternalTool.GetParsers(Index: integer): TExtToolParser;
begin
Result:=TExtToolParser(FParsers[Index]);
end;
function TAbstractExternalTool.GetReferences(Index: integer): string;
begin
Result:=FReferences[Index];
end;
function TAbstractExternalTool.GetViews(Index: integer): TExtToolView;
begin
Result:=TExtToolView(FViews[Index]);
end;
procedure TAbstractExternalTool.SetCmdLineParams(aParams: string);
var
sl: TStringList;
begin
sl:=TStringList.Create;
try
SplitCmdLineParams(aParams,sl);
Process.Parameters:=sl;
finally
sl.Free;
end;
end;
procedure TAbstractExternalTool.SetEnvironmentOverrides(AValue: TStrings);
begin
if (FEnvironmentOverrides=AValue)
or (FEnvironmentOverrides.Equals(AValue)) then Exit;
FEnvironmentOverrides.Assign(AValue);
end;
procedure TAbstractExternalTool.SetGroup(AValue: TExternalToolGroup);
begin
if FGroup=AValue then Exit;
if Group<>nil then
Group.InternalRemove(Self);
FGroup:=AValue;
if Group<>nil then
Group.InternallAdd(Self);
end;
procedure TAbstractExternalTool.SetTitle(const AValue: string);
begin
if FTitle=AValue then exit;
FTitle:=AValue;
end;
procedure TAbstractExternalTool.AddHandler(HandlerType: TExternalToolHandler;
const AMethod: TMethod; AsFirst: boolean);
begin
if FHandlers[HandlerType]=nil then
FHandlers[HandlerType]:=TMethodList.Create;
FHandlers[HandlerType].Add(AMethod,not AsFirst);
end;
procedure TAbstractExternalTool.RemoveHandler(
HandlerType: TExternalToolHandler; const AMethod: TMethod);
begin
FHandlers[HandlerType].Remove(AMethod);
end;
procedure TAbstractExternalTool.DoCallNotifyHandler(
HandlerType: TExternalToolHandler);
begin
FHandlers[HandlerType].CallNotifyEvents(Self);
end;
procedure TAbstractExternalTool.Notification(AComponent: TComponent;
Operation: TOperation);
begin
inherited Notification(AComponent, Operation);
if Operation=opRemove then begin
EnterCriticalSection;
try
if FViews<>nil then
FViews.Remove(AComponent);
finally
LeaveCriticalSection;
end;
end;
end;
function TAbstractExternalTool.CanFree: boolean;
begin
Result:=false;
if csDestroying in ComponentState then exit;
if (FReferences.Count>0)
or (ViewCount>0) then exit;
if (Process<>nil) and (Process.Running) then
exit;
Result:=true;
end;
constructor TAbstractExternalTool.Create(AOwner: TComponent);
begin
if AOwner is TExternalToolsBase then
FTools:=TExternalToolsBase(AOwner);
inherited Create(AOwner);
if FWorkerMessagesClass=nil then
FWorkerMessagesClass:=TMessageLine;
FWorkerMessages:=TMessageLines.Create(Self,FWorkerMessagesClass);
FParsers:=TFPList.Create;
FViews:=TFPList.Create;
FStage:=etsInit;
FEstimatedLoad:=1;
FEnvironmentOverrides:=TStringList.Create;
FReferences:=TStringList.Create;
end;
destructor TAbstractExternalTool.Destroy;
var
h: TExternalToolHandler;
begin
{$IFDEF VerboseCheckInterPkgFiles}
debugln(['TAbstractExternalTool.Destroy ',Title]);
{$ENDIF}
EnterCriticalSection;
try
if FreeData then FreeAndNil(FData);
ClearParsers;
ClearViews;
Group:=nil;
FreeAndNil(FReferences);
for h:=low(FHandlers) to high(FHandlers) do
FreeAndNil(FHandlers[h]);
FWorkerMessages.Clear;
FreeAndNil(FParsers);
FreeAndNil(FViews);
FreeAndNil(FEnvironmentOverrides);
inherited Destroy;
finally
LeaveCriticalsection;
end;
FreeAndNil(FWorkerMessages);
end;
procedure TAbstractExternalTool.EnterCriticalSection;
begin
FWorkerMessages.EnterCriticalSection;
end;
procedure TAbstractExternalTool.LeaveCriticalSection;
begin
FWorkerMessages.LeaveCriticalSection;
end;
procedure TAbstractExternalTool.ConsistencyCheck;
var
i: Integer;
begin
EnterCriticalSection;
try
for i:=0 to ParserCount-1 do
Parsers[i].ConsistencyCheck;
finally
LeaveCriticalSection;
end;
end;
procedure TAbstractExternalTool.AutoFree;
begin
if MainThreadID<>GetCurrentThreadId then
raise Exception.Create('AutoFree only via main thread');
if CanFree then
Free;
end;
procedure TAbstractExternalTool.RemoveAllHandlersOfObject(AnObject: TObject);
var
HandlerType: TExternalToolHandler;
begin
for HandlerType:=Low(HandlerType) to High(HandlerType) do
FHandlers[HandlerType].RemoveAllMethodsOfObject(AnObject);
end;
procedure TAbstractExternalTool.AddHandlerOnStopped(
const OnStopped: TNotifyEvent; AsFirst: boolean);
begin
AddHandler(ethStopped,TMethod(OnStopped),AsFirst);
end;
procedure TAbstractExternalTool.RemoveHandlerOnStopped(
const OnStopped: TNotifyEvent);
begin
RemoveHandler(ethStopped,TMethod(OnStopped));
end;
procedure TAbstractExternalTool.Reference(Thing: TObject; const Note: string);
var
i: Integer;
begin
if csDestroying in ComponentState then
raise Exception.Create('too late');
if (Note='') or (Thing=nil) then
raise Exception.Create('invalid parameters');
for i:=0 to FReferences.Count-1 do
if FReferences.Objects[i]=Thing then
raise Exception.Create('already referenced');
FReferences.AddObject(Note,Thing);
end;
procedure TAbstractExternalTool.Release(Thing: TObject);
var
i: Integer;
begin
for i:=0 to FReferences.Count-1 do
if FReferences.Objects[i]=Thing then begin
FReferences.Delete(i);
AutoFree;
exit;
end;
raise Exception.Create('reference not found');
end;
function TAbstractExternalTool.ReferenceCount: integer;
begin
Result:=FReferences.Count;
end;
procedure TAbstractExternalTool.AddHandlerOnNewOutput(
const OnNewOutput: TExternalToolNewOutputEvent; AsFirst: boolean);
begin
AddHandler(ethNewOutput,TMethod(OnNewOutput),AsFirst);
end;
procedure TAbstractExternalTool.RemoveHandlerOnNewOutput(
const OnNewOutput: TExternalToolNewOutputEvent);
begin
RemoveHandler(ethNewOutput,TMethod(OnNewOutput));
end;
function TAbstractExternalTool.AddParsers(const SubTool: string
): TExtToolParser;
var
ParserClass: TExtToolParserClass;
i: Integer;
begin
Result:=nil;
for i:=0 to ExternalToolList.ParserCount-1 do begin
ParserClass:=ExternalToolList.Parsers[i];
if not ParserClass.CanParseSubTool(SubTool) then continue;
Result:=AddParser(ParserClass);
end;
if Result<>nil then exit;
raise Exception.Create(Format(lisUnableToFindParserForTool, [SubTool]));
end;
function TAbstractExternalTool.AddParser(ParserClass: TExtToolParserClass
): TExtToolParser;
var
i: Integer;
begin
for i:=0 to ParserCount-1 do begin
Result:=Parsers[i];
if Result.ClassType=ParserClass then
exit;
end;
Result:=ParserClass.Create(nil);
i:=0;
while (i<FParsers.Count) and (Parsers[i].Priority>=ParserClass.Priority) do
inc(i);
FParsers.Insert(i,Result);
Result.FTool:=Self;
end;
function TAbstractExternalTool.AddParserByName(const ParserName: string
): TExtToolParser;
var
aClass: TExtToolParserClass;
begin
Result:=nil;
aClass:=ExternalToolList.FindParserWithName(ParserName);
if aClass=nil then
raise Exception.Create(Format(lisUnableToFindParserWithName, [ParserName]));
Result:=AddParser(aClass);
end;
procedure TAbstractExternalTool.RemoveParser(Parser: TExtToolParser);
begin
FParsers.Remove(Parser);
if Parser.Tool<>Self then exit;
Parser.FTool:=nil;
end;
function TAbstractExternalTool.IndexOfParser(Parser: TExtToolParser): integer;
begin
Result:=FParsers.IndexOf(Parser);
end;
procedure TAbstractExternalTool.DeleteParser(Parser: TExtToolParser);
begin
Parser.Free;
end;
procedure TAbstractExternalTool.ClearParsers(Delete: boolean);
begin
while ParserCount>0 do
if Delete then
DeleteParser(Parsers[ParserCount-1])
else
RemoveParser(Parsers[ParserCount-1]);
end;
function TAbstractExternalTool.FindParser(aParserClass: TExtToolParserClass
): TExtToolParser;
var
i: Integer;
begin
for i:=0 to ParserCount-1 do begin
Result:=Parsers[i];
if Result.InheritsFrom(aParserClass) then exit;
end;
Result:=nil;
end;
function TAbstractExternalTool.FindParser(const SubTool: string
): TExtToolParser;
var
i: Integer;
ParserClass: TExtToolParserClass;
begin
for i:=0 to ExternalToolList.ParserCount-1 do begin
ParserClass:=ExternalToolList.Parsers[i];
if not ParserClass.CanParseSubTool(SubTool) then continue;
Result:=FindParser(ParserClass);
if Result<>nil then exit;
end;
Result:=nil;
end;
function TAbstractExternalTool.ViewCount: integer;
begin
Result:=FViews.Count;
end;
function TAbstractExternalTool.AddView(View: TExtToolView): integer;
begin
View.EnterCriticalSection;
try
if View.Tool<>nil then
raise Exception.Create('');
Result:=FViews.Add(View);
FreeNotification(View);
View.FTool:=Self;
View.Lines.BaseDirectory:=WorkerDirectory;
finally
View.LeaveCriticalSection;
end;
end;
procedure TAbstractExternalTool.RemoveView(View: TExtToolView);
begin
View.EnterCriticalSection;
try
View.fTool:=nil;
FViews.Remove(View);
finally
View.LeaveCriticalSection;
end;
AutoFree;
end;
function TAbstractExternalTool.IndexOfView(View: TExtToolView): integer;
begin
Result:=FViews.IndexOf(View);
end;
procedure TAbstractExternalTool.DeleteView(View: TExtToolView);
begin
RemoveView(View);
View.Free;
end;
procedure TAbstractExternalTool.ClearViews(Delete: boolean);
begin
EnterCriticalSection;
try
while ViewCount>0 do
if Delete then
DeleteView(Views[ViewCount-1])
else
RemoveView(Views[ViewCount-1]);
finally
LeaveCriticalSection;
end;
end;
function TAbstractExternalTool.FindUnfinishedView: TExtToolView;
var
i: Integer;
begin
for i:=0 to ViewCount-1 do begin
Result:=Views[i];
if not Result.HasFinished then exit;
end;
Result:=nil;
end;
{ TExtToolParser }
destructor TExtToolParser.Destroy;
begin
if Tool<>nil then
Tool.RemoveParser(Self);
inherited Destroy;
end;
procedure TExtToolParser.Init;
begin
end;
procedure TExtToolParser.InitReading;
begin
end;
procedure TExtToolParser.Done;
begin
end;
function TExtToolParser.CreateMsgLine(OutputIndex: integer): TMessageLine;
begin
Result:=Tool.WorkerMessages.CreateLine(OutputIndex);
if OutputIndex>=0 then
Result.Msg:=Tool.WorkerOutput[OutputIndex]; // use raw output as default msg
end;
procedure TExtToolParser.AddMsgLine(MsgLine: TMessageLine);
begin
Tool.WorkerMessages.Add(MsgLine);
end;
procedure TExtToolParser.ImproveMessages(aPhase: TExtToolParserSyncPhase);
begin
end;
procedure TExtToolParser.ConsistencyCheck;
begin
end;
class function TExtToolParser.GetParserName: string;
begin
Result:=DefaultSubTool;
end;
class function TExtToolParser.GetLocalizedParserName: string;
begin
Result:=DefaultSubTool;
end;
class function TExtToolParser.IsSubTool(const SubTool: string): boolean;
begin
Result:=CanParseSubTool(SubTool);
end;
class function TExtToolParser.CanParseSubTool(const SubTool: string): boolean;
begin
Result:=CompareText(DefaultSubTool,SubTool)=0;
end;
class function TExtToolParser.GetMsgPattern(SubTool: string; MsgID: integer;
out Urgency: TMessageLineUrgency): string;
begin
Urgency:=mluNone;
Result:='';
end;
class function TExtToolParser.GetMsgHint(SubTool: string; MsgID: integer
): string;
begin
Result:='';
end;
class function TExtToolParser.GetMsgParser(Msg: TMessageLine;
ParserClass: TClass): TExtToolParser;
var
aTool: TAbstractExternalTool;
begin
Result:=nil;
if ExternalToolList=nil then exit;
aTool:=ExternalToolList.GetMsgTool(Msg);
if aTool=nil then exit;
Result:=aTool.FindParser(TExtToolParserClass(ParserClass));
end;
class function TExtToolParser.Priority: integer;
begin
Result:=0;
end;
{ TExternalToolsBase }
// inline
function TExternalToolsBase.GetItems(Index: integer): TAbstractExternalTool;
begin
Result:=TAbstractExternalTool(fItems[Index]);
end;
// inline
function TExternalToolsBase.Count: integer;
begin
Result:=fItems.Count;
end;
function TExternalToolsBase.AddDummy(Title: string): TAbstractExternalTool;
begin
Result:=Add(Title);
Result.Terminate;
end;
constructor TExternalToolsBase.Create(aOwner: TComponent);
begin
inherited Create(aOwner);
fItems:=TFPList.Create;
end;
destructor TExternalToolsBase.Destroy;
begin
inherited Destroy;
FreeAndNil(fItems);
if ExternalToolList=Self then
ExternalToolList:=nil;
end;
procedure TExternalToolsBase.ConsistencyCheck;
var
i: Integer;
begin
for i:=0 to Count-1 do
Items[i].ConsistencyCheck;
end;
function TExternalToolsBase.GetMsgPattern(SubTool: string; MsgID: integer; out
Urgency: TMessageLineUrgency): string;
var
Parser: TExtToolParserClass;
i: Integer;
begin
Result:='';
Urgency:=mluNone;
for i:=0 to ParserCount-1 do begin
Parser:=Parsers[i];
Result:=Parser.GetMsgPattern(SubTool,MsgID,Urgency);
if Result<>'' then exit;
end;
end;
function TExternalToolsBase.GetMsgHint(SubTool: string; MsgID: integer): string;
var
Parser: TExtToolParserClass;
i: Integer;
begin
Result:='';
for i:=0 to ParserCount-1 do begin
Parser:=Parsers[i];
Result:=Parser.GetMsgHint(SubTool,MsgID);
if Result<>'' then exit;
end;
end;
{ TMessageLines }
function TMessageLines.GetItems(Index: integer): TMessageLine;
begin
Result:=TMessageLine(fItems[Index]);
end;
procedure TMessageLines.SetBaseDirectory(const AValue: string);
var
NewValue: String;
begin
NewValue:=CleanAndExpandDirectory(AValue);
if FBaseDirectory=NewValue then exit;
FBaseDirectory:=NewValue;
IncreaseChangeStamp;
end;
procedure TMessageLines.LineChanged(Line: TMessageLine);
begin
IncreaseChangeStamp;
if fChangedHandler<>nil then
fChangedHandler.CallNotifyEvents(Line);
end;
constructor TMessageLines.Create(aOwner: TObject;
aMsgLineClass: TMessageLineClass);
begin
FOwner:=aOwner;
InitCriticalSection(FCritSec);
FMessageLineClass:=aMsgLineClass;
fItems:=TFPList.Create;
FSortedForSrcPos:=TAvlTree.Create(@CompareMsgLinesSrcPos);
FSortedForSrcPos.SetNodeManager(nil);
FUpdateSortedSrcPos:=true;
fChangedHandler:=TMethodList.Create;
end;
destructor TMessageLines.Destroy;
begin
EnterCriticalSection;
try
Clear;
FreeAndNil(FSortedForSrcPos);
FreeAndNil(fItems);
inherited Destroy;
FreeAndNil(fChangedHandler);
finally
LeaveCriticalsection;
end;
DoneCriticalsection(FCritSec);
end;
procedure TMessageLines.EnterCriticalSection;
begin
System.EnterCriticalsection(FCritSec);
end;
procedure TMessageLines.LeaveCriticalSection;
begin
System.LeaveCriticalsection(FCritSec);
end;
function TMessageLines.Count: integer;
begin
Result:=fItems.Count;
end;
procedure TMessageLines.Clear;
var
i: Integer;
c: TMessageLineUrgency;
begin
if fItems.Count=0 then exit;
FreeAndNil(fMarkedFixed);
for i:=0 to fItems.Count-1 do
TObject(fItems[i]).Free;
fItems.Clear;
FSortedForSrcPos.Clear;
for c:=low(UrgencyCounts) to high(UrgencyCounts) do UrgencyCounts[c]:=0;
IncreaseChangeStamp;
end;
function TMessageLines.GetLastLine: TMessageLine;
begin
if Count>0 then
Result:=Items[Count-1]
else
Result:=nil;
end;
function TMessageLines.CreateLine(OutputIndex: integer): TMessageLine;
begin
Result:=MessageLineClass.Create;
Result.FIndex:=-1;
Result.FOutputIndex:=OutputIndex;
end;
procedure TMessageLines.Add(MsgLine: TMessageLine);
var
Cnt: Integer;
Prev: TMessageLine;
begin
if MsgLine.Index>=0 then
raise Exception.Create('TMessageLines.Add already added');
MsgLine.FLines:=Self;
MsgLine.FIndex:=fItems.Add(MsgLine);
FSortedForSrcPos.Add(MsgLine);
inc(UrgencyCounts[MsgLine.Urgency]);
// save some memory by combining strings
Cnt:=Count;
if (Cnt>1) then begin
Prev:=Items[Cnt-2];
if MsgLine.Filename=Prev.Filename then
MsgLine.fFilename:=Prev.Filename;
if MsgLine.OriginalLine=Prev.OriginalLine then
MsgLine.fOriginalLine:=Prev.OriginalLine;
end;
LineChanged(MsgLine);
end;
procedure TMessageLines.Remove(MsgLine: TMessageLine);
var
i: Integer;
begin
if MsgLine.FLines<>Self then
raise Exception.Create('');
FSortedForSrcPos.Remove(MsgLine);
fItems.Delete(MsgLine.Index);
for i:=MsgLine.Index to Count-1 do
Items[i].FIndex:=i;
MsgLine.FLines:=nil;
dec(UrgencyCounts[MsgLine.Urgency]);
IncreaseChangeStamp;
end;
procedure TMessageLines.Delete(MsgLine: TMessageLine);
begin
Remove(MsgLine);
MsgLine.Free;
end;
procedure TMessageLines.MarkFixed(MsgLine: TMessageLine);
begin
//debugln(['TMessageLines.MarkFixed ',MsgLine.Msg,' ',MsgLine.Line,',',MsgLine.Column]);
if fMarkedFixed=nil then
fMarkedFixed:=TAvlTree.Create;
if fMarkedFixed.Find(MsgLine)=nil then
fMarkedFixed.Add(MsgLine);
end;
procedure TMessageLines.ApplyFixedMarks;
var
Node: TAvlTreeNode;
Msg: TMessageLine;
List: TFPList;
begin
//debugln(['TMessageLines.ApplyFixedMarks ']);
if fMarkedFixed=nil then exit;
List:=TFPList.Create;
try
for Node in fMarkedFixed do begin
Msg:=TMessageLine(Node.Data);
if mlfFixed in Msg.Flags then Continue;
Msg.Flags:=Msg.Flags+[mlfFixed];
List.Add(Msg);
end;
if List.Count=0 then exit;
if Assigned(OnMarksFixed) then
OnMarksFixed(List);
finally
FreeAndNil(fMarkedFixed);
List.Free;
end;
end;
procedure TMessageLines.FetchAll(SrcLines: TMessageLines);
var
i: Integer;
u: TMessageLineUrgency;
MsgLine: TMessageLine;
begin
if (SrcLines=nil) or (SrcLines=Self) or (SrcLines.Count=0) then exit;
SrcLines.FSortedForSrcPos.Clear;
for i:=0 to SrcLines.Count-1 do begin
MsgLine:=SrcLines[i];
//debugln(['TMessageLines.FetchAll ',MsgLine.Msg]);
MsgLine.FLines:=Self;
MsgLine.FIndex:=fItems.Add(MsgLine);
FSortedForSrcPos.Add(MsgLine);
inc(UrgencyCounts[MsgLine.Urgency]);
LineChanged(MsgLine);
end;
SrcLines.fItems.Clear;
for u:=low(TMessageLineUrgency) to high(TMessageLineUrgency) do
SrcLines.UrgencyCounts[u]:=0;
IncreaseChangeStamp;
end;
procedure TMessageLines.SourceLinesInserted(Filename: string; Line,
InsertedCount: integer);
// adjust Line numbers in all messages
var
CmpLine: TMessageLine;
Node: TAvlTreeNode;
MsgLine: TMessageLine;
begin
if (Filename='') or (Count<=0) then exit;
CmpLine:=TMessageLine.Create;
try
CmpLine.Filename:=Filename;
CmpLine.Line:=Line;
Node:=FSortedForSrcPos.FindNearest(CmpLine);
if Node=nil then exit;
Node:=FSortedForSrcPos.FindLeftMost(CmpLine);
// Note: if no exact node was found, Node can be one too less or too high
if FSortedForSrcPos.Compare(Node,CmpLine)<0 then
Node:=FSortedForSrcPos.FindSuccessor(Node);
// adjust line numbers behind (in same source)
CmpLine.Line:=High(integer);
while (Node<>nil) and (FSortedForSrcPos.Compare(Node,CmpLine)<0) do begin
MsgLine:=TMessageLine(Node.Data);
inc(MsgLine.FLine,InsertedCount);
LineChanged(MsgLine);
Node:=FSortedForSrcPos.FindSuccessor(Node);
end;
finally
CmpLine.Free;
end;
end;
procedure TMessageLines.SourceLinesDeleted(Filename: string; FirstLine,
DeletedCount: integer);
// adjust Line numbers in all messages and mark lines in range as deleted
var
CmpLine: TMessageLine;
Node: TAvlTreeNode;
MsgLine: TMessageLine;
begin
if (Filename='') or (Count<=0) then exit;
CmpLine:=TMessageLine.Create;
try
CmpLine.Filename:=Filename;
CmpLine.Line:=FirstLine;
Node:=FSortedForSrcPos.FindNearest(CmpLine);
if Node=nil then exit;
Node:=FSortedForSrcPos.FindLeftMost(CmpLine);
// Note: if no exact node was found, Node can be one too less or too high
if FSortedForSrcPos.Compare(Node,CmpLine)<0 then
Node:=FSortedForSrcPos.FindSuccessor(Node);
// mark lines as deleted
CmpLine.Line:=FirstLine+DeletedCount;
while (Node<>nil) and (FSortedForSrcPos.Compare(Node,CmpLine)<0) do begin
MsgLine:=TMessageLine(Node.Data);
MsgLine.Flags:=MsgLine.Flags+[mlfFixed];
Node:=FSortedForSrcPos.FindSuccessor(Node);
end;
// adjust line numbers behind (in same source)
CmpLine.Line:=High(integer);
while (Node<>nil) and (FSortedForSrcPos.Compare(Node,CmpLine)<0) do begin
MsgLine:=TMessageLine(Node.Data);
dec(MsgLine.FLine,DeletedCount);
LineChanged(MsgLine);
Node:=FSortedForSrcPos.FindSuccessor(Node);
end;
finally
CmpLine.Free;
end;
end;
procedure TMessageLines.IncreaseChangeStamp;
begin
LUIncreaseChangeStamp64(FChangeStamp);
end;
function TMessageLines.IndexOfOutputIndex(OutputIndex: integer): integer;
var
l: Integer;
r: Integer;
CurOutputIndex: Integer;
begin
l:=0;
r:=Count-1;
while (l<=r) do begin
Result:=(l+r) div 2;
CurOutputIndex:=Items[Result].OutputIndex;
if CurOutputIndex>OutputIndex then
r:=Result-1
else if CurOutputIndex<OutputIndex then
l:=Result+1
else
exit;
end;
Result:=-1;
end;
function TMessageLines.EnumerateFile(aFilename: string; MinLine: integer;
MaxLine: integer): TMessageLineEnumerator;
begin
Result:=TMessageLineEnumerator.Create(FSortedForSrcPos,aFilename,MinLine,MaxLine);
end;
procedure TMessageLines.AddChangedHandler(const OnLineChanged: TNotifyEvent;
AsFirst: boolean);
begin
fChangedHandler.Add(TMethod(OnLineChanged),not AsFirst);
end;
procedure TMessageLines.RemoveChangedHandler(const OnLineChanged: TNotifyEvent);
begin
fChangedHandler.Remove(TMethod(OnLineChanged));
end;
procedure TMessageLines.ConsistencyCheck;
begin
FSortedForSrcPos.ConsistencyCheck;
end;
{ TMessageLine }
//inline
function TMessageLine.GetShortFilename: string;
begin
Result:=ExtractFileName(Filename);
end;
//inline
function TMessageLine.GetFullFilename: string;
begin
Result:=Filename;
end;
function TMessageLine.GetAttribute(const Identifier: string): string;
begin
if fAttributes=nil then
Result:=''
else
Result:=fAttributes.Values[Identifier];
end;
procedure TMessageLine.SetAttribute(const Identifier: string;
const AValue: string);
begin
if GetAttribute(Identifier)=AValue then exit;
if fAttributes=nil then
fAttributes:=TStringList.Create;
fAttributes.Values[Identifier]:=AValue;
IncreaseChangeStamp;
end;
procedure TMessageLine.SetColumn(const AValue: integer);
begin
if FColumn=AValue then exit;
SortedSrcPosUnbind;
FColumn:=AValue;
SortedSrcPosBind;
IncreaseChangeStamp;
end;
procedure TMessageLine.SetFilename(AValue: string);
begin
AValue:=TrimFilename(AValue);
if not FilenameIsAbsolute(AValue) then begin
if (FLines<>nil)
and (FLines.BaseDirectory<>'') then
AValue:=AppendPathDelim(FLines.BaseDirectory)+AValue;
end;
if FFilename=AValue then exit;
SortedSrcPosUnbind;
FFilename:=AValue;
SortedSrcPosBind;
IncreaseChangeStamp;
end;
procedure TMessageLine.SetFlags(AValue: TMessageLineFlags);
begin
if FFlags=AValue then Exit;
FFlags:=AValue;
IncreaseChangeStamp;
end;
procedure TMessageLine.SetLine(const AValue: integer);
begin
if FLine=AValue then exit;
SortedSrcPosUnbind;
FLine:=AValue;
SortedSrcPosBind;
IncreaseChangeStamp;
end;
procedure TMessageLine.SortedSrcPosBind;
begin
if (Index>=0) and Lines.UpdateSortedSrcPos then
Lines.FSortedForSrcPos.Add(Self);
end;
procedure TMessageLine.SortedSrcPosUnbind;
begin
if (Index>=0) and Lines.UpdateSortedSrcPos then
Lines.FSortedForSrcPos.Remove(Self);
end;
procedure TMessageLine.SetLines(AValue: TMessageLines);
begin
if FLines=AValue then exit;
if FLines<>nil then begin
SortedSrcPosUnbind;
FLines.Remove(Self);
end;
FLines:=AValue;
if FLines<>nil then begin
FLines.Add(Self);
if (not FilenameIsAbsolute(FFilename))
and (Lines.BaseDirectory<>'') then
FFilename:=AppendPathDelim(Lines.BaseDirectory)+FFilename;
SortedSrcPosBind;
end else
FLines:=nil;
end;
procedure TMessageLine.SetMsg(AValue: string);
begin
if FMsg=AValue then Exit;
FMsg:=AValue;
FTranslatedMsg:='';
IncreaseChangeStamp;
end;
procedure TMessageLine.SetMsgID(AValue: integer);
begin
if FMsgID=AValue then Exit;
FMsgID:=AValue;
IncreaseChangeStamp;
end;
procedure TMessageLine.SetSubTool(AValue: string);
begin
if FSubTool=AValue then Exit;
FSubTool:=AValue;
IncreaseChangeStamp;
end;
procedure TMessageLine.SetSubType(AValue: PtrUInt);
begin
if FSubType=AValue then Exit;
FSubType:=AValue;
IncreaseChangeStamp;
end;
procedure TMessageLine.SetTranslatedMsg(AValue: string);
begin
if FTranslatedMsg=AValue then Exit;
FTranslatedMsg:=AValue;
IncreaseChangeStamp;
end;
procedure TMessageLine.SetUrgency(AValue: TMessageLineUrgency);
begin
if FUrgency=AValue then Exit;
if Index>=0 then
dec(Lines.UrgencyCounts[Urgency]);
FUrgency:=AValue;
if Index>=0 then
inc(Lines.UrgencyCounts[Urgency]);
IncreaseChangeStamp;
end;
constructor TMessageLine.Create;
begin
inherited Create;
FIndex:=-1;
FOutputIndex:=-1;
IncreaseChangeStamp;
end;
destructor TMessageLine.Destroy;
begin
FreeAndNil(fAttributes);
inherited Destroy;
end;
procedure TMessageLine.Assign(Source: TMessageLine);
begin
if Source.fAttributes<>nil then begin
if fAttributes=nil then
fAttributes:=TStringList.Create;
fAttributes.Assign(Source.fAttributes);
end else begin
FreeAndNil(fAttributes);
end;
Urgency:=Source.Urgency;
Column:=Source.Column;
Filename:=Source.Filename;
Flags:=Source.Flags;
Line:=Source.Line;
Msg:=Source.Msg;
MsgID:=Source.MsgID;
fOutputIndex:=Source.OutputIndex;
SubTool:=Source.SubTool;
SubType:=Source.SubType;
TranslatedMsg:=Source.TranslatedMsg;
IncreaseChangeStamp;
end;
function TMessageLine.Equals(Obj: TObject): boolean;
var
Source: TMessageLine;
begin
if Obj is TMessageLine then begin
Source:=TMessageLine(Obj);
Result:=false;
if Source.fAttributes<>nil then begin
if fAttributes=nil then exit;
if not fAttributes.Equals(Source.fAttributes) then exit;
end else begin
if (fAttributes<>nil) and (fAttributes.Count>0) then
exit;
end;
Result:=(Urgency=Source.Urgency)
and (Column=Source.Column)
and (Filename=Source.Filename)
and (Flags=Source.Flags)
and (Line=Source.Line)
and (Msg=Source.Msg)
and (MsgID=Source.MsgID)
and (fOutputIndex=Source.OutputIndex)
and (SubTool=Source.SubTool)
and (SubType=Source.SubType)
and (TranslatedMsg=Source.TranslatedMsg);
end else
Result:=inherited Equals(Obj);
end;
procedure TMessageLine.Clear;
begin
SubTool:='';
Msg:='';
TranslatedMsg:='';
Filename:='';
FreeAndNil(fAttributes);
end;
function TMessageLine.GetRelativeFilename: string;
begin
Result:=FFilename;
if (Lines<>nil) and (Lines.BaseDirectory<>'') then
Result:=CreateRelativePath(Result,FLines.BaseDirectory);
end;
procedure TMessageLine.ShareStrings(const ShareStringEvent: TETShareStringEvent);
var
i: Integer;
s: String;
begin
ShareStringEvent(FFilename);
ShareStringEvent(FMsg);
ShareStringEvent(FTranslatedMsg);
ShareStringEvent(FSubTool);
if fAttributes<>nil then begin
for i:=0 to fAttributes.Count-1 do begin
s:=fAttributes[i];
ShareStringEvent(s);
fAttributes[i]:=s;
end;
end;
end;
procedure TMessageLine.SetSourcePosition(NewFilename: string; NewLine,
NewColumn: integer);
begin
NewFilename:=TrimFilename(NewFilename);
if (FFilename=NewFilename) and (NewLine=Line) and (NewColumn=Column) then exit;
SortedSrcPosUnbind;
FFilename:=NewFilename;
FLine:=NewLine;
FColumn:=NewColumn;
SortedSrcPosBind;
IncreaseChangeStamp;
end;
procedure TMessageLine.IncreaseChangeStamp;
begin
if Lines<>nil then begin
Lines.LineChanged(Self);
FChangeStamp:=Lines.ChangeStamp;
end else
LUIncreaseChangeStamp64(FChangeStamp);
end;
procedure TMessageLine.MarkFixed;
begin
Lines.MarkFixed(Self);
end;
function TMessageLine.HasSourcePosition: boolean;
begin
Result:=(Line>0) and (Column>0) and (GetFullFilename<>'');
end;
procedure TMessageLine.GetAttributes(List: TStrings);
begin
if fAttributes<>nil then
List.Assign(fAttributes)
else
List.Clear;
List.Values['Urgency']:=MessageLineUrgencyNames[Urgency];
List.Values['SubTool']:=SubTool;
List.Values['SubType']:=IntToStr(SubType);
List.Values['File']:=Filename;
List.Values['Line']:=IntToStr(Line);
List.Values['Col']:=IntToStr(Column);
List.Values['Msg']:=Msg;
List.Values['MsgID']:=IntToStr(MsgID);
List.Values['OriginalLine']:=OriginalLine;
end;
function TMessageLine.GetToolData: TIDEExternalToolData;
var
Tool: TAbstractExternalTool;
begin
Result:=nil;
if Lines=nil then exit;
if Lines.Owner is TAbstractExternalTool then
Tool:=TAbstractExternalTool(Lines.Owner)
else if Lines.Owner is TExtToolView then begin
Tool:=TExtToolView(Lines.Owner).Tool;
if Tool=nil then exit;
end else
exit;
Result:=TIDEExternalToolData(Tool.Data);
if not (Result is TIDEExternalToolData) then
Result:=nil;
end;
{ TExtToolView }
procedure TExtToolView.FetchAllPending;
begin
Lines.FetchAll(PendingLines);
end;
procedure TExtToolView.ToolExited;
begin
;
end;
procedure TExtToolView.QueueAsyncOnChanged;
begin
raise Exception.Create('TExtToolView.QueueAsyncOnChanged should be overridden when needed.');
end;
procedure TExtToolView.RemoveAsyncOnChanged;
begin
;
end;
procedure TExtToolView.CreateLines;
begin
FLines:=TMessageLines.Create(Self, FMessageLineClass);
FProgressLine:=FMessageLineClass.Create;
FPendingLines:=TMessageLines.Create(Self, FMessageLineClass);
FPendingProgressLine:=FMessageLineClass.Create;
end;
constructor TExtToolView.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
if FMessageLineClass=nil then
FMessageLineClass:=TMessageLine;
CreateLines;
FRunning:=true;
FMinUrgency:=DefaultETViewMinUrgency;
FLastWorkerMessageCount:=-1;
end;
destructor TExtToolView.Destroy;
begin
// wait for other threads to finish their access
EnterCriticalSection;
try
if (Tool<>nil) and (not (csDestroying in Tool.ComponentState)) then
Tool.RemoveView(Self);
RemoveAsyncOnChanged;
ClearLines;
FreeAndNil(FProgressLine);
FreeAndNil(FPendingLines);
FreeAndNil(FPendingProgressLine);
inherited Destroy;
finally
LeaveCriticalSection;
end;
FreeAndNil(FLines);
end;
procedure TExtToolView.ProcessNewMessages(AThread: TThread);
{ Called by TExternalTool.AddOutputLines
Tool is in Critical section
}
var
i: Integer;
SrcMsg: TMessageLine;
NewMsg: TMessageLine;
Changed: Boolean;
NewProgressLine: TMessageLine;
begin
if csDestroying in ComponentState then exit;
Changed:=false;
EnterCriticalSection; // Beware: Tool is already in critical section
try
if (FPendingLines=nil) or (FPendingProgressLine=nil) then exit;
//DebugLn(['TExtToolView.ProcessNewMessages START From=',FirstMsgLine,' To=',Tool.WorkerMessages.Count-1]);
NewProgressLine:=nil;
for i:=FLastWorkerMessageCount+1 to Tool.WorkerMessages.Count-1 do begin
SrcMsg:=Tool.WorkerMessages[i];
//debugln(['TExtToolView.ProcessNewMessages Msg="',SrcMsg.Msg,'" Fits=',LineFits(SrcMsg)]);
if LineFits(SrcMsg) then begin
NewProgressLine:=nil;
Changed:=true;
NewMsg:=PendingLines.CreateLine(-1);
NewMsg.Assign(SrcMsg);
//debugln(['TExtToolView.ProcessNewMessages NewMsg=',Lines.Count,'="',NewMsg.Msg,'"']);
PendingLines.Add(NewMsg);
end else begin
NewProgressLine:=SrcMsg;
end;
end;
FLastWorkerMessageCount:=Tool.WorkerMessages.Count-1;
if (NewProgressLine<>nil) and Running then begin
Changed:=true;
PendingProgressLine.Assign(NewProgressLine);
end
else if PendingProgressLine.Msg<>'' then begin
Changed:=true;
PendingProgressLine.Msg:='';
end;
//debugln(['TExtToolView.ProcessNewMessages END Changed=',Changed,' Progress="',ProgressLine.Msg,'"']);
finally
LeaveCriticalSection;
end;
if Changed and Assigned(OnChanged) then begin
// wake up main thread
QueueAsyncOnChanged;
end;
end;
procedure TExtToolView.ClearLines;
var
i: Integer;
begin
EnterCriticalSection;
try
FLastWorkerMessageCount:=-1;
if Lines<>nil then
Lines.Clear;
if ProgressLine<>nil then
ProgressLine.Clear;
if PendingLines<>nil then begin
for i:=0 to PendingLines.Count-1 do
TObject(PendingLines[i]).Free;
PendingLines.Clear;
end;
if PendingProgressLine<>nil then
PendingProgressLine.Clear;
finally
LeaveCriticalSection;
end;
end;
function TExtToolView.ApplyPending: boolean;
// returns true if something changed
begin
Result:=false;
if csDestroying in ComponentState then exit;
EnterCriticalSection;
try
if csDestroying in ComponentState then exit;
if PendingLines.Count>0 then begin
FetchAllPending;
Result:=true;
end;
if not ProgressLine.Equals(PendingProgressLine) then begin
ProgressLine.Assign(PendingProgressLine);
Result:=true;
end;
finally
LeaveCriticalSection;
end;
end;
procedure TExtToolView.InputClosed;
begin
if csDestroying in ComponentState then exit;
if not Running then begin
raise Exception.Create('TExtToolView.InputClosed already closed: '+Caption);
end;
FRunning:=false;
EnterCriticalSection;
try
if csDestroying in ComponentState then exit;
if PendingProgressLine.Msg<>'' then
PendingProgressLine.Clear;
finally
LeaveCriticalSection;
end;
if Tool<>nil then begin
ExitCode:=Tool.ExitCode;
ExitStatus:=Tool.ExitStatus;
end;
ToolExited;
if Assigned(OnChanged) then begin
RemoveAsyncOnChanged;
OnChanged(Self);
end;
end;
function TExtToolView.LineFits(Line: TMessageLine): boolean;
begin
Result:=(Line.Msg<>'') and (Line.Urgency>=MinUrgency);
end;
procedure TExtToolView.EnterCriticalSection;
begin
FLines.EnterCriticalSection;
end;
procedure TExtToolView.LeaveCriticalSection;
begin
FLines.LeaveCriticalSection;
end;
procedure TExtToolView.ConsistencyCheck;
begin
EnterCriticalSection;
try
FLines.ConsistencyCheck;
FPendingLines.ConsistencyCheck;
finally
LeaveCriticalSection;
end;
end;
function TExtToolView.HasFinished: boolean;
begin
Result:=false;
EnterCriticalSection;
try
if Running then exit;
if (Tool<>nil) and (Tool.Stage<>etsStopped) then exit;
if PendingLines.Count>0 then exit;
Result:=true;
finally
LeaveCriticalSection;
end;
end;
initialization
// on single cores there is delay due to file reads
// => use 2 processes in parallel by default
DefaultMaxProcessCount:=Max(2,GetSystemThreadCount);
end.
|