Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added XML reference for disabled plugins #373

Merged
merged 4 commits into from
Nov 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

package com.magento.idea.magento2plugin.reference.provider;

import com.intellij.ide.highlighter.XmlFileType;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.TextRange;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiReference;
import com.intellij.psi.PsiReferenceProvider;
import com.intellij.psi.search.GlobalSearchScope;
import com.intellij.psi.xml.XmlAttribute;
import com.intellij.psi.xml.XmlTag;
import com.intellij.util.ProcessingContext;
import com.magento.idea.magento2plugin.indexes.PluginIndex;
import com.magento.idea.magento2plugin.reference.xml.PolyVariantReferenceBase;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import org.jetbrains.annotations.NotNull;

public class PluginReferenceProvider extends PsiReferenceProvider {
@Override
public @NotNull PsiReference[] getReferencesByElement(
@NotNull final PsiElement element,
@NotNull final ProcessingContext context
) {
final List<PsiReference> psiReferences = new ArrayList<>();
final Project project = element.getProject();
final List<PsiElement> psiElements = new ArrayList<>();

final XmlTag originalPluginTag = (XmlTag) element.getParent().getParent();
final XmlTag originalTypeTag = originalPluginTag.getParentTag();
final String originalPluginName = originalPluginTag.getAttribute("name").getValue();
final String originalTypeName = originalTypeTag.getAttribute("name").getValue();

final Collection<PsiElement> types = PluginIndex.getInstance(project).getPluginElements(
originalTypeName,
GlobalSearchScope.getScopeRestrictedByFileTypes(
GlobalSearchScope.allScope(project), XmlFileType.INSTANCE
)
);

for (final PsiElement type: types) {
final XmlTag typeTag = (XmlTag) type.getParent().getParent();
final XmlTag[] pluginTags = typeTag.findSubTags("plugin");
for (final XmlTag pluginTag: pluginTags) {
final XmlAttribute pluginNameAttribute = pluginTag.getAttribute("name");
if (pluginNameAttribute.getValue().equals(originalPluginName)) {
psiElements.add(pluginNameAttribute.getValueElement());
}
}
}

if (!psiElements.isEmpty()) {
final int startIndex = element.getText().indexOf(originalPluginName);
final int endIndex = startIndex + originalPluginName.length();
final TextRange range = new TextRange(startIndex, endIndex);
psiReferences.add(new PolyVariantReferenceBase(element, range, psiElements));
}

return psiReferences.toArray(new PsiReference[0]);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,18 @@ public void registerReferenceProviders(@NotNull PsiReferenceRegistrar registrar)
new TableColumnNamesReferenceProvider()
);

// <plugin name="pluginName" disabled="true" /> in di.xml
registrar.registerReferenceProvider(
XmlPatterns.xmlAttributeValue().withParent(
XmlPatterns.xmlAttribute().withName("name").withParent(
XmlPatterns.xmlTag().withName("plugin").withChild(
XmlPatterns.xmlAttribute().withName("disabled")
)
)
).inFile(xmlFile().withName(string().endsWith("di.xml"))),
new PluginReferenceProvider()
);

registerReferenceForDifferentNesting(registrar);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0"?>
<!--
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="Magento\Theme\Block\Html\Topmenu">
<plugin name="catalogTopmenu<caret>" disabled="true"/>
</type>
</config>
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

package com.magento.idea.magento2plugin.reference.xml;

import com.magento.idea.magento2plugin.magento.files.ModuleDiXml;

public class DisabledPluginReferenceRegistrarTest extends ReferenceXmlFixtureTestCase {

/**
* Tests for disabled plugin name reference to original definition.
*/
public void testDisabledPluginNameMustHaveReference() {
myFixture.configureByFile(this.getFixturePath(ModuleDiXml.FILE_NAME));

assertHasReferenceToXmlAttributeValue("catalogTopmenu");
}
}