|
2 | 2 | * Copyright © Magento, Inc. All rights reserved.
|
3 | 3 | * See COPYING.txt for license details.
|
4 | 4 | */
|
| 5 | + |
5 | 6 | package com.magento.idea.magento2plugin.indexes;
|
6 | 7 |
|
7 | 8 | import com.intellij.openapi.project.Project;
|
|
13 | 14 | import com.intellij.psi.xml.XmlFile;
|
14 | 15 | import com.intellij.util.indexing.FileBasedIndex;
|
15 | 16 | import com.magento.idea.magento2plugin.stubs.indexes.EventNameIndex;
|
| 17 | +import com.magento.idea.magento2plugin.stubs.indexes.EventObserverIndex; |
16 | 18 | import com.magento.idea.magento2plugin.util.xml.XmlPsiTreeUtil;
|
17 |
| - |
18 | 19 | import java.util.ArrayList;
|
19 | 20 | import java.util.Collection;
|
20 | 21 |
|
21 | 22 | public class EventIndex {
|
22 | 23 |
|
23 |
| - private static EventIndex INSTANCE; |
24 |
| - |
25 |
| - private Project project; |
26 |
| - |
27 |
| - private EventIndex() { |
28 |
| - } |
29 |
| - |
30 |
| - public static EventIndex getInstance(final Project project) { |
31 |
| - if (null == INSTANCE) { |
32 |
| - INSTANCE = new EventIndex(); |
33 |
| - } |
34 |
| - INSTANCE.project = project; |
| 24 | + private final Project project; |
35 | 25 |
|
36 |
| - return INSTANCE; |
| 26 | + /** |
| 27 | + * Constructor. |
| 28 | + */ |
| 29 | + public EventIndex(final Project project) { |
| 30 | + this.project = project; |
37 | 31 | }
|
38 | 32 |
|
39 |
| - public Collection<PsiElement> getEventElements(final String name, final GlobalSearchScope scope) { |
40 |
| - Collection<PsiElement> result = new ArrayList<>(); |
| 33 | + /** |
| 34 | + * Gets event elements by event name. |
| 35 | + */ |
| 36 | + public Collection<PsiElement> getEventElements( |
| 37 | + final String name, |
| 38 | + final GlobalSearchScope scope |
| 39 | + ) { |
| 40 | + final Collection<PsiElement> result = new ArrayList<>(); |
41 | 41 |
|
42 |
| - Collection<VirtualFile> virtualFiles = |
| 42 | + final Collection<VirtualFile> virtualFiles = |
43 | 43 | FileBasedIndex.getInstance().getContainingFiles(EventNameIndex.KEY, name, scope);
|
44 | 44 |
|
45 |
| - for (VirtualFile virtualFile : virtualFiles) { |
46 |
| - XmlFile xmlFile = (XmlFile) PsiManager.getInstance(project).findFile(virtualFile); |
47 |
| - Collection<XmlAttributeValue> valueElements = XmlPsiTreeUtil |
| 45 | + for (final VirtualFile virtualFile : virtualFiles) { |
| 46 | + final XmlFile xmlFile = (XmlFile) PsiManager.getInstance(project).findFile(virtualFile); |
| 47 | + final Collection<XmlAttributeValue> valueElements = XmlPsiTreeUtil |
48 | 48 | .findAttributeValueElements(xmlFile, "event", "name", name);
|
49 | 49 | result.addAll(valueElements);
|
50 | 50 | }
|
51 | 51 | return result;
|
52 | 52 | }
|
| 53 | + |
| 54 | + /** |
| 55 | + * Gets observers by event-observer name combination. |
| 56 | + */ |
| 57 | + public Collection<PsiElement> getObservers( |
| 58 | + final String eventName, |
| 59 | + final String observerName, |
| 60 | + final GlobalSearchScope scope |
| 61 | + ) { |
| 62 | + final Collection<PsiElement> result = new ArrayList<>(); |
| 63 | + final Collection<VirtualFile> virtualFiles |
| 64 | + = FileBasedIndex.getInstance().getContainingFiles( |
| 65 | + EventObserverIndex.KEY, eventName, scope |
| 66 | + ); |
| 67 | + |
| 68 | + for (final VirtualFile virtualFile: virtualFiles) { |
| 69 | + final XmlFile eventsXmlFile |
| 70 | + = (XmlFile) PsiManager.getInstance(project).findFile(virtualFile); |
| 71 | + if (eventsXmlFile != null) { |
| 72 | + result.addAll( |
| 73 | + XmlPsiTreeUtil.findObserverTags(eventsXmlFile, eventName, observerName) |
| 74 | + ); |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + return result; |
| 79 | + } |
53 | 80 | }
|
0 commit comments