codetools: extract proc: use GetIndentStr of beautifier

git-svn-id: trunk@40817 -
This commit is contained in:
mattias 2013-04-15 09:23:57 +00:00
parent e9156f83de
commit 1c83932008

View File

@ -266,6 +266,7 @@ var
MainBlockNode: TCodeTreeNode; // the main proc node of the selection, or main begin block of program MainBlockNode: TCodeTreeNode; // the main proc node of the selection, or main begin block of program
VarTree: TAVLTree; VarTree: TAVLTree;
ResultNode: TCodeTreeNode; ResultNode: TCodeTreeNode;
Beauty: TBeautifyCodeOptions;
function FindFunctionResultNode: boolean; function FindFunctionResultNode: boolean;
var var
@ -332,8 +333,7 @@ var
if ResultNode<>nil then begin if ResultNode<>nil then begin
CallCode:=GetIdentifier(@Src[ResultNode.StartPos])+':='+CallCode; CallCode:=GetIdentifier(@Src[ResultNode.StartPos])+':='+CallCode;
end; end;
CallCode:=SourceChangeCache.BeautifyCodeOptions.BeautifyStatement( CallCode:=Beauty.BeautifyStatement(CallCode,Indent);
CallCode,Indent);
{$IFDEF CTDebug} {$IFDEF CTDebug}
DebugLn('TExtractProcTool.ReplaceSelectionWithCall C "',CallCode,'" Indent=',dbgs(Indent)); DebugLn('TExtractProcTool.ReplaceSelectionWithCall C "',CallCode,'" Indent=',dbgs(Indent));
{$ENDIF} {$ENDIF}
@ -658,10 +658,8 @@ var
begin begin
// extract identifier and type // extract identifier and type
if VarSectionCode='' then if VarSectionCode='' then
VarSectionCode:='var' VarSectionCode:='var'+Beauty.LineEnd;
+SourceChangeCache.BeautifyCodeOptions.LineEnd; VarSectionCode:=VarSectionCode+Beauty.GetIndentStr(Beauty.Indent);
VarSectionCode:=VarSectionCode
+GetIndentStr(SourceChangeCache.BeautifyCodeOptions.Indent);
VariableName:=GetIdentifier(@Src[ProcVar.Node.StartPos]); VariableName:=GetIdentifier(@Src[ProcVar.Node.StartPos]);
VarTypeNode:=FindTypeNodeOfDefinition(ProcVar.Node); VarTypeNode:=FindTypeNodeOfDefinition(ProcVar.Node);
{$IFDEF CTDebug} {$IFDEF CTDebug}
@ -677,7 +675,7 @@ var
DebugLn('TExtractProcTool.CreateProcVarSection C VariableName="',VariableName,'" VariableType="',VariableTypeCode,'"'); DebugLn('TExtractProcTool.CreateProcVarSection C VariableName="',VariableName,'" VariableType="',VariableTypeCode,'"');
{$ENDIF} {$ENDIF}
VarSectionCode:=VarSectionCode+VariableName+':'+VariableTypeCode VarSectionCode:=VarSectionCode+VariableName+':'+VariableTypeCode
+SourceChangeCache.BeautifyCodeOptions.LineEnd; +Beauty.LineEnd;
end; end;
AVLNode:=VarTree.FindSuccessor(AVLNode); AVLNode:=VarTree.FindSuccessor(AVLNode);
end; end;
@ -685,8 +683,7 @@ var
{$IFDEF CTDebug} {$IFDEF CTDebug}
DebugLn('TExtractProcTool.CreateProcVarSection END VarSectionCode="',VarSectionCode,'"'); DebugLn('TExtractProcTool.CreateProcVarSection END VarSectionCode="',VarSectionCode,'"');
{$ENDIF} {$ENDIF}
VarSectionCode:=SourceChangeCache.BeautifyCodeOptions.BeautifyStatement( VarSectionCode:=Beauty.BeautifyStatement(VarSectionCode,0);
VarSectionCode,0);
Result:=true; Result:=true;
end; end;
@ -699,7 +696,7 @@ var
begin begin
Result:=false; Result:=false;
BeginEndCode:=''; BeginEndCode:='';
le:=SourceChangeCache.BeautifyCodeOptions.LineEnd; le:=Beauty.LineEnd;
// extract dirty source, so that compiler directives are moved too // extract dirty source, so that compiler directives are moved too
StartPos.Code.LineColToPosition(StartPos.Y,StartPos.X,DirtyStartPos); StartPos.Code.LineColToPosition(StartPos.Y,StartPos.X,DirtyStartPos);
StartPos.Code.LineColToPosition(EndPos.Y,EndPos.X,DirtyEndPos); StartPos.Code.LineColToPosition(EndPos.Y,EndPos.X,DirtyEndPos);
@ -714,13 +711,13 @@ var
// adjust indent // adjust indent
Indent:=GetBlockMinIndent(DirtySelection,1,length(DirtySelection)); Indent:=GetBlockMinIndent(DirtySelection,1,length(DirtySelection));
IndentText(DirtySelection, IndentText(DirtySelection,
SourceChangeCache.BeautifyCodeOptions.Indent-Indent, Beauty.Indent-Indent,
SourceChangeCache.BeautifyCodeOptions.TabWidth, Beauty.TabWidth,
s); s);
DirtySelection:=s; DirtySelection:=s;
if ResultNode<>nil then begin if ResultNode<>nil then begin
DirtySelection:=DirtySelection DirtySelection:=DirtySelection
+GetIndentStr(SourceChangeCache.BeautifyCodeOptions.Indent) +Beauty.GetIndentStr(Beauty.Indent)
+'Result:='+GetIdentifier(@Src[ResultNode.StartPos])+';'+le; +'Result:='+GetIdentifier(@Src[ResultNode.StartPos])+';'+le;
end; end;
// create Begin..End block // create Begin..End block
@ -753,8 +750,7 @@ var
while BeginNode.Desc<>ctnBeginBlock do while BeginNode.Desc<>ctnBeginBlock do
BeginNode:=BeginNode.PriorBrother; BeginNode:=BeginNode.PriorBrother;
InsertPos:=BeginNode.StartPos; InsertPos:=BeginNode.StartPos;
Indent:=GetLineIndent(Src,InsertPos) Indent:=GetLineIndent(Src,InsertPos)+Beauty.Indent;
+SourceChangeCache.BeautifyCodeOptions.Indent;
end; end;
eptSubProcedureSameLvl: eptSubProcedureSameLvl:
@ -879,8 +875,7 @@ var
eptProcedureWithInterface: eptProcedureWithInterface:
begin begin
ProcHeader:=Keyword+' '+ProcName+CompleteParamList+';'; ProcHeader:=Keyword+' '+ProcName+CompleteParamList+';';
ProcHeader:=SourceChangeCache.BeautifyCodeOptions.BeautifyStatement( ProcHeader:=Beauty.BeautifyStatement(ProcHeader,IntfIndent);
ProcHeader,IntfIndent);
{$IFDEF CTDebug} {$IFDEF CTDebug}
DebugLn('TExtractProcTool.InsertProcIntf END ProcHeader="',ProcHeader,'"'); DebugLn('TExtractProcTool.InsertProcIntf END ProcHeader="',ProcHeader,'"');
{$ENDIF} {$ENDIF}
@ -931,7 +926,7 @@ var
le: String; le: String;
ProcHeader: String; ProcHeader: String;
begin begin
le:=SourceChangeCache.BeautifyCodeOptions.LineEnd; le:=Beauty.LineEnd;
if ResultNode=nil then if ResultNode=nil then
ProcHeader:='procedure ' ProcHeader:='procedure '
else else
@ -939,8 +934,7 @@ var
if ProcClassName<>'' then if ProcClassName<>'' then
ProcHeader:=ProcHeader+ProcClassName+'.'; ProcHeader:=ProcHeader+ProcClassName+'.';
ProcHeader:=ProcHeader+ProcName+ParamList+';'+le; ProcHeader:=ProcHeader+ProcName+ParamList+';'+le;
ProcHeader:=SourceChangeCache.BeautifyCodeOptions.BeautifyStatement( ProcHeader:=Beauty.BeautifyStatement(ProcHeader,0);
ProcHeader,0);
ProcCode:=ProcHeader+VarSection+BeginEndCode; ProcCode:=ProcHeader+VarSection+BeginEndCode;
Result:=true; Result:=true;
end; end;
@ -958,7 +952,7 @@ var
if not CreateMissingProcBodies then if not CreateMissingProcBodies then
RaiseException(ctsErrorDuringCreationOfNewProcBodies); RaiseException(ctsErrorDuringCreationOfNewProcBodies);
end else begin end else begin
TabWidth:=SourceChangeCache.BeautifyCodeOptions.TabWidth; TabWidth:=Beauty.TabWidth;
IndentText(ProcCode,Indent,TabWidth,IndentedProcCode); IndentText(ProcCode,Indent,TabWidth,IndentedProcCode);
{$IFDEF CTDebug} {$IFDEF CTDebug}
DebugLn('TExtractProcTool.InsertProcBody END ProcCode="',ProcCode,'"'); DebugLn('TExtractProcTool.InsertProcBody END ProcCode="',ProcCode,'"');
@ -1044,6 +1038,7 @@ begin
if (not SubProcSameLvlPossible) and (ProcType=eptSubProcedureSameLvl) then if (not SubProcSameLvlPossible) and (ProcType=eptSubProcedureSameLvl) then
exit; exit;
CodeCompleteSrcChgCache:=SourceChangeCache; CodeCompleteSrcChgCache:=SourceChangeCache;
Beauty:=SourceChangeCache.BeautifyCodeOptions;
VarTree:=CreateExtractProcVariableTree; VarTree:=CreateExtractProcVariableTree;
NewProcPath:=nil; NewProcPath:=nil;
@ -1092,6 +1087,7 @@ var
WithIdentifiers: TAVLTree; // identifiers to change WithIdentifiers: TAVLTree; // identifiers to change
WithVarCache: TFPList; // list of PWithVarCache WithVarCache: TFPList; // list of PWithVarCache
WithVarEndPos: LongInt; WithVarEndPos: LongInt;
Beauty: TBeautifyCodeOptions;
procedure AddIdentifier(CleanPos: integer); procedure AddIdentifier(CleanPos: integer);
var var
@ -1270,7 +1266,7 @@ var
end; end;
until (CurPos.StartPos>SrcLen) or (CurPos.StartPos>StatementNode.EndPos); until (CurPos.StartPos>SrcLen) or (CurPos.StartPos>StatementNode.EndPos);
IndentWith:=GetLineIndentWithTabs(Src,WithKeywordStartPos, IndentWith:=GetLineIndentWithTabs(Src,WithKeywordStartPos,
SourceChangeCache.BeautifyCodeOptions.TabWidth); Beauty.TabWidth);
WithKeywordStartPos:=FindLineEndOrCodeInFrontOfPosition(WithKeywordStartPos); WithKeywordStartPos:=FindLineEndOrCodeInFrontOfPosition(WithKeywordStartPos);
DoKeywordEndPos:=FindLineEndOrCodeAfterPosition(DoKeywordEndPos); DoKeywordEndPos:=FindLineEndOrCodeAfterPosition(DoKeywordEndPos);
if not SourceChangeCache.Replace(gtSpace,gtNone,WithKeywordStartPos,DoKeywordEndPos,'') if not SourceChangeCache.Replace(gtSpace,gtNone,WithKeywordStartPos,DoKeywordEndPos,'')
@ -1285,8 +1281,7 @@ var
// unindent // unindent
StartPos:=FindLineEndOrCodeAfterPosition(DoKeywordEndPos,true,true); StartPos:=FindLineEndOrCodeAfterPosition(DoKeywordEndPos,true,true);
IndentInnerWith:=GetLineIndentWithTabs(Src,StartPos, IndentInnerWith:=GetLineIndentWithTabs(Src,StartPos,Beauty.TabWidth);
SourceChangeCache.BeautifyCodeOptions.TabWidth);
if IndentWith<IndentInnerWith then if IndentWith<IndentInnerWith then
if not SourceChangeCache.IndentBlock(DoKeywordEndPos,EndKeywordStartPos, if not SourceChangeCache.IndentBlock(DoKeywordEndPos,EndKeywordStartPos,
IndentWith-IndentInnerWith) IndentWith-IndentInnerWith)
@ -1353,6 +1348,7 @@ begin
debugln(['TExtractProcTool.RemoveWithBlock missing statement']); debugln(['TExtractProcTool.RemoveWithBlock missing statement']);
exit; exit;
end; end;
Beauty:=SourceChangeCache.BeautifyCodeOptions;
// parse block // parse block
WithVarEndPos:=FindEndOfTerm(WithVarNode.StartPos,false,true); WithVarEndPos:=FindEndOfTerm(WithVarNode.StartPos,false,true);
MoveCursorToCleanPos(WithVarEndPos); MoveCursorToCleanPos(WithVarEndPos);
@ -1394,6 +1390,7 @@ var
CleanStartPos: integer; CleanStartPos: integer;
CleanEndPos: integer; CleanEndPos: integer;
StartNode: TCodeTreeNode; StartNode: TCodeTreeNode;
Beauty: TBeautifyCodeOptions;
function Add(IdentifierStart, IdentifierEnd: integer; function Add(IdentifierStart, IdentifierEnd: integer;
const Identifier: string): boolean; const Identifier: string): boolean;
@ -1504,25 +1501,26 @@ begin
// ToDo: check if identifiers are variables // ToDo: check if identifiers are variables
Beauty:=SourceChangeCache.BeautifyCodeOptions;
if WithExpr<>'' then begin if WithExpr<>'' then begin
// add 'with expr do begin' // add 'with expr do begin'
Indent:=GetLineIndentWithTabs(Src,CleanStartPos, Indent:=GetLineIndentWithTabs(Src,CleanStartPos,
SourceChangeCache.BeautifyCodeOptions.TabWidth); Beauty.TabWidth);
Code:='with '+WithExpr+' do begin'; Code:='with '+WithExpr+' do begin';
Code:=SourceChangeCache.BeautifyCodeOptions.BeautifyStatement(Code,Indent); Code:=Beauty.BeautifyStatement(Code,Indent);
//debugln(['TExtractProcTool.AddWithBlock Header=',Code]); //debugln(['TExtractProcTool.AddWithBlock Header=',Code]);
if not SourceChangeCache.Replace(gtNewLine,gtNewLine, if not SourceChangeCache.Replace(gtNewLine,gtNewLine,
CleanStartPos,CleanStartPos,Code) then exit; CleanStartPos,CleanStartPos,Code) then exit;
// add 'end;' // add 'end;'
Code:='end;'; Code:='end;';
Code:=SourceChangeCache.BeautifyCodeOptions.BeautifyStatement(Code,Indent); Code:=Beauty.BeautifyStatement(Code,Indent);
//debugln(['TExtractProcTool.AddWithBlock Footer=',Code]); //debugln(['TExtractProcTool.AddWithBlock Footer=',Code]);
if not SourceChangeCache.Replace(gtNewLine,gtNewLine, if not SourceChangeCache.Replace(gtNewLine,gtNewLine,
CleanEndPos,CleanEndPos,Code) then exit; CleanEndPos,CleanEndPos,Code) then exit;
// indent all between // indent all between
//debugln(['TExtractProcTool.AddWithBlock Indent...']); //debugln(['TExtractProcTool.AddWithBlock Indent...']);
if not SourceChangeCache.IndentBlock(CleanStartPos,CleanEndPos, if not SourceChangeCache.IndentBlock(CleanStartPos,CleanEndPos,
SourceChangeCache.BeautifyCodeOptions.Indent) then exit; Beauty.Indent) then exit;
//debugln(['TExtractProcTool.AddWithBlock Apply']); //debugln(['TExtractProcTool.AddWithBlock Apply']);
if not SourceChangeCache.Apply then exit; if not SourceChangeCache.Apply then exit;
end; end;