Skip to main content



      Home
Home » Modeling » TMF (Xtext) » Use Formatter1 in Xtext 2.9
Use Formatter1 in Xtext 2.9 [message #1725134] Tue, 01 March 2016 02:48 Go to next message
Eclipse UserFriend
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 16:58 Go to previous messageGo to next message
Eclipse UserFriend
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.
Re: Use Formatter1 in Xtext 2.9 [message #1725234 is a reply to message #1725232] Tue, 01 March 2016 17:03 Go to previous messageGo to next message
Eclipse UserFriend
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())
	}
	
}
Re: Use Formatter1 in Xtext 2.9 [message #1725236 is a reply to message #1725234] Tue, 01 March 2016 18:17 Go to previous messageGo to next message
Eclipse UserFriend
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
Re: Use Formatter1 in Xtext 2.9 [message #1725247 is a reply to message #1725236] Wed, 02 March 2016 01:25 Go to previous messageGo to next message
Eclipse UserFriend
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 02:33 Go to previous messageGo to next message
Eclipse UserFriend
have a look at the test build/reports/tests/index.html
what kind of error does it report
Re: Use Formatter1 in Xtext 2.9 [message #1725253 is a reply to message #1725252] Wed, 02 March 2016 02:35 Go to previous messageGo to next message
Eclipse UserFriend
if i have a look at the Formatter gradle generates i can see that this formatter throws exactly that error
Re: Use Formatter1 in Xtext 2.9 [message #1725254 is a reply to message #1725253] Wed, 02 March 2016 02:38 Go to previous messageGo to next message
Eclipse UserFriend
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 02:45 Go to previous messageGo to next message
Eclipse UserFriend
yes then this i the bug i created.
you can try to apply my workaround
Re: Use Formatter1 in Xtext 2.9 [message #1725259 is a reply to message #1725255] Wed, 02 March 2016 03:02 Go to previous messageGo to next message
Eclipse UserFriend
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 03:21 Go to previous messageGo to next message
Eclipse UserFriend
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)
	}

}

Re: Use Formatter1 in Xtext 2.9 [message #1725304 is a reply to message #1725263] Wed, 02 March 2016 06:45 Go to previous message
Eclipse UserFriend
This way it's working, thanks!
Previous Topic:Content assist when model is incomplete
Next Topic:Hovers on Keywords
Goto Forum:
  


Current Time: Wed Jul 23 13:02:06 EDT 2025

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

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

Back to the top