Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » M2T (model-to-text transformation) » Formatting jet-generated java sources according to Eclipse formatting rules ?
Formatting jet-generated java sources according to Eclipse formatting rules ? [message #55255] Mon, 12 January 2009 10:20 Go to next message
Edoardo Comar is currently offline Edoardo Comar
Messages: 102
Registered: July 2009
Senior Member
How can I get the jet-generated java sources formatted according to
Eclipse formatting rules ?

sure I could manually select the files and invoke the formatter, but
since they are scattered acorss my projects and packages it's too long
and error-prone, so I'd rather have it done as part of the one-click
code generation ...
Re: Formatting jet-generated java sources according to Eclipse formatting rules ? [message #55362 is a reply to message #55255] Wed, 14 January 2009 08:21 Go to previous message
Paul Elder is currently offline Paul Elder
Messages: 849
Registered: July 2009
Senior Member
Edoardo:

A quick look at the JDT APIs reveals CodeFormatter.format:

http://help.eclipse.org/ganymede/topic/org.eclipse.jdt.doc.i sv/reference/api/org/eclipse/jdt/core/formatter/CodeFormatte r.html#format (int,%20java.lang.String,%20int,%20int,%20int,%20java.lang.S tring)

So, a basic code format could be implemented as a function tag that called
this. It gets a little tricky, as format returns a TextEdit, the the Java
doc shows how to turn this into a modified string:

http://help.eclipse.org/ganymede/topic/org.eclipse.platform. doc.isv/reference/api/org/eclipse/text/edits/TextEdit.html

There also appears to be the very useful ToolFactory.createCodeFormatter()
method that instantiates a code formatter:

http://help.eclipse.org/ganymede/topic/org.eclipse.jdt.doc.i sv/reference/api/org/eclipse/jdt/core/ToolFactory.html#creat eCodeFormatter(java.util.Map,%20int)

The last problem is getting the options for the target project. This appears
to be done via a number of methods:

JavaCode.create(IProject):
http://help.eclipse.org/ganymede/topic/org.eclipse.jdt.doc.i sv/reference/api/org/eclipse/jdt/core/JavaCore.html#create(org.eclipse.core.resources.IProject)

and then:

IJavaProject.getOptions(boolean):
http://help.eclipse.org/ganymede/topic/org.eclipse.jdt.doc.i sv/reference/api/org/eclipse/jdt/core/IJavaProject.html#getO ptions(boolean)


So, all this suggests you could implement a tag such as:

<x:javaFormat project="foo.bar">
..... Java code to format ...
</x:javaFormat>

I'd be interested in adding such a tag to JET. Would you be interested in
implementing it (and getting your masters to agree to its contribution)?

Paul


"Edoardo Comar" <ecomar@uk.ibm.com> wrote in message
news:gkfn3e$5an$1@build.eclipse.org...
> How can I get the jet-generated java sources formatted according to
> Eclipse formatting rules ?
>
> sure I could manually select the files and invoke the formatter, but since
> they are scattered acorss my projects and packages it's too long and
> error-prone, so I'd rather have it done as part of the one-click code
> generation ...
Previous Topic:JET: user regions and iterators
Next Topic:Existing question
Goto Forum:
  


Current Time: Wed Apr 16 12:05:17 EDT 2014

Powered by FUDForum. Page generated in 0.03103 seconds