Menu

[r660]: / trunk / Src / USnippets.pas  Maximize  Restore  History

Download this file

2605 lines (2426 with data), 90.0 kB

   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
{
* USnippets.pas
*
* Defines a singleton object and subsidiary classes that encapsulate the
* snippets and categories in the CodeSnip database and user defined databases.
*
* $Rev$
* $Date$
*
* ***** BEGIN LICENSE BLOCK *****
*
* Version: MPL 1.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
* the specific language governing rights and limitations under the License.
*
* The Original Code is USnippets.pas
*
* The Initial Developer of the Original Code is Peter Johnson
* (http://www.delphidabbler.com/).
*
* Portions created by the Initial Developer are Copyright (C) 2005-2009 Peter
* Johnson. All Rights Reserved.
*
* Contributor(s)
* NONE
*
* ***** END LICENSE BLOCK *****
}
unit USnippets;
interface
uses
// Delphi
Classes,
// Project
IntfCompilers, UActiveText, UIStringList, ULists, UMultiCastEvents,
USnippetIDs;
type
TRoutine = class;
TRoutineList = class;
TCategory = class;
TCategoryList = class;
{
TSnippetKind:
Enumeration of various supported kinds of snippets.
}
TSnippetKind = (
skFreeform, // free-form code - not in any of other supported formats
skRoutine, // procedure or function in standard format
skConstant, // constant definition in standard format
skTypeDef // type definition in standard format
);
{
TSnippetKinds:
Sets of snippet kinds.
}
TSnippetKinds = set of TSnippetKind;
{
TRoutineData:
Record that provides information about a snippet's properties.
}
TRoutineData = record
Kind: TSnippetKind; // Kind of snippet
Cat: string; // Category containing snippet
Desc: string; // Description of snippet
SourceCode: string; // Snippet's source code
Extra: IActiveText; // Extra text used to describe snippet
CompilerResults: TCompileResults; // Compilation results
procedure Init;
{Initialises record by setting default values for fields.
}
procedure Assign(const Src: TRoutineData);
{Sets this record to be a copy of another record.
@param Src [in] Record to be copied.
}
end;
{
TRoutineReferences:
Record that details a snippet's references
}
TRoutineReferences = record
Units: IStringList; // List of required units
Depends: ISnippetIDList; // List of IDs of required snippets
XRef: ISnippetIDList; // List of IDs of cross referenced snippets
procedure Init;
{Initialises record by creating objects stored in fields.
}
procedure Assign(const Src: TRoutineReferences);
{Sets this record to be a copy of another record.
@param Src [in] Record to be copied.
}
end;
{
TRoutineEditData:
Provides deatils of a snippet's data that can be edited.
}
TRoutineEditData = record
Props: TRoutineData; // Snippet's editable properties
Refs: TRoutineReferences; // Snippet's editable referfences
procedure Init;
{Initialises record by creating default values and field objects.
}
procedure Assign(const Src: TRoutineEditData);
{Sets this record to be a copy of another record.
@param Src [in] Record to be copied.
}
end;
{
TRoutineInfo:
Record that encapsulates data read from an import file that describes a
snippet.
}
TRoutineInfo = record
Name: string; // Snippet name
Data: TRoutineEditData; // Describes a snippet
procedure Assign(const Src: TRoutineInfo);
{Sets this record's fields to be same as another TRoutineInfo record.
Object fields are copied appropriately.
@param Src [in] Record containing fields to be copied.
}
procedure Init;
{Initialises record to nul values.
}
end;
{
TRoutineInfoList:
Dynamic array of TRoutineInfo records.
}
TRoutineInfoList = array of TRoutineInfo;
{
TCategoryData:
Provides information about a category's properties.
}
TCategoryData = record
Desc: string; // Description of categpry
procedure Assign(const Src: TCategoryData);
{Sets this record's fields to be same as another TCategoryData record.
@param Src [in] Record containing fields to be copied.
}
procedure Init;
{Initialises record to nul values.
}
end;
{
TCategoryInfo:
Record that fully describes a category, i.e. contains all the required data.
}
TCategoryInfo = record
Name: string; // Name (unique id) of category
Data: TCategoryData; // Category's properties
procedure Assign(const Src: TCategoryInfo);
{Sets this record's fields to be same as another TCategoryInfo record.
@param Src [in] Record containing fields to be copied.
}
procedure Init;
{Initialises record to nul values.
}
end;
{
TSnippetChangeEventKind:
Enumeration that specifies the different kind of change events triggered by
the user database.
}
TSnippetChangeEventKind = (
evChangeBegin, // a change to the database is about to take place
evChangeEnd, // a change to the database has completed
evRoutineAdded, // a snippet has been added
evBeforeRoutineDelete, // a snippet is about to be deleted
evAfterRoutineDelete, // a snippet has just been deleted
evRoutineDeleted, // a snippet has been deleted
evRoutineChanged, // a snippet's properties / references have changed
evCategoryAdded, // a category has been added
evBeforeCategoryDelete, // a category is about to be deleted
evAfterCategoryDelete, // a category has just been deleted
evCategoryDeleted, // a category has been deleted
evCategoryChanged // a category's properties have changed
);
{
ISnippetsDataProvider:
Interface supported by objects that provides data about the categories and
snippets in the database.
}
ISnippetsDataProvider = interface(IInterface)
['{D2D57A0D-DB29-4012-891E-E817E0EED8C8}']
function GetCategoryProps(const Cat: TCategory): TCategoryData;
{Retrieves all the properties of a category.
@param Cat [in] Category for which data is requested.
@return Record containing property data.
}
function GetCategoryRoutines(const Cat: TCategory): IStringList;
{Retrieves names of all snippets that belong to a category.
@param Cat [in] Category for which snippet names are requested.
@return Required list of snippet names.
}
function GetRoutineProps(const Routine: TRoutine): TRoutineData;
{Retrieves all the properties of a snippet.
@param Routine [in] Snippet for which data is requested.
@return Record containing property data.
}
function GetRoutineRefs(const Routine: TRoutine): TRoutineReferences;
{Retrieves information about all the references of a snippet.
@param Routine [in] Snippet for which information is requested.
@return Record containing references.
}
end;
{
ISnippetChangeEventInfo:
Interface supported by objects passed to Snippets object's change event
handler that provides information about a change event. Some properites
are not defined for certain event types. Kind property always defined.
}
ISnippetChangeEventInfo = interface(IInterface)
['{80DEE62F-DC23-4EE7-A0B1-5DE46F483CE1}']
function GetKind: TSnippetChangeEventKind;
{Gets kind (type) of event.
@return Event kind.
}
function GetInfo: TObject;
{Gets additional information about event.
@return Object that provides required information.
}
property Kind: TSnippetChangeEventKind read GetKind;
{Identifies kind (type) of an event. Always defined}
property Info: TObject read GetInfo;
{Provides additional information about the event. Actual type of object
depends on Kind. May be nil}
end;
{
ISnippetsFactory:
Interface to factory object that creates snippet and category objects. For
use by database loader objects.
}
ISnippetsFactory = interface(IInterface)
['{C6DD85BD-E649-4A90-961C-4011D2714B3E}']
function CreateCategory(const CatID: string; const UserDefined: Boolean;
const Data: TCategoryData): TCategory;
{Creates a new category object.
@param CatID [in] ID (name) of new category. Must be unique.
@param UserDefined [in] True if category is user defined, False if not.
@param Data [in] Record describing category's properties.
@return Instance of new category object.
}
function CreateRoutine(const Name: string; const UserDefined: Boolean;
const Props: TRoutineData): TRoutine;
{Creates a new snippet object.
@param Name [in] Name of new snippet. Must not exist in database
specified by UserDefined parameter.
@param UserDefined [in] True if snippet is user defined, False if not.
@param Props [in] Record describing snippet's properties.
@return Instance of new snippet with no references.
}
end;
{
TRoutine:
Encapsulates a snippet from the database. Can be routine, type, constant or
free-form.
}
TRoutine = class(TObject)
strict private
fKind: TSnippetKind; // Kind of snippet this is
fCategory: string; // Name of snippet's category
fDescription: string; // Description of snippet
fSourceCode: string; // Snippet's source code}
fName: string; // Name of snippet
fUnits: TStringList; // List of required units
fDepends: TRoutineList; // List of required snippets
fXRef: TRoutineList; // List of cross-referenced snippets
fExtra: IActiveText; // Additional information about snippet
fCompatibility: TCompileResults; // Routine's compiler compatibility
fUserDefined: Boolean; // Whether this snippet is user-defined
function GetID: TSnippetID;
{Gets snippet's unique ID.
@return Required ID.
}
strict protected
procedure SetName(const Name: string);
{Sets Name property.
@param Name [in] New name.
}
procedure SetProps(const Data: TRoutineData);
{Sets snippet's properties.
@param Data [in] Record containing property values.
}
public
constructor Create(const Name: string; const UserDefined: Boolean;
const Props: TRoutineData);
{Class contructor. Sets up snippet object with given property values.
@param Name [in] Name of snippet.
@param UserDefined [in] Indicates if this is a user defined snippet.
@param Props [in] Values of various snippet properties.
}
destructor Destroy; override;
{Class destructor. Tears down object.
}
function IsEqual(const Routine: TRoutine): Boolean;
{Checks if this snippet is same as another snippet. Snippets are
considered equal if they have the same name and come from same database.
@param Routine [in] Snippet being compared.
@return True if snippets are equal, False if not.
}
function CanCompile: Boolean;
{Checks if snippet can be compiled.
@return True if compilation supported and False if not.
}
property Kind: TSnippetKind read fKind;
{Kind of snippet represented by this object}
property ID: TSnippetID read GetID;
{Snippet's unique ID}
property Name: string read fName;
{Name of snippet}
property Category: string read fCategory;
{Category to which snippet belongs}
property Description: string read fDescription;
{Description of snippet}
property SourceCode: string read fSourceCode;
{Source code of snippet}
property Extra: IActiveText read fExtra;
{Additional information about snippet}
property Compatibility: TCompileResults read fCompatibility;
{Compiler compatibilty of this snippet}
property Units: TStringList read fUnits;
{List of units used by snippet}
property Depends: TRoutineList read fDepends;
{List of any other snippet in database on which this snippet depends}
property XRef: TRoutineList read fXRef;
{List of cross referenced snippets in database}
property UserDefined: Boolean read fUserDefined;
{Flag that indicates if this is a user defined snippet}
end;
{
TRoutineList:
Class that implements a list of TRoutine objects.
}
TRoutineList = class(TObject)
strict private
function GetItem(Idx: Integer): TRoutine;
{Read accessor for Items property.
@param Idx [in] Index of required snippet in list.
@return Snippet at specified index in list.
}
function Find(const RoutineName: string;
const UserDefined: Boolean; out Index: Integer): Boolean; overload;
{Finds a snippet in the list that has a specified name and user defined
property. Uses a binary search.
@param RoutineName [in] Name of snippet to be found.
@param UserDefined [in] Whether required snippet is user defined or not.
@param Index [out] Index of required snippet in list. Valid only if
method returns True.
@return True if snippet found, False if not.
}
function CompareIDs(const SID1, SID2: TSnippetID): Integer;
{Compares IDS of two snippets.
@param SID1 [in] First ID to be compared.
@param SID2 [in] Second ID to be compared.
@return 0 if IDs are the same, -ve if SID1 < SID2 and +ve if
SID1 > SID2.
}
strict protected
var fList: TObjectListEx; // Stores list of snippets
public
type
{
TEnumerator:
Implements enumerator for TRoutineList.
}
TEnumerator = class(TObject)
private
fRoutineList: TRoutineList; // Reference to object being enumerated
fIndex: Integer; // Index of current item in enumeration
function GetCurrent: TRoutine;
{Gets current snippet in enumeration. Error if at end of enumeration.
@return Current snippet.
}
public
constructor Create(const RL: TRoutineList);
{Class constructor. Sets up and initialises enumeration.
@param RL [in] Reference to snippet list object to be enumerated.
}
function MoveNext: Boolean;
{Moves to next item in enumeration.
@return True if there is a next item, False if enumeration
completed.
}
property Current: TRoutine read GetCurrent;
{Current item in enumeration. Error if at end of enumeration}
end;
public
constructor Create(const OwnsObjects: Boolean = False);
{Class constructor. Creates a new empty list.
@param OwnsObjects [in] Specifies whether list owns the snippet objects
it contains. If True the snippets are freed when deleted from list.
}
destructor Destroy; override;
{Class destructor. Tears down object.
}
procedure Assign(const SrcList: TRoutineList);
{Sets this list to be same as another list. List items are referenced, not
copied.
@param SrcList [in] List of snippets to be assigned.
}
function IsEqual(const AList: TRoutineList): Boolean;
{Checks if this list contains same snippets as another list.
@param AList [in] List of snippets to compare.
@return True if lists are same, False if not.
}
function Add(const Routine: TRoutine): Integer; virtual;
{Adds new snippet to the list, maintaining list in alphabetical order.
@param Routine [in] Snippet being added.
@return Index where item was inserted in list
@except Raised if duplicate snippet added to list.
}
function Find(const RoutineName: string;
const UserDefined: Boolean): TRoutine; overload;
{Finds a named snippet in list with a matching user defined property.
@param RoutineName [in] Name of required snippet.
@param UserDefined [in] Flag that determines if we are looking for a
user defined snippet or one from main database.
@return Reference to object representing snippet in list or nil if not
in list.
}
function Contains(const Routine: TRoutine): Boolean;
{Checks whether list contains a specified snippet.
@param Routine [in] Required snippet.
@return True if snippet is in list, False otherwise.
}
function ContainsKinds(const Kinds: TSnippetKinds): Boolean;
{Checks if the list contains specified kinds of snippets.
@param Kinds [in] Set of kinds to test for.
@return True if list contains at least one snippet of any of the
specified kinds.
}
procedure Clear;
{Clears the list.
}
function GetEnumerator: TEnumerator;
{Gets an intialised snippet list enumerator.
@return Required enumerator.
}
function Count(const UserDefined: Boolean): Integer; overload;
{Counts number of snippets in list that are either from or not from user
defined database.
@param UserDefined [in] Flags whether to count snippets in user database
(True) or in main database (False).
@return Number of snippets in specified database.
}
function Count: Integer; overload;
{Counts number of snippets in list.
@return Number of snippets in list.
}
property Items[Idx: Integer]: TRoutine read GetItem; default;
{List of snippets}
end;
{
TCategory:
Class that encapsulates a codesnip database category and list of all
snippets in it.
}
TCategory = class(TObject)
strict private
fRoutines: TRoutineList; // List of snippet objects in category
fCategory: string; // Category name
fDescription: string; // Category description
fUserDefined: Boolean; // Whether this is a user-defined snippet
public
constructor Create(const CatID: string; const UserDefined: Boolean;
const Data: TCategoryData);
{Class contructor. Sets up category object with given property values.
@param Data [in] Contains required property values.
}
destructor Destroy; override;
{Class destructor. Tears down object.
}
function IsEqual(const Cat: TCategory): Boolean;
{Checks if this category is same as another category. Categories are
considered equal if they have the same name.
@param Cat [in] Category being compared.
@return True if categories are equal, False if not.
}
property Category: string read fCategory;
{Category name. Must be unique}
property Description: string read fDescription;
{Description of category}
property Routines: TRoutineList read fRoutines;
{List of snippets in this category}
property UserDefined: Boolean read fUserDefined;
{Flag that indicates if this is a user defined category}
end;
{
TCategoryList:
Class that implements a list of TCategory objects.
}
TCategoryList = class(TObject)
strict private
function GetCount: Integer;
{Read accessor for Count property.
@return Number of categories in list.
}
function GetItem(Idx: Integer): TCategory;
{Read accessor for Items property.
@param Idx [in] Index of required category in list.
@return Category at specified index in list.
}
strict protected
var fList: TObjectListEx; // Stores list of categories
public
type
{
TEnumerator:
Implements enumerator for TCategoryList.
}
TEnumerator = class(TObject)
private
fCategoryList: TCategoryList; // Category list being enumerated
fIndex: Integer; // Index of current item in enumeration
function GetCurrent: TCategory;
{Gets current category in enumeration. Error if at end of enumeration.
@return Current category.
}
public
constructor Create(const CL: TCategoryList);
{Class constructor. Sets up and initialises enumeration.
@param CL [in] Reference to object to be enumerated.
}
function MoveNext: Boolean;
{Moves to next item in enumeration.
@return True if there is a next item, False if enumeration
completed.
}
property Current: TCategory read GetCurrent;
{Current item in enumeration. Error if at end of enumeration}
end;
public
constructor Create(const OwnsObjects: Boolean = False);
{Class constructor. Creates a new empty list.
@param OwnsObjects [in] Specifies whether list owns the category objects
it contains. If True the categories are freed when deleted from list.
}
destructor Destroy; override;
{Class destructor. Tears down object.
}
procedure Clear;
{Clears the list.
}
function Add(const Category: TCategory): Integer;
{Adds new category to list.
@param Category [in] Category to be added.
@return Index where category inserted in list.
}
function Find(const CatName: string): TCategory;
{Finds a named category in list.
@param CatName [in] Name of required category.
@return Reference to object representing category in list or nil if not
in list.
}
function Contains(const Category: TCategory): Boolean;
{Checks if a category is in list. Checks object reference.
@param Category [in] Category to be checked.
@return True if category object is in list.
}
function GetEnumerator: TEnumerator;
{Gets an intialised category list enumerator.
@return Required enumerator.
}
property Items[Idx: Integer]: TCategory read GetItem; default;
{List of categories}
property Count: Integer read GetCount;
{Number of categories in list}
end;
{
ISnippets:
Interface to object that encapsulates the whole (main and user) databases
and provides access to all snippets and all categories.
}
ISnippets = interface(IInterface)
['{A280DEEF-0336-4264-8BD0-7CDFBB207D2E}']
procedure Load;
{Loads data from main and user databases.
}
procedure Clear;
{Clears all data.
}
procedure AddChangeEventHandler(const Handler: TNotifyEventInfo);
{Adds a change event handler to list of listeners.
@param Handler [in] Event handler to be added.
}
procedure RemoveChangeEventHandler(const Handler: TNotifyEventInfo);
{Removes a change event handler from list of listeners.
@param Handler [in] Handler to remove from list.
}
function GetRoutines: TRoutineList;
{Gets list of snippets in main and user databases.
@return Required list.
}
function GetCategories: TCategoryList;
{Gets list of categories in main and user databases.
@return Required list.
}
property Categories: TCategoryList read GetCategories;
{List of categories in main and user databases}
property Routines: TRoutineList read GetRoutines;
{List of snippets in main and user databases}
end;
{
ISnippetsEdit:
Interface to object that can be used to edit the user database.
}
ISnippetsEdit = interface(IInterface)
['{CBF6FBB0-4C18-481F-A378-84BB09E5ECF4}']
function GetEditableRoutineInfo(
const Routine: TRoutine = nil): TRoutineEditData;
{Provides details of all a snippet's data (properties and references) that
may be edited.
@param Routine [in] Snippet for which data is required. May be nil in
which case a blank record is returned.
@return Required data.
}
function GetDependents(const Routine: TRoutine): ISnippetIDList;
{Builds an ID list of all snippets that depend on a specified snippet.
@param Routine [in] Snippet for which dependents are required.
@return List of IDs of dependent snippets.
}
function GetReferrers(const Routine: TRoutine): ISnippetIDList;
{Builds an ID list of all snippets that cross reference a specified
snippet.
@param Routine [in] Snippet for which cross referers are required.
@return List of IDs of referring snippets.
}
function UpdateRoutine(const Routine: TRoutine;
const Data: TRoutineEditData; const NewName: string = ''): TRoutine;
{Updates a user defined snippet's properties and references using provided
data.
@param Routine [in] Snippet to be updated. Must be user-defined.
@param Data [in] Record containing revised data.
@param NewName [in] New name of snippet. Set to '' or Routine.Name if
name is not to change.
@return Reference to updated snippet. Will have changed.
}
function AddRoutine(const RoutineName: string;
const Data: TRoutineEditData): TRoutine;
{Adds a new snippet to the user database.
@param RoutineName [in] Name of new snippet.
@param Data [in] Record storing new snippet's properties and references.
@return Reference to new snippet.
}
function CreateTempRoutine(const RoutineName: string;
const Data: TRoutineEditData): TRoutine; overload;
{Creates a new temporary snippet without adding it to the Snippets
object's snippets list. The new instance may not be added to the
Snippets object.
@param RoutineName [in] Name of new snippet.
@param Data [in] Record storing new snippet's properties and references.
@return Reference to new snippet.
}
function CreateTempRoutine(const Routine: TRoutine): TRoutine; overload;
{Creates a new temporary copy of a snippet without adding it to the
Snippets object's snippets list. The new instance may not be added to the
Snippets object.
@param Routine [in] Snippet to be copied.
@return Reference to new copied snippet.
}
procedure DeleteRoutine(const Routine: TRoutine);
{Deletes a snippet from the user database.
@param Routine [in] Snippet to be deleted.
}
function GetEditableCategoryInfo(
const Category: TCategory = nil): TCategoryData;
{Provides details of all a category's data that may be edited.
@param Category [in] Category for which data is required. May be nil in
whih case a blank record is returned.
@return Required data.
}
function AddCategory(const CatName: string;
const Data: TCategoryData): TCategory;
{Adds a new category to the user database.
@param CatName [in] Name of new category.
@param Data [in] Record storing new category's properties.
@return Reference to new category.
}
function UpdateCategory(const Category: TCategory;
const Data: TCategoryData): TCategory;
{Updates a user defined category's properties.
@param Category [in] Category to be updated. Must be user-defined.
@param Data [in] Record containing revised data.
@return Reference to updated category. Will have changed.
}
procedure DeleteCategory(const Category: TCategory);
{Deletes a category and all its snippets from the user database.
@param Category [in] Category to be deleted.
}
function Updated: Boolean;
{Checks if user database has been updated since last save.
@return True if database has been updated, False otherwise.
}
procedure Save;
{Saves user database.
}
end;
function Snippets: ISnippets;
{Returns singleton instance of object that encapsulates main and user
databases.
@return Singleton object.
}
implementation
uses
// Delphi
SysUtils, Windows {for inlining},
// Project
IntfCommon, UExceptions, USnippetsIO;
var
// Private global snippets singleton object
PvtSnippets: ISnippets = nil;
type
{
TSnippetsFactory:
Class that can create category and snippet objects.
}
TSnippetsFactory = class(TInterfacedObject, ISnippetsFactory)
public
function CreateCategory(const CatID: string; const UserDefined: Boolean;
const Data: TCategoryData): TCategory;
{Creates a new category object.
@param CatID [in] ID (name) of new category. Must be unique.
@param UserDefined [in] True if category is user defined, False if not.
@param Data [in] Record describing category's properties.
@return Instance of new category object.
}
function CreateRoutine(const Name: string; const UserDefined: Boolean;
const Props: TRoutineData): TRoutine;
{Creates a new snippet object.
@param Name [in] Name of new snippet. Must not exist in database
specified by UserDefined parameter.
@param UserDefined [in] True if snippet is user defined, False if not.
@param Props [in] Record describing snippet's properties.
@return Instance of new snippet with no references.
}
end;
{
TRoutineEx:
Private extension of TRoutine for use internally by Snippets object.
}
TRoutineEx = class(TRoutine)
public
procedure UpdateRefs(const Refs: TRoutineReferences;
const AllRoutines: TRoutineList);
{Updates a snippet's references.
@param Refs [in] Stores all snippet's references (XRef, Depends and
Units).
@param AllRoutines [in] List of all snippets in database.
}
procedure Update(const Data: TRoutineEditData;
const AllRoutines: TRoutineList);
{Updates snippet's properties and references.
@param Data [in] New property values and references.
@param AllRoutines [in] List of all snippets in database.
}
function GetEditData: TRoutineEditData;
{Gets details of all editable data of snippet.
@return Required editable properties and references.
}
function GetProps: TRoutineData;
{Gets details of snippet's properties.
@return Record containing property values.
}
function GetReferences: TRoutineReferences;
{Gets details of snippet's references.
@return Information sufficient to define references.
}
end;
{
TTempRoutine:
Special subclass of TRoutineEx that can't be added to the Snippets object.
Class does nothing, simply provides a class name for testing when a snippet
is added to a TRoutineListEx. TTempRoutine can be added to a normal snippet
list.
}
TTempRoutine = class(TRoutineEx);
{
TRoutineListEx:
Private extension of TRoutineList for use internally by Snippets object.
}
TRoutineListEx = class(TRoutineList)
public
function Add(const Routine: TRoutine): Integer; override;
{Adds a snippet to list. Prevents TTempRoutine instances from being added.
@param Routine [in] Snippet to be added.
@return Index where snippet was added to list.
@except EBug raised if Routine is a TTempRoutine instance.
}
procedure Delete(const Routine: TRoutine);
{Deletes a snippet from list.
@param Routine [in] Snippet to be deleted. No action taken if snippet
not in list.
}
end;
{
TCategoryEx:
Private extension of TCategory for use internally by Snippets object.
}
TCategoryEx = class(TCategory)
public
function GetEditData: TCategoryData;
{Gets details of all editable data of category.
@return Required editable data.
}
end;
{
TCategoryListEx:
Private extension of TCategoryList for use internally by snippets object.
}
TCategoryListEx = class(TCategoryList)
public
procedure Delete(const Category: TCategory);
{Deletes a category from the list.
@param Category [in] Category to be deleted.
}
end;
{
TSnippets:
Class that encapsulates the main and user databases. Provides access to all
snippets and all categories via the ISnippets interface. Also enables user
defined database to be modified via ISnippetsEdit interface.
}
TSnippets = class(TInterfacedObject,
ISnippets,
ISnippetsEdit
)
strict private
fUpdated: Boolean; // Flags if user database has been updated
fCategories: TCategoryList; // List of categories
fRoutines: TRoutineList; // List of snippets
fChangeEvents: TMulticastEvents; // List of change event handlers
type
{
TEventInfo:
Class that provides information about a change event.
}
TEventInfo = class(TInterfacedObject, ISnippetChangeEventInfo)
strict private
fKind: TSnippetChangeEventKind; // Kind of event
fInfo: TObject; // Extra info about event
protected // do not make strict
{ ISnippetChangeEventInfo methods }
function GetKind: TSnippetChangeEventKind;
{Gets kind (type) of event.
@return Event kind.
}
function GetInfo: TObject;
{Gets additional information about event.
@return Object that provides required information.
}
public
constructor Create(const Kind: TSnippetChangeEventKind;
const Info: TObject = nil);
{Class constructor. Creates an event information object.
@param Kind [in] Kind of event.
@param Info [in] Reference to further information about the event.
May be nil if event doesn't have additional information.
}
end;
procedure TriggerEvent(const Kind: TSnippetChangeEventKind;
const Info: TObject = nil);
{Triggers a change event. Notifies all registered listeners.
@param Kind [in] Kind of event.
@param Info [in] Reference to any further information for event. May be
nil.
}
function InternalAddRoutine(const RoutineName: string;
const Data: TRoutineEditData): TRoutine;
{Adds a new snippet to the user database. Assumes snippet not already in
user database.
@param RoutineName [in] Name of new snippet.
@param Data [in] Properties and references of new snippet.
@return Reference to new snippet object.
@except Exception raised if snippet's category does not exist.
}
procedure InternalDeleteRoutine(const Routine: TRoutine);
{Deletes a snippet from the user database.
@param Routine [in] Snippet to delete from database.
}
function InternalAddCategory(const CatName: string;
const Data: TCategoryData): TCategory;
{Adds a new category to the user database. Assumes category not already in
user database.
@param CatName [in] Name of new category.
@param Data [in] Properties of new category.
@return Reference to new category object.
}
procedure InternalDeleteCategory(const Cat: TCategory);
{Deletes a category from the user database.
@param Cat [in] Category to delete from database.
}
procedure GetDependentList(const ARoutine: TRoutine;
const List: TRoutineList);
{Builds a list of all snippets that depend on a specified snippet.
@param ARoutine [in] Snippet for which dependents are required.
@param List [in] Receives list of dependent snippets.
}
procedure GetReferrerList(const ARoutine: TRoutine;
const List: TRoutineList);
{Builds list of all snippets that cross reference a specified snippet.
@param ARoutine [in] The cross referenced snippet.
@param List [in] Receives list of cross referencing snippets.
}
protected
{ ISnippets methods }
function GetCategories: TCategoryList;
{Gets list of all categories in database.
@return Required list.
}
function GetRoutines: TRoutineList;
{Gets list of all snippets in database.
@return Required list.
}
procedure Load;
{Loads object's data from main and user defined databases.
}
procedure Clear;
{Clears the object's data.
}
procedure AddChangeEventHandler(const Handler: TNotifyEventInfo);
{Adds a change event handler to list of listeners.
@param Handler [in] Event handler to be added.
}
procedure RemoveChangeEventHandler(const Handler: TNotifyEventInfo);
{Removes a change event handler from list of listeners.
@param Handler [in] Handler to remove from list.
}
{ ISnippetsEdit methods }
function GetEditableRoutineInfo(
const Routine: TRoutine = nil): TRoutineEditData;
{Provides details of all a snippet's data (properties and references) that
may be edited.
@param Routine [in] Snippet for which data is required. May be nil in
which case a blank record is returned.
@return Required data.
}
function GetDependents(const Routine: TRoutine): ISnippetIDList;
{Builds an ID list of all snippets that depend on a specified snippet.
@param Routine [in] Snippet for which dependents are required.
@return List of IDs of dependent snippets.
}
function GetReferrers(const Routine: TRoutine): ISnippetIDList;
{Builds an ID list of all snippets that cross reference a specified
snippet.
@param Routine [in] Snippet which is cross referenced.
@return List of IDs of referring snippets.
}
function UpdateRoutine(const Routine: TRoutine;
const Data: TRoutineEditData; const NewName: string = ''): TRoutine;
{Updates a user defined snippet's properties and references using provided
data.
@param Routine [in] Snippet to be updated. Must be user-defined.
@param Data [in] Record containing revised data.
@param NewName [in] New name of snippet. Set to '' or Routine.Name if
name is not to change.
@return Reference to updated snippet. Will have changed.
}
function AddRoutine(const RoutineName: string;
const Data: TRoutineEditData): TRoutine;
{Adds a new snippet to the user database.
@param RoutineName [in] Name of new snippet.
@param Data [in] Record storing new snippet's properties and references.
@return Reference to new snippet.
}
function CreateTempRoutine(const RoutineName: string;
const Data: TRoutineEditData): TRoutine; overload;
{Creates a new temporary user defined snippet without adding it to the
Snippets object's snippets list. The new instance may not be added to the
Snippets object.
@param RoutineName [in] Name of new snippet.
@param Data [in] Record storing new snippet's properties and references.
@return Reference to new snippet.
}
function CreateTempRoutine(const Routine: TRoutine): TRoutine; overload;
{Creates a new temporary copy of a snippet without adding it to the
Snippets object's snippets list. The new instance may not be added to the
Snippets object.
@param Routine [in] Snippet to be copied.
@return Reference to new snippet.
}
procedure DeleteRoutine(const Routine: TRoutine);
{Deletes a snippet from the user database.
@param Routine [in] Snippet to be deleted.
}
function GetEditableCategoryInfo(
const Category: TCategory = nil): TCategoryData;
{Provides details of all a category's data that may be edited.
@param Category [in] Category for which data is required. May be nil in
which case a blank record is returned.
@return Required data.
}
function AddCategory(const CatName: string;
const Data: TCategoryData): TCategory;
{Adds a new category to the user database.
@param CatName [in] Name of new category.
@param Data [in] Record storing new category's properties.
@return Reference to new category.
}
function UpdateCategory(const Category: TCategory;
const Data: TCategoryData): TCategory;
{Updates a user defined category's properties.
@param Category [in] Category to be updated. Must be user-defined.
@param Data [in] Record containing revised data.
@return Reference to updated category. Will have changed.
}
procedure DeleteCategory(const Category: TCategory);
{Deletes a category and all its snippets from the user database.
@param Category [in] Category to be deleted.
}
function Updated: Boolean;
{Checks if user database has been updated since last save.
@return True if database has been updated, False otherwise.
}
procedure Save;
{Saves user defined snippets and all categories to user database.
}
public
constructor Create;
{Class constructor. Sets up new empty object.
}
destructor Destroy; override;
{Class destructor. Tidies up and tears down object.
}
end;
{
TUserDataProvider:
Class that provides data about the categories and snippets in the user-
defined database.
}
TUserDataProvider = class(TInterfacedObject, ISnippetsDataProvider)
strict private
fRoutines: TRoutineList; // All snippets in the whole database
fCategories: TCategoryList; // All categories in the whole database
public
constructor Create(const Routines: TRoutineList;
const Categories: TCategoryList);
{Class constructor. Records list of all snippets and categories in both
databases.
@param Routines [in] List of all snippets in whole database.
@param Categories [in] List of all categories in whole database.
}
{ ISnippetsDataProvider methods }
function GetCategoryProps(const Cat: TCategory): TCategoryData;
{Retrieves all the properties of a category.
@param Cat [in] Category for which data is requested.
@return Record containing property data.
}
function GetCategoryRoutines(const Cat: TCategory): IStringList;
{Retrieves names of all user-defined snippets that belong to a category.
@param Cat [in] Category for which snippet names are requested.
@return Required list of snippet names.
}
function GetRoutineProps(const Routine: TRoutine): TRoutineData;
{Retrieves all the properties of a snippet.
@param Routine [in] Snippet for which data is requested.
@return Record containing property data.
}
function GetRoutineRefs(const Routine: TRoutine): TRoutineReferences;
{Retrieves information about all the references of a snippet.
@param Routine [in] Snippet for which information is requested.
@return Record containing references.
}
end;
{
TSnippetIDListEx:
Extension of TSnippetIDList that provides an additional constructor that can
create a snippet ID list from a TRoutineList.
}
TSnippetIDListEx = class(TSnippetIDList)
public
constructor Create(const Routines: TRoutineList); overload;
{Class constructor overload that creates a snippets ID list from a
TRoutineList object.
@param Routines [in] List of snippets objects for which ID list is
required.
}
end;
function Snippets: ISnippets;
{Returns singleton instance of object that encapsulates main and user
databases.
@return Singleton object.
}
begin
if not Assigned(PvtSnippets) then
PvtSnippets := TSnippets.Create;
Result := PvtSnippets;
end;
{ TSnippets }
function TSnippets.AddCategory(const CatName: string;
const Data: TCategoryData): TCategory;
{Adds a new category to the user database.
@param CatName [in] Name of new category.
@param Data [in] Record storing new category's properties.
@return Reference to new category.
}
resourcestring
// Error message
sNameExists = 'Category %s already exists in user database';
begin
Result := nil;
TriggerEvent(evChangeBegin);
try
// Check if category with same name exists in user database: error if so
if fCategories.Find(CatName) <> nil then
raise ECodeSnip.CreateFmt(sNameExists, [CatName]);
Result := InternalAddCategory(CatName, Data);
TriggerEvent(evCategoryAdded, Result);
finally
fUpdated := True;
TriggerEvent(evChangeEnd);
end;
end;
procedure TSnippets.AddChangeEventHandler(const Handler: TNotifyEventInfo);
{Adds a change event handler to list of listeners.
@param Handler [in] Event handler to be added.
}
begin
fChangeEvents.AddHandler(Handler);
end;
function TSnippets.AddRoutine(const RoutineName: string;
const Data: TRoutineEditData): TRoutine;
{Adds a new snippet to the user database.
@param RoutineName [in] Name of new snippet.
@param Data [in] Record storing new snippet's properties and references.
@return Reference to new snippet.
}
resourcestring
// Error message
sNameExists = 'Snippet "%s" already exists in user database';
begin
Result := nil; // keeps compiler happy
TriggerEvent(evChangeBegin);
try
// Check if snippet with same name exists in user database: error if so
if fRoutines.Find(RoutineName, True) <> nil then
raise ECodeSnip.CreateFmt(sNameExists, [RoutineName]);
Result := InternalAddRoutine(RoutineName, Data);
TriggerEvent(evRoutineAdded, Result);
finally
fUpdated := True;
TriggerEvent(evChangeEnd);
end;
end;
procedure TSnippets.Clear;
{Clears the object's data.
}
begin
fCategories.Clear;
fRoutines.Clear;
end;
constructor TSnippets.Create;
{Class constructor. Sets up new empty object.
}
begin
inherited Create;
fRoutines := TRoutineListEx.Create(True);
fCategories := TCategoryListEx.Create(True);
fChangeEvents := TMultiCastEvents.Create(Self);
end;
function TSnippets.CreateTempRoutine(const Routine: TRoutine): TRoutine;
{Creates a new temporary copy of a snippet without adding it to the
Snippets object's snippets list. The new instance may not be added to the
Snippets object.
@param Routine [in] Snippet to be copied.
@return Reference to new snippet.
}
var
Data: TRoutineEditData; // data describing snippet's properties and references
begin
Assert(Assigned(Routine), ClassName + '.CreateTempRoutine: Routine is nil');
Assert(Routine is TRoutineEx,
ClassName + '.CreateTempRoutine: Routine is not a TRoutineEx');
Data := (Routine as TRoutineEx).GetEditData;
Result := TTempRoutine.Create(
Routine.Name, Routine.UserDefined, (Routine as TRoutineEx).GetProps);
(Result as TTempRoutine).UpdateRefs(
(Routine as TRoutineEx).GetReferences, fRoutines
);
end;
function TSnippets.CreateTempRoutine(const RoutineName: string;
const Data: TRoutineEditData): TRoutine;
{Creates a new temporary user defined snippet without adding it to the
Snippets object's snippets list. The new instance may not be added to the
Snippets object.
@param RoutineName [in] Name of new snippet.
@param Data [in] Record storing new snippet's properties and references.
@return Reference to new snippet.
}
begin
Result := TTempRoutine.Create(RoutineName, True, Data.Props);
(Result as TTempRoutine).UpdateRefs(Data.Refs, fRoutines);
end;
procedure TSnippets.DeleteCategory(const Category: TCategory);
{Deletes a category and all its snippets from the user database.
@param Category [in] Category to be deleted.
}
var
SnipIdx: Integer; // loops thru all category's snippets
begin
Assert(Category.UserDefined,
ClassName + '.DeleteCategory: Category is not user defined');
Assert(fCategories.Contains(Category),
ClassName + '.DeleteCategory: Category is not in the database');
TriggerEvent(evChangeBegin);
try
// all snippets that belong to category are deleted before category itself
// can't use for..in here since Routines list is modified in loop
for SnipIdx := Pred(Category.Routines.Count) downto 0 do
InternalDeleteRoutine(Category.Routines[SnipIdx]);
InternalDeleteCategory(Category);
TriggerEvent(evCategoryDeleted);
finally
TriggerEvent(evChangeEnd);
fUpdated := True;
end;
end;
procedure TSnippets.DeleteRoutine(const Routine: TRoutine);
{Deletes a snippet from the user database.
@param Routine [in] Snippet to be deleted.
}
var
Dependent: TRoutine; // loops thru each snippet that depends on Routine
Dependents: TRoutineList; // list of dependent snippets
Referrer: TRoutine; // loops thru snippets that cross references Routine
Referrers: TRoutineList; // list of referencing snippets
begin
Assert(Routine.UserDefined,
ClassName + '.DeleteRoutine: Routine is not user-defined');
Assert(fRoutines.Contains(Routine),
ClassName + '.DeleteRoutine: Routine is not in the database');
TriggerEvent(evChangeBegin);
// Get list of referencing and dependent routines
Dependents := nil;
Referrers := nil;
try
Dependents := TRoutineList.Create;
GetDependentList(Routine, Dependents);
Referrers := TRoutineList.Create;
GetReferrerList(Routine, Referrers);
// Delete routine for XRef or Depends list of referencing snippets
for Referrer in Referrers do
(Referrer.XRef as TRoutineListEx).Delete(Routine);
for Dependent in Dependents do
(Dependent.Depends as TRoutineListEx).Delete(Routine);
// Delete routine itself
InternalDeleteRoutine(Routine);
TriggerEvent(evRoutineDeleted);
finally
FreeAndNil(Referrers);
FreeAndNil(Dependents);
fUpdated := True;
TriggerEvent(evChangeEnd);
end;
end;
destructor TSnippets.Destroy;
{Class destructor. Tidies up and tears down object.
}
begin
FreeAndNil(fChangeEvents);
FreeAndNil(fCategories);
FreeAndNil(fRoutines);
inherited;
end;
function TSnippets.GetCategories: TCategoryList;
{Gets list of all categories in database.
@return Required list.
}
begin
Result := fCategories;
end;
procedure TSnippets.GetDependentList(const ARoutine: TRoutine;
const List: TRoutineList);
{Builds a list of all snippets that depend on a specified snippet.
@param ARoutine [in] Snippet for which dependents are required.
@param List [in] Receives list of dependent snippets.
}
var
Routine: TRoutine; // references each snippet in database
begin
List.Clear;
for Routine in fRoutines do
if not Routine.IsEqual(ARoutine) and Routine.Depends.Contains(ARoutine) then
List.Add(Routine);
end;
function TSnippets.GetDependents(const Routine: TRoutine): ISnippetIDList;
{Builds an ID list of all snippets that depend on a specified snippet.
@param Routine [in] Snippet for which dependents are required.
@return List of IDs of dependent snippets.
}
var
List: TRoutineList; // list of dependent snippets
begin
List := TRoutineList.Create;
try
GetDependentList(Routine, List);
Result := TSnippetIDListEx.Create(List);
finally
FreeAndNil(List);
end;
end;
function TSnippets.GetEditableCategoryInfo(
const Category: TCategory): TCategoryData;
{Provides details of all a category's data that may be edited.
@param Category [in] Category for which data is required. May be nil in
whih case a blank record is returned.
@return Required data.
}
begin
Assert(not Assigned(Category) or Category.UserDefined,
ClassName + '.GetEditableCategoryInfo: Category is not user-defined');
if Assigned(Category) then
else
Result.Init;
end;
function TSnippets.GetEditableRoutineInfo(
const Routine: TRoutine): TRoutineEditData;
{Provides details of all a snippet's data (properties and references) that may
be edited.
@param Routine [in] Snippet for which data is required. May be nil in which
case a blank record is returned.
@return Required data.
}
begin
Assert(not Assigned(Routine) or Routine.UserDefined,
ClassName + '.GetEditableRoutineInfo: Routine is not user-defined');
if Assigned(Routine) then
Result := (Routine as TRoutineEx).GetEditData
else
Result.Init;
end;
function TSnippets.GetReferrers(const Routine: TRoutine): ISnippetIDList;
{Builds an ID list of all snippets that cross reference a specified
snippet.
@param Routine [in] Snippet which is cross referenced.
@return List of IDs of referring snippets.
}
var
List: TRoutineList; // list of referring snippets
begin
List := TRoutineList.Create;
try
GetReferrerList(Routine, List);
Result := TSnippetIDListEx.Create(List);
finally
FreeAndNil(List);
end;
end;
procedure TSnippets.GetReferrerList(const ARoutine: TRoutine;
const List: TRoutineList);
{Builds list of all snippets that cross reference a specified snippet.
@param ARoutine [in] The cross referenced snippet.
@param List [in] Receives list of cross referencing snippets.
}
var
Routine: TRoutine; // references each routine in database
begin
List.Clear;
for Routine in fRoutines do
if not Routine.IsEqual(ARoutine) and Routine.XRef.Contains(ARoutine) then
List.Add(Routine);
end;
function TSnippets.GetRoutines: TRoutineList;
{Gets list of all snippets in database.
@return Required list.
}
begin
Result := fRoutines;
end;
function TSnippets.InternalAddCategory(const CatName: string;
const Data: TCategoryData): TCategory;
{Adds a new category to the user database. Assumes category not already in
user database.
@param CatName [in] Name of new category.
@param Data [in] Properties of new category.
@return Reference to new category object.
}
begin
Result := TCategoryEx.Create(CatName, True, Data);
fCategories.Add(Result);
end;
function TSnippets.InternalAddRoutine(const RoutineName: string;
const Data: TRoutineEditData): TRoutine;
{Adds a new snippet to the user database. Assumes snippet not already in user
database.
@param RoutineName [in] Name of new snippet.
@param Data [in] Properties and references of new snippet.
@return Reference to new snippet object.
@except Exception raised if snippet's category does not exist.
}
var
Cat: TCategory; // category object containing new snippet
resourcestring
// Error message
sCatNotFound = 'Category "%0:s" for new snippet "%1:s" does not exist';
begin
Result := TRoutineEx.Create(RoutineName, True, Data.Props);
(Result as TRoutineEx).UpdateRefs(Data.Refs, fRoutines);
Cat := fCategories.Find(Result.Category);
if not Assigned(Cat) then
raise ECodeSnip.CreateFmt(sCatNotFound, [Result.Category, Result.Name]);
Cat.Routines.Add(Result);
fRoutines.Add(Result);
end;
procedure TSnippets.InternalDeleteCategory(const Cat: TCategory);
{Deletes a category from the user database.
@param Cat [in] Category to delete from database.
}
begin
TriggerEvent(evBeforeCategoryDelete, Cat);
(fCategories as TCategoryListEx).Delete(Cat);
TriggerEvent(evAfterCategoryDelete);
end;
procedure TSnippets.InternalDeleteRoutine(const Routine: TRoutine);
{Deletes a snippet from the user database.
@param Routine [in] Snippet to delete from database.
}
var
Cat: TCategory; // category containing snippet
begin
// Delete from category if found
Cat := fCategories.Find(Routine.Category);
if Assigned(Cat) then
(Cat.Routines as TRoutineListEx).Delete(Routine);
// Delete from "master" list: this frees Routine
TriggerEvent(evBeforeRoutineDelete, Routine);
(fRoutines as TRoutineListEx).Delete(Routine);
TriggerEvent(evAfterRoutineDelete);
end;
procedure TSnippets.Load;
{Loads object's data from main and user defined databases.
}
var
Factory: ISnippetsFactory; // object reader uses to create snippets objects
begin
Clear;
// Create factory that reader calls into to create category and snippet
// objects. This is done to keep updating of snippet and categories private
// to this unit
Factory := TSnippetsFactory.Create;
try
// Load main database: MUST do this first since user database can
// reference objects in main database
with TSnippetsIOFactory.CreateMainDBLoader do
Load(fRoutines, fCategories, Factory);
// Load any user database
with TSnippetsIOFactory.CreateUserDBLoader do
Load(fRoutines, fCategories, Factory);
fUpdated := False;
except
// If an exception occurs clear the database
Clear;
raise;
end;
end;
procedure TSnippets.RemoveChangeEventHandler(const Handler: TNotifyEventInfo);
{Removes a change event handler from list of listeners.
@param Handler [in] Handler to remove from list.
}
begin
fChangeEvents.RemoveHandler(Handler);
end;
procedure TSnippets.Save;
{Saves user defined snippets and all categories to user database.
}
var
Provider: ISnippetsDataProvider; // object that supplies info to writer
begin
// Create object that can provide required information about user database
Provider := TUserDataProvider.Create(fRoutines, fCategories);
// Use a writer object to write out the database
with TSnippetsIOFactory.CreateWriter do
Write(fRoutines, fCategories, Provider);
fUpdated := False;
end;
procedure TSnippets.TriggerEvent(const Kind: TSnippetChangeEventKind;
const Info: TObject);
{Triggers a change event. Notifies all registered listeners.
@param Kind [in] Kind of event.
@param Info [in] Reference to any further information for event. May be nil.
}
var
EvtInfo: ISnippetChangeEventInfo; // event information object
begin
EvtInfo := TEventInfo.Create(Kind, Info);
fChangeEvents.TriggerEvents(EvtInfo);
end;
function TSnippets.UpdateCategory(const Category: TCategory;
const Data: TCategoryData): TCategory;
{Updates a user defined category's properties.
@param Category [in] Category to be updated. Must be user-defined.
@param Data [in] Record containing revised data.
@return Reference to updated category. Will have changed.
}
var
SnippetList: TRoutineList;
Snippet: TRoutine;
CatName: string;
begin
TriggerEvent(evChangeBegin);
try
SnippetList := TRoutineList.Create;
try
for Snippet in Category.Routines do
SnippetList.Add(Snippet);
CatName := Category.Category;
InternalDeleteCategory(Category);
Result := InternalAddCategory(CatName, Data);
for Snippet in SnippetList do
Result.Routines.Add(Snippet);
finally
FreeAndNil(SnippetList);
end;
TriggerEvent(evCategoryChanged, Result);
finally
fUpdated := True;
TriggerEvent(evChangeEnd);
end;
end;
function TSnippets.Updated: Boolean;
{Checks if user database has been updated since last save.
@return True if database has been updated, False otherwise.
}
begin
Result := fUpdated;
end;
function TSnippets.UpdateRoutine(const Routine: TRoutine;
const Data: TRoutineEditData; const NewName: string): TRoutine;
{Updates a user defined snippet's properties and references using provided
data.
@param Routine [in] Snippet to be updated. Must be user-defined.
@param Data [in] Record containing revised data.
@param NewName [in] New name of snippet. Set to '' or Routine.Name if name
is not to change.
@return Reference to updated snippet. Will have changed.
}
var
RoutineName: string; // name of snippet
Dependent: TRoutine; // loops thru each snippetthat depends on Routine
Dependents: TRoutineList; // list of dependent snippets
Referrer: TRoutine; // loops thru snippets that cross references Routine
Referrers: TRoutineList; // list of referencing snippets
resourcestring
// Error message
sCantRename = 'Can''t rename snippet %0:s to %1:s: Snippet %1:s already '
+ 'exists in user database';
begin
Result := Routine; // keeps compiler happy
Assert(Routine.UserDefined,
ClassName + '.UpdateRoutine: Routine is not user-defined');
TriggerEvent(evChangeBegin);
try
// Calculate new name
if NewName <> '' then
RoutineName := NewName
else
RoutineName := Routine.Name;
// If name has changed then new name musn't exist in user database
if not AnsiSameText(RoutineName, Routine.Name) then
if fRoutines.Find(RoutineName, True) <> nil then
raise ECodeSnip.CreateFmt(sCantRename, [Routine.Name, RoutineName]);
// We update by deleting old snippet and inserting new one
// get lists of snippets that cross reference or depend on this routine
Dependents := TRoutineList.Create;
GetDependentList(Routine, Dependents);
Referrers := TRoutineList.Create;
GetReferrerList(Routine, Referrers);
// remove invalid references from referring snippets
for Referrer in Referrers do
(Referrer.XRef as TRoutineListEx).Delete(Routine);
for Dependent in Dependents do
(Dependent.Depends as TRoutineListEx).Delete(Routine);
// delete the snippet
InternalDeleteRoutine(Routine);
// add new routine
Result := InternalAddRoutine(RoutineName, Data);
// add new snippet to referrer list of referring snippets
for Referrer in Referrers do
Referrer.XRef.Add(Result);
for Dependent in Dependents do
Dependent.Depends.Add(Result);
TriggerEvent(evRoutineChanged, Result);
finally
fUpdated := True;
TriggerEvent(evChangeEnd);
end;
end;
{ TSnippets.TEventInfo }
constructor TSnippets.TEventInfo.Create(const Kind: TSnippetChangeEventKind;
const Info: TObject);
{Class constructor. Creates an event information object.
@param Kind [in] Kind of event.
@param Routine [in] Reference to any snippet affected by event. May be nil
if event does not affect a snippet.
}
begin
inherited Create;
fKind := Kind;
fInfo := Info;
end;
function TSnippets.TEventInfo.GetInfo: TObject;
{Gets additional information about event.
@return Object that provides required information.
}
begin
Result := fInfo;
end;
function TSnippets.TEventInfo.GetKind: TSnippetChangeEventKind;
{Gets kind (type) of event.
@return Event kind.
}
begin
Result := fKind;
end;
{ TRoutine }
function TRoutine.CanCompile: Boolean;
{Checks if snippet can be compiled.
@return True if compilation supported and False if not.
}
begin
Result := Kind <> skFreeform;
end;
constructor TRoutine.Create(const Name: string; const UserDefined: Boolean;
const Props: TRoutineData);
{Class contructor. Sets up snippet object with given property values.
@param Name [in] Name of snippet.
@param UserDefined [in] Indicates if this is a user defined snippet.
@param Props [in] Values of various snippet properties.
}
begin
Assert(ClassType <> TRoutine,
ClassName + '.Create: must only be called from descendants.');
inherited Create;
// Record simple property values
SetName(Name);
SetProps(Props);
// Create string list to store required units
fUnits := TStringList.Create;
// Create routine lists for Depends and XRef properties
fDepends := TRoutineListEx.Create;
fXRef := TRoutineListEx.Create;
// The following properties added to support user defined snippets
fUserDefined := UserDefined;
end;
destructor TRoutine.Destroy;
{Class destructor. Tears down object.
}
begin
FreeAndNil(fXRef);
FreeAndNil(fDepends);
FreeAndNil(fUnits);
fExtra := nil;
inherited;
end;
function TRoutine.GetID: TSnippetID;
{Gets snippet's unique ID.
@return Required ID.
}
begin
Result := TSnippetID.Create(fName, fUserDefined);
end;
function TRoutine.IsEqual(const Routine: TRoutine): Boolean;
{Checks if this snippet is same as another snippet. Snippets are considered
equal if they have the same name and come from same database.
@param Routine [in] Snippet being compared.
@return True if snippets are equal, False if not.
}
begin
Result := Routine.ID = Self.ID;
end;
procedure TRoutine.SetName(const Name: string);
{Sets Name property.
@param Name [in] New name.
}
begin
fName := Name;
end;
procedure TRoutine.SetProps(const Data: TRoutineData);
{Sets snippet's properties.
@param Data [in] Record containing property values.
}
begin
fCategory := Data.Cat;
fKind := Data.Kind;
fDescription := Data.Desc;
fSourceCode := AdjustLineBreaks(Data.SourceCode); // ensure EOLs are CRLF
fExtra := TActiveTextFactory.CloneActiveText(Data.Extra);
fCompatibility := Data.CompilerResults;
end;
{ TRoutineEx }
function TRoutineEx.GetEditData: TRoutineEditData;
{Gets details of all editable data of snippet.
@return Required editable properties and references.
}
begin
Result.Props := GetProps;
Result.Refs := GetReferences;
end;
function TRoutineEx.GetProps: TRoutineData;
{Gets details of snippet's properties.
@return Record containing property values.
}
begin
Result.Cat := Category;
Result.Kind := Kind;
Result.Desc := Description;
Result.SourceCode := SourceCode;
Result.Extra := TActiveTextFactory.CloneActiveText(Extra);
Result.CompilerResults := Compatibility;
end;
function TRoutineEx.GetReferences: TRoutineReferences;
{Gets details of snippet's references.
@return Information sufficient to define references.
}
begin
Result.Units := TIStringList.Create(Units);
Result.Depends := TSnippetIDListEx.Create(Depends);
Result.XRef := TSnippetIDListEx.Create(XRef);
end;
procedure TRoutineEx.Update(const Data: TRoutineEditData;
const AllRoutines: TRoutineList);
{Updates snippet's properties and references.
@param Data [in] New property values and references.
@param AllRoutines [in] List of all snippets in database.
}
begin
SetProps(Data.Props);
UpdateRefs(Data.Refs, AllRoutines);
end;
procedure TRoutineEx.UpdateRefs(const Refs: TRoutineReferences;
const AllRoutines: TRoutineList);
{Updates a snippet's references.
@param Refs [in] Stores all snippet's references (XRef, Depends and
Units).
@param AllRoutines [in] List of all snippets in database.
}
// ---------------------------------------------------------------------------
procedure BuildRoutineList(const SL: TRoutineList;
const IDList: ISnippetIDList);
{Creates a snippets list from a snippets ID list. Looks up snippets in list
of all snippets in database. Any snippets in ID list that do not exist in
database are ignored.
@param SL [in] Snippets list object to be updated.
@param IDList [in] Snippets ID list that provides information used to
create snippets list.
}
var
ID: TSnippetID; // refers to each ID in ID list
Routine: TRoutine; // references each snippet identified by ID
begin
SL.Clear;
for ID in IDList do
begin
Routine := AllRoutines.Find(ID.Name, ID.UserDefined);
if Assigned(Routine) then
SL.Add(Routine);
end;
end;
// ---------------------------------------------------------------------------
begin
Refs.Units.CopyTo(Self.Units, True); // copy units
BuildRoutineList(Self.Depends, Refs.Depends); // build Depends list
BuildRoutineList(Self.XRef, Refs.XRef); // build XRef list
end;
{ TRoutineList }
function TRoutineList.Add(const Routine: TRoutine): Integer;
{Adds new snippet to the list, maintaining list in alphabetical order.
@param Routine [in] Snippet being added.
@return Index where item was inserted in list
@except Raised if duplicate snippet added to list.
}
var
Idx: Integer; // loops thru list
Cmp: Integer; // result of comparison of snippet names
begin
// Assume adding to end of list
Result := Count;
// Loop thru existing items searching for location to insert
for Idx := 0 to Pred(Count) do
begin
// compare new item to current item
Cmp := CompareIDs(Routine.ID, Items[Idx].ID);
if Cmp = 0 then
// don't allow duplicates
raise EBug.CreateFmt(
ClassName + '.Add: Duplicate snippet name (%s) in same database',
[Routine.Name]
);
if Cmp < 0 then
begin
// current item is greater than new one: insert before it
Result := Idx;
Break;
end;
end;
// add the new item
fList.Insert(Result, Routine);
end;
procedure TRoutineList.Assign(const SrcList: TRoutineList);
{Sets this list to be same as another list. List items are referenced, not
copied.
@param SrcList [in] List of snippets to be assigned.
}
var
Idx: Integer; // loops thru source list
begin
Assert(not fList.OwnsObjects,
ClassName + '.Assign: can''t assign to master list');
Self.Clear;
if Assigned(SrcList) then
begin
for Idx := 0 to Pred(SrcList.Count) do
Self.Add(SrcList[Idx]);
end;
end;
procedure TRoutineList.Clear;
{Clears the list.
}
begin
fList.Clear;
end;
function TRoutineList.CompareIDs(const SID1, SID2: TSnippetID): Integer;
{Compares IDS of two snippets.
@param SID1 [in] First ID to be compared.
@param SID2 [in] Second ID to be compared.
@return 0 if IDs are the same, -ve if SID1 < SID2 and +ve if
SID1 > SID2.
}
begin
Result := SID1.Compare(SID2);
end;
function TRoutineList.Contains(const Routine: TRoutine): Boolean;
{Checks whether list contains a specified snippet.
@param Routine [in] Required snippet.
@return True if snippet is in list, False otherwise.
}
begin
Result := fList.Contains(Routine);
end;
function TRoutineList.ContainsKinds(const Kinds: TSnippetKinds): Boolean;
{Checks if the list contains specified kinds of snippets.
@param Kinds [in] Set of kinds to test for.
@return True if list contains at least one snippet of any of the specified
kinds.
}
var
Snippet: TRoutine; // reference to all snippets in list
begin
Result := False;
for Snippet in Self do
if Snippet.Kind in Kinds then
begin
Result := True;
Break;
end;
end;
function TRoutineList.Count(const UserDefined: Boolean): Integer;
{Counts number of snippets in list that are either from or not from user
defined database.
@param UserDefined [in] Flags whether to count snippets in user database
(True) or in main database (False).
@return Number of snippets in specified database.
}
var
Routine: TRoutine; // refers to all snippets in list
begin
Result := 0;
for Routine in Self do
if Routine.UserDefined = UserDefined then
Inc(Result);
end;
function TRoutineList.Count: Integer;
{Counts number of snippets in list.
@return Number of snippets in list.
}
begin
Result := fList.Count;
end;
constructor TRoutineList.Create(const OwnsObjects: Boolean = False);
{Class constructor. Creates a new empty list.
@param OwnsObjects [in] Specifies whether list owns the snippet objects it
contains. If True the snippets are freed when deleted from list.
}
begin
inherited Create;
fList := TObjectListEx.Create(OwnsObjects);
end;
destructor TRoutineList.Destroy;
{Class destructor. Tears down object.
}
begin
FreeAndNil(fList); // destroys owned snippets if OwnsObjects=True
inherited;
end;
function TRoutineList.Find(const RoutineName: string;
const UserDefined: Boolean; out Index: Integer): Boolean;
{Finds a snippet in the list that has a specified name and user defined
property. Uses a binary search.
@param RoutineName [in] Name of snippet to be found.
@param UserDefined [in] Whether required snippet is user defined or not.
@param Index [out] Index of required snippet in list. Valid only if
method returns True.
@return True if snippet found, False if not.
}
var
Low, High: Integer; // low and high bounds of search
Cur: Integer; // current item to be tested
Cmp: Integer; // result of comparing two items
SearchID: TSnippetID; // ID of snippet being search for
begin
// Uses binary search: based on TStringList.Find from Delphi Classes unit
// Initialise
Result := False;
Low := 0;
High := Count - 1;
SearchID := TSnippetID.Create(RoutineName, UserDefined);
// Perform binary search
while Low <= High do
begin
// Choose item to be compared: mid point of range
Cur := (Low + High) shr 1;
// Compare chosen
Cmp := CompareIDs(Items[Cur].ID, SearchID);
if Cmp < 0 then
// Current item < required: search above current item
Low := Cur + 1
else
begin
// Current item >= required: search below current item
High := Cur - 1;
if Cmp = 0 then
begin
// We found it: set Low to Cur because Cur > High => loop will terminate
Result := True;
Low := Cur;
end;
end;
end;
// Set index (Low = Cur if found)
Index := Low;
end;
function TRoutineList.Find(const RoutineName: string;
const UserDefined: Boolean): TRoutine;
{Finds a named snippet in list with a matching user defined property.
@param RoutineName [in] Name of required snippet.
@param UserDefined [in] Flag that determines if we are looking for a
user defined snippet or one from main database.
@return Reference to object representing snippet in list or nil if not
in list.
}
var
Idx: Integer; // index of snippet name in list
begin
if Find(RoutineName, UserDefined, Idx) then
Result := Items[Idx]
else
Result := nil;
end;
function TRoutineList.GetEnumerator: TEnumerator;
{Gets an intialised snippet list enumerator.
@return Required enumerator.
}
begin
Result := TEnumerator.Create(Self);
end;
function TRoutineList.GetItem(Idx: Integer): TRoutine;
{Read accessor for Items property.
@param Idx [in] Index of required snippet in list.
@return Snippet at specified index in list.
}
begin
Result := fList[Idx] as TRoutine;
end;
function TRoutineList.IsEqual(const AList: TRoutineList): Boolean;
{Checks if this list contains same snippets as another list.
@param AList [in] List of snippets to compare.
@return True if lists are same, False if not.
}
var
Idx: Integer; // loops thru all snippets in list
begin
// To be same comparison list must exist and have same number of snippets
Result := Assigned(AList) and (Self.Count = AList.Count);
if Result then
begin
// Same number of snippets: scan list checking routine names same. We can
// rely on items being in same order since lists are sorted
for Idx := 0 to Pred(Self.Count) do
begin
if not Self[Idx].IsEqual(AList[Idx]) then
begin
Result := False;
Break;
end;
end;
end;
end;
{ TRoutineList.TEnumerator }
constructor TRoutineList.TEnumerator.Create(const RL: TRoutineList);
{Class constructor. Sets up and initialises enumeration.
@param RL [in] Reference to snippet list object to be enumerated.
}
begin
inherited Create;
fRoutineList := RL;
fIndex := -1;
end;
function TRoutineList.TEnumerator.GetCurrent: TRoutine;
{Gets current snippet in enumeration. Error if at end of enumeration.
@return Current snippet.
}
begin
Result := fRoutineList[fIndex];
end;
function TRoutineList.TEnumerator.MoveNext: Boolean;
{Moves to next item in enumeration.
@return True if there is a next item, False if enumeration
completed.
}
begin
Result := fIndex < Pred(fRoutineList.Count);
if Result then
Inc(fIndex);
end;
{ TRoutineListEx }
function TRoutineListEx.Add(const Routine: TRoutine): Integer;
{Adds a snippet to list. Prevents TTempRoutine instances from being added.
@param Routine [in] Snippet to be added.
@return Index where snippet was added to list.
@except EBug raised if Routine is a TTempRoutine instance.
}
begin
if Routine is TTempRoutine then
// don't allow temp routines to be added to list
raise EBug.CreateFmt(
ClassName + '.Add: Can''t add temporary snippets to database (%s)',
[Routine.Name]
);
Result := inherited Add(Routine);
end;
procedure TRoutineListEx.Delete(const Routine: TRoutine);
{Deletes a snippet from list.
@param Routine [in] Snippet to be deleted. No action taken if snippet not in
list.
}
var
Idx: Integer; // index of snippet in list.
begin
Idx := fList.IndexOf(Routine);
if Idx = -1 then
Exit;
fList.Delete(Idx); // this frees snippet if list owns objects
end;
{ TCategory }
constructor TCategory.Create(const CatID: string; const UserDefined: Boolean;
const Data: TCategoryData);
{Class contructor. Sets up category object with given property values.
@param Data [in] Contains required property values.
}
begin
Assert(ClassType <> TCategory,
ClassName + '.Create: must only be called from descendants.');
inherited Create;
fCategory := CatID;
fDescription := Data.Desc;
fUserDefined := UserDefined;
// Create list to store snippets in category
fRoutines := TRoutineListEx.Create;
end;
destructor TCategory.Destroy;
{Class destructor. Tears down object.
}
begin
FreeAndNil(fRoutines);
inherited;
end;
function TCategory.IsEqual(const Cat: TCategory): Boolean;
{Checks if this category is same as another category. Categories are
considered equal if they have the same name.
@param Cat [in] Category being compared.
@return True if categories are equal, False if not.
}
begin
Result := AnsiSameText(Self.Category, Cat.Category);
end;
{ TCategoryEx }
function TCategoryEx.GetEditData: TCategoryData;
{Gets details of all editable data of category.
@return Required editable data.
}
begin
Result.Desc := Self.Description;
end;
{ TCategoryList }
function TCategoryList.Add(const Category: TCategory): Integer;
{Adds new category to list.
@param Category [in] Category to be added.
@return Index where category inserted in list.
}
begin
Assert(not fList.Contains(Category),
ClassName + '.Add: Category already in list');
Result := fList.Add(Category);
end;
procedure TCategoryList.Clear;
{Clears the list.
}
begin
fList.Clear;
end;
function TCategoryList.Contains(const Category: TCategory): Boolean;
{Checks if a category is in list. Checks object reference.
@param Category [in] Category to be checked.
@return True if category object is in list.
}
begin
Result := fList.Contains(Category);
end;
constructor TCategoryList.Create(const OwnsObjects: Boolean);
{Class constructor. Creates a new empty list.
@param OwnsObjects [in] Specifies whether list owns the category objects it
contains. If True the categories are freed when deleted from list.
}
begin
inherited Create;
fList := TObjectListEx.Create(OwnsObjects);
end;
destructor TCategoryList.Destroy;
{Class destructor. Tears down object.
}
begin
FreeAndNil(fList);
inherited;
end;
function TCategoryList.Find(const CatName: string): TCategory;
{Finds a named category in list.
@param CatName [in] Name of required category.
@return Reference to object representing category in list or nil if not in
list.
}
var
Idx: Integer; // loops thru categories
begin
Result := nil;
for Idx := 0 to Pred(Count) do
begin
if AnsiSameText(CatName, Items[Idx].Category) then
begin
Result := Items[Idx];
Break;
end;
end;
end;
function TCategoryList.GetCount: Integer;
{Read accessor for Count property.
@return Number of categories in list.
}
begin
Result := fList.Count;
end;
function TCategoryList.GetEnumerator: TEnumerator;
{Gets an intialised category list enumerator.
@return Required enumerator.
}
begin
Result := TEnumerator.Create(Self);
end;
function TCategoryList.GetItem(Idx: Integer): TCategory;
{Read accessor for Items property.
@param Idx [in] Index of required category in list.
@return Category at specified index in list.
}
begin
Result := fList[Idx] as TCategory;
end;
{ TCategoryList.TEnumerator }
constructor TCategoryList.TEnumerator.Create(const CL: TCategoryList);
{Class constructor. Sets up and initialises enumeration.
@param CL [in] Reference to object to be enumerated.
}
begin
inherited Create;
fCategoryList := CL;
fIndex := -1;
end;
function TCategoryList.TEnumerator.GetCurrent: TCategory;
{Gets current category in enumeration. Error if at end of enumeration.
@return Current category.
}
begin
Result := fCategoryList[fIndex];
end;
function TCategoryList.TEnumerator.MoveNext: Boolean;
{Moves to next item in enumeration.
@return True if there is a next item, False if enumeration completed.
}
begin
Result := fIndex < Pred(fCategoryList.Count);
if Result then
Inc(fIndex);
end;
{ TCategoryListEx }
procedure TCategoryListEx.Delete(const Category: TCategory);
{Deletes a category from the list.
@param Category [in] Category to be deleted.
}
var
Idx: Integer; // index of snippet in list.
begin
Idx := fList.IndexOf(Category);
if Idx = -1 then
Exit;
fList.Delete(Idx); // this frees category if list owns objects
end;
{ TSnippetsFactory }
function TSnippetsFactory.CreateCategory(const CatID: string;
const UserDefined: Boolean; const Data: TCategoryData): TCategory;
{Creates a new category object.
@param CatID [in] ID (name) of new category. Must be unique.
@param UserDefined [in] True if category is user defined, False if not.
@param Data [in] Record describing category's properties.
@return Instance of new category object.
}
begin
Result := TCategoryEx.Create(CatID, UserDefined, Data);
end;
function TSnippetsFactory.CreateRoutine(const Name: string;
const UserDefined: Boolean; const Props: TRoutineData): TRoutine;
{Creates a new snippet object.
@param Name [in] Name of new snippet. Must not exist in database specified
by UserDefined parameter.
@param UserDefined [in] True if snippet is user defined, False if not.
@param Props [in] Record describing snippet's properties.
@return Instance of new snippet with no references.
}
begin
Result := TRoutineEx.Create(Name, UserDefined, Props);
end;
{ TUserDataProvider }
constructor TUserDataProvider.Create(const Routines: TRoutineList;
const Categories: TCategoryList);
{Class constructor. Records list of all snippets and categories in both
databases.
@param Routines [in] List of all snippets in whole database.
@param Categories [in] List of all categories in whole database.
}
begin
inherited Create;
fRoutines := Routines;
fCategories := Categories;
end;
function TUserDataProvider.GetCategoryProps(
const Cat: TCategory): TCategoryData;
{Retrieves all the properties of a category.
@param Cat [in] Category for which data is requested.
@return Record containing property data.
}
begin
Result.Desc := Cat.Description;
end;
function TUserDataProvider.GetCategoryRoutines(
const Cat: TCategory): IStringList;
{Retrieves names of all user-defined snippets that belong to a category.
@param Cat [in] Category for which snippet names are requested.
@return Required list of snippet names.
}
var
Routine: TRoutine; // references each snippet in category
begin
Result := TIStringList.Create;
for Routine in Cat.Routines do
if Routine.UserDefined then
Result.Add(Routine.Name);
end;
function TUserDataProvider.GetRoutineProps(
const Routine: TRoutine): TRoutineData;
{Retrieves all the properties of a snippet.
@param Routine [in] Snippet for which data is requested.
@return Record containing property data.
}
begin
Result := (Routine as TRoutineEx).GetProps;
end;
function TUserDataProvider.GetRoutineRefs(
const Routine: TRoutine): TRoutineReferences;
{Retrieves information about all the references of a snippet.
@param Routine [in] Snippet for which information is requested.
@return Record containing references.
}
begin
Result := (Routine as TRoutineEx).GetReferences;
end;
{ TRoutineData }
procedure TRoutineData.Assign(const Src: TRoutineData);
{Sets this record to be a copy of another record.
@param Src [in] Record to be copied.
}
begin
Kind := Src.Kind;
Cat := Src.Cat;
Desc := Src.Desc;
SourceCode := Src.SourceCode;
// we use cloning for Extra below because it deals uccessfully with both
// Self.Extra = nil and Src.Extra = nil
Extra := TActiveTextFactory.CloneActiveText(Src.Extra);
CompilerResults := Src.CompilerResults;
end;
procedure TRoutineData.Init;
{Initialises record by setting default values for fields.
}
var
CompID: TCompilerID; // loops thru compiler IDs
begin
Kind := skFreeform;
Cat := '';
Desc := '';
SourceCode := '';
Extra := TActiveTextFactory.CreateActiveText;
for CompID := Low(TCompilerID) to High(TCompilerID) do
CompilerResults[CompID] := crQuery;
end;
{ TRoutineReferences }
procedure TRoutineReferences.Assign(const Src: TRoutineReferences);
{Sets this record to be a copy of another record.
@param Src [in] Record to be copied.
}
begin
Init;
(Units as IAssignable).Assign(Src.Units);
(Depends as IAssignable).Assign(Src.Depends);
(XRef as IAssignable).Assign(Src.XRef);
end;
procedure TRoutineReferences.Init;
{Initialises record by creating objects stored in fields.
}
begin
Units := TIStringList.Create;
Depends := TSnippetIDList.Create;
XRef := TSnippetIDList.Create;
end;
{ TRoutineEditData }
procedure TRoutineEditData.Assign(const Src: TRoutineEditData);
{Sets this record to be a copy of another record.
@param Src [in] Record to be copied.
}
begin
Props.Assign(Src.Props);
Refs.Assign(Src.Refs);
end;
procedure TRoutineEditData.Init;
{Initialises record by creating default values and field objects.
}
begin
Props.Init;
Refs.Init;
end;
{ TRoutineInfo }
procedure TRoutineInfo.Assign(const Src: TRoutineInfo);
{Sets this record's fields to be same as another TRoutineInfo record.
Object fields are copied appropriately.
@param Src [in] Record containing fields to be copied.
}
begin
Name := Src.Name;
Data.Assign(Src.Data);
end;
procedure TRoutineInfo.Init;
{Initialises record to nul values.
}
begin
Name := '';
Data.Init;
end;
{ TCategoryData }
procedure TCategoryData.Assign(const Src: TCategoryData);
{Sets this record's fields to be same as another TCategoryData record.
@param Src [in] Record containing fields to be copied.
}
begin
Desc := Src.Desc;
end;
procedure TCategoryData.Init;
{Initialises record to nul values.
}
begin
Desc := '';
end;
{ TCategoryInfo }
procedure TCategoryInfo.Assign(const Src: TCategoryInfo);
{Sets this record's fields to be same as another TCategoryInfo record.
@param Src [in] Record containing fields to be copied.
}
begin
Name := Src.Name;
Data.Assign(Src.Data);
end;
procedure TCategoryInfo.Init;
{Initialises record to nul values.
}
begin
Name := '';
Data.Init;
end;
{ TSnippetIDListEx }
constructor TSnippetIDListEx.Create(const Routines: TRoutineList);
{Class constructor overload that creates a snippets ID list from a
TRoutineList object.
@param Routines [in] List of snippets objects for which ID list is
required.
}
var
Snippet: TRoutine; // references each snippet in list
begin
inherited Create;
for Snippet in Routines do
Add(Snippet.ID);
end;
initialization
finalization
// Free the singleton
PvtSnippets := nil;
end.
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.