mirror of
https://gitlab.com/freepascal.org/lazarus/lazarus.git
synced 2025-11-08 15:19:30 +01:00
codetools: added function to remove empty method bodies
git-svn-id: trunk@14970 -
This commit is contained in:
parent
cc954306a9
commit
c0fef1b31e
@ -242,13 +242,16 @@ type
|
|||||||
procedure WriteCodeGraphDebugReport(Graph: TCodeGraph);
|
procedure WriteCodeGraphDebugReport(Graph: TCodeGraph);
|
||||||
function FindEmptyMethods(CursorPos: TCodeXYPosition;
|
function FindEmptyMethods(CursorPos: TCodeXYPosition;
|
||||||
const Sections: TPascalClassSections;
|
const Sections: TPascalClassSections;
|
||||||
ListOfPCodeXYPosition: TFPList): boolean;
|
ListOfPCodeXYPosition: TFPList;
|
||||||
|
out AllEmpty: boolean): boolean;
|
||||||
function FindEmptyMethods(CursorPos: TCodeXYPosition;
|
function FindEmptyMethods(CursorPos: TCodeXYPosition;
|
||||||
const Sections: TPascalClassSections;
|
const Sections: TPascalClassSections;
|
||||||
CodeTreeNodeExtensions: TAVLTree): boolean;
|
CodeTreeNodeExtensions: TAVLTree;
|
||||||
|
out AllEmpty: boolean): boolean;
|
||||||
function RemoveEmptyMethods(CursorPos: TCodeXYPosition;
|
function RemoveEmptyMethods(CursorPos: TCodeXYPosition;
|
||||||
const Sections: TPascalClassSections;
|
const Sections: TPascalClassSections;
|
||||||
SourceChangeCache: TSourceChangeCache): boolean;
|
SourceChangeCache: TSourceChangeCache;
|
||||||
|
out AllRemoved: boolean): boolean;
|
||||||
|
|
||||||
// custom class completion
|
// custom class completion
|
||||||
function InitClassCompletion(const UpperClassName: string;
|
function InitClassCompletion(const UpperClassName: string;
|
||||||
@ -4010,8 +4013,8 @@ begin
|
|||||||
end;
|
end;
|
||||||
|
|
||||||
function TCodeCompletionCodeTool.FindEmptyMethods(CursorPos: TCodeXYPosition;
|
function TCodeCompletionCodeTool.FindEmptyMethods(CursorPos: TCodeXYPosition;
|
||||||
const Sections: TPascalClassSections; ListOfPCodeXYPosition: TFPList
|
const Sections: TPascalClassSections; ListOfPCodeXYPosition: TFPList;
|
||||||
): boolean;
|
out AllEmpty: boolean): boolean;
|
||||||
var
|
var
|
||||||
ProcBodyNodes: TAVLTree;
|
ProcBodyNodes: TAVLTree;
|
||||||
AVLNode: TAVLTreeNode;
|
AVLNode: TAVLTreeNode;
|
||||||
@ -4022,7 +4025,7 @@ begin
|
|||||||
Result:=false;
|
Result:=false;
|
||||||
ProcBodyNodes:=TAVLTree.Create(@CompareCodeTreeNodeExt);
|
ProcBodyNodes:=TAVLTree.Create(@CompareCodeTreeNodeExt);
|
||||||
try
|
try
|
||||||
Result:=FindEmptyMethods(CursorPos,Sections,ProcBodyNodes);
|
Result:=FindEmptyMethods(CursorPos,Sections,ProcBodyNodes,AllEmpty);
|
||||||
if Result then begin
|
if Result then begin
|
||||||
AVLNode:=ProcBodyNodes.FindLowest;
|
AVLNode:=ProcBodyNodes.FindLowest;
|
||||||
while AVLNode<>nil do begin
|
while AVLNode<>nil do begin
|
||||||
@ -4043,8 +4046,8 @@ begin
|
|||||||
end;
|
end;
|
||||||
|
|
||||||
function TCodeCompletionCodeTool.FindEmptyMethods(CursorPos: TCodeXYPosition;
|
function TCodeCompletionCodeTool.FindEmptyMethods(CursorPos: TCodeXYPosition;
|
||||||
const Sections: TPascalClassSections; CodeTreeNodeExtensions: TAVLTree
|
const Sections: TPascalClassSections; CodeTreeNodeExtensions: TAVLTree;
|
||||||
): boolean;
|
out AllEmpty: boolean): boolean;
|
||||||
var
|
var
|
||||||
CleanCursorPos: integer;
|
CleanCursorPos: integer;
|
||||||
CursorNode: TCodeTreeNode;
|
CursorNode: TCodeTreeNode;
|
||||||
@ -4071,6 +4074,7 @@ var
|
|||||||
|
|
||||||
begin
|
begin
|
||||||
Result:=false;
|
Result:=false;
|
||||||
|
AllEmpty:=false;
|
||||||
BuildTreeAndGetCleanPos(trAll,CursorPos,CleanCursorPos,[]);
|
BuildTreeAndGetCleanPos(trAll,CursorPos,CleanCursorPos,[]);
|
||||||
CursorNode:=FindDeepestNodeAtPos(CleanCursorPos,true);
|
CursorNode:=FindDeepestNodeAtPos(CleanCursorPos,true);
|
||||||
CodeCompleteClassNode:=FindClassNode(CursorNode);
|
CodeCompleteClassNode:=FindClassNode(CursorNode);
|
||||||
@ -4116,6 +4120,7 @@ begin
|
|||||||
end;
|
end;
|
||||||
AVLNode:=NextAVLNode;
|
AVLNode:=NextAVLNode;
|
||||||
end;
|
end;
|
||||||
|
AllEmpty:=ProcBodyNodes.Count=0;
|
||||||
Result:=true;
|
Result:=true;
|
||||||
finally
|
finally
|
||||||
if ClassProcs<>nil then begin
|
if ClassProcs<>nil then begin
|
||||||
@ -4130,11 +4135,52 @@ begin
|
|||||||
end;
|
end;
|
||||||
|
|
||||||
function TCodeCompletionCodeTool.RemoveEmptyMethods(CursorPos: TCodeXYPosition;
|
function TCodeCompletionCodeTool.RemoveEmptyMethods(CursorPos: TCodeXYPosition;
|
||||||
const Sections: TPascalClassSections; SourceChangeCache: TSourceChangeCache
|
const Sections: TPascalClassSections; SourceChangeCache: TSourceChangeCache;
|
||||||
): boolean;
|
out AllRemoved: boolean): boolean;
|
||||||
|
var
|
||||||
|
ProcBodyNodes: TAVLTree;
|
||||||
|
AVLNode: TAVLTreeNode;
|
||||||
|
NodeExt: TCodeTreeNodeExtension;
|
||||||
|
FirstNodeExt: TCodeTreeNodeExtension;
|
||||||
|
LastNodeExt: TCodeTreeNodeExtension;
|
||||||
|
FromPos: LongInt;
|
||||||
|
ToPos: LongInt;
|
||||||
begin
|
begin
|
||||||
Result:=false;
|
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;
|
end;
|
||||||
|
|
||||||
function TCodeCompletionCodeTool.InitClassCompletion(
|
function TCodeCompletionCodeTool.InitClassCompletion(
|
||||||
|
|||||||
@ -474,9 +474,11 @@ type
|
|||||||
function FixForwardDefinitions(Code: TCodeBuffer): boolean;
|
function FixForwardDefinitions(Code: TCodeBuffer): boolean;
|
||||||
function FindEmptyMethods(Code: TCodeBuffer; X,Y: integer;
|
function FindEmptyMethods(Code: TCodeBuffer; X,Y: integer;
|
||||||
const Sections: TPascalClassSections;
|
const Sections: TPascalClassSections;
|
||||||
ListOfPCodeXYPosition: TFPList): boolean;
|
ListOfPCodeXYPosition: TFPList;
|
||||||
|
out AllEmpty: boolean): boolean;
|
||||||
function RemoveEmptyMethods(Code: TCodeBuffer; X,Y: integer;
|
function RemoveEmptyMethods(Code: TCodeBuffer; X,Y: integer;
|
||||||
const Sections: TPascalClassSections): boolean;
|
const Sections: TPascalClassSections;
|
||||||
|
out AllRemoved: boolean): boolean;
|
||||||
|
|
||||||
// custom class completion
|
// custom class completion
|
||||||
function InitClassCompletion(Code: TCodeBuffer;
|
function InitClassCompletion(Code: TCodeBuffer;
|
||||||
@ -3318,8 +3320,8 @@ begin
|
|||||||
end;
|
end;
|
||||||
|
|
||||||
function TCodeToolManager.FindEmptyMethods(Code: TCodeBuffer; X, Y: integer;
|
function TCodeToolManager.FindEmptyMethods(Code: TCodeBuffer; X, Y: integer;
|
||||||
const Sections: TPascalClassSections; ListOfPCodeXYPosition: TFPList
|
const Sections: TPascalClassSections; ListOfPCodeXYPosition: TFPList;
|
||||||
): boolean;
|
out AllEmpty: boolean): boolean;
|
||||||
var
|
var
|
||||||
CursorPos: TCodeXYPosition;
|
CursorPos: TCodeXYPosition;
|
||||||
begin
|
begin
|
||||||
@ -3333,14 +3335,14 @@ begin
|
|||||||
CursorPos.Code:=Code;
|
CursorPos.Code:=Code;
|
||||||
try
|
try
|
||||||
Result:=FCurCodeTool.FindEmptyMethods(CursorPos,Sections,
|
Result:=FCurCodeTool.FindEmptyMethods(CursorPos,Sections,
|
||||||
ListOfPCodeXYPosition);
|
ListOfPCodeXYPosition,AllEmpty);
|
||||||
except
|
except
|
||||||
on e: Exception do Result:=HandleException(e);
|
on e: Exception do Result:=HandleException(e);
|
||||||
end;
|
end;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
function TCodeToolManager.RemoveEmptyMethods(Code: TCodeBuffer; X,Y: integer;
|
function TCodeToolManager.RemoveEmptyMethods(Code: TCodeBuffer; X,Y: integer;
|
||||||
const Sections: TPascalClassSections): boolean;
|
const Sections: TPascalClassSections; out AllRemoved: boolean): boolean;
|
||||||
var
|
var
|
||||||
CursorPos: TCodeXYPosition;
|
CursorPos: TCodeXYPosition;
|
||||||
begin
|
begin
|
||||||
@ -3353,7 +3355,8 @@ begin
|
|||||||
CursorPos.Y:=Y;
|
CursorPos.Y:=Y;
|
||||||
CursorPos.Code:=Code;
|
CursorPos.Code:=Code;
|
||||||
try
|
try
|
||||||
Result:=FCurCodeTool.RemoveEmptyMethods(CursorPos,Sections,SourceChangeCache);
|
Result:=FCurCodeTool.RemoveEmptyMethods(CursorPos,Sections,
|
||||||
|
SourceChangeCache,AllRemoved);
|
||||||
except
|
except
|
||||||
on e: Exception do Result:=HandleException(e);
|
on e: Exception do Result:=HandleException(e);
|
||||||
end;
|
end;
|
||||||
|
|||||||
@ -42,6 +42,7 @@ var
|
|||||||
ListOfPCodeXYPosition: TFPList;
|
ListOfPCodeXYPosition: TFPList;
|
||||||
i: Integer;
|
i: Integer;
|
||||||
P: PCodeXYPosition;
|
P: PCodeXYPosition;
|
||||||
|
All: boolean;
|
||||||
begin
|
begin
|
||||||
if (ParamCount>=1) and (Paramcount<>3) then begin
|
if (ParamCount>=1) and (Paramcount<>3) then begin
|
||||||
writeln('Usage:');
|
writeln('Usage:');
|
||||||
@ -70,13 +71,22 @@ begin
|
|||||||
// complete code
|
// complete code
|
||||||
ListOfPCodeXYPosition:=TFPList.Create;
|
ListOfPCodeXYPosition:=TFPList.Create;
|
||||||
if CodeToolBoss.FindEmptyMethods(Code,X,Y,[pcsPublished],
|
if CodeToolBoss.FindEmptyMethods(Code,X,Y,[pcsPublished],
|
||||||
ListOfPCodeXYPosition)
|
ListOfPCodeXYPosition,All)
|
||||||
then begin
|
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
|
for i:=0 to ListOfPCodeXYPosition.Count-1 do begin
|
||||||
P:=PCodeXYPosition(ListOfPCodeXYPosition[i]);
|
P:=PCodeXYPosition(ListOfPCodeXYPosition[i]);
|
||||||
writeln(i,' ',DbgsCXY(P^));
|
writeln(i,' ',DbgsCXY(P^));
|
||||||
end;
|
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
|
end else begin
|
||||||
writeln('FindEmptyMethods failed: ',CodeToolBoss.ErrorMessage);
|
writeln('FindEmptyMethods failed: ',CodeToolBoss.ErrorMessage);
|
||||||
end;
|
end;
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user