Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » [Formatter2] append newLine + spaces
[Formatter2] append newLine + spaces [message #1803049] Thu, 21 February 2019 13:20 Go to next message
ayman salah is currently offline ayman salahFriend
Messages: 130
Registered: June 2015
Senior Member
Hi all,

I am trying to achieve alignment with list opening bracket, but I can't.

Here is how I am trying:

Grammar:
grammar org.xtext.example.mydsl.MyDsl with org.eclipse.xtext.common.Terminals

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

DSL returns XTDSL:
	models+=Model*
;

Model returns XTModel:
	{XTModel} 'startList' '(' greetings+=Greeting*')'';';
	
Greeting returns XTGreeting:
	'list' ids_list=identifier_list ':' 'endlist' ';';

identifier_list returns XTIdentifierList:
	ids+=identifier (',' ids+=identifier)*
;

identifier returns XTIdentifier:
	{XTIdentifier} ID
;


Formatter:
	def dispatch void format(XTDSL xtdsl, extension IFormattableDocument document) {
		xtdsl.models.forEach[format]
	}

	def dispatch void format(XTModel xtmodel, extension IFormattableDocument document) {
		xtmodel.greetings.forEach[format; append[newLine]]
		xtmodel.regionFor.keyword("startList").append[oneSpace]
		xtmodel.regionFor.keyword("(").append[newLine]
		xtmodel.regionFor.keyword(")").prepend[noSpace]
		xtmodel.regionFor.keyword(";").prepend[noSpace]
		xtmodel.append[newLine]
		alignGreetingsOnOpenBracket(xtmodel.greetings, document)
	}

	def void alignGreetingsOnOpenBracket(EList<XTGreeting> greetings, extension IFormattableDocument document) {
		val precedingString = "startList ("
		for (greeting : greetings) {
			greeting.prepend[space = Strings.repeat(" ", precedingString .length); highPriority]
		}
	}


What happens is:
startList (
list A : endlist;
list B : endlist;
list C : endlist;
);


While I expect:
startList (
           list A : endlist;
           list B : endlist;
           list C : endlist;
);


Why does append[newLine] result in ignoring any spaces added after (newLine = newLine and spaces ignored)?

I know that I can use interior[indent], but that would result in indenting using whatever tab width is set, which is not necessarily going to match the width of "startList (".

[Updated on: Sun, 24 February 2019 10:39]

Report message to a moderator

Re: [Formatter2] append newLine + spaces [message #1803055 is a reply to message #1803049] Thu, 21 February 2019 14:07 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 13062
Registered: July 2009
Senior Member
Please use the indent feature to implement
Indentations


Need professional support for Xtext, Xpand, EMF?
Go to: https://www.itemis.com/en/xtext/
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: [Formatter2] append newLine + spaces [message #1803161 is a reply to message #1803055] Sun, 24 February 2019 10:37 Go to previous messageGo to next message
ayman salah is currently offline ayman salahFriend
Messages: 130
Registered: June 2015
Senior Member
I know that I am supposed to use the indent feature.
But the indent feature uses the tab width set by the user.

I want to be able to set the indent width for each type of list to match the (list type + space + open bracket) width.

For example:
greetingsList (
               greeting g1;
               greeting g2;
);


asdList (
         asd a1;
         asd a2;
);


You get me?

[Updated on: Sun, 24 February 2019 10:38]

Report message to a moderator

Re: [Formatter2] append newLine + spaces [message #1803162 is a reply to message #1803161] Sun, 24 February 2019 10:44 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 13062
Registered: July 2009
Senior Member
did you have a look at the example here:

https://de.slideshare.net/meysholdt/xtexts-new-formatter-api

(Page 139)


Need professional support for Xtext, Xpand, EMF?
Go to: https://www.itemis.com/en/xtext/
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de

[Updated on: Sun, 24 February 2019 11:08]

Report message to a moderator

Re: [Formatter2] append newLine + spaces [message #1803165 is a reply to message #1803162] Sun, 24 February 2019 13:45 Go to previous messageGo to next message
ayman salah is currently offline ayman salahFriend
Messages: 130
Registered: June 2015
Senior Member
Well, yes I have.

But that's not what I am talking about I guess.

If I understand correctly, the slide you are talking about adjusts the space width before the '=>' not before each transition (which is what I need).

The slide controls this part (*):
state baz
     1***=> foo;
     12**=> foo;
     123*=> foo;
end


I want to control this part (*):
state baz
*****1   => foo;
*****12  => foo;
*****123 => foo;
end


Let's say my grammar has states and has lists containing transitions. I would like to have the following result:
state baz
*****1   => foo;
*****12  => foo;
*****123 => foo;
end
list bar
****1   => foo;
****12  => foo;
****123 => foo;
end


Am I clear?
Re: [Formatter2] append newLine + spaces [message #1803170 is a reply to message #1803165] Sun, 24 February 2019 15:45 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 13062
Registered: July 2009
Senior Member
this looks like a bug to me.

workaround:

def dispatch void format(XTModel xTModel, extension IFormattableDocument document) {
for (xTGreeting : xTModel.greetings) {
xTGreeting.regionFor.keyword("list").prepend[space = "\n "]
}
}

alternative solution:
https://stackoverflow.com/questions/45305293/how-to-define-different-indentation-levels-in-the-same-document-with-xtext-forma

can you please create a ticket at github.com/eclipse/xtext-core


Need professional support for Xtext, Xpand, EMF?
Go to: https://www.itemis.com/en/xtext/
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: [Formatter2] append newLine + spaces [message #1805252 is a reply to message #1803170] Wed, 10 April 2019 12:23 Go to previous message
ayman salah is currently offline ayman salahFriend
Messages: 130
Registered: June 2015
Senior Member
Done here https://github.com/eclipse/xtext-core/issues/1069.
Previous Topic:Formatter is triggered on quickfix
Next Topic:Breakpoint in outside workspace model file
Goto Forum:
  


Current Time: Mon Aug 19 07:36:59 GMT 2019

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

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

Back to the top