From 0c415f0a1d0f429cebb9bee6387881c48835b635 Mon Sep 17 00:00:00 2001 From: mattias Date: Mon, 7 Feb 2011 10:05:58 +0000 Subject: [PATCH] codetools: find declaration: started search in type of var/const git-svn-id: trunk@29407 - --- .../codetools/examples/finddeclaration.lpr | 12 ++++++-- components/codetools/finddeclarationtool.pas | 30 ++++++++++++++----- 2 files changed, 32 insertions(+), 10 deletions(-) diff --git a/components/codetools/examples/finddeclaration.lpr b/components/codetools/examples/finddeclaration.lpr index b6b3df54a9..b4db52182f 100644 --- a/components/codetools/examples/finddeclaration.lpr +++ b/components/codetools/examples/finddeclaration.lpr @@ -42,6 +42,7 @@ var X: Integer; Y: Integer; Filename: String; + Line: String; begin if (ParamCount>=1) and (Paramcount<3) then begin writeln('Usage:'); @@ -87,22 +88,29 @@ begin writeln('FPCTARGET=',Options.TargetOS); writeln('FPCTARGETCPU=',Options.TargetProcessor); if (ParamCount>=3) then begin - Options.TestPascalFile:=CleanAndExpandFilename(ParamStr(1)); + Filename:=CleanAndExpandFilename(ParamStr(1)); X:=StrToInt(ParamStr(2)); Y:=StrToInt(ParamStr(3)); end; + writeln('File: ',Filename,' Line=',Y,' Column=',X); // Step 1: load the file Code:=CodeToolBoss.LoadFile(Filename,false,false); if Code=nil then raise Exception.Create('loading failed '+Filename); + Line:=Code.GetLine(Y-1); + writeln('Line ',Y,': ',copy(Line,1,X-1),'|',copy(Line,X,length(Line))); + // Step 2: find declaration if CodeToolBoss.FindDeclaration(Code,X,Y,NewCode,NewX,NewY,NewTopLine) then begin writeln('Declaration found: ',NewCode.Filename,' Line=',NewY,' Column=',NewX); end else begin - writeln('Parse error: ',CodeToolBoss.ErrorMessage); + if CodeToolBoss.ErrorMessage<>'' then + writeln('Parse error: ',CodeToolBoss.ErrorMessage) + else + writeln('Declaration not found'); end; except on E: Exception do begin diff --git a/components/codetools/finddeclarationtool.pas b/components/codetools/finddeclarationtool.pas index 0a31fe0c8f..9f698bb11d 100644 --- a/components/codetools/finddeclarationtool.pas +++ b/components/codetools/finddeclarationtool.pas @@ -2713,6 +2713,7 @@ var NameNode:=ContextNode.FirstChild; if NameNode=nil then exit; end; + if (fdfCollect in Params.Flags) or CompareSrcIdentifiers(NameNode.StartPos,Params.Identifier) then begin @@ -2740,13 +2741,18 @@ var end; end; - function SearchInConstant: boolean; + function SearchInTypeOfVarConst: boolean; // returns: true if ok to exit // false if search should continue begin Result:=false; - if (ContextNode.Parent.Desc in [ctnVarDefinition,ctnConstDefinition]) then - Result:=FindIdentifierInTypeOfConstant(ContextNode.Parent,Params); + //debugln(['SearchInTypeOfVarConst ',ContextNode.Parent.DescAsString]); + if ContextNode.Parent.Desc in [ctnConstDefinition,ctnVarDefinition] then + begin + if FindIdentifierInTypeOfConstant(ContextNode.Parent,Params) then begin + Result:=CheckResult(true,false); + end; + end; end; function SearchInEnumDefinition: boolean; @@ -3089,7 +3095,7 @@ begin end; if FirstSearchedNode=nil then FirstSearchedNode:=ContextNode; LastSearchedNode:=ContextNode; - + case ContextNode.Desc of ctnTypeSection, ctnVarSection, ctnConstSection, ctnResStrSection, @@ -3111,8 +3117,10 @@ begin ctnGlobalProperty, ctnGenericType: if SearchInTypeVarConstPropDefinition then exit; - ctnConstant: - if SearchInConstant then exit; + ctnIdentifier: + if (ContextNode.Parent.Desc in [ctnConstDefinition,ctnVarDefinition]) + and (ContextNode=ContextNode.Parent.FirstChild) + and SearchInTypeOfVarConst then exit; ctnEnumIdentifier: if SearchInEnumDefinition then exit; @@ -5997,11 +6005,17 @@ function TFindDeclarationTool.FindIdentifierInTypeOfConstant( { const a: atype = context; for example: const p: TPoint = (x:0; y:0); } +var + TypeNode: TCodeTreeNode; begin Result:=false; - if VarConstNode.FirstChild=nil then exit; - if VarConstNode.Desc=ctnIdentifier then begin + //debugln(['TFindDeclarationTool.FindIdentifierInTypeOfConstant ',VarConstNode.DescAsString]); + TypeNode:=VarConstNode.FirstChild; + if TypeNode=nil then exit; + if TypeNode.Desc=ctnIdentifier then begin debugln(['TFindDeclarationTool.FindIdentifierInTypeOfConstant ']); + //ExprType:=FindExpressionTypeOfTerm(TypeNode.StartPos,-1,Params); + end; end;