Insert code after a comment with ListRewrite [message #249102] |
Mon, 29 October 2007 12:13 |
Eclipse User![Friend of Eclipse Friend](/donate/web-api/friends_decorator.php?email=) |
|
|
|
Originally posted by: venkataramana.m.tcs.com
We are working on a code-generation plugin which looks for some comments
in the java classes (inside the methods as well) and generates some code.
We are making use of JDT refactoring API like ListRewrite for the same.
Since the comments are not part of AST, we are trying to compute a
comment-location in the AST by figuring out the inner-most block enclosing
the comment and the comment-position w.r.t the child statements of that
block. Now there arise two scenarios of generating code.
1. The comment appears in the first-line of the block enclosing it.
2. The comment appears after some java statement.
When we use ListRewrite.insertFirst, then the inserted code appears above
the comment. But we require it to be appearing after the comment.
In the second case with ListRewrite.insertAfter, the inserted code appears
below the comment.
SO THE PROBLEM IS WITH THE FIRST SCENARIO. Please advise us on this if
anyone has had such requirement. I have attached the code snippet below.
<p><code><pre>
private void expandTagBeforeTheFirstStatement(Tag tag) throws Exception
{
ListRewrite stmtListWriter =
getASTRewriter().getListRewrite(tag.parentBlockNode,
Block.STATEMENTS_PROPERTY);
Block tagSrc =
(Block)(getASTRewriter().createStringPlaceholder(getJavaAdvi ce(),
ASTNode.BLOCK));
stmtListWriter.insertFirst(tagSrc, null); /* WRONG - INSERTS AFTER THE
COMMENT */
/* debug only */
save(tag);
}
private void expandTagAfterThePreceedingStmt(Tag tag) throws Exception
{
ListRewrite stmtListWriter =
getASTRewriter().getListRewrite(tag.parentBlockNode,
Block.STATEMENTS_PROPERTY);
Block tagSrc =
(Block)(getASTRewriter().createStringPlaceholder(getJavaAdvi ce(),
ASTNode.BLOCK));
stmtListWriter.insertAfter(tagSrc, getPreceedingStatememt(tag), null);
/* debug only */
save(tag);
}
</pre></code></p>
Thanks
Venkat
|
|
|
Powered by
FUDForum. Page generated in 0.03353 seconds