Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Formatting nested if expression in switch expression fails
Formatting nested if expression in switch expression fails [message #933780] Fri, 05 October 2012 10:35 Go to next message
Martin Nilsson is currently offline Martin NilssonFriend
Messages: 7
Registered: November 2009
Junior Member
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
Re: Formatting nested if expression in switch expression fails [message #936659 is a reply to message #933780] Mon, 08 October 2012 07:47 Go to previous message
Martin Nilsson is currently offline Martin NilssonFriend
Messages: 7
Registered: November 2009
Junior Member
The hack suggested by Sandra Shklyaeva in this post: http://www.eclipse.org/forums/index.php/mv/msg/385087/936554/#msg_936554 actually solves the problem.

With best regards,

Martin
Previous Topic:Backtrack and Formatting bug?
Next Topic:xtend: How to import resources of resource
Goto Forum:
  


Current Time: Wed Apr 24 17:33:50 GMT 2024

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

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

Back to the top