mirror of
https://gitlab.com/freepascal.org/lazarus/lazarus.git
synced 2025-12-08 05:17:24 +01:00
codetools: FindReferences for unit names
git-svn-id: trunk@64931 -
This commit is contained in:
parent
8686aefe45
commit
13450f46a2
@ -2654,12 +2654,12 @@ begin
|
|||||||
Result:=true;
|
Result:=true;
|
||||||
end else begin
|
end else begin
|
||||||
//debugln(['TCodeToolManager.FindReferences cache not valid']);
|
//debugln(['TCodeToolManager.FindReferences cache not valid']);
|
||||||
{debugln(['TCodeToolManager.FindReferences IdentifierCode=',Cache.IdentifierCode=IdentifierCode,
|
//debugln(['TCodeToolManager.FindReferences IdentifierCode=',Cache.IdentifierCode=IdentifierCode,
|
||||||
' X=',Cache.X=X,' Y=',Cache.Y=Y,
|
// ' X=',Cache.X=X,' Y=',Cache.Y=Y,
|
||||||
' SourcesChangeStep=',Cache.SourcesChangeStep=SourceCache.ChangeStamp,',',Cache.SourcesChangeStep=CTInvalidChangeStamp64,
|
// ' SourcesChangeStep=',Cache.SourcesChangeStep=SourceCache.ChangeStamp,',',Cache.SourcesChangeStep=CTInvalidChangeStamp64,
|
||||||
' FilesChangeStep=',Cache.FilesChangeStep=FileStateCache.TimeStamp,',',Cache.FilesChangeStep=CTInvalidChangeStamp64,
|
// ' FilesChangeStep=',Cache.FilesChangeStep=FileStateCache.TimeStamp,',',Cache.FilesChangeStep=CTInvalidChangeStamp64,
|
||||||
' InitValuesChangeStep=',Cache.InitValuesChangeStep=DefineTree.ChangeStep,',',Cache.InitValuesChangeStep=CTInvalidChangeStamp,
|
// ' InitValuesChangeStep=',Cache.InitValuesChangeStep=DefineTree.ChangeStep,',',Cache.InitValuesChangeStep=CTInvalidChangeStamp,
|
||||||
'']);}
|
// '']);
|
||||||
Cache.Clear;
|
Cache.Clear;
|
||||||
Cache.IdentifierCode:=IdentifierCode;
|
Cache.IdentifierCode:=IdentifierCode;
|
||||||
Cache.X:=X;
|
Cache.X:=X;
|
||||||
@ -2673,7 +2673,7 @@ begin
|
|||||||
CursorPos.Y:=Y;
|
CursorPos.Y:=Y;
|
||||||
CursorPos.Code:=IdentifierCode;
|
CursorPos.Code:=IdentifierCode;
|
||||||
try
|
try
|
||||||
Result:=FCurCodeTool.FindDeclaration(CursorPos,[fsfFindMainDeclaration],
|
Result:=FCurCodeTool.FindDeclaration(CursorPos,[fsfFindMainDeclaration,fsfSearchSourceName],
|
||||||
Cache.NewTool,Cache.NewNode,Cache.NewPos,NewTopLine);
|
Cache.NewTool,Cache.NewNode,Cache.NewPos,NewTopLine);
|
||||||
except
|
except
|
||||||
on e: Exception do HandleException(e);
|
on e: Exception do HandleException(e);
|
||||||
@ -2705,12 +2705,14 @@ begin
|
|||||||
if NewTopLine=0 then ;
|
if NewTopLine=0 then ;
|
||||||
if not InitCurCodeTool(SearchInCode) then exit;
|
if not InitCurCodeTool(SearchInCode) then exit;
|
||||||
if Cache.IsPrivate and (FCurCodeTool<>Cache.NewTool) then begin
|
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);
|
exit(true);
|
||||||
end;
|
end;
|
||||||
|
|
||||||
CursorPos:=Cache.NewPos;
|
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);
|
DebugLn('TCodeToolManager.FindReferences Searching ',dbgs(FCurCodeTool.Scanner<>nil),' for reference to x=',dbgs(CursorPos.X),' y=',dbgs(CursorPos.Y),' ',CursorPos.Code.Filename);
|
||||||
{$ENDIF}
|
{$ENDIF}
|
||||||
try
|
try
|
||||||
|
|||||||
@ -363,6 +363,7 @@ type
|
|||||||
//-----------------------------------------------------------------------------
|
//-----------------------------------------------------------------------------
|
||||||
// useful functions
|
// useful functions
|
||||||
function NodeDescriptionAsString(Desc: TCodeTreeNodeDesc): string;
|
function NodeDescriptionAsString(Desc: TCodeTreeNodeDesc): string;
|
||||||
|
function NodePathAsString(Node: TCodeTreeNode): string;
|
||||||
procedure WriteNodeExtTree(Tree: TAVLTree);
|
procedure WriteNodeExtTree(Tree: TAVLTree);
|
||||||
function FindCodeTreeNodeExt(Tree: TAVLTree; const Txt: string
|
function FindCodeTreeNodeExt(Tree: TAVLTree; const Txt: string
|
||||||
): TCodeTreeNodeExtension;
|
): TCodeTreeNodeExtension;
|
||||||
@ -517,6 +518,19 @@ begin
|
|||||||
end;
|
end;
|
||||||
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);
|
procedure WriteNodeExtTree(Tree: TAVLTree);
|
||||||
var
|
var
|
||||||
Node: TAVLTreeNode;
|
Node: TAVLTreeNode;
|
||||||
|
|||||||
@ -2021,7 +2021,7 @@ var
|
|||||||
NewTool: TFindDeclarationTool;
|
NewTool: TFindDeclarationTool;
|
||||||
NewNode: TCodeTreeNode;
|
NewNode: TCodeTreeNode;
|
||||||
begin
|
begin
|
||||||
Result:=FindDeclaration(CursorPos,[fsfFindMainDeclaration],NewTool,NewNode,
|
Result:=FindDeclaration(CursorPos,[fsfFindMainDeclaration,fsfSearchSourceName],NewTool,NewNode,
|
||||||
NewPos,NewTopLine);
|
NewPos,NewTopLine);
|
||||||
end;
|
end;
|
||||||
|
|
||||||
@ -2552,8 +2552,9 @@ function TFindDeclarationTool.FindDeclaration(const CursorPos: TCodeXYPosition;
|
|||||||
var
|
var
|
||||||
ExprType: TExpressionType;
|
ExprType: TExpressionType;
|
||||||
begin
|
begin
|
||||||
Result := FindDeclaration(CursorPos, SearchSmartFlags, ExprType, NewPos, NewTopLine, BlockTopLine, BlockBottomLine) and
|
Result := FindDeclaration(CursorPos, SearchSmartFlags, ExprType,
|
||||||
(NewPos.X >= 0) and (NewPos.Y >= 0);
|
NewPos, NewTopLine, BlockTopLine, BlockBottomLine)
|
||||||
|
and (NewPos.X >= 0) and (NewPos.Y >= 0);
|
||||||
if Result then begin
|
if Result then begin
|
||||||
NewTool := ExprType.Context.Tool;
|
NewTool := ExprType.Context.Tool;
|
||||||
NewNode := ExprType.Context.Node;
|
NewNode := ExprType.Context.Node;
|
||||||
@ -4980,8 +4981,9 @@ begin
|
|||||||
|
|
||||||
ctnIdentifier:
|
ctnIdentifier:
|
||||||
if (ContextNode.Parent.Desc in [ctnConstDefinition,ctnVarDefinition])
|
if (ContextNode.Parent.Desc in [ctnConstDefinition,ctnVarDefinition])
|
||||||
and (ContextNode=ContextNode.Parent.LastChild)
|
and (ContextNode=ContextNode.Parent.LastChild)
|
||||||
and SearchInTypeOfVarConst then exit;
|
and SearchInTypeOfVarConst then
|
||||||
|
exit;
|
||||||
|
|
||||||
ctnEnumIdentifier,ctnLabel:
|
ctnEnumIdentifier,ctnLabel:
|
||||||
if SearchInEnumLabelDefinition then exit;
|
if SearchInEnumLabelDefinition then exit;
|
||||||
@ -6264,8 +6266,8 @@ var
|
|||||||
Identifier: string;
|
Identifier: string;
|
||||||
CleanDeclCursorPos: integer;
|
CleanDeclCursorPos: integer;
|
||||||
DeclarationTool: TFindDeclarationTool;
|
DeclarationTool: TFindDeclarationTool;
|
||||||
DeclarationNode: TCodeTreeNode;
|
DeclarationNode: TCodeTreeNode; // in DeclarationTool
|
||||||
AliasDeclarationNode: TCodeTreeNode; // if exists: always in front of DeclarationNode
|
AliasDeclarationNode: TCodeTreeNode; // if exists: always in front of DeclarationNode, and in DeclarationTool
|
||||||
Params: TFindDeclarationParams;
|
Params: TFindDeclarationParams;
|
||||||
PosTree: TAVLTree; // tree of PChar positions in Src
|
PosTree: TAVLTree; // tree of PChar positions in Src
|
||||||
ReferencePos: TCodeXYPosition;
|
ReferencePos: TCodeXYPosition;
|
||||||
@ -6281,7 +6283,9 @@ var
|
|||||||
if PosTree=nil then
|
if PosTree=nil then
|
||||||
PosTree:=TAVLTree.Create;
|
PosTree:=TAVLTree.Create;
|
||||||
p:=@Src[ACleanPos];
|
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
|
if PosTree.Find(p)=nil then
|
||||||
PosTree.Add(p);
|
PosTree.Add(p);
|
||||||
end;
|
end;
|
||||||
@ -6314,6 +6318,11 @@ var
|
|||||||
var
|
var
|
||||||
IdentStartPos: Integer;
|
IdentStartPos: Integer;
|
||||||
IdentEndPos: integer;
|
IdentEndPos: integer;
|
||||||
|
UnitNamePos, UnitInFilePos: TAtomPosition;
|
||||||
|
NewCodeTool: TFindDeclarationTool;
|
||||||
|
AnUnitName: String;
|
||||||
|
UnitInFilename: AnsiString;
|
||||||
|
Node: TCodeTreeNode;
|
||||||
begin
|
begin
|
||||||
if (not IsComment) then
|
if (not IsComment) then
|
||||||
UnitStartFound:=true;
|
UnitStartFound:=true;
|
||||||
@ -6326,17 +6335,19 @@ var
|
|||||||
if IdentEndPos-IdentStartPos<>length(Identifier) then exit;
|
if IdentEndPos-IdentStartPos<>length(Identifier) then exit;
|
||||||
if CompareIdentifiers(PChar(Pointer(Identifier)),@Src[IdentStartPos])<>0 then exit;
|
if CompareIdentifiers(PChar(Pointer(Identifier)),@Src[IdentStartPos])<>0 then exit;
|
||||||
if IsComment and (SkipComments or (not UnitStartFound)) 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]),
|
IdentStartPos,'=',CleanPosToStr(StartPos),' ',GetIdentifier(@Src[IdentStartPos]),
|
||||||
' CleanDeclCursorPos=',CleanDeclCursorPos,
|
' CleanDeclCursorPos=',CleanDeclCursorPos,
|
||||||
' MaxPos=',MaxPos,
|
' MaxPos=',MaxPos,
|
||||||
' IsComment=',IsComment,
|
' IsComment=',IsComment,
|
||||||
' SkipComments=',SkipComments,
|
' SkipComments=',SkipComments,
|
||||||
' UnitStartFound=',UnitStartFound
|
' UnitStartFound=',UnitStartFound
|
||||||
]);}
|
]);
|
||||||
|
{$ENDIF}
|
||||||
|
|
||||||
CursorNode:=BuildSubTreeAndFindDeepestNodeAtPos(IdentStartPos,true);
|
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)
|
if (DeclarationTool=Self)
|
||||||
and ((IdentStartPos=CleanDeclCursorPos) or (CursorNode=AliasDeclarationNode))
|
and ((IdentStartPos=CleanDeclCursorPos) or (CursorNode=AliasDeclarationNode))
|
||||||
@ -6377,12 +6388,34 @@ var
|
|||||||
end;
|
end;
|
||||||
|
|
||||||
//debugln(' Found=',dbgs(Found));
|
//debugln(' Found=',dbgs(Found));
|
||||||
if Found and (Params.NewNode<>nil) then begin
|
Node:=Params.NewNode;
|
||||||
UseProcHead(Params.NewNode);
|
if Found and (Node<>nil) then begin
|
||||||
//debugln('Context=',Params.NewNode.DescAsString,' ',dbgs(Params.NewNode.StartPos),' ',dbgs(DeclarationNode.StartPos));
|
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)
|
if (Params.NewNode=DeclarationNode)
|
||||||
or (Params.NewNode=AliasDeclarationNode) then begin
|
or (Params.NewNode=AliasDeclarationNode) then begin
|
||||||
//debugln(['ReadIdentifier reference found, adding ...']);
|
debugln(['ReadIdentifier reference found, adding ...']);
|
||||||
AddReference(IdentStartPos);
|
AddReference(IdentStartPos);
|
||||||
end;
|
end;
|
||||||
end;
|
end;
|
||||||
@ -6559,7 +6592,7 @@ var
|
|||||||
end;
|
end;
|
||||||
|
|
||||||
// find alias declaration node
|
// find alias declaration node
|
||||||
//debugln('FindDeclarationNode DeclarationNode=',DeclarationNode.DescAsString);
|
//debugln('FindDeclarationNode DeclarationNode=',NodePathAsString(DeclarationNode),' at ',DeclarationTool.CleanPosToStr(DeclarationNode.StartPos));
|
||||||
AliasDeclarationNode:=nil;
|
AliasDeclarationNode:=nil;
|
||||||
case DeclarationNode.Desc of
|
case DeclarationNode.Desc of
|
||||||
|
|
||||||
@ -6574,7 +6607,7 @@ var
|
|||||||
|
|
||||||
ctnVarDefinition:
|
ctnVarDefinition:
|
||||||
if DeclarationNode.HasParentOfType(ctnProcedureHead) then begin
|
if DeclarationNode.HasParentOfType(ctnProcedureHead) then begin
|
||||||
AliasDeclarationNode:=FindCorrespondingProcParamNode(DeclarationNode,ProcAttr);
|
AliasDeclarationNode:=DeclarationTool.FindCorrespondingProcParamNode(DeclarationNode,ProcAttr);
|
||||||
end;
|
end;
|
||||||
|
|
||||||
ctnTypeDefinition:
|
ctnTypeDefinition:
|
||||||
@ -6598,7 +6631,6 @@ var
|
|||||||
AliasDeclarationNode:=DeclarationNode;
|
AliasDeclarationNode:=DeclarationNode;
|
||||||
DeclarationNode:=Node;
|
DeclarationNode:=Node;
|
||||||
end;
|
end;
|
||||||
//debugln('FindDeclarationNode AliasDeclarationNode=',AliasDeclarationNode.DescAsString,' ',DeclarationTool.CleanPosToStr(AliasDeclarationNode.StartPos,DeclarationTool<>Self));
|
|
||||||
end;
|
end;
|
||||||
|
|
||||||
// search comment in front of declaration
|
// search comment in front of declaration
|
||||||
@ -6635,7 +6667,7 @@ var
|
|||||||
if MaxPos>SrcLen then MaxPos:=SrcLen;
|
if MaxPos>SrcLen then MaxPos:=SrcLen;
|
||||||
|
|
||||||
if DeclarationTool<>Self then begin
|
if DeclarationTool<>Self then begin
|
||||||
MinPos:=UseNode.Parent.EndPos;
|
MinPos:=UseNode.StartPos;
|
||||||
exit;
|
exit;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
@ -6698,7 +6730,9 @@ var
|
|||||||
AVLNode: TAVLTreeNode;
|
AVLNode: TAVLTreeNode;
|
||||||
begin
|
begin
|
||||||
Result:=false;
|
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;
|
ListOfPCodeXYPosition:=nil;
|
||||||
Params:=nil;
|
Params:=nil;
|
||||||
@ -6956,6 +6990,10 @@ begin
|
|||||||
|
|
||||||
ctnUnit:
|
ctnUnit:
|
||||||
Result:=PositionInSourceName(CleanPos);
|
Result:=PositionInSourceName(CleanPos);
|
||||||
|
|
||||||
|
ctnIdentifier:
|
||||||
|
if Node.Parent.Desc=ctnSrcName then
|
||||||
|
Result:=true;
|
||||||
end;
|
end;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
|
|||||||
@ -274,7 +274,7 @@ begin
|
|||||||
CodeToolBoss.GetIdentifierAt(DeclCode,DeclarationCaretXY.X,DeclarationCaretXY.Y,Identifier);
|
CodeToolBoss.GetIdentifierAt(DeclCode,DeclarationCaretXY.X,DeclarationCaretXY.Y,Identifier);
|
||||||
CurUnitname:=ExtractFileNameOnly(DeclCode.Filename);
|
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;
|
Files:=nil;
|
||||||
OwnerList:=nil;
|
OwnerList:=nil;
|
||||||
@ -943,6 +943,7 @@ var
|
|||||||
begin
|
begin
|
||||||
FIdentifierFilename:=NewIdentifierFilename;
|
FIdentifierFilename:=NewIdentifierFilename;
|
||||||
FIdentifierPosition:=NewIdentifierPosition;
|
FIdentifierPosition:=NewIdentifierPosition;
|
||||||
|
//debugln(['TFindRenameIdentifierDialog.SetIdentifier ',FIdentifierFilename,' ',dbgs(FIdentifierPosition)]);
|
||||||
CurrentListBox.Items.Clear;
|
CurrentListBox.Items.Clear;
|
||||||
s:=IdentifierFilename
|
s:=IdentifierFilename
|
||||||
+'('+IntToStr(IdentifierPosition.Y)+','+IntToStr(IdentifierPosition.X)+')';
|
+'('+IntToStr(IdentifierPosition.Y)+','+IntToStr(IdentifierPosition.X)+')';
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user