How do you get a handle to the method the cursor is over?

So I want to get the return type of the method my cursor is in/over. That's the goal. I thought if I could get a handle to a PsiMethod I could invoke getReturnType and see what the return type is. This is exactly (a part of ) the information you get when you control+Q over an element in the editor.

I want to first find out what kind of thing the cursor is over. I can use psiFile.findElementAt and it will return the PsiElement, but unfortunately, the PsiElement is merely a PsiIdentifier and not a PsiMethod.

Some ways up in the containment hierarchy, according to the PsiViewer, a parent of this PsiIdentifer I had returned to me is a PsiMethodCallExpression. If I clambered up and got to this object, I might be able to invoke resolveMethod and get my hands on a PsiMethod.

My question is

is there an easier way to get what method (return type is actually what I am after) the cursor is under?

Is walking up the containment hierarchy and checking the type of thing you encounter along the way a typical and necessary way of programming with the Psi?

I am green with envy towards control-Q's ability to quickly get the information I covet, and wonder if there is source anyone knows that demonstrates this kind of thing?

3 comments

Hello softwarevisualization,

So I want to get the return type of the method my cursor is in/over.
That's the goal. I thought if I could get a handle to a PsiMethod I
could invoke getReturnType and see what the return type is. This is
exactly (a part of ) the information you get when you control+Q over
an element in the editor.

I want to first find out what kind of thing the cursor is over. I can
use psiFile.findElementAt and it will return the PsiElement, but
unfortunately, the PsiElement is merely a PsiIdentifier and not a
PsiMethod.

Some ways up in the containment hierarchy, according to the PsiViewer,
a parent of this PsiIdentifer I had returned to my is a
PsiMethodCallExpression. If I clambered up and got to this object, I
might be able to invoke resolveMethod and get my hands on a PsiMethod.

My question is

is there an easier way to get what method (return type is actually
what I am after) the cursor is under?

Is walking up the containment hierarchy and checking the type of thing
you encounter along the way a typical and necessary way of
programming with the Psi?

I am green with envy towards control-Q's ability to quickly get the
information I covet, and wonder if there is source anyone knows that
demonstrates this kind of thing?


My plugin experience is limited, but I think it's very common to query
"starting from current PSI element, find an ancestor of type xxx".
See the various static utility methods from the PsiTreeUtil class.

In your example PsiMethodClassExpression can contain a subtree of arbitrary
complexity (for example anonymous classes passed as arguments). So it's quite
"natural" that IDEA requires you to explicitly search for a parent node of
some type.

I'm not sure if you need to use resolveMethod(). Perhaps calling getType()
on the PsiMethodCallExpression provides all you need?

-tt


0

I think this is right. A lot of this programming is going to be an exercise in tree-walking.

0

have you tried this?

int caretOffset = editor.getCaretModel().getOffset();
psiFile.findElementAt(caretOffset);

0

Please sign in to leave a comment.