Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Use Formatter1 in Xtext 2.9
Use Formatter1 in Xtext 2.9 [message #1725134] Tue, 01 March 2016 07:48 Go to next message
Markus Amshove is currently offline Markus AmshoveFriend
Messages: 25
Registered: March 2016
Junior Member
Hello,

I'm currently migrating a DSL to Xtext 2.9 and don't want to rewrite the formatter with the new API (as the grammar doesn't need to change).

I tried to bind the IFormatter in the RuntimeSetup and the tests are working in Eclipse and are green.

However if I try to build and test it in out CI (via Gradle) the formatting tests fail with "org.junit.ComparisonFailure", which basically means that the formatter isn't called.

My Helpermethod to test the formatting ist:

private def void assertFormattedAs(CharSequence input, CharSequence erwartet)
{
	erwartet.toString.assertEquals(
		(input.parse.eResource as XtextResource).parseResult.rootNode.format(0, input.length).formattedText)
}


Is there any other setup I need to make to have it working in Gradle?


best regards!
Re: Use Formatter1 in Xtext 2.9 [message #1725232 is a reply to message #1725134] Tue, 01 March 2016 21:58 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14076
Registered: July 2009
Senior Member
in gradle i get this error in test

java.lang.Error: Unresolved compilation problems:
getNameAssignment_1 cannot be resolved
getExclamationMarkKeyword_2 cannot be resolved
	at org.xtext.example.mydsl9.MyDslFormatter.configureFormatting(MyDslFormatter.java:15)
	at org.eclipse.xtext.formatting.impl.AbstractDeclarativeFormatter.getConfig(AbstractDeclarativeFormatter.java:93)
	at org.eclipse.xtext.formatting.impl.AbstractDeclarativeFormatter.createFormatterStream(AbstractDeclarativeFormatter.java:75)
	at org.eclipse.xtext.formatting.impl.DefaultNodeModelFormatter.format(DefaultNodeModelFormatter.java:84)
	at org.xtext.example.mydsl9.tests.DummyTest.assertFormattedAs(DummyTest.java:50)
	at org.xtext.example.mydsl9.tests.DummyTest.testIt(DummyTest.java:68)


this is cause the xtend-generation is not working properly

can you please file a ticket for that.


Need professional support for Xtext, Xpand, EMF?
Go to: https://www.itemis.com/en/it-services/methods-and-tools/xtext
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Use Formatter1 in Xtext 2.9 [message #1725234 is a reply to message #1725232] Tue, 01 March 2016 22:03 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14076
Registered: July 2009
Senior Member
as a workaround you might give the xtend compiler some mode hints e.g


class MyDslFormatter extends AbstractDeclarativeFormatter {
	
	@Inject
	MyDslGrammarAccess ga
	
	override protected configureFormatting(FormattingConfig config) {
		val GreetingElements e = ga.getGreetingAccess()
		format(config, e)
	}
	
	def private format(FormattingConfig config, GreetingElements e) {
		config.setLinewrap().before(e.getNameAssignment_1())
		config.setLinewrap().before(e.getExclamationMarkKeyword_2())
	}
	
}


Need professional support for Xtext, Xpand, EMF?
Go to: https://www.itemis.com/en/it-services/methods-and-tools/xtext
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Use Formatter1 in Xtext 2.9 [message #1725236 is a reply to message #1725234] Tue, 01 March 2016 23:17 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14076
Registered: July 2009
Senior Member
i have created https://bugs.eclipse.org/bugs/show_bug.cgi?id=488808 for the compiler plugin,

i dont know if this is your root cause for the failure.

if not can you share a small example reproducing


Need professional support for Xtext, Xpand, EMF?
Go to: https://www.itemis.com/en/it-services/methods-and-tools/xtext
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Use Formatter1 in Xtext 2.9 [message #1725247 is a reply to message #1725236] Wed, 02 March 2016 06:25 Go to previous messageGo to next message
Markus Amshove is currently offline Markus AmshoveFriend
Messages: 25
Registered: March 2016
Junior Member
Hi Christian,

this was the first error I got.

After deleting the formatter, configuring formatter2 in mwe2, deleting formatter2 and then reapply formatter1 ( Smile ) I got rid of that strange nameAssignment-bug.

I now have a formatter that formats and unittests that (in eclipse) tell me that the formatting works.

However if I build with gradle, the formatting test fails. It must be something with resolving the formatter? But there is no NPE..

Screenshots and the example are included.

Thanks for your help Smile
Re: Use Formatter1 in Xtext 2.9 [message #1725252 is a reply to message #1725247] Wed, 02 March 2016 07:33 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14076
Registered: July 2009
Senior Member
have a look at the test build/reports/tests/index.html
what kind of error does it report


Need professional support for Xtext, Xpand, EMF?
Go to: https://www.itemis.com/en/it-services/methods-and-tools/xtext
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Use Formatter1 in Xtext 2.9 [message #1725253 is a reply to message #1725252] Wed, 02 March 2016 07:35 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14076
Registered: July 2009
Senior Member
if i have a look at the Formatter gradle generates i can see that this formatter throws exactly that error

Need professional support for Xtext, Xpand, EMF?
Go to: https://www.itemis.com/en/it-services/methods-and-tools/xtext
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Use Formatter1 in Xtext 2.9 [message #1725254 is a reply to message #1725253] Wed, 02 March 2016 07:38 Go to previous messageGo to next message
Markus Amshove is currently offline Markus AmshoveFriend
Messages: 25
Registered: March 2016
Junior Member
You're right, it's

nameAssignment_1 cannot be resolved

Re: Use Formatter1 in Xtext 2.9 [message #1725255 is a reply to message #1725254] Wed, 02 March 2016 07:45 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14076
Registered: July 2009
Senior Member
yes then this i the bug i created.
you can try to apply my workaround


Need professional support for Xtext, Xpand, EMF?
Go to: https://www.itemis.com/en/it-services/methods-and-tools/xtext
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Use Formatter1 in Xtext 2.9 [message #1725259 is a reply to message #1725255] Wed, 02 March 2016 08:02 Go to previous messageGo to next message
Markus Amshove is currently offline Markus AmshoveFriend
Messages: 25
Registered: March 2016
Junior Member
Strangely if I add another test:

	@Test
	def void makeSureThatThereIsNoSpaceAfterTheName()
	{
		'''Hello Christian !'''.assertFormattedAs('''Hello Christian!''')
	}
	
	
	@Test
	def void doASecondTest()
	{
		'''Hello Christian !'''.assertFormattedAs('''Hello Christian!''')
	}


One of the tests fails with compilation error, the other with
org.junit.ComparisonFailure: expected:<Hello Christian[]!> but was:<Hello Christian[ ]!>



What hints do you suggest in your workaround? The formatter just looks as mine does
Re: Use Formatter1 in Xtext 2.9 [message #1725263 is a reply to message #1725259] Wed, 02 March 2016 08:21 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14076
Registered: July 2009
Senior Member
package org.xtext.example.mydsl.formatting

import com.google.inject.Inject
import org.eclipse.xtext.Assignment
import org.eclipse.xtext.formatting.impl.AbstractDeclarativeFormatter
import org.eclipse.xtext.formatting.impl.FormattingConfig
import org.xtext.example.mydsl.services.MyDslGrammarAccess
import org.xtext.example.mydsl.services.MyDslGrammarAccess.GreetingElements

class Formatter extends AbstractDeclarativeFormatter
{
	@Inject
	MyDslGrammarAccess ga

	
	override protected configureFormatting(FormattingConfig config) {
		val GreetingElements e = ga.getGreetingAccess()
		format(config, e)
	}
	
	def private format(FormattingConfig config, GreetingElements e) {
		val Assignment nameToGreet = e.getNameAssignment_1()
		config.setNoSpace.after(nameToGreet)
	}

}



Need professional support for Xtext, Xpand, EMF?
Go to: https://www.itemis.com/en/it-services/methods-and-tools/xtext
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Use Formatter1 in Xtext 2.9 [message #1725304 is a reply to message #1725263] Wed, 02 March 2016 11:45 Go to previous message
Markus Amshove is currently offline Markus AmshoveFriend
Messages: 25
Registered: March 2016
Junior Member
This way it's working, thanks!
Previous Topic:Content assist when model is incomplete
Next Topic:Hovers on Keywords
Goto Forum:
  


Current Time: Fri Oct 22 20:38:06 GMT 2021

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

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

Back to the top