Skip to content

Commit 553e14e

Browse files
authored
Merge pull request #373 from drpayyne/issue-146
Added XML reference for disabled plugins
2 parents b7d2dea + 8bf831c commit 553e14e

File tree

4 files changed

+112
-0
lines changed

4 files changed

+112
-0
lines changed
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/**
2+
* Copyright © Magento, Inc. All rights reserved.
3+
* See COPYING.txt for license details.
4+
*/
5+
6+
package com.magento.idea.magento2plugin.reference.provider;
7+
8+
import com.intellij.ide.highlighter.XmlFileType;
9+
import com.intellij.openapi.project.Project;
10+
import com.intellij.openapi.util.TextRange;
11+
import com.intellij.psi.PsiElement;
12+
import com.intellij.psi.PsiReference;
13+
import com.intellij.psi.PsiReferenceProvider;
14+
import com.intellij.psi.search.GlobalSearchScope;
15+
import com.intellij.psi.xml.XmlAttribute;
16+
import com.intellij.psi.xml.XmlTag;
17+
import com.intellij.util.ProcessingContext;
18+
import com.magento.idea.magento2plugin.indexes.PluginIndex;
19+
import com.magento.idea.magento2plugin.reference.xml.PolyVariantReferenceBase;
20+
import java.util.ArrayList;
21+
import java.util.Collection;
22+
import java.util.List;
23+
import org.jetbrains.annotations.NotNull;
24+
25+
public class PluginReferenceProvider extends PsiReferenceProvider {
26+
@Override
27+
public @NotNull PsiReference[] getReferencesByElement(
28+
@NotNull final PsiElement element,
29+
@NotNull final ProcessingContext context
30+
) {
31+
final List<PsiReference> psiReferences = new ArrayList<>();
32+
final Project project = element.getProject();
33+
final List<PsiElement> psiElements = new ArrayList<>();
34+
35+
final XmlTag originalPluginTag = (XmlTag) element.getParent().getParent();
36+
final XmlTag originalTypeTag = originalPluginTag.getParentTag();
37+
final String originalPluginName = originalPluginTag.getAttribute("name").getValue();
38+
final String originalTypeName = originalTypeTag.getAttribute("name").getValue();
39+
40+
final Collection<PsiElement> types = PluginIndex.getInstance(project).getPluginElements(
41+
originalTypeName,
42+
GlobalSearchScope.getScopeRestrictedByFileTypes(
43+
GlobalSearchScope.allScope(project), XmlFileType.INSTANCE
44+
)
45+
);
46+
47+
for (final PsiElement type: types) {
48+
final XmlTag typeTag = (XmlTag) type.getParent().getParent();
49+
final XmlTag[] pluginTags = typeTag.findSubTags("plugin");
50+
for (final XmlTag pluginTag: pluginTags) {
51+
final XmlAttribute pluginNameAttribute = pluginTag.getAttribute("name");
52+
if (pluginNameAttribute.getValue().equals(originalPluginName)) {
53+
psiElements.add(pluginNameAttribute.getValueElement());
54+
}
55+
}
56+
}
57+
58+
if (!psiElements.isEmpty()) {
59+
final int startIndex = element.getText().indexOf(originalPluginName);
60+
final int endIndex = startIndex + originalPluginName.length();
61+
final TextRange range = new TextRange(startIndex, endIndex);
62+
psiReferences.add(new PolyVariantReferenceBase(element, range, psiElements));
63+
}
64+
65+
return psiReferences.toArray(new PsiReference[0]);
66+
}
67+
}

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,18 @@ public void registerReferenceProviders(@NotNull PsiReferenceRegistrar registrar)
369369
new TableColumnNamesReferenceProvider()
370370
);
371371

372+
// <plugin name="pluginName" disabled="true" /> in di.xml
373+
registrar.registerReferenceProvider(
374+
XmlPatterns.xmlAttributeValue().withParent(
375+
XmlPatterns.xmlAttribute().withName("name").withParent(
376+
XmlPatterns.xmlTag().withName("plugin").withChild(
377+
XmlPatterns.xmlAttribute().withName("disabled")
378+
)
379+
)
380+
).inFile(xmlFile().withName(string().endsWith("di.xml"))),
381+
new PluginReferenceProvider()
382+
);
383+
372384
registerReferenceForDifferentNesting(registrar);
373385
}
374386

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?xml version="1.0"?>
2+
<!--
3+
/**
4+
* Copyright © Magento, Inc. All rights reserved.
5+
* See COPYING.txt for license details.
6+
*/
7+
-->
8+
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
9+
xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
10+
<type name="Magento\Theme\Block\Html\Topmenu">
11+
<plugin name="catalogTopmenu<caret>" disabled="true"/>
12+
</type>
13+
</config>
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/*
2+
* Copyright © Magento, Inc. All rights reserved.
3+
* See COPYING.txt for license details.
4+
*/
5+
6+
package com.magento.idea.magento2plugin.reference.xml;
7+
8+
import com.magento.idea.magento2plugin.magento.files.ModuleDiXml;
9+
10+
public class DisabledPluginReferenceRegistrarTest extends ReferenceXmlFixtureTestCase {
11+
12+
/**
13+
* Tests for disabled plugin name reference to original definition.
14+
*/
15+
public void testDisabledPluginNameMustHaveReference() {
16+
myFixture.configureByFile(this.getFixturePath(ModuleDiXml.FILE_NAME));
17+
18+
assertHasReferenceToXmlAttributeValue("catalogTopmenu");
19+
}
20+
}

0 commit comments

Comments
 (0)