22
22
import fr .adrienbrault .idea .symfony2plugin .templating .variable .TwigFileVariableCollectorParameter ;
23
23
import fr .adrienbrault .idea .symfony2plugin .templating .variable .TwigTypeContainer ;
24
24
import fr .adrienbrault .idea .symfony2plugin .templating .variable .collector .*;
25
+ import fr .adrienbrault .idea .symfony2plugin .templating .variable .dict .PsiVariable ;
26
+ import fr .adrienbrault .idea .symfony2plugin .templating .variable .resolver .FormFieldResolver ;
27
+ import fr .adrienbrault .idea .symfony2plugin .templating .variable .resolver .FormVarsResolver ;
28
+ import fr .adrienbrault .idea .symfony2plugin .templating .variable .resolver .TwigTypeResolver ;
25
29
import fr .adrienbrault .idea .symfony2plugin .util .PhpElementsUtil ;
26
30
import fr .adrienbrault .idea .symfony2plugin .util .PsiElementUtils ;
27
31
import fr .adrienbrault .idea .symfony2plugin .util .yaml .YamlHelper ;
28
32
import org .apache .commons .lang .StringUtils ;
29
- import org .jetbrains .annotations .Nullable ;
30
33
31
34
import java .util .*;
32
35
import java .util .regex .Matcher ;
@@ -46,6 +49,11 @@ public class TwigTypeResolveUtil {
46
49
new ControllerVariableCollector (),
47
50
};
48
51
52
+ private static TwigTypeResolver [] twigTypeResolvers = new TwigTypeResolver [] {
53
+ new FormVarsResolver (),
54
+ new FormFieldResolver (),
55
+ };
56
+
49
57
public static String [] formatPsiTypeName (PsiElement psiElement , boolean includeCurrent ) {
50
58
ArrayList <String > strings = new ArrayList <String >(Arrays .asList (formatPsiTypeName (psiElement )));
51
59
strings .add (psiElement .getText ());
@@ -81,12 +89,23 @@ public static Collection<TwigTypeContainer> resolveTwigMethodName(PsiElement psi
81
89
82
90
Collection <? extends PhpNamedElement > rootVariable = getRootVariableByName (psiElement , typeName [0 ]);
83
91
if (typeName .length == 1 ) {
84
- return TwigTypeContainer .fromCollection (rootVariable );
92
+
93
+ Collection <TwigTypeContainer > twigTypeContainers = TwigTypeContainer .fromCollection (rootVariable );
94
+
95
+ for (TwigTypeResolver twigTypeResolver : twigTypeResolvers ) {
96
+ twigTypeResolver .resolve (twigTypeContainers , twigTypeContainers , typeName [0 ], new ArrayList <List <TwigTypeContainer >>());
97
+ }
98
+
99
+ return twigTypeContainers ;
85
100
}
86
101
87
102
Collection <TwigTypeContainer > type = TwigTypeContainer .fromCollection (rootVariable );
103
+ Collection <List <TwigTypeContainer >> previousElements = new ArrayList <List <TwigTypeContainer >> ();
104
+ previousElements .add (new ArrayList <TwigTypeContainer >(type ));
105
+
88
106
for (int i = 1 ; i <= typeName .length - 1 ; i ++ ) {
89
- type = resolveTwigMethodName (type , typeName [i ]);
107
+ type = resolveTwigMethodName (type , typeName [i ], previousElements );
108
+ previousElements .add (new ArrayList <TwigTypeContainer >(type ));
90
109
91
110
// we can stop on empty list
92
111
if (type .size () == 0 ) {
@@ -95,7 +114,7 @@ public static Collection<TwigTypeContainer> resolveTwigMethodName(PsiElement psi
95
114
96
115
}
97
116
98
- return TwigTypeContainer . fromCollection ( rootVariable ) ;
117
+ return type ;
99
118
}
100
119
101
120
/**
@@ -165,26 +184,36 @@ private static HashMap<String, Set<String>> convertHashMapToTypeSet(HashMap<Stri
165
184
return globalVars ;
166
185
}
167
186
168
- public static HashMap <String , Set < String > > collectScopeVariables (PsiElement psiElement ) {
187
+ public static HashMap <String , PsiVariable > collectScopeVariables (PsiElement psiElement ) {
169
188
170
189
HashMap <String , Set <String >> globalVars = new HashMap <String , Set <String >>();
190
+ HashMap <String , PsiVariable > controllerVars = new HashMap <String , PsiVariable >();
171
191
172
192
TwigFileVariableCollectorParameter collectorParameter = new TwigFileVariableCollectorParameter (psiElement );
173
193
for (TwigFileVariableCollector collector : twigFileVariableCollectors ) {
174
194
collector .collect (collectorParameter , globalVars );
195
+
196
+ if (collector instanceof TwigFileVariableCollector .TwigFileVariableCollectorExt ) {
197
+ ((TwigFileVariableCollector .TwigFileVariableCollectorExt ) collector ).collectVars (collectorParameter , controllerVars );
198
+ }
199
+
175
200
}
176
201
177
202
// globals first
178
203
globalVars .putAll (convertHashMapToTypeSet (findInlineStatementVariableDocBlock (psiElement , TwigElementTypes .BLOCK_STATEMENT )));
179
204
globalVars .putAll (convertHashMapToTypeSet (findInlineStatementVariableDocBlock (psiElement , TwigElementTypes .FOR_STATEMENT )));
180
205
181
206
// check if we are in "for" scope and resolve types ending with []
182
- collectForArrayScopeVariables (psiElement , globalVars );
207
+ collectForArrayScopeVariables (psiElement , controllerVars );
183
208
184
- return globalVars ;
209
+ for (Map .Entry <String , Set <String >> entry : globalVars .entrySet ()) {
210
+ controllerVars .put (entry .getKey (), new PsiVariable (entry .getValue (), null ));
211
+ }
212
+
213
+ return controllerVars ;
185
214
}
186
215
187
- private static void collectForArrayScopeVariables (PsiElement psiElement , HashMap <String , Set < String > > globalVars ) {
216
+ private static void collectForArrayScopeVariables (PsiElement psiElement , HashMap <String , PsiVariable > globalVars ) {
188
217
189
218
PsiElement twigCompositeElement = PsiTreeUtil .findFirstParent (psiElement , new Condition <PsiElement >() {
190
219
@ Override
@@ -226,24 +255,24 @@ public boolean value(PsiElement psiElement) {
226
255
Set <String > types = new HashSet <String >();
227
256
228
257
PhpType phpType = new PhpType ();
229
- phpType .add (globalVars .get (variableName ));
258
+ phpType .add (globalVars .get (variableName ). getTypes () );
230
259
231
260
for (String arrayType : PhpIndex .getInstance (psiElement .getProject ()).completeType (psiElement .getProject (), phpType , new HashSet <String >()).getTypes ()) {
232
261
if (arrayType .endsWith ("[]" )) {
233
262
types .add (arrayType .substring (0 , arrayType .length () -2 ));
234
263
}
235
264
}
236
265
237
- globalVars .put (scopeVariable , types );
266
+ globalVars .put (scopeVariable , new PsiVariable ( types ) );
238
267
239
268
}
240
269
241
270
private static Collection <? extends PhpNamedElement > getRootVariableByName (PsiElement psiElement , String variableName ) {
242
271
243
272
ArrayList <PhpNamedElement > phpNamedElements = new ArrayList <PhpNamedElement >();
244
- for (Map .Entry <String , Set < String > > variable : collectScopeVariables (psiElement ).entrySet ()) {
273
+ for (Map .Entry <String , PsiVariable > variable : collectScopeVariables (psiElement ).entrySet ()) {
245
274
if (variable .getKey ().equals (variableName )) {
246
- phpNamedElements .addAll (PhpElementsUtil .getClassFromPhpTypeSet (psiElement .getProject (), variable .getValue ()));
275
+ phpNamedElements .addAll (PhpElementsUtil .getClassFromPhpTypeSet (psiElement .getProject (), variable .getValue (). getTypes () ));
247
276
}
248
277
249
278
}
@@ -252,7 +281,7 @@ private static Collection<? extends PhpNamedElement> getRootVariableByName(PsiEl
252
281
253
282
}
254
283
255
- private static Collection <TwigTypeContainer > resolveTwigMethodName (Collection <TwigTypeContainer > previousElement , String typeName ) {
284
+ private static Collection <TwigTypeContainer > resolveTwigMethodName (Collection <TwigTypeContainer > previousElement , String typeName , Collection < List < TwigTypeContainer >> twigTypeContainer ) {
256
285
257
286
ArrayList <TwigTypeContainer > phpNamedElements = new ArrayList <TwigTypeContainer >();
258
287
@@ -270,6 +299,10 @@ private static Collection<TwigTypeContainer> resolveTwigMethodName(Collection<Tw
270
299
}
271
300
}
272
301
302
+ for (TwigTypeResolver twigTypeResolver : twigTypeResolvers ) {
303
+ twigTypeResolver .resolve (phpNamedElements , previousElement , typeName , twigTypeContainer );
304
+ }
305
+
273
306
}
274
307
275
308
return phpNamedElements ;
0 commit comments