99import com .intellij .psi .PsiErrorElement ;
1010import com .intellij .psi .PsiWhiteSpace ;
1111import com .intellij .psi .tree .IElementType ;
12+ import com .intellij .psi .tree .TokenSet ;
1213import com .intellij .util .ProcessingContext ;
1314import com .jetbrains .twig .TwigLanguage ;
1415import com .jetbrains .twig .TwigTokenTypes ;
15- import com .jetbrains .twig .elements .TwigBlockTag ;
16- import com .jetbrains .twig .elements .TwigCompositeElement ;
17- import com .jetbrains .twig .elements .TwigElementTypes ;
16+ import com .jetbrains .twig .elements .*;
1817import fr .adrienbrault .idea .symfony2plugin .templating .util .TwigTypeResolveUtil ;
1918import fr .adrienbrault .idea .symfony2plugin .templating .util .TwigUtil ;
2019import org .jetbrains .annotations .NotNull ;
@@ -414,16 +413,18 @@ public static ElementPattern<PsiElement> getAfterIsTokenWithOneIdentifierLeafPat
414413 //noinspection unchecked
415414 return PlatformPatterns
416415 .psiElement ()
417- .afterLeafSkipping (
418- PlatformPatterns .or (
419- PlatformPatterns .psiElement (PsiWhiteSpace .class ),
420- PlatformPatterns .psiElement (TwigTokenTypes .WHITE_SPACE )
421- ),
422- PlatformPatterns .psiElement (TwigTokenTypes .IDENTIFIER ).afterLeafSkipping (PlatformPatterns .psiElement (PsiWhiteSpace .class ), PlatformPatterns .or (
423- PlatformPatterns .psiElement (TwigTokenTypes .IS ),
424- PlatformPatterns .psiElement (TwigTokenTypes .NOT )
416+ .withParent (PlatformPatterns
417+ .psiElement ()
418+ .afterLeafSkipping (
419+ PlatformPatterns .or (
420+ PlatformPatterns .psiElement (PsiWhiteSpace .class ),
421+ PlatformPatterns .psiElement (TwigTokenTypes .WHITE_SPACE )
422+ ),
423+ PlatformPatterns .psiElement (TwigTokenTypes .IDENTIFIER ).afterLeafSkipping (PlatformPatterns .psiElement (PsiWhiteSpace .class ), PlatformPatterns .or (
424+ PlatformPatterns .psiElement (TwigTokenTypes .IS ),
425+ PlatformPatterns .psiElement (TwigTokenTypes .NOT )
426+ ))
425427 ))
426- )
427428 .withLanguage (TwigLanguage .INSTANCE );
428429 }
429430
@@ -445,41 +446,10 @@ public static ElementPattern<PsiElement> getAfterIsTokenTextPattern() {
445446 * {% if 'foo.bar' <carpet> %}
446447 */
447448 public static ElementPattern <PsiElement > getAfterOperatorPattern () {
448- // @TODO: make it some nicer. can wrap it with whitespace
449-
450- //noinspection unchecked
451- ElementPattern <PsiElement > or = PlatformPatterns .or (
452- PlatformPatterns .psiElement (PsiWhiteSpace .class ),
453- PlatformPatterns .psiElement (TwigTokenTypes .WHITE_SPACE ),
454- PlatformPatterns .psiElement (TwigTokenTypes .IDENTIFIER ),
455- PlatformPatterns .psiElement (TwigTokenTypes .SINGLE_QUOTE ),
456- PlatformPatterns .psiElement (TwigTokenTypes .STRING_TEXT ),
457- PlatformPatterns .psiElement (TwigTokenTypes .DOT ),
458- PlatformPatterns .psiElement (TwigTokenTypes .DOUBLE_QUOTE ),
459- PlatformPatterns .psiElement (TwigTokenTypes .LBRACE ),
460- PlatformPatterns .psiElement (TwigTokenTypes .RBRACE ),
461- PlatformPatterns .psiElement (TwigTokenTypes .LBRACE_SQ ),
462- PlatformPatterns .psiElement (TwigTokenTypes .RBRACE_SQ ),
463- PlatformPatterns .psiElement (TwigTokenTypes .NUMBER ),
464- PlatformPatterns .psiElement (TwigTokenTypes .FILTER )
465- );
466-
467- //noinspection unchecked
468- ElementPattern <PsiElement > anIf = PlatformPatterns .or (
469- PlatformPatterns .psiElement (TwigTokenTypes .TAG_NAME ).withText ("if" ),
470- PlatformPatterns .psiElement (TwigTokenTypes .AND ),
471- PlatformPatterns .psiElement (TwigTokenTypes .OR )
472- );
473-
474449 return PlatformPatterns
475450 .psiElement (TwigTokenTypes .IDENTIFIER )
476- .afterLeaf (PlatformPatterns .not (
477- PlatformPatterns .psiElement (TwigTokenTypes .DOT )
478- ))
479- .withParent (
480- PlatformPatterns .psiElement (TwigElementTypes .IF_TAG )
481- )
482- .afterLeafSkipping (or , anIf )
451+ .inside (PlatformPatterns .psiElement (TwigElementTypes .IF_TAG ))
452+ .andNot (PlatformPatterns .psiElement ().inside (PlatformPatterns .psiElement (TwigElementTypes .FIELD_REFERENCE )))
483453 .withLanguage (TwigLanguage .INSTANCE );
484454 }
485455
@@ -555,7 +525,12 @@ public static ElementPattern<PsiElement> getEmbedPattern() {
555525 }
556526
557527 static ElementPattern <PsiElement > getPrintBlockFunctionPattern () {
558- return PlatformPatterns .psiElement (TwigTokenTypes .IDENTIFIER ).withParent (getFunctionCallScopePattern ()).withLanguage (TwigLanguage .INSTANCE );
528+ return PlatformPatterns .psiElement (TwigTokenTypes .IDENTIFIER )
529+ .inside (PlatformPatterns .or (
530+ PlatformPatterns .psiElement (TwigPsiReference .class ),
531+ PlatformPatterns .psiElement (TwigElementTypes .FUNCTION_CALL )))
532+ .inside (getFunctionCallScopePattern ())
533+ .withLanguage (TwigLanguage .INSTANCE );
559534 }
560535
561536 public static ElementPattern <PsiElement > getFunctionPattern (@ NotNull String ...functionName ) {
@@ -601,25 +576,16 @@ private static ElementPattern<PsiElement> getFunctionCallScopePattern() {
601576 */
602577 public static ElementPattern <PsiElement > getCompletablePattern () {
603578 //noinspection unchecked
604- return PlatformPatterns .psiElement ()
579+ return PlatformPatterns .psiElement (TwigTokenTypes .IDENTIFIER )
580+ .inside (PlatformPatterns .psiElement (TwigElementTypes .VARIABLE_REFERENCE ))
605581 .andNot (
582+ PlatformPatterns .psiElement ().inside (PlatformPatterns .psiElement (TwigElementTypes .FIELD_REFERENCE ))
583+ ).inside (
606584 PlatformPatterns .or (
607- PlatformPatterns .psiElement ().afterLeaf (PlatformPatterns .psiElement (TwigTokenTypes .DOT )),
608- PlatformPatterns .psiElement ().afterLeaf (PlatformPatterns .psiElement (TwigTokenTypes .SINGLE_QUOTE )),
609- PlatformPatterns .psiElement ().afterLeaf (PlatformPatterns .psiElement (TwigTokenTypes .DOUBLE_QUOTE ))
585+ PlatformPatterns .psiElement (TwigElementTypes .PRINT_BLOCK ),
586+ PlatformPatterns .psiElement (TwigElementTypes .SET_TAG )
610587 )
611588 )
612- .afterLeafSkipping (
613- PlatformPatterns .or (
614- PlatformPatterns .psiElement (TwigTokenTypes .LBRACE ),
615- PlatformPatterns .psiElement (PsiWhiteSpace .class )
616- ),
617- PlatformPatterns .psiElement ()
618- )
619- .withParent (PlatformPatterns .or (
620- PlatformPatterns .psiElement (TwigElementTypes .PRINT_BLOCK ),
621- PlatformPatterns .psiElement (TwigElementTypes .SET_TAG )
622- ))
623589 .withLanguage (TwigLanguage .INSTANCE );
624590 }
625591
@@ -873,9 +839,7 @@ public static ElementPattern<PsiElement> getParentFunctionPattern() {
873839 public static ElementPattern <PsiElement > getTypeCompletionPattern () {
874840 return PlatformPatterns
875841 .psiElement (TwigTokenTypes .IDENTIFIER )
876- .afterLeaf (
877- PlatformPatterns .psiElement (TwigTokenTypes .DOT )
878- )
842+ .withParent (PlatformPatterns .psiElement (TwigElementTypes .FIELD_REFERENCE ))
879843 .withLanguage (TwigLanguage .INSTANCE );
880844 }
881845
@@ -1080,9 +1044,7 @@ public static ElementPattern<PsiElement> getForTagVariablePattern() {
10801044 // {% for "user" %}
10811045
10821046 //noinspection unchecked
1083- return PlatformPatterns
1084- .psiElement (TwigTokenTypes .IDENTIFIER )
1085- .beforeLeafSkipping (
1047+ return captureVariableOrField ().beforeLeafSkipping (
10861048 PlatformPatterns .or (
10871049 PlatformPatterns .psiElement (PsiWhiteSpace .class ),
10881050 PlatformPatterns .psiElement (TwigTokenTypes .WHITE_SPACE )
@@ -1129,16 +1091,19 @@ public boolean accepts(@NotNull PsiElement psiElement, ProcessingContext process
11291091 .withLanguage (TwigLanguage .INSTANCE );
11301092 }
11311093
1094+ public static PsiElementPattern .Capture <PsiElement > captureVariableOrField () {
1095+ return PlatformPatterns .psiElement ().withElementType (TokenSet .create (TwigElementTypes .VARIABLE_REFERENCE ,
1096+ TwigElementTypes .FIELD_REFERENCE ));
1097+ }
1098+
11321099 public static ElementPattern <PsiElement > getForTagInVariablePattern () {
11331100
11341101 // {% for key, user in "users" %}
11351102 // {% for user in "users" %}
11361103 // {% for user in "users"|slice(0, 10) %}
11371104
11381105 //noinspection unchecked
1139- return PlatformPatterns
1140- .psiElement (TwigTokenTypes .IDENTIFIER )
1141- .afterLeafSkipping (
1106+ return captureVariableOrField ().afterLeafSkipping (
11421107 PlatformPatterns .or (
11431108 PlatformPatterns .psiElement (PsiWhiteSpace .class ),
11441109 PlatformPatterns .psiElement (TwigTokenTypes .WHITE_SPACE )
@@ -1153,9 +1118,7 @@ public static ElementPattern<PsiElement> getIfVariablePattern() {
11531118 // {% if "var" %}
11541119
11551120 //noinspection unchecked
1156- return PlatformPatterns
1157- .psiElement (TwigTokenTypes .IDENTIFIER )
1158- .afterLeafSkipping (
1121+ return captureVariableOrField ().afterLeafSkipping (
11591122 PlatformPatterns .or (
11601123 PlatformPatterns .psiElement (PsiWhiteSpace .class ),
11611124 PlatformPatterns .psiElement (TwigTokenTypes .WHITE_SPACE )
@@ -1177,9 +1140,7 @@ public static ElementPattern<PsiElement> getIfConditionVariablePattern() {
11771140 // and so on
11781141
11791142 //noinspection unchecked
1180- return PlatformPatterns
1181- .psiElement (TwigTokenTypes .IDENTIFIER )
1182- .afterLeafSkipping (
1143+ return captureVariableOrField ().afterLeafSkipping (
11831144 PlatformPatterns .or (
11841145 PlatformPatterns .psiElement (PsiWhiteSpace .class ),
11851146 PlatformPatterns .psiElement (TwigTokenTypes .WHITE_SPACE )
@@ -1246,9 +1207,7 @@ public static ElementPattern<PsiElement> getSetVariablePattern() {
12461207 // {% set count1 = "var" %}
12471208
12481209 //noinspection unchecked
1249- return PlatformPatterns
1250- .psiElement (TwigTokenTypes .IDENTIFIER )
1251- .afterLeafSkipping (
1210+ return captureVariableOrField ().afterLeafSkipping (
12521211 PlatformPatterns .or (
12531212 PlatformPatterns .psiElement (PsiWhiteSpace .class ),
12541213 PlatformPatterns .psiElement (TwigTokenTypes .WHITE_SPACE )
0 commit comments