Skip to content

Commit 330267f

Browse files
committed
fix "Cannot resolve symbol" for factory service regression and drop deprecated getVariant for factory method completion #791
1 parent a701f48 commit 330267f

File tree

9 files changed

+256
-64
lines changed

9 files changed

+256
-64
lines changed

src/fr/adrienbrault/idea/symfony2plugin/completion/xml/XmlGotoCompletionRegistrar.java

+52-1
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,16 @@
99
import com.intellij.psi.xml.XmlAttributeValue;
1010
import com.intellij.psi.xml.XmlTag;
1111
import com.intellij.psi.xml.XmlToken;
12+
import com.intellij.util.containers.ContainerUtil;
13+
import com.jetbrains.php.completion.PhpLookupElement;
14+
import com.jetbrains.php.lang.psi.elements.PhpClass;
1215
import fr.adrienbrault.idea.symfony2plugin.Symfony2Icons;
1316
import fr.adrienbrault.idea.symfony2plugin.codeInsight.GotoCompletionProvider;
1417
import fr.adrienbrault.idea.symfony2plugin.codeInsight.GotoCompletionRegistrar;
1518
import fr.adrienbrault.idea.symfony2plugin.codeInsight.GotoCompletionRegistrarParameter;
1619
import fr.adrienbrault.idea.symfony2plugin.codeInsight.utils.GotoCompletionUtil;
1720
import fr.adrienbrault.idea.symfony2plugin.config.xml.XmlHelper;
21+
import fr.adrienbrault.idea.symfony2plugin.util.PhpElementsUtil;
1822
import fr.adrienbrault.idea.symfony2plugin.util.dict.ServiceUtil;
1923
import fr.adrienbrault.idea.symfony2plugin.util.resource.FileResourceUtil;
2024
import org.apache.commons.lang.StringUtils;
@@ -23,6 +27,7 @@
2327
import java.util.ArrayList;
2428
import java.util.Collection;
2529
import java.util.Collections;
30+
import java.util.stream.Collectors;
2631

2732
public class XmlGotoCompletionRegistrar implements GotoCompletionRegistrar {
2833

@@ -39,11 +44,20 @@ public void register(GotoCompletionRegistrarParameter registrar) {
3944
XmlPatterns.psiElement().withParent(XmlHelper.getServiceIdNamePattern()),
4045
ServiceIdCompletionProvider::new
4146
);
47+
48+
// <factory class="AppBundle\Trivago\ConfigFactory" method="create"/>
49+
// <factory service="foo" method="create"/>
50+
registrar.register(
51+
XmlPatterns.psiElement().withParent(XmlHelper.getTagAttributePattern("factory", "method")
52+
.inside(XmlHelper.getInsideTagPattern("services"))
53+
.inFile(XmlHelper.getXmlFilePattern())),
54+
ServiceFactoryMethodCompletionProvider::new
55+
);
4256
}
4357

4458
private static class ImportResourceGotoCompletionProvider extends GotoCompletionProvider {
4559

46-
public ImportResourceGotoCompletionProvider(PsiElement element) {
60+
ImportResourceGotoCompletionProvider(PsiElement element) {
4761
super(element);
4862
}
4963

@@ -114,4 +128,41 @@ public Collection<PsiElement> getPsiTargets(PsiElement element) {
114128
return Collections.emptyList();
115129
}
116130
}
131+
132+
private static class ServiceFactoryMethodCompletionProvider extends GotoCompletionProvider {
133+
ServiceFactoryMethodCompletionProvider(PsiElement element) {
134+
super(element);
135+
}
136+
137+
@NotNull
138+
@Override
139+
public Collection<LookupElement> getLookupElements() {
140+
PsiElement parent = getElement().getParent();
141+
if(!(parent instanceof XmlAttributeValue)) {
142+
return Collections.emptyList();
143+
}
144+
145+
Collection<PhpClass> phpClasses = new ArrayList<>();
146+
147+
ContainerUtil.addIfNotNull(phpClasses, XmlHelper.getPhpClassForClassFactory((XmlAttributeValue) parent));
148+
ContainerUtil.addIfNotNull(phpClasses, XmlHelper.getPhpClassForServiceFactory((XmlAttributeValue) parent));
149+
150+
Collection<LookupElement> lookupElements = new ArrayList<>();
151+
152+
for (PhpClass phpClass : phpClasses) {
153+
lookupElements.addAll(PhpElementsUtil.getClassPublicMethod(phpClass).stream()
154+
.map(PhpLookupElement::new)
155+
.collect(Collectors.toList())
156+
);
157+
}
158+
159+
return lookupElements;
160+
}
161+
162+
@NotNull
163+
@Override
164+
public Collection<PsiElement> getPsiTargets(PsiElement element) {
165+
return Collections.emptyList();
166+
}
167+
}
117168
}

src/fr/adrienbrault/idea/symfony2plugin/config/ClassPublicMethodReference.java

+2-13
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
import com.jetbrains.php.completion.PhpLookupElement;
99
import com.jetbrains.php.lang.psi.elements.Method;
1010
import com.jetbrains.php.lang.psi.elements.PhpClass;
11-
import com.jetbrains.php.lang.psi.elements.StringLiteralExpression;
1211
import fr.adrienbrault.idea.symfony2plugin.util.PhpElementsUtil;
1312
import fr.adrienbrault.idea.symfony2plugin.util.PsiElementUtils;
1413
import fr.adrienbrault.idea.symfony2plugin.util.dict.ServiceUtil;
@@ -22,22 +21,15 @@ public class ClassPublicMethodReference extends PsiPolyVariantReferenceBase<PsiE
2221
private String className;
2322
private String method;
2423

25-
public ClassPublicMethodReference(@NotNull PsiElement element, String className) {
24+
public ClassPublicMethodReference(@NotNull PsiElement element, @NotNull String className) {
2625
super(element);
2726
this.className = className;
2827
this.method = PsiElementUtils.trimQuote(element.getText());
2928
}
3029

31-
public ClassPublicMethodReference(@NotNull StringLiteralExpression element, String className) {
32-
super(element);
33-
this.className = className;
34-
this.method = element.getContents();
35-
}
36-
3730
@NotNull
3831
@Override
3932
public ResolveResult[] multiResolve(boolean incompleteCode) {
40-
4133
PhpClass phpClass = ServiceUtil.getResolvedClassDefinition(getElement().getProject(), this.className);
4234
if(phpClass == null) {
4335
return new ResolveResult[0];
@@ -48,10 +40,7 @@ public ResolveResult[] multiResolve(boolean incompleteCode) {
4840
return new ResolveResult[0];
4941
}
5042

51-
return new ResolveResult[] {
52-
new PsiElementResolveResult(targetMethod)
53-
};
54-
43+
return PsiElementResolveResult.createResults(targetMethod);
5544
}
5645

5746
@NotNull

src/fr/adrienbrault/idea/symfony2plugin/config/xml/XmlHelper.java

+44
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,11 @@
66
import com.intellij.psi.impl.source.xml.XmlDocumentImpl;
77
import com.intellij.psi.util.PsiTreeUtil;
88
import com.intellij.psi.xml.*;
9+
import com.jetbrains.php.lang.psi.elements.PhpClass;
10+
import fr.adrienbrault.idea.symfony2plugin.util.PhpElementsUtil;
11+
import fr.adrienbrault.idea.symfony2plugin.util.dict.ServiceUtil;
912
import org.apache.commons.lang.StringUtils;
13+
import org.jetbrains.annotations.NotNull;
1014
import org.jetbrains.annotations.Nullable;
1115

1216
import java.util.HashMap;
@@ -426,4 +430,44 @@ public static String getServiceDefinitionClass(PsiElement psiInsideService) {
426430
return null;
427431
}
428432

433+
434+
@Nullable
435+
public static PhpClass getPhpClassForClassFactory(@NotNull XmlAttributeValue xmlAttributeValue) {
436+
String method = xmlAttributeValue.getValue();
437+
if(StringUtils.isBlank(method)) {
438+
return null;
439+
}
440+
441+
XmlTag parentOfType = PsiTreeUtil.getParentOfType(xmlAttributeValue, XmlTag.class);
442+
if(parentOfType == null) {
443+
return null;
444+
}
445+
446+
String aClass = parentOfType.getAttributeValue("class");
447+
if(aClass == null || StringUtils.isBlank(aClass)) {
448+
return null;
449+
}
450+
451+
return PhpElementsUtil.getClass(xmlAttributeValue.getProject(), aClass);
452+
}
453+
454+
@Nullable
455+
public static PhpClass getPhpClassForServiceFactory(@NotNull XmlAttributeValue xmlAttributeValue) {
456+
String method = xmlAttributeValue.getValue();
457+
if(StringUtils.isBlank(method)) {
458+
return null;
459+
}
460+
461+
XmlTag callXmlTag = PsiTreeUtil.getParentOfType(xmlAttributeValue, XmlTag.class);
462+
if(callXmlTag == null) {
463+
return null;
464+
}
465+
466+
String service = callXmlTag.getAttributeValue("service");
467+
if(StringUtils.isBlank(service)) {
468+
return null;
469+
}
470+
471+
return ServiceUtil.getResolvedClassDefinition(xmlAttributeValue.getProject(), service);
472+
}
429473
}

0 commit comments

Comments
 (0)