|
| 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.ide.highlighter.XmlFileType; |
| 9 | +import com.intellij.openapi.project.Project; |
| 10 | +import com.intellij.openapi.util.TextRange; |
| 11 | +import com.intellij.psi.PsiElement; |
| 12 | +import com.intellij.psi.PsiReference; |
| 13 | +import com.intellij.psi.PsiReferenceProvider; |
| 14 | +import com.intellij.psi.search.GlobalSearchScope; |
| 15 | +import com.intellij.psi.xml.XmlAttribute; |
| 16 | +import com.intellij.psi.xml.XmlTag; |
| 17 | +import com.intellij.util.ProcessingContext; |
| 18 | +import com.magento.idea.magento2plugin.indexes.PluginIndex; |
| 19 | +import com.magento.idea.magento2plugin.reference.xml.PolyVariantReferenceBase; |
| 20 | +import java.util.ArrayList; |
| 21 | +import java.util.Collection; |
| 22 | +import java.util.List; |
| 23 | +import org.jetbrains.annotations.NotNull; |
| 24 | + |
| 25 | +public class PluginReferenceProvider extends PsiReferenceProvider { |
| 26 | + @Override |
| 27 | + public @NotNull PsiReference[] getReferencesByElement( |
| 28 | + @NotNull final PsiElement element, |
| 29 | + @NotNull final ProcessingContext context |
| 30 | + ) { |
| 31 | + final List<PsiReference> psiReferences = new ArrayList<>(); |
| 32 | + final Project project = element.getProject(); |
| 33 | + final List<PsiElement> psiElements = new ArrayList<>(); |
| 34 | + |
| 35 | + final XmlTag originalPluginTag = (XmlTag) element.getParent().getParent(); |
| 36 | + final XmlTag originalTypeTag = originalPluginTag.getParentTag(); |
| 37 | + final String originalPluginName = originalPluginTag.getAttribute("name").getValue(); |
| 38 | + final String originalTypeName = originalTypeTag.getAttribute("name").getValue(); |
| 39 | + |
| 40 | + final Collection<PsiElement> types = PluginIndex.getInstance(project).getPluginElements( |
| 41 | + originalTypeName, |
| 42 | + GlobalSearchScope.getScopeRestrictedByFileTypes( |
| 43 | + GlobalSearchScope.allScope(project), XmlFileType.INSTANCE |
| 44 | + ) |
| 45 | + ); |
| 46 | + |
| 47 | + for (final PsiElement type: types) { |
| 48 | + final XmlTag typeTag = (XmlTag) type.getParent().getParent(); |
| 49 | + final XmlTag[] pluginTags = typeTag.findSubTags("plugin"); |
| 50 | + for (final XmlTag pluginTag: pluginTags) { |
| 51 | + final XmlAttribute pluginNameAttribute = pluginTag.getAttribute("name"); |
| 52 | + if (pluginNameAttribute.getValue().equals(originalPluginName)) { |
| 53 | + psiElements.add(pluginNameAttribute.getValueElement()); |
| 54 | + } |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + if (!psiElements.isEmpty()) { |
| 59 | + final int startIndex = element.getText().indexOf(originalPluginName); |
| 60 | + final int endIndex = startIndex + originalPluginName.length(); |
| 61 | + final TextRange range = new TextRange(startIndex, endIndex); |
| 62 | + psiReferences.add(new PolyVariantReferenceBase(element, range, psiElements)); |
| 63 | + } |
| 64 | + |
| 65 | + return psiReferences.toArray(new PsiReference[0]); |
| 66 | + } |
| 67 | +} |
0 commit comments