codetools: find identifier references: limit scope

git-svn-id: trunk@25959 -
This commit is contained in:
mattias 2010-06-06 22:23:44 +00:00
parent 478337aa6f
commit 886f997841
3 changed files with 75 additions and 13 deletions

View File

@ -46,7 +46,7 @@ uses
FindDeclarationCache, DirectoryCacher, AVL_Tree, LFMTrees, DirectivesTree,
PascalParserTool, CodeToolsConfig, CustomCodeTool, FindDeclarationTool,
IdentCompletionTool, StdCodeTools, ResourceCodeTool, CodeToolsStructs,
CodeTemplatesTool, ExtractProcTool;
CodeTemplatesTool, ExtractProcTool, PascalReaderTool;
type
TCodeToolManager = class;
@ -2188,24 +2188,45 @@ function TCodeToolManager.FindReferences(IdentifierCode: TCodeBuffer;
var ListOfPCodeXYPosition: TFPList): boolean;
var
CursorPos: TCodeXYPosition;
NewCode: TCodeBuffer;
NewX, NewY, NewTopLine: integer;
NewTool: TFindDeclarationTool;
NewNode: TCodeTreeNode;
NewPos: TCodeXYPosition;
NewTopLine: integer;
PrivateDeclaration: Boolean;
ImplementationNode: TCodeTreeNode;
begin
Result:=false;
{$IFDEF CTDEBUG}
DebugLn('TCodeToolManager.FindReferences A ',IdentifierCode.Filename,' x=',dbgs(x),' y=',dbgs(y));
{$ENDIF}
ListOfPCodeXYPosition:=nil;
if not FindMainDeclaration(IdentifierCode,X,Y,NewCode,NewX,NewY,NewTopLine)
then begin
DebugLn('TCodeToolManager.FindReferences unable to FindMainDeclaration ',IdentifierCode.Filename,' x=',dbgs(x),' y=',dbgs(y));
if not InitCurCodeTool(IdentifierCode) then exit;
CursorPos.X:=X;
CursorPos.Y:=Y;
CursorPos.Code:=IdentifierCode;
try
Result:=FCurCodeTool.FindDeclaration(CursorPos,[fsfFindMainDeclaration],
NewTool,NewNode,NewPos,NewTopLine)
except
on e: Exception do HandleException(e);
end;
if not Result then begin
DebugLn('TCodeToolManager.FindReferences unable to FindDeclaration ',IdentifierCode.Filename,' x=',dbgs(x),' y=',dbgs(y));
exit;
end;
// check if
PrivateDeclaration:=(NewTool.GetSourceType in [ctnLibrary,ctnProgram]);
if not PrivateDeclaration then begin
ImplementationNode:=NewTool.FindImplementationNode;
if (ImplementationNode<>nil) and (NewNode.StartPos>=ImplementationNode.StartPos)
then
PrivateDeclaration:=true;
end;
if NewTopLine=0 then ;
if not InitCurCodeTool(TargetCode) then exit;
CursorPos.X:=NewX;
CursorPos.Y:=NewY;
CursorPos.Code:=NewCode;
if PrivateDeclaration and (FCurCodeTool<>NewTool) then exit(true);
CursorPos:=NewPos;
{$IFDEF CTDEBUG}
DebugLn('TCodeToolManager.FindReferences B ',dbgs(FCurCodeTool.Scanner<>nil),' x=',dbgs(CursorPos.X),' y=',dbgs(CursorPos.Y),' ',CursorPos.Code.Filename);
{$ENDIF}

View File

@ -269,6 +269,7 @@ type
procedure DeleteNode(ANode: TCodeTreeNode);
procedure AddNodeAsLastChild(ParentNode, ANode: TCodeTreeNode);
procedure AddNodeInFrontOf(NextBrotherNode, ANode: TCodeTreeNode);
function FindFirstPosition: integer;
function FindLastPosition: integer;
function ContainsNode(ANode: TCodeTreeNode): boolean;
procedure Clear;
@ -902,6 +903,13 @@ begin
ANode.PriorBrother.NextBrother:=ANode;
end;
function TCodeTree.FindFirstPosition: integer;
begin
Result:=-1;
if Root=nil then exit;
Result:=Root.StartPos;
end;
function TCodeTree.FindLastPosition: integer;
var
ANode: TCodeTreeNode;

View File

@ -3907,7 +3907,7 @@ var
PosTree: TAVLTree; // tree of PChar positions in Src
AVLNode: TAVLTreeNode;
ReferencePos: TCodeXYPosition;
MaxPos: Integer;
MinPos, MaxPos: Integer;
CursorNode: TCodeTreeNode;
UnitStartFound, Found: Boolean;
@ -4027,7 +4027,7 @@ var
InStrConst: Boolean;
//CommentStart: LongInt;
begin
StartPos:=1;
StartPos:=MinPos;
UnitStartFound:=false;
while StartPos<=MaxPos do begin
case Src[StartPos] of
@ -4206,6 +4206,39 @@ var
Result:=true;
end;
procedure LimitScope;
var
Node: TCodeTreeNode;
begin
MinPos:=Tree.FindFirstPosition;
MaxPos:=Tree.FindLastPosition;
if MaxPos>SrcLen then MaxPos:=SrcLen;
if DeclarationTool<>Self then exit;
Node:=DeclarationNode;
while Node<>nil do begin
case Node.Desc of
ctnImplementation:
// only search in implementation
if MinPos<Node.StartPos then MinPos:=Node.StartPos;
ctnVarDefinition,ctnConstDefinition,ctnLabelType:
begin
// only search behind variable
if MinPos<Node.StartPos then MinPos:=Node.StartPos;
if (Node.Parent.Desc in [ctnVarSection,ctnConstSection,ctnLabelSection,ctnResStrSection])
and (Node.Parent.Parent.Desc=ctnProcedure) then begin
// only search till procedure end
if MaxPos>Node.Parent.Parent.EndPos then
MaxPos:=Node.Parent.Parent.EndPos;
end;
end;
end;
Node:=Node.Parent;
end;
//debugln(['LimitScope ',CleanPosToStr(MinPos),'..',CleanPosToStr(MaxPos),': ',dbgstr(copy(Src,MinPos,20)),'..',dbgstr(copy(Src,MaxPos-20,20))]);
end;
begin
Result:=false;
@ -4223,8 +4256,8 @@ begin
if not FindDeclarationNode then exit;
// search identifiers
MaxPos:=Tree.FindLastPosition;
if MaxPos>SrcLen then MaxPos:=SrcLen;
LimitScope;
//debugln('FindReferences StartPos=',dbgs(StartPos),' MaxPos=',dbgs(MaxPos));
SearchIdentifiers;