@@ -224,61 +224,66 @@ private static Map<String, String> getInlineCommentDocsVars(@NotNull PsiElement
224
224
return variables ;
225
225
}
226
226
227
- private static Map <String , Set <String >> convertHashMapToTypeSet (Map <String , String > hashMap ) {
228
- Map <String , Set <String >> globalVars = new HashMap <>();
229
-
230
- for (final Map .Entry <String , String > entry : hashMap .entrySet ()) {
231
- globalVars .put (entry .getKey (), new HashSet <>(Collections .singletonList (entry .getValue ())));
232
- }
233
-
234
- return globalVars ;
235
- }
236
-
237
227
@ NotNull
238
228
public static Map <String , PsiVariable > collectScopeVariables (@ NotNull PsiElement psiElement ) {
239
229
return collectScopeVariables (psiElement , new HashSet <>());
240
230
}
241
231
242
232
@ NotNull
243
233
public static Map <String , PsiVariable > collectScopeVariables (@ NotNull PsiElement psiElement , @ NotNull Set <VirtualFile > visitedFiles ) {
244
- Map <String , Set <String >> globalVars = new HashMap <>();
245
- Map <String , PsiVariable > controllerVars = new HashMap <>();
246
-
247
234
VirtualFile virtualFile = psiElement .getContainingFile ().getVirtualFile ();
248
235
if (visitedFiles .contains (virtualFile )) {
249
- return controllerVars ;
236
+ return Collections . emptyMap () ;
250
237
}
251
238
252
239
visitedFiles .add (virtualFile );
253
240
241
+ Map <String , PsiVariable > controllerVars = new HashMap <>();
242
+
254
243
TwigFileVariableCollectorParameter collectorParameter = new TwigFileVariableCollectorParameter (psiElement , visitedFiles );
255
244
for (TwigFileVariableCollector collector : TWIG_FILE_VARIABLE_COLLECTORS .getExtensions ()) {
256
245
Map <String , Set <String >> globalVarsScope = new HashMap <>();
257
246
collector .collect (collectorParameter , globalVarsScope );
258
247
259
248
// @TODO: resolve this in change extension point, so that its only possible to provide data and dont give full scope to break / overwrite other variables
260
249
globalVarsScope .forEach ((s , strings ) -> {
261
- globalVars .putIfAbsent (s , new HashSet <> ());
262
- globalVars .get (s ).addAll (strings );
250
+ controllerVars .putIfAbsent (s , new PsiVariable ());
251
+ controllerVars .get (s ).addTypes (strings );
263
252
});
264
253
265
- // @TODO: provide merge for multiple matches
266
- collector .collectPsiVariables (collectorParameter , controllerVars );
254
+ // merging elements
255
+ Map <String , PsiVariable > controllerVars1 = new HashMap <>();
256
+ collector .collectPsiVariables (collectorParameter , controllerVars1 );
257
+
258
+ controllerVars1 .forEach ((s , psiVariable ) -> {
259
+ controllerVars .putIfAbsent (s , new PsiVariable ());
260
+ controllerVars .get (s ).addTypes (psiVariable .getTypes ());
261
+
262
+ PsiElement context = psiVariable .getElement ();
263
+ if (context != null ) {
264
+ controllerVars .get (s ).addElements (context );
265
+ }
266
+ });
267
267
}
268
268
269
269
// globals first
270
- globalVars .putAll (convertHashMapToTypeSet (findInlineStatementVariableDocBlock (psiElement , TwigElementTypes .BLOCK_STATEMENT , true )));
271
- globalVars .putAll (convertHashMapToTypeSet (findInlineStatementVariableDocBlock (psiElement , TwigElementTypes .MACRO_STATEMENT , false )));
272
- globalVars .putAll (convertHashMapToTypeSet (findInlineStatementVariableDocBlock (psiElement , TwigElementTypes .FOR_STATEMENT , false )));
273
-
274
- for (Map .Entry <String , Set <String >> entry : globalVars .entrySet ()) {
275
- Set <String > types = entry .getValue ();
270
+ Collection <Map <String , String >> vars = Arrays .asList (
271
+ findInlineStatementVariableDocBlock (psiElement , TwigElementTypes .BLOCK_STATEMENT , true ),
272
+ findInlineStatementVariableDocBlock (psiElement , TwigElementTypes .MACRO_STATEMENT , false ),
273
+ findInlineStatementVariableDocBlock (psiElement , TwigElementTypes .FOR_STATEMENT , false )
274
+ );
276
275
277
- // collect iterator
278
- types .addAll (collectIteratorReturns (psiElement , entry .getValue ()));
276
+ for (Map <String , String > entry : vars ) {
277
+ entry .forEach ((s , s2 ) -> {
278
+ controllerVars .putIfAbsent (s , new PsiVariable ());
279
+ controllerVars .get (s ).addType (s2 );
280
+ });
281
+ }
279
282
280
- // convert to variable model
281
- controllerVars .put (entry .getKey (), new PsiVariable (types , null ));
283
+ // collect iterator
284
+ for (Map .Entry <String , PsiVariable > entry : controllerVars .entrySet ()) {
285
+ PsiVariable psiVariable = entry .getValue ();
286
+ psiVariable .addTypes (collectIteratorReturns (psiElement , psiVariable .getTypes ()));
282
287
}
283
288
284
289
// check if we are in "for" scope and resolve types ending with []
@@ -347,7 +352,7 @@ private static Collection<String> collectForArrayScopeVariablesFoo(@NotNull Proj
347
352
return previousElements ;
348
353
}
349
354
350
- private static void collectForArrayScopeVariables (PsiElement psiElement , Map <String , PsiVariable > globalVars ) {
355
+ private static void collectForArrayScopeVariables (@ NotNull PsiElement psiElement , @ NotNull Map <String , PsiVariable > globalVars ) {
351
356
PsiElement twigCompositeElement = PsiTreeUtil .findFirstParent (psiElement , psiElement1 -> {
352
357
if (psiElement1 instanceof TwigCompositeElement ) {
353
358
if (PlatformPatterns .psiElement (TwigElementTypes .FOR_STATEMENT ).accepts (psiElement1 )) {
@@ -412,12 +417,12 @@ private static void collectForArrayScopeVariables(PsiElement psiElement, Map<Str
412
417
}
413
418
414
419
// we already have same variable in scope, so merge types
415
- if (globalVars .containsKey (scopeVariable )) {
416
- globalVars .get (scopeVariable ).getTypes ().addAll (types );
420
+ PsiVariable psiVariable = globalVars .get (scopeVariable );
421
+ if (psiVariable != null ) {
422
+ psiVariable .addTypes (types );
417
423
} else {
418
424
globalVars .put (scopeVariable , new PsiVariable (types ));
419
425
}
420
-
421
426
}
422
427
423
428
@ NotNull
0 commit comments