Skip to content

Commit

Permalink
fix Twig "for in" completion of variables
Browse files Browse the repository at this point in the history
  • Loading branch information
Haehnchen committed Jun 24, 2023
1 parent 77447ce commit cc709c2
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1180,18 +1180,16 @@ public boolean accepts(@NotNull PsiElement psiElement, ProcessingContext process
.withLanguage(TwigLanguage.INSTANCE);
}

/**
* @TODO: check related features introduce by PhpStorm for Twig changes in 2020
*/
public static PsiElementPattern.Capture<PsiElement> captureVariableOrField() {
return PlatformPatterns.psiElement().withElementType(TokenSet.create(TwigElementTypes.VARIABLE_REFERENCE,
TwigElementTypes.FIELD_REFERENCE));
}

public static ElementPattern<PsiElement> getForTagInVariablePattern() {

// {% for key, user in "users" %}
// {% for user in "users" %}
// {% for user in "users"|slice(0, 10) %}

//noinspection unchecked
public static ElementPattern<PsiElement> getForTagInVariableReferencePattern() {
// {% for user in test.test %}
return captureVariableOrField().afterLeafSkipping(
PlatformPatterns.or(
PlatformPatterns.psiElement(PsiWhiteSpace.class),
Expand All @@ -1202,6 +1200,18 @@ public static ElementPattern<PsiElement> getForTagInVariablePattern() {
.withLanguage(TwigLanguage.INSTANCE);
}

public static ElementPattern<PsiElement> getForTagInVariablePattern() {
// {% for key, user in "users" %}
// {% for user in "users" %}
// {% for user in "users"|slice(0, 10) %}
return PlatformPatterns.psiElement().afterLeafSkipping(
PlatformPatterns.or(
PlatformPatterns.psiElement(PsiWhiteSpace.class),
PlatformPatterns.psiElement(TwigTokenTypes.WHITE_SPACE)
),
PlatformPatterns.psiElement(TwigTokenTypes.IN).withLanguage(TwigLanguage.INSTANCE));
}

public static ElementPattern<PsiElement> getIfVariablePattern() {

// {% if "var" %}
Expand Down Expand Up @@ -1367,6 +1377,7 @@ public static ElementPattern<PsiElement> getVariableTypePattern() {
//noinspection unchecked
return PlatformPatterns.or(
getForTagInVariablePattern(),
getForTagInVariableReferencePattern(),
getIfVariablePattern(),
getIfConditionVariablePattern(),
getSetVariablePattern()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,7 @@ private static void collectForArrayScopeVariables(@NotNull PsiElement psiElement

// {% for user in "users" %}
PsiElement forTag = twigCompositeElement.getFirstChild();
PsiElement inVariable = PsiElementUtils.getChildrenOfType(forTag, TwigPattern.getForTagInVariablePattern());
PsiElement inVariable = PsiElementUtils.getChildrenOfType(forTag, TwigPattern.getForTagInVariableReferencePattern());
inVariable = inVariable instanceof TwigVariableReference ? inVariable : PsiTreeUtil.getChildOfType(inVariable, TwigVariableReference.class);
if(inVariable == null) {
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2715,7 +2715,7 @@ private static void visitTemplateVariables(@NotNull PsiElement scope, @NotNull C
if(firstChild.getNode().getElementType() == TwigElementTypes.FOR_TAG) {
PsiElement afterIn = PsiElementUtils.getNextSiblingOfType(
firstChild.getFirstChild(),
TwigPattern.getForTagInVariablePattern()
TwigPattern.getForTagInVariableReferencePattern()
);

visitTemplateVariablesConsumer(afterIn, consumer);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package fr.adrienbrault.idea.symfony2plugin.tests.templating;

import com.intellij.psi.PsiElement;
import com.jetbrains.twig.TwigFileType;
import fr.adrienbrault.idea.symfony2plugin.templating.TwigPattern;
import fr.adrienbrault.idea.symfony2plugin.tests.SymfonyLightCodeInsightFixtureTestCase;
Expand Down Expand Up @@ -442,4 +443,18 @@ public void testGetTranslationPattern() {
assertTrue(TwigPattern.getTranslationKeyPattern("trans").accepts(findElementAt(TwigFileType.INSTANCE, "{{ 'f<caret>oo'|trans }}")));
assertTrue(TwigPattern.getTranslationKeyPattern("trans").accepts(findElementAt(TwigFileType.INSTANCE, "{{ 'f<caret>oo'|trans('foobar') }}")));
}

public void testGetForTagInVariablePattern() {
PsiElement elementAt = findElementAt(TwigFileType.INSTANCE, "{% for user in <caret> %}");
assertTrue(TwigPattern.getForTagInVariablePattern().accepts(elementAt));

PsiElement elementAt2 = findElementAt(TwigFileType.INSTANCE, "{% for user in te<caret>st %}");
assertTrue(TwigPattern.getForTagInVariablePattern().accepts(elementAt2));

PsiElement elementAt3 = findElementAt(TwigFileType.INSTANCE, "{% for key, user in <caret> %}");
assertTrue(TwigPattern.getForTagInVariablePattern().accepts(elementAt3));

PsiElement elementAt4 = findElementAt(TwigFileType.INSTANCE, "{% for key, user in <caret>|test('test') %}");
assertTrue(TwigPattern.getForTagInVariablePattern().accepts(elementAt4));
}
}

0 comments on commit cc709c2

Please sign in to comment.