Skip to content

Commit 20d5e93

Browse files
authored
Merge pull request #814 from bohdan-harniuk/bugfix/666-stub-ids-not-found-for-key-in-index
[Bug] 666: Fixed stub ids not found for key in index
2 parents 7fd808b + 2381060 commit 20d5e93

File tree

1 file changed

+37
-9
lines changed

1 file changed

+37
-9
lines changed

src/com/magento/idea/magento2plugin/reference/provider/PhpClassReferenceProvider.java

Lines changed: 37 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,17 @@
55

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

8+
import com.intellij.openapi.project.Project;
89
import com.intellij.openapi.util.TextRange;
910
import com.intellij.psi.PsiElement;
1011
import com.intellij.psi.PsiReference;
1112
import com.intellij.psi.PsiReferenceProvider;
13+
import com.intellij.psi.stubs.StubIndex;
1214
import com.intellij.util.ProcessingContext;
1315
import com.jetbrains.php.PhpIndex;
1416
import com.jetbrains.php.lang.psi.elements.PhpClass;
1517
import com.jetbrains.php.lang.psi.elements.PhpNamespace;
18+
import com.jetbrains.php.lang.psi.stubs.indexes.PhpNamespaceIndex;
1619
import com.magento.idea.magento2plugin.reference.xml.PolyVariantReferenceBase;
1720
import com.magento.idea.magento2plugin.util.RegExUtil;
1821
import java.util.ArrayList;
@@ -26,13 +29,11 @@
2629
@SuppressWarnings({"PMD.AvoidInstantiatingObjectsInLoops"})
2730
public class PhpClassReferenceProvider extends PsiReferenceProvider {
2831

29-
@NotNull
3032
@Override
31-
public PsiReference[] getReferencesByElement(
33+
public @NotNull PsiReference[] getReferencesByElement(
3234
final @NotNull PsiElement element,
3335
final @NotNull ProcessingContext context
3436
) {
35-
3637
final String origValue = element.getText();
3738

3839
final Pattern pattern = Pattern.compile(RegExUtil.PhpRegex.FQN);
@@ -49,15 +50,21 @@ public PsiReference[] getReferencesByElement(
4950
final StringBuilder namespace = new StringBuilder();
5051
String namespacePart;
5152
final List<PsiReference> psiReferences = new ArrayList<>();
53+
5254
for (int i = 0; i < fqnParts.length - 1; i++) {
5355
namespacePart = fqnParts[i];
56+
namespace.append('\\');
57+
namespace.append(namespacePart);
58+
59+
final String namespaceId = namespace
60+
.toString()
61+
.toLowerCase(new Locale("en","EN"));
62+
63+
final Collection<PhpNamespace> references = hasNamespaceInIndex(
64+
namespaceId,
65+
element.getProject()
66+
) ? phpIndex.getNamespacesByName(namespaceId) : new ArrayList<>();
5467

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

7178
final String className = classFQN.substring(classFQN.lastIndexOf(92) + 1);
7279
final Collection<PhpClass> classes = phpIndex.getAnyByFQN(classFQN);
80+
7381
if (!classes.isEmpty()) {
7482
final TextRange range = new TextRange(
7583
origValue.lastIndexOf(92) + 1,
@@ -80,4 +88,24 @@ public PsiReference[] getReferencesByElement(
8088

8189
return psiReferences.toArray(new PsiReference[0]);
8290
}
91+
92+
/**
93+
* Check if php namespace index has specified identifier.
94+
*
95+
* @param namespaceIdentifier String
96+
* @param project Project
97+
*
98+
* @return boolean
99+
*/
100+
private boolean hasNamespaceInIndex(
101+
final @NotNull String namespaceIdentifier,
102+
final @NotNull Project project
103+
) {
104+
final Collection<String> keys = StubIndex.getInstance().getAllKeys(
105+
PhpNamespaceIndex.KEY,
106+
project
107+
);
108+
109+
return keys.contains(namespaceIdentifier);
110+
}
83111
}

0 commit comments

Comments
 (0)