codetools: removing empty methods: auto removing class completion comment

git-svn-id: trunk@14971 -
This commit is contained in:
mattias 2008-04-25 13:31:19 +00:00
parent c0fef1b31e
commit 5399b1c322
4 changed files with 84 additions and 26 deletions

View File

@ -152,6 +152,8 @@ type
function InsertClassHeaderComment: boolean;
function InsertMissingClassSemicolons: boolean;
function InsertAllNewUnitsToMainUsesSection: boolean;
function FindClassMethodsComment(StartPos: integer;
out CommentStart, CommentEnd: TCodeXYPosition): boolean;
function CreateMissingProcBodies: boolean;
function ApplyChangesAndJumpToFirstNewProc(CleanPos: integer;
OldTopLine: integer;
@ -4095,7 +4097,7 @@ begin
while AVLNode<>nil do begin
NextAVLNode:=ProcBodyNodes.FindSuccessor(AVLNode);
NodeExt:=TCodeTreeNodeExtension(AVLNode.Data);
DebugLn(['TCodeCompletionCodeTool.FindEmptyMethods ',NodeExt.Txt,' ',ProcBodyIsEmpty(NodeExt.Node)]);
//DebugLn(['TCodeCompletionCodeTool.FindEmptyMethods ',NodeExt.Txt,' ',ProcBodyIsEmpty(NodeExt.Node)]);
// check if proc body is empty (no code, no comments)
if ProcBodyIsEmpty(NodeExt.Node) then begin
GatherClassProcs;
@ -4145,6 +4147,11 @@ var
LastNodeExt: TCodeTreeNodeExtension;
FromPos: LongInt;
ToPos: LongInt;
FirstGroup: Boolean;
CommentStart: TCodeXYPosition;
CommentEnd: TCodeXYPosition;
CommentEndPos: integer;
CommentStartPos: integer;
begin
Result:=false;
AllRemoved:=false;
@ -4157,6 +4164,7 @@ begin
// sort the nodes for position
ProcBodyNodes.OnCompare:=@CompareCodeTreeNodeExtWithPos;
AVLNode:=ProcBodyNodes.FindLowest;
FirstGroup:=true;
while AVLNode<>nil do begin
// gather a group of continuous proc nodes
FirstNodeExt:=TCodeTreeNodeExtension(AVLNode.Data);
@ -4170,10 +4178,22 @@ begin
end;
// delete group
FromPos:=FindLineEndOrCodeInFrontOfPosition(FirstNodeExt.Node.StartPos,true);
ToPos:=FindLineEndOrCodeAfterPosition(LastNodeExt.Node.EndPos,AllRemoved);
if not SourceChangeCache.Replace(gtNone,gtNone,FromPos,ToPos,'')
then
ToPos:=FindLineEndOrCodeAfterPosition(LastNodeExt.Node.EndPos,true);
if AllRemoved and FirstGroup
and FindClassMethodsComment(FromPos,CommentStart,CommentEnd) then begin
// all method bodies will be removed => remove the default comment too
if CaretToCleanPos(CommentEnd,CommentEndPos)=0 then begin
if FindNextNonSpace(Src,CommentEndPos)>=FromPos then begin
// the default comment is directly in front
// => remove it too
if CaretToCleanPos(CommentStart,CommentStartPos)=0 then
FromPos:=FindLineEndOrCodeInFrontOfPosition(CommentStartPos,true);
end;
end;
end;
if not SourceChangeCache.Replace(gtNone,gtNone,FromPos,ToPos,'') then
exit;
FirstGroup:=false;
end;
end;
Result:=SourceChangeCache.Apply;
@ -5287,6 +5307,20 @@ begin
end;
end;
function TCodeCompletionCodeTool.FindClassMethodsComment(StartPos: integer; out
CommentStart, CommentEnd: TCodeXYPosition): boolean;
var
InsertXYPos: TCodeXYPosition;
Code: String;
begin
Result:=false;
if not CleanPosToCaret(StartPos,InsertXYPos) then exit;
Code:=ExtractClassName(CodeCompleteClassNode,false);
// search the comment
Result:=FindCommentInFront(InsertXYPos,Code,false,true,false,false,true,true,
CommentStart,CommentEnd)
end;
procedure TCodeCompletionCodeTool.AddNewPropertyAccessMethodsToClassProcs(
ClassProcs: TAVLTree; const TheClassName: string);
var ANodeExt: TCodeTreeNodeExtension;
@ -5537,19 +5571,13 @@ var
procedure InsertClassMethodsComment;
var
Code: String;
InsertXYPos, CommentStart, CommentEnd: TCodeXYPosition;
CommentStart, CommentEnd: TCodeXYPosition;
begin
// insert class comment
if ClassProcs.Count=0 then exit;
// find the start of the class (the position in front of the class name)
if not CleanPosToCaret(InsertPos,InsertXYPos) then exit;
Code:=ExtractClassName(CodeCompleteClassNode,false);
// check if there is already a comment in front
if FindCommentInFront(InsertXYPos,Code,false,true,false,false,true,true,
CommentStart,CommentEnd)
then begin
if FindClassMethodsComment(InsertPos,CommentStart,CommentEnd) then begin
// comment already exists
exit;
end;

View File

@ -43,6 +43,7 @@ var
i: Integer;
P: PCodeXYPosition;
All: boolean;
Sections: TPascalClassSections;
begin
if (ParamCount>=1) and (Paramcount<>3) then begin
writeln('Usage:');
@ -54,7 +55,7 @@ begin
CodeToolBoss.SimpleInit(ConfigFilename);
X:=10;
Y:=16;
Y:=22;
Filename:=ExpandFileName('scanexamples'+PathDelim+'emptymethods1.pas');
if (ParamCount>=3) then begin
@ -70,15 +71,15 @@ begin
// complete code
ListOfPCodeXYPosition:=TFPList.Create;
if CodeToolBoss.FindEmptyMethods(Code,X,Y,[pcsPublished],
ListOfPCodeXYPosition,All)
Sections:=[pcsPublished,pcsPrivate,pcsProtected,pcsPublic];
if CodeToolBoss.FindEmptyMethods(Code,X,Y,Sections,ListOfPCodeXYPosition,All)
then begin
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)
if CodeToolBoss.RemoveEmptyMethods(Code,X,Y,Sections,All)
then begin
writeln('Empty methods removed:');
writeln('=========================');

View File

@ -16,10 +16,17 @@ type
procedure DoSomething;
end;
{ TDirtyClass }
TDirtyClass = class(TPersistent)
published
procedure APublishedMethod;
private
procedure APrivateMethod;
protected
procedure AProtectedMethod;
public
procedure APublicMethod;
end;
implementation
@ -31,5 +38,27 @@ begin
end;
{ TDirtyClass }
procedure TDirtyClass.APublishedMethod;
begin
end;
procedure TDirtyClass.APrivateMethod;
begin
end;
procedure TDirtyClass.AProtectedMethod;
begin
end;
procedure TDirtyClass.APublicMethod;
begin
end;
end.

View File

@ -3481,10 +3481,10 @@ function TStandardCodeTool.FindCommentInFront(const StartPos: TCodeXYPosition;
out CommentStart, CommentEnd: TCodeXYPosition): boolean;
// searches a comment in front.
var
FoundStartPos: LongInt;
FoundEndPos: LongInt;
FoundStartPos: integer;
FoundEndPos: integer;
procedure CompareComment(StartPos, EndPos: integer);
procedure CompareComment(CStartPos, CEndPos: integer);
var
Found: LongInt;
CompareStartPos: LongInt;
@ -3492,10 +3492,10 @@ var
CompareLen: Integer;
CompareCommentLength: Integer;
begin
//debugln('CompareComment "',copy(Src,StartPos,EndPos-StartPos),'"');
//debugln('CompareComment "',copy(Src,CStartPos,CEndPos-CStartPos),'"');
CompareStartPos:=StartPos;
CompareEndPos:=EndPos;
CompareStartPos:=CStartPos;
CompareEndPos:=CEndPos;
if not WithCommentBounds then begin
// chomp comment boundaries
case Src[CompareStartPos] of
@ -3535,12 +3535,12 @@ var
CaseSensitive);
end else begin
Found:=CompareText(@Src[CompareStartPos],CompareLen,
@CommentText[1],length(CommentText),
CaseSensitive);
@CommentText[1],length(CommentText),
CaseSensitive);
end;
if Found=0 then begin
FoundStartPos:=StartPos;
FoundEndPos:=EndPos;
FoundStartPos:=CStartPos;
FoundEndPos:=CEndPos;
end;
end;