PsiToken addAfter

I seemed to have produced a valid statement (nothing blows up, the variable does hold a PsiStatement type thing).

I also seem to have gotten a handle to the opening brace; I cite similar evidence.

openingBrace= currentMethod.getBody().getLBrace();

However,

openingBrace.addAfter(openingBrace, statement) ;

does not love me.

Neither does
openingBrace.add(statement) ;


where I am trying for
public void someMethod () { <-- opening brace, I presume

statement <-- output from plugin

etc .....

3 comments

And the answer seems to be:

the API for this call needs the parent of the PsiElement you want to add something after (i.e. the implied subject of "addAfter").

// gettting the parent

PsiElement parent = yourElement.getParent();

//using the parent, the new statement and the PsiElement after which the statement will be added

parent.addAfter(newStatement, elementAfterWhichStatementWillBeInserted) ;

it worked!

Message was edited by:
softwarevisualization

0

Hello softwarevisualization,

I seemed to have produced a valid statement (nothing blows up, the
variable does hold a PsiStatement type thing).

I also seem to have gotten a handle to the opening brace; I cite
similar evidence.

openingBrace= currentMethod.getBody().getLBrace();

However,

openingBrace.addAfter(openingBrace, statement) ;

does not love me.

Neither does openingBrace.add(statement) ;


The add() should be called on the container in which your target element
should be placed. Obviously an opening brace is not a valid container for
a statement. The correct way to do this is:

currentMethod.getBody().addAfter(statement, openingBrace);

You can use the PsiViewer plugin to examine the PSI structure. Also, you
can find many examples of working with PSI in the bundled open-source plugins
and third-party plugins.

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


0

couldn't figure out how to give you points for answering the question. I love the points I get in other forums, and horde them, in the basement.

0

Please sign in to leave a comment.