Skip to content

Commit a888beb

Browse files
authored
Merge pull request #2436 from Haehnchen/feature/twig-lookup-read-access
Fix read thread access of Twig lookup elements rendering
2 parents fc3c1ea + a0ee9be commit a888beb

File tree

2 files changed

+26
-17
lines changed

2 files changed

+26
-17
lines changed

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

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,40 +8,45 @@
88
import fr.adrienbrault.idea.symfony2plugin.templating.util.TwigTypeResolveUtil;
99
import fr.adrienbrault.idea.symfony2plugin.util.completion.TwigTypeInsertHandler;
1010
import org.jetbrains.annotations.NotNull;
11+
import org.jetbrains.annotations.Nullable;
1112

1213
/**
1314
* @author Daniel Espendiller <daniel@espendiller.net>
1415
*/
1516
public class PhpTwigMethodLookupElement extends PhpLookupElement {
17+
@Nullable
18+
private String methodName;
19+
1620
PhpTwigMethodLookupElement(@NotNull PhpNamedElement namedElement) {
1721
super(namedElement);
22+
23+
if (namedElement instanceof Method method) {
24+
this.methodName = method.getName();
25+
}
1826
}
1927

2028
@Override
21-
public void handleInsert(InsertionContext context) {
29+
public void handleInsert(@NotNull InsertionContext context) {
2230
TwigTypeInsertHandler.getInstance().handleInsert(context, this);
2331
}
2432

2533
@Override
26-
public void renderElement(LookupElementPresentation presentation) {
34+
public void renderElement(@NotNull LookupElementPresentation presentation) {
2735
super.renderElement(presentation);
2836

29-
PhpNamedElement phpNamedElement = this.getNamedElement();
30-
3137
// reset method to show full name again, which was stripped inside getLookupString
32-
if(phpNamedElement instanceof Method && TwigTypeResolveUtil.isPropertyShortcutMethod((Method) phpNamedElement)) {
33-
presentation.setItemText(phpNamedElement.getName());
38+
if (methodName != null && TwigTypeResolveUtil.isPropertyShortcutMethod(methodName)) {
39+
presentation.setItemText(methodName);
3440
}
35-
3641
}
3742

3843
@NotNull
3944
public String getLookupString() {
4045
String lookupString = super.getLookupString();
4146

4247
// remove property shortcuts eg getter / issers
43-
if(this.getNamedElement() instanceof Method && TwigTypeResolveUtil.isPropertyShortcutMethod((Method) this.getNamedElement())) {
44-
lookupString = TwigTypeResolveUtil.getPropertyShortcutMethodName((Method) this.getNamedElement());
48+
if (methodName != null && TwigTypeResolveUtil.isPropertyShortcutMethod(methodName)) {
49+
lookupString = TwigTypeResolveUtil.getPropertyShortcutMethodName(methodName);
4550
}
4651

4752
return lookupString;

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

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -658,9 +658,12 @@ public static String getTypeDisplayName(Project project, Set<String> types) {
658658
}
659659

660660
public static boolean isPropertyShortcutMethod(Method method) {
661+
return isPropertyShortcutMethod(method.getName());
662+
}
661663

662-
for(String shortcut: PROPERTY_SHORTCUTS) {
663-
if(method.getName().startsWith(shortcut) && method.getName().length() > shortcut.length()) {
664+
public static boolean isPropertyShortcutMethod(String methodName) {
665+
for (String shortcut: PROPERTY_SHORTCUTS) {
666+
if (methodName.startsWith(shortcut) && methodName.length() > shortcut.length()) {
664667
return true;
665668
}
666669
}
@@ -669,9 +672,8 @@ public static boolean isPropertyShortcutMethod(Method method) {
669672
}
670673

671674
public static boolean isPropertyShortcutMethodEqual(String methodName, String variableName) {
672-
673-
for(String shortcut: PROPERTY_SHORTCUTS) {
674-
if(methodName.equalsIgnoreCase(shortcut + variableName)) {
675+
for (String shortcut: PROPERTY_SHORTCUTS) {
676+
if (methodName.equalsIgnoreCase(shortcut + variableName)) {
675677
return true;
676678
}
677679
}
@@ -688,11 +690,13 @@ public static boolean isPropertyShortcutMethodEqual(String methodName, String va
688690
*/
689691
@NotNull
690692
public static String getPropertyShortcutMethodName(@NotNull Method method) {
691-
String methodName = method.getName();
693+
return getPropertyShortcutMethodName(method.getName());
694+
}
692695

693-
for(String shortcut: PROPERTY_SHORTCUTS) {
696+
public static String getPropertyShortcutMethodName(@NotNull String methodName) {
697+
for (String shortcut: PROPERTY_SHORTCUTS) {
694698
// strip possible property shortcut and make it lcfirst
695-
if(method.getName().startsWith(shortcut) && method.getName().length() > shortcut.length()) {
699+
if (methodName.startsWith(shortcut) && methodName.length() > shortcut.length()) {
696700
methodName = methodName.substring(shortcut.length());
697701
return Character.toLowerCase(methodName.charAt(0)) + methodName.substring(1);
698702
}

0 commit comments

Comments
 (0)