codetools: find references: limit scope for local variables, bug #16826

git-svn-id: trunk@26436 -
This commit is contained in:
mattias 2010-07-03 08:09:07 +00:00
parent f4a3839b99
commit 09c1e1892f

View File

@ -3926,10 +3926,19 @@ var
if PosTree=nil then if PosTree=nil then
PosTree:=TAVLTree.Create; PosTree:=TAVLTree.Create;
p:=@Src[StartPos]; p:=@Src[StartPos];
//debugln('TFindDeclarationTool.FindReferences.AddReference ',DbgS(p),' ',dbgs(PosTree.Find(p)=nil)); //debugln('TFindDeclarationTool.FindReferences.AddReference ',CleanPosToStr(StartPos),' ',dbgs(PosTree.Find(p)=nil));
if PosTree.Find(p)=nil then if PosTree.Find(p)=nil then
PosTree.Add(p); PosTree.Add(p);
end; end;
procedure UseProcHead(var Node: TCodeTreeNode);
begin
if Node=nil then exit;
if (Node.Desc=ctnProcedure)
and (Node.FirstChild<>nil)
and (Node.FirstChild.Desc=ctnProcedureHead) then
Node:=Node.FirstChild;
end;
procedure ReadIdentifier(IsComment: boolean); procedure ReadIdentifier(IsComment: boolean);
var var
@ -4005,15 +4014,7 @@ var
//debugln(' Found=',dbgs(Found)); //debugln(' Found=',dbgs(Found));
if Found and (Params.NewNode<>nil) then begin if Found and (Params.NewNode<>nil) then begin
if (Params.NewNode.Desc=ctnProcedure) UseProcHead(Params.NewNode);
and (Params.NewNode.FirstChild<>nil)
and (Params.NewNode.FirstChild.Desc=ctnProcedureHead) then begin
// Instead of jumping to the procedure keyword,
// jump to the procedure name
Params.NewNode:=Params.NewNode.FirstChild;
Params.NewCodeTool.MoveCursorToProcName(Params.NewNode,true);
Params.NewCleanPos:=Params.NewCodeTool.CurPos.StartPos;
end;
//debugln('Context=',Params.NewNode.DescAsString,' ',dbgs(Params.NewNode.StartPos),' ',dbgs(DeclarationNode.StartPos)); //debugln('Context=',Params.NewNode.DescAsString,' ',dbgs(Params.NewNode.StartPos),' ',dbgs(DeclarationNode.StartPos));
if (Params.NewNode=DeclarationNode) if (Params.NewNode=DeclarationNode)
or (Params.NewNode=AliasDeclarationNode) then or (Params.NewNode=AliasDeclarationNode) then
@ -4136,6 +4137,8 @@ var
end; end;
function FindDeclarationNode: boolean; function FindDeclarationNode: boolean;
var
Node: TCodeTreeNode;
begin begin
Result:=false; Result:=false;
@ -4158,6 +4161,9 @@ var
debugln('FindDeclarationNode Identifier="',Identifier,'"'); debugln('FindDeclarationNode Identifier="',Identifier,'"');
exit; exit;
end; end;
UseProcHead(DeclarationNode);
StartPos:=DeclarationNode.StartPos;
AddReference;
// find alias declaration node // find alias declaration node
//debugln('FindDeclarationNode DeclarationNode=',DeclarationNode.DescAsString); //debugln('FindDeclarationNode DeclarationNode=',DeclarationNode.DescAsString);
@ -4176,12 +4182,16 @@ var
end; end;
end; end;
if (AliasDeclarationNode<>nil) and (AliasDeclarationNode.Desc=ctnProcedure)
and (AliasDeclarationNode.FirstChild<>nil)
and (AliasDeclarationNode.FirstChild.Desc=ctnProcedureHead) then
AliasDeclarationNode:=AliasDeclarationNode.FirstChild;
if AliasDeclarationNode<>nil then begin if AliasDeclarationNode<>nil then begin
UseProcHead(AliasDeclarationNode);
StartPos:=AliasDeclarationNode.StartPos;
AddReference;
if AliasDeclarationNode.StartPos>DeclarationNode.StartPos then begin
Node:=AliasDeclarationNode;
AliasDeclarationNode:=DeclarationNode;
DeclarationNode:=Node;
end;
//debugln('FindDeclarationNode AliasDeclarationNode=',AliasDeclarationNode.DescAsString); //debugln('FindDeclarationNode AliasDeclarationNode=',AliasDeclarationNode.DescAsString);
end; end;
@ -4204,17 +4214,34 @@ var
ctnImplementation: ctnImplementation:
// only search in implementation // only search in implementation
if MinPos<Node.StartPos then MinPos:=Node.StartPos; if MinPos<Node.StartPos then MinPos:=Node.StartPos;
ctnVarDefinition,ctnConstDefinition,ctnLabelType:
ctnTypeDefinition:
begin
// Note: types can be used before declaration
end;
ctnVarDefinition,ctnConstDefinition,ctnLabelType,ctnEnumIdentifier:
begin begin
// only search behind variable // only search behind variable
if MinPos<Node.StartPos then MinPos:=Node.StartPos; if MinPos<Node.StartPos then MinPos:=Node.StartPos;
if (Node.Parent.Desc in [ctnVarSection,ctnConstSection,ctnLabelSection,ctnResStrSection]) end;
and (Node.Parent.Parent.Desc=ctnProcedure) then begin
// only search till procedure end ctnProcedure:
if MaxPos>Node.Parent.Parent.EndPos then begin
MaxPos:=Node.Parent.Parent.EndPos; if (DeclarationNode<>Node)
and (DeclarationNode<>Node.FirstChild)
and (AliasDeclarationNode<>Node)
and (AliasDeclarationNode<>Node.FirstChild)
then begin
// DeclarationNode is a local identifier
// limit scope to procedure
if MinPos<Node.FirstChild.EndPos then
MinPos:=Node.FirstChild.EndPos;
if MaxPos>Node.EndPos then
MaxPos:=Node.EndPos;
end; end;
end; end;
end; end;
Node:=Node.Parent; Node:=Node.Parent;
end; end;