From c8a38f73282a9d0fe656a69bfc12aa43a52fa79e Mon Sep 17 00:00:00 2001 From: Daniel Espendiller Date: Sat, 20 Apr 2024 19:35:17 +0200 Subject: [PATCH] fix Twig filter pattern to support wrapped TwigVariableReference since PhpStorm 2024.1 --- .../templating/TwigPattern.java | 28 +++++++++++++++++++ .../TwigTemplateCompletionContributor.java | 22 ++++----------- .../tests/templating/TwigPatternTest.java | 8 ++++++ 3 files changed, 42 insertions(+), 16 deletions(-) diff --git a/src/main/java/fr/adrienbrault/idea/symfony2plugin/templating/TwigPattern.java b/src/main/java/fr/adrienbrault/idea/symfony2plugin/templating/TwigPattern.java index f04b3a7ae..004654be6 100644 --- a/src/main/java/fr/adrienbrault/idea/symfony2plugin/templating/TwigPattern.java +++ b/src/main/java/fr/adrienbrault/idea/symfony2plugin/templating/TwigPattern.java @@ -1167,8 +1167,19 @@ public static ElementPattern getForTagVariablePattern() { /** * {{ 'test'| }} + * {% for user in users|de %} */ public static ElementPattern getFilterPattern() { + return PlatformPatterns.or( + getFilterAsIdentifierPattern(), + getFilterAsVariableReferencePattern() + ); + } + + /** + * {{ 'test'| }} + */ + private static ElementPattern getFilterAsIdentifierPattern() { return PlatformPatterns.psiElement() .afterLeafSkipping( PlatformPatterns.or( @@ -1180,6 +1191,23 @@ public static ElementPattern getFilterPattern() { .withLanguage(TwigLanguage.INSTANCE); } + /** + * Since PhpStorm 2024.1 value can be nested inside TwigVariableReference element + * + * {% for user in users|de %} + */ + private static ElementPattern 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 %}foobar{% endapply %} */ diff --git a/src/main/java/fr/adrienbrault/idea/symfony2plugin/templating/TwigTemplateCompletionContributor.java b/src/main/java/fr/adrienbrault/idea/symfony2plugin/templating/TwigTemplateCompletionContributor.java index 6329a21b2..c213c954d 100644 --- a/src/main/java/fr/adrienbrault/idea/symfony2plugin/templating/TwigTemplateCompletionContributor.java +++ b/src/main/java/fr/adrienbrault/idea/symfony2plugin/templating/TwigTemplateCompletionContributor.java @@ -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; @@ -562,24 +561,15 @@ private boolean isCompletionStartingRegexMatch(@RegExp String fullText, @NotNull private static class FilterCompletionProvider extends CompletionProvider { 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 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 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())); } } } diff --git a/src/test/java/fr/adrienbrault/idea/symfony2plugin/tests/templating/TwigPatternTest.java b/src/test/java/fr/adrienbrault/idea/symfony2plugin/tests/templating/TwigPatternTest.java index 515666e58..708cdfc19 100644 --- a/src/test/java/fr/adrienbrault/idea/symfony2plugin/tests/templating/TwigPatternTest.java +++ b/src/test/java/fr/adrienbrault/idea/symfony2plugin/tests/templating/TwigPatternTest.java @@ -465,4 +465,12 @@ public void testGetForTagInVariablePattern() { PsiElement elementAt4 = findElementAt(TwigFileType.INSTANCE, "{% for key, user in |test('test') %}"); assertTrue(TwigPattern.getForTagInVariablePattern().accepts(elementAt4)); } + + public void testGetFilterPattern() { + PsiElement elementAt = findElementAt(TwigFileType.INSTANCE, "{{ 'foo'|trans }}"); + assertTrue(TwigPattern.getFilterPattern().accepts(elementAt)); + + PsiElement elementAt2 = findElementAt(TwigFileType.INSTANCE, "{% for user in foo|tra %}").getParent(); + assertTrue(TwigPattern.getFilterPattern().accepts(elementAt2)); + } }