From b56730e82444d1f3cc2799be45bb75831f554683 Mon Sep 17 00:00:00 2001 From: mattias Date: Fri, 3 Jul 2020 10:23:06 +0000 Subject: [PATCH] codetools: var completion of for-var-in-genericarray, from Pascal Riekenberg, issue #37076 git-svn-id: branches/fixes_2_0@63496 - --- components/codetools/finddeclarationtool.pas | 56 +++++++++++-------- .../moduletests/fdt_generics_guesstype.pas | 4 ++ 2 files changed, 37 insertions(+), 23 deletions(-) diff --git a/components/codetools/finddeclarationtool.pas b/components/codetools/finddeclarationtool.pas index 470253f571..cdc1745065 100644 --- a/components/codetools/finddeclarationtool.pas +++ b/components/codetools/finddeclarationtool.pas @@ -844,7 +844,8 @@ type function FindEnumerationTypeOfSetType(SetTypeNode: TCodeTreeNode; out Context: TFindContext): boolean; function FindElementTypeOfArrayType(ArrayNode: TCodeTreeNode; - out ExprType: TExpressionType; AliasType: PFindContext): boolean; + out ExprType: TExpressionType; AliasType: PFindContext; + ParentParams: TFindDeclarationParams = nil): boolean; function CheckOperatorEnumerator(Params: TFindDeclarationParams; const FoundContext: TFindContext): TIdentifierFoundResult; function CheckModifierEnumeratorCurrent({%H-}Params: TFindDeclarationParams; @@ -12748,7 +12749,7 @@ function TFindDeclarationTool.FindForInTypeAsString(TermPos: TAtomPosition; begin AliasType:=CleanFindContext; if SubExprType.Context.Tool.FindElementTypeOfArrayType( - SubExprType.Context.Node,ExprType,@AliasType) + SubExprType.Context.Node,ExprType,@AliasType,Params) then begin Result:=FindExprTypeAsString(ExprType,TermPos.StartPos,@AliasType); end; @@ -13145,7 +13146,7 @@ end; function TFindDeclarationTool.FindElementTypeOfArrayType( ArrayNode: TCodeTreeNode; out ExprType: TExpressionType; - AliasType: PFindContext): boolean; + AliasType: PFindContext; ParentParams: TFindDeclarationParams): boolean; var Params: TFindDeclarationParams; p: LongInt; @@ -13156,27 +13157,36 @@ begin if (ArrayNode=nil) then exit; if (ArrayNode.Desc<>ctnOpenArrayType) and (ArrayNode.Desc<>ctnRangedArrayType) then exit; - MoveCursorToNodeStart(ArrayNode); - ReadNextAtom; // array - if not UpAtomIs('ARRAY') then exit; - ReadNextAtom; // of - if CurPos.Flag=cafEdgedBracketOpen then begin - ReadTilBracketClose(true); - ReadNextAtom; - end; - if not UpAtomIs('OF') then exit; - ReadNextAtom; - if not AtomIsIdentifier then exit; - Params:=TFindDeclarationParams.Create; - try - Params.Flags:=fdfDefaultForExpressions; - Params.ContextNode:=ArrayNode; - p:=CurPos.StartPos; - Params.SetIdentifier(Self,@Src[p],nil); - ExprType:=FindExpressionResultType(Params,p,-1,AliasType); + if (ArrayNode.Parent <> nil) + and (ArrayNode.Parent.Desc = ctnGenericType) + and (ParentParams <> nil) then begin + ExprType.Desc := xtContext; + ExprType.Context.Node := ParentParams.GenParamValueMappings.SpecializeParamsNode.FirstChild; + ExprType.Context.Tool := ParentParams.GenParamValueMappings.SpecializeParamsTool; Result:=true; - finally - Params.Free; + end else begin + MoveCursorToNodeStart(ArrayNode); + ReadNextAtom; // array + if not UpAtomIs('ARRAY') then exit; + ReadNextAtom; // of + if CurPos.Flag=cafEdgedBracketOpen then begin + ReadTilBracketClose(true); + ReadNextAtom; + end; + if not UpAtomIs('OF') then exit; + ReadNextAtom; + if not AtomIsIdentifier then exit; + Params:=TFindDeclarationParams.Create; + try + Params.Flags:=fdfDefaultForExpressions; + Params.ContextNode:=ArrayNode; + p:=CurPos.StartPos; + Params.SetIdentifier(Self,@Src[p],nil); + ExprType:=FindExpressionResultType(Params,p,-1,AliasType); + Result:=true; + finally + Params.Free; + end; end; end; diff --git a/components/codetools/tests/moduletests/fdt_generics_guesstype.pas b/components/codetools/tests/moduletests/fdt_generics_guesstype.pas index 5bd80522d4..25b3ac560a 100644 --- a/components/codetools/tests/moduletests/fdt_generics_guesstype.pas +++ b/components/codetools/tests/moduletests/fdt_generics_guesstype.pas @@ -111,6 +111,8 @@ type TOL3 = TOL2; TOL4 = class(TOL2); + generic TArray = array of T; + var OL_P1_1: TOL_P1_1; OL_P1_2: TOL_P1_2; @@ -124,6 +126,7 @@ var OL2: TOL2; OL3: TOL3; OL4: TOL4; + A: specialize TArray; begin for o_p1_1{guesstype:TObj} in OL_P1_1 do ; @@ -138,6 +141,7 @@ begin for o2{guesstype:TObj} in OL2 do ; for o3{guesstype:TObj} in OL3 do ; for o4{guesstype:TObj} in OL4 do ; + for i{guesstype:TObj} in A do; end.