forked from magento/magento2-phpstorm-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathXmlIndex.java
92 lines (72 loc) · 2.9 KB
/
XmlIndex.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
package com.magento.idea.magento2plugin.indexes;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.PsiManager;
import com.intellij.psi.search.GlobalSearchScope;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.psi.xml.*;
import com.intellij.util.indexing.FileBasedIndex;
import com.jetbrains.php.lang.psi.elements.PhpClass;
import com.magento.idea.magento2plugin.stubs.indexes.xml.PhpClassNameIndex;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
public class XmlIndex {
private static XmlIndex INSTANCE;
private Project project;
private XmlIndex() {
}
public static XmlIndex getInstance(final Project project) {
if (null == INSTANCE) {
INSTANCE = new XmlIndex();
}
INSTANCE.project = project;
return INSTANCE;
}
public static List<XmlTag> getPhpClassDeclarations(PhpClass phpClass) {
List<XmlTag> result = new ArrayList<>();
String fqn = phpClass.getPresentableFQN();
PsiManager psiManager = PsiManager.getInstance(phpClass.getProject());
Collection<VirtualFile> vfs = FileBasedIndex.getInstance()
.getContainingFiles(PhpClassNameIndex.KEY, fqn, GlobalSearchScope.allScope(phpClass.getProject()));
for (VirtualFile vf : vfs) {
XmlFile xmlFile = (XmlFile)psiManager.findFile(vf);
if (xmlFile == null) {
continue;
}
XmlTag xmlTags[] = PsiTreeUtil.getChildrenOfType(xmlFile.getFirstChild(), XmlTag.class);
if (xmlTags == null) {
continue;
}
for (XmlTag xmlTag: xmlTags) {
fillList(xmlTag, fqn, result);
}
}
return result;
}
private static void fillList(XmlTag parentTag, String fqn, List<XmlTag> list) {
for (XmlTag childTag: parentTag.getSubTags()) {
for (XmlAttribute xmlAttribute: childTag.getAttributes()) {
String xmlAttributeValue = xmlAttribute.getValue();
if (xmlAttributeValue != null) {
xmlAttributeValue = xmlAttributeValue.startsWith("\\")
? xmlAttributeValue.substring(1) : xmlAttributeValue;
if (xmlAttributeValue.startsWith(fqn)) {
list.add(childTag);
}
}
}
XmlTagValue childTagValue = childTag.getValue();
String tagValue = childTagValue.getTrimmedText();
tagValue = tagValue.startsWith("\\") ? tagValue.substring(1) : tagValue;
if (!tagValue.isEmpty() && tagValue.startsWith(fqn)) {
list.add(childTag);
}
fillList(childTag, fqn, list);
}
}
}