IDE: Add new Application.Title and .Scale statements after Application.Create line. Issue #34318.

git-svn-id: trunk@59156 -
This commit is contained in:
juha 2018-09-24 12:10:50 +00:00
parent 2b0b9e3d74
commit 57d56b4303

View File

@ -3018,38 +3018,19 @@ function TStandardCodeTool.SetApplicationStatement(const APropertyName,
NewCode: string; SourceChangeCache: TSourceChangeCache): boolean;
var
StartPos, ConstStartPos, EndPos: integer;
OldExists: Boolean;
NewStatement: String;
Indent: Integer;
MainBeginNode: TCodeTreeNode;
Beauty: TBeautifyCodeOptions;
begin
Result:=false;
// search old Application.XYZ:= statement
Beauty:=SourceChangeCache.BeautifyCodeOptions;
OldExists:=FindApplicationStatement(UpperCase(APropertyName), StartPos,ConstStartPos,EndPos);
if ConstStartPos=0 then ;
if OldExists then begin
// replace old statement
Indent:=0;
Indent:=Beauty.GetLineIndent(Src,StartPos)
end else begin
// insert as first line in program begin..end block
MainBeginNode:=FindMainBeginEndNode;
if MainBeginNode=nil then exit;
MoveCursorToNodeStart(MainBeginNode);
ReadNextAtom;
StartPos:=CurPos.EndPos;
EndPos:=StartPos;
Indent:=Beauty.GetLineIndent(Src,StartPos)+Beauty.Indent;
end;
// create statement
// search old Application.APropertyName:=XYZ statement
FindApplicationStatement(UpperCase(APropertyName),StartPos,ConstStartPos,EndPos);
// create statement. FindApplicationStatement always returns an insert point.
NewStatement:='Application.'+APropertyName+':='+NewCode+';';
NewStatement:=Beauty.BeautifyStatement(NewStatement,Indent);
NewStatement:=Beauty.BeautifyStatement(NewStatement,Beauty.Indent);
SourceChangeCache.MainScanner:=Scanner;
if not SourceChangeCache.Replace(gtNewLine,gtNewLine,StartPos,EndPos,
NewStatement)
then
if not SourceChangeCache.Replace(gtNewLine,gtNewLine,StartPos,EndPos,NewStatement) then
exit;
if not SourceChangeCache.Apply then exit;
Result:=true;
@ -3750,43 +3731,68 @@ begin
Result := FindApplicationStatement('SCALED', StartPos, BooleanConstStartPos, EndPos);
end;
function TStandardCodeTool.FindApplicationStatement(
const APropertyUpCase: string; out StartPos, ConstStartPos, EndPos: integer
): boolean;
function TStandardCodeTool.FindApplicationStatement(const APropertyUpCase: string;
out StartPos, ConstStartPos, EndPos: integer): boolean;
// Find statement "Application.APropertyUpCase:=XYZ;" and return True if found.
// Also return its positions (Start, const "XYZ" and End) in out parameters.
// If not found, out parameters get a good position to insert such a statement.
var
MainBeginNode: TCodeTreeNode;
Position: Integer;
AppPos, FirstAppPos: Integer;
begin
Result:=false;
StartPos:=-1;
ConstStartPos:=-1;
EndPos:=-1;
FirstAppPos:=-1;
BuildTree(lsrEnd);
MainBeginNode:=FindMainBeginEndNode;
if MainBeginNode=nil then exit;
Position:=MainBeginNode.StartPos;
if Position<1 then exit;
MoveCursorToCleanPos(Position);
if (MainBeginNode=nil) or (MainBeginNode.StartPos<1) then exit;
MoveCursorToCleanPos(MainBeginNode.StartPos);
repeat
ReadNextAtom;
if UpAtomIs('APPLICATION') then begin
StartPos:=CurPos.StartPos;
if ReadNextAtomIsChar('.') and ReadNextUpAtomIs(APropertyUpCase)
and ReadNextUpAtomIs(':=') then begin
// read till semicolon or end
repeat
if UpAtomIs('APPLICATION') then
begin
AppPos:=CurPos.StartPos;
if FirstAppPos=-1 then
FirstAppPos:=AppPos;
ReadNextAtom;
if AtomIsChar('.') then
begin // Application.APropertyUpCase:=XYZ;
if ReadNextUpAtomIs(APropertyUpCase) and ReadNextUpAtomIs(':=') then
begin
StartPos:=AppPos;
repeat // read till semicolon or end
ReadNextAtom;
if ConstStartPos<1 then
ConstStartPos:=CurPos.StartPos;
EndPos:=CurPos.EndPos;
if CurPos.Flag in [cafEnd,cafSemicolon] then
exit(true);
until CurPos.StartPos>SrcLen;
end;
end
else // Application:=TMyApplication.Create(nil);
if UpAtomIs(':=') and ReadNextUpAtomIs('TMYAPPLICATION')
and ReadNextAtomIsChar('.') and ReadNextUpAtomIs('CREATE') then
repeat // read till semicolon or end
ReadNextAtom;
if ConstStartPos<1 then
ConstStartPos:=CurPos.StartPos;
EndPos:=CurPos.EndPos;
if CurPos.Flag in [cafEnd,cafSemicolon] then begin
Result:=true;
exit;
end;
StartPos:=CurPos.EndPos; // Insert point behind the TMyApplication.Create line.
if CurPos.Flag in [cafEnd,cafSemicolon] then
break;
until CurPos.StartPos>SrcLen;
end;
end;
end; // UpAtomIs('APPLICATION')
until (CurPos.StartPos>SrcLen);
// The statement was not found. Return a good place for insertion.
if StartPos=-1 then
if FirstAppPos <> -1 then
StartPos:=FirstAppPos // Before first Application statement if there is one
else begin
MoveCursorToNodeStart(MainBeginNode);
ReadNextAtom;
StartPos:=CurPos.EndPos; // or after the main Begin.
end;
EndPos:=StartPos; // Both StartPos and EndPos return the same insert point.
end;
function TStandardCodeTool.GatherResourceStringSections(
@ -6994,9 +7000,8 @@ begin
Result := RemoveApplicationStatement('SCALED', SourceChangeCache);
end;
function TStandardCodeTool.RemoveApplicationStatement(
const APropertyUpCase: string; SourceChangeCache: TSourceChangeCache
): boolean;
function TStandardCodeTool.RemoveApplicationStatement(const APropertyUpCase: string;
SourceChangeCache: TSourceChangeCache): boolean;
var
StartPos, ConstStartPos, EndPos: integer;
OldExists: Boolean;
@ -7006,11 +7011,8 @@ begin
Result:=false;
// search old Application.XYZ:= statement
OldExists:=FindApplicationStatement(APropertyUpCase,StartPos,ConstStartPos,EndPos);
if not OldExists then begin
Result:=true;
exit;
end;
if ConstStartPos=0 then ;
if not OldExists then
exit(true);
// -> delete whole line
FromPos:=FindLineEndOrCodeInFrontOfPosition(StartPos);
ToPos:=FindLineEndOrCodeAfterPosition(EndPos);