Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » formatting generated java-files after build(Is IXtextBuilderParticipant the right way to format artifacts as post-build-step)
formatting generated java-files after build [message #1272362] Mon, 17 March 2014 19:04 Go to next message
Marc Schlegel is currently offline Marc SchlegelFriend
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 #1272367 is a reply to message #1272362] Mon, 17 March 2014 19:19 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
Hi,

i have no idea on that but
did you consider to implement org.eclipse.xtext.generator.IFilePostProcessor instead?


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: formatting generated java-files after build [message #1272381 is a reply to message #1272367] Mon, 17 March 2014 19:56 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33140
Registered: July 2009
Senior Member
Yes, it would make more sense to format the test you're generated before
you write it out (using
org.eclipse.jdt.core.ToolFactory.createCodeFormatter(Map) to create the
formatter)...

On 17/03/2014 12:19 PM, Christian Dietrich wrote:
> Hi,
>
> i have no idea on that but
> did you consider to implement
> org.eclipse.xtext.generator.IFilePostProcessor instead?


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: formatting generated java-files after build [message #1272611 is a reply to message #1272362] Tue, 18 March 2014 10:34 Go to previous messageGo to next message
Marc Schlegel is currently offline Marc SchlegelFriend
Messages: 69
Registered: July 2009
Member
Thanks for your suggestion.

IFilePostProcessor seems to be exactly what I need. Could you give me some advice on how to register my implementation of a post-processor. I couldn't find any extension-point as I did with the XtextBuilder. Or do I need to add some lines to MyDslUiModule?

Regarding the actual format I am also a little confused. The Eclipse-Formatter that I created using the ToolFactory works with ICompilationUnit and returns a TextEdit. Since the IFilePostProcessor works with a CharSequence there might be no actual file to use for the CompilationUnit. The interface IFilePostProcessor declares a file-URI but has the sequence already been written to that file?

regards
Marc
Re: formatting generated java-files after build [message #1274479 is a reply to message #1272611] Fri, 21 March 2014 14:32 Go to previous message
Marc Schlegel is currently offline Marc SchlegelFriend
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 this
import 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

Previous Topic:Annotation values generation 2.4 vs 2.5
Next Topic:how to generate a method using xtend from xtext based syntax
Goto Forum:
  


Current Time: Thu Apr 25 05:49:07 GMT 2024

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

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

Back to the top