For example, in my custom language a have defined a class Dummy and want to reference it
from an Java class (and in the future also from other languages) like this.
package foo;
import bar.Dummy;
public class Main {
public static void main(String args) {
final var dummy = new Dummy();
}
}
But the getReferencesByElement method of my PsiReferenceProvider is not invoked.
The string element you’re trying to resolve in the sample is a PsiLiteralExpression, which appears to be one of the classes that getReferencesByElement() can accept.
Although the exact rules aren’t clear, an element seems to be passed to getReferencesByElement() if it (1) inherits from certain PSI classes or (2) overrides PsiElement.getReferences() as shown below. (This is the technique I used to enable reference resolution for elements in my own custom language.)
@Override
public @NotNull PsiReference[] getReferences() {
return ReferenceProvidersRegistry.getReferencesFromProviders(this);
}
With that in mind, would it be possible to create a custom PSI element class for a dummy call, then have your injection recognize that element type and add the above overrides?