Skip to content

Commit 243d989

Browse files
committed
Move write action out of AWT event for PhpServiceArgumentIntention
Fixes: Haehnchen#1184
1 parent b8008cd commit 243d989

File tree

1 file changed

+50
-43
lines changed

1 file changed

+50
-43
lines changed

src/main/java/fr/adrienbrault/idea/symfony2plugin/dic/intention/PhpServiceArgumentIntention.java

Lines changed: 50 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
import com.intellij.psi.PsiElement;
1111
import com.intellij.psi.util.PsiTreeUtil;
1212
import com.intellij.psi.xml.XmlTag;
13-
import com.intellij.ui.components.JBList;
13+
import com.intellij.util.Consumer;
1414
import com.intellij.util.IncorrectOperationException;
1515
import com.jetbrains.php.lang.PhpLanguage;
1616
import com.jetbrains.php.lang.psi.elements.PhpClass;
@@ -30,57 +30,63 @@ public class PhpServiceArgumentIntention extends PsiElementBaseIntentionAction {
3030
@Override
3131
public void invoke(@NotNull Project project, Editor editor, @NotNull PsiElement psiElement) throws IncorrectOperationException {
3232
PhpClass phpClass = PsiTreeUtil.getParentOfType(psiElement, PhpClass.class);
33-
if(phpClass != null) {
34-
Set<String> serviceNames = ContainerCollectionResolver.ServiceCollector.create(project).convertClassNameToServices(phpClass.getFQN());
35-
if(serviceNames.size() > 0) {
36-
Collection<PsiElement> psiElements = new ArrayList<>();
37-
for(String serviceName: serviceNames) {
38-
psiElements.addAll(ServiceIndexUtil.findServiceDefinitions(project, serviceName));
39-
}
40-
41-
if(psiElements.size() > 0) {
42-
Map<String, PsiElement> map = new HashMap<>();
43-
44-
for (PsiElement element : psiElements) {
45-
map.put(VfsUtil.getRelativePath(element.getContainingFile().getVirtualFile(), element.getProject().getBaseDir()), element);
46-
}
47-
48-
final JBList<String> list = new JBList<>(map.keySet());
49-
50-
JBPopupFactory.getInstance().createListPopupBuilder(list)
51-
.setTitle("Symfony: Services Definitions")
52-
.setItemChoosenCallback(() -> new WriteCommandAction.Simple(editor.getProject(), "Service Update") {
53-
@Override
54-
protected void run() {
55-
String selectedValue = list.getSelectedValue();
56-
57-
PsiElement target = map.get(selectedValue);
58-
invokeByScope(target, editor);
59-
}
60-
}.execute())
61-
.createPopup()
62-
.showInBestPositionFor(editor);
63-
}
64-
}
33+
if (phpClass == null) {
34+
35+
return;
36+
}
37+
38+
Set<String> serviceNames = ContainerCollectionResolver.ServiceCollector.create(project).convertClassNameToServices(phpClass.getFQN());
39+
if (serviceNames.isEmpty()) {
40+
41+
return;
42+
}
43+
44+
Collection<PsiElement> psiElements = new ArrayList<>();
45+
for (String serviceName : serviceNames) {
46+
psiElements.addAll(ServiceIndexUtil.findServiceDefinitions(project, serviceName));
6547
}
48+
49+
if (psiElements.isEmpty()) {
50+
51+
return;
52+
}
53+
54+
Map<String, PsiElement> map = new HashMap<>();
55+
56+
for (PsiElement element : psiElements) {
57+
map.put(VfsUtil.getRelativePath(element.getContainingFile().getVirtualFile(), element.getProject().getBaseDir()), element);
58+
}
59+
60+
Consumer<String> consumer = (selected) -> {
61+
WriteCommandAction.writeCommandAction(project).withName("Service Update").run(() -> {
62+
invokeByScope(map.get(selected), editor);
63+
});
64+
};
65+
66+
JBPopupFactory.getInstance().createPopupChooserBuilder(new ArrayList<>(map.keySet()))
67+
.setTitle("Symfony: Services Definitions")
68+
.setItemChosenCallback(consumer)
69+
.createPopup()
70+
.showInBestPositionFor(editor)
71+
;
6672
}
6773

6874
private void invokeByScope(@NotNull PsiElement psiElement, @NotNull Editor editor) {
6975
boolean success = false;
7076

71-
if(psiElement instanceof XmlTag) {
77+
if (psiElement instanceof XmlTag) {
7278
List<String> args = ServiceActionUtil.getXmlMissingArgumentTypes((XmlTag) psiElement, true, new ContainerCollectionResolver.LazyServiceCollector(psiElement.getProject()));
7379

7480
success = args.size() > 0;
7581
if (success) {
7682
ServiceActionUtil.fixServiceArgument(args, (XmlTag) psiElement);
7783
}
78-
} else if(psiElement instanceof YAMLKeyValue) {
84+
} else if (psiElement instanceof YAMLKeyValue) {
7985
List<String> args = ServiceActionUtil.getYamlMissingArgumentTypes(
80-
psiElement.getProject(),
81-
ServiceActionUtil.ServiceYamlContainer.create((YAMLKeyValue) psiElement),
82-
false,
83-
new ContainerCollectionResolver.LazyServiceCollector(psiElement.getProject())
86+
psiElement.getProject(),
87+
ServiceActionUtil.ServiceYamlContainer.create((YAMLKeyValue) psiElement),
88+
false,
89+
new ContainerCollectionResolver.LazyServiceCollector(psiElement.getProject())
8490
);
8591

8692
success = args.size() > 0;
@@ -89,13 +95,14 @@ private void invokeByScope(@NotNull PsiElement psiElement, @NotNull Editor edito
8995
}
9096
}
9197

92-
if(!success) {
98+
if (!success) {
9399
HintManager.getInstance().showErrorHint(editor, "No argument update needed");
100+
94101
return;
95102
}
96103

97104
String relativePath = VfsUtil.getRelativePath(psiElement.getContainingFile().getVirtualFile(), psiElement.getProject().getBaseDir());
98-
if(relativePath == null) {
105+
if (relativePath == null) {
99106
relativePath = "n/a";
100107
}
101108

@@ -125,7 +132,7 @@ private Set<String> getServicesInScope(@NotNull PsiElement psiElement) {
125132
PhpClass phpClass = PsiTreeUtil.getParentOfType(psiElement, PhpClass.class);
126133

127134
return phpClass == null
128-
? Collections.emptySet()
129-
: ContainerCollectionResolver.ServiceCollector.create(psiElement.getProject()).convertClassNameToServices(phpClass.getFQN());
135+
? Collections.emptySet()
136+
: ContainerCollectionResolver.ServiceCollector.create(psiElement.getProject()).convertClassNameToServices(phpClass.getFQN());
130137
}
131138
}

0 commit comments

Comments
 (0)