Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Language IDEs » Java Development Tools (JDT) » Pretty printing generated AST?
Pretty printing generated AST? [message #253654] Sat, 31 May 2008 22:32 Go to next message
Eclipse User
Originally posted by: colin.mailinglist.gmail.com

Hi all,

I'm pretty new to the JDT core. I'm trying to generate code using the core
AST APIs in an application external to Eclipse, and write the result to
files. I've managed to generate an AST, and write it to a file using
ASTFlattener. I'd like to format this code with the formatter, is this
possible? Can I directly print formatted code from the AST without having
to re-parse the generated code?

Thanks for any help,
Colin
Re: Pretty printing generated AST? [message #844345 is a reply to message #253654] Fri, 13 April 2012 23:27 Go to previous message
Mirko Klemm is currently offline Mirko Klemm
Messages: 1
Registered: April 2012
Junior Member
This might work:

ASTFlattener fl = new ASTFlattener();
unit.accept(fl);
String code = fl.getResult();
CodeFormatter cf = new DefaultCodeFormatter();
TextEdit te = cf.format(CodeFormatter.K_UNKNOWN, code, 0,
code.length(), 0, null);
Document doc = new Document(code);
te.apply(doc);
System.out.println(doc.get());

Good Luck!
Mirko

EDIT: Better use "ToolFactory" to create the formatter, and experiment with the "options" map parameter of the "createCodeFormatter" method!

[Updated on: Sun, 15 April 2012 12:13]

Report message to a moderator

Previous Topic:Import jsf 2.0 netbeans project in to Eclipse IDE Helios
Next Topic:problem with eclipse installation
Goto Forum:
  


Current Time: Wed Oct 22 21:57:11 GMT 2014

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

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