8000 add support for Twig 2 hassers for the attribute operator #964 · Koc/idea-php-symfony2-plugin@182b5da · GitHub
[go: up one dir, main page]

Skip to content

Commit 182b5da

Browse files
committed
add support for Twig 2 hassers for the attribute operator Haehnchen#964
1 parent 8156024 commit 182b5da

File tree

4 files changed

+45
-6
lines changed

4 files changed

+45
-6
lines changed

src/fr/adrienbrault/idea/symfony2plugin/templating/util/TwigTypeResolveUtil.java

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public class TwigTypeResolveUtil {
4949
// for supporting completion and navigation of one line element
5050
public static final String DOC_TYPE_PATTERN_SINGLE = "\\{#[\\s]+@var[\\s]+([\\w]+)[\\s]+([\\w\\\\\\[\\]]+)[\\s]+#}";
5151

52-
private static String[] propertyShortcuts = new String[] {"get", "is"};
52+
private static String[] PROPERTY_SHORTCUTS = new String[] {"get", "is", "has"};
5353

5454
private static final ExtensionPointName<TwigFileVariableCollector> TWIG_FILE_VARIABLE_COLLECTORS = new ExtensionPointName<>(
5555
"fr.adrienbrault.idea.symfony2plugin.extension.TwigVariableCollector"
@@ -461,7 +461,7 @@ public static String getTypeDisplayName(Project project, Set<String> types) {
461461

462462
public static boolean isPropertyShortcutMethod(Method method) {
463463

464-
for(String shortcut: propertyShortcuts) {
464+
for(String shortcut: PROPERTY_SHORTCUTS) {
465465
if(method.getName().startsWith(shortcut) && method.getName().length() > shortcut.length()) {
466466
return true;
467467
}
@@ -472,7 +472,7 @@ public static boolean isPropertyShortcutMethod(Method method) {
472472

473473
public static boolean isPropertyShortcutMethodEqual(String methodName, String variableName) {
474474

475-
for(String shortcut: propertyShortcuts) {
475+
for(String shortcut: PROPERTY_SHORTCUTS) {
476476
if(methodName.equalsIgnoreCase(shortcut + variableName)) {
477477
return true;
478478
}
@@ -481,10 +481,18 @@ public static boolean isPropertyShortcutMethodEqual(String methodName, String va
481481
return false;
482482
}
483483

484-
public static String getPropertyShortcutMethodName(Method method) {
485-
484+
/**
485+
* Twig attribute shortcuts
486+
*
487+
* getFoo => foo
488+
* hasFoo => foo
489+
* isFoo => foo
490+
*/
491+
@NotNull
492+
public static String getPropertyShortcutMethodName(@NotNull Method method) {
486493
String methodName = method.getName();
487-
for(String shortcut: propertyShortcuts) {
494+
495+
for(String shortcut: PROPERTY_SHORTCUTS) {
488496
// strip possible property shortcut and make it lcfirst
489497
if(method.getName().startsWith(shortcut) && method.getName().length() > shortcut.length()) {
490498
methodName = methodName.substring(shortcut.length());

tests/fr/adrienbrault/idea/symfony2plugin/tests/templating/inspection/TwigVariablePathInspectionTest.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ public void testThatOnlyVariablesWithPublicAccessLevelAreHighlighted() {
3232

3333
assertLocalInspectionNotContains("f.html.twig", "{# @var bar \\Foo\\Bar #} {{ bar.c<caret>ar }}", "Field or method not found");
3434
assertLocalInspectionNotContains("f.html.twig", "{# @var bar \\Foo\\Bar #} {{ bar.is<caret>Car }}", "Field or method not found");
35+
assertLocalInspectionNotContains("f.html.twig", "{# @var bar \\Foo\\Bar #} {{ bar.has<caret>Hassers }}", "Field or method not found");
36+
assertLocalInspectionNotContains("f.html.twig", "{# @var bar \\Foo\\Bar #} {{ bar.ha<caret>ssers }}", "Field or method not found");
3537
assertLocalInspectionNotContains("f.html.twig", "{# @var bar \\Foo\\Bar #} {{ bar.pub<caret>lic }}", "Field or method not found");
3638
assertLocalInspectionNotContains("f.html.twig", "{# @var bar \\Foo\\Bar #} {{ bar.next.pub<caret>lic }}", "Field or method not found");
3739
assertLocalInspectionNotContains("f.html.twig", "{# @var bar \\Foo\\Bar #} {{ bar.getNext.pub<caret>lic }}", "Field or method not found");

tests/fr/adrienbrault/idea/symfony2plugin/tests/templating/inspection/fixtures/classes.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ class Bar
1515
public $public;
1616

1717
public function getFoo() {}
18+
public function hasHassers() {}
1819
public function isCar() {}
1920
protected function getApple() {}
2021

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package fr.adrienbrault.idea.symfony2plugin.tests.templating.util;
2+
3+
import com.jetbrains.php.lang.psi.PhpPsiElementFactory;
4+
import com.jetbrains.php.lang.psi.elements.Method;
5+
import fr.adrienbrault.idea.symfony2plugin.templating.util.TwigTypeResolveUtil;
6+
import fr.adrienbrault.idea.symfony2plugin.tests.SymfonyLightCodeInsightFixtureTestCase;
7+
import org.jetbrains.annotations.NotNull;
8+
9+
/**
10+
* @author Daniel Espendiller <daniel@espendiller.net>
11+
*/
12+
public class TwigTypeResolveUtilTest extends SymfonyLightCodeInsightFixtureTestCase {
13+
public void testThatTwigGetAttributeSupportShortcuts() {
14+
assertEquals("myFoobar", TwigTypeResolveUtil.getPropertyShortcutMethodName(createMethod("myFoobar")));
15+
assertEquals("foo", TwigTypeResolveUtil.getPropertyShortcutMethodName(createMethod("getFoo")));
16+
assertEquals("foo", TwigTypeResolveUtil.getPropertyShortcutMethodName(createMethod("hasFoo")));
17+
assertEquals("foo", TwigTypeResolveUtil.getPropertyShortcutMethodName(createMethod("isFoo")));
18+
}
19+
20+
@NotNull
21+
private Method createMethod(@NotNull String method) {
22+
return PhpPsiElementFactory.createFromText(
23+
getProject(),
24+
Method.class,
25+
"<?php interface F { function " + method + "(); }"
26+
);
27+
}
28+
}

0 commit comments

Comments
 (0)
0