How can I add a reference to PsiMethod or find usages ? Follow
Answered
Hi,
I am developing a Intellij Plugin, and I wanna to add a reference from execMethod("hello") to method `hello` so that I can use ctrl-click to hello.
How can I achieve this ?
private void hello(String name) {
System.out.println("hello, " + name);
}
public static void main(String[] args) {
execMethod("hello");
}
I use this, but it can't work.
registrar.registerReferenceProvider(PlatformPatterns.psiElement(PsiMethod.class),
new PsiReferenceProvider() {
@NotNull
@Override
public PsiReference[] getReferencesByElement(
@NotNull PsiElement element, @NotNull ProcessingContext context) {
log.info("element: {}", element);
if (!(element instanceof PsiMethod)) {
return PsiReference.EMPTY_ARRAY;
}
PsiMethod psiMethod = (PsiMethod) element;
String name = psiMethod.getName();
log.info("name: {}", name);
return PsiReference.EMPTY_ARRAY;
}
});
Thanks!
Please sign in to leave a comment.
AFAIU you want on install a reference from the parameter String literal "hello" resolving to the method declaration "hello()"? then the pattern to install reference must be installed on PsiLiteral as method parameter. See com.intellij.psi.impl.source.resolve.reference.impl.JavaReflectionReferenceContributor or others in IJ sources as reference.
Thank you Yann Cebron, I've found the way
Register this to open auto-complete popup.