MG: implemented form renaming

git-svn-id: trunk@3235 -
This commit is contained in:
lazarus 2002-08-23 11:24:43 +00:00
parent 8b8fccae08
commit fdf0b1a83c
3 changed files with 262 additions and 100 deletions

View File

@ -271,6 +271,10 @@ type
const AClassName, AVarName: string): boolean; const AClassName, AVarName: string): boolean;
function RemoveCreateFormStatement(Code: TCodeBuffer; function RemoveCreateFormStatement(Code: TCodeBuffer;
const AVarName: string): boolean; const AVarName: string): boolean;
function ChangeCreateFormStatement(Code: TCodeBuffer;
const OldClassName, OldVarName: string;
const NewClassName, NewVarName: string;
OnlyIfExists: boolean): boolean;
function ListAllCreateFormStatements(Code: TCodeBuffer): TStrings; function ListAllCreateFormStatements(Code: TCodeBuffer): TStrings;
function SetAllCreateFromStatements(Code: TCodeBuffer; function SetAllCreateFromStatements(Code: TCodeBuffer;
List: TStrings): boolean; List: TStrings): boolean;
@ -1320,6 +1324,26 @@ begin
end; end;
end; end;
function TCodeToolManager.ChangeCreateFormStatement(Code: TCodeBuffer;
const OldClassName, OldVarName: string; const NewClassName,
NewVarName: string; OnlyIfExists: boolean): boolean;
begin
Result:=false;
{$IFDEF CTDEBUG}
writeln('TCodeToolManager.ChangeCreateFormStatement A ',Code.Filename,
' ',OldVarName.':',OldClassName,' -> ',NewVarName.':',NewClassName,
' OnlyIfExists=',OnlyIfExists);
{$ENDIF}
if not InitCurCodeTool(Code) then exit;
try
Result:=FCurCodeTool.ChangeCreateFormStatement(-1,OldClassName,OldVarName,
NewClassName,NewVarName,true,
SourceChangeCache);
except
on e: Exception do Result:=HandleException(e);
end;
end;
function TCodeToolManager.ListAllCreateFormStatements( function TCodeToolManager.ListAllCreateFormStatements(
Code: TCodeBuffer): TStrings; Code: TCodeBuffer): TStrings;
begin begin

View File

@ -115,11 +115,14 @@ type
FrontGap, AfterGap: TGapTyp; FrontGap, AfterGap: TGapTyp;
FromPos, ToPos: integer; FromPos, ToPos: integer;
Text: string; Text: string;
FromCode: TCodeBuffer; DirectCode: TCodeBuffer; // set if change of non cleaned source
FromDirectPos: integer; FromDirectPos, ToDirectPos: integer;
IsDirectChange: boolean;
constructor Create(aFrontGap, anAfterGap: TGapTyp; aFromPos, constructor Create(aFrontGap, anAfterGap: TGapTyp; aFromPos,
aToPos: integer; const aText: string; aFromCode: TCodeBuffer; aToPos: integer; const aText: string; aDirectCode: TCodeBuffer;
aFromDirectPos: integer); aFromDirectPos, AToDirectPos: integer; aIsDirectChange: boolean);
function IsDeleteOperation: boolean;
function IsAtSamePos(AnEntry: TSourceChangeCacheEntry): boolean;
end; end;
//---------------------------------------------------------------------------- //----------------------------------------------------------------------------
@ -137,7 +140,9 @@ type
FUpdateLock: integer; FUpdateLock: integer;
Src: string; // current cleaned source Src: string; // current cleaned source
SrcLen: integer; // same as length(Src) SrcLen: integer; // same as length(Src)
procedure DeleteOldText(CleanFromPos,CleanToPos: integer); procedure DeleteCleanText(CleanFromPos,CleanToPos: integer);
procedure DeleteDirectText(ACode: TCodeBuffer;
DirectFromPos,DirectToPos: integer);
procedure InsertNewText(ACode: TCodeBuffer; DirectPos: integer; procedure InsertNewText(ACode: TCodeBuffer; DirectPos: integer;
const InsertText: string); const InsertText: string);
procedure SetMainScanner(NewScanner: TLinkScanner); procedure SetMainScanner(NewScanner: TLinkScanner);
@ -151,7 +156,7 @@ type
function Replace(FrontGap, AfterGap: TGapTyp; FromPos, ToPos: integer; function Replace(FrontGap, AfterGap: TGapTyp; FromPos, ToPos: integer;
const Text: string): boolean; const Text: string): boolean;
function ReplaceEx(FrontGap, AfterGap: TGapTyp; FromPos, ToPos: integer; function ReplaceEx(FrontGap, AfterGap: TGapTyp; FromPos, ToPos: integer;
FromCode: TCodeBuffer; FromDirectPos: integer; DirectCode: TCodeBuffer; FromDirectPos, ToDirectPos: integer;
const Text: string): boolean; const Text: string): boolean;
function Apply: boolean; function Apply: boolean;
function FindEntryInRange(FromPos, ToPos: integer): TSourceChangeCacheEntry; function FindEntryInRange(FromPos, ToPos: integer): TSourceChangeCacheEntry;
@ -204,6 +209,7 @@ function MethodInsertPolicyNameToPolicy(const s: string): TMethodInsertPolicy;
function ForwardProcInsertPolicyNameToPolicy( function ForwardProcInsertPolicyNameToPolicy(
const s: string): TForwardProcInsertPolicy; const s: string): TForwardProcInsertPolicy;
implementation implementation
@ -253,15 +259,21 @@ begin
Result:=1 Result:=1
else if Entry1.FromPos<Entry2.FromPos then else if Entry1.FromPos<Entry2.FromPos then
Result:=-1 Result:=-1
else else begin
Result:=0; if Entry1.FromDirectPos>Entry2.FromDirectPos then
Result:=1
else if Entry1.FromDirectPos<Entry2.FromDirectPos then
Result:=-1
else
Result:=0;
end;
end; end;
{ TSourceChangeCacheEntry } { TSourceChangeCacheEntry }
constructor TSourceChangeCacheEntry.Create(aFrontGap, anAfterGap: TGapTyp; constructor TSourceChangeCacheEntry.Create(aFrontGap, anAfterGap: TGapTyp;
aFromPos, aToPos: integer; const aText: string; aFromCode: TCodeBuffer; aFromPos, aToPos: integer; const aText: string; aDirectCode: TCodeBuffer;
aFromDirectPos: integer); aFromDirectPos, AToDirectPos: integer; aIsDirectChange: boolean);
begin begin
inherited Create; inherited Create;
FrontGap:=aFrontGap; FrontGap:=aFrontGap;
@ -269,8 +281,22 @@ begin
FromPos:=aFromPos; FromPos:=aFromPos;
ToPos:=aToPos; ToPos:=aToPos;
Text:=aText; Text:=aText;
FromCode:=aFromCode; DirectCode:=aDirectCode;
FromDirectPos:=aFromDirectPos; FromDirectPos:=aFromDirectPos;
ToDirectPos:=aToDirectPos;
IsDirectChange:=aIsDirectChange;
end;
function TSourceChangeCacheEntry.IsDeleteOperation: boolean;
begin
Result:=(ToPos>FromPos)
or ((DirectCode<>nil) and (FromDirectPos>0) and (ToDirectPos>FromDirectPos));
end;
function TSourceChangeCacheEntry.IsAtSamePos(AnEntry: TSourceChangeCacheEntry
): boolean;
begin
Result:=(FromPos=AnEntry.FromPos) and (FromDirectPos=AnEntry.FromDirectPos);
end; end;
@ -330,39 +356,48 @@ begin
Result:=nil; Result:=nil;
end; end;
function TSourceChangeCache.ReplaceEx(FrontGap, AfterGap: TGapTyp; function TSourceChangeCache.ReplaceEx(FrontGap, AfterGap: TGapTyp;
FromPos, ToPos: integer; FromPos, ToPos: integer;
FromCode: TCodeBuffer; FromDirectPos: integer; DirectCode: TCodeBuffer; FromDirectPos, ToDirectPos: integer;
const Text: string): boolean; const Text: string): boolean;
var ANode: TAVLTreeNode; var
ANode: TAVLTreeNode;
NewEntry: TSourceChangeCacheEntry; NewEntry: TSourceChangeCacheEntry;
p: pointer; p: pointer;
IsDirectChange: boolean;
begin begin
{$IFDEF CTDEBUG} {$IFDEF CTDEBUG}
writeln('TSourceChangeCache.ReplaceEx FrontGap=',ord(FrontGap), writeln('TSourceChangeCache.ReplaceEx FrontGap=',ord(FrontGap),
' AfterGap=',ord(AfterGap),' FromPos=',FromPos,' ToPos=',ToPos, ' AfterGap=',ord(AfterGap),' CleanPos=',FromPos,'-',ToPos,
' Text="',Text,'"'); ' Text="',Text,'"');
if FromCode<>nil then writeln('FromCode=',FromCode.Filename,' FromDirectPos=',FromDirectPos); if DirectCode<>nil then
writeln('DirectCode=',DirectCode.Filename,' DirectPos=',FromDirectPos,'-',ToDirectPos);
{$ENDIF} {$ENDIF}
Result:=false; Result:=false;
if (MainScanner=nil) or (FromPos>ToPos) or (FromPos<1) if (MainScanner=nil) or (FromPos>ToPos) or (FromPos<1)
or (ToPos>MainScanner.CleanedLen+1) then or (ToPos>MainScanner.CleanedLen+1) then
exit; exit;
IsDirectChange:=DirectCode<>nil;
if FindEntryInRange(FromPos,ToPos)<>nil then exit; if FindEntryInRange(FromPos,ToPos)<>nil then exit;
if ToPos>FromPos then begin if ToPos>FromPos then begin
// this is a delete operation -> check the whole range for writable buffers // this is a delete operation -> check the whole range for writable buffers
if not MainScanner.WholeRangeIsWritable(FromPos,ToPos) then exit; if not MainScanner.WholeRangeIsWritable(FromPos,ToPos) then exit;
end else if (DirectCode<>nil) and (FromDirectPos<ToDirectPos) then begin
// this is a direct delete operation -> check if the DirectCode is writable
if DirectCode.ReadOnly then exit;
end; end;
if FromCode=nil then begin if DirectCode=nil then begin
if not MainScanner.CleanedPosToCursor(FromPos,FromDirectPos,p) then if not MainScanner.CleanedPosToCursor(FromPos,FromDirectPos,p) then
exit; exit;
FromCode:=TCodeBuffer(p); DirectCode:=TCodeBuffer(p);
ToDirectPos:=0;
end; end;
// add entry // add entry
NewEntry:=TSourceChangeCacheEntry.Create(FrontGap,AfterGap,FromPos,ToPos, NewEntry:=TSourceChangeCacheEntry.Create(FrontGap,AfterGap,FromPos,ToPos,
Text,FromCode,FromDirectPos); Text,DirectCode,FromDirectPos,ToDirectPos,IsDirectChange);
ANode:=FEntries.Add(NewEntry); ANode:=FEntries.Add(NewEntry);
if ToPos=FromPos then if not NewEntry.IsDeleteOperation then
FEntries.MoveDataLeftMost(ANode) FEntries.MoveDataLeftMost(ANode)
else else
// the new entry is a delete operation -> put it rightmost, so that it will // the new entry is a delete operation -> put it rightmost, so that it will
@ -379,7 +414,7 @@ end;
function TSourceChangeCache.Replace(FrontGap, AfterGap: TGapTyp; function TSourceChangeCache.Replace(FrontGap, AfterGap: TGapTyp;
FromPos, ToPos: integer; const Text: string): boolean; FromPos, ToPos: integer; const Text: string): boolean;
begin begin
Result:=ReplaceEx(FrontGap,AfterGap,FromPos,ToPos,nil,0,Text); Result:=ReplaceEx(FrontGap,AfterGap,FromPos,ToPos,nil,0,0,Text);
end; end;
procedure TSourceChangeCache.Clear; procedure TSourceChangeCache.Clear;
@ -412,12 +447,108 @@ begin
end; end;
function TSourceChangeCache.Apply: boolean; function TSourceChangeCache.Apply: boolean;
var CurNode, PrecNode: TAVLTreeNode; var
CurNode, PrecNode: TAVLTreeNode;
CurEntry, PrecEntry, FirstEntry: TSourceChangeCacheEntry; CurEntry, PrecEntry, FirstEntry: TSourceChangeCacheEntry;
InsertText: string; InsertText: string;
i, j, NeededLineEnds, NeededIndent, FromPosAdjustment: integer; FromPosAdjustment: integer;
BetweenGap: TGapTyp; BetweenGap: TGapTyp;
Abort: boolean; Abort: boolean;
procedure AddAfterGap(AnEntry: TSourceChangeCacheEntry);
var
ToPos, ToSrcLen: integer;
ToSrc: string;
NeededLineEnds, NeededIndent, i, j: integer;
begin
if not AnEntry.IsDirectChange then begin
ToPos:=AnEntry.ToPos;
ToSrc:=Src;
end else begin
ToPos:=AnEntry.ToDirectPos;
ToSrc:=AnEntry.DirectCode.Source;
end;
case AnEntry.AfterGap of
gtSpace:
begin
if ((ToPos>length(ToSrc))
or (not IsSpaceChar[ToSrc[ToPos]])) then
InsertText:=InsertText+' ';
end;
gtNewLine:
begin
NeededLineEnds:=CountNeededLineEndsToAddForward(ToSrc,ToPos,1);
if NeededLineEnds>0 then
InsertText:=InsertText+BeautifyCodeOptions.LineEnd;
end;
gtEmptyLine:
begin
NeededLineEnds:=CountNeededLineEndsToAddForward(ToSrc,ToPos,2);
for i:=1 to NeededLineEnds do
InsertText:=InsertText+BeautifyCodeOptions.LineEnd;
end;
end;
if AnEntry.AfterGap in [gtNewLine,gtEmptyLine] then begin
// move the rest of the line behind the insert position to the next line
// with auto indent
NeededIndent:=GetLineIndent(ToSrc,ToPos);
j:=ToPos;
ToSrcLen:=length(ToSrc);
while (j<=ToSrcLen) and (IsSpaceChar[ToSrc[j]]) do
inc(j);
dec(NeededIndent,j-ToPos);
if NeededIndent>0 then
InsertText:=InsertText+GetIndentStr(NeededIndent);
end;
end;
procedure AddFrontGap(AnEntry: TSourceChangeCacheEntry);
var
NeededLineEnds: integer;
FromPos: integer;
FromSrc: string;
i: integer;
begin
if not AnEntry.IsDirectChange then begin
FromPos:=AnEntry.FromPos;
FromSrc:=Src;
end else begin
FromPos:=AnEntry.FromDirectPos;
FromSrc:=AnEntry.DirectCode.Source;
end;
NeededLineEnds:=0;
case CurEntry.FrontGap of
gtSpace:
begin
if (FromPos<=1)
or (not IsSpaceChar[FromSrc[FromPos-1]]) then
InsertText:=' '+InsertText;
end;
gtNewLine:
begin
NeededLineEnds:=CountNeededLineEndsToAddBackward(FromSrc,FromPos-1,1);
if NeededLineEnds>0 then
InsertText:=BeautifyCodeOptions.LineEnd+InsertText;
end;
gtEmptyLine:
begin
NeededLineEnds:=CountNeededLineEndsToAddBackward(FromSrc,FromPos-1,2);
for i:=1 to NeededLineEnds do
InsertText:=BeautifyCodeOptions.LineEnd+InsertText;
end;
end;
FromPosAdjustment:=0;
if (CurEntry.FrontGap in [gtNewLine,gtEmptyLine]) and (NeededLineEnds=0)
then begin
// no line end was inserted in front
// -> adjust the FromPos to replace the space in the existing line
while (FromPos+FromPosAdjustment>1)
and (not (FromSrc[FromPos+FromPosAdjustment-1]
in [#10,#13]))
do dec(FromPosAdjustment);
end;
end;
begin begin
{$IFDEF CTDEBUG} {$IFDEF CTDEBUG}
writeln('TSourceChangeCache.Apply EntryCount=',FEntries.Count); writeln('TSourceChangeCache.Apply EntryCount=',FEntries.Count);
@ -449,45 +580,13 @@ begin
{$ENDIF} {$ENDIF}
InsertText:=FirstEntry.Text; InsertText:=FirstEntry.Text;
// add after gap // add after gap
case FirstEntry.AfterGap of AddAfterGap(FirstEntry);
gtSpace:
begin
if ((FirstEntry.ToPos>SrcLen)
or (not IsSpaceChar[Src[FirstEntry.ToPos]])) then
InsertText:=InsertText+' ';
end;
gtNewLine:
begin
NeededLineEnds:=CountNeededLineEndsToAddForward(Src,
FirstEntry.ToPos,1);
if NeededLineEnds>0 then
InsertText:=InsertText+BeautifyCodeOptions.LineEnd;
end;
gtEmptyLine:
begin
NeededLineEnds:=CountNeededLineEndsToAddForward(Src,
FirstEntry.ToPos,2);
for i:=1 to NeededLineEnds do
InsertText:=InsertText+BeautifyCodeOptions.LineEnd;
end;
end;
if FirstEntry.AfterGap in [gtNewLine,gtEmptyLine] then begin
// move the rest of the line behind the insert position to the next line
// with auto indent
NeededIndent:=GetLineIndent(Src,FirstEntry.ToPos);
j:=FirstEntry.ToPos;
while (j<=SrcLen) and (IsSpaceChar[Src[j]]) do
inc(j);
dec(NeededIndent,j-FirstEntry.ToPos);
if NeededIndent>0 then
InsertText:=InsertText+GetIndentStr(NeededIndent);
end;
// add text from nodes inserted at the same position // add text from nodes inserted at the same position
PrecNode:=FEntries.FindPrecessor(CurNode); PrecNode:=FEntries.FindPrecessor(CurNode);
CurEntry:=FirstEntry; CurEntry:=FirstEntry;
while (PrecNode<>nil) do begin while (PrecNode<>nil) do begin
PrecEntry:=TSourceChangeCacheEntry(PrecNode.Data); PrecEntry:=TSourceChangeCacheEntry(PrecNode.Data);
if PrecEntry.FromPos=CurEntry.FromPos then begin if PrecEntry.IsAtSamePos(CurEntry) then begin
BetweenGap:=PrecEntry.AfterGap; BetweenGap:=PrecEntry.AfterGap;
if ord(BetweenGap)<ord(CurEntry.FrontGap) then if ord(BetweenGap)<ord(CurEntry.FrontGap) then
BetweenGap:=CurEntry.FrontGap; BetweenGap:=CurEntry.FrontGap;
@ -508,43 +607,16 @@ begin
PrecNode:=FEntries.FindPrecessor(CurNode); PrecNode:=FEntries.FindPrecessor(CurNode);
end; end;
// add front gap // add front gap
NeededLineEnds:=0; AddFrontGap(CurEntry);
case CurEntry.FrontGap of
gtSpace:
begin
if (CurEntry.FromPos=1)
or (not IsSpaceChar[Src[CurEntry.FromPos-1]]) then
InsertText:=' '+InsertText;
end;
gtNewLine:
begin
NeededLineEnds:=CountNeededLineEndsToAddBackward(Src,
CurEntry.FromPos-1,1);
if NeededLineEnds>0 then
InsertText:=BeautifyCodeOptions.LineEnd+InsertText;
end;
gtEmptyLine:
begin
NeededLineEnds:=CountNeededLineEndsToAddBackward(Src,
CurEntry.FromPos-1,2);
for i:=1 to NeededLineEnds do
InsertText:=BeautifyCodeOptions.LineEnd+InsertText;
end;
end;
FromPosAdjustment:=0;
if (CurEntry.FrontGap in [gtNewLine,gtEmptyLine]) and (NeededLineEnds=0)
then begin
// no line end was inserted in front
// -> adjust the FromPos to replace the space in the existing line
while (FirstEntry.FromPos+FromPosAdjustment>1)
and (not (Src[FirstEntry.FromPos+FromPosAdjustment-1]
in [#10,#13]))
do dec(FromPosAdjustment);
end;
// delete old text in code buffers // delete old text in code buffers
DeleteOldText(FirstEntry.FromPos+FromPosAdjustment,FirstEntry.ToPos); if not FirstEntry.IsDirectChange then
DeleteCleanText(FirstEntry.FromPos+FromPosAdjustment,FirstEntry.ToPos)
else
DeleteDirectText(FirstEntry.DirectCode,
FirstEntry.FromDirectPos+FromPosAdjustment,
FirstEntry.ToDirectPos);
// insert new text // insert new text
InsertNewText(FirstEntry.FromCode, InsertNewText(FirstEntry.DirectCode,
FirstEntry.FromDirectPos+FromPosAdjustment,InsertText); FirstEntry.FromDirectPos+FromPosAdjustment,InsertText);
CurNode:=PrecNode; CurNode:=PrecNode;
end; end;
@ -555,14 +627,26 @@ begin
Result:=true; Result:=true;
end; end;
procedure TSourceChangeCache.DeleteOldText(CleanFromPos,CleanToPos: integer); procedure TSourceChangeCache.DeleteCleanText(CleanFromPos,CleanToPos: integer);
begin begin
{$IFDEF CTDEBUG} {$IFDEF CTDEBUG}
writeln('[TSourceChangeCache.DeleteOldText] Pos=',CleanFromPos,'-',CleanToPos); writeln('[TSourceChangeCache.DeleteCleanText] Pos=',CleanFromPos,'-',CleanToPos);
{$ENDIF} {$ENDIF}
if CleanFromPos=CleanToPos then exit;
MainScanner.DeleteRange(CleanFromPos,CleanToPos); MainScanner.DeleteRange(CleanFromPos,CleanToPos);
end; end;
procedure TSourceChangeCache.DeleteDirectText(ACode: TCodeBuffer; DirectFromPos,
DirectToPos: integer);
begin
{$IFDEF CTDEBUG}
writeln('[TSourceChangeCache.DeleteDirectText] Code=',ACode.Filename,
' Pos=',DirectFromPos,'-',DirectToPos);
{$ENDIF}
if DirectFromPos=DirectToPos then exit;
ACode.Delete(DirectFromPos,DirectToPos-DirectFromPos);
end;
procedure TSourceChangeCache.InsertNewText(ACode: TCodeBuffer; procedure TSourceChangeCache.InsertNewText(ACode: TCodeBuffer;
DirectPos: integer; const InsertText: string); DirectPos: integer; const InsertText: string);
begin begin
@ -570,6 +654,7 @@ begin
writeln('[TSourceChangeCache.InsertNewText] Code=',ACode.Filename, writeln('[TSourceChangeCache.InsertNewText] Code=',ACode.Filename,
' Pos=',DirectPos,' Text="',InsertText,'"'); ' Pos=',DirectPos,' Text="',InsertText,'"');
{$ENDIF} {$ENDIF}
if InsertText='' then exit;
ACode.Insert(DirectPos,InsertText); ACode.Insert(DirectPos,InsertText);
end; end;

View File

@ -112,6 +112,11 @@ type
AVarName: string; SourceChangeCache: TSourceChangeCache): boolean; AVarName: string; SourceChangeCache: TSourceChangeCache): boolean;
function RemoveCreateFormStatement(const UpperVarName: string; function RemoveCreateFormStatement(const UpperVarName: string;
SourceChangeCache: TSourceChangeCache): boolean; SourceChangeCache: TSourceChangeCache): boolean;
function ChangeCreateFormStatement(StartPos: integer;
const OldClassName, OldVarName: string;
const NewClassName, NewVarName: string;
OnlyIfExists: boolean;
SourceChangeCache: TSourceChangeCache): boolean;
function ListAllCreateFormStatements: TStrings; function ListAllCreateFormStatements: TStrings;
function SetAllCreateFromStatements(List: TStrings; function SetAllCreateFromStatements(List: TStrings;
SourceChangeCache: TSourceChangeCache): boolean; SourceChangeCache: TSourceChangeCache): boolean;
@ -637,7 +642,9 @@ begin
FromPos:=Scanner.Links[i].CleanedPos; FromPos:=Scanner.Links[i].CleanedPos;
end; end;
if not SourceChangeCache.ReplaceEx(gtNewLine,gtNewLine,FromPos,FromPos, if not SourceChangeCache.ReplaceEx(gtNewLine,gtNewLine,FromPos,FromPos,
ResourceCode,ResourceCode.SourceLength+1,ResourceData) then exit; ResourceCode,ResourceCode.SourceLength+1,ResourceCode.SourceLength+1,
ResourceData)
then exit;
end; end;
if not SourceChangeCache.Apply then exit; if not SourceChangeCache.Apply then exit;
Result:=true; Result:=true;
@ -808,12 +815,14 @@ begin
SourceChangeCache.BeautifyCodeOptions.BeautifyStatement( SourceChangeCache.BeautifyCodeOptions.BeautifyStatement(
'Application.CreateForm('+AClassName+','+AVarName+');',Indent)); 'Application.CreateForm('+AClassName+','+AVarName+');',Indent));
end else begin end else begin
// it exists -> replace it
FromPos:=FindLineEndOrCodeInFrontOfPosition(OldPosition.StartPos); FromPos:=FindLineEndOrCodeInFrontOfPosition(OldPosition.StartPos);
ToPos:=FindFirstLineEndAfterInCode(OldPosition.EndPos); ToPos:=FindFirstLineEndAfterInCode(OldPosition.EndPos);
SourceChangeCache.MainScanner:=Scanner; SourceChangeCache.MainScanner:=Scanner;
SourceChangeCache.Replace(gtNewLine,gtNewLine,FromPos,ToPos, SourceChangeCache.Replace(gtNewLine,gtNewLine,FromPos,ToPos,
SourceChangeCache.BeautifyCodeOptions.BeautifyStatement( SourceChangeCache.BeautifyCodeOptions.BeautifyStatement(
'Application.CreateForm('+AClassName+','+AVarName+')',2)); 'Application.CreateForm('+AClassName+','+AVarName+');',
SourceChangeCache.BeautifyCodeOptions.Indent));
end; end;
Result:=SourceChangeCache.Apply; Result:=SourceChangeCache.Apply;
end; end;
@ -833,6 +842,45 @@ begin
Result:=SourceChangeCache.Apply; Result:=SourceChangeCache.Apply;
end; end;
function TStandardCodeTool.ChangeCreateFormStatement(StartPos: integer;
const OldClassName, OldVarName: string;
const NewClassName, NewVarName: string;
OnlyIfExists: boolean; SourceChangeCache: TSourceChangeCache): boolean;
var MainBeginNode: TCodeTreeNode;
OldPosition: TAtomPosition;
FromPos, ToPos, Indent: integer;
begin
Result:=false;
if (OldClassName='') or (length(OldClassName)>255)
or (OldVarName='') or (length(OldVarName)>255)
or (NewClassName='') or (length(NewClassName)>255)
or (NewVarName='') or (length(NewVarName)>255)
then exit;
BuildTree(false);
MainBeginNode:=FindMainBeginEndNode;
if MainBeginNode=nil then exit;
FromPos:=-1;
if FindCreateFormStatement(MainBeginNode.StartPos,UpperCaseStr(OldClassName),
UpperCaseStr(OldVarName),OldPosition)=-1 then begin
// does not exists
if OnlyIfExists then begin
Result:=true;
exit;
end;
Result:=AddCreateFormStatement(NewClassName,NewVarName,SourceChangeCache);
end else begin
// replace
FromPos:=FindLineEndOrCodeInFrontOfPosition(OldPosition.StartPos);
ToPos:=FindFirstLineEndAfterInCode(OldPosition.EndPos);
SourceChangeCache.MainScanner:=Scanner;
SourceChangeCache.Replace(gtNewLine,gtNewLine,FromPos,ToPos,
SourceChangeCache.BeautifyCodeOptions.BeautifyStatement(
'Application.CreateForm('+NewClassName+','+NewVarName+');',
SourceChangeCache.BeautifyCodeOptions.Indent));
Result:=SourceChangeCache.Apply;
end;
end;
function TStandardCodeTool.ListAllCreateFormStatements: TStrings; function TStandardCodeTool.ListAllCreateFormStatements: TStrings;
// list format: VarName:ClassName // list format: VarName:ClassName
var Position: integer; var Position: integer;
@ -962,13 +1010,13 @@ end;
function TStandardCodeTool.ReplaceIdentifiers(IdentList: TStrings; function TStandardCodeTool.ReplaceIdentifiers(IdentList: TStrings;
SourceChangeCache: TSourceChangeCache): boolean; SourceChangeCache: TSourceChangeCache): boolean;
procedure ReplaceIdentifiersInSource(Code: TCodeBuffer); procedure ReplaceIdentifiersInSource(ACode: TCodeBuffer);
var var
StartPos, EndPos, MaxPos, IdentStart: integer; StartPos, EndPos, MaxPos, IdentStart, IdentEnd: integer;
CurSource: string; CurSource: string;
i: integer; i: integer;
begin begin
CurSource:=Code.Source; CurSource:=ACode.Source;
MaxPos:=length(CurSource); MaxPos:=length(CurSource);
StartPos:=1; StartPos:=1;
// go through all source parts between compiler directives // go through all source parts between compiler directives
@ -986,7 +1034,9 @@ function TStandardCodeTool.ReplaceIdentifiers(IdentList: TStrings;
@CurSource[IdentStart])=0 then @CurSource[IdentStart])=0 then
begin begin
// identifier found -> replace // identifier found -> replace
IdentEnd:=IdentStart+length(IdentList[i]);
SourceChangeCache.ReplaceEx(gtNone,gtNone,1,1,
ACode,IdentStart,IdentEnd,IdentList[i+1]);
break; break;
end; end;
inc(i,2); inc(i,2);
@ -1018,6 +1068,7 @@ begin
or (Odd(IdentList.Count)) then exit; or (Odd(IdentList.Count)) then exit;
BuildTree(false); BuildTree(false);
if Scanner=nil then exit; if Scanner=nil then exit;
SourceChangeCache.MainScanner:=Scanner;
SourceList:=TList.Create; SourceList:=TList.Create;
try try
Scanner.FindCodeInRange(1,SrcLen,SourceList); Scanner.FindCodeInRange(1,SrcLen,SourceList);
@ -1027,6 +1078,8 @@ begin
finally finally
SourceList.Free; SourceList.Free;
end; end;
if not SourceChangeCache.Apply then exit;
Result:=true;
end; end;
function TStandardCodeTool.FindPublishedVariable(const UpperClassName, function TStandardCodeTool.FindPublishedVariable(const UpperClassName,