Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix Twig "for in" completion of variables #2180

Merged
merged 1 commit into from
Jun 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1180,18 +1180,17 @@ 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 %}
// {% for user in test.test %}
return captureVariableOrField().afterLeafSkipping(
PlatformPatterns.or(
PlatformPatterns.psiElement(PsiWhiteSpace.class),
Expand All @@ -1202,6 +1201,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 +1378,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));
}
}