|
| 1 | +package com.magento.idea.magento2plugin.indexes; |
| 2 | + |
| 3 | +import com.intellij.codeInsight.completion.PrefixMatcher; |
| 4 | +import com.intellij.openapi.project.Project; |
| 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.search.GlobalSearchScope; |
| 10 | +import com.intellij.psi.util.PsiTreeUtil; |
| 11 | +import com.intellij.psi.xml.*; |
| 12 | +import com.intellij.util.indexing.FileBasedIndex; |
| 13 | +import com.jetbrains.php.PhpIndex; |
| 14 | +import com.jetbrains.php.lang.psi.elements.PhpClass; |
| 15 | +import com.magento.idea.magento2plugin.stubs.indexes.VirtualTypeIndex; |
| 16 | +import com.magento.idea.magento2plugin.xml.XmlPsiTreeUtil; |
| 17 | +import org.jetbrains.annotations.NotNull; |
| 18 | +import org.jetbrains.annotations.Nullable; |
| 19 | + |
| 20 | +import java.util.ArrayList; |
| 21 | +import java.util.Collection; |
| 22 | +import java.util.List; |
| 23 | + |
| 24 | +public class DiIndex { |
| 25 | + |
| 26 | + private static DiIndex INSTANCE; |
| 27 | + |
| 28 | + private Project project; |
| 29 | + |
| 30 | + private DiIndex() { |
| 31 | + } |
| 32 | + |
| 33 | + public static DiIndex getInstance(final Project project) { |
| 34 | + if (null == INSTANCE) { |
| 35 | + INSTANCE = new DiIndex(); |
| 36 | + } |
| 37 | + INSTANCE.project = project; |
| 38 | + return INSTANCE; |
| 39 | + } |
| 40 | + |
| 41 | + @Nullable |
| 42 | + public PhpClass getPhpClassOfArgument(XmlElement psiArgumentValueElement) { |
| 43 | + |
| 44 | + XmlTag typeTag = XmlPsiTreeUtil.getTypeTagOfArgument(psiArgumentValueElement); |
| 45 | + if (null == typeTag) { |
| 46 | + return null; |
| 47 | + } |
| 48 | + |
| 49 | + String className = typeTag.getAttributeValue("name"); |
| 50 | + if (null == className) { |
| 51 | + return null; |
| 52 | + } |
| 53 | + className = getTopTypeOfVirtualType(className); |
| 54 | + |
| 55 | + PhpIndex phpIndex = PhpIndex.getInstance(psiArgumentValueElement.getProject()); |
| 56 | + Collection<PhpClass> phpClasses = phpIndex.getAnyByFQN(className); |
| 57 | + |
| 58 | + if (phpClasses.size() > 0) { |
| 59 | + return phpClasses.iterator().next(); |
| 60 | + } |
| 61 | + |
| 62 | + return null; |
| 63 | + } |
| 64 | + |
| 65 | + @Nullable |
| 66 | + public static PhpClass getPhpClassOfServiceMethod(XmlElement psiMethodValueElement) { |
| 67 | + XmlTag serviceTag = PsiTreeUtil.getParentOfType(psiMethodValueElement, XmlTag.class); |
| 68 | + if (serviceTag == null) { |
| 69 | + return null; |
| 70 | + } |
| 71 | + |
| 72 | + XmlAttribute attribute = serviceTag.getAttribute("class"); |
| 73 | + if (attribute == null) { |
| 74 | + return null; |
| 75 | + } |
| 76 | + |
| 77 | + XmlAttributeValue valueElement = attribute.getValueElement(); |
| 78 | + if (valueElement == null) { |
| 79 | + return null; |
| 80 | + } |
| 81 | + |
| 82 | + for (PsiReference reference : valueElement.getReferences()) { |
| 83 | + if (reference != null) { |
| 84 | + PsiElement element = reference.resolve(); |
| 85 | + if (element instanceof PhpClass) { |
| 86 | + return (PhpClass) element; |
| 87 | + } |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + return null; |
| 92 | + } |
| 93 | + |
| 94 | + public Collection<PsiElement> getVirtualTypeElements(final String name, final GlobalSearchScope scope) { |
| 95 | + Collection<PsiElement> result = new ArrayList<>(); |
| 96 | + |
| 97 | + Collection<VirtualFile> virtualFiles = |
| 98 | + FileBasedIndex.getInstance().getContainingFiles(VirtualTypeIndex.KEY, name, scope); |
| 99 | + |
| 100 | + for (VirtualFile virtualFile : virtualFiles) { |
| 101 | + XmlFile xmlFile = (XmlFile) PsiManager.getInstance(project).findFile(virtualFile); |
| 102 | + if (xmlFile != null) { |
| 103 | + Collection<XmlAttributeValue> valueElements = XmlPsiTreeUtil |
| 104 | + .findAttributeValueElements(xmlFile, "virtualType", "name", name); |
| 105 | + result.addAll(valueElements); |
| 106 | + } |
| 107 | + } |
| 108 | + return result; |
| 109 | + } |
| 110 | + |
| 111 | + public Collection<String> getAllVirtualTypeElementNames(PrefixMatcher prefixMatcher, final GlobalSearchScope scope) { |
| 112 | + Collection<String> keys = |
| 113 | + FileBasedIndex.getInstance().getAllKeys(VirtualTypeIndex.KEY, project); |
| 114 | + |
| 115 | + keys.removeIf(k -> !prefixMatcher.prefixMatches(k)); |
| 116 | + return keys; |
| 117 | + } |
| 118 | + |
| 119 | + @NotNull |
| 120 | + private String getTopTypeOfVirtualType(@NotNull String name) { |
| 121 | + List<String> values; |
| 122 | + int parentNestingLevel = 0; |
| 123 | + int maxNestingLevel = 5; |
| 124 | + |
| 125 | + do { |
| 126 | + values = FileBasedIndex.getInstance() |
| 127 | + .getValues(VirtualTypeIndex.KEY, name, GlobalSearchScope.allScope(project)); |
| 128 | + if (values.size() > 0 && values.get(0) != null) { |
| 129 | + name = values.get(0); |
| 130 | + } |
| 131 | + } while (values.size() > 0 || maxNestingLevel > parentNestingLevel++); |
| 132 | + |
| 133 | + return name; |
| 134 | + } |
| 135 | +} |
0 commit comments