|
1 | | -/** |
| 1 | +/* |
2 | 2 | * Copyright © Magento, Inc. All rights reserved. |
3 | 3 | * See COPYING.txt for license details. |
4 | 4 | */ |
|
16 | 16 | import com.intellij.psi.xml.XmlTag; |
17 | 17 | import com.intellij.util.ProcessingContext; |
18 | 18 | import com.magento.idea.magento2plugin.indexes.PluginIndex; |
| 19 | +import com.magento.idea.magento2plugin.magento.files.ModuleDiXml; |
19 | 20 | import com.magento.idea.magento2plugin.reference.xml.PolyVariantReferenceBase; |
20 | 21 | import java.util.ArrayList; |
21 | 22 | import java.util.Collection; |
22 | 23 | import java.util.List; |
23 | 24 | import org.jetbrains.annotations.NotNull; |
24 | 25 |
|
25 | 26 | public class PluginReferenceProvider extends PsiReferenceProvider { |
| 27 | + |
| 28 | + @SuppressWarnings({ |
| 29 | + "PMD.CognitiveComplexity", |
| 30 | + "PMD.CyclomaticComplexity", |
| 31 | + "PMD.NPathComplexity" |
| 32 | + }) |
26 | 33 | @Override |
27 | 34 | public @NotNull PsiReference[] getReferencesByElement( |
28 | | - @NotNull final PsiElement element, |
29 | | - @NotNull final ProcessingContext context |
| 35 | + final @NotNull PsiElement element, |
| 36 | + final @NotNull ProcessingContext context |
30 | 37 | ) { |
31 | | - final List<PsiReference> psiReferences = new ArrayList<>(); |
32 | | - final Project project = element.getProject(); |
33 | | - final List<PsiElement> psiElements = new ArrayList<>(); |
| 38 | + if (!(element.getParent() instanceof XmlAttribute) |
| 39 | + || !ModuleDiXml.NAME_ATTR.equals(((XmlAttribute) element.getParent()).getName()) |
| 40 | + || !(element.getParent().getParent() instanceof XmlTag) |
| 41 | + || !ModuleDiXml.PLUGIN_TAG_NAME.equals( |
| 42 | + ((XmlTag) element.getParent().getParent()).getName()) |
| 43 | + ) { |
| 44 | + return PsiReference.EMPTY_ARRAY; |
| 45 | + } |
34 | 46 |
|
35 | 47 | final XmlTag originalPluginTag = (XmlTag) element.getParent().getParent(); |
36 | 48 | final XmlTag originalTypeTag = originalPluginTag.getParentTag(); |
37 | | - final String originalPluginName = originalPluginTag.getAttribute("name").getValue(); |
38 | | - final String originalTypeName = originalTypeTag.getAttribute("name").getValue(); |
| 49 | + |
| 50 | + if (originalTypeTag == null || !ModuleDiXml.TYPE_TAG.equals(originalTypeTag.getName())) { |
| 51 | + return PsiReference.EMPTY_ARRAY; |
| 52 | + } |
| 53 | + final XmlAttribute originalPluginNameAttr = originalPluginTag.getAttribute("name"); |
| 54 | + final XmlAttribute originalTypeNameAttr = originalTypeTag.getAttribute("name"); |
| 55 | + |
| 56 | + if (originalPluginNameAttr == null || originalTypeNameAttr == null) { |
| 57 | + return PsiReference.EMPTY_ARRAY; |
| 58 | + } |
| 59 | + final String originalPluginName = originalPluginNameAttr.getValue(); |
| 60 | + final String originalTypeName = originalTypeNameAttr.getValue(); |
| 61 | + |
| 62 | + if (originalPluginName == null || originalTypeName == null) { |
| 63 | + return PsiReference.EMPTY_ARRAY; |
| 64 | + } |
| 65 | + final Project project = element.getProject(); |
39 | 66 |
|
40 | 67 | final Collection<PsiElement> types = PluginIndex.getInstance(project).getPluginElements( |
41 | 68 | originalTypeName, |
42 | 69 | GlobalSearchScope.getScopeRestrictedByFileTypes( |
43 | 70 | GlobalSearchScope.allScope(project), XmlFileType.INSTANCE |
44 | 71 | ) |
45 | 72 | ); |
| 73 | + final List<PsiElement> psiElements = new ArrayList<>(); |
46 | 74 |
|
47 | 75 | for (final PsiElement type: types) { |
48 | 76 | final XmlTag typeTag = (XmlTag) type.getParent().getParent(); |
49 | 77 | final XmlTag[] pluginTags = typeTag.findSubTags("plugin"); |
| 78 | + |
50 | 79 | for (final XmlTag pluginTag: pluginTags) { |
51 | 80 | final XmlAttribute pluginNameAttribute = pluginTag.getAttribute("name"); |
52 | | - if (pluginNameAttribute.getValue().equals(originalPluginName)) { |
| 81 | + |
| 82 | + if (pluginNameAttribute != null |
| 83 | + && pluginNameAttribute.getValue() != null |
| 84 | + && originalPluginName.equals(pluginNameAttribute.getValue())) { |
53 | 85 | psiElements.add(pluginNameAttribute.getValueElement()); |
54 | 86 | } |
55 | 87 | } |
56 | 88 | } |
| 89 | + final List<PsiReference> psiReferences = new ArrayList<>(); |
57 | 90 |
|
58 | 91 | if (!psiElements.isEmpty()) { |
59 | 92 | final int startIndex = element.getText().indexOf(originalPluginName); |
60 | 93 | final int endIndex = startIndex + originalPluginName.length(); |
61 | 94 | final TextRange range = new TextRange(startIndex, endIndex); |
| 95 | + |
62 | 96 | psiReferences.add(new PolyVariantReferenceBase(element, range, psiElements)); |
63 | 97 | } |
64 | 98 |
|
|
0 commit comments