Access to method call under caret instead of PsiMethod

Hi,

When I click on a method call (having my custom action triggered), like myMethod(5), then in the action I invoke I can get to the PsiMethod:myMethod using e.getData(LangDataKeys.PSI_ELEMENT). However, I would like to be able to access PsiMethodCallExpression instead. I can't find a way to do that. How can I access the call?

4 comments

Editor editor = e.getData(DataKeys.EDITOR);
PsiFile psiFile = e.getData(DataKeys.PSI_FILE);
int offset = editor.getCaretModel().getOffset();
PsiMethodCallExpression mce = PsiTreeUtil.getParentOfType(psiFile.findElementAt(offset), PsiMethodCallExpression.class);

0

Thanks for the answer. However, I'd like to know where the difference comes from. Can you explain it to me?

0

getData(LangDataKeys.PSI_ELEMENT) resolves the reference under the caret. The code I've posted simply takes the element at caret offset and does not resolve any references.

0

Please sign in to leave a comment.