Skip to content

Commit 1a3b4f1

Browse files
committed
[212]_add reference navigation and completion for crontab.xml
1 parent f45b3a9 commit 1a3b4f1

File tree

6 files changed

+140
-0
lines changed

6 files changed

+140
-0
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/**
2+
* Copyright © Magento, Inc. All rights reserved.
3+
* See COPYING.txt for license details.
4+
*/
5+
package com.magento.idea.magento2plugin.completion.provider;
6+
7+
import com.intellij.codeInsight.completion.CompletionParameters;
8+
import com.intellij.codeInsight.completion.CompletionProvider;
9+
import com.intellij.codeInsight.completion.CompletionResultSet;
10+
import com.intellij.codeInsight.lookup.LookupElementBuilder;
11+
import com.intellij.psi.PsiElement;
12+
import com.intellij.psi.xml.XmlElement;
13+
import com.intellij.util.ProcessingContext;
14+
import com.jetbrains.php.lang.psi.elements.Method;
15+
import com.jetbrains.php.lang.psi.elements.PhpClass;
16+
import com.magento.idea.magento2plugin.indexes.DiIndex;
17+
import org.jetbrains.annotations.NotNull;
18+
19+
public class PhpJobMethodCompletionContributor extends CompletionProvider<CompletionParameters> {
20+
21+
@Override
22+
protected void addCompletions(@NotNull CompletionParameters parameters,
23+
ProcessingContext context,
24+
@NotNull CompletionResultSet result) {
25+
PsiElement position = parameters.getPosition().getOriginalElement();
26+
if (position == null) {
27+
return;
28+
}
29+
30+
PhpClass phpClass = DiIndex.getPhpClassOfJobMethod((XmlElement) position);
31+
if (phpClass != null) {
32+
for (Method method : phpClass.getMethods()) {
33+
result.addElement(
34+
LookupElementBuilder
35+
.create(method.getName())
36+
.withIcon(method.getIcon())
37+
);
38+
}
39+
}
40+
}
41+
}

src/com/magento/idea/magento2plugin/completion/xml/XmlCompletionContributor.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,12 @@ public XmlCompletionContributor() {
6363
new PhpClassCompletionProvider()
6464
);
6565

66+
// <randomTag instance="completion">
67+
extend(CompletionType.BASIC, psiElement(XmlTokenType.XML_ATTRIBUTE_VALUE_TOKEN)
68+
.inside(XmlPatterns.xmlAttribute().withName(CommonXml.ATTR_INSTANCE)),
69+
new PhpClassCompletionProvider()
70+
);
71+
6672
// <preference for="completion">
6773
extend(CompletionType.BASIC, psiElement(XmlTokenType.XML_ATTRIBUTE_VALUE_TOKEN)
6874
.inside(XmlPatterns.xmlAttribute().withName(ModuleDiXml.PREFERENCE_ATTR_FOR)),
@@ -182,6 +188,14 @@ public XmlCompletionContributor() {
182188
new PhpServiceMethodCompletionContributor()
183189
);
184190

191+
// <job method="methodName"/>
192+
extend(CompletionType.BASIC, psiElement(XmlTokenType.XML_ATTRIBUTE_VALUE_TOKEN)
193+
.inside(XmlPatterns.xmlAttribute().withName("method")
194+
.withParent(XmlPatterns.xmlTag().withName("job"))
195+
).inFile(xmlFile().withName(string().endsWith("crontab.xml"))),
196+
new PhpJobMethodCompletionContributor()
197+
);
198+
185199
extend(CompletionType.BASIC, psiElement(XmlTokenType.XML_ATTRIBUTE_VALUE_TOKEN)
186200
.inside(XmlPatterns.xmlAttribute().withName("name")
187201
.withParent(XmlPatterns.xmlTag().withName("referenceContainer"))

src/com/magento/idea/magento2plugin/indexes/DiIndex.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,35 @@ public static PhpClass getPhpClassOfServiceMethod(XmlElement psiMethodValueEleme
9595
return null;
9696
}
9797

98+
@Nullable
99+
public static PhpClass getPhpClassOfJobMethod(XmlElement psiMethodValueElement) {
100+
XmlTag serviceTag = PsiTreeUtil.getParentOfType(psiMethodValueElement, XmlTag.class);
101+
if (serviceTag == null) {
102+
return null;
103+
}
104+
105+
XmlAttribute attribute = serviceTag.getAttribute("instance");
106+
if (attribute == null) {
107+
return null;
108+
}
109+
110+
XmlAttributeValue valueElement = attribute.getValueElement();
111+
if (valueElement == null) {
112+
return null;
113+
}
114+
115+
for (PsiReference reference : valueElement.getReferences()) {
116+
if (reference != null) {
117+
PsiElement element = reference.resolve();
118+
if (element instanceof PhpClass) {
119+
return (PhpClass) element;
120+
}
121+
}
122+
}
123+
124+
return null;
125+
}
126+
98127
public Collection<PsiElement> getVirtualTypeElements(final String name, final GlobalSearchScope scope) {
99128
Collection<PsiElement> result = new ArrayList<>();
100129

src/com/magento/idea/magento2plugin/magento/files/CommonXml.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,6 @@ public class CommonXml {
1111
public static String INIT_PARAMETER = "init_parameter";
1212
public static String OBJECT = "object";
1313
public static String ATTR_CLASS = "class";
14+
public static String ATTR_INSTANCE = "instance";
1415
public static String ATTRIBUTE_ARGUMENTS = "arguments";
1516
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/**
2+
* Copyright © Magento, Inc. All rights reserved.
3+
* See COPYING.txt for license details.
4+
*/
5+
package com.magento.idea.magento2plugin.reference.provider;
6+
7+
import com.intellij.openapi.util.text.StringUtil;
8+
import com.intellij.psi.PsiElement;
9+
import com.intellij.psi.PsiReference;
10+
import com.intellij.psi.PsiReferenceProvider;
11+
import com.intellij.psi.xml.XmlElement;
12+
import com.intellij.util.ProcessingContext;
13+
import com.jetbrains.php.lang.psi.elements.Method;
14+
import com.jetbrains.php.lang.psi.elements.PhpClass;
15+
import com.magento.idea.magento2plugin.indexes.DiIndex;
16+
import com.magento.idea.magento2plugin.reference.xml.PolyVariantReferenceBase;
17+
import org.jetbrains.annotations.NotNull;
18+
19+
import java.util.ArrayList;
20+
import java.util.Collection;
21+
import java.util.List;
22+
23+
public class PhpJobMethodReferenceProvider extends PsiReferenceProvider {
24+
25+
@NotNull
26+
@Override
27+
public PsiReference[] getReferencesByElement(@NotNull PsiElement element, @NotNull ProcessingContext context) {
28+
if (!(element instanceof XmlElement)) {
29+
return PsiReference.EMPTY_ARRAY;
30+
}
31+
32+
List<PsiReference> psiReferences = new ArrayList<>();
33+
34+
String methodName = StringUtil.unquoteString(element.getText());
35+
36+
PhpClass phpClass = DiIndex.getPhpClassOfJobMethod((XmlElement) element);
37+
if (phpClass != null) {
38+
Collection<Method> methods = phpClass.getMethods();
39+
methods.removeIf(m -> !m.getName().equalsIgnoreCase(methodName));
40+
psiReferences.add(new PolyVariantReferenceBase(element, methods));
41+
}
42+
43+
return psiReferences.toArray(new PsiReference[psiReferences.size()]);
44+
}
45+
}

src/com/magento/idea/magento2plugin/reference/xml/XmlReferenceContributor.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,16 @@ public void registerReferenceProviders(@NotNull PsiReferenceRegistrar registrar)
8787
new PhpServiceMethodReferenceProvider()
8888
);
8989

90+
// <job method="methodName"/>
91+
registrar.registerReferenceProvider(
92+
XmlPatterns.xmlAttributeValue().withParent(
93+
XmlPatterns.xmlAttribute().withName("method").withParent(
94+
XmlPatterns.xmlTag().withName("job")
95+
)
96+
).inFile(xmlFile().withName(string().endsWith("crontab.xml"))),
97+
new PhpJobMethodReferenceProvider()
98+
);
99+
90100
registrar.registerReferenceProvider(
91101
XmlPatterns.xmlAttributeValue().withParent(
92102
XmlPatterns.xmlAttribute().withName("name").withParent(

0 commit comments

Comments
 (0)