Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Issue with formatting line breaks
Issue with formatting line breaks [message #1786100] Wed, 25 April 2018 14:17 Go to next message
Parsa Pourali is currently offline Parsa PouraliFriend
Messages: 197
Registered: February 2014
Senior Member
Hello :)

I am currently trying to apply some formatting rules on my textual model which inserts line breaks after and before each curly bracket. Although this is what I want in general, I need to make an exception for one grammar rule/element.

This is my formatting code:
                 // set indentation inside all curly brackets 
		// set line wrap after each left curly bracket
		// set line wrap around each right curly bracket

		for (org.eclipse.xtext.util.Pair<Keyword, Keyword> p : f.findKeywordPairs("{", "}")) {
			c.setIndentationIncrement().after(p.getFirst());
			c.setIndentationDecrement().before(p.getSecond());
			c.setLinewrap().after(p.getFirst());
			c.setLinewrap().around(p.getSecond());
		}


Now let's say I have an element type which I don't want this formatting rule to be applied to. Below is my grammar for that element, where you can see that there is curly bracket and I don't want it to have line break after and before.
WCA returns Modifiable:
	{WCA} name=ID '{'
	override=Override?
	guard=Guard?
	segment=Action
	'}'
	;


Should I add anything in my formatting code ?

Any help would be appreciated.
Thanks,
Parsa
Re: Issue with formatting line breaks [message #1786101 is a reply to message #1786100] Wed, 25 April 2018 14:38 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 12360
Registered: July 2009
Senior Member
you could compare the keywords you get wih the constants in your grammar access.
and use this in a if else


Need professional support for Xtext, Xpand, EMF?
Go to: https://xtext.itemis.com
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Issue with formatting line breaks [message #1786121 is a reply to message #1786101] Wed, 25 April 2018 19:49 Go to previous messageGo to next message
Parsa Pourali is currently offline Parsa PouraliFriend
Messages: 197
Registered: February 2014
Senior Member
Thanks Christian for the quick reply.

That is what I don't know actually. the p variable which is my keyword, doesn't have anything that can help me to identify the element type. It just has first and second and I can't seem to find anything from the first and second objects. Ideally, I would like to say the following but I don't know how to get to that:
if (p partof WCA)
{
Don't format !
}
else
{
Do Format !
}

Could you please give some more details on how to do this ?

Thanks,
Parsa
Re: Issue with formatting line breaks [message #1786123 is a reply to message #1786121] Wed, 25 April 2018 19:59 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 12360
Registered: July 2009
Senior Member
The the yourdslgrammar Access (inject it or use the already available)
There should be a getyourifelsestatementaccess

Which should have two open and two closed curly keyword
You can compare with the Ones from the pairs



Need professional support for Xtext, Xpand, EMF?
Go to: https://xtext.itemis.com
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Issue with formatting line breaks [message #1786124 is a reply to message #1786123] Wed, 25 April 2018 20:08 Go to previous message
Parsa Pourali is currently offline Parsa PouraliFriend
Messages: 197
Registered: February 2014
Senior Member
Thanks a lot Christian, it works perfectly.

Just for people who might have the same issue as mine, below is the if statement:

for (org.eclipse.xtext.util.Pair<Keyword, Keyword> p : f.findKeywordPairs("{", "}")) {

			if (!(p.first.equals(f.WCAAccess.leftCurlyBracketKeyword_2) &&
				p.second.equals(f.WCAAccess.rightCurlyBracketKeyword_6))) {

				c.setIndentationIncrement().after(p.getFirst());
				c.setIndentationDecrement().before(p.getSecond());
				c.setLinewrap().after(p.getFirst());
				c.setLinewrap().around(p.getSecond());
			}
		}
Previous Topic:quickfix support theia/vscode ides
Next Topic:Migrate from AbstractDeclarativeFormatter to AbstractFormatter2
Goto Forum:
  


Current Time: Wed Nov 14 04:43:35 GMT 2018

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

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

Back to the top