Skip to content

Commit 5eca487

Browse files
committed
Added reference navigation for observer name
1 parent a1a4d30 commit 5eca487

File tree

4 files changed

+128
-11
lines changed

4 files changed

+128
-11
lines changed

src/com/magento/idea/magento2plugin/indexes/EventIndex.java

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,22 +13,19 @@
1313
import com.intellij.psi.xml.XmlFile;
1414
import com.intellij.util.indexing.FileBasedIndex;
1515
import com.magento.idea.magento2plugin.stubs.indexes.EventNameIndex;
16+
import com.magento.idea.magento2plugin.stubs.indexes.EventObserverIndex;
1617
import com.magento.idea.magento2plugin.util.xml.XmlPsiTreeUtil;
1718

1819
import java.util.ArrayList;
1920
import java.util.Collection;
2021

2122
public class EventIndex {
2223

23-
private static EventIndex INSTANCE;
24-
24+
private static EventIndex INSTANCE; //NOPMD
2525
private Project project;
2626

27-
private EventIndex() {
28-
}
29-
3027
public static EventIndex getInstance(final Project project) {
31-
if (null == INSTANCE) {
28+
if (null == INSTANCE) { //NOPMD
3229
INSTANCE = new EventIndex();
3330
}
3431
INSTANCE.project = project;
@@ -37,17 +34,40 @@ public static EventIndex getInstance(final Project project) {
3734
}
3835

3936
public Collection<PsiElement> getEventElements(final String name, final GlobalSearchScope scope) {
40-
Collection<PsiElement> result = new ArrayList<>();
37+
final Collection<PsiElement> result = new ArrayList<>();
4138

42-
Collection<VirtualFile> virtualFiles =
39+
final Collection<VirtualFile> virtualFiles =
4340
FileBasedIndex.getInstance().getContainingFiles(EventNameIndex.KEY, name, scope);
4441

45-
for (VirtualFile virtualFile : virtualFiles) {
46-
XmlFile xmlFile = (XmlFile) PsiManager.getInstance(project).findFile(virtualFile);
47-
Collection<XmlAttributeValue> valueElements = XmlPsiTreeUtil
42+
for (final VirtualFile virtualFile : virtualFiles) {
43+
final XmlFile xmlFile = (XmlFile) PsiManager.getInstance(project).findFile(virtualFile);
44+
final Collection<XmlAttributeValue> valueElements = XmlPsiTreeUtil
4845
.findAttributeValueElements(xmlFile, "event", "name", name);
4946
result.addAll(valueElements);
5047
}
5148
return result;
5249
}
50+
51+
public Collection<PsiElement> getObserver(
52+
final String eventName,
53+
final String observerName,
54+
final GlobalSearchScope scope
55+
) {
56+
final Collection<PsiElement> result = new ArrayList<>();
57+
final Collection<VirtualFile> virtualFiles = FileBasedIndex.getInstance().getContainingFiles(
58+
EventObserverIndex.KEY, eventName, scope
59+
);
60+
61+
for (final VirtualFile virtualFile: virtualFiles) {
62+
final XmlFile eventsXmlFile
63+
= (XmlFile) PsiManager.getInstance(project).findFile(virtualFile);
64+
if (eventsXmlFile != null) {
65+
result.addAll(
66+
XmlPsiTreeUtil.findObserverTag(eventsXmlFile, eventName, observerName)
67+
);
68+
}
69+
}
70+
71+
return result;
72+
}
5373
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.magento.idea.magento2plugin.reference.provider;
2+
3+
import com.intellij.psi.PsiElement;
4+
import com.intellij.psi.PsiReference;
5+
import com.intellij.psi.PsiReferenceProvider;
6+
import com.intellij.psi.impl.source.xml.XmlAttributeValueImpl;
7+
import com.intellij.psi.impl.source.xml.XmlTagImpl;
8+
import com.intellij.psi.search.GlobalSearchScope;
9+
import com.intellij.util.ProcessingContext;
10+
import com.magento.idea.magento2plugin.indexes.EventIndex;
11+
import com.magento.idea.magento2plugin.reference.xml.PolyVariantReferenceBase;
12+
import java.util.ArrayList;
13+
import java.util.Collection;
14+
import java.util.List;
15+
import org.jetbrains.annotations.NotNull;
16+
17+
public class ObserverNameReferenceProvider extends @NotNull PsiReferenceProvider {
18+
@Override
19+
public PsiReference @NotNull [] getReferencesByElement(
20+
@NotNull final PsiElement element,
21+
@NotNull final ProcessingContext context
22+
) {
23+
final List<PsiReference> psiReferences = new ArrayList<>();
24+
final XmlTagImpl eventTag = (XmlTagImpl) element.getParent().getParent().getParent();
25+
final String eventName = eventTag.getAttributeValue("name");
26+
27+
if (eventName == null) {
28+
return psiReferences.toArray(new PsiReference[0]);
29+
}
30+
31+
final String observerName = ((XmlAttributeValueImpl) element).getValue();
32+
final Collection<PsiElement> observers
33+
= EventIndex.getInstance(element.getProject()).getObserver(
34+
eventName, observerName, GlobalSearchScope.allScope(element.getProject())
35+
);
36+
37+
if (!observers.isEmpty()) {
38+
psiReferences.add(new PolyVariantReferenceBase(element, observers));
39+
}
40+
41+
return psiReferences.toArray(new PsiReference[0]);
42+
}
43+
}

src/com/magento/idea/magento2plugin/reference/xml/XmlReferenceContributor.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,16 @@ public void registerReferenceProviders(@NotNull PsiReferenceRegistrar registrar)
153153
new EventNameReferenceProvider()
154154
);
155155

156+
// <observer name="reference" />
157+
registrar.registerReferenceProvider(
158+
XmlPatterns.xmlAttributeValue().withParent(
159+
XmlPatterns.xmlAttribute().withName("name").withParent(
160+
XmlPatterns.xmlTag().withName("observer")
161+
)
162+
),
163+
new ObserverNameReferenceProvider()
164+
);
165+
156166
// <someXmlTag someAttribute="Module_Name[.*]" />
157167
registrar.registerReferenceProvider(
158168
XmlPatterns.xmlAttributeValue().withValue(string().matches(".*[A-Z][a-zA-Z0-9]+_[A-Z][a-zA-Z0-9]+.*")),

src/com/magento/idea/magento2plugin/util/xml/XmlPsiTreeUtil.java

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@
2929
"PMD.UseObjectForClearerAPI"
3030
})
3131
public class XmlPsiTreeUtil {
32+
public static final String EVENT_TAG_NAME = "event";
33+
public static final String OBSERVER_TAG_NAME = "observer";
34+
public static final String NAME_ATTRIBUTE = "name";
35+
3236
/**
3337
* Get type tag of an argument.
3438
*
@@ -47,6 +51,46 @@ public static XmlTag getTypeTagOfArgument(final XmlElement psiArgumentValueEleme
4751
return PsiTreeUtil.getParentOfType(argumentsTag, XmlTag.class);
4852
}
4953

54+
public static Collection<XmlAttributeValue> findObserverTag(
55+
final XmlFile xmlFile,
56+
final String eventName,
57+
final String observerName
58+
) {
59+
Collection<XmlAttributeValue> psiElements = new THashSet<>();
60+
final XmlTag configTag = xmlFile.getRootTag();
61+
62+
if (configTag == null) {
63+
return psiElements;
64+
}
65+
66+
/* Loop through event tags */
67+
for (XmlTag eventTag: configTag.getSubTags()) {
68+
XmlAttribute eventNameAttribute = eventTag.getAttribute(NAME_ATTRIBUTE);
69+
70+
/* Check if event tag and name matches */
71+
if (EVENT_TAG_NAME.equals(eventTag.getName())
72+
&& eventNameAttribute != null
73+
&& eventName.equals(eventNameAttribute.getValue())
74+
) {
75+
/* Loop through observer tags under matched event tag */
76+
for (XmlTag observerTag: eventTag.getSubTags()) {
77+
XmlAttribute observerNameAttribute = observerTag.getAttribute(NAME_ATTRIBUTE);
78+
79+
/* Check if observer tag and name matches */
80+
if (OBSERVER_TAG_NAME.equals(observerTag.getName())
81+
&& observerNameAttribute != null
82+
&& observerNameAttribute.getValueElement() != null
83+
&& observerName.equals(observerNameAttribute.getValue())
84+
) {
85+
psiElements.add(observerNameAttribute.getValueElement());
86+
}
87+
}
88+
}
89+
}
90+
91+
return psiElements;
92+
}
93+
5094
/**
5195
* Find attribute value elements based on the tag name.
5296
*

0 commit comments

Comments
 (0)