Skip to content

Twig form linemarker should only be triggered on smallest (leaf) elements #1954

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

Merged
merged 1 commit into from
Jun 3, 2022
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 @@ -105,8 +105,10 @@ public void collectSlowLineMarkers(@NotNull List<? extends PsiElement> psiElemen
if(lineOverwrites != null) {
results.add(lineOverwrites);
}
} else if(TwigPattern.getFunctionPattern("form_start", "form", "form_end", "form_rest").accepts(psiElement)) {
LineMarkerInfo<?> lineOverwrites = attachFormType(psiElement);
} else if(TwigPattern.getLeafFunctionPattern("form_start", "form", "form_end", "form_rest").accepts(psiElement)) {
PsiElement parent = psiElement.getParent();

LineMarkerInfo<?> lineOverwrites = attachFormType(parent);
if(lineOverwrites != null) {
results.add(lineOverwrites);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,11 +175,8 @@ public static ElementPattern<PsiElement> getPrintBlockOrTagFunctionPattern(Strin

/**
* Check for {{ include('|') }}, {% include('|') %}
*
* @param functionName twig function name
*/
public static ElementPattern<PsiElement> getPrintBlockOrTagFunctionPattern() {
//noinspection unchecked
return PlatformPatterns
.psiElement(TwigTokenTypes.STRING_TEXT)
.withParent(
Expand Down Expand Up @@ -579,17 +576,19 @@ static ElementPattern<PsiElement> getPrintBlockFunctionPattern() {
.withLanguage(TwigLanguage.INSTANCE);
}

public static ElementPattern<PsiElement> getFunctionPattern(@NotNull String ...functionName) {
return PlatformPatterns.psiElement(TwigElementTypes.FUNCTION_CALL).withText(PlatformPatterns.string().with(new PatternCondition<String>("Twig: Function call") {
@Override
public boolean accepts(@NotNull String function, ProcessingContext processingContext) {
String funcWithoutSpace = function.replaceAll(" +", "");

return Arrays.stream(functionName).anyMatch(wantFunction ->
funcWithoutSpace.startsWith(wantFunction + "(") || funcWithoutSpace.equals(wantFunction)
);
}
}));
public static ElementPattern<PsiElement> getLeafFunctionPattern(@NotNull String ...functionName) {
return PlatformPatterns.psiElement(TwigTokenTypes.IDENTIFIER)
.withParent(PlatformPatterns.psiElement(TwigElementTypes.FUNCTION_CALL))
.withText(PlatformPatterns.string().with(new PatternCondition<>("Twig: Leaf function call") {
@Override
public boolean accepts(@NotNull String function, ProcessingContext processingContext) {
String funcWithoutSpace = function.replaceAll(" +", "");

return Arrays.stream(functionName).anyMatch(wantFunction ->
funcWithoutSpace.startsWith(wantFunction + "(") || funcWithoutSpace.equals(wantFunction)
);
}
}));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,19 +69,19 @@ public void testGetPrintBlockOrTagFunctionPattern() {
}

/**
* @see fr.adrienbrault.idea.symfony2plugin.templating.TwigPattern#getFunctionPattern
* @see fr.adrienbrault.idea.symfony2plugin.templating.TwigPattern#getLeafFunctionPattern
*/
public void testgetFunctionPattern() {
assertTrue(fr.adrienbrault.idea.symfony2plugin.templating.TwigPattern.getFunctionPattern("form").accepts(
findElementAt(TwigFileType.INSTANCE, "{{ for<caret>m(test) }}").getParent()
public void testGetFunctionPattern() {
assertTrue(fr.adrienbrault.idea.symfony2plugin.templating.TwigPattern.getLeafFunctionPattern("form").accepts(
findElementAt(TwigFileType.INSTANCE, "{{ for<caret>m(test) }}")
));

assertTrue(fr.adrienbrault.idea.symfony2plugin.templating.TwigPattern.getFunctionPattern("form").accepts(
findElementAt(TwigFileType.INSTANCE, "{{ for<caret>m (test) }}").getParent()
assertTrue(fr.adrienbrault.idea.symfony2plugin.templating.TwigPattern.getLeafFunctionPattern("form").accepts(
findElementAt(TwigFileType.INSTANCE, "{{ for<caret>m (test) }}")
));

assertFalse(fr.adrienbrault.idea.symfony2plugin.templating.TwigPattern.getFunctionPattern("f").accepts(
findElementAt(TwigFileType.INSTANCE, "{{ for<caret>m(test) }}").getParent()
assertFalse(fr.adrienbrault.idea.symfony2plugin.templating.TwigPattern.getLeafFunctionPattern("f").accepts(
findElementAt(TwigFileType.INSTANCE, "{{ for<caret>m(test) }}")
));
}

Expand Down