codetools: local var completion: group vars with same type, from Ondrej Pokorny

git-svn-id: trunk@49840 -
This commit is contained in:
mattias 2015-09-16 12:06:18 +00:00
parent cf3d8e35ba
commit 6e84cb390d
6 changed files with 138 additions and 40 deletions

View File

@ -1050,6 +1050,16 @@ function TCodeCompletionCodeTool.AddLocalVariable(CleanCursorPos: integer;
CleanLevelPos: integer): boolean;
// if CleanLevelPos<1 then CleanLevelPos:=CleanCursorPos
// CleanLevelPos selects the target node, e.g. a ctnProcedure
function FindFirstVarDeclaration(var Node: TCodeTreeNode): TCodeTreeNode;
begin
Result := Node;
while Assigned(Result.PriorBrother) and (Result.PriorBrother.Desc = ctnVarDefinition) and
not Assigned(Result.PriorBrother.LastChild)
do
Result := Result.PriorBrother;
end;
var
CursorNode, VarSectionNode, VarNode: TCodeTreeNode;
Indent, InsertPos: integer;
@ -1060,6 +1070,10 @@ var
OtherSectionNode: TCodeTreeNode;
HeaderNode: TCodeTreeNode;
Beauty: TBeautifyCodeOptions;
VarTypeNode: TCodeTreeNode;
InsertVarLineStart: integer;
InsertVarLineEnd: integer;
InsertAsNewLine: Boolean;
begin
Result:=false;
if CleanLevelPos<1 then CleanLevelPos:=CleanCursorPos;
@ -1127,21 +1141,70 @@ begin
//DebugLn(['TCodeCompletionCodeTool.AddLocalVariable C InsertTxt="',InsertTxt,'" ParentNode=',ParentNode.DescAsString,' HeaderNode=',HeaderNode.DescAsString,' OtherSectionNode=',OtherSectionNode.DescAsString,' VarSectionNode=',VarSectionNode.DescAsString,' CursorNode=',CursorNode.DescAsString]);
end;
InsertAsNewLine := True;
if (VarSectionNode<>nil) then begin
// there is already a var section
// -> append variable
//debugln(['TCodeCompletionCodeTool.AddLocalVariable insert into existing var section']);
VarNode:=VarSectionNode.LastChild;
if VarNode<>nil then begin
Indent:=Beauty.GetLineIndent(Src,VarNode.StartPos);
if PositionsInSameLine(Src,VarSectionNode.StartPos,VarNode.StartPos) then
// there is already a var section
// -> first check if variables with the same type are defined (search backwards)
VarTypeNode := nil;
if Beauty.GroupLocalVariables then
begin
VarNode:=VarSectionNode.LastChild;
while Assigned(VarNode) and not Assigned(VarTypeNode) do
begin
if (VarNode.Desc = ctnVarDefinition) and Assigned(VarNode.LastChild) and
(VarNode.LastChild.Desc = ctnIdentifier) and
(CompareIdentifiers(PChar(VariableType), PChar(ExtractNode(VarNode.LastChild,[phpCommentsToSpace]))) = 0)
then
VarTypeNode := VarNode;
VarNode := VarNode.PriorBrother;
end;
end;
if Assigned(VarTypeNode) then
begin
// -> append variable to already defined line
VarNode := FindFirstVarDeclaration(VarTypeNode);//find starting indentation
Indent:=Beauty.GetLineIndent(Src,VarTypeNode.StartPos);
if PositionsInSameLine(Src,VarTypeNode.StartPos,VarNode.StartPos) then
inc(Indent,Beauty.Indent);
InsertPos:=FindLineEndOrCodeAfterPosition(VarNode.EndPos);
end else begin
Indent:=Beauty.GetLineIndent(Src,VarSectionNode.StartPos);
MoveCursorToNodeStart(VarSectionNode);
ReadNextAtom;
InsertPos:=CurPos.EndPos;
MoveCursorToNodeStart(VarTypeNode.LastChild);
ReadPriorAtom;
if CurPos.Flag = cafColon then
begin
InsertPos:=CurPos.StartPos;
GetLineStartEndAtPosition(Src, InsertPos, InsertVarLineStart, InsertVarLineEnd);
InsertTxt:=VariableName;
if InsertPos-InsertVarLineStart+Length(VariableName)+2 > Beauty.LineLength then//the variable name doesn't fit into the line
InsertTxt := Beauty.LineEnd + Beauty.GetIndentStr(Indent) + InsertTxt
else if InsertVarLineEnd-InsertVarLineStart+Length(VariableName)+2 > Beauty.LineLength then//the variable type doesn't fit into the line
begin
if atColon in Beauty.DoNotSplitLineInFront then
InsertTxt := Beauty.LineEnd + Beauty.GetIndentStr(Indent) + InsertTxt
else
InsertTxt := InsertTxt + Beauty.LineEnd + Beauty.GetIndentStr(Indent);
end;
InsertTxt:=','+InsertTxt;
Indent := 0;
InsertAsNewLine := False;
end else
VarTypeNode := nil;//error: colon not found, insert as new line
end;
if not Assigned(VarTypeNode) then
begin
// -> append variable to new line
VarNode:=VarSectionNode.LastChild;
if VarNode<>nil then begin
InsertPos:=FindLineEndOrCodeAfterPosition(VarNode.EndPos);
VarNode := FindFirstVarDeclaration(VarNode);//find indentation of first var definition
Indent:=Beauty.GetLineIndent(Src,VarNode.StartPos);
if PositionsInSameLine(Src,VarSectionNode.StartPos,VarNode.StartPos) then
inc(Indent,Beauty.Indent);
end else begin
Indent:=Beauty.GetLineIndent(Src,VarSectionNode.StartPos)+Beauty.Indent;
MoveCursorToNodeStart(VarSectionNode);
ReadNextAtom;
InsertPos:=CurPos.EndPos;
end;
end;
end else begin
// there is no var section yet
@ -1183,11 +1246,14 @@ begin
InsertTxt:='var'+Beauty.LineEnd
+Beauty.GetIndentStr(Indent+Beauty.Indent)+InsertTxt;
end;
// insert new code
InsertTxt:=Beauty.BeautifyStatement(InsertTxt,Indent);
//DebugLn('TCodeCompletionCodeTool.AddLocalVariable E ',InsertTxt,' ');
SourceChangeCache.Replace(gtNewLine,gtNewLine,InsertPos,InsertPos,InsertTxt);
if InsertAsNewLine then
SourceChangeCache.Replace(gtNewLine,gtNewLine,InsertPos,InsertPos,InsertTxt)
else
SourceChangeCache.Replace(gtNone,gtNone,InsertPos,InsertPos,InsertTxt);
if (VariableTypeUnitName<>'')
and (not IsHiddenUsedUnit(PChar(VariableTypeUnitName))) then begin

View File

@ -150,6 +150,7 @@ type
KeepForwardProcOrder: boolean;
UpdateMultiProcSignatures: boolean;
UpdateOtherProcSignaturesCase: boolean; // when updating proc signatures not under cursor, fix case
GroupLocalVariables: boolean;
// classes, methods, properties
ClassHeaderComments: boolean;
ClassImplementationComments: boolean;
@ -1256,6 +1257,7 @@ begin
UpdateAllMethodSignatures:=true;
UpdateMultiProcSignatures:=true;
UpdateOtherProcSignaturesCase:=true;
GroupLocalVariables:=true;
MethodInsertPolicy:=mipClassOrder;
ForwardProcBodyInsertPolicy:=fpipBehindMethods;
KeepForwardProcOrder:=true;

View File

@ -87,6 +87,7 @@ type
FUpdateAllMethodSignatures: boolean;
FUpdateMultiProcSignatures: boolean;
FUpdateOtherProcSignaturesCase: boolean;
FGroupLocalVariables: boolean;
FWordPolicyExceptions: TStringList;
FDoNotSplitLineInFront: TAtomTypes;
FDoNotSplitLineAfter: TAtomTypes;
@ -178,6 +179,8 @@ type
read FUpdateMultiProcSignatures write FUpdateMultiProcSignatures;
property UpdateOtherProcSignaturesCase: boolean
read FUpdateOtherProcSignaturesCase write FUpdateOtherProcSignaturesCase;
property GroupLocalVariables: boolean
read FGroupLocalVariables write FGroupLocalVariables;
property ClassHeaderComments: boolean
read FClassHeaderComments write FClassHeaderComments;
property ClassImplementationComments: boolean
@ -446,6 +449,8 @@ begin
'CodeToolsOptions/UpdateMultiProcSignatures/Value',true);
FUpdateOtherProcSignaturesCase:=XMLConfig.GetValue(
'CodeToolsOptions/UpdateOtherProcSignaturesCase/Value',true);
FGroupLocalVariables:=XMLConfig.GetValue(
'CodeToolsOptions/GroupLocalVariables/Value',true);
FClassHeaderComments:=XMLConfig.GetValue(
'CodeToolsOptions/ClassHeaderComments/Value',true);
FClassImplementationComments:=XMLConfig.GetValue(
@ -600,6 +605,9 @@ begin
XMLConfig.SetDeleteValue(
'CodeToolsOptions/UpdateOtherProcSignaturesCase/Value',FUpdateOtherProcSignaturesCase,
true);
XMLConfig.SetDeleteValue(
'CodeToolsOptions/GroupLocalVariables/Value',FGroupLocalVariables,
true);
XMLConfig.SetDeleteValue(
'CodeToolsOptions/ClassImplementationComments/Value',
FClassImplementationComments,true);
@ -767,6 +775,7 @@ begin
FKeepForwardProcOrder:=CodeToolsOpts.KeepForwardProcOrder;
FUpdateMultiProcSignatures:=CodeToolsOpts.UpdateMultiProcSignatures;
FUpdateOtherProcSignaturesCase:=CodeToolsOpts.UpdateOtherProcSignaturesCase;
FGroupLocalVariables:=CodeToolsOpts.GroupLocalVariables;
FClassHeaderComments:=CodeToolsOpts.ClassHeaderComments;
FClassImplementationComments:=CodeToolsOpts.ClassImplementationComments;
FMethodInsertPolicy:=CodeToolsOpts.FMethodInsertPolicy;
@ -826,6 +835,7 @@ begin
FKeepForwardProcOrder:=true;
FUpdateMultiProcSignatures:=true;
FUpdateOtherProcSignaturesCase:=true;
FGroupLocalVariables:=true;
FClassHeaderComments:=true;
FClassImplementationComments:=true;
FMethodInsertPolicy:=mipClassOrder;
@ -903,6 +913,7 @@ begin
and (FKeepForwardProcOrder=CodeToolsOpts.KeepForwardProcOrder)
and (FUpdateMultiProcSignatures=CodeToolsOpts.UpdateMultiProcSignatures)
and (FUpdateOtherProcSignaturesCase=CodeToolsOpts.UpdateOtherProcSignaturesCase)
and (FGroupLocalVariables=CodeToolsOpts.GroupLocalVariables)
and (FClassHeaderComments=CodeToolsOpts.ClassHeaderComments)
and (FClassImplementationComments=CodeToolsOpts.ClassImplementationComments)
and (FMethodInsertPolicy=CodeToolsOpts.FMethodInsertPolicy)
@ -1022,6 +1033,7 @@ begin
Beauty.KeepForwardProcOrder:=KeepForwardProcOrder;
Beauty.UpdateMultiProcSignatures:=UpdateMultiProcSignatures;
Beauty.UpdateOtherProcSignaturesCase:=UpdateOtherProcSignaturesCase;
Beauty.GroupLocalVariables:=GroupLocalVariables;
Beauty.ClassHeaderComments:=ClassHeaderComments;
Beauty.ClassImplementationComments:=ClassImplementationComments;
Beauty.MethodInsertPolicy:=MethodInsertPolicy;

View File

@ -7,8 +7,8 @@ object CodetoolsCodeCreationOptionsFrame: TCodetoolsCodeCreationOptionsFrame
ClientWidth = 566
TabOrder = 0
Visible = False
DesignLeft = 211
DesignTop = 209
DesignLeft = 322
DesignTop = 184
object ForwardProcsInsertPolicyRadioGroup: TRadioGroup
AnchorSideLeft.Control = Owner
AnchorSideTop.Control = Owner
@ -37,9 +37,9 @@ object CodetoolsCodeCreationOptionsFrame: TCodetoolsCodeCreationOptionsFrame
AnchorSideTop.Control = ForwardProcsInsertPolicyRadioGroup
AnchorSideTop.Side = asrBottom
Left = 6
Height = 18
Height = 19
Top = 112
Width = 233
Width = 200
BorderSpacing.Around = 6
Caption = 'ForwardProcsKeepOrderCheckBox'
TabOrder = 1
@ -52,7 +52,7 @@ object CodetoolsCodeCreationOptionsFrame: TCodetoolsCodeCreationOptionsFrame
AnchorSideRight.Side = asrBottom
Left = 6
Height = 100
Top = 142
Top = 143
Width = 554
Anchors = [akTop, akLeft, akRight]
AutoFill = True
@ -76,9 +76,9 @@ object CodetoolsCodeCreationOptionsFrame: TCodetoolsCodeCreationOptionsFrame
AnchorSideTop.Control = UsesInsertPolicyRadioGroup
AnchorSideTop.Side = asrBottom
Left = 6
Height = 18
Top = 248
Width = 251
Height = 19
Top = 249
Width = 217
BorderSpacing.Left = 6
BorderSpacing.Top = 6
Caption = 'UpdateMultiProcSignaturesCheckBox'
@ -89,37 +89,37 @@ object CodetoolsCodeCreationOptionsFrame: TCodetoolsCodeCreationOptionsFrame
AnchorSideTop.Control = UpdateMultiProcSignaturesCheckBox
AnchorSideTop.Side = asrBottom
Left = 6
Height = 18
Top = 266
Width = 284
Height = 19
Top = 268
Width = 244
BorderSpacing.Left = 6
Caption = 'UpdateOtherProcSignaturesCaseCheckBox'
TabOrder = 4
end
object TemplateFileLabel: TLabel
AnchorSideLeft.Control = UpdateOtherProcSignaturesCaseCheckBox
AnchorSideLeft.Control = GroupLocalVariablesCheckBox
AnchorSideTop.Control = TemplateFileEdit
AnchorSideTop.Side = asrCenter
Left = 6
Height = 16
Top = 293
Width = 114
Height = 15
Top = 316
Width = 95
Caption = 'TemplateFileLabel'
ParentColor = False
end
object TemplateFileEdit: TEdit
AnchorSideLeft.Control = TemplateFileLabel
AnchorSideLeft.Side = asrBottom
AnchorSideTop.Control = UpdateOtherProcSignaturesCaseCheckBox
AnchorSideTop.Control = GroupLocalVariablesCheckBox
AnchorSideTop.Side = asrBottom
AnchorSideRight.Control = TemplateFileBrowseButton
Left = 120
Height = 22
Top = 290
Width = 370
Left = 101
Height = 23
Top = 312
Width = 424
Anchors = [akTop, akLeft, akRight]
BorderSpacing.Top = 6
TabOrder = 5
TabOrder = 6
Text = 'TemplateFileEdit'
end
object TemplateFileBrowseButton: TButton
@ -128,15 +128,27 @@ object CodetoolsCodeCreationOptionsFrame: TCodetoolsCodeCreationOptionsFrame
AnchorSideRight.Side = asrBottom
AnchorSideBottom.Control = TemplateFileEdit
AnchorSideBottom.Side = asrBottom
Left = 490
Height = 22
Top = 290
Width = 70
Left = 525
Height = 23
Top = 312
Width = 35
Anchors = [akTop, akRight, akBottom]
AutoSize = True
BorderSpacing.Right = 6
Caption = '...'
OnClick = TemplateFileBrowseButtonClick
TabOrder = 6
TabOrder = 7
end
object GroupLocalVariablesCheckBox: TCheckBox
AnchorSideLeft.Control = Owner
AnchorSideTop.Control = UpdateOtherProcSignaturesCaseCheckBox
AnchorSideTop.Side = asrBottom
Left = 6
Height = 19
Top = 287
Width = 180
BorderSpacing.Left = 6
Caption = 'GroupLocalVariablesCheckBox'
TabOrder = 5
end
end

View File

@ -41,6 +41,7 @@ type
TemplateFileLabel: TLabel;
UpdateMultiProcSignaturesCheckBox: TCheckBox;
UpdateOtherProcSignaturesCaseCheckBox: TCheckBox;
GroupLocalVariablesCheckBox: TCheckBox;
UsesInsertPolicyRadioGroup: TRadioGroup;
procedure TemplateFileBrowseButtonClick(Sender: TObject);
private
@ -116,6 +117,8 @@ begin
lisCTOUpdateMultipleProcedureSignatures;
UpdateOtherProcSignaturesCaseCheckBox.Caption:=
lisUpdateOtherProcedureSignaturesWhenOnlyLetterCaseHa;
GroupLocalVariablesCheckBox.Caption:=
lisGroupLocalVariables;
TemplateFileLabel.Caption:=lisTemplateFile;
{$IFNDEF EnableCodeCompleteTemplates}
@ -152,6 +155,7 @@ begin
UpdateMultiProcSignaturesCheckBox.Checked:=UpdateMultiProcSignatures;
UpdateOtherProcSignaturesCaseCheckBox.Checked:=UpdateOtherProcSignaturesCase;
GroupLocalVariablesCheckBox.Checked:=GroupLocalVariables;
TemplateFileEdit.Text:=CodeCompletionTemplateFileName;
end;
@ -180,6 +184,7 @@ begin
UpdateMultiProcSignatures:=UpdateMultiProcSignaturesCheckBox.Checked;
UpdateOtherProcSignaturesCase:=UpdateOtherProcSignaturesCaseCheckBox.Checked;
GroupLocalVariables:=GroupLocalVariablesCheckBox.Checked;
CodeCompletionTemplateFileName:=TemplateFileEdit.Text;
end;

View File

@ -5999,6 +5999,7 @@ resourcestring
lisUDUnits2 = 'Units: %s';
lisCTOUpdateAllMethodSignatures = 'Update all method signatures';
lisCTOUpdateMultipleProcedureSignatures = 'Update multiple procedure signatures';
lisGroupLocalVariables = 'Group automatically defined local variables';
lisUpdateOtherProcedureSignaturesWhenOnlyLetterCaseHa = 'Update other '
+'procedure signatures when only letter case has changed';
lisTemplateFile = 'Template file';