Skip to content

[4.2.0-develop] Bug-663: Fixed NullPointerException for PluginReferenceProvider #801

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

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
@@ -1,4 +1,4 @@
/**
/*
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
Expand All @@ -16,49 +16,83 @@
import com.intellij.psi.xml.XmlTag;
import com.intellij.util.ProcessingContext;
import com.magento.idea.magento2plugin.indexes.PluginIndex;
import com.magento.idea.magento2plugin.magento.files.ModuleDiXml;
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 {

@SuppressWarnings({
"PMD.CognitiveComplexity",
"PMD.CyclomaticComplexity",
"PMD.NPathComplexity"
})
@Override
public @NotNull PsiReference[] getReferencesByElement(
@NotNull final PsiElement element,
@NotNull final ProcessingContext context
final @NotNull PsiElement element,
final @NotNull ProcessingContext context
) {
final List<PsiReference> psiReferences = new ArrayList<>();
final Project project = element.getProject();
final List<PsiElement> psiElements = new ArrayList<>();
if (!(element.getParent() instanceof XmlAttribute)
|| !ModuleDiXml.NAME_ATTR.equals(((XmlAttribute) element.getParent()).getName())
|| !(element.getParent().getParent() instanceof XmlTag)
|| !ModuleDiXml.PLUGIN_TAG_NAME.equals(
((XmlTag) element.getParent().getParent()).getName())
) {
return PsiReference.EMPTY_ARRAY;
}

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();

if (originalTypeTag == null || !ModuleDiXml.TYPE_TAG.equals(originalTypeTag.getName())) {
return PsiReference.EMPTY_ARRAY;
}
final XmlAttribute originalPluginNameAttr = originalPluginTag.getAttribute("name");
final XmlAttribute originalTypeNameAttr = originalTypeTag.getAttribute("name");

if (originalPluginNameAttr == null || originalTypeNameAttr == null) {
return PsiReference.EMPTY_ARRAY;
}
final String originalPluginName = originalPluginNameAttr.getValue();
final String originalTypeName = originalTypeNameAttr.getValue();

if (originalPluginName == null || originalTypeName == null) {
return PsiReference.EMPTY_ARRAY;
}
final Project project = element.getProject();

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

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)) {

if (pluginNameAttribute != null
&& pluginNameAttribute.getValue() != null
&& originalPluginName.equals(pluginNameAttribute.getValue())) {
psiElements.add(pluginNameAttribute.getValueElement());
}
}
}
final List<PsiReference> psiReferences = new ArrayList<>();

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));
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/**
/*
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
Expand All @@ -12,6 +12,7 @@
import com.magento.idea.magento2plugin.magento.files.MftfActionGroup;
import com.magento.idea.magento2plugin.magento.files.MftfTest;
import com.magento.idea.magento2plugin.magento.files.ModuleDbSchemaXml;
import com.magento.idea.magento2plugin.magento.files.ModuleDiXml;
import com.magento.idea.magento2plugin.magento.files.ModuleMenuXml;
import com.magento.idea.magento2plugin.magento.files.UiComponentXml;
// CHECKSTYLE IGNORE check FOR NEXT 5 LINES
Expand Down Expand Up @@ -387,7 +388,7 @@ public void registerReferenceProviders(@NotNull PsiReferenceRegistrar registrar)
XmlPatterns.xmlAttribute().withName("disabled")
)
)
).inFile(xmlFile().withName(string().endsWith("di.xml"))),
).inFile(xmlFile().withName(string().matches(ModuleDiXml.FILE_NAME))),
new PluginReferenceProvider()
);

Expand Down