|
| 1 | +package com.magento.idea.magento2plugin.reference.provider.mftf; |
| 2 | + |
| 3 | +import com.intellij.ide.highlighter.XmlFileType; |
| 4 | +import com.intellij.openapi.util.text.StringUtil; |
| 5 | +import com.intellij.openapi.vfs.VirtualFile; |
| 6 | +import com.intellij.psi.PsiElement; |
| 7 | +import com.intellij.psi.PsiManager; |
| 8 | +import com.intellij.psi.PsiReference; |
| 9 | +import com.intellij.psi.PsiReferenceProvider; |
| 10 | +import com.intellij.psi.search.GlobalSearchScope; |
| 11 | +import com.intellij.psi.xml.XmlAttribute; |
| 12 | +import com.intellij.psi.xml.XmlAttributeValue; |
| 13 | +import com.intellij.psi.xml.XmlFile; |
| 14 | +import com.intellij.psi.xml.XmlTag; |
| 15 | +import com.intellij.util.ProcessingContext; |
| 16 | +import com.intellij.util.indexing.FileBasedIndex; |
| 17 | +import com.magento.idea.magento2plugin.reference.xml.PolyVariantReferenceBase; |
| 18 | +import com.magento.idea.magento2plugin.stubs.indexes.mftf.DataIndex; |
| 19 | +import com.magento.idea.magento2plugin.xml.XmlPsiTreeUtil; |
| 20 | +import org.jetbrains.annotations.NotNull; |
| 21 | + |
| 22 | +import java.util.ArrayList; |
| 23 | +import java.util.Collection; |
| 24 | +import java.util.List; |
| 25 | + |
| 26 | + |
| 27 | +public class DataReferenceProvider extends PsiReferenceProvider { |
| 28 | + |
| 29 | + @NotNull |
| 30 | + @Override |
| 31 | + public PsiReference[] getReferencesByElement(@NotNull PsiElement element, @NotNull ProcessingContext context) { |
| 32 | + List<PsiReference> psiReferences = new ArrayList<>(); |
| 33 | + |
| 34 | + String origValue = StringUtil.unquoteString(element.getText()); |
| 35 | + String modifiedValue = origValue.replaceAll("\\{{2}([_A-Za-z0-9.]+)(\\([^}]+\\))?\\}{2}", "$1").toString(); |
| 36 | + |
| 37 | + Collection<VirtualFile> containingFiles = FileBasedIndex.getInstance() |
| 38 | + .getContainingFiles( |
| 39 | + DataIndex.KEY, |
| 40 | + modifiedValue, |
| 41 | + GlobalSearchScope.getScopeRestrictedByFileTypes( |
| 42 | + GlobalSearchScope.allScope(element.getProject()), |
| 43 | + XmlFileType.INSTANCE |
| 44 | + ) |
| 45 | + ); |
| 46 | + |
| 47 | + PsiManager psiManager = PsiManager.getInstance(element.getProject()); |
| 48 | + |
| 49 | + List<PsiElement> psiElements = new ArrayList<>(); |
| 50 | + |
| 51 | + for (VirtualFile virtualFile: containingFiles) { |
| 52 | + XmlFile xmlFile = (XmlFile) psiManager.findFile(virtualFile); |
| 53 | + |
| 54 | + if (xmlFile == null) { |
| 55 | + continue; |
| 56 | + } |
| 57 | + |
| 58 | + if (!modifiedValue.contains(".")) { |
| 59 | + Collection<XmlAttributeValue> valueElements = XmlPsiTreeUtil |
| 60 | + .findAttributeValueElements(xmlFile, "entity", "name", modifiedValue); |
| 61 | + |
| 62 | + psiElements.addAll(valueElements); |
| 63 | + continue; |
| 64 | + } |
| 65 | + |
| 66 | + |
| 67 | + String[] parts = modifiedValue.split("\\."); |
| 68 | + String entityName = parts[0]; |
| 69 | + String dataName = parts[1]; |
| 70 | + |
| 71 | + XmlTag rootTag = xmlFile.getRootTag(); |
| 72 | + |
| 73 | + for (XmlTag entityTag: rootTag.findSubTags("entity")) { |
| 74 | + if (entityTag == null) { |
| 75 | + continue; |
| 76 | + } |
| 77 | + |
| 78 | + XmlAttribute entityNameAttribute = entityTag.getAttribute("name"); |
| 79 | + |
| 80 | + if (entityNameAttribute == null || |
| 81 | + entityNameAttribute.getValueElement() == null || |
| 82 | + !entityNameAttribute.getValueElement().getValue().equals(entityName) |
| 83 | + ) { |
| 84 | + continue; |
| 85 | + } |
| 86 | + |
| 87 | + for (XmlTag dataTag: entityTag.findSubTags("data")) { |
| 88 | + XmlAttribute keyNameAttribute = dataTag.getAttribute("key"); |
| 89 | + |
| 90 | + if (keyNameAttribute != null && |
| 91 | + keyNameAttribute.getValueElement() != null && |
| 92 | + keyNameAttribute.getValueElement().getValue().equals(dataName) |
| 93 | + ) { |
| 94 | + psiElements.add(keyNameAttribute.getValueElement()); |
| 95 | + } |
| 96 | + } |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + if (psiElements.size() > 0) { |
| 101 | + psiReferences.add(new PolyVariantReferenceBase(element, psiElements)); |
| 102 | + } |
| 103 | + |
| 104 | + return psiReferences.toArray(new PsiReference[psiReferences.size()]); |
| 105 | + } |
| 106 | +} |
0 commit comments