Hello
I have a language based on Xbase that I want to be able to format e.g. when save is initiated or Ctrl-Shift-F is pressed. I have written a formatter that delegates to the XbaseFormatter for Xbase expressions and then formats my own expressions. My problem is that the formatter behaves a bit strange when I have a nested if expression in a switch expression. To show you what I mean I have a simple demo made in the Domainmodel language:
This is what the code looks like when I have a switch expression with simple feature calls in the case parts after I have formatted the code:
package dm {
entity Dm {
op f ( ) : int {
switch ( 1 ) {
case 1 :
1
case 2 :
3
case 3 :
4
case 5 :
5
default :
6
}
}
}
}
This is what the code looks like when I have inserted an if expression in one of the case parts after I have formatted the code:
package dm {
entity Dm {
op f ( ) : int {
switch ( 1 ) {
case 1 :
1
case 2 :
if ( true ) 2 else 3 case 3 :
4 case 5 :
5
default :
6
}
} }
}
It is as if the formatter can't handle some of the configuration (decrementing and linewrapping) after the if expression.
To recreate the behaviour of the formatter you have to start a new Domainmodel example, and make sure the DomainmodelFormatter looks like this (I have omitted the import statements and package declaration):
public class DomainmodelFormatter extends AbstractDeclarativeFormatter {
@Override
protected void configureFormatting(FormattingConfig c) {
DomainmodelGrammarAccess f = (DomainmodelGrammarAccess) getGrammarAccess();
c.setAutoLinewrap(120);
c.setLinewrap(1, 2, 3).around(f.getAbstractElementRule());
c.setLinewrap(1, 2, 3).around(f.getPackageDeclarationRule());
c.setLinewrap(1, 1, 2).around(f.getFeatureRule());
c.setLinewrap(1, 2, 3).around(f.getXSwitchExpressionRule()); /* Added this */
List<Pair<Keyword,Keyword>> pairs = f.findKeywordPairs("{", "}");
for (Pair<Keyword, Keyword> pair : pairs) {
c.setIndentation(pair.getFirst(), pair.getSecond());
}
c.setLinewrap(0, 1, 2).before(f.getSL_COMMENTRule());
c.setLinewrap(0, 1, 2).before(f.getML_COMMENTRule());
c.setLinewrap(0, 1, 1).after(f.getML_COMMENTRule());
configureXSwitchExpression(c, f.getXSwitchExpressionAccess()); /* Added this */
configureXCasePart(c, f.getXCasePartAccess()); /* Added this */
}
/* Copied from XbaseFormatter, removed single space formatting and curly braces indentation */
public void configureXSwitchExpression(FormattingConfig c, XSwitchExpressionElements ele) {
c.setLinewrap().after(ele.getLeftCurlyBracketKeyword_3());
c.setIndentationIncrement().before(ele.getDefaultAssignment_5_2());
c.setLinewrap().around(ele.getDefaultAssignment_5_2());
c.setIndentationDecrement().after(ele.getDefaultAssignment_5_2());
c.setLinewrap().before(ele.getRightCurlyBracketKeyword_6());
}
/* Copied from XbaseFormatter, removed single space formatting */
public void configureXCasePart(FormattingConfig c, XCasePartElements ele) {
c.setIndentationIncrement().before(ele.getThenAssignment_3());
c.setLinewrap().around(ele.getThenAssignment_3());
c.setIndentationDecrement().after(ele.getThenAssignment_3());
}
}
I am currently using Eclipse 4.2 together with Xtext 2.3.1
With best regards,
Martin