JSDT: how to add JSdoc comment before ASTNode [message #557716] |
Wed, 08 September 2010 15:12 |
Harald Finster Messages: 37 Registered: July 2009 |
Member |
|
|
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 #629945 is a reply to message #557716] |
Thu, 30 September 2010 05:51 |
|
On 9/8/2010 11:12 AM, Harald Finster wrote:
> yet an other JSDT question.
>
> I would liek to add a JSdoc comment to a specific ASTNode,
> for example to a VariableDeclarationStatement.
Sorry about the late reply. As you found, JSDoc doesn't usually get
treated as a statement, it's a field on the particular AST object it
precedes. You're not doing anything particularly wrong with the
exception of JSDT (like JDT) not supporting JSDoc on local variable
declarations, even though there is a placeholder field in the
corresponding AST class. Function declarations are supported best, and
as constructors their doc will be used in the UI for its "type". Your
use of the list rewrite as an alternate is actually a pleasant surprise,
and looks to me to actually be your best option.
--
Nitin Dahyabhai
Eclipse WTP Source Editing and JSDT
IBM Rational
_
Nitin Dahyabhai
Eclipse Web Tools Platform
|
|
|
|
Powered by
FUDForum. Page generated in 0.03187 seconds