Intelij idea plugin : Psi Classes


I am developing a plugin in Intellij idea. I am stuck at inserting a piece of code after a statement.

For eg:


I need to find the element launchPurchaseflow and add a piece of code after that statement.I used Psielement and string matcher to get to the corresponding string.

Now my PsiElement = mhelper.launchPurchaseFlow. If I use psiClass.addAfter(newElement, PsiElement.getContext());

It is only trying to add after the closing paranthesis.So its pop out my new element as Incorrect Statement.

For eg:

Its adding at position mHelper.launchPurchaseflow(str,str,str)//newElement; But I need to add at mHelper.launchPurchaseflow(str,str,str); // new Element.

I am an Amateur developer.Any help will be useful to me. Thanks in advance


I'm not sure I understand completely. Most likely you need to find parent code block, create your PSI via PsiElementFactory and invoke codeBlock.addAfter(newStatement, foundStatement), where foundStatement is of PsiExpressionStatement type. You should find the latter in the tree. If you find some its descendant (e.g. mhelper.launchPurchaseFlow reference expression), you can use PsiTreeUtil.getParentOfType(refExpr, PsiExpressionStatement.class).


That was really useful and it helped to solve our issue..  

Thanks a lot...


Please sign in to leave a comment.