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