PsiElement#getReferences() returns null?

Answered

Hi everyone,

I'm writing a really simple, "hello world" level plugin; it has a single action (on the right click popup menu) with the following code:

@Override
public void actionPerformed(AnActionEvent e) {

PsiElement psiElement = e.getData(CommonDataKeys.PSI_ELEMENT);
PsiReference reference = psiElement.getReference();

}

The IDEA instance running the plugin has a Java project loaded up; the right click happened on a method call, so there should be references. In fact, "jump to declaration" works perfectly; so do e.g. psiElement.getClass(), so it looks like Java code was parsed nicely.

Yet, getReference() returns null.

After a closer look (debugger), the "null" value is returned immediately by the method in PsiElementBase, which is apparently never overridden by PsiMethod, the class of psiElement.

Does anyone have some advice on what I'm doing wrong & how to get references to PsiElements the right way?

Thanks!!!

(... it's IDEA 2016.2.1, Community Edition.)

 

3 comments
Comment actions Permalink

... solution: e.getData(...) returns not the method call PsiElement but the declaration it's referring to, which doesn't have references to anything else (since it is itself already).

For a working (but probably far from perfect) version of what I've been trying to accomplish, here is an example:

e.getData(CommonDataKeys.PSI_ELEMENT).getContainingFile().findElementAt(e.getData(CommonDataKeys.CARET).getOffset()).getContext().getReference()

0
Comment actions Permalink

A more orthodox way would be to use PSI_FILE data key to get PsiFile, and after that call psiFile.findReferenceAt(caretOffset).

1
Comment actions Permalink

... which is way simpler; thanks Peter for the actually reasonable solution!

0

Please sign in to leave a comment.