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 15:12 Go to next message
Harald Finster is currently offline Harald FinsterFriend
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 Go to previous messageGo to next message
Nitin Dahyabhai is currently offline Nitin DahyabhaiFriend
Messages: 2332
Registered: July 2009
Senior Member
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 WTP, IBM
Re: JSDT: how to add JSdoc comment before ASTNode [message #631455 is a reply to message #629945] Thu, 07 October 2010 14:54 Go to previous message
Harald Finster is currently offline Harald FinsterFriend
Messages: 37
Registered: July 2009
Member
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
Previous Topic:XML validation and markers
Next Topic:XML Schema
Goto Forum:
  


Current Time: Sun Nov 23 14:56:31 GMT 2014

Powered by FUDForum. Page generated in 0.02418 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software