forked from magento/magento2-phpstorm-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEventIndex.java
80 lines (69 loc) · 2.64 KB
/
EventIndex.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
/**
* 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.PsiElement;
import com.intellij.psi.PsiManager;
import com.intellij.psi.search.GlobalSearchScope;
import com.intellij.psi.xml.XmlAttributeValue;
import com.intellij.psi.xml.XmlFile;
import com.intellij.util.indexing.FileBasedIndex;
import com.magento.idea.magento2plugin.stubs.indexes.EventNameIndex;
import com.magento.idea.magento2plugin.stubs.indexes.EventObserverIndex;
import com.magento.idea.magento2plugin.util.xml.XmlPsiTreeUtil;
import java.util.ArrayList;
import java.util.Collection;
public class EventIndex {
private final Project project;
/**
* Constructor.
*/
public EventIndex(final Project project) {
this.project = project;
}
/**
* Gets event elements by event name.
*/
public Collection<PsiElement> getEventElements(
final String name,
final GlobalSearchScope scope
) {
final Collection<PsiElement> result = new ArrayList<>();
final Collection<VirtualFile> virtualFiles =
FileBasedIndex.getInstance().getContainingFiles(EventNameIndex.KEY, name, scope);
for (final VirtualFile virtualFile : virtualFiles) {
final XmlFile xmlFile = (XmlFile) PsiManager.getInstance(project).findFile(virtualFile);
final Collection<XmlAttributeValue> valueElements = XmlPsiTreeUtil
.findAttributeValueElements(xmlFile, "event", "name", name);
result.addAll(valueElements);
}
return result;
}
/**
* Gets observers by event-observer name combination.
*/
public Collection<PsiElement> getObservers(
final String eventName,
final String observerName,
final GlobalSearchScope scope
) {
final Collection<PsiElement> result = new ArrayList<>();
final Collection<VirtualFile> virtualFiles
= FileBasedIndex.getInstance().getContainingFiles(
EventObserverIndex.KEY, eventName, scope
);
for (final VirtualFile virtualFile: virtualFiles) {
final XmlFile eventsXmlFile
= (XmlFile) PsiManager.getInstance(project).findFile(virtualFile);
if (eventsXmlFile != null) {
result.addAll(
XmlPsiTreeUtil.findObserverTags(eventsXmlFile, eventName, observerName)
);
}
}
return result;
}
}