Finding the "owning" method of a selection

Hi,

I'm new to all this so this may be a pretty simple task - I cant seem to do it though!

I need to find the immediate parent method of a selected bit of text.

For example: Object obj = myOuterMethod(myInnerMethod(var1, var2, var3));

If the caret was on var3 (for example) I would like to know that myInnerMethod is the immediate parent method - is there an easy way to do this?

thanks very much,

Sean

Edited by: Sean Landsman on Oct 26, 2008 12:58 PM

6 comments
Comment actions Permalink

Hello Sean,

I'm new to all this so this may be a pretty simple task - I cant seem
to do it though!

I need to find the immediate parent method of a selected bit of text.

For example: Object obj = myOuterMethod(myInnerMethod(var1, var2,
var3));

If the caret was on var3 (for example) I would like to know that
myInnerMethod is the immediate parent method - is there an easy way to
do this?


I assume this happens in an Action.

public void actionPerformed(AnActionEvent e) {
PsiElement element = e.getData(DataKeys.PSI_ELEMENT);
PsiMethod method = PsiTreeUtil.getParentOfType(element, PsiMethod.class);
}

--
Dmitry Jemerov
Development Lead
JetBrains, Inc.
http://www.jetbrains.com/
"Develop with Pleasure!"


0
Comment actions Permalink

Yep, all in an Action - thats brilliant - thanks for your help Dmitry!

0
Comment actions Permalink

Hi,

Sorry, I assumed that it worked but unfortunately its not exactly what I needed.

The above code will return the overall method being called.

For example, given the code below

public void testMethod() {
Object obj = myOuterMethod(myInnerMethod(var1, var2, var3));
}

I will get testMethod back, which is close, but what I'm really after is the immediate context - myInnerMethod.

Any other ideas? I'm still trying to work it out but help would be appreciated :)

thanks

Edited by: Sean Landsman on Oct 27, 2008 1:03 PM

0
Comment actions Permalink

Hello Sean,

PsiCall call = PsiTreeUtil.getParentOfType(element, PsiCall.class);
if (call != null) innerMethod = call.resolveMethod();

Hi,

Sorry, I assumed that it worked but unfortunately its not exactly what
I needed.

The above code will return the overall method being called.

For example, given the code below

public void testMethod() {
Object obj = myOuterMethod(myInnerMethod(var1, var2, var3));
}
I will get testMethod back, which is close, but what I'm really after
is the immediate context - my InnerMethod.

Any other ideas? I'm still trying to work it out but help would be
appreciated :)

thanks

--
Dmitry Jemerov
Development Lead
JetBrains, Inc.
http://www.jetbrains.com/
"Develop with Pleasure!"


0
Comment actions Permalink

Hi Dmitry,

Thanks for answering, but I'm afraid that doesn't help either - the call variable is always null.

I've tried putting the caret at the beginning and end of a variable, as well as selecting the whole variable but still no luck.

I'll keep looking - thanks for helping!

Sean

0
Comment actions Permalink

Hello Sean,

Thanks for answering, but I'm afraid that doesn't help either - the
call variable is always null.

I've tried putting the caret at the beginning and end of a variable,
as well as selecting the whole variable but still no luck.

I'll keep looking - thanks for helping!


You don't have to keep asking us in order to find out this information. The
PSI viewer plugin will let you explore the PSI structure by yourself and
see what elements are available in your particular code fragment.
http://plugins.intellij.net/plugin/?id=227

--
Dmitry Jemerov
Development Lead
JetBrains, Inc.
http://www.jetbrains.com/
"Develop with Pleasure!"


0

Please sign in to leave a comment.