codetools: added function to remove empty method bodies

git-svn-id: trunk@14970 -
This commit is contained in:
mattias 2008-04-25 12:35:55 +00:00
parent cc954306a9
commit c0fef1b31e
3 changed files with 79 additions and 20 deletions

View File

@ -242,13 +242,16 @@ type
procedure WriteCodeGraphDebugReport(Graph: TCodeGraph);
function FindEmptyMethods(CursorPos: TCodeXYPosition;
const Sections: TPascalClassSections;
ListOfPCodeXYPosition: TFPList): boolean;
ListOfPCodeXYPosition: TFPList;
out AllEmpty: boolean): boolean;
function FindEmptyMethods(CursorPos: TCodeXYPosition;
const Sections: TPascalClassSections;
CodeTreeNodeExtensions: TAVLTree): boolean;
CodeTreeNodeExtensions: TAVLTree;
out AllEmpty: boolean): boolean;
function RemoveEmptyMethods(CursorPos: TCodeXYPosition;
const Sections: TPascalClassSections;
SourceChangeCache: TSourceChangeCache): boolean;
SourceChangeCache: TSourceChangeCache;
out AllRemoved: boolean): boolean;
// custom class completion
function InitClassCompletion(const UpperClassName: string;
@ -4010,8 +4013,8 @@ begin
end;
function TCodeCompletionCodeTool.FindEmptyMethods(CursorPos: TCodeXYPosition;
const Sections: TPascalClassSections; ListOfPCodeXYPosition: TFPList
): boolean;
const Sections: TPascalClassSections; ListOfPCodeXYPosition: TFPList;
out AllEmpty: boolean): boolean;
var
ProcBodyNodes: TAVLTree;
AVLNode: TAVLTreeNode;
@ -4022,7 +4025,7 @@ begin
Result:=false;
ProcBodyNodes:=TAVLTree.Create(@CompareCodeTreeNodeExt);
try
Result:=FindEmptyMethods(CursorPos,Sections,ProcBodyNodes);
Result:=FindEmptyMethods(CursorPos,Sections,ProcBodyNodes,AllEmpty);
if Result then begin
AVLNode:=ProcBodyNodes.FindLowest;
while AVLNode<>nil do begin
@ -4043,8 +4046,8 @@ begin
end;
function TCodeCompletionCodeTool.FindEmptyMethods(CursorPos: TCodeXYPosition;
const Sections: TPascalClassSections; CodeTreeNodeExtensions: TAVLTree
): boolean;
const Sections: TPascalClassSections; CodeTreeNodeExtensions: TAVLTree;
out AllEmpty: boolean): boolean;
var
CleanCursorPos: integer;
CursorNode: TCodeTreeNode;
@ -4071,6 +4074,7 @@ var
begin
Result:=false;
AllEmpty:=false;
BuildTreeAndGetCleanPos(trAll,CursorPos,CleanCursorPos,[]);
CursorNode:=FindDeepestNodeAtPos(CleanCursorPos,true);
CodeCompleteClassNode:=FindClassNode(CursorNode);
@ -4116,6 +4120,7 @@ begin
end;
AVLNode:=NextAVLNode;
end;
AllEmpty:=ProcBodyNodes.Count=0;
Result:=true;
finally
if ClassProcs<>nil then begin
@ -4130,11 +4135,52 @@ begin
end;
function TCodeCompletionCodeTool.RemoveEmptyMethods(CursorPos: TCodeXYPosition;
const Sections: TPascalClassSections; SourceChangeCache: TSourceChangeCache
): boolean;
const Sections: TPascalClassSections; SourceChangeCache: TSourceChangeCache;
out AllRemoved: boolean): boolean;
var
ProcBodyNodes: TAVLTree;
AVLNode: TAVLTreeNode;
NodeExt: TCodeTreeNodeExtension;
FirstNodeExt: TCodeTreeNodeExtension;
LastNodeExt: TCodeTreeNodeExtension;
FromPos: LongInt;
ToPos: LongInt;
begin
Result:=false;
// ToDo
AllRemoved:=false;
if (SourceChangeCache=nil) or (Scanner=nil) then exit;
SourceChangeCache.MainScanner:=Scanner;
ProcBodyNodes:=TAVLTree.Create(@CompareCodeTreeNodeExt);
try
Result:=FindEmptyMethods(CursorPos,Sections,ProcBodyNodes,AllRemoved);
if Result and (ProcBodyNodes<>nil) and (ProcBodyNodes.Count>0) then begin
// sort the nodes for position
ProcBodyNodes.OnCompare:=@CompareCodeTreeNodeExtWithPos;
AVLNode:=ProcBodyNodes.FindLowest;
while AVLNode<>nil do begin
// gather a group of continuous proc nodes
FirstNodeExt:=TCodeTreeNodeExtension(AVLNode.Data);
LastNodeExt:=FirstNodeExt;
AVLNode:=ProcBodyNodes.FindSuccessor(AVLNode);
while (AVLNode<>nil) do begin
NodeExt:=TCodeTreeNodeExtension(AVLNode.Data);
if NodeExt.Node<>LastNodeExt.Node.NextBrother then break;
LastNodeExt:=NodeExt;
AVLNode:=ProcBodyNodes.FindSuccessor(AVLNode);
end;
// delete group
FromPos:=FindLineEndOrCodeInFrontOfPosition(FirstNodeExt.Node.StartPos,true);
ToPos:=FindLineEndOrCodeAfterPosition(LastNodeExt.Node.EndPos,AllRemoved);
if not SourceChangeCache.Replace(gtNone,gtNone,FromPos,ToPos,'')
then
exit;
end;
end;
Result:=SourceChangeCache.Apply;
finally
ProcBodyNodes.FreeAndClear;
ProcBodyNodes.Free;
end;
end;
function TCodeCompletionCodeTool.InitClassCompletion(

View File

@ -474,9 +474,11 @@ type
function FixForwardDefinitions(Code: TCodeBuffer): boolean;
function FindEmptyMethods(Code: TCodeBuffer; X,Y: integer;
const Sections: TPascalClassSections;
ListOfPCodeXYPosition: TFPList): boolean;
ListOfPCodeXYPosition: TFPList;
out AllEmpty: boolean): boolean;
function RemoveEmptyMethods(Code: TCodeBuffer; X,Y: integer;
const Sections: TPascalClassSections): boolean;
const Sections: TPascalClassSections;
out AllRemoved: boolean): boolean;
// custom class completion
function InitClassCompletion(Code: TCodeBuffer;
@ -3318,8 +3320,8 @@ begin
end;
function TCodeToolManager.FindEmptyMethods(Code: TCodeBuffer; X, Y: integer;
const Sections: TPascalClassSections; ListOfPCodeXYPosition: TFPList
): boolean;
const Sections: TPascalClassSections; ListOfPCodeXYPosition: TFPList;
out AllEmpty: boolean): boolean;
var
CursorPos: TCodeXYPosition;
begin
@ -3333,14 +3335,14 @@ begin
CursorPos.Code:=Code;
try
Result:=FCurCodeTool.FindEmptyMethods(CursorPos,Sections,
ListOfPCodeXYPosition);
ListOfPCodeXYPosition,AllEmpty);
except
on e: Exception do Result:=HandleException(e);
end;
end;
function TCodeToolManager.RemoveEmptyMethods(Code: TCodeBuffer; X,Y: integer;
const Sections: TPascalClassSections): boolean;
const Sections: TPascalClassSections; out AllRemoved: boolean): boolean;
var
CursorPos: TCodeXYPosition;
begin
@ -3353,7 +3355,8 @@ begin
CursorPos.Y:=Y;
CursorPos.Code:=Code;
try
Result:=FCurCodeTool.RemoveEmptyMethods(CursorPos,Sections,SourceChangeCache);
Result:=FCurCodeTool.RemoveEmptyMethods(CursorPos,Sections,
SourceChangeCache,AllRemoved);
except
on e: Exception do Result:=HandleException(e);
end;

View File

@ -42,6 +42,7 @@ var
ListOfPCodeXYPosition: TFPList;
i: Integer;
P: PCodeXYPosition;
All: boolean;
begin
if (ParamCount>=1) and (Paramcount<>3) then begin
writeln('Usage:');
@ -70,13 +71,22 @@ begin
// complete code
ListOfPCodeXYPosition:=TFPList.Create;
if CodeToolBoss.FindEmptyMethods(Code,X,Y,[pcsPublished],
ListOfPCodeXYPosition)
ListOfPCodeXYPosition,All)
then begin
writeln('Found ',ListOfPCodeXYPosition.Count,' empty methods:');
writeln('Found ',ListOfPCodeXYPosition.Count,' empty methods (All=',All,'):');
for i:=0 to ListOfPCodeXYPosition.Count-1 do begin
P:=PCodeXYPosition(ListOfPCodeXYPosition[i]);
writeln(i,' ',DbgsCXY(P^));
end;
if CodeToolBoss.RemoveEmptyMethods(Code,X,Y,[pcsPublished],All)
then begin
writeln('Empty methods removed:');
writeln('=========================');
writeln(Code.Source);
writeln('=========================');
end else begin
writeln('RemoveEmptyMethods failed: ',CodeToolBoss.ErrorMessage);
end;
end else begin
writeln('FindEmptyMethods failed: ',CodeToolBoss.ErrorMessage);
end;