Skip to content

Fix read thread access of Twig lookup elements rendering #2436

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 9, 2025
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 @@ -8,40 +8,45 @@
import fr.adrienbrault.idea.symfony2plugin.templating.util.TwigTypeResolveUtil;
import fr.adrienbrault.idea.symfony2plugin.util.completion.TwigTypeInsertHandler;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

/**
* @author Daniel Espendiller <daniel@espendiller.net>
*/
public class PhpTwigMethodLookupElement extends PhpLookupElement {
@Nullable
private String methodName;

PhpTwigMethodLookupElement(@NotNull PhpNamedElement namedElement) {
super(namedElement);

if (namedElement instanceof Method method) {
this.methodName = method.getName();
}
}

@Override
public void handleInsert(InsertionContext context) {
public void handleInsert(@NotNull InsertionContext context) {
TwigTypeInsertHandler.getInstance().handleInsert(context, this);
}

@Override
public void renderElement(LookupElementPresentation presentation) {
public void renderElement(@NotNull LookupElementPresentation presentation) {
super.renderElement(presentation);

PhpNamedElement phpNamedElement = this.getNamedElement();

// reset method to show full name again, which was stripped inside getLookupString
if(phpNamedElement instanceof Method && TwigTypeResolveUtil.isPropertyShortcutMethod((Method) phpNamedElement)) {
presentation.setItemText(phpNamedElement.getName());
if (methodName != null && TwigTypeResolveUtil.isPropertyShortcutMethod(methodName)) {
presentation.setItemText(methodName);
}

}

@NotNull
public String getLookupString() {
String lookupString = super.getLookupString();

// remove property shortcuts eg getter / issers
if(this.getNamedElement() instanceof Method && TwigTypeResolveUtil.isPropertyShortcutMethod((Method) this.getNamedElement())) {
lookupString = TwigTypeResolveUtil.getPropertyShortcutMethodName((Method) this.getNamedElement());
if (methodName != null && TwigTypeResolveUtil.isPropertyShortcutMethod(methodName)) {
lookupString = TwigTypeResolveUtil.getPropertyShortcutMethodName(methodName);
}

return lookupString;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -658,9 +658,12 @@ public static String getTypeDisplayName(Project project, Set<String> types) {
}

public static boolean isPropertyShortcutMethod(Method method) {
return isPropertyShortcutMethod(method.getName());
}

for(String shortcut: PROPERTY_SHORTCUTS) {
if(method.getName().startsWith(shortcut) && method.getName().length() > shortcut.length()) {
public static boolean isPropertyShortcutMethod(String methodName) {
for (String shortcut: PROPERTY_SHORTCUTS) {
if (methodName.startsWith(shortcut) && methodName.length() > shortcut.length()) {
return true;
}
}
Expand All @@ -669,9 +672,8 @@ public static boolean isPropertyShortcutMethod(Method method) {
}

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

for(String shortcut: PROPERTY_SHORTCUTS) {
if(methodName.equalsIgnoreCase(shortcut + variableName)) {
for (String shortcut: PROPERTY_SHORTCUTS) {
if (methodName.equalsIgnoreCase(shortcut + variableName)) {
return true;
}
}
Expand All @@ -688,11 +690,13 @@ public static boolean isPropertyShortcutMethodEqual(String methodName, String va
*/
@NotNull
public static String getPropertyShortcutMethodName(@NotNull Method method) {
String methodName = method.getName();
return getPropertyShortcutMethodName(method.getName());
}

for(String shortcut: PROPERTY_SHORTCUTS) {
public static String getPropertyShortcutMethodName(@NotNull String methodName) {
for (String shortcut: PROPERTY_SHORTCUTS) {
// strip possible property shortcut and make it lcfirst
if(method.getName().startsWith(shortcut) && method.getName().length() > shortcut.length()) {
if (methodName.startsWith(shortcut) && methodName.length() > shortcut.length()) {
methodName = methodName.substring(shortcut.length());
return Character.toLowerCase(methodName.charAt(0)) + methodName.substring(1);
}
Expand Down