Skip to content

Commit b70e250

Browse files
authored
Merge pull request #1528 from Haehnchen/feature/form-pattern
smarter Twig function pattern
2 parents cb78be8 + e137467 commit b70e250

File tree

2 files changed

+24
-3
lines changed

2 files changed

+24
-3
lines changed

src/main/java/fr/adrienbrault/idea/symfony2plugin/templating/TwigPattern.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -558,11 +558,15 @@ static ElementPattern<PsiElement> getPrintBlockFunctionPattern() {
558558
return PlatformPatterns.psiElement(TwigTokenTypes.IDENTIFIER).withParent(getFunctionCallScopePattern()).withLanguage(TwigLanguage.INSTANCE);
559559
}
560560

561-
static ElementPattern<PsiElement> getFunctionPattern(@NotNull String ...functionName) {
561+
public static ElementPattern<PsiElement> getFunctionPattern(@NotNull String ...functionName) {
562562
return PlatformPatterns.psiElement(TwigElementTypes.FUNCTION_CALL).withText(PlatformPatterns.string().with(new PatternCondition<String>("Twig: Function call") {
563563
@Override
564-
public boolean accepts(@NotNull String s, ProcessingContext processingContext) {
565-
return Arrays.asList(functionName).contains(s);
564+
public boolean accepts(@NotNull String function, ProcessingContext processingContext) {
565+
String funcWithoutSpace = function.replaceAll(" +", "");
566+
567+
return Arrays.stream(functionName).anyMatch(wantFunction ->
568+
funcWithoutSpace.startsWith(wantFunction + "(") || funcWithoutSpace.equals(wantFunction)
569+
);
566570
}
567571
}));
568572
}

src/test/java/fr/adrienbrault/idea/symfony2plugin/tests/templating/TwigPatternTest.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,23 @@ public void testGetPrintBlockOrTagFunctionPattern() {
6868
));
6969
}
7070

71+
/**
72+
* @see fr.adrienbrault.idea.symfony2plugin.templating.TwigPattern#getFunctionPattern
73+
*/
74+
public void testgetFunctionPattern() {
75+
assertTrue(fr.adrienbrault.idea.symfony2plugin.templating.TwigPattern.getFunctionPattern("form").accepts(
76+
findElementAt(TwigFileType.INSTANCE, "{{ for<caret>m(test) }}").getParent()
77+
));
78+
79+
assertTrue(fr.adrienbrault.idea.symfony2plugin.templating.TwigPattern.getFunctionPattern("form").accepts(
80+
findElementAt(TwigFileType.INSTANCE, "{{ for<caret>m (test) }}").getParent()
81+
));
82+
83+
assertFalse(fr.adrienbrault.idea.symfony2plugin.templating.TwigPattern.getFunctionPattern("f").accepts(
84+
findElementAt(TwigFileType.INSTANCE, "{{ for<caret>m(test) }}").getParent()
85+
));
86+
}
87+
7188
/**
7289
* @see fr.adrienbrault.idea.symfony2plugin.templating.TwigPattern#getFunctionWithFirstParameterAsArrayPattern
7390
*/

0 commit comments

Comments
 (0)