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
|
{-------------------------------------------------------------------------------
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: SynHighlighterGeneral.pas, released 2000-04-07.
The Original Code is based on the mwGeneralSyn.pas file from the
mwEdit component suite by Martin Waldenburg and other developers, the Initial
Author of this file is Martin Waldenburg.
Portions written by Martin Waldenburg are copyright 1999 Martin Waldenburg.
All Rights Reserved.
Contributors to the SynEdit and mwEdit projects are listed in the
Contributors.txt file.
Alternatively, the contents of this file may be used under the terms of the
GNU General Public License Version 2 or later (the "GPL"), in which case
the provisions of the GPL are applicable instead of those above.
If you wish to allow use of your version of this file only under the terms
of the GPL and not to allow others to use your version of this file
under the MPL, indicate your decision by deleting the provisions above and
replace them with the notice and other provisions required by the GPL.
If you do not delete the provisions above, a recipient may use your version
of this file under either the MPL or the GPL.
$Id: SynHighlighterGeneral.pas,v 1.3 2000/11/08 22:09:59 mghie Exp $
You may retrieve the latest version of this file at the SynEdit home page,
located at http://SynEdit.SourceForge.net
}
unit SynBeautifier;
{$I synedit.inc}
interface
uses
Classes, SysUtils, RegExpr, LCLProc,
SynEditMiscClasses, SynEditMiscProcs, LazSynEditText, SynEditPointClasses,
SynEditKeyCmds, SynHighlighterPas, SynEditHighlighterFoldBase;
type
TSynCustomBeautifier = class;
// Callback for indent
TSynBeautifierSetIndentProc =
procedure(
(* LinePos:
1-based, the line that should be changed *)
LinePos: Integer;
(* Indent:
New indent in spaces (Logical = Physical *)
Indent: Integer;
(* RelativeToLinePos:
Indent specifies +/- offset from indent on RTLine (0: for absolute indent) *)
RelativeToLinePos: Integer = 0;
(* IndentChars:
String used to build indent; maybe empty, single char, or string (usually 1 tab or 1 space)
The String will be repeated and cut as needed, then filled with spaces at the end
* NOTE: If this is specified the TSynBeautifierIndentType is ignored
*)
IndentChars: String = '';
(* IndentCharsFromLinePos:
Use tab/space mix from this Line for indent (if specified > 0)
"IndentChars" will only be used, if the found tab/space mix is to short
* NOTE: If this is specified the TSynBeautifierIndentType is ignored
*)
IndentCharsFromLinePos: Integer = 0
) of object;
// Event triggered if Lines may needs Indend
TSynBeautifierGetIndentEvent =
function(
Sender: TObject; // the beautifier
Editor: TObject; // the synedit
LogCaret, OldLogCaret: TPoint; // Caret after and before the edit action
FirstLinePos, LastLinePos: Integer; // Changed lines. this can include lines outside the range of OldLogCaret to LogCaret
Reason: TSynEditorCommand; // what caused the event
SetIndentProc: TSynBeautifierSetIndentProc
): boolean of object;
{ TSynCustomBeautifier }
TSynCustomBeautifier = class(TComponent)
private
FAutoIndent: Boolean;
FOnGetDesiredIndent: TSynBeautifierGetIndentEvent;
FCurrentEditor: TSynEditBase; // For callback / applyIndent
FCurrentLines: TSynEditStrings;
protected
procedure DoBeforeCommand(const ACaret: TSynEditCaret;
var Command: TSynEditorCommand); virtual; abstract;
procedure DoAfterCommand(const ACaret: TSynEditCaret;
var Command: TSynEditorCommand;
StartLinePos, EndLinePos: Integer); virtual; abstract;
property CurrentEditor: TSynEditBase read FCurrentEditor;
property CurrentLines: TSynEditStrings read FCurrentLines;
public
procedure Assign(Src: TPersistent); override;
function GetCopy: TSynCustomBeautifier;
procedure BeforeCommand(const Editor: TSynEditBase; const Lines: TSynEditStrings;
const ACaret: TSynEditCaret; var Command: TSynEditorCommand;
InitialCmd: TSynEditorCommand);
procedure AfterCommand(const Editor: TSynEditBase; const Lines: TSynEditStrings;
const ACaret: TSynEditCaret; var Command: TSynEditorCommand;
InitialCmd: TSynEditorCommand; StartLinePos, EndLinePos: Integer);
// GetDesiredIndentForLine: Returns the 1-based Physical x pos
function GetDesiredIndentForLine
(Editor: TSynEditBase; const Lines: TSynEditStrings;
const ACaret: TSynEditCaret): Integer; virtual; abstract;
function GetDesiredIndentForLine
(Editor: TSynEditBase; const Lines: TSynEditStrings;
const ACaret: TSynEditCaret; out ReplaceIndent: Boolean;
out DesiredIndent: String): Integer; virtual; abstract;
property OnGetDesiredIndent: TSynBeautifierGetIndentEvent
read FOnGetDesiredIndent write FOnGetDesiredIndent;
property AutoIndent: Boolean read FAutoIndent write FAutoIndent;
end;
TSynCustomBeautifierClass = class of TSynCustomBeautifier;
TSynBeautifierIndentType = (
sbitSpace, sbitCopySpaceTab,
sbitPositionCaret,
sbitConvertToTabSpace, // convert to tabs, fill with spcaces if needed
sbitConvertToTabOnly // convert to tabs, even if shorter
);
{ TSynBeautifier }
TSynBeautifier = class(TSynCustomBeautifier)
private
FIndentType: TSynBeautifierIndentType;
protected
FLogicalIndentLen: Integer;
function GetLine(AnIndex: Integer): String; virtual;
procedure GetIndentInfo(const LinePos: Integer;
out ACharMix: String; out AnIndent: Integer;
AStopScanAtLine: Integer = 0);
// requiring FCurrentEditor, FCurrentLines
procedure DoBeforeCommand(const ACaret: TSynEditCaret;
var Command: TSynEditorCommand); override;
procedure DoAfterCommand(const ACaret: TSynEditCaret;
var Command: TSynEditorCommand;
StartLinePos, EndLinePos: Integer); override;
function GetIndent(const LinePos: Integer; out BasedOnLine: Integer;
AStopScanAtLine: Integer = 0): Integer;
function AdjustCharMix(DesiredIndent: Integer; CharMix, AppendMix: String): String;
function CreateTabSpaceMix(var DesiredIndent: Integer; OnlyTabs: Boolean): String;
function GetCharMix(const LinePos: Integer; var Indent: Integer;
var IndentCharsFromLinePos: Integer;
ModifyIndent: Boolean = False): String;
procedure ApplyIndent(LinePos: Integer; Indent: Integer;
RelativeToLinePos: Integer = 0; IndentChars: String = '';
IndentCharsFromLinePos: Integer = 0);
function UnIndentLine(const ACaret: TSynEditCaret; out CaretNewX: Integer): Boolean;
public
procedure Assign(Src: TPersistent); override;
// Retruns a 0-based position (even 0-based physical)
function GetIndentForLine(Editor: TSynEditBase; const Line: string;
Physical: boolean): Integer;
function GetDesiredIndentForLine
(Editor: TSynEditBase; const Lines: TSynEditStrings;
const ACaret: TSynEditCaret): Integer; override;
function GetDesiredIndentForLine
(Editor: TSynEditBase; const Lines: TSynEditStrings;
const ACaret: TSynEditCaret; out ReplaceIndent: Boolean;
out DesiredIndent: String): Integer; override;
published
property IndentType: TSynBeautifierIndentType read FIndentType write FIndentType;
end;
TSynCommentContineMode = (
sccNoPrefix, // May still do indent, if matched
sccPrefixAlways, // If the pattern did not match all will be done, except the indent AFTER the prefix (can not be detected)
sccPrefixMatch
//sccPrefixMatchFirst
//sccPrefixMatchPrev
//sccPrefixMatchPrevTwo // last 2 lines match
//sccPrefixMatchPrevTwoExact // last 2 lines match and same result for prefix
);
TSynCommentMatchLine = (
sclMatchFirst, // Match the first line of the comment to get substitutes for Prefix ($1)
sclMatchPrev // Match the previous line of the comment to get substitutes for Prefix ($1)
//sclMatchNestedFirst // For nested comments, first line of this nest.
);
TSynCommentMatchMode = (
// Which parts of the first line to match sclMatchFirst or sclMatchPrev (if prev = first)
scmMatchAfterOpening, // will not include (*,{,//. The ^ will match the first char after
scmMatchOpening, // will include (*,{,//. The ^ will match the ({/
scmMatchWholeLine, // Match the entire line
scmMatchAtAsterisk // AnsiComment only, will match the * of (*, but not the (
);
TSynCommentIndentFlag = (
// * For Matching lines (FCommentMode)
//
// * [], sciNone, sciAlignOpen
// [] (neither sciNone nor sciAlignOpen)
// indent is the same as for none comment lines
// that is indent from previous line
sciNone, // Does not Indent comment lines (Prefix may contain a fixed indent)
// sciNone overrides sciAlignOpen
sciAlignOpen, // Indent to real opening pos on first line, if comment does not start at BOL "Foo(); (*"
// Will force every new line back to Align.
// add-ons (combine with above)
sciAlignOpenOnce, // Force AlignOpen once for first line
// Can be combined sciNone or used without any of the above
sciAlignOpenSkipBOL, // Combine with any sciAlignOpen...,
// only align if the comment starts behind other text (is not at BOL)
// sciAdd...: if (only if) previous line had started with the opening token "(*" or "{".
// or if sciAlignOpen is set
// This is not done for "//", as Comment Extension will add a new "//"
// But Applies to sciNone
sciAddTokenLen, // add 1 or 2 spaces to indent (for the length of the token)
// in case of scmMatchAtAsterisk, 1 space is added. ("(" only)
sciAddPastTokenIndent, // Adds any indent found past the opening token "(*", "{" or "//".
// For "//" this is added after the nem "//", but before the prefix.
sciMatchOnlyTokenLen, // Apply the Above only if first line matches. (Only if sciAddTokenLen is specified)
sciMatchOnlyPastTokenIndent,
sciAlignOnlyTokenLen, // Apply the Above only if sciAlignOpen/sciAlignOpenOnce was used
sciAlignOnlyPastTokenIndent,
// TODO: sciAlignOpenOnce followed by [] (neither = default indent) should keep the extra align, if it still is on the correct column
// flag to ignore spaces, that are matched.
// flag to be smart, if not matched
sciApplyIndentForNoMatch // Apply above rules For NONE Matching lines (FCommentMode),
// includes FIndentFirstLineExtra
);
TSynCommentIndentFlags = set of TSynCommentIndentFlag;
TSynCommentExtendMode = (
sceNever, // Never Extend
sceAlways, // Always
sceSplitLine, // If the line was split (caret was not at EOL, when enter was pressed
sceMatching, // If the line matched (even if sccPrefixAlways or sccNoPrefix
sceMatchingSplitLine
);
TSynCommentType = (sctAnsi, sctBor, sctSlash);
// end mode
{ TSynBeautifierPascal }
TSynBeautifierPascal = class(TSynBeautifier)
private
FIndentMode: Array [TSynCommentType] of TSynCommentIndentFlags;
FIndentFirstLineExtra: Array [TSynCommentType] of String;
FIndentFirstLineMax: Array [TSynCommentType] of Integer;
FCommentMode: Array [TSynCommentType] of TSynCommentContineMode;
FMatchMode: Array [TSynCommentType] of TSynCommentMatchMode;
FMatchLine: Array [TSynCommentType] of TSynCommentMatchLine;
FMatch: Array [TSynCommentType] of String;
FPrefix: Array [TSynCommentType] of String;
FCommentIndent: Array [TSynCommentType] of TSynBeautifierIndentType;
FEolMatch: Array [TSynCommentType] of String;
FEolMode: Array [TSynCommentType] of TSynCommentContineMode;
FEolPostfix: Array [TSynCommentType] of String;
FEolSkipLongerLine: Array [TSynCommentType] of Boolean;
FExtendSlashCommentMode: TSynCommentExtendMode;
private
FPasHighlighter: TSynPasSyn;
FCaretAtEOL: Boolean;
FStringBreakAppend: String;
FStringBreakEnabled: Boolean;
FStringBreakPrefix: String;
FWorkLine: Integer; // 1 based
FWorkFoldType: TSynCommentType;
FGetLineAfterComment: Boolean;
FRegExprEngine: TRegExpr;
FCacheFoldEndLvlIdx, FCacheFoldEndLvl: Integer;
FCacheCommentLvlIdx, FCacheCommentLvl: Integer;
FCacheFoldTypeForIdx: Integer;
FCacheFoldType: TPascalCodeFoldBlockType;
FCacheFirstLineForIdx, FCacheFirstLine: Integer;
FCacheSlashColumnForIdx, FCacheSlashColumn: Integer;
FCacheCommentStartColForIdx, FCacheCommentStartCol: Integer;
FCacheLastHlTokenForIdx, FCacheLastHlTokenCol: Integer;
FCacheLastHlTokenKind: TtkTokenKind;
FCacheWorkFoldEndLvl: Integer;
FCacheWorkCommentLvl: Integer;
FCacheWorkFoldType: TPascalCodeFoldBlockType;
FCacheWorkFirstLine: Integer;
FCacheWorkSlashCol: Integer;
FCacheWorkCommentStartCol: Integer;
protected
function IsPasHighlighter: Boolean;
procedure InitCache;
procedure InitPasHighlighter;
// Generic Helpers
function GetFoldEndLevelForIdx(AIndex: Integer): Integer;
function GetFoldCommentLevelForIdx(AIndex: Integer): Integer;
function GetFoldTypeAtEndOfLineForIdx(AIndex: Integer): TPascalCodeFoldBlockType;
function GetFirstCommentLineForIdx(AIndex: Integer): Integer; // Result is 1 based
function GetSlashStartColumnForIdx(AIndex: Integer): Integer;
function GetLastHlTokenForIdx(AIndex: Integer; out APos: Integer): TtkTokenKind;
function GetCommentStartColForIdx(AIndex: Integer): Integer; // Returns Column (logic) for GetFirstCommentLineForIdx(AIndex)
// Values based on FWorkLine
function GetFoldEndLevel: Integer;
function GetFoldCommentLevel: Integer;
function GetFoldTypeAtEndOfLine: TPascalCodeFoldBlockType;
function GetFirstCommentLine: Integer; // Result is 1 based
function GetSlashStartColumn: Integer; // Acts on FWorkLine-1
function GetCommentStartCol: Integer; // Acts on GetFirstCommentLineForIdx(FWorkLine) / Logical Resulc
function GetMatchStartColForIdx(AIndex: Integer): Integer; // Res=1-based
function GetMatchStartPos(AIndex: Integer = -1; AForceFirst: Boolean = False): TPoint; // Res=1-based / AIndex-1 is only used for sclMatchPrev
protected
function GetLine(AnIndex: Integer): String; override;
procedure DoBeforeCommand(const ACaret: TSynEditCaret;
var Command: TSynEditorCommand); override;
procedure DoAfterCommand(const ACaret: TSynEditCaret;
var Command: TSynEditorCommand;
StartLinePos, EndLinePos: Integer); override;
procedure DoNewLineInString(AStringStartY, AStringStartX: Integer;
const ACaret: TSynEditCaret;
var Command: TSynEditorCommand;
StartLinePos, EndLinePos: Integer);
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
procedure Assign(Src: TPersistent); override;
// Retruns a 0-based position (even 0-based physical)
published
// *** coments with (* *)
(* AnsiIndentFirstLineMax:
* For comments that use any sciAlignOpen...
if AnsiIndentFirstLineMax is is set (none zero):
The indent will be limited to this value
AnsiIndentFirstLineExtra:
For comments that do NOT start at the BOL, an extra indent is added
on the 2nd line (except if sciAlignOpen is used (in Flags))
*)
property AnsiIndentMode: TSynCommentIndentFlags read FIndentMode[sctAnsi]
write FIndentMode[sctAnsi];
property AnsiIndentFirstLineMax: Integer read FIndentFirstLineMax[sctAnsi]
write FIndentFirstLineMax[sctAnsi];
property AnsiIndentFirstLineExtra: String read FIndentFirstLineExtra[sctAnsi]
write FIndentFirstLineExtra[sctAnsi];
(* match should start with ^, and leading spaces should not be in () "^\s?(\*\*?\*?)"
prefix can refer to $1 and should have spaces to indent after the match "$1 "
*)
property AnsiCommentMode: TSynCommentContineMode read FCommentMode[sctAnsi]
write FCommentMode[sctAnsi];
property AnsiMatch: String read FMatch[sctAnsi]
write FMatch[sctAnsi];
property AnsiPrefix : String read FPrefix[sctAnsi]
write FPrefix[sctAnsi];
property AnsiMatchMode: TSynCommentMatchMode read FMatchMode[sctAnsi]
write FMatchMode[sctAnsi];
property AnsiMatchLine: TSynCommentMatchLine read FMatchLine[sctAnsi]
write FMatchLine[sctAnsi];
property AnsiCommentIndent: TSynBeautifierIndentType read FCommentIndent[sctAnsi]
write FCommentIndent[sctAnsi];
// Add postfix at EOL
property AnsiEolMode: TSynCommentContineMode read FEolMode[sctAnsi]
write FEolMode[sctAnsi];
property AnsiEolPostfix: String read FEolPostfix[sctAnsi]
write FEolPostfix[sctAnsi];
property AnsiEolMatch: String read FEolMatch[sctAnsi]
write FEolMatch[sctAnsi];
property AnsiEolSkipLongerLine: Boolean read FEolSkipLongerLine[sctAnsi]
write FEolSkipLongerLine[sctAnsi];
// *** coments with { }
property BorIndentMode: TSynCommentIndentFlags read FIndentMode[sctBor]
write FIndentMode[sctBor];
property BorIndentFirstLineMax: Integer read FIndentFirstLineMax[sctBor]
write FIndentFirstLineMax[sctBor];
property BorIndentFirstLineExtra: String read FIndentFirstLineExtra[sctBor]
write FIndentFirstLineExtra[sctBor];
property BorCommentMode: TSynCommentContineMode read FCommentMode[sctBor]
write FCommentMode[sctBor];
property BorMatch: String read FMatch[sctBor]
write FMatch[sctBor];
property BorPrefix : String read FPrefix[sctBor]
write FPrefix[sctBor];
property BorMatchMode: TSynCommentMatchMode read FMatchMode[sctBor]
write FMatchMode[sctBor];
property BorMatchLine: TSynCommentMatchLine read FMatchLine[sctBor]
write FMatchLine[sctBor];
property BorCommentIndent: TSynBeautifierIndentType read FCommentIndent[sctBor]
write FCommentIndent[sctBor];
property BorEolMode: TSynCommentContineMode read FEolMode[sctBor]
write FEolMode[sctBor];
property BorEolPostfix : String read FEolPostfix[sctBor]
write FEolPostfix[sctBor];
property BorEolMatch: String read FEolMatch[sctBor]
write FEolMatch[sctBor];
property BorEolSkipLongerLine: Boolean read FEolSkipLongerLine[sctBor]
write FEolSkipLongerLine[sctBor];
// *** coments with //
// Continue only, if Extended
property ExtendSlashCommentMode: TSynCommentExtendMode read FExtendSlashCommentMode
write FExtendSlashCommentMode;
property SlashIndentMode: TSynCommentIndentFlags read FIndentMode[sctSlash]
write FIndentMode[sctSlash];
property SlashIndentFirstLineMax: Integer read FIndentFirstLineMax[sctSlash]
write FIndentFirstLineMax[sctSlash];
property SlashIndentFirstLineExtra: String read FIndentFirstLineExtra[sctSlash]
write FIndentFirstLineExtra[sctSlash];
property SlashCommentMode: TSynCommentContineMode read FCommentMode[sctSlash]
write FCommentMode[sctSlash];
property SlashMatch: String read FMatch[sctSlash]
write FMatch[sctSlash];
property SlashPrefix : String read FPrefix[sctSlash]
write FPrefix[sctSlash];
property SlashMatchMode: TSynCommentMatchMode read FMatchMode[sctSlash]
write FMatchMode[sctSlash];
property SlashMatchLine: TSynCommentMatchLine read FMatchLine[sctSlash]
write FMatchLine[sctSlash];
property SlashCommentIndent: TSynBeautifierIndentType read FCommentIndent[sctSlash]
write FCommentIndent[sctSlash];
property SlashEolMode: TSynCommentContineMode read FEolMode[sctSlash]
write FEolMode[sctSlash];
property SlashEolPostfix : String read FEolPostfix[sctSlash]
write FEolPostfix[sctSlash];
property SlashEolMatch: String read FEolMatch[sctSlash]
write FEolMatch[sctSlash];
property SlashEolSkipLongerLine: Boolean read FEolSkipLongerLine[sctSlash]
write FEolSkipLongerLine[sctSlash];
// String
property StringBreakEnabled: Boolean read FStringBreakEnabled write FStringBreakEnabled;
property StringBreakAppend: String read FStringBreakAppend write FStringBreakAppend;
property StringBreakPrefix: String read FStringBreakPrefix write FStringBreakPrefix;
end;
function dbgs(ACommentType: TSynCommentType): String; overload;
function dbgs(AContinueMode: TSynCommentContineMode): String; overload;
function dbgs(AMatchLine: TSynCommentMatchLine): String; overload;
function dbgs(AMatchMode: TSynCommentMatchMode): String; overload;
function dbgs(AIndentFlag: TSynCommentIndentFlag): String; overload;
function dbgs(AIndentFlags: TSynCommentIndentFlags): String; overload;
function dbgs(AExtendMode: TSynCommentExtendMode): String; overload;
implementation
uses SynEdit;
function dbgs(ACommentType: TSynCommentType): String;
begin
Result := ''; WriteStr(Result, ACommentType);
end;
function dbgs(AContinueMode: TSynCommentContineMode): String;
begin
Result := ''; WriteStr(Result, AContinueMode);
end;
function dbgs(AMatchLine: TSynCommentMatchLine): String;
begin
Result := ''; WriteStr(Result, AMatchLine);
end;
function dbgs(AMatchMode: TSynCommentMatchMode): String;
begin
Result := ''; WriteStr(Result, AMatchMode);
end;
function dbgs(AIndentFlag: TSynCommentIndentFlag): String;
begin
Result := ''; WriteStr(Result, AIndentFlag);
end;
function dbgs(AIndentFlags: TSynCommentIndentFlags): String;
var
i: TSynCommentIndentFlag;
begin
Result := '';
for i := low(TSynCommentIndentFlag) to high(TSynCommentIndentFlag) do
if i in AIndentFlags then
if Result = ''
then Result := dbgs(i)
else Result := Result + ',' + dbgs(i);
if Result <> '' then
Result := '[' + Result + ']';
end;
function dbgs(AExtendMode: TSynCommentExtendMode): String;
begin
Result := ''; WriteStr(Result, AExtendMode);
end;
{ TSynBeautifierPascal }
function TSynBeautifierPascal.IsPasHighlighter: Boolean;
begin
Result := (TSynEdit(FCurrentEditor).Highlighter <> nil) and
(TSynEdit(FCurrentEditor).Highlighter is TSynPasSyn);
end;
procedure TSynBeautifierPascal.InitCache;
begin
FCacheFoldEndLvlIdx := -1;
FCacheCommentLvlIdx := -1;
FCacheFoldTypeForIdx := -1;
FCacheFirstLineForIdx := -1;
FCacheSlashColumnForIdx := -1;
FCacheCommentStartColForIdx := -1;
FCacheWorkFoldEndLvl := -2;
FCacheWorkCommentLvl := -2;
FCacheWorkFoldType := cfbtIfThen; // dummy for not yet cached
FCacheWorkFirstLine := -2;
FCacheWorkSlashCol := -2;
FCacheWorkCommentStartCol := -2;
end;
procedure TSynBeautifierPascal.InitPasHighlighter;
begin
FPasHighlighter := TSynPasSyn(TSynEdit(FCurrentEditor).Highlighter);
FPasHighlighter.CurrentLines := FCurrentLines;
FPasHighlighter.ScanRanges;
end;
function TSynBeautifierPascal.GetFoldEndLevelForIdx(AIndex: Integer): Integer;
begin
Result := FCacheFoldEndLvl;
if AIndex = FCacheFoldEndLvlIdx then
exit;
FCacheFoldEndLvlIdx := AIndex;
FCacheFoldEndLvl := FPasHighlighter.FoldBlockEndLevel(AIndex, FOLDGROUP_PASCAL, [sfbIncludeDisabled]);
Result := FCacheFoldEndLvl;
end;
function TSynBeautifierPascal.GetFoldCommentLevelForIdx(AIndex: Integer): Integer;
var
tmp: Pointer;
Block: TPascalCodeFoldBlockType;
begin
Result := FCacheCommentLvl;
if AIndex = FCacheCommentLvlIdx then
exit;
FCacheCommentLvlIdx := AIndex;
FCacheCommentLvl := GetFoldEndLevelForIdx(AIndex) - 1;
while (FCacheCommentLvl > 0) do begin
FPasHighlighter.FoldBlockNestedTypes(AIndex , FCacheCommentLvl - 1,
tmp, FOLDGROUP_PASCAL, [sfbIncludeDisabled]);
Block:=TPascalCodeFoldBlockType(PtrUInt(tmp));
if not (Block in [cfbtAnsiComment, cfbtBorCommand, cfbtSlashComment]) then
break;
dec(FCacheCommentLvl);
end;
inc(FCacheCommentLvl);
Result := FCacheCommentLvl;
end;
function TSynBeautifierPascal.GetFoldTypeAtEndOfLineForIdx(AIndex: Integer): TPascalCodeFoldBlockType;
var
EndLevel: Integer;
tmp: Pointer;
begin
// TODO cfbtNestedComment
Result := FCacheFoldType;
if AIndex = FCacheFoldTypeForIdx then
exit;
FCacheFoldTypeForIdx := AIndex;
EndLevel := GetFoldEndLevelForIdx(AIndex);
if (EndLevel > 0) then
begin
if FPasHighlighter.FoldBlockNestedTypes(AIndex, EndLevel - 1,
tmp, FOLDGROUP_PASCAL, [sfbIncludeDisabled])
then
FCacheFoldType:=TPascalCodeFoldBlockType(PtrUInt(tmp))
else
FCacheFoldType := cfbtNone;
end;
while (FCacheFoldType = cfbtNestedComment) and (EndLevel > 1) do begin
dec(EndLevel);
if FPasHighlighter.FoldBlockNestedTypes(AIndex, EndLevel - 1,
tmp, FOLDGROUP_PASCAL, [sfbIncludeDisabled])
then
FCacheFoldType:=TPascalCodeFoldBlockType(PtrUInt(tmp))
else
FCacheFoldType := cfbtNone;
end;
Result := FCacheFoldType;
end;
function TSynBeautifierPascal.GetFirstCommentLineForIdx(AIndex: Integer): Integer;
var
FoldType: TPascalCodeFoldBlockType;
ANewIndex, EndLvl, CommentLvl: Integer;
begin
Result := FCacheFirstLine;
if AIndex = FCacheFirstLineForIdx then
exit;
ANewIndex := AIndex;
FoldType := GetFoldTypeAtEndOfLineForIdx(ANewIndex);
EndLvl := GetFoldEndLevelForIdx(ANewIndex);
//if (EndLvl = 0) or not(FoldType in [cfbtAnsiComment, cfbtBorCommand, cfbtSlashComment]) and
// (AIndex > 0) and (GetSlashStartColumnForIdx(AIndex - 1) > 0)
//then begin
// dec(ANewIndex);
// FoldType := GetFoldTypeAtEndOfLineForIdx(ANewIndex);
// EndLvl := GetFoldEndLevelForIdx(ANewIndex);
//end;
if (EndLvl = 0) or not(FoldType in [cfbtAnsiComment, cfbtBorCommand, cfbtSlashComment])
then begin
Result := ToPos(AIndex) - 1; // 1 based - the line above ANewIndex
// maybe the line above has a trailing comment
//if (AIndex <> ANewIndex) and (ANewIndex > 0) and (GetSlashStartColumnForIdx(ANewIndex-1) > 0) then
// Result := ToPos(ANewIndex) - 1;
exit;
end;
FCacheFirstLineForIdx := AIndex;
FCacheFirstLine := ToPos(ANewIndex) - 1;
CommentLvl := GetFoldCommentLevelForIdx(ANewIndex);
while (FCacheFirstLine > 0) do begin
if CommentLvl > FPasHighlighter.FoldBlockMinLevel(FCacheFirstLine-1, FOLDGROUP_PASCAL, [sfbIncludeDisabled]) then
break;
dec(FCacheFirstLine);
end;
if FoldType = cfbtSlashComment then begin
// maybe the line above has a trailing comment
if GetSlashStartColumnForIdx(ToIdx(FCacheFirstLine)) > 0 then
dec(FCacheFirstLine);
end;
Result := FCacheFirstLine;
//debugln(['FIRST LINE ', FCacheFirstLine]);
end;
function TSynBeautifierPascal.GetSlashStartColumnForIdx(AIndex: Integer): Integer;
var
Tk: TtkTokenKind;
begin
Result := FCacheSlashColumn;
if AIndex = FCacheSlashColumnForIdx then
exit;
FCacheSlashColumnForIdx := AIndex;
Tk := GetLastHlTokenForIdx(AIndex, FCacheSlashColumn);
if Tk <> SynHighlighterPas.tkComment then
FCacheSlashColumn := -1;
Result := FCacheSlashColumn;
end;
function TSynBeautifierPascal.GetLastHlTokenForIdx(AIndex: Integer; out
APos: Integer): TtkTokenKind;
begin
Result := FCacheLastHlTokenKind;
APos := FCacheLastHlTokenCol;
if AIndex = FCacheLastHlTokenForIdx then
exit;
FCacheSlashColumnForIdx := AIndex;
FCacheLastHlTokenKind := SynHighlighterPas.tkUnknown;
FCacheLastHlTokenCol := -1;
if (FCurrentLines[AIndex] <> '') then begin
FPasHighlighter.StartAtLineIndex(AIndex);
while not FPasHighlighter.GetEol do begin
FCacheLastHlTokenKind := TtkTokenKind(FPasHighlighter.GetTokenKind);
FCacheLastHlTokenCol := FPasHighlighter.GetTokenPos + 1;
FPasHighlighter.Next;
end;
end;
Result := FCacheLastHlTokenKind;
APos := FCacheLastHlTokenCol;
end;
function TSynBeautifierPascal.GetCommentStartColForIdx(AIndex: Integer): Integer;
var
nl: TLazSynFoldNodeInfoList;
i: Integer;
FoldCommentLvl: Integer;
begin
Result := FCacheCommentStartCol;
if AIndex = FCacheCommentStartColForIdx then
exit;
FCacheCommentStartColForIdx := AIndex;
if (GetFoldEndLevelForIdx(AIndex) = 0) or
not(GetFoldTypeAtEndOfLineForIdx(AIndex) in [cfbtAnsiComment, cfbtBorCommand, cfbtSlashComment])
then begin
// must be SlashComment
FCacheCommentStartCol := GetSlashStartColumnForIdx(AIndex - 1);
//FCacheCommentStartCol := GetSlashStartColumnForIdx(AIndex);
//if FCacheCommentStartCol > 0 then begin
// i := GetSlashStartColumnForIdx(AIndex - 1);
// if i > 0 then
// FCacheCommentStartCol := i;
//end;
Result := FCacheCommentStartCol;
//debugln(['FIRST COL prev-// ', FCacheCommentStartCol]);
exit;
end;
FCacheCommentStartCol := -1;
FoldCommentLvl := GetFoldCommentLevelForIdx(AIndex);
nl := FPasHighlighter.FoldNodeInfo[GetFirstCommentLineForIdx(AIndex) - 1];
nl.AddReference;
nl.ClearFilter;
nl.ActionFilter := [sfaOpen];
nl.GroupFilter := FOLDGROUP_PASCAL;
i := nl.Count - 1;
while i >= 0 do begin
if (not (sfaInvalid in nl[i].FoldAction)) and
(nl[i].NestLvlEnd = FoldCommentLvl)
then begin
FCacheCommentStartCol := nl[i].LogXStart + 1; // Highlighter pos are 0 based
break;
end;
dec(i);
end;
nl.ReleaseReference;
//debugln(['FIRST COL ', FCacheCommentStartCol]);
Result := FCacheCommentStartCol;
end;
function TSynBeautifierPascal.GetFoldEndLevel: Integer;
begin
Result := FCacheWorkFoldEndLvl;
if FCacheWorkFoldEndLvl > -2 then
exit;
FCacheWorkFoldEndLvl := GetFoldEndLevelForIdx(FWorkLine-1);
Result := FCacheWorkFoldEndLvl;
end;
function TSynBeautifierPascal.GetFoldCommentLevel: Integer;
begin
Result := FCacheWorkCommentLvl;
if FCacheWorkCommentLvl > -2 then
exit;
FCacheWorkCommentLvl := GetFoldCommentLevelForIdx(FWorkLine-1);
Result := FCacheWorkCommentLvl;
end;
function TSynBeautifierPascal.GetFoldTypeAtEndOfLine: TPascalCodeFoldBlockType;
begin
Result := FCacheWorkFoldType;
if FCacheWorkFoldType <> cfbtIfThen then
exit;
FCacheWorkFoldType := GetFoldTypeAtEndOfLineForIdx(FWorkLine-1);
Result := FCacheWorkFoldType;
end;
function TSynBeautifierPascal.GetFirstCommentLine: Integer;
begin
Result := FCacheWorkFirstLine;
if FCacheWorkFirstLine > -2 then
exit;
FCacheWorkFirstLine := GetFirstCommentLineForIdx(FWorkLine-1);
Result := FCacheWorkFirstLine;
end;
function TSynBeautifierPascal.GetSlashStartColumn: Integer;
begin
Result := FCacheWorkSlashCol;
if FCacheWorkSlashCol > -2 then
exit;
FCacheWorkSlashCol := GetSlashStartColumnForIdx(FWorkLine-2);
Result := FCacheWorkSlashCol;
end;
function TSynBeautifierPascal.GetCommentStartCol: Integer;
begin
Result := FCacheWorkCommentStartCol;
if FCacheWorkCommentStartCol > -2 then
exit;
FCacheWorkCommentStartCol := GetCommentStartColForIdx(FWorkLine-1);
Result := FCacheWorkCommentStartCol;
end;
function TSynBeautifierPascal.GetMatchStartColForIdx(AIndex: Integer): Integer;
begin
if ToPos(AIndex) = GetFirstCommentLine then begin
// Match on FirstLine
case FMatchMode[FWorkFoldType] of
scmMatchAfterOpening: begin
if FWorkFoldType = sctBor
then Result := GetCommentStartCol + 1
else Result := GetCommentStartCol + 2;
end;
scmMatchOpening: Result := GetCommentStartCol;
scmMatchWholeLine: Result := 1;
scmMatchAtAsterisk: Result := GetCommentStartCol + 1;
end;
end
else begin
// Match on prev, not first
case FMatchMode[FWorkFoldType] of
scmMatchAfterOpening, scmMatchOpening, scmMatchAtAsterisk:
Result := 1 + GetIndentForLine(nil, FCurrentLines[AIndex], False);
scmMatchWholeLine:
Result := 1;
end;
end;
end;
function TSynBeautifierPascal.GetMatchStartPos(AIndex: Integer; AForceFirst: Boolean): TPoint;
begin
if AIndex < 0 then
AIndex := ToIdx(FWorkLine);
if AForceFirst then
Result.Y := GetFirstCommentLine
else
case FMatchLine[FWorkFoldType] of
sclMatchFirst: Result.Y := GetFirstCommentLine;
sclMatchPrev: Result.Y := ToPos(AIndex)-1; // FWorkLine - 1
end;
Result.X := GetMatchStartColForIdx(ToIdx(Result.Y));
end;
function TSynBeautifierPascal.GetLine(AnIndex: Integer): String;
var
ReplacedPrefix: String;
Indent, PreFixPos: Integer;
r: Boolean;
p: TPoint;
begin
if not FGetLineAfterComment then begin
Result := inherited GetLine(AnIndex);
exit;
end;
// Need to cut of existing Prefix to find indent after prefix
if AnIndex < GetFirstCommentLine-1 then begin
Result := '';
//debugln(['GETLINE FROM (< First) ', AnIndex,' ''', FCurrentLines[AnIndex], ''' to empty']);
end;
// 1) Run the match for this line (match prev, or first)
// and see if we can find the replaced prefix in this line
if AnIndex > GetFirstCommentLine-1 then begin
p := GetMatchStartPos(AnIndex);
FRegExprEngine.InputString:= copy(FCurrentLines[ToIdx(p.y)], p.x, MaxInt);
FRegExprEngine.Expression := FMatch[FWorkFoldType];
if FRegExprEngine.ExecPos(1) then begin
ReplacedPrefix := FRegExprEngine.Substitute(FPrefix[FWorkFoldType]);
while (ReplacedPrefix <> '') and (ReplacedPrefix[1] in [#9, ' ']) do
delete(ReplacedPrefix, 1, 1);
while (ReplacedPrefix <> '') and (ReplacedPrefix[length(ReplacedPrefix)] in [#9, ' ']) do
delete(ReplacedPrefix, length(ReplacedPrefix), 1);
if ReplacedPrefix <> '' then begin
Result := FCurrentLines[AnIndex];
PreFixPos := pos(ReplacedPrefix, Result);
if (PreFixPos > 0) and (PreFixPos <= GetMatchStartColForIdx(AnIndex)) then begin
delete(Result, 1, PreFixPos - 1 + Length(ReplacedPrefix));
//debugln(['GETLINE FROM (1) ', AnIndex,' ''', FCurrentLines[AnIndex], ''' to ''', Result, '''']);
exit;
end;
end;
end;
end
else
p := Point(0,0);
// 2) See, if the current-1 line can be matched
r := False;
Result := FCurrentLines[AnIndex];
Indent := GetMatchStartColForIdx(AnIndex);
FRegExprEngine.InputString:= copy(FCurrentLines[AnIndex], Indent, MaxInt);
FRegExprEngine.Expression := FMatch[FWorkFoldType];
r := FRegExprEngine.ExecPos(1);
if (not r) and (Indent > 1) and
((p.y <> GetFirstCommentLine) or (FMatchMode[FWorkFoldType] = scmMatchWholeLine))
then begin
// try whole line
// TODO: only if not first, or if setting
FRegExprEngine.InputString := FCurrentLines[AnIndex];
r := FRegExprEngine.ExecPos(1);
if r then
Indent := 1;
end;
if (r) then begin
Indent := Indent + FRegExprEngine.MatchPos[0] - 1 + FRegExprEngine.MatchLen[0];
Result := FCurrentLines[AnIndex];
if (Indent <= Length(Result)) then
while (Indent > 1) and (Result[Indent] in [#9, ' ']) do
dec(Indent);
inc(Indent);
if Indent > 0 then begin
Result := copy(Result, Indent, MaxInt);
//debugln(['GETLINE FROM (2) ', AnIndex,' ''', FCurrentLines[AnIndex], ''' to ''', Result, '''']);
exit;
end;
end;
// 3) maybe try currest replace, if different from 1?
// Nothing found
Result := '';
//debugln(['GETLINE FROM (X) ', AnIndex,' ''', FCurrentLines[AnIndex], ''' to empty']);
end;
procedure TSynBeautifierPascal.DoBeforeCommand(const ACaret: TSynEditCaret;
var Command: TSynEditorCommand);
begin
FCaretAtEOL := ACaret.BytePos > Length(FCurrentLines[ToIdx(ACaret.LinePos)]);
FGetLineAfterComment := False;
inherited DoBeforeCommand(ACaret, Command);
end;
procedure TSynBeautifierPascal.DoAfterCommand(const ACaret: TSynEditCaret;
var Command: TSynEditorCommand; StartLinePos, EndLinePos: Integer);
var
WorkLine, PrevLineShlasCol: Integer;
ReplacedPrefix: String; // Each run matches only one Type
MatchResultIntern, MatchedBOLIntern: Array [Boolean] of Boolean; // Each run matches only one Type
function CheckMatch(AType: TSynCommentType; AFailOnNoPattern: Boolean = False;
AForceFirst: Boolean = False): Boolean;
var
p: TPoint;
begin
if (FMatch[AType] = '') and AFailOnNoPattern then begin
Result := False;
exit;
end;
if MatchedBOLIntern[AForceFirst] then begin
Result := MatchResultIntern[AForceFirst];
exit;
end;
p := GetMatchStartPos(-1, AForceFirst);
FRegExprEngine.InputString:= copy(FCurrentLines[ToIdx(p.y)], p.x, MaxInt);
FRegExprEngine.Expression := FMatch[AType];
if not FRegExprEngine.ExecPos(1) then begin
ReplacedPrefix := FRegExprEngine.Substitute(FPrefix[AType]);
MatchedBOLIntern[AForceFirst] := True;
MatchResultIntern[AForceFirst] := False;
Result := MatchResultIntern[AForceFirst];
exit;
end;
ReplacedPrefix := FRegExprEngine.Substitute(FPrefix[AType]);
MatchedBOLIntern[AForceFirst] := True;
MatchResultIntern[AForceFirst] := True;
Result := MatchResultIntern[AForceFirst];
end;
function IsFoldTypeEnabled(AType: TSynCommentType): Boolean;
begin
Result := ( ( (AType <> sctSlash) or
( ((not FCaretAtEOL) and (FExtendSlashCommentMode <> sceNever)) or
((FCaretAtEOL) and not(FExtendSlashCommentMode in [sceNever, sceSplitLine, sceMatchingSplitLine]))
)
) and
( (FIndentMode[AType] <> []) or
(FCommentMode[AType] <> sccNoPrefix) or
(FEolMode[AType] <> sccNoPrefix)
))
end;
var
Indent, dummy: Integer;
s: String;
FoldTyp: TSynCommentType;
AnyEnabled: Boolean;
ExtendSlash, BeSmart, Matching: Boolean;
PreviousIsFirst, CommentStartsAtBOL, DidAlignOpen: Boolean;
IndentTypeBackup: TSynBeautifierIndentType;
begin
if (EndLinePos < 1) or
((Command <> ecLineBreak) and (Command <> ecInsertLine)) or
(not IsPasHighlighter)
then begin
inherited DoAfterCommand(ACaret, Command, StartLinePos, EndLinePos);
exit;
end;
AnyEnabled := False;
for FoldTyp := low(TSynCommentType) to high(TSynCommentType) do
AnyEnabled := AnyEnabled or IsFoldTypeEnabled(FoldTyp);
if (not AnyEnabled) and (not FStringBreakEnabled) then begin
inherited DoAfterCommand(ACaret, Command, StartLinePos, EndLinePos);
exit;
end;
InitCache;
InitPasHighlighter;
FGetLineAfterComment := False;
MatchedBOLIntern[True] := False;
MatchedBOLIntern[False] := False;
PrevLineShlasCol := -1;
dummy := 0;
if (Command = ecLineBreak)
then WorkLine := ACaret.LinePos
else WorkLine := ACaret.LinePos + 1;
FWorkLine := WorkLine;
// Find Foldtype
case GetFoldTypeAtEndOfLine of
cfbtAnsiComment: FoldTyp := sctAnsi;
cfbtBorCommand: FoldTyp := sctBor;
cfbtSlashComment: FoldTyp := sctSlash;
else
begin
if (FCurrentLines[ToIdx(WorkLine)-1] <> '') and
(FExtendSlashCommentMode <> sceNever) and
( (not FCaretAtEOL) or not(FExtendSlashCommentMode in [sceSplitLine, sceMatchingSplitLine]) )
then begin
PrevLineShlasCol := GetSlashStartColumn;
if PrevLineShlasCol > 0 then
FoldTyp := sctSlash;
end;
if PrevLineShlasCol < 0 then begin
if (FCurrentLines[ToIdx(WorkLine)-1] <> '') and
(GetLastHlTokenForIdx(ToIdx(WorkLine)-1, dummy) = SynHighlighterPas.tkString)
then
DoNewLineInString(WorkLine - 1, dummy, ACaret, Command, StartLinePos, EndLinePos)
else
inherited DoAfterCommand(ACaret, Command, StartLinePos, EndLinePos);
exit;
end;
end;
end;
if not IsFoldTypeEnabled(FoldTyp) then begin
inherited DoAfterCommand(ACaret, Command, StartLinePos, EndLinePos);
exit;
end;
FWorkFoldType := FoldTyp;
// Check if we need extend
ExtendSlash := False;
if (FoldTyp = sctSlash) and (ACaret.OldLineBytePos.x > GetSlashStartColumn+2) then begin
// Check if extension is needed
case FExtendSlashCommentMode of
sceAlways: ExtendSlash := True;
sceSplitLine: ExtendSlash := not FCaretAtEOL;
sceMatching: ExtendSlash := CheckMatch(FoldTyp);
sceMatchingSplitLine: ExtendSlash := CheckMatch(FoldTyp) and (not FCaretAtEOL);
end;
if not ExtendSlash then begin
inherited DoAfterCommand(ACaret, Command, StartLinePos, EndLinePos);
exit;
end;
end;
// Indent
Matching := CheckMatch(FoldTyp);
PreviousIsFirst := (GetFirstCommentLine = WorkLine -1);
DidAlignOpen := False;
CommentStartsAtBOL := True;
if PreviousIsFirst then
CommentStartsAtBOL := GetCommentStartCol = 1 + GetIndentForLine(nil, FCurrentLines[ToIdx(GetFirstCommentLine)], False);
// Aply indent before prefix
if Matching or (FCommentMode[FoldTyp] = sccPrefixAlways) or (sciApplyIndentForNoMatch in FIndentMode[FoldTyp])
then begin
IndentTypeBackup := IndentType;
try
if IndentType = sbitPositionCaret then
IndentType := sbitSpace;
if IndentType = sbitConvertToTabOnly then
IndentType := sbitConvertToTabSpace;
// Align with open
if ( (FIndentMode[FoldTyp] * [sciNone, sciAlignOpen] = [sciAlignOpen]) or
( (sciAlignOpenOnce in FIndentMode[FoldTyp]) and PreviousIsFirst )
) and
( (not (sciAlignOpenSkipBOL in FIndentMode[FoldTyp])) or (not CommentStartsAtBOL) )
then begin
Indent := FCurrentLines.LogicalToPhysicalCol(FCurrentLines[ToIdx(GetFirstCommentLine)],
ToIdx(GetFirstCommentLine),
GetCommentStartCol-1);
if FIndentFirstLineMax[FoldTyp] > 0
then Indent := Min(Indent, FIndentFirstLineMax[FoldTyp]);
s := GetCharMix(WorkLine, Indent, dummy);
FLogicalIndentLen := length(s);
FCurrentLines.EditInsert(1, WorkLine, s);
DidAlignOpen := True;
end
else
// Do not align with open, but check MaxIndent
if PreviousIsFirst and (FIndentFirstLineMax[FoldTyp] > 0) and
(FIndentMode[FoldTyp] * [sciNone] = []) then begin
Indent := GetIndentForLine(nil, FCurrentLines[ToIdx(WorkLine-1)], False);
Indent := Min(Indent, FIndentFirstLineMax[FoldTyp]);
s := GetCharMix(WorkLine, Indent, dummy);
FLogicalIndentLen := length(s);
FCurrentLines.EditInsert(1, WorkLine, s);
end
else
if (sciNone in FIndentMode[FoldTyp]) then begin
// No indent
end
else
begin
inherited DoAfterCommand(ACaret, Command, StartLinePos, EndLinePos);
end;
finally
IndentType := IndentTypeBackup;
end;
// AnsiIndentFirstLineExtra
if PreviousIsFirst and (not CommentStartsAtBOL) and (not DidAlignOpen) then begin
FCurrentLines.EditInsert(1 + FLogicalIndentLen, WorkLine, FIndentFirstLineExtra[FoldTyp]);
FLogicalIndentLen := FLogicalIndentLen + length(FIndentFirstLineExtra[FoldTyp]);
end;
end
else
inherited DoAfterCommand(ACaret, Command, StartLinePos, EndLinePos);
Indent := 0; // Extra Indent
BeSmart := (PreviousIsFirst or (sciAlignOpen in FIndentMode[FoldTyp])) and
(Matching or ExtendSlash or (sciApplyIndentForNoMatch in FIndentMode[FoldTyp]) or
(FCommentMode[FoldTyp] = sccPrefixAlways) );
// sciAddTokenLen -- Spaces for (* or {
if BeSmart and
( (sciAddTokenLen in FIndentMode[FoldTyp]) and
( (not(sciMatchOnlyTokenLen in FIndentMode[FoldTyp])) or CheckMatch(FoldTyp, False, True) ) and
( (not(sciAlignOnlyTokenLen in FIndentMode[FoldTyp])) or DidAlignOpen )
)
then begin
case FoldTyp of
sctAnsi: if FMatchMode[FoldTyp] = scmMatchAtAsterisk
then Indent := 1
else Indent := 2;
sctBor: Indent := 1;
sctSlash: if ExtendSlash
then Indent := 0 // do the slashes
else Indent := 2;
end;
end;
// sciAddPastTokenIndent -- Spaces from after (* or { (to go before prefix e.g " { * foo")
if BeSmart and
( (sciAddPastTokenIndent in FIndentMode[FoldTyp]) and
( (not(sciMatchOnlyPastTokenIndent in FIndentMode[FoldTyp])) or CheckMatch(FoldTyp, False, True) ) and
( (not(sciAlignOnlyPastTokenIndent in FIndentMode[FoldTyp])) or DidAlignOpen )
) and
(GetCommentStartCol > 0) // foundStartCol
then begin
case FoldTyp of
// ignores scmMatchAtAsterisk
sctAnsi: s := copy(FCurrentLines[ToIdx(GetFirstCommentLine)], GetCommentStartCol+2, MaxInt);
sctBor: s := copy(FCurrentLines[ToIdx(GetFirstCommentLine)], GetCommentStartCol+1, MaxInt);
sctSlash: s := copy(FCurrentLines[ToIdx(GetFirstCommentLine)], GetCommentStartCol+2, MaxInt);
end;
Indent := Indent + GetIndentForLine(nil, s, False);
end;
// Extend //
if ExtendSlash then begin
FCurrentLines.EditInsert(1 + FLogicalIndentLen, WorkLine, '//');
FLogicalIndentLen := FLogicalIndentLen + 2;
end;
if (Indent > 0) then begin
FCurrentLines.EditInsert(1 + FLogicalIndentLen, WorkLine, StringOfChar(' ', Indent ));
FLogicalIndentLen := FLogicalIndentLen + Indent;
end;
// Apply prefix
if (FCommentMode[FoldTyp] = sccPrefixAlways) or
((FCommentMode[FoldTyp] = sccPrefixMatch) and Matching)
then begin
FCurrentLines.EditInsert(1 + FLogicalIndentLen, WorkLine, ReplacedPrefix);
FLogicalIndentLen := FLogicalIndentLen + length(ReplacedPrefix);
// Post prefix indent
FGetLineAfterComment := True;
try
GetIndentInfo(WorkLine, s, Indent, GetFirstCommentLine);
if s <> '' then begin
FCurrentLines.EditInsert(1 + FLogicalIndentLen, WorkLine, s);
FLogicalIndentLen := FLogicalIndentLen + length(s); // logical (Indent is phisical)
end
else
FLogicalIndentLen := FLogicalIndentLen + Indent; // maybe position caret
finally
FGetLineAfterComment := False;
end;
end;
if (Command = ecLineBreak) then begin
ACaret.IncForcePastEOL;
ACaret.BytePos := 1 + FLogicalIndentLen;
ACaret.DecForcePastEOL;
end;
end;
procedure TSynBeautifierPascal.DoNewLineInString(AStringStartY, AStringStartX: Integer;
const ACaret: TSynEditCaret; var Command: TSynEditorCommand; StartLinePos,
EndLinePos: Integer);
var
s: String;
WorkLine: Integer;
f: Boolean;
begin
inherited DoAfterCommand(ACaret, Command, StartLinePos, EndLinePos);
if not FStringBreakEnabled then
exit;
if (Command = ecLineBreak)
then WorkLine := ACaret.LinePos - 1
else WorkLine := ACaret.LinePos;
s := FCurrentLines[ToIdx(WorkLine)];
if (AStringStartX < 1) or (AStringStartX > Length(s)) or (s[AStringStartX] <> '''') then
exit;
f := False;
while AStringStartX <= length(s) do begin
if (s[AStringStartX] = '''') then f := not f;
inc(AStringStartX);
end;
if not f then exit;
ACaret.IncForcePastEOL;
FCurrentLines.EditInsert(1 + length(s), WorkLine, '''' + FStringBreakAppend);
//if Command = ecInsertLine then
// ACaret.BytePos := ACaret.BytePos + Length(FStringBreakAppend) + 1;
FCurrentLines.EditInsert(1 + FLogicalIndentLen, WorkLine + 1, FStringBreakPrefix + '''');
if (Command = ecLineBreak) then
ACaret.BytePos := ACaret.BytePos + Length(FStringBreakPrefix) + 1;
ACaret.DecForcePastEOL;
end;
constructor TSynBeautifierPascal.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
FRegExprEngine := TRegExpr.Create;
FRegExprEngine.ModifierI := True;
FIndentMode[sctAnsi] := [sciAddTokenLen, sciAddPastTokenIndent];
FIndentFirstLineExtra[sctAnsi] := '';
FIndentFirstLineMax[sctAnsi] := 0;
FCommentMode[sctAnsi] := sccPrefixMatch;
FMatch[sctAnsi] := '^ ?(\*)';
FMatchMode[sctAnsi] := scmMatchAfterOpening;
FMatchLine[sctAnsi] := sclMatchPrev;
FPrefix[sctAnsi] := '$1';
FEolMatch[sctAnsi] := '';
FEolMode[sctAnsi] := sccNoPrefix;
FEolPostfix[sctAnsi] := '';
FEolSkipLongerLine[sctAnsi] := False;
FIndentMode[sctBor] := [sciAddTokenLen, sciAddPastTokenIndent];
FIndentFirstLineExtra[sctBor] := '';
FIndentFirstLineMax[sctBor] := 0;
FCommentMode[sctBor] := sccPrefixMatch;
FMatch[sctBor] := '^ ?(\*)';
FMatchMode[sctBor] := scmMatchAfterOpening;
FMatchLine[sctBor] := sclMatchPrev;
FPrefix[sctBor] := '$1';
FEolMatch[sctBor] := '';
FEolMode[sctBor] := sccNoPrefix;
FEolPostfix[sctBor] := '';
FEolSkipLongerLine[sctBor] := False;
FExtendSlashCommentMode := sceNever;
FIndentMode[sctSlash] := [];
FIndentFirstLineExtra[sctSlash] := '';
FIndentFirstLineMax[sctSlash] := 0;
FCommentMode[sctSlash] := sccPrefixMatch;
FMatch[sctSlash] := '^ ?(\*)';
FMatchMode[sctSlash] := scmMatchAfterOpening;
FMatchLine[sctSlash] := sclMatchPrev;
FPrefix[sctSlash] := '$1';
FEolMatch[sctSlash] := '';
FEolMode[sctSlash] := sccNoPrefix;
FEolPostfix[sctSlash] := '';
FEolSkipLongerLine[sctSlash] := False;
FStringBreakEnabled := False;
FStringBreakAppend := ' +';
FStringBreakPrefix := '';
end;
destructor TSynBeautifierPascal.Destroy;
begin
inherited Destroy;
FreeAndNil(FRegExprEngine);
end;
procedure TSynBeautifierPascal.Assign(Src: TPersistent);
var
i: TSynCommentType;
begin
inherited Assign(Src);
if not(Src is TSynBeautifierPascal) then exit;
FExtendSlashCommentMode := TSynBeautifierPascal(Src).FExtendSlashCommentMode;
for i := low(TSynCommentType) to high(TSynCommentType) do begin
FIndentMode[i] := TSynBeautifierPascal(Src).FIndentMode[i];
FIndentFirstLineExtra[i] := TSynBeautifierPascal(Src).FIndentFirstLineExtra[i];
FIndentFirstLineMax[i] := TSynBeautifierPascal(Src).FIndentFirstLineMax[i];
FCommentMode[i] := TSynBeautifierPascal(Src).FCommentMode[i];
FMatch[i] := TSynBeautifierPascal(Src).FMatch[i];
FMatchMode[i] := TSynBeautifierPascal(Src).FMatchMode[i];
FMatchLine[i] := TSynBeautifierPascal(Src).FMatchLine[i];
FPrefix[i] := TSynBeautifierPascal(Src).FPrefix[i];
FEolMatch[i] := TSynBeautifierPascal(Src).FEolMatch[i];
FEolMode[i] := TSynBeautifierPascal(Src).FEolMode[i];
FEolPostfix[i] := TSynBeautifierPascal(Src).FEolPostfix[i];
FEolSkipLongerLine[i] := TSynBeautifierPascal(Src).FEolSkipLongerLine[i];
end;
FStringBreakAppend := TSynBeautifierPascal(Src).FStringBreakAppend;
FStringBreakEnabled := TSynBeautifierPascal(Src).FStringBreakEnabled;
FStringBreakPrefix := TSynBeautifierPascal(Src).FStringBreakPrefix;
end;
{ TSynCustomBeautifier }
procedure TSynCustomBeautifier.Assign(Src: TPersistent);
begin
if Src is TSynCustomBeautifier then begin
FCurrentEditor := TSynCustomBeautifier(Src).FCurrentEditor;
FCurrentLines := TSynCustomBeautifier(Src).FCurrentLines;
FOnGetDesiredIndent := TSynCustomBeautifier(Src).FOnGetDesiredIndent;
FAutoIndent := TSynCustomBeautifier(Src).FAutoIndent;
end
else
inherited;
end;
function TSynCustomBeautifier.GetCopy: TSynCustomBeautifier;
begin
// Since all synedits share one beautifier, create a temp instance.
// Todo: have individual beautifiers
Result := TSynCustomBeautifierClass(self.ClassType).Create(nil);
Result.assign(self);
end;
procedure TSynCustomBeautifier.BeforeCommand(const Editor: TSynEditBase;
const Lines: TSynEditStrings; const ACaret: TSynEditCaret;
var Command: TSynEditorCommand; InitialCmd: TSynEditorCommand);
begin
// Must be called on GetCopy
// Todo: have individual beautifiers
FCurrentEditor := Editor;
FCurrentLines := Lines;
DoBeforeCommand(ACaret, Command);
end;
procedure TSynCustomBeautifier.AfterCommand(const Editor: TSynEditBase;
const Lines: TSynEditStrings; const ACaret: TSynEditCaret;
var Command: TSynEditorCommand; InitialCmd: TSynEditorCommand;
StartLinePos, EndLinePos: Integer);
begin
// Must be called on GetCopy
// Todo: have individual beautifiers
FCurrentEditor := Editor;
FCurrentLines := Lines;
DoAfterCommand(ACaret, Command, StartLinePos, EndLinePos);
end;
{ TSynBeautifier }
procedure TSynBeautifier.Assign(Src: TPersistent);
begin
inherited Assign(Src);
if Src is TSynBeautifier then begin
FIndentType := TSynBeautifier(Src).FIndentType;
FCurrentEditor := TSynBeautifier(Src).FCurrentEditor;
FCurrentLines := TSynBeautifier(Src).FCurrentLines;
end;
end;
function TSynBeautifier.GetLine(AnIndex: Integer): String;
begin
Result := FCurrentLines[AnIndex];
end;
procedure TSynBeautifier.GetIndentInfo(const LinePos: Integer; out ACharMix: String; out
AnIndent: Integer; AStopScanAtLine: Integer);
var
b: Integer;
begin
ACharMix := '';
if (GetLine(LinePos-2) = '') and (GetLine(LinePos-1) <> '') then
AnIndent := 0
else
AnIndent := GetIndent(LinePos, b, AStopScanAtLine);
if AnIndent > 0 then begin
ACharMix := GetCharMix(LinePos, AnIndent, b, True);
if (FIndentType = sbitPositionCaret) and (GetLine(LinePos-1) = '') then
ACharMix := '';
end;
end;
procedure TSynBeautifier.DoBeforeCommand(const ACaret: TSynEditCaret;
var Command: TSynEditorCommand);
var
x: Integer;
begin
if (Command = ecDeleteLastChar) and
(FAutoIndent) and
(ACaret.CharPos > 1) and
(not TCustomSynEdit(FCurrentEditor).ReadOnly) and
( (not TCustomSynEdit(FCurrentEditor).SelAvail) or
(eoPersistentBlock in TCustomSynEdit(FCurrentEditor).Options2) ) and
(GetIndentForLine(FCurrentEditor, ACaret.LineText, True) = ACaret.CharPos - 1)
then begin
FCurrentLines.UndoList.CurrentReason := ecSmartUnindent;
UnIndentLine(ACaret, x);
ACaret.CharPos := x;
Command := ecNone;
end;
end;
procedure TSynBeautifier.DoAfterCommand(const ACaret: TSynEditCaret;
var Command: TSynEditorCommand; StartLinePos, EndLinePos: Integer);
var
y, Indent: Integer;
s: String;
begin
FLogicalIndentLen := 0;
if EndLinePos < 1 then
exit;
if assigned(FOnGetDesiredIndent) and
FOnGetDesiredIndent(self, FCurrentEditor, ACaret.LineBytePos,
ACaret.OldLineBytePos, StartLinePos, EndLinePos, Command,
@ApplyIndent)
then
exit;
if ((Command = ecLineBreak) or (Command = ecInsertLine)) and FAutoIndent then begin
if (Command = ecLineBreak) then
y := ACaret.LinePos
else
y := ACaret.LinePos + 1;
GetIndentInfo(y, s, Indent);
if s <> '' then begin;
FCurrentLines.EditInsert(1, y, s);
FLogicalIndentLen := length(s);
end;
if (Command = ecLineBreak) then begin
ACaret.IncForcePastEOL;
ACaret.CharPos := Indent + 1;
ACaret.DecForcePastEOL;
end;
end;
end;
function TSynBeautifier.UnIndentLine(const ACaret: TSynEditCaret;
out CaretNewX: Integer): Boolean;
var
SpaceCount1, SpaceCount2: Integer;
BackCounter, LogSpacePos, FillSpace: Integer;
LogCaret: TPoint;
Line, Temp: String;
begin
Line := ACaret.LineText;
SpaceCount1 := GetIndentForLine(FCurrentEditor, Line, true); // physical, desired pos
SpaceCount2 := 0;
if (SpaceCount1 > 0) then begin
BackCounter := ACaret.LinePos - 2;
while BackCounter >= 0 do begin
Temp := FCurrentLines[BackCounter];
SpaceCount2 := GetIndentForLine(FCurrentEditor, Temp, true);
if (SpaceCount2 < SpaceCount1) and (temp <> '') then
break;
Dec(BackCounter);
end;
end;
if SpaceCount2 >= SpaceCount1 then
SpaceCount2 := 0;
// remove visible spaces
LogSpacePos := FCurrentLines.PhysicalToLogicalCol(Line, ACaret.LinePos-1, SpaceCount2 + 1);
FillSpace := SpaceCount2 + 1 - FCurrentLines.LogicalToPhysicalCol(Line, ACaret.LinePos-1, LogSpacePos);
LogCaret := ACaret.LineBytePos;
CaretNewX := SpaceCount2 + 1;
FCurrentLines.EditDelete(LogSpacePos, ACaret.LinePos, LogCaret.X - LogSpacePos);
if FillSpace > 0 then
FCurrentLines.EditInsert(LogSpacePos, ACaret.LinePos, StringOfChar(' ', FillSpace));
Result := True;
end;
function TSynBeautifier.GetIndent(const LinePos: Integer; out BasedOnLine: Integer;
AStopScanAtLine: Integer): Integer;
var
Temp: string;
begin
if AStopScanAtLine > 0 then
dec(AStopScanAtLine); // Convert to index
BasedOnLine := LinePos - 1; // Convert to index
while (BasedOnLine > AStopScanAtLine) do begin
dec(BasedOnLine);
Temp := GetLine(BasedOnLine);
if Temp <> '' then begin
Result := GetIndentForLine(FCurrentEditor, Temp, True);
exit;
end;
end;
Result := 0;
//BasedOnLine := LinePos;
//Result := GetIndentForLine(FCurrentEditor, GetLine(BasedOnLine), True);
end;
function TSynBeautifier.AdjustCharMix(DesiredIndent: Integer; CharMix, AppendMix: String): String;
var
i: Integer;
CurLen: Integer;
begin
CurLen := FCurrentLines.LogicalToPhysicalCol(CharMix, -1, length(CharMix)+1) - 1; // TODO: Need the real index of the line
if AppendMix <> '' then begin
while CurLen < DesiredIndent do begin
CharMix := CharMix + AppendMix;
CurLen := FCurrentLines.LogicalToPhysicalCol(CharMix, -1, length(CharMix)+1) - 1; // TODO: Need the real index of the line
end
end;
i := length(CharMix);
while CurLen > DesiredIndent do begin
Dec(i);
CurLen := FCurrentLines.LogicalToPhysicalCol(CharMix, -1, i+1) - 1; // TODO: Need the real index of the line
end;
CharMix := copy(CharMix, 1, i) + StringOfChar(' ', DesiredIndent - CurLen);
Result := CharMix;
end;
function TSynBeautifier.CreateTabSpaceMix(var DesiredIndent: Integer;
OnlyTabs: Boolean): String;
var
CurLen: Integer;
begin
CurLen := 0;
Result := '';
while CurLen < DesiredIndent do begin
Result := Result + #9;
CurLen := FCurrentLines.LogicalToPhysicalCol(Result, -1, length(Result)+1) - 1; // TODO: Need the real index of the line
end;
if CurLen = DesiredIndent then
exit;
Delete(Result, Length(Result), 1);
if OnlyTabs then begin
CurLen := FCurrentLines.LogicalToPhysicalCol(Result, -1, length(Result)+1) - 1; // TODO: Need the real index of the line
DesiredIndent := CurLen;
exit;
end;
repeat
Result := Result + ' ';
CurLen := FCurrentLines.LogicalToPhysicalCol(Result, -1, length(Result)+1) - 1; // TODO: Need the real index of the line
until CurLen >= DesiredIndent;
end;
function TSynBeautifier.GetCharMix(const LinePos: Integer; var Indent: Integer;
var IndentCharsFromLinePos: Integer; ModifyIndent: Boolean): String;
var
Temp, KnownMix, BasedMix: string;
KnownPhysLen, PhysLen: Integer;
BackCounter: LongInt;
OrigIndent: Integer;
begin
OrigIndent := Indent;
case FIndentType of
sbitSpace, sbitPositionCaret:
begin
IndentCharsFromLinePos := 0;
Result := StringOfChar(' ', Indent);
if not ModifyIndent then Indent := OrigIndent;
exit;
end;
sbitConvertToTabSpace:
begin
IndentCharsFromLinePos := 0;
Result := CreateTabSpaceMix(Indent, False);
exit;
if not ModifyIndent then Indent := OrigIndent;
end;
sbitConvertToTabOnly:
begin
IndentCharsFromLinePos := 0;
Result := CreateTabSpaceMix(Indent, True);
if not ModifyIndent then Indent := OrigIndent;
exit;
end;
end;
if (IndentCharsFromLinePos > 0) and (IndentCharsFromLinePos <= FCurrentLines.Count) then
begin
Temp := GetLine(IndentCharsFromLinePos);
KnownMix := copy(Temp, 1, GetIndentForLine(FCurrentEditor, Temp, False));
end
else
KnownMix := '';
BasedMix := KnownMix;
KnownPhysLen := GetIndentForLine(FCurrentEditor, KnownMix, True);
BackCounter := LinePos;
while (BackCounter > 0) and (KnownPhysLen < Indent) do begin
dec(BackCounter);
Temp := GetLine(BackCounter);
if Temp <> '' then begin
Temp := copy(Temp, 1, GetIndentForLine(FCurrentEditor, Temp, False));
PhysLen := GetIndentForLine(FCurrentEditor, Temp, True);
if (PhysLen > KnownPhysLen) and (copy(temp, 1, length(BasedMix)) = BasedMix) then
begin
KnownMix := Temp;
KnownPhysLen := PhysLen;
IndentCharsFromLinePos := BackCounter + 1;
end;
end;
end;
Result := AdjustCharMix(Indent, KnownMix, '');
if not ModifyIndent then Indent := OrigIndent;
end;
procedure TSynBeautifier.ApplyIndent(LinePos: Integer;
Indent: Integer; RelativeToLinePos: Integer; IndentChars: String = '';
IndentCharsFromLinePos: Integer = 0);
var
CharMix: String;
i: Integer;
begin
if (LinePos < 1) or (LinePos > FCurrentEditor.Lines.Count) then
exit;
// calculate the final indent needed
if (RelativeToLinePos > 0) and (RelativeToLinePos <= FCurrentEditor.Lines.Count) then
Indent := Indent + GetIndentForLine(FCurrentEditor, GetLine(RelativeToLinePos-1), True);
if Indent< 0 then
Indent := 0;
// Calculate the charmix
CharMix := '';
if Indent > 0 then begin
if (IndentCharsFromLinePos > 0) and (IndentCharsFromLinePos <= FCurrentEditor.Lines.Count) then begin
CharMix := GetLine(IndentCharsFromLinePos-1);
i := GetIndentForLine(FCurrentEditor, CharMix, False);
CharMix := AdjustCharMix(Indent, copy(CharMix, 1, i), IndentChars);
end
else if IndentChars <> '' then begin
CharMix := AdjustCharMix(Indent, '', IndentChars);
end
else begin
i := LinePos;
CharMix := GetCharMix(LinePos, Indent, i);
end;
end;
{$IFDEF VerboseIndenter}
DebugLn(['TSynBeautifier.ApplyIndent IndentChars="',dbgstr(IndentChars),' Indent=',Indent]);
{$ENDIF}
i := GetIndentForLine(FCurrentEditor, GetLine(LinePos-1), False);
FCurrentLines.EditDelete(1, LinePos, i);
if (CharMix <> '') and not((FIndentType = sbitPositionCaret) and (GetLine(LinePos-1) = '')) then
FCurrentLines.EditInsert(1, LinePos, CharMix);
FLogicalIndentLen := length(CharMix);
{$IFDEF VerboseIndenter}
DebugLn(['TSynBeautifier.ApplyIndent Line="',dbgstr(FCurrentLines.ExpandedStrings[LinePos-1]),'"']);
{$ENDIF}
end;
function TSynBeautifier.GetIndentForLine(Editor: TSynEditBase; const Line: string; Physical: boolean): Integer;
var
p: PChar;
begin
p := PChar(Line);
if Assigned(p) then begin
Result := 0;
while p^ in [#1..#32] do begin
Inc(p);
Inc(Result);
end;
if Physical and (Result>0) then
Result := FCurrentLines.LogicalToPhysicalCol(Line, -1, Result+1)-1; // TODO, Need the real index of the line
end else
Result := 0;
end;
function TSynBeautifier.GetDesiredIndentForLine(Editor: TSynEditBase;
const Lines: TSynEditStrings; const ACaret: TSynEditCaret;
out ReplaceIndent: Boolean; out DesiredIndent: String): Integer;
var
BackCounter, PhysLen: Integer;
Temp: string;
FoundLine: LongInt;
begin
Result := 1;
FCurrentLines := Lines; // for GetCurrentIndent
BackCounter := ACaret.LinePos - 1;
if BackCounter > 0 then
repeat
Dec(BackCounter);
Temp := Lines[BackCounter];
Result := GetIndentForLine(Editor, Temp, True) + 1; // Physical
until (BackCounter = 0) or (Temp <> '');
FoundLine := BackCounter + 1;
ReplaceIndent := False;
//if assigned(FOnGetDesiredIndent) then
// FOnGetDesiredIndent(Editor, ACaret.LineBytePos, ACaret.LinePos, Result,
// FoundLine, ReplaceIndent);
//if Result < 0 then exit;
//if FoundLine > 0 then
// Temp := Lines[FoundLine-1]
//else
// FoundLine := BackCounter + 1;
Temp := copy(Temp, 1, GetIndentForLine(Editor, Temp, False));
case FIndentType of
sbitCopySpaceTab:
begin
DesiredIndent := copy(Temp, 1,
Lines.PhysicalToLogicalCol(Temp, FoundLine - 1, Result) - 1);
PhysLen := Lines.LogicalToPhysicalCol(Temp, ACaret.LinePos - 1, Length(Temp) + 1);
if PhysLen < Result then
DesiredIndent := DesiredIndent + StringOfChar(' ', Result - PhysLen);
end;
sbitConvertToTabSpace:
begin
dec(Result);
DesiredIndent := CreateTabSpaceMix(Result, False);
inc(Result);
end;
sbitConvertToTabOnly:
begin
dec(Result);
DesiredIndent := CreateTabSpaceMix(Result, True);
inc(Result);
end;
else
DesiredIndent := StringOfChar(' ', Result - 1);
end;
end;
function TSynBeautifier.GetDesiredIndentForLine(Editor: TSynEditBase;
const Lines: TSynEditStrings; const ACaret: TSynEditCaret): Integer;
var
Dummy: String;
Replace: Boolean;
begin
Result := GetDesiredIndentForLine(Editor, Lines, ACaret, Replace, Dummy);
end;
end.
|