DZone Forums
Go Back   DZone Forums > Community > Tools & IDEs > Eclipse
Reload this Page Creating comments
Notices
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  (#1 (permalink)) Old
Member
 
Posts: 21
Thanks: 5
Thanked 2 Times in 2 Posts
Join Date: Jun 2009
Default Creating comments - 07-30-2009, 12:26 AM

I'm using the AST to create new java code in a sort of extended JavaCompletionProposal. I'm wondering what is the easiest way to create single and multi line comments in an existing compilationunit? I would hope to preserve the undo stack.
Reply With Quote
  (#2 (permalink)) Old
Member
 
Posts: 2
Thanks: 0
Thanked 1 Time in 1 Post
Join Date: Jul 2010
Default Adding comment - 07-29-2010, 12:48 AM

if u have object of AST then u might already have object of ICompilationUnit...

u can directly call the method codeComplete(....) for ur purpose...

ICompilationUnit myCU;

myCU.codeComplete(....) ;

hope u understand..
Reply With Quote
The Following User Says Thank You to Gaurav Saxena For This Useful Post:
mmootygam (07-29-2010)
  (#3 (permalink)) Old
Member
 
Posts: 21
Thanks: 5
Thanked 2 Times in 2 Posts
Join Date: Jun 2009
Default Thanks - 07-29-2010, 07:02 AM

Thanks for your help, but I've solved this myself since I posted the thread. I used the following code:

Code:
// Actually edits the document via the AST, using ASTRewrite.
		ASTRewrite rewriter = ASTRewrite.create(ast);
		ASTHandler handler = new ASTHandler();
		ASTNode currentNode = handler.descend(root,
				CalciteCompletionProposalComputer
						.getIdentifierBeforeOffset(context));
		ASTNode parent = currentNode.getParent();
		//ITrackedNodePosition parentLocation = rewriter.track(currentNode.getParent());
		ListRewrite lrw;
		try {
			if (parent instanceof Block)
				lrw = rewriter
						.getListRewrite(parent, Block.STATEMENTS_PROPERTY);
			else
				lrw = rewriter.getListRewrite(parent,
						((AbstractTypeDeclaration) parent)
								.getBodyDeclarationsProperty());
		} catch (Throwable e) {
			return;
		}
		for (ASTNode prior : preStatements) {
			lrw.insertBefore(prior, currentNode, null);
		}
		lrw = rewriter.getListRewrite(root, CompilationUnit.IMPORTS_PROPERTY);
		for (ImportDeclaration imp : importStatements)
			lrw.insertLast(imp, null);
		TextEdit edits = rewriter.rewriteAST(document, null);

		// Sets cursor position and applies the primary edit
		int cursorDelta = super.getReplacementString().indexOf(',');
		if (cursorDelta == -1)
			cursorDelta = regexParseDisplay(super.getReplacementString());
		if (preStatements.size() > 0)
			cursorDelta += COMMENT_LENGTH;
		cursorDelta += getMultiEditLength(edits);
		super.setCursorPosition(cursorDelta);
		super.apply(document, trigger, offset);

		try {
			// Preserves the undo stack.
			edits.apply(document);
		} catch (MalformedTreeException e) {
			e.printStackTrace();
		} catch (BadLocationException e) {
			e.printStackTrace();
		}
If anyone who reads this needs more help, please just reply to this thread.

Last edited by mmootygam; 07-29-2010 at 07:09 AM. Reason: Misread previous post
Reply With Quote
  (#4 (permalink)) Old
Member
 
Posts: 2
Thanks: 0
Thanked 1 Time in 1 Post
Join Date: Jul 2010
Default 07-29-2010, 07:06 AM

I would like to know ur way...
Reply With Quote
  (#5 (permalink)) Old
Member
 
Posts: 21
Thanks: 5
Thanked 2 Times in 2 Posts
Join Date: Jun 2009
Default 07-29-2010, 07:11 AM

Sorry, I misread this thread when I first replied. I edited it to answer the right question, and decided I'd go ahead and include my code anyway. The bolded lines are the ones most important to preserving the undo stack. The other ones are just there for context.
Reply With Quote
Reply

Tags
ast, comments, completion proposal, source code

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Creating .lib Files Woogoo Eclipse 0 07-27-2009 07:28 AM
Problem with formating comments lubomirus Eclipse 0 07-07-2009 01:41 AM
Creating an SWT Scrolled Composite alwillms Eclipse 0 02-27-2009 05:04 PM
Permalinks to comments on 2nd page incorrect ahalsey Feedback 0 08-20-2008 04:23 PM
Blog Comments in Forums gdboling Zones 1 01-27-2008 07:29 PM


Copyright 1997-2009, DZone, Inc.
vBulletin Skin developed by: vBStyles.com