JSDT: how to add JSdoc comment before ASTNode [message #557716] |
Wed, 08 September 2010 11:12  |
Eclipse User |
|
|
|
Hello,
yet an other JSDT question.
I would liek to add a JSdoc comment to a specific ASTNode,
for example to a VariableDeclarationStatement.
I tried many different variants. You will find the "best" one below:
(variant 1) insert JSDoc as first "statement"
public class TestrewriteJSDoc {
public static void main(String[] args) throws MalformedTreeException,
BadLocationException, JavaScriptModelException,
IllegalArgumentException {
Document document = new Document("var x;\nvar y;");
System.out.println("initial document\n" + document.get());
ASTParser parser = ASTParser.newParser(AST.JLS3);
parser.setKind(ASTParser.K_COMPILATION_UNIT);
parser.setSource(document.get().toCharArray());
JavaScriptUnit unit = (JavaScriptUnit) parser.createAST(null);
TextEdit edits = new MultiTextEdit();
VariableDeclarationStatement vds = (VariableDeclarationStatement) unit
.statements().get(1);
ASTRewrite rewrite = ASTRewrite.create(unit.getAST());
JSdoc doc = (JSdoc) rewrite.createStringPlaceholder("/** MyDoc */",
ASTNode.JSDOC);
ListRewrite lr = rewrite.getListRewrite(unit,
JavaScriptUnit.STATEMENTS_PROPERTY);
lr.insertFirst(doc, null);
edits = rewrite.rewriteAST(document, null);
System.out.println("edits = " + edits);
edits.apply(document);
System.out.println("modified document\n" + document.get());
}
}
this version inserts the JSdoc comment as the first "Statement" of the document,
i.e. the original document
var x;
var y;
is transformed to
/** MyDoc */
var x;
var y;
I find it a bit suspicious, that the ListRewrite uses STATEMENTS_PROPERTY as
ChildListPropertyDescripter, because a JSDoc comment certainly is not a "Statement"
But what would be the alternative?
(variant 2):
I attempted to place the JSdoc before a specific VariableDeclarationStatement as follows:
lr.insertBefore(vds, doc, null);
but this results in
Exception in thread "main" java.lang.IllegalArgumentException: Node does not exist
I debugged the code and found, that the 'doc' node is not found in the
getEvent().getIndex(..) method in 'insertBefore'.
The question is: how can I insert the JSdoc into the rewrites' AST?
(variant 3):
I also tried to set the JAVADOC_PROPERTY of the VariableDeclarationStatement as follows:
rewrite.set(vds, VariableDeclarationStatement.JAVADOC_PROPERTY, doc, null);
but the document remains unchanged in this case.
I appreciate any help!
Thanks and kind regards
Harald Finster
|
|
|
|
Re: JSDT: how to add JSdoc comment before ASTNode [message #631455 is a reply to message #629945] |
Thu, 07 October 2010 10:54  |
Eclipse User |
|
|
|
Hello
Am 30.09.2010 07:51, schrieb Nitin Dahyabhai:
> On 9/8/2010 11:12 AM, Harald Finster wrote:
>> yet an other JSDT question.
> Sorry about the late reply.
no problem at all - I have been on holidays anyway ;-)
I appreciate your enlightening and plausible answer!
Greetings Harald
|
|
|
Powered by
FUDForum. Page generated in 0.05991 seconds