Skip to content

Commit 7a30df7

Browse files
author
Vitaliy
authored
Merge branch '2.0.0-develop' into fixed-usage-of-JavaScriptFileType
2 parents 2ab9e61 + a5f150f commit 7a30df7

File tree

6 files changed

+203
-6
lines changed

6 files changed

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

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

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,6 @@ public XmlCompletionContributor() {
6262
.inside(XmlPatterns.xmlAttribute().withName(CommonXml.ATTR_CLASS)),
6363
new PhpClassCompletionProvider()
6464
);
65-
6665
// <preference for="completion">
6766
extend(CompletionType.BASIC, psiElement(XmlTokenType.XML_ATTRIBUTE_VALUE_TOKEN)
6867
.inside(XmlPatterns.xmlAttribute().withName(ModuleDiXml.PREFERENCE_ATTR_FOR)),
@@ -129,12 +128,20 @@ public XmlCompletionContributor() {
129128
extend(CompletionType.BASIC, psiElement(XmlTokenType.XML_ATTRIBUTE_VALUE_TOKEN)
130129
.inside(XmlPatterns.xmlAttribute().withName(ModuleEventsXml.INSTANCE_ATTRIBUTE)
131130
.withParent(XmlPatterns.xmlTag().withName(ModuleEventsXml.OBSERVER_TAG)
132-
.withParent(XmlPatterns.xmlTag().withName(ModuleEventsXml.EVENT_TAG))
133131
)
134132
).inFile(xmlFile().withName(string().matches(ModuleEventsXml.FILE_NAME))),
135133
new PhpClassCompletionProvider()
136134
);
137135

136+
// <job instance="class">
137+
extend(CompletionType.BASIC, psiElement(XmlTokenType.XML_ATTRIBUTE_VALUE_TOKEN)
138+
.inside(XmlPatterns.xmlAttribute().withName(CommonXml.ATTR_INSTANCE)
139+
.withParent(XmlPatterns.xmlTag().withName(CrontabXmlTemplate.CRON_JOB_TAG)
140+
)
141+
).inFile(xmlFile().withName(string().matches(CrontabXmlTemplate.FILE_NAME))),
142+
new PhpClassCompletionProvider()
143+
);
144+
138145
// <source_model>php class completion</source_model> in system.xml files.
139146
extend(CompletionType.BASIC, psiElement(XmlTokenType.XML_DATA_CHARACTERS)
140147
.inside(XmlPatterns.xmlTag().withName(ModuleSystemXml.XML_TAG_SOURCE_MODEL)
@@ -182,6 +189,14 @@ public XmlCompletionContributor() {
182189
new PhpServiceMethodCompletionContributor()
183190
);
184191

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

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

Lines changed: 83 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
* Copyright © Magento, Inc. All rights reserved.
33
* See COPYING.txt for license details.
44
*/
5+
56
package com.magento.idea.magento2plugin.indexes;
67

78
import com.intellij.codeInsight.completion.PrefixMatcher;
@@ -12,19 +13,24 @@
1213
import com.intellij.psi.PsiReference;
1314
import com.intellij.psi.search.GlobalSearchScope;
1415
import com.intellij.psi.util.PsiTreeUtil;
15-
import com.intellij.psi.xml.*;
16+
// CHECKSTYLE IGNORE check FOR NEXT 1 LINES
17+
import com.intellij.psi.xml.*;//NOPMD
1618
import com.intellij.util.indexing.FileBasedIndex;
1719
import com.jetbrains.php.PhpIndex;
1820
import com.jetbrains.php.lang.psi.elements.PhpClass;
1921
import com.magento.idea.magento2plugin.stubs.indexes.VirtualTypeIndex;
2022
import com.magento.idea.magento2plugin.util.xml.XmlPsiTreeUtil;
21-
import org.jetbrains.annotations.NotNull;
22-
import org.jetbrains.annotations.Nullable;
23-
23+
// CHECKSTYLE IGNORE check FOR NEXT 1 LINES
2424
import java.util.ArrayList;
2525
import java.util.Collection;
2626
import java.util.List;
27+
import org.jetbrains.annotations.NotNull;
28+
import org.jetbrains.annotations.Nullable;
2729

30+
/**
31+
* TODO: enable style checks after decomposition.
32+
*/
33+
@SuppressWarnings({"PMD", "checkstyle:all"})
2834
public class DiIndex {
2935

3036
private static DiIndex INSTANCE;
@@ -34,6 +40,12 @@ public class DiIndex {
3440
private DiIndex() {
3541
}
3642

43+
/**
44+
* Get Instance.
45+
*
46+
* @param project
47+
* @return DiIndex
48+
*/
3749
public static DiIndex getInstance(final Project project) {
3850
if (null == INSTANCE) {
3951
INSTANCE = new DiIndex();
@@ -42,6 +54,12 @@ public static DiIndex getInstance(final Project project) {
4254
return INSTANCE;
4355
}
4456

57+
/**
58+
* Get Php Class Of Argument.
59+
*
60+
* @param psiArgumentValueElement
61+
* @return PhpClass
62+
*/
4563
@Nullable
4664
public PhpClass getPhpClassOfArgument(XmlElement psiArgumentValueElement) {
4765

@@ -66,6 +84,12 @@ public PhpClass getPhpClassOfArgument(XmlElement psiArgumentValueElement) {
6684
return null;
6785
}
6886

87+
/**
88+
* Get Php Class Of Service Method.
89+
*
90+
* @param psiMethodValueElement
91+
* @return PhpClass
92+
*/
6993
@Nullable
7094
public static PhpClass getPhpClassOfServiceMethod(XmlElement psiMethodValueElement) {
7195
XmlTag serviceTag = PsiTreeUtil.getParentOfType(psiMethodValueElement, XmlTag.class);
@@ -95,6 +119,48 @@ public static PhpClass getPhpClassOfServiceMethod(XmlElement psiMethodValueEleme
95119
return null;
96120
}
97121

122+
/**
123+
* Get Php Class Of Job Method.
124+
*
125+
* @param psiMethodValueElement
126+
* @return PhpClass
127+
*/
128+
@Nullable
129+
public static PhpClass getPhpClassOfJobMethod(XmlElement psiMethodValueElement) {
130+
XmlTag serviceTag = PsiTreeUtil.getParentOfType(psiMethodValueElement, XmlTag.class);
131+
if (serviceTag == null) {
132+
return null;
133+
}
134+
135+
XmlAttribute attribute = serviceTag.getAttribute("instance");
136+
if (attribute == null) {
137+
return null;
138+
}
139+
140+
XmlAttributeValue valueElement = attribute.getValueElement();
141+
if (valueElement == null) {
142+
return null;
143+
}
144+
145+
for (PsiReference reference : valueElement.getReferences()) {
146+
if (reference != null) {
147+
PsiElement element = reference.resolve();
148+
if (element instanceof PhpClass) {
149+
return (PhpClass) element;
150+
}
151+
}
152+
}
153+
154+
return null;
155+
}
156+
157+
/**
158+
* Get Virtual Type Elements.
159+
*
160+
* @param name
161+
* @param scope
162+
* @return Collection<PsiElement>
163+
*/
98164
public Collection<PsiElement> getVirtualTypeElements(final String name, final GlobalSearchScope scope) {
99165
Collection<PsiElement> result = new ArrayList<>();
100166

@@ -112,6 +178,13 @@ public Collection<PsiElement> getVirtualTypeElements(final String name, final Gl
112178
return result;
113179
}
114180

181+
/**
182+
* Get All Virtual Type Element Names.
183+
*
184+
* @param prefixMatcher
185+
* @param scope
186+
* @return Collection<String>
187+
*/
115188
public Collection<String> getAllVirtualTypeElementNames(PrefixMatcher prefixMatcher, final GlobalSearchScope scope) {
116189
Collection<String> keys =
117190
FileBasedIndex.getInstance().getAllKeys(VirtualTypeIndex.KEY, project);
@@ -120,6 +193,12 @@ public Collection<String> getAllVirtualTypeElementNames(PrefixMatcher prefixMatc
120193
return keys;
121194
}
122195

196+
/**
197+
* Get Top Type Of Virtual Type.
198+
*
199+
* @param name
200+
* @return String
201+
*/
123202
@NotNull
124203
private String getTopTypeOfVirtualType(@NotNull String name) {
125204
List<String> values;

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: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/**
2+
* Copyright © Magento, Inc. All rights reserved.
3+
* See COPYING.txt for license details.
4+
*/
5+
6+
package com.magento.idea.magento2plugin.reference.provider;
7+
8+
import com.intellij.openapi.util.text.StringUtil;
9+
import com.intellij.psi.PsiElement;
10+
import com.intellij.psi.PsiReference;
11+
import com.intellij.psi.PsiReferenceProvider;
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 com.magento.idea.magento2plugin.reference.xml.PolyVariantReferenceBase;
18+
// CHECKSTYLE IGNORE check FOR NEXT 1 LINES
19+
import java.util.ArrayList;
20+
import java.util.Collection;
21+
import java.util.List;
22+
import org.jetbrains.annotations.NotNull;
23+
24+
/**
25+
* TODO: enable style checks after decomposition.
26+
*/
27+
@SuppressWarnings({"PMD", "checkstyle:all"})
28+
public class PhpJobMethodReferenceProvider extends PsiReferenceProvider {
29+
30+
@NotNull
31+
@Override
32+
public PsiReference[] getReferencesByElement(@NotNull PsiElement element, @NotNull ProcessingContext context) {
33+
if (!(element instanceof XmlElement)) {
34+
return PsiReference.EMPTY_ARRAY;
35+
}
36+
37+
List<PsiReference> psiReferences = new ArrayList<>();
38+
39+
String methodName = StringUtil.unquoteString(element.getText());
40+
41+
PhpClass phpClass = DiIndex.getPhpClassOfJobMethod((XmlElement) element);
42+
if (phpClass != null) {
43+
Collection<Method> methods = phpClass.getMethods();
44+
methods.removeIf(m -> !m.getName().equalsIgnoreCase(methodName));
45+
psiReferences.add(new PolyVariantReferenceBase(element, methods));
46+
}
47+
48+
return psiReferences.toArray(new PsiReference[psiReferences.size()]);
49+
}
50+
}

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)