codetools: FindReferences for unit names

git-svn-id: trunk@64931 -
This commit is contained in:
mattias 2021-04-06 00:11:28 +00:00
parent 8686aefe45
commit 13450f46a2
4 changed files with 85 additions and 30 deletions

View File

@ -2654,12 +2654,12 @@ begin
Result:=true;
end else begin
//debugln(['TCodeToolManager.FindReferences cache not valid']);
{debugln(['TCodeToolManager.FindReferences IdentifierCode=',Cache.IdentifierCode=IdentifierCode,
' X=',Cache.X=X,' Y=',Cache.Y=Y,
' SourcesChangeStep=',Cache.SourcesChangeStep=SourceCache.ChangeStamp,',',Cache.SourcesChangeStep=CTInvalidChangeStamp64,
' FilesChangeStep=',Cache.FilesChangeStep=FileStateCache.TimeStamp,',',Cache.FilesChangeStep=CTInvalidChangeStamp64,
' InitValuesChangeStep=',Cache.InitValuesChangeStep=DefineTree.ChangeStep,',',Cache.InitValuesChangeStep=CTInvalidChangeStamp,
'']);}
//debugln(['TCodeToolManager.FindReferences IdentifierCode=',Cache.IdentifierCode=IdentifierCode,
// ' X=',Cache.X=X,' Y=',Cache.Y=Y,
// ' SourcesChangeStep=',Cache.SourcesChangeStep=SourceCache.ChangeStamp,',',Cache.SourcesChangeStep=CTInvalidChangeStamp64,
// ' FilesChangeStep=',Cache.FilesChangeStep=FileStateCache.TimeStamp,',',Cache.FilesChangeStep=CTInvalidChangeStamp64,
// ' InitValuesChangeStep=',Cache.InitValuesChangeStep=DefineTree.ChangeStep,',',Cache.InitValuesChangeStep=CTInvalidChangeStamp,
// '']);
Cache.Clear;
Cache.IdentifierCode:=IdentifierCode;
Cache.X:=X;
@ -2673,7 +2673,7 @@ begin
CursorPos.Y:=Y;
CursorPos.Code:=IdentifierCode;
try
Result:=FCurCodeTool.FindDeclaration(CursorPos,[fsfFindMainDeclaration],
Result:=FCurCodeTool.FindDeclaration(CursorPos,[fsfFindMainDeclaration,fsfSearchSourceName],
Cache.NewTool,Cache.NewNode,Cache.NewPos,NewTopLine);
except
on e: Exception do HandleException(e);
@ -2705,12 +2705,14 @@ begin
if NewTopLine=0 then ;
if not InitCurCodeTool(SearchInCode) then exit;
if Cache.IsPrivate and (FCurCodeTool<>Cache.NewTool) then begin
//debugln(['TCodeToolManager.FindReferences identifier is not reachable from this unit => skipping search']);
{$IFDEF VerboseFindReferences}
debugln(['TCodeToolManager.FindReferences identifier is not reachable from this unit => skipping search']);
{$ENDIF}
exit(true);
end;
CursorPos:=Cache.NewPos;
{$IFDEF CTDEBUG}
{$IF defined(CTDEBUG) or defined(VerboseFindReferences)}
DebugLn('TCodeToolManager.FindReferences Searching ',dbgs(FCurCodeTool.Scanner<>nil),' for reference to x=',dbgs(CursorPos.X),' y=',dbgs(CursorPos.Y),' ',CursorPos.Code.Filename);
{$ENDIF}
try

View File

@ -363,6 +363,7 @@ type
//-----------------------------------------------------------------------------
// useful functions
function NodeDescriptionAsString(Desc: TCodeTreeNodeDesc): string;
function NodePathAsString(Node: TCodeTreeNode): string;
procedure WriteNodeExtTree(Tree: TAVLTree);
function FindCodeTreeNodeExt(Tree: TAVLTree; const Txt: string
): TCodeTreeNodeExtension;
@ -517,6 +518,19 @@ begin
end;
end;
function NodePathAsString(Node: TCodeTreeNode): string;
begin
if Node=nil then
exit('nil');
Result:='';
while Node<>nil do begin
if Result<>'' then
Result:='.'+Result;
Result:=Node.DescAsString+Result;
Node:=Node.Parent;
end;
end;
procedure WriteNodeExtTree(Tree: TAVLTree);
var
Node: TAVLTreeNode;

View File

@ -2021,7 +2021,7 @@ var
NewTool: TFindDeclarationTool;
NewNode: TCodeTreeNode;
begin
Result:=FindDeclaration(CursorPos,[fsfFindMainDeclaration],NewTool,NewNode,
Result:=FindDeclaration(CursorPos,[fsfFindMainDeclaration,fsfSearchSourceName],NewTool,NewNode,
NewPos,NewTopLine);
end;
@ -2552,8 +2552,9 @@ function TFindDeclarationTool.FindDeclaration(const CursorPos: TCodeXYPosition;
var
ExprType: TExpressionType;
begin
Result := FindDeclaration(CursorPos, SearchSmartFlags, ExprType, NewPos, NewTopLine, BlockTopLine, BlockBottomLine) and
(NewPos.X >= 0) and (NewPos.Y >= 0);
Result := FindDeclaration(CursorPos, SearchSmartFlags, ExprType,
NewPos, NewTopLine, BlockTopLine, BlockBottomLine)
and (NewPos.X >= 0) and (NewPos.Y >= 0);
if Result then begin
NewTool := ExprType.Context.Tool;
NewNode := ExprType.Context.Node;
@ -4980,8 +4981,9 @@ begin
ctnIdentifier:
if (ContextNode.Parent.Desc in [ctnConstDefinition,ctnVarDefinition])
and (ContextNode=ContextNode.Parent.LastChild)
and SearchInTypeOfVarConst then exit;
and (ContextNode=ContextNode.Parent.LastChild)
and SearchInTypeOfVarConst then
exit;
ctnEnumIdentifier,ctnLabel:
if SearchInEnumLabelDefinition then exit;
@ -6264,8 +6266,8 @@ var
Identifier: string;
CleanDeclCursorPos: integer;
DeclarationTool: TFindDeclarationTool;
DeclarationNode: TCodeTreeNode;
AliasDeclarationNode: TCodeTreeNode; // if exists: always in front of DeclarationNode
DeclarationNode: TCodeTreeNode; // in DeclarationTool
AliasDeclarationNode: TCodeTreeNode; // if exists: always in front of DeclarationNode, and in DeclarationTool
Params: TFindDeclarationParams;
PosTree: TAVLTree; // tree of PChar positions in Src
ReferencePos: TCodeXYPosition;
@ -6281,7 +6283,9 @@ var
if PosTree=nil then
PosTree:=TAVLTree.Create;
p:=@Src[ACleanPos];
//debugln('TFindDeclarationTool.FindReferences.AddReference ',CleanPosToStr(ACleanPos),' ',dbgs(PosTree.Find(p)=nil),' Code=',dbgstr(copy(Src,ACleanPos-8,8)+'|'+dbgstr(copy(Src,ACleanPos,5))));
{$IFDEF VerboseFindReferences}
debugln('TFindDeclarationTool.FindReferences.AddReference ',CleanPosToStr(ACleanPos),' ',dbgs(PosTree.Find(p)=nil),' Code=',dbgstr(copy(Src,ACleanPos-8,8)+'|'+dbgstr(copy(Src,ACleanPos,5))));
{$ENDIF}
if PosTree.Find(p)=nil then
PosTree.Add(p);
end;
@ -6314,6 +6318,11 @@ var
var
IdentStartPos: Integer;
IdentEndPos: integer;
UnitNamePos, UnitInFilePos: TAtomPosition;
NewCodeTool: TFindDeclarationTool;
AnUnitName: String;
UnitInFilename: AnsiString;
Node: TCodeTreeNode;
begin
if (not IsComment) then
UnitStartFound:=true;
@ -6326,17 +6335,19 @@ var
if IdentEndPos-IdentStartPos<>length(Identifier) then exit;
if CompareIdentifiers(PChar(Pointer(Identifier)),@Src[IdentStartPos])<>0 then exit;
if IsComment and (SkipComments or (not UnitStartFound)) then exit;
{debugln(['Identifier with same name found at: ',
{$IFDEF VerboseFindReferences}
debugln(['Identifier with same name found at: ',
IdentStartPos,'=',CleanPosToStr(StartPos),' ',GetIdentifier(@Src[IdentStartPos]),
' CleanDeclCursorPos=',CleanDeclCursorPos,
' MaxPos=',MaxPos,
' IsComment=',IsComment,
' SkipComments=',SkipComments,
' UnitStartFound=',UnitStartFound
]);}
]);
{$ENDIF}
CursorNode:=BuildSubTreeAndFindDeepestNodeAtPos(IdentStartPos,true);
//debugln(' CursorNode=',CursorNode.DescAsString,' Forward=',dbgs(CursorNode.SubDesc and ctnsForwardDeclaration));
//debugln(' CursorNode=',NodePathAsString(CursorNode),' Forward=',dbgs(CursorNode.SubDesc and ctnsForwardDeclaration));
if (DeclarationTool=Self)
and ((IdentStartPos=CleanDeclCursorPos) or (CursorNode=AliasDeclarationNode))
@ -6377,12 +6388,34 @@ var
end;
//debugln(' Found=',dbgs(Found));
if Found and (Params.NewNode<>nil) then begin
UseProcHead(Params.NewNode);
//debugln('Context=',Params.NewNode.DescAsString,' ',dbgs(Params.NewNode.StartPos),' ',dbgs(DeclarationNode.StartPos));
Node:=Params.NewNode;
if Found and (Node<>nil) then begin
if ((Node.Desc=ctnUseUnit) or (Node.Parent.Desc=ctnUseUnit))
and (Params.NewCodeTool=Self) then begin
// identifier is a unit reference
if (DeclarationNode.Desc=ctnSrcName)
or ((DeclarationNode.Parent<>nil) and (DeclarationNode.Parent.Desc=ctnSrcName)) then begin
// searching a unit reference -> check if it is the same
MoveCursorToNodeStart(Node);
if ReadNextUsedUnit(UnitNamePos,UnitInFilePos) then begin
// cursor is on an used unit -> try to locate it
MoveCursorToCleanPos(UnitNamePos.StartPos);
ReadNextAtom;
AnUnitName:=ExtractUsedUnitNameAtCursor(@UnitInFilename);
NewCodeTool:=FindCodeToolForUsedUnit(AnUnitName,UnitInFilename,false);
if NewCodeTool=DeclarationTool then begin
AddReference(IdentStartPos);
exit;
end;
end;
end;
end;
UseProcHead(Node);
//debugln('Context=',NodePathAsString(Params.NewNode),' FoundPos=',Params.NewCodeTool.CleanPosToStr(Params.NewNode.StartPos,true),' SearchPos=',DeclarationTool.CleanPosToStr(DeclarationNode.StartPos,true));
if (Params.NewNode=DeclarationNode)
or (Params.NewNode=AliasDeclarationNode) then begin
//debugln(['ReadIdentifier reference found, adding ...']);
debugln(['ReadIdentifier reference found, adding ...']);
AddReference(IdentStartPos);
end;
end;
@ -6559,7 +6592,7 @@ var
end;
// find alias declaration node
//debugln('FindDeclarationNode DeclarationNode=',DeclarationNode.DescAsString);
//debugln('FindDeclarationNode DeclarationNode=',NodePathAsString(DeclarationNode),' at ',DeclarationTool.CleanPosToStr(DeclarationNode.StartPos));
AliasDeclarationNode:=nil;
case DeclarationNode.Desc of
@ -6574,7 +6607,7 @@ var
ctnVarDefinition:
if DeclarationNode.HasParentOfType(ctnProcedureHead) then begin
AliasDeclarationNode:=FindCorrespondingProcParamNode(DeclarationNode,ProcAttr);
AliasDeclarationNode:=DeclarationTool.FindCorrespondingProcParamNode(DeclarationNode,ProcAttr);
end;
ctnTypeDefinition:
@ -6598,7 +6631,6 @@ var
AliasDeclarationNode:=DeclarationNode;
DeclarationNode:=Node;
end;
//debugln('FindDeclarationNode AliasDeclarationNode=',AliasDeclarationNode.DescAsString,' ',DeclarationTool.CleanPosToStr(AliasDeclarationNode.StartPos,DeclarationTool<>Self));
end;
// search comment in front of declaration
@ -6635,7 +6667,7 @@ var
if MaxPos>SrcLen then MaxPos:=SrcLen;
if DeclarationTool<>Self then begin
MinPos:=UseNode.Parent.EndPos;
MinPos:=UseNode.StartPos;
exit;
end;
@ -6698,7 +6730,9 @@ var
AVLNode: TAVLTreeNode;
begin
Result:=false;
//debugln('FindReferences ',MainFilename,' CursorPos=',CursorPos.Code.Filename,' x=',dbgs(CursorPos.X),' y=',dbgs(CursorPos.Y),' SkipComments=',dbgs(SkipComments));
{$IFDEF VerboseFindReferences}
debugln('FindReferences ',MainFilename,' CursorPos=',CursorPos.Code.Filename,' x=',dbgs(CursorPos.X),' y=',dbgs(CursorPos.Y),' SkipComments=',dbgs(SkipComments));
{$ENDIF}
ListOfPCodeXYPosition:=nil;
Params:=nil;
@ -6956,6 +6990,10 @@ begin
ctnUnit:
Result:=PositionInSourceName(CleanPos);
ctnIdentifier:
if Node.Parent.Desc=ctnSrcName then
Result:=true;
end;
end;

View File

@ -274,7 +274,7 @@ begin
CodeToolBoss.GetIdentifierAt(DeclCode,DeclarationCaretXY.X,DeclarationCaretXY.Y,Identifier);
CurUnitname:=ExtractFileNameOnly(DeclCode.Filename);
//debugln('TMainIDE.DoFindRenameIdentifier A DeclarationCaretXY=x=',dbgs(DeclarationCaretXY.X),' y=',dbgs(DeclarationCaretXY.Y));
//debugln('TMainIDE.DoFindRenameIdentifier A DeclarationCaretXY=',dbgs(DeclarationCaretXY));
Files:=nil;
OwnerList:=nil;
@ -943,6 +943,7 @@ var
begin
FIdentifierFilename:=NewIdentifierFilename;
FIdentifierPosition:=NewIdentifierPosition;
//debugln(['TFindRenameIdentifierDialog.SetIdentifier ',FIdentifierFilename,' ',dbgs(FIdentifierPosition)]);
CurrentListBox.Items.Clear;
s:=IdentifierFilename
+'('+IntToStr(IdentifierPosition.Y)+','+IntToStr(IdentifierPosition.X)+')';