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
|
{
***************************************************************************
* *
* This source is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This code is distributed in the hope that it will be useful, but *
* WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
* General Public License for more details. *
* *
* A copy of the GNU General Public License is available on the World *
* Wide Web at <http://www.gnu.org/copyleft/gpl.html>. You can also *
* obtain it by writing to the Free Software Foundation, *
* Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1335, USA. *
* *
***************************************************************************
Author: Mattias Gaertner
Abstract:
Functions to substitute code macros.
Dialog to setup interactive macros by the programmer.
}
unit CodeMacroPrompt;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, LCLProc, Forms, Controls, Graphics, Dialogs,
BasicCodeTools, CodeToolManager,
SynEditAutoComplete, SynPluginTemplateEdit, SynPluginSyncronizedEditBase, SynEdit,
LazIDEIntf, SrcEditorIntf, LazUTF8;
type
TCodeMacroPromptDlg = class(TForm)
private
{ private declarations }
public
{ public declarations }
end;
TLazSynPluginSyncronizedEditCell = class(TSynPluginSyncronizedEditCell)
public
CellValue: String;
end;
{ TLazSynPluginSyncronizedEditList }
TLazSynPluginSyncronizedEditList = class(TSynPluginSyncronizedEditList)
private
public
function AddNew: TSynPluginSyncronizedEditCell; override;
end;
{ TLazTemplateParser }
TLazTemplateParser = class(TIDETemplateParser)
private
FCaret: TPoint;
FEditCellList: TSynPluginSyncronizedEditList;
FEnableMacros: Boolean;
FIndent: integer;
FKeepSubIndent: Boolean;
FSrcTemplate: String;
FDestTemplate: String;
FSrcPosition: Integer;
FDestPosition: Integer;
FDestPosX: Integer;
FDestPosY: Integer;
FLevel: Integer;
FSrcEdit: TSourceEditorInterface;
FSubIndent: integer;
FUseTabWidth: integer;
protected
// nested macros, get the X pos of the outer macro
function GetSrcPosition: Integer; override;
function GetDestPosition: Integer; override;
function GetDestPosX: Integer; override;
function GetDestPosY: Integer; override;
function GetSrcTemplate: String; override;
function GetDestTemplate: String; override;
function SubstituteMacro(const MacroName, MacroParameter: string;
out MacroValue: string): boolean;
function SubstituteMacros(var Template: String): boolean;
public
constructor Create(TheTemplate: String);
destructor Destroy; override;
function SubstituteCodeMacros(SrcEdit: TSourceEditorInterface): boolean;
procedure TrimEOTChar(eot: Char);
property EnableMacros: Boolean read FEnableMacros write FEnableMacros;
property KeepSubIndent: Boolean read FKeepSubIndent write FKeepSubIndent;
property Indent: integer read FIndent write FIndent;
property SubIndent: integer read FSubIndent write FSubIndent;
property DestCaret: TPoint read FCaret;
property UseTabWidth: integer read FUseTabWidth write FUseTabWidth; // if >0 use tabs for indenting with this size
property EditCellList: TSynPluginSyncronizedEditList read FEditCellList;
end;
function ExecuteCodeTemplate(SrcEdit: TSourceEditorInterface;
const TemplateName, TemplateValue, {%H-}TemplateComment,
EndOfTokenChr: string; Attributes: TStrings;
IndentToTokenStart: boolean): boolean;
implementation
{$R *.lfm}
const
MaxLevel = 10; // prevent cycling
{ TLazTemplateParser }
constructor TLazTemplateParser.Create(TheTemplate: String);
begin
inherited Create;
FEditCellList := TLazSynPluginSyncronizedEditList.Create;
FSrcTemplate := TheTemplate;
FDestTemplate := '';
FSrcPosition := 1;
FDestPosition := 1;
FDestPosX := 1;
FDestPosY := 1;
FLevel := 0;
FCaret.y := -1;
end;
destructor TLazTemplateParser.Destroy;
begin
FEditCellList.Free;
inherited Destroy;
end;
function TLazTemplateParser.GetSrcPosition: Integer;
begin
Result := FSrcPosition;
end;
function TLazTemplateParser.GetDestPosition: Integer;
begin
Result := FDestPosition;
end;
function TLazTemplateParser.GetDestPosX: Integer;
begin
Result := FDestPosX;
end;
function TLazTemplateParser.GetDestPosY: Integer;
begin
Result := FDestPosY;
end;
function TLazTemplateParser.GetSrcTemplate: String;
begin
Result := FSrcTemplate;
end;
function TLazTemplateParser.GetDestTemplate: String;
begin
Result := FDestTemplate;
end;
function TLazTemplateParser.SubstituteMacro(const MacroName,
MacroParameter: string; out MacroValue: string): boolean;
var
Macro: TIDECodeMacro;
NewValue: String;
ErrMsg: string;
begin
Result := false;
MacroValue:='';
Macro := IDECodeMacros.FindByName(MacroName);
//debugln('SubstituteMacro A ',MacroName,' ',dbgs(Macro<>nil),' ',MacroParameter);
if Macro <> nil then begin
// substitute macros in Parameter
MacroValue := MacroParameter;
if (FLevel < MaxLevel) and (not SubstituteMacros(MacroValue)) then
exit;
if Macro.Interactive then begin
// collect interactive macro
debugln('SubstitutMacros TODO interactive macros');
end else begin
// normal macro -> substitute
NewValue:='';
try
if not Macro.GetValue(MacroValue, nil, FSrcEdit, NewValue, ErrMsg, self)
then
exit;
except
exit;
end;
MacroValue:=NewValue;
end;
end else begin
// macro unknown
MacroValue:='UnknownMacro('+MacroName+')';
end;
if ErrMsg='' then ;
Result:=true;
end;
function TLazTemplateParser.SubstituteMacros(var Template: String): boolean;
const
TemplateTabWidth = 8;
var
IndentLevel: Integer;
CurLineIndent, LastLineIndent: Integer;
IsLineStart: boolean;
procedure AddIndent;
var
i: Integer;
s: String;
begin
// compare the indentation of the current and the last line of the template
if CurLineIndent>LastLineIndent then
inc(IndentLevel)
else if (IndentLevel>0) and (CurLineIndent<LastLineIndent) then
dec(IndentLevel);
//debugln(['AddIndent LastLineIndent=',LastLineIndent,' CurLineIndent=',CurLineIndent]);
LastLineIndent:=CurLineIndent;
// append space
i:=Indent+IndentLevel*SubIndent;
//debugln(['AddIndent Indent=',Indent,' IndentLevel=',IndentLevel,' SubIndent=',SubIndent,' UseTabWidth=',UseTabWidth]);
s:=GetIndentStr(i,UseTabWidth);
FDestTemplate += s;
FDestPosX:=length(s)+1;
end;
procedure AppendToDest(S: String);
// only called when FLevel=1
var
p, LastCopy: Integer;
begin
if IsLineStart then begin
// remove old indent
p:=length(FDestTemplate);
while (p>=1) and (FDestTemplate[p] in [' ',#9]) do dec(p);
FDestTemplate:=LeftStr(FDestTemplate,p);
end;
p := 1;
LastCopy := 1;
//debugln(['AppendToDest START S="',dbgstr(S),'" Indent=',Indent,' IsLineStart=',IsLineStart,' KeepSubIndent=',KeepSubIndent]);
while p <= length(S) do begin
if IsLineStart and (not KeepSubIndent) and (FDestTemplate<>'')
then begin
// at start of template line (not first line)
LastCopy:=p;
case s[p] of
' ':
begin
inc(CurLineIndent);
inc(p);
continue;
end;
#9:
begin
inc(p);
inc(CurLineIndent,TemplateTabWidth);
CurLineIndent:=CurLineIndent-(CurLineIndent mod TemplateTabWidth);
continue;
end;
else
// first character of line
IsLineStart:=false;
LastCopy:=p;
AddIndent;
// keep p and handle the character in the next step
end;
end;
case s[p] of
#10, #13:
begin
// copy line break
inc(p);
if (p <= length(S)) and (s[p] in [#10,#13]) and (s[p] <> s[p-1]) then
inc(p);
//debugln(['AppendToDest linebreak flush "',dbgstr(copy(s, LastCopy, p - LastCopy)),'"']);
FDestTemplate += copy(s, LastCopy, p - LastCopy);
LastCopy := p;
FDestPosX := 1 + Indent;
IsLineStart:=true;
CurLineIndent:=0;
inc(FDestPosY);
end;
else // case else
if (s[p] = '|') and (FCaret.y < 0) then
begin
// place cursor
System.Delete(s, p, 1);
FCaret.y := FDestPosY;
FCaret.x := FDestPosX;
//debugln(['AppendToDest Caret=',dbgs(FCaret)]);
end
else begin
inc(p);
inc(FDestPosX);
IsLineStart:=false;
end;
end;
end;
//debugln(['AppendToDest LAST flush "',dbgstr(copy(s, LastCopy, p - LastCopy)),'"']);
if IsLineStart then
AddIndent
else
FDestTemplate += copy(s, LastCopy, p - LastCopy);
FDestPosition := length(FDestTemplate);
//debugln(['AppendToDest END FDestTemplate="',dbgstr(FDestTemplate),'" FDestPosition=',FDestPosition,' FDestPosX=',FDestPosX,' FDestPosY=',FDestPosY]);
end;
var
p: LongInt;
len: Integer;
SrcCopiedPos: LongInt;
MacroStartPos: LongInt;
MacroParamStartPos: LongInt;
MacroParamEndPos: LongInt;
MacroEndPos: LongInt;
MacroName: String;
MacroParameter: String;
MacroValue: string;
Lvl: Integer;
SaveSrcPos: LongInt;
begin
// replace as many macros as possible
inc(FLevel);
p:=1;
SrcCopiedPos := 1;
len:=length(Template);
IndentLevel:=0;
CurLineIndent:=0;
LastLineIndent:=0;
IsLineStart:=false;
while p <= len do begin
case Template[p] of
'$':
begin
inc(p);
// could be a macro start
MacroStartPos := p - 1;
MacroEndPos := -1;
if (p <= len) and (Template[p]='$') then begin
System.Delete(Template, p, 1);
inc(FSrcPosition);
end
else begin
// find the macro end
while (p <= len) and (Template[p] in ['a'..'z','A'..'Z','0'..'9','_']) do
inc(p);
if FEnableMacros and (p <= len) and (p-MacroStartPos > 1) and
(Template[p] = '(') then
begin
inc(p);
MacroParamStartPos:=p;
Lvl:=1;
while (p<=len) and (Lvl>0) do begin
case Template[p] of
'(': inc(Lvl);
')': dec(Lvl);
end;
inc(p);
end;
if Lvl=0 then begin
// macro parameter end found
MacroParamEndPos:=p-1;
MacroEndPos:=p;
end;
end;
end;
if MacroEndPos < 0 then begin
// not a macro
p := MacroStartPos + 1;
inc(FSrcPosition);
end
else begin
// Got a Macro
if FLevel = 1 then begin
AppendToDest(copy(Template, SrcCopiedPos, MacroStartPos - SrcCopiedPos));
end;
FSrcPosition := FSrcPosition + MacroEndPos - MacroStartPos;
// read macro name
MacroName:=copy(Template,MacroStartPos+1,
MacroParamStartPos-MacroStartPos-2);
MacroParameter:=copy(Template,MacroParamStartPos,
MacroParamEndPos-MacroParamStartPos);
SaveSrcPos := FSrcPosition;
if not SubstituteMacro(MacroName,MacroParameter,MacroValue) then
exit(false);
FSrcPosition := SaveSrcPos;
//debugln('SubstituteMacros MacroName="',MacroName,'" MacroParameter="',MacroParameter,'" MacroValue="',MacroValue,'"');
Template := copy(Template, 1, MacroStartPos-1)
+ MacroValue
+ copy(Template, MacroEndPos, len);
len:=length(Template);
p:=MacroStartPos+length(MacroValue);
SrcCopiedPos := p;
// scan the result for new lines
if FLevel = 1 then
AppendToDest(MacroValue);
end;
// else it is a normal $ character
end;
else
begin
inc(p);
inc(FSrcPosition);
end;
end;
end;
if FLevel = 1 then begin
AppendToDest(copy(Template, SrcCopiedPos, p - SrcCopiedPos));
if IsLineStart and (not KeepSubIndent) and (FDestTemplate<>'') then
AddIndent;
end;
dec(FLevel);
Result:=true;
end;
function TLazTemplateParser.SubstituteCodeMacros(SrcEdit: TSourceEditorInterface): boolean;
begin
FDestTemplate := '';
FDestPosition := 1;
FDestPosX := 1;
FDestPosY := 1;
FLevel := 0;
FSrcEdit := SrcEdit;
Result := SubstituteMacros(FSrcTemplate);
end;
procedure TLazTemplateParser.TrimEOTChar(eot: Char);
begin
if (FDestTemplate <> '') and (FDestTemplate[length(FDestTemplate)] = eot) then
System.Delete(FDestTemplate, length(FDestTemplate), 1);
end;
function ExecuteCodeTemplate(SrcEdit: TSourceEditorInterface;
const TemplateName, TemplateValue, TemplateComment,
EndOfTokenChr: string; Attributes: TStrings;
IndentToTokenStart: boolean): boolean;
var
AEditor: TSynEdit;
p: TPoint;
TokenStartX: LongInt;
s: string;
BaseIndent, LogBaseIndent: Integer;
i: Integer;
j: LongInt;
Pattern: String;
LineText: String;
Parser: TLazTemplateParser;
CodeToolBossOriginalIndent : Integer; // So I don't break anything else (hopefully)
begin
Result:=false;
//debugln('ExecuteCodeTemplate ',dbgsName(SrcEdit),' ',dbgsName(SrcEdit.EditorControl));
AEditor:=SrcEdit.EditorControl as TSynEdit;
Pattern:=TemplateValue;
Parser := TLazTemplateParser.Create(Pattern);
AEditor.BeginUpdate;
try
Parser.SubIndent:=AEditor.BlockIndent+AEditor.BlockTabIndent*AEditor.TabWidth;
if (AEditor.BlockTabIndent=0) or (eoTabsToSpaces in AEditor.Options) then
Parser.UseTabWidth:=0
else
Parser.UseTabWidth:=AEditor.BlockTabIndent*AEditor.TabWidth;
p := AEditor.LogicalCaretXY;
TokenStartX:=p.x;
if IndentToTokenStart then begin
BaseIndent := TokenStartX - 1;
end else begin
// indent the same as the first line
BaseIndent:=1;
if (p.y>0) and (p.y<=AEditor.Lines.Count) then begin
s:=AEditor.Lines[p.y-1];
while (BaseIndent<p.x)
and ((BaseIndent>length(s)) or (s[BaseIndent] in [#9,' '])) do
inc(BaseIndent);
end;
LogBaseIndent := BaseIndent - 1;
BaseIndent:=AEditor.LogicalToPhysicalCol(s, p.y - 1, BaseIndent);// consider tabs
dec(BaseIndent);
end;
Parser.EnableMacros := Attributes.IndexOfName(CodeTemplateEnableMacros)>=0;
Parser.KeepSubIndent := Attributes.IndexOfName(CodeTemplateKeepSubIndent)>=0;
Parser.Indent := LogBaseIndent;
CodeToolBossOriginalIndent := CodeToolBoss.IndentSize;
if Parser.KeepSubIndent then
CodeToolBoss.IndentSize := BaseIndent // Use additional indentation
else
CodeToolBoss.IndentSize := 0; // Use current indentation
try
LazarusIDE.SaveSourceEditorChangesToCodeCache(nil);
if not Parser.SubstituteCodeMacros(SrcEdit) then exit;
finally
CodeToolBoss.IndentSize := CodeToolBossOriginalIndent;
end;
s:=AEditor.Lines[p.y-1];
if TokenStartX>length(s) then
TokenStartX:=length(s)+1;
j:=length(TemplateName);
while (j>0)
and (UTF8CompareText(copy(TemplateName,1,j),copy(s,TokenStartX-j,j))<>0) do
dec(j);
dec(TokenStartX,j);
AEditor.BlockBegin := Point(TokenStartX, p.y);
AEditor.BlockEnd := p;
// New Caret
p := Parser.DestCaret; // Logical
if p.y >= 0 then begin
if p.y = 1 then
p.x := p.x + TokenStartX - 1;
p.y := p.y + AEditor.BlockBegin.y - 1;
// p must be logical, until template text is inserted
end;
// delete double end separator (e.g. avoid creating two semicolons 'begin end;;')
if (AEditor.BlockEnd.Y>0) and (AEditor.BlockEnd.Y<=AEditor.Lines.Count)
then begin
LineText:=AEditor.Lines[AEditor.BlockEnd.Y-1];
if AEditor.BlockEnd.X <= length(LineText) then
i := pos(LineText[AEditor.BlockEnd.X], EndOfTokenChr)
else
i := -1;
if i > 0 then
Parser.TrimEOTChar(EndOfTokenChr[i]);
end;
if Parser.EditCellList.Count > 0 then
i := AEditor.PluginCount - 1
else
i := -1;
while i >= 0 do begin
if AEditor.Plugin[i] is TSynPluginTemplateEdit then begin
if p.y < 1 then
p := AEditor.CaretXY;
TSynPluginTemplateEdit(AEditor.Plugin[i]).CellParserEnabled := False;
TSynPluginTemplateEdit(AEditor.Plugin[i]).SetTemplate(Parser.DestTemplate, p);
TSynPluginTemplateEdit(AEditor.Plugin[i]).AddEditCells(Parser.EditCellList);
break;
end;
dec(i);
end;
if i < 0 then begin
// replace the selected text and position the caret
AEditor.SetTextBetweenPoints(AEditor.BlockBegin, AEditor.BlockEnd, Parser.DestTemplate, [], scamEnd);
if p.y > 0 then
AEditor.MoveCaretIgnoreEOL(AEditor.LogicalToPhysicalPos(p));
end;
finally
AEditor.EndUpdate;
Parser.Free;
end;
Result:=true;
end;
{ TLazSynPluginSyncronizedEditList }
function TLazSynPluginSyncronizedEditList.AddNew: TSynPluginSyncronizedEditCell;
begin
Result := TLazSynPluginSyncronizedEditCell.Create;
Add(Result);
end;
end.
|