Find Usages of a StringLiteralExpression

Hey, I have have a StringLiteralExpression inside the RoutePdfNameReference resolve method. Now I need to find out if this string is used elsewhere in the project and display its usages.

What is the best way to search for all occurrences of a specific StringLiteralExpression across the entire project ?

I tried Collection<PsiReference> classUsages = ReferencesSearch.search(yourPsiMethod).findAll(); but it did not work.

import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.TextRange;
import com.intellij.psi.*;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

public class RoutePdfNameReference extends PsiReferenceBase<PsiElement> { 
    private Project project;

    public RoutePdfNameReference(@NotNull PsiElement element, TextRange rangeInElement) {
        super(element, rangeInElement);
        this.project = element.getProject();
    }

    @Override
    public @Nullable PsiElement resolve() {
        String s = myElement.getText(); 
        return null;
    }

    @Override
    public Object @NotNull [] getVariants() {
        return EMPTY_ARRAY;
    }
}

import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.TextRange;
import com.intellij.patterns.PlatformPatterns;
import com.intellij.psi.*;
import com.intellij.util.ProcessingContext;
import com.jetbrains.php.lang.psi.elements.MethodReference;
import com.jetbrains.php.lang.psi.elements.StringLiteralExpression;
import org.jetbrains.annotations.NotNull;

public class RoutePdfReferenceContributor extends PsiReferenceContributor {
    @Override
    public void registerReferenceProviders(@NotNull PsiReferenceRegistrar psiReferenceRegistrar) {
        psiReferenceRegistrar.registerReferenceProvider(
            PlatformPatterns.psiElement(StringLiteralExpression.class),
            new PsiReferenceProvider() {
                @Override
                public PsiReference @NotNull [] getReferencesByElement(@NotNull PsiElement psiElement, @NotNull ProcessingContext processingContext) {
                    Project project = psiElement.getProject();

                    if (!(psiElement instanceof StringLiteralExpression stringLiteralExpression)) {
                        return PsiReference.EMPTY_ARRAY;
                    }

                    MethodReference methodReference = MethodUtils.resolveMethodReference(psiElement, 5);

                    if (Utils.shouldComplete(methodReference, psiElement, project)) {
                        return new PsiReference[]{
                            new RoutePdfNameReference(
                                stringLiteralExpression,
                                new TextRange(
                                    PsiElementUtils.getStartOffset(stringLiteralExpression),
                                    PsiElementUtils.getEndOffset(stringLiteralExpression)
                                )
                            )
                        };
                    }
                    return PsiReference.EMPTY_ARRAY;
                }
            }
        );
    }
}

I think the simplest possible solution for you would be to resolve() references to a class inheritor of com.intellij.psi.impl.FakePsiElement

Example of such use is com.intellij.psi.impl.beanProperties.BeanPropertyElement in Java support. intellij-community/java/java-impl/src/com/intellij/psi/impl/beanProperties/BeanPropertyElement.java at master · JetBrains/intellij-community · GitHub

Looking for usages of String is not really possible the way you wish. So instead:

  1. Resolve reference to a fake element
  2. Make sure equals and hashCode implemented for it so similar elements in different files are equal
  3. Find usages of this fake element to get places

FYI it is not the best possible solution as we have better Experimental API nowadays, but probably the simplest possible way for you.

The right API is Symbol.