formatting generated java-files after build [message #1272362] |
Mon, 17 March 2014 19:04 |
Marc Schlegel Messages: 69 Registered: July 2009 |
Member |
|
|
Hello everyone
After taking a step back and simplified my xText-Proof-of-Concept to a single grammar I am currently fighting my way through the API to add some post-build-step.
All I want is to format all generated Java-Files with the Eclipse-integrated formatter.
I got to the point where I register my custom IXtextBuilderParticipant in the plugin.xml of my ui-module which is called when I edit one of my DSL files. Unfortunately my builder is somehow called in the wrong order and the standard-builder overwrites my formatted files.
A little example
project
-- a.dsl <- I edit this one
-- b.dsl
/src-gen
--a.java
--b.java <- only this file is formatted correctly
After some debugging I am quite sure that my FormattingBuilder is called first which formats all files. After that the incremental builder for a.dsl is called which overwrites the formatted a.java. Since b.java is not touched by the incremental builder it remains formatted correct.
I registered my participant next to the already existing standard-participant. I was trying to rather extend the standard so only one builder will be called but this gave me only errors.
My question is: am I on the right track or do I have to use some other xtext-api? Of course it would be perfect to only format the files that just got generated but for my little example it is enough that all java-files get formatted in a single post-build-step.
Thanks
[Updated on: Mon, 17 March 2014 19:06] Report message to a moderator
|
|
|
|
|
|
Re: formatting generated java-files after build [message #1274479 is a reply to message #1272611] |
Fri, 21 March 2014 14:32 |
Marc Schlegel Messages: 69 Registered: July 2009 |
Member |
|
|
Just want to post my solution.
After reading a short introduction to Xtexts DI with Guice it was easy to attach the IFilePostProcessor. public class MyDslRuntimeModule extends
de.zivit.stplf.meldungen.AbstractMyDslRuntimeModule
{
@Override
public void configure(Binder binder) {
super.configure(binder);
// bind interface to implementation
binder.bind(IFilePostProcessor.class).to(JavaFormatterPostProcessor.class);
}
}
The actuall formatting was a bit more tricky and I fell back to an old JavaBeautifier provided by XPand (xTexts old template engine). The problem with the Eclipse-builtin formatter was, that it brings quite some dependencies to my generator-plugin (jdt.core, text, runtime). The bigger issue though was, that in order to use the formatter I needed a actuall Java-Project with propert src-folder because the formatter works with CompilationUnit and returns TextEdits.
Anyway, in order to use the JavaBeautifier all you need is thisimport java.io.File;
import org.eclipse.emf.common.util.URI;
import org.eclipse.xpand2.output.FileHandle;
import org.eclipse.xpand2.output.FileHandleImpl;
import org.eclipse.xpand2.output.JavaBeautifier;
import org.eclipse.xpand2.output.Outlet;
import org.eclipse.xtext.generator.IFilePostProcessor;
import com.google.inject.Inject;
public class JavaFormatterPostProcessor implements IFilePostProcessor {
@Inject
JavaBeautifier beautifier;
@Override
public CharSequence postProcess(URI fileURI, CharSequence content) {
if ("java".equalsIgnoreCase(fileURI.fileExtension())) {
FileHandle handle = new FileHandleImpl(new Outlet(), new File(fileURI.toPlatformString(true)));
handle.setBuffer(content);
beautifier.beforeWriteAndClose(handle);
return handle.getBuffer();
}
return content;
}
}
I found this code where someone was looking for a Java-Formatter (Beautifier) in xTend2, which is currently not available. Using the old xPand-Implementation is a workaround. Remeber to add org.eclipse.xpand to your dependecies.
[Updated on: Fri, 21 March 2014 14:58] Report message to a moderator
|
|
|
Powered by
FUDForum. Page generated in 0.03452 seconds