How to get PsiElement of focused line?

Answered

I am developing a plugin to do some thing based on the selected text in the editor. 
In order to achieve my goal, I need to know the PsiElement type of the focused line. 
For example: 
if(a != null) 

System.out.print(a); 

if I selected 'a != null', the focused line is a PsiIfStatement. 
I know how to get the PsiElement of the selected text. But I don't how to get the PsiElement of that line. 
Any suggestion?

0
4 comments

I'm not sure I understand your question, but there is no such thing as a "PsiElement for the current line". Could you please explain why you need it/what you want to achieve?

0

I want to add some code to the file.

If the selected text is in a if statement, the code is add before the focus line.

If the selected text is the parameter of a method, the code should be add after the Lbrace.

0

Hi Yann. Thanks for your reply.

I found I can use PsiTreeUtil.findCommonParent() to get parent of the selected text. Then I check the type the common parent.

0

Please sign in to leave a comment.