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

provide Twig type for "loop" variable inside "foreach" #1533

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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 @@ -25,6 +25,7 @@
import fr.adrienbrault.idea.symfony2plugin.templating.variable.TwigTypeContainer;
import fr.adrienbrault.idea.symfony2plugin.templating.variable.collector.StaticVariableCollector;
import fr.adrienbrault.idea.symfony2plugin.templating.variable.dict.PsiVariable;
import fr.adrienbrault.idea.symfony2plugin.templating.variable.resolver.ForEachLoopResolver;
import fr.adrienbrault.idea.symfony2plugin.templating.variable.resolver.FormFieldResolver;
import fr.adrienbrault.idea.symfony2plugin.templating.variable.resolver.FormVarsResolver;
import fr.adrienbrault.idea.symfony2plugin.templating.variable.resolver.TwigTypeResolver;
Expand Down Expand Up @@ -83,6 +84,7 @@ public class TwigTypeResolveUtil {
private static TwigTypeResolver[] TWIG_TYPE_RESOLVERS = new TwigTypeResolver[] {
new FormVarsResolver(),
new FormFieldResolver(),
new ForEachLoopResolver(),
};

@NotNull
Expand Down Expand Up @@ -366,6 +368,8 @@ private static void collectForArrayScopeVariables(@NotNull PsiElement psiElement
return;
}

globalVars.put("loop", new PsiVariable("\\loop"));

// {% for user in "users" %}
PsiElement forTag = twigCompositeElement.getFirstChild();
PsiElement inVariable = PsiElementUtils.getChildrenOfType(forTag, TwigPattern.getForTagInVariablePattern());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ public static Collection<TwigTypeContainer> fromCollection(Project project, Coll
if(phpClass.size() > 0) {
twigTypeContainerList.add(new TwigTypeContainer(phpClass.iterator().next()));
}

// inside {% for ... %}{% endfor %} we have a "loop" var; fake an internal type here
if (phpNamedElement.getTypes().contains("\\loop")) {
twigTypeContainerList.add(new TwigTypeContainer("\\loop"));
}
}

return twigTypeContainerList;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package fr.adrienbrault.idea.symfony2plugin.templating.variable.resolver;

import fr.adrienbrault.idea.symfony2plugin.templating.variable.TwigTypeContainer;
import fr.adrienbrault.idea.symfony2plugin.templating.variable.dict.PsiVariable;
import org.jetbrains.annotations.Nullable;

import java.util.Collection;
import java.util.List;

/**
* Provide the loop.XXX types inside foreach block if previous element is named loop with the type "\loop" {% for ... %}{% endfor %}
*
* @author Daniel Espendiller <[email protected]>
*/
public class ForEachLoopResolver implements TwigTypeResolver {

/**
* @link https://twig.symfony.com/doc/3.x/tags/for.html#the-loop-variable
*/
public static final String[] LOOP_VARIABLES = {
"index", "index0", "revindex", "revindex0", "first", "last", "length", "parent"
};

@Override
public void resolve(Collection<TwigTypeContainer> targets, @Nullable Collection<TwigTypeContainer> previousElement, String typeName, Collection<List<TwigTypeContainer>> previousElements, @Nullable Collection<PsiVariable> psiVariables) {
if (previousElement == null || previousElement.stream().noneMatch(p -> "\\loop".equals(p.getStringElement()))) {
return;
}

for(String string: LOOP_VARIABLES) {
targets.add(new TwigTypeContainer(string));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,15 @@
import com.jetbrains.twig.TwigFileType;
import com.jetbrains.twig.TwigLanguage;
import fr.adrienbrault.idea.symfony2plugin.templating.util.TwigTypeResolveUtil;
import fr.adrienbrault.idea.symfony2plugin.templating.variable.TwigTypeContainer;
import fr.adrienbrault.idea.symfony2plugin.templating.variable.dict.PsiVariable;
import fr.adrienbrault.idea.symfony2plugin.tests.SymfonyLightCodeInsightFixtureTestCase;
import org.jetbrains.annotations.NotNull;

import java.util.Collection;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
* @author Daniel Espendiller <[email protected]>
Expand Down Expand Up @@ -84,6 +88,41 @@ public void testCollectScopeVariables() {
assertContainsElements(stringPsiVariableMap.get("b").getTypes(), "\\Foo\\Bar");
}

/**
* @see TwigTypeResolveUtil#collectScopeVariables
*/
public void testForeachMustProvideLoopVariable() {
myFixture.configureByText(TwigFileType.INSTANCE,
"{% for foo in foo %}" +
"{{ <caret> }}" +
"{% endfor %}"
);

PsiElement psiElement = myFixture.getFile().findElementAt(myFixture.getCaretOffset());

Map<String, PsiVariable> stringPsiVariableMap = TwigTypeResolveUtil.collectScopeVariables(psiElement);
assertContainsElements(stringPsiVariableMap.keySet(), "loop");
}

/**
* @see TwigTypeResolveUtil#resolveTwigMethodName
*/
public void testForeachMustProvideLoopTypeVariable() {
myFixture.configureByText(TwigFileType.INSTANCE,
"{% for foo in foo %}" +
"{{ loop.i<caret> }}" +
"{% endfor %}"
);

PsiElement psiElement = myFixture.getFile().findElementAt(myFixture.getCaretOffset());
Collection<String> beforeLeaf = TwigTypeResolveUtil.formatPsiTypeName(psiElement);

Collection<TwigTypeContainer> stringPsiVariableMap = TwigTypeResolveUtil.resolveTwigMethodName(psiElement.getPrevSibling(), beforeLeaf);

Set<String> types = stringPsiVariableMap.stream().map(TwigTypeContainer::getStringElement).collect(Collectors.toSet());
assertContainsElements(types, "last", "index", "index0");
}

private void assertMatches(@NotNull String content, @NotNull String... regularExpressions) {
for (String regularExpression : regularExpressions) {
if(content.matches(regularExpression)) {
Expand Down