Skip to content

Commit

Permalink
fix Twig filter pattern to support wrapped TwigVariableReference sinc…
Browse files Browse the repository at this point in the history
…e PhpStorm 2024.1
  • Loading branch information
Haehnchen committed Apr 20, 2024
1 parent 512a8b1 commit c8a38f7
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1167,8 +1167,19 @@ public static ElementPattern<PsiElement> getForTagVariablePattern() {

/**
* {{ 'test'|<caret> }}
* {% for user in users|de<caret> %}
*/
public static ElementPattern<PsiElement> getFilterPattern() {
return PlatformPatterns.or(
getFilterAsIdentifierPattern(),
getFilterAsVariableReferencePattern()
);
}

/**
* {{ 'test'|<caret> }}
*/
private static ElementPattern<PsiElement> getFilterAsIdentifierPattern() {
return PlatformPatterns.psiElement()
.afterLeafSkipping(
PlatformPatterns.or(
Expand All @@ -1180,6 +1191,23 @@ public static ElementPattern<PsiElement> getFilterPattern() {
.withLanguage(TwigLanguage.INSTANCE);
}

/**
* Since PhpStorm 2024.1 value can be nested inside TwigVariableReference element
*
* {% for user in users|de<caret> %}
*/
private static ElementPattern<PsiElement> getFilterAsVariableReferencePattern() {
return PlatformPatterns.psiElement()
.withParent(PlatformPatterns.psiElement(TwigVariableReference.class).afterLeafSkipping(
PlatformPatterns.or(
PlatformPatterns.psiElement(PsiWhiteSpace.class),
PlatformPatterns.psiElement(TwigTokenTypes.WHITE_SPACE)
),
PlatformPatterns.psiElement().withElementType(TwigTokenTypes.FILTER)
))
.withLanguage(TwigLanguage.INSTANCE);
}

/**
* {% apply <caret> %}foobar{% endapply %}
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFile;
import com.intellij.psi.PsiReference;
import com.intellij.psi.PsiWhiteSpace;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.util.PlatformIcons;
import com.intellij.util.ProcessingContext;
Expand Down Expand Up @@ -562,24 +561,15 @@ private boolean isCompletionStartingRegexMatch(@RegExp String fullText, @NotNull

private static class FilterCompletionProvider extends CompletionProvider<CompletionParameters> {
public void addCompletions(@NotNull CompletionParameters parameters, @NotNull ProcessingContext context, @NotNull CompletionResultSet resultSet) {
if(!Symfony2ProjectComponent.isEnabled(parameters.getPosition())) {
if (!Symfony2ProjectComponent.isEnabled(parameters.getPosition())) {
return;
}

// move this stuff to pattern fixed event stopping by phpstorm
PsiElement currElement = parameters.getPosition().getOriginalElement();
PsiElement prevElement = currElement.getPrevSibling();
if (((prevElement instanceof PsiWhiteSpace))) prevElement = prevElement.getPrevSibling();

if ((prevElement != null) && (prevElement.getNode().getElementType() == TwigTokenTypes.FILTER)) {
Project project = parameters.getPosition().getProject();

for(Map.Entry<String, TwigExtension> entry : TwigExtensionParser.getFilters(project).entrySet()) {
TwigExtension twigExtension = entry.getValue();
resultSet.addElement(new TwigExtensionLookupElement(currElement.getProject(), entry.getKey(), twigExtension));

resultSet.addAllElements(getTypesFilters(project, entry.getKey(), entry.getValue()));
}
Project project = parameters.getPosition().getProject();
for (Map.Entry<String, TwigExtension> entry : TwigExtensionParser.getFilters(project).entrySet()) {
TwigExtension twigExtension = entry.getValue();
resultSet.addElement(new TwigExtensionLookupElement(project, entry.getKey(), twigExtension));
resultSet.addAllElements(getTypesFilters(project, entry.getKey(), entry.getValue()));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -465,4 +465,12 @@ public void testGetForTagInVariablePattern() {
PsiElement elementAt4 = findElementAt(TwigFileType.INSTANCE, "{% for key, user in <caret>|test('test') %}");
assertTrue(TwigPattern.getForTagInVariablePattern().accepts(elementAt4));
}

public void testGetFilterPattern() {
PsiElement elementAt = findElementAt(TwigFileType.INSTANCE, "{{ 'f<caret>oo'|tr<caret>ans }}");
assertTrue(TwigPattern.getFilterPattern().accepts(elementAt));

PsiElement elementAt2 = findElementAt(TwigFileType.INSTANCE, "{% for user in foo|tr<caret>a %}").getParent();
assertTrue(TwigPattern.getFilterPattern().accepts(elementAt2));
}
}

0 comments on commit c8a38f7

Please sign in to comment.