codetools: CleanCodeFromComments added parameter KeepDirectives

git-svn-id: branches/fixes_1_6@51270 -
This commit is contained in:
mattias 2016-01-13 17:25:49 +00:00
parent e92baf1992
commit 06fe3f3117
2 changed files with 32 additions and 21 deletions

View File

@ -69,8 +69,8 @@ function FindNextIncludeDirective(const ASource: string;
CommentStartPos, CommentEndPos: integer): integer;
function FindNextIDEDirective(const ASource: string; StartPos: integer;
NestedComments: boolean; EndPos: integer = 0): integer;
function CleanCodeFromComments(const DirtyCode: string;
NestedComments: boolean): string;
function CleanCodeFromComments(const Src: string;
NestedComments: boolean; KeepDirectives: boolean = false): string;
function ExtractCommentContent(const ASource: string; CommentStart: integer;
NestedComments: boolean;
TrimStart: boolean = false; TrimEnd: boolean = false;
@ -3990,27 +3990,36 @@ begin
Result:=-1;
end;
function CleanCodeFromComments(const DirtyCode: string;
NestedComments: boolean): string;
function CleanCodeFromComments(const Src: string;
NestedComments: boolean; KeepDirectives: boolean): string;
var
DirtyPos: Integer;
CleanPos: Integer;
SrcPos: Integer;
ResultPos: Integer;
StartPos: Integer;
l: Integer;
p: PChar;
begin
SetLength(Result,length(DirtyCode));
DirtyPos:=1;
CleanPos:=1;
while DirtyPos<=length(DirtyCode) do begin
StartPos:=FindNextComment(DirtyCode,DirtyPos);
l:=StartPos-DirtyPos;
if l>0 then begin
System.Move(DirtyCode[DirtyPos],Result[CleanPos],l);
inc(CleanPos,l);
SetLength(Result,length(Src));
SrcPos:=1;
ResultPos:=1;
while SrcPos<=length(Src) do begin
StartPos:=FindNextComment(Src,SrcPos);
l:=StartPos-SrcPos;
if (l>0) then begin
System.Move(Src[SrcPos],Result[ResultPos],l);
inc(ResultPos,l);
end;
SrcPos:=FindCommentEnd(Src,StartPos,NestedComments);
if KeepDirectives and (StartPos<=length(Src)) then begin
p:=@Src[StartPos];
if (p^='{') and (p[1]='$') then begin
l:=SrcPos-StartPos;
System.Move(Src[StartPos],Result[ResultPos],l);
inc(ResultPos,l);
end;
end;
DirtyPos:=FindCommentEnd(DirtyCode,StartPos,NestedComments);
end;
SetLength(Result,CleanPos-1);
SetLength(Result,ResultPos-1);
end;
function ExtractCommentContent(const ASource: string; CommentStart: integer;

View File

@ -405,9 +405,10 @@ type
// keywords and comments
function IsKeyword(Code: TCodeBuffer; const KeyWord: string): boolean;
function ExtractCodeWithoutComments(Code: TCodeBuffer): string;
function ExtractCodeWithoutComments(Code: TCodeBuffer;
KeepDirectives: boolean = false): string;
function GetPasDocComments(Code: TCodeBuffer; X, Y: integer;
out ListOfPCodeXYPosition: TFPList): boolean;
out ListOfPCodeXYPosition: TFPList): boolean;
// blocks (e.g. begin..end, case..end, try..finally..end, repeat..until)
function FindBlockCounterPart(Code: TCodeBuffer; X,Y: integer;
@ -3447,10 +3448,11 @@ begin
end;
end;
function TCodeToolManager.ExtractCodeWithoutComments(Code: TCodeBuffer): string;
function TCodeToolManager.ExtractCodeWithoutComments(Code: TCodeBuffer;
KeepDirectives: boolean): string;
begin
Result:=CleanCodeFromComments(Code.Source,
GetNestedCommentsFlagForFile(Code.Filename));
GetNestedCommentsFlagForFile(Code.Filename),KeepDirectives);
end;
function TCodeToolManager.GetPasDocComments(Code: TCodeBuffer; X, Y: integer;