Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Need assistance with formatting...
Need assistance with formatting... [message #911471] Tue, 11 September 2012 17:16 Go to next message
Cristiano Gavião is currently offline Cristiano GaviãoFriend
Messages: 279
Registered: July 2009
Senior Member
hello,
I'm facing a problem with formatting for days that is giving me headache Sad

Could someone, please, point me what am I missing ?

I created a simple model to easy reproduction and a simple test that shows what I want to achieve.

thanks,

Cristiano

test:
	@Test
	def void testFormatingForExampleTable() {

		val storyParsed = 
		'''
		rule Test1

		rule Test2
		examples:
		|h1|h2|h3|
		|col1|col2|col3|
		
		'''.parse.serialize(SaveOptions::newBuilder.format().getOptions())
		
		val storyExpected = 
		'''
		
		rule Test1

		rule Test2
			examples:
			|h1|h2|h3|
			|col1|col2|col3|'''.toString

		assertEquals(storyExpected, storyParsed)
	}


test result:
org.junit.ComparisonFailure: expected:<...xamples:
	|h1|h2|h3|[]
	|col1|col2|col3|> but was:<...xamples:
	|h1|h2|h3|[ ]
	|col1|col2|col3|>



grammar:
grammar org.xtext.example.mydsl.MyDsl hidden(WS, SL_COMMENT, NEWLINE)

generate myDsl "http://www.xtext.org/example/mydsl/MyDsl"

import "http://www.eclipse.org/emf/2002/Ecore" as ecore

Rules:
	(rules+=Rule*)	
;
Rule:
	'rule' name=ID
	(
		(exampleTable=Examples)? &
		(expectationTable=Expectations)?	
	)
;
Examples:
	'examples:'
	table=Table
;
Expectations:
	'expectations:'
	table=Table
;
Table hidden(WS, SL_COMMENT):
	{Table}
	((rows+=TableRow) (NEWLINE rows+=TableRow)*)
;
TableRow hidden(WS, SL_COMMENT):
	{TableRow}
	('|' (cells += TableCell ('|' cells += TableCell)*) '|')
;
TableCell:
	name = Content
;
Content returns ecore::EString:
	(STRING | ID | INT | ANY_OTHER) (STRING | ID | INT | ANY_OTHER)*;
FqnWithWildCard:
	Fqn ('.' '*')?;
Fqn:
  ValidID (=>'.' ValidID)*;
ValidID:
	ID;
terminal ID:
	'^'?('a'..'z'|'A'..'Z'|'_') ('a'..'z'|'A'..'Z'|'_'|'0'..'9')*;
terminal INT returns ecore::EDouble:
	'-'? ('0'..'9')+ ('.' ('0'..'9')+)?;
terminal PLACEHOLDER:
	'<' ('a'..'z' | 'A'..'Z') !('.' | '>' | '\n' | '\r')* '>';
terminal PATH:
	'/' ('/' | '.' | '_' | 'a'..'z' | 'A'..'Z' | '0'..'9')+;
terminal STRING:
	'"' ('\\' ('b' | 't' | 'n' | 'f' | 'r' | 'u' | '"' | "'" | '\\') | !('\\' | '"'))* '"' |
	"'" ('\\' ('b' | 't' | 'n' | 'f' | 'r' | 'u' | '"' | "'" | '\\') | !('\\' | "'"))* "'";
terminal NEWLINE:
	'\r'? '\n'?;
terminal SL_COMMENT 	: '!--' !('\n'|'\r')* ('\r'? '\n')?;
terminal WS			: (' '|'\t'|'\r'|'\n')+;
terminal ANY_OTHER:
.;


formatter:
	protected void configureFormatting(FormattingConfig c) {
		
		MyDslGrammarAccess gac = (MyDslGrammarAccess) getGrammarAccess();

		// rule
		c.setLinewrap(1, 1, 2).before(gac.getRuleAccess().getRuleKeyword_0());
		
		// Example table
		c.setLinewrap(1, 1, 2).before(gac.getExamplesRule());
		c.setIndentationIncrement().before(gac.getExamplesAccess().getExamplesKeyword_0());
		c.setLinewrap(1, 1, 2).after(gac.getExamplesAccess().getExamplesKeyword_0());
		c.setIndentationDecrement().after(gac.getExamplesAccess().getExamplesKeyword_0());
		c.setLinewrap(1, 1, 2).after(gac.getExamplesRule());

		// Table

		c.setLinewrap(0, 1, 1).before(gac.getTableAccess().getNEWLINETerminalRuleCall_1_1_0());
		c.setIndentationIncrement().before(gac.getTableAccess().getGroup());
		c.setNoSpace().around(gac.getTableCellRule());
		c.setIndentationDecrement().after(gac.getTableRule());			
	}

[Updated on: Tue, 11 September 2012 18:14]

Report message to a moderator

Re: Need assistance with formatting... [message #911486 is a reply to message #911471] Tue, 11 September 2012 18:02 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 13669
Registered: July 2009
Senior Member
Hi,

can you share your formatter too?

~Christian


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: Need assistance with formatting... [message #911491 is a reply to message #911486] Tue, 11 September 2012 18:15 Go to previous messageGo to next message
Cristiano Gavião is currently offline Cristiano GaviãoFriend
Messages: 279
Registered: July 2009
Senior Member
Hi Christian,

without formatter is a kind difficult, no ? Smile

I've edited the main question..

thanks
Re: Need assistance with formatting... [message #911492 is a reply to message #911486] Tue, 11 September 2012 18:17 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 13669
Registered: July 2009
Senior Member
Here is my solution

// Table

		c.setLinewrap(0, 1, 1).before(gac.getTableAccess().getNEWLINETerminalRuleCall_1_1_0());
		c.setIndentationIncrement().before(gac.getTableAccess().getGroup());
		c.setNoSpace().after(gac.getTableRowAccess().getVerticalLineKeyword_1_0());
		c.setNoSpace().after(gac.getTableRowAccess().getVerticalLineKeyword_1_1_1_0());
		c.setNoSpace().after(gac.getTableRowAccess().getVerticalLineKeyword_1_2());
		c.setNoSpace().before(gac.getTableRowAccess().getVerticalLineKeyword_1_1_1_0());
		c.setNoSpace().before(gac.getTableRowAccess().getVerticalLineKeyword_1_2());
		c.setIndentationDecrement().after(gac.getTableRule());	


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: Need assistance with formatting... [message #911497 is a reply to message #911492] Tue, 11 September 2012 18:31 Go to previous messageGo to next message
Cristiano Gavião is currently offline Cristiano GaviãoFriend
Messages: 279
Registered: July 2009
Senior Member
Hi Christian,

what a h.., I have to remove the keyword spaces, too !!!

Man, I owned you a box of beers !!!

thank you very very much !! Very Happy
Re: Need assistance with formatting... [message #911509 is a reply to message #911492] Tue, 11 September 2012 18:57 Go to previous messageGo to next message
Cristiano Gavião is currently offline Cristiano GaviãoFriend
Messages: 279
Registered: July 2009
Senior Member
Christian,

There is a problem.

Using the setNoSpace have a not good consequence. It doesn't let you include a NewLine.

If you include one more rule after the table then test will fail again:
org.junit.ComparisonFailure: expected:<...3|
	|col1|col2|col3|[

rule Test3
]> but was:<...3|
	|col1|col2|col3|[rule Test3]>



	@Test
	def void testFormatingForExampleTable() {

		val storyParsed = 
		'''
		rule Test1

		rule Test2
		examples:
		|h1|h2|h3|
		|col1|col2|col3|
		
		rule Test3
		'''.parse.serialize(SaveOptions::newBuilder.format().getOptions())
		
		val storyExpected = 
		'''
		
		rule Test1

		rule Test2
			examples:
			|h1|h2|h3|
			|col1|col2|col3|
		
		rule Test3
			'''.toString

		assertEquals(storyExpected, storyParsed)
	}
Re: Need assistance with formatting... [message #911513 is a reply to message #911509] Tue, 11 September 2012 19:08 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 13669
Registered: July 2009
Senior Member
c.setNoSpace().between(gac.getTableRowAccess().getVerticalLineKeyword_1_2(), gac.getTableAccess().getNEWLINETerminalRuleCall_1_1_0());

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: Need assistance with formatting... [message #911563 is a reply to message #911513] Tue, 11 September 2012 21:41 Go to previous messageGo to next message
Cristiano Gavião is currently offline Cristiano GaviãoFriend
Messages: 279
Registered: July 2009
Senior Member
Well, I walked, walked and comeback to same point than last week Sad. running editor behaves different from what I achieve in tests.

Could you please run the example model, putting the text that we use in the test and call format action ?

here, using this text:
rule Test1

rule Test2
examples:
|h1|h2|h3|
|col1|col2|col3|
		
rule Test3


after first calling of format action, I got this message error at row 2: missing EOF at '|':


rule Test1

rule Test2 

	examples: 

	|h1|h2|h3|
	|col1|col2|col3|

rule Test3


calling again, without change the text, I got this:

rule Test1

rule Test2

	examples:

	|h1|h2|h3| | col1 | col2 | col3 | rule Test3

[Updated on: Tue, 11 September 2012 21:42]

Report message to a moderator

Re: Need assistance with formatting... [message #911590 is a reply to message #911513] Tue, 11 September 2012 23:14 Go to previous message
Cristiano Gavião is currently offline Cristiano GaviãoFriend
Messages: 279
Registered: July 2009
Senior Member
Investigation is over !!! Smile

I discovered that the grammar was not supporting the formatted text and unfortunately tests was not capturing that...

Thank you Christian again for the time dispensed !

For people that could be interested following is the solution.

New grammar. Note that I'm not using NEWLINE anymore:

grammar org.xtext.example.mydsl.MyDsl hidden(WS, SL_COMMENT)

generate myDsl "http://www.xtext.org/example/mydsl/MyDsl"

import "http://www.eclipse.org/emf/2002/Ecore" as ecore

Rules:
	(rules+=Rule*)
	
;

Rule:
	'rule' name=ID
	(
		(exampleTable=Examples)? &
		(expectationTable=Expectations)?	
	)
;

Examples:
	'examples:'
	table=Table

;
Expectations:
	'expectations:'
	table=Table
;

Table:
	{Table}
	((rows+=TableRow) (rows+=TableRow)*)
;

TableRow:
	{TableRow}
	('|' (cells += TableCell ('|' cells += TableCell)*) '|')
;

TableCell:
	name = Content
;

Content returns ecore::EString:
	(STRING | ID | INT | ANY_OTHER) (STRING | ID | INT | ANY_OTHER)*;

FqnWithWildCard:
	Fqn ('.' '*')?;

Fqn:
  ValidID (=>'.' ValidID)*;
  
ValidID:
	ID;
	
terminal ID:
	'^'?('a'..'z'|'A'..'Z'|'_') ('a'..'z'|'A'..'Z'|'_'|'0'..'9')*;

terminal INT returns ecore::EDouble:
	'-'? ('0'..'9')+ ('.' ('0'..'9')+)?;

terminal PLACEHOLDER:
	'<' ('a'..'z' | 'A'..'Z') !('.' | '>' | '\n' | '\r')* '>';

terminal PATH:
	'/' ('/' | '.' | '_' | 'a'..'z' | 'A'..'Z' | '0'..'9')+;

terminal STRING:
	'"' ('\\' ('b' | 't' | 'n' | 'f' | 'r' | 'u' | '"' | "'" | '\\') | !('\\' | '"'))* '"' |
	"'" ('\\' ('b' | 't' | 'n' | 'f' | 'r' | 'u' | '"' | "'" | '\\') | !('\\' | "'"))* "'";

//terminal NEWLINE:
//	'\r'? '\n'?;

terminal SL_COMMENT 	: '!--' !('\n'|'\r')* ('\r'? '\n')?;

terminal WS			: (' '|'\t'|'\r'|'\n')+;

terminal ANY_OTHER:
	.;


The changed formatting code:
	@Override
	protected void configureFormatting(FormattingConfig c) {
		
		MyDslGrammarAccess gac = (MyDslGrammarAccess) getGrammarAccess();

		// rule
		c.setLinewrap(1, 1, 2).before(gac.getRuleAccess().getRuleKeyword_0());
		
		// Example table
		c.setLinewrap(1, 1, 2).before(gac.getExamplesRule());
		c.setIndentationIncrement().before(gac.getExamplesAccess().getExamplesKeyword_0());
		c.setLinewrap(1, 1, 2).after(gac.getExamplesAccess().getExamplesKeyword_0());
		c.setIndentationDecrement().after(gac.getExamplesAccess().getExamplesKeyword_0());
		c.setLinewrap(1, 1, 2).after(gac.getExamplesRule());

		// Table

		c.setLinewrap(1, 1, 1).bounds(gac.getTableRowAccess().getVerticalLineKeyword_1_2(), gac.getTableRowAccess().getVerticalLineKeyword_1_0());
		c.setIndentationIncrement().before(gac.getTableAccess().getGroup());
		c.setNoSpace().around(gac.getTableCellRule());
		c.setIndentationDecrement().after(gac.getTableRule());	
	}


and new tests:
	@Test
	def void testFormatingForExampleTable2() {

		val storyParsed = 
		'''
		rule Test1

		rule Test2
		examples:
		|h1|h2|h3||col1|col2|col3|
		
		rule Test3
		'''.parse.serialize(SaveOptions::newBuilder.format().getOptions())
		
		val storyExpected = 
		'''
		
		rule Test1

		rule Test2
			examples:
			|h1|h2|h3|
			|col1|col2|col3|
		
		rule Test3'''.toString

		assertEquals(storyExpected, storyParsed)
	}

Previous Topic:How to iterate in a collection using Xtend?
Next Topic:XtextStyledTextCellEditor
Goto Forum:
  


Current Time: Sat Dec 05 03:28:29 GMT 2020

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

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

Back to the top