codetools: GetIDEDirectives: implemented filter, needed to fix removing all

git-svn-id: branches/fixes_1_4@48337 -
This commit is contained in:
mattias 2015-03-14 10:34:29 +00:00
parent 3b17cceb51
commit 8ac17ca899
2 changed files with 41 additions and 24 deletions

View File

@ -820,10 +820,10 @@ type
const APropertyPath: string = ''): boolean;
// IDE % directives
function GetIDEDirectives(Code: TCodeBuffer;
DirectiveList: TStrings): boolean;
function SetIDEDirectives(Code: TCodeBuffer;
DirectiveList: TStrings): boolean;
function GetIDEDirectives(Code: TCodeBuffer; DirectiveList: TStrings;
const Filter: TOnIDEDirectiveFilter = nil): boolean;
function SetIDEDirectives(Code: TCodeBuffer; DirectiveList: TStrings;
const Filter: TOnIDEDirectiveFilter = nil): boolean;
// linker jumping
function JumpToLinkerIdentifier(Code: TCodeBuffer;
@ -3778,7 +3778,7 @@ begin
end;
function TCodeToolManager.GetIDEDirectives(Code: TCodeBuffer;
DirectiveList: TStrings): boolean;
DirectiveList: TStrings; const Filter: TOnIDEDirectiveFilter): boolean;
begin
{$IFDEF CTDEBUG}
DebugLn('TCodeToolManager.GetIDEDirectives A ',Code.Filename);
@ -3786,14 +3786,14 @@ begin
Result:=false;
if not InitCurCodeTool(Code) then exit;
try
Result:=FCurCodeTool.GetIDEDirectives(DirectiveList);
Result:=FCurCodeTool.GetIDEDirectives(DirectiveList,Filter);
except
on e: Exception do Result:=HandleException(e);
end;
end;
function TCodeToolManager.SetIDEDirectives(Code: TCodeBuffer;
DirectiveList: TStrings): boolean;
DirectiveList: TStrings; const Filter: TOnIDEDirectiveFilter): boolean;
begin
{$IFDEF CTDEBUG}
DebugLn('TCodeToolManager.GetIDEDirectives A ',Code.Filename);
@ -3801,7 +3801,7 @@ begin
Result:=false;
if not InitCurCodeTool(Code) then exit;
try
Result:=FCurCodeTool.SetIDEDirectives(DirectiveList,SourceChangeCache);
Result:=FCurCodeTool.SetIDEDirectives(DirectiveList,SourceChangeCache,Filter);
except
on e: Exception do Result:=HandleException(e);
end;

View File

@ -57,12 +57,16 @@ uses
CustomCodeTool, CodeToolsStructs, LazFileUtils;
type
TStandardCodeTool = class;
TUsesSection = (usMain, usImplementation);
TOnFindDefinePropertyForContext = procedure(Sender: TObject;
const ClassContext, AncestorClassContext: TFindContext;
LFMNode: TLFMTreeNode;
const IdentName: string; var IsDefined: boolean) of object;
TOnIDEDirectiveFilter = function(Tool: TStandardCodeTool;
StartPos, EndPos: integer): boolean of object; // true = use
{ TStandardCodeTool }
@ -333,9 +337,11 @@ type
SourceChangeCache: TSourceChangeCache): boolean;
// IDE % directives
function GetIDEDirectives(DirectiveList: TStrings): boolean;
function GetIDEDirectives(DirectiveList: TStrings;
const Filter: TOnIDEDirectiveFilter = nil): boolean;
function SetIDEDirectives(DirectiveList: TStrings;
SourceChangeCache: TSourceChangeCache): boolean;
SourceChangeCache: TSourceChangeCache;
const Filter: TOnIDEDirectiveFilter = nil): boolean;
procedure CalcMemSize(Stats: TCTMemStats); override;
end;
@ -4086,7 +4092,8 @@ begin
Result:=true;
end;
function TStandardCodeTool.GetIDEDirectives(DirectiveList: TStrings): boolean;
function TStandardCodeTool.GetIDEDirectives(DirectiveList: TStrings;
const Filter: TOnIDEDirectiveFilter): boolean;
var
StartPos: Integer;
EndPos: Integer;
@ -4099,6 +4106,7 @@ begin
StartPos:=FindNextIDEDirective(Src,EndPos,Scanner.NestedComments);
if StartPos<1 then break;
EndPos:=FindCommentEnd(Src,StartPos,Scanner.NestedComments);
if (Filter=nil) or Filter(Self,StartPos,EndPos) then
DirectiveList.Add(copy(Src,StartPos,EndPos-StartPos));
if EndPos>SrcLen then break;
StartPos:=EndPos;
@ -4107,7 +4115,8 @@ begin
end;
function TStandardCodeTool.SetIDEDirectives(DirectiveList: TStrings;
SourceChangeCache: TSourceChangeCache): boolean;
SourceChangeCache: TSourceChangeCache; const Filter: TOnIDEDirectiveFilter
): boolean;
var
InsertPos: Integer;
EndPos: Integer;
@ -4122,7 +4131,12 @@ begin
// find first old IDE directive
InsertPos:=FindNextIDEDirective(Src,1,Scanner.NestedComments);
if InsertPos<1 then InsertPos:=0;
if InsertPos>=1 then begin
EndPos:=FindCommentEnd(Src,InsertPos,Scanner.NestedComments);
if (Filter<>nil) and (not Filter(Self,InsertPos,EndPos)) then
InsertPos:=0;
end else
InsertPos:=0;
// remove all old IDE directives
if InsertPos>=1 then
@ -4134,6 +4148,7 @@ begin
StartPos:=FindNextIDEDirective(Src,EndPos,Scanner.NestedComments);
if StartPos<1 then break;
EndPos:=FindCommentEnd(Src,StartPos,Scanner.NestedComments);
if (Filter=nil) or Filter(Self,StartPos,EndPos) then begin
// remove also space in front of directive
while (StartPos>1) and (Src[StartPos-1] in [' ',#9]) do dec(StartPos);
// remove also space behind directive
@ -4145,7 +4160,9 @@ begin
inc(EndPos);
end;
// remove directive
SourceChangeCache.Replace(gtNone,gtNone,StartPos,EndPos,'');
if not SourceChangeCache.Replace(gtNone,gtNone,StartPos,EndPos,'') then
exit;
end;
if EndPos>SrcLen then break;
StartPos:=EndPos;
until false;