forked from magento/magento2-phpstorm-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPluginIndex.java
124 lines (107 loc) · 4.11 KB
/
PluginIndex.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
package com.magento.idea.magento2plugin.stubs.indexes;
import com.intellij.ide.highlighter.XmlFileType;
import com.intellij.psi.PsiFile;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.psi.xml.XmlDocument;
import com.intellij.psi.xml.XmlFile;
import com.intellij.psi.xml.XmlTag;
import com.intellij.util.indexing.*;
import com.intellij.util.io.DataExternalizer;
import com.intellij.util.io.EnumeratorStringDescriptor;
import com.intellij.util.io.KeyDescriptor;
import com.jetbrains.php.lang.PhpLangUtil;
import com.jetbrains.php.lang.psi.stubs.indexes.StringSetDataExternalizer;
import com.magento.idea.magento2plugin.project.Settings;
import org.jetbrains.annotations.NotNull;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
public class PluginIndex extends FileBasedIndexExtension<String, Set<String>> {
public static final ID<String, Set<String>> KEY
= ID.create("com.magento.idea.magento2plugin.stubs.indexes.plugin_to_type");
private final KeyDescriptor<String> myKeyDescriptor = new EnumeratorStringDescriptor();
@NotNull
@Override
public ID<String, Set<String>> getName() {
return KEY;
}
@NotNull
@Override
public DataIndexer<String, Set<String>, FileContent> getIndexer() {
return new DataIndexer<String, Set<String>, FileContent>() {
@NotNull
@Override
public Map<String, Set<String>> map(@NotNull FileContent fileContent) {
Map<String, Set<String>> map = new HashMap<>();
PsiFile psiFile = fileContent.getPsiFile();
if (!Settings.isEnabled(psiFile.getProject())) {
return map;
}
if (!(psiFile instanceof XmlFile)) {
return map;
}
XmlDocument document = ((XmlFile) psiFile).getDocument();
if(document == null) {
return map;
}
XmlTag xmlTags[] = PsiTreeUtil.getChildrenOfType(psiFile.getFirstChild(), XmlTag.class);
if (xmlTags == null) {
return map;
}
for (XmlTag xmlTag: xmlTags) {
if (xmlTag.getName().equals("config")) {
for (XmlTag typeNode: xmlTag.findSubTags("type")) {
String typeName = typeNode.getAttributeValue("name");
if (typeName != null) {
Set<String> plugins = getPluginsForType(typeNode);
if (plugins.size() > 0) {
map.put(PhpLangUtil.toPresentableFQN(typeName), plugins);
}
}
}
}
}
return map;
}
private Set<String> getPluginsForType(XmlTag typeNode) {
Set<String> results = new HashSet<String>();
for (XmlTag pluginTag: typeNode.findSubTags("plugin")) {
String pluginType = pluginTag.getAttributeValue("type");
if (pluginType != null) {
results.add(PhpLangUtil.toPresentableFQN(pluginType));
}
}
return results;
}
};
}
@NotNull
@Override
public KeyDescriptor<String> getKeyDescriptor() {
return myKeyDescriptor;
}
@NotNull
@Override
public DataExternalizer<Set<String>> getValueExternalizer() {
return new StringSetDataExternalizer();
}
@NotNull
@Override
public FileBasedIndex.InputFilter getInputFilter() {
return virtualFile -> (virtualFile.getFileType() == XmlFileType.INSTANCE
&& virtualFile.getNameWithoutExtension().equals("di"));
}
@Override
public boolean dependsOnFileContent() {
return true;
}
@Override
public int getVersion() {
return 1;
}
}