Need assistance with formatting... [message #911471] |
Tue, 11 September 2012 17:16 |
Cristiano Gavião Messages: 279 Registered: July 2009 |
Senior Member |
|
|
hello,
I'm facing a problem with formatting for days that is giving me headache
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 #911492 is a reply to message #911486] |
Tue, 11 September 2012 18:17 |
|
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());
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
|
|
|
|
|
|
|
Re: Need assistance with formatting... [message #911590 is a reply to message #911513] |
Tue, 11 September 2012 23:14 |
Cristiano Gavião Messages: 279 Registered: July 2009 |
Senior Member |
|
|
Investigation is over !!!
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)
}
|
|
|
Powered by
FUDForum. Page generated in 0.04137 seconds