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

[Bug] 666: Fixed stub ids not found for key in index #814

Merged
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
Expand Up @@ -5,14 +5,17 @@

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

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.stubs.StubIndex;
import com.intellij.util.ProcessingContext;
import com.jetbrains.php.PhpIndex;
import com.jetbrains.php.lang.psi.elements.PhpClass;
import com.jetbrains.php.lang.psi.elements.PhpNamespace;
import com.jetbrains.php.lang.psi.stubs.indexes.PhpNamespaceIndex;
import com.magento.idea.magento2plugin.reference.xml.PolyVariantReferenceBase;
import com.magento.idea.magento2plugin.util.RegExUtil;
import java.util.ArrayList;
Expand All @@ -26,13 +29,11 @@
@SuppressWarnings({"PMD.AvoidInstantiatingObjectsInLoops"})
public class PhpClassReferenceProvider extends PsiReferenceProvider {

@NotNull
@Override
public PsiReference[] getReferencesByElement(
public @NotNull PsiReference[] getReferencesByElement(
final @NotNull PsiElement element,
final @NotNull ProcessingContext context
) {

final String origValue = element.getText();

final Pattern pattern = Pattern.compile(RegExUtil.PhpRegex.FQN);
Expand All @@ -49,15 +50,21 @@ public PsiReference[] getReferencesByElement(
final StringBuilder namespace = new StringBuilder();
String namespacePart;
final List<PsiReference> psiReferences = new ArrayList<>();

for (int i = 0; i < fqnParts.length - 1; i++) {
namespacePart = fqnParts[i];
namespace.append('\\');
namespace.append(namespacePart);

final String namespaceId = namespace
.toString()
.toLowerCase(new Locale("en","EN"));

final Collection<PhpNamespace> references = hasNamespaceInIndex(
namespaceId,
element.getProject()
) ? phpIndex.getNamespacesByName(namespaceId) : new ArrayList<>();

namespace.append("\\");//NOPMD
namespace.append(namespacePart);//NOPMD
final Collection<PhpNamespace> references =
phpIndex.getNamespacesByName(namespace.toString().toLowerCase(
new Locale("en","EN"))
);
if (!references.isEmpty()) {
final TextRange range = new TextRange(
origValue.indexOf(classFQN) + namespace.toString().lastIndexOf(92),
Expand All @@ -70,6 +77,7 @@ public PsiReference[] getReferencesByElement(

final String className = classFQN.substring(classFQN.lastIndexOf(92) + 1);
final Collection<PhpClass> classes = phpIndex.getAnyByFQN(classFQN);

if (!classes.isEmpty()) {
final TextRange range = new TextRange(
origValue.lastIndexOf(92) + 1,
Expand All @@ -80,4 +88,24 @@ public PsiReference[] getReferencesByElement(

return psiReferences.toArray(new PsiReference[0]);
}

/**
* Check if php namespace index has specified identifier.
*
* @param namespaceIdentifier String
* @param project Project
*
* @return boolean
*/
private boolean hasNamespaceInIndex(
final @NotNull String namespaceIdentifier,
final @NotNull Project project
) {
final Collection<String> keys = StubIndex.getInstance().getAllKeys(
PhpNamespaceIndex.KEY,
project
);

return keys.contains(namespaceIdentifier);
}
}