From 5698c6ed0204b2be34e0dcb785fc76c11e15984e Mon Sep 17 00:00:00 2001 From: mattias Date: Wed, 13 Jan 2016 17:25:10 +0000 Subject: [PATCH] codetools: CleanCodeFromComments added parameter KeepDirectives git-svn-id: trunk@51269 - --- components/codetools/basiccodetools.pas | 43 ++++++++++++++---------- components/codetools/codetoolmanager.pas | 10 +++--- 2 files changed, 32 insertions(+), 21 deletions(-) diff --git a/components/codetools/basiccodetools.pas b/components/codetools/basiccodetools.pas index 6304c31246..2424af9af0 100644 --- a/components/codetools/basiccodetools.pas +++ b/components/codetools/basiccodetools.pas @@ -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; diff --git a/components/codetools/codetoolmanager.pas b/components/codetools/codetoolmanager.pas index ab0989144d..96a2938342 100644 --- a/components/codetools/codetoolmanager.pas +++ b/components/codetools/codetoolmanager.pas @@ -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;