Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Web Tools Project (WTP) » JSDT: how to add JSdoc comment before ASTNode
JSDT: how to add JSdoc comment before ASTNode [message #557716] Wed, 08 September 2010 11:12 Go to previous message
Harald Finster is currently offline 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
 
Read Message
Read Message
Read Message
Previous Topic:XML validation and markers
Next Topic:XML Schema
Goto Forum:
  


Current Time: Tue May 21 18:09:27 EDT 2013

Powered by FUDForum. Page generated in 0.01675 seconds