From 23810609ee8af84e07740d26b875a2d02e621a9f Mon Sep 17 00:00:00 2001 From: bohdan-harniuk Date: Mon, 29 Nov 2021 22:04:26 +0100 Subject: [PATCH] 666: Fixed stub ids not found for key in index --- .../provider/PhpClassReferenceProvider.java | 46 +++++++++++++++---- 1 file changed, 37 insertions(+), 9 deletions(-) diff --git a/src/com/magento/idea/magento2plugin/reference/provider/PhpClassReferenceProvider.java b/src/com/magento/idea/magento2plugin/reference/provider/PhpClassReferenceProvider.java index 39618e545..40ff460d9 100644 --- a/src/com/magento/idea/magento2plugin/reference/provider/PhpClassReferenceProvider.java +++ b/src/com/magento/idea/magento2plugin/reference/provider/PhpClassReferenceProvider.java @@ -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; @@ -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); @@ -49,15 +50,21 @@ public PsiReference[] getReferencesByElement( final StringBuilder namespace = new StringBuilder(); String namespacePart; final List 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 references = hasNamespaceInIndex( + namespaceId, + element.getProject() + ) ? phpIndex.getNamespacesByName(namespaceId) : new ArrayList<>(); - namespace.append("\\");//NOPMD - namespace.append(namespacePart);//NOPMD - final Collection 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), @@ -70,6 +77,7 @@ public PsiReference[] getReferencesByElement( final String className = classFQN.substring(classFQN.lastIndexOf(92) + 1); final Collection classes = phpIndex.getAnyByFQN(classFQN); + if (!classes.isEmpty()) { final TextRange range = new TextRange( origValue.lastIndexOf(92) + 1, @@ -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 keys = StubIndex.getInstance().getAllKeys( + PhpNamespaceIndex.KEY, + project + ); + + return keys.contains(namespaceIdentifier); + } }