diff --git a/components/codetools/codetoolmanager.pas b/components/codetools/codetoolmanager.pas index fea3f1d83b..d7497b6aa6 100644 --- a/components/codetools/codetoolmanager.pas +++ b/components/codetools/codetoolmanager.pas @@ -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} diff --git a/components/codetools/codetree.pas b/components/codetools/codetree.pas index d47cfe8131..2b6cffa037 100644 --- a/components/codetools/codetree.pas +++ b/components/codetools/codetree.pas @@ -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; diff --git a/components/codetools/finddeclarationtool.pas b/components/codetools/finddeclarationtool.pas index ba75fa1fb8..f686a2024b 100644 --- a/components/codetools/finddeclarationtool.pas +++ b/components/codetools/finddeclarationtool.pas @@ -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 MinPosNode.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;