8000 add controller and action in twig render tag #516 · Koc/idea-php-symfony2-plugin@ad9a2b4 · GitHub
[go: up one dir, main page]

Skip to content

Commit ad9a2b4

Browse files
committed
add controller and action in twig render tag Haehnchen#516
1 parent dedb7b6 commit ad9a2b4

File tree

5 files changed

+108
-2
lines changed

5 files changed

+108
-2
lines changed

src/fr/adrienbrault/idea/symfony2plugin/TwigHelper.java

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -472,6 +472,59 @@ public static ElementPattern<PsiElement> getPrintBlockFunctionPattern(String...
472472
.withLanguage(TwigLanguage.INSTANCE);
473473
}
474474

475+
/**
476+
* Check for {{ include('|') }}, {% include('|') %}
477+
*
478+
* @param functionName twig function name
479+
*/
480+
public static ElementPattern<PsiElement> getPrintBlockOrTagFunctionPattern(String... functionName) {
481+
//noinspection unchecked
482+
return PlatformPatterns
483+
.psiElement(TwigTokenTypes.STRING_TEXT)
484+
.withParent(
485+
PlatformPatterns.or(
486+
PlatformPatterns.psiElement(TwigElementTypes.PRINT_BLOCK),
487+
PlatformPatterns.psiElement(TwigElementTypes.TAG)
488+
)
489+
)
490+
.afterLeafSkipping(
491+
PlatformPatterns.or(
492+
PlatformPatterns.psiElement(TwigTokenTypes.LBRACE),
493+
PlatformPatterns.psiElement(PsiWhiteSpace.class),
494+
PlatformPatterns.psiElement(TwigTokenTypes.WHITE_SPACE),
495+
PlatformPatterns.psiElement(TwigTokenTypes.SINGLE_QUOTE),
496+
PlatformPatterns.psiElement(TwigTokenTypes.DOUBLE_QUOTE)
497+
),
498+
PlatformPatterns.psiElement(TwigTokenTypes.IDENTIFIER).withText(PlatformPatterns.string().oneOf(functionName))
499+
)
500+
.withLanguage(TwigLanguage.INSTANCE);
501+
}
502+
503+
/**
504+
* {% render "foo"
505+
*
506+
* @param tagName twig tag name
507+
*/
508+
public static ElementPattern<PsiElement> getStringAfterTagNamePattern(@NotNull String tagName) {
509+
//noinspection unchecked
510+
return PlatformPatterns
511+
.psiElement(TwigTokenTypes.STRING_TEXT)
512+
.afterLeafSkipping(
513+
PlatformPatterns.or(
514+
PlatformPatterns.psiElement(TwigTokenTypes.LBRACE),
515+
PlatformPatterns.psiElement(PsiWhiteSpace.class),
516+
PlatformPatterns.psiElement(TwigTokenTypes.WHITE_SPACE),
517+
PlatformPatterns.psiElement(TwigTokenTypes.SINGLE_QUOTE),
518+
PlatformPatterns.psiElement(TwigTokenTypes.DOUBLE_QUOTE)
519+
),
520+
PlatformPatterns.psiElement(TwigTokenTypes.TAG_NAME).withText(tagName)
521+
)
522+
.withParent(
523+
PlatformPatterns.psiElement(TwigElementTypes.TAG)
524+
)
525+
.withLanguage(TwigLanguage.INSTANCE);
526+
}
527+
475528
/**
476529
* Check for {% if foo is "foo" %}
477530
*/
@@ -639,6 +692,8 @@ public static ElementPattern<PsiElement> getBlockTagPattern() {
639692
}
640693

641694
/**
695+
* use getStringAfterTagNamePattern @TODO
696+
*
642697
* {% trans_default_domain '<carpet>' %}
643698
* {% trans_default_domain <carpet> %}
644699
*/

src/fr/adrienbrault/idea/symfony2plugin/templating/TwigTemplateCompletionContributor.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,8 +255,12 @@ public void addCompletions(@NotNull CompletionParameters parameters,
255255
// {% transchoice from "<carpet>" %}
256256
extend(CompletionType.BASIC, TwigHelper.getTranslationTokenTagFromPattern(), new TranslationDomainCompletionProvider());
257257

258-
extend(CompletionType.BASIC, TwigHelper.getPrintBlockFunctionPattern("controller"), new ControllerCompletionProvider());
258+
// {{ controller('<caret>') }}
259+
// {% render(controller('<caret>')) %}
260+
extend(CompletionType.BASIC, TwigHelper.getPrintBlockOrTagFunctionPattern("controller"), new ControllerCompletionProvider());
259261

262+
// {% render '<caret>' %}"
263+
extend(CompletionType.BASIC, TwigHelper.getStringAfterTagNamePattern("render"), new ControllerCompletionProvider());
260264

261265
// assets completion:
262266
// stylesheets and javascripts tags

src/fr/adrienbrault/idea/symfony2plugin/templating/TwigTemplateGoToDeclarationHandler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ public PsiElement[] getGotoDeclarationTargets(PsiElement psiElement, int i, Edit
8686
return this.getTwigFiles(psiElement);
8787
}
8888

89-
if(TwigHelper.getPrintBlockFunctionPattern("controller").accepts(psiElement)) {
89+
if(TwigHelper.getPrintBlockOrTagFunctionPattern("controller").accepts(psiElement) || TwigHelper.getStringAfterTagNamePattern("render").accepts(psiElement)) {
9090
PsiElement controllerMethod = this.getControllerGoTo(psiElement);
9191
if(controllerMethod != null) {
9292
return new PsiElement[] { controllerMethod };

tests/fr/adrienbrault/idea/symfony2plugin/tests/templating/TwigFilterCompletionContributorTest.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package fr.adrienbrault.idea.symfony2plugin.tests.templating;
22

3+
import com.intellij.patterns.PlatformPatterns;
4+
import com.jetbrains.php.lang.psi.elements.Method;
35
import com.jetbrains.twig.TwigFileType;
46
import com.jetbrains.twig.elements.TwigElementTypes;
57
import fr.adrienbrault.idea.symfony2plugin.tests.SymfonyLightCodeInsightFixtureTestCase;
@@ -14,6 +16,7 @@ public class TwigFilterCompletionContributorTest extends SymfonyLightCodeInsight
1416

1517
public void setUp() throws Exception {
1618
super.setUp();
19+
myFixture.copyFileToProject("classes.php");
1720
myFixture.copyFileToProject("TwigFilterExtension.php");
1821
}
1922

@@ -132,4 +135,28 @@ public void testMacroImport() {
132135
assertCompletionContains(TwigFileType.INSTANCE, "{% macro foo() %}{% endmacro %}{% import _self as bar %}{{ <caret> }}", "bar.foo");
133136
assertNavigationMatchWithParent(TwigFileType.INSTANCE, "{% macro foo() %}{% endmacro %}{% import _self as bar %}{{ bar.f<caret>oo }}", TwigElementTypes.MACRO_STATEMENT);
134137
}
138+
139+
/**
140+
* @see fr.adrienbrault.idea.symfony2plugin.templating.TwigTemplateCompletionContributor
141+
* @see fr.adrienbrault.idea.symfony2plugin.templating.TwigTemplateGoToDeclarationHandler
142+
*/
143+
public void testControllerReferences() {
144+
assertCompletionContains(TwigFileType.INSTANCE, "{{ controller('<caret>') }}", "FooBundle:Foo:bar");
145+
assertNavigationMatch(TwigFileType.INSTANCE, "{{ controller('FooBundl<caret>e:Foo:bar') }}", PlatformPatterns.psiElement(Method.class).withName("barAction"));
146+
147+
assertCompletionContains(TwigFileType.INSTANCE, "{{ controller(\"<caret>\") }}", "FooBundle:Foo:bar");
148+
assertNavigationMatch(TwigFileType.INSTANCE, "{{ controller(\"FooBundl<caret>e:Foo:bar\") }}", PlatformPatterns.psiElement(Method.class).withName("barAction"));
149+
150+
assertCompletionContains(TwigFileType.INSTANCE, "{{ render(controller('<caret>')) }}", "FooBundle:Foo:bar");
151+
assertNavigationMatch(TwigFileType.INSTANCE, "{{ render(controller('FooBundl<caret>e:Foo:bar')) }}", PlatformPatterns.psiElement(Method.class).withName("barAction"));
152+
153+
assertCompletionContains(TwigFileType.INSTANCE, "{% render(controller('<caret>')) %}", "FooBundle:Foo:bar");
154+
assertNavigationMatch(TwigFileType.INSTANCE, "{% render(controller('FooBundl<caret>e:Foo:bar')) %}", PlatformPatterns.psiElement(Method.class).withName("barAction"));
155+
156+
assertCompletionContains(TwigFileType.INSTANCE, "{% render '<caret>' %}", "FooBundle:Foo:bar");
157+
assertNavigationMatch(TwigFileType.INSTANCE, "{% render 'FooBundl<caret>e:Foo:bar' %}", PlatformPatterns.psiElement(Method.class).withName("barAction"));
158+
159+
assertCompletionContains(TwigFileType.INSTANCE, "{% render \"<caret>\" %}", "FooBundle:Foo:bar");
160+
assertNavigationMatch(TwigFileType.INSTANCE, "{% render \"FooBundl<caret>e:Foo:bar\" %}", PlatformPatterns.psiElement(Method.class).withName("barAction"));
161+
}
135162
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace Symfony\Component\HttpKernel\Bundle
4+
{
5+
interface Bundle {}
6+
}
7+
8+
namespace FooBundle
9+
{
10+
use Symfony\Component\HttpKernel\Bundle\Bundle;
11+
class FooBundle implements Bundle {}
12+
}
13+
14+
namespace FooBundle\Controller
15+
{
16+
class FooController
17+
{
18+
public function barAction() {}
19+
}
20+
}

0 commit comments

Comments
 (0)
0