Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Formatting comma-separated list with new line & indent
Formatting comma-separated list with new line & indent [message #1849586] Wed, 26 January 2022 10:36 Go to next message
Carl Weathers is currently offline Carl WeathersFriend
Messages: 2
Registered: January 2022
Junior Member
Hi,

I'm trying to write a formatter for a grammar rule like this:

Block: 
	{Block} 'Block'
		defs+=Def (',' defs+=Def)*
;

Def: 
	name=ID
;


I would like to have this formatted like so:

Block
	Alpha,
	Beta,
	Gamma


All the Defs should be on a new line, and indented.

I can get it to work if my syntax for the Block rule has a "end" token, using the formatter's "interior" API (I also didn't have comma separators back then). But now I cannot make it work.

Here is an example of many I tried which I would assume should work, but doesn't:
	def dispatch void format(Block block, extension IFormattableDocument document) {
		block.regionFor.keyword("Block").append[newLine]
		block.defs.tail.forEach [ def | def.prepend[newLine]]
		block.defs.forEach[def|def.prepend[indent]]
	}


What this gives me is:
Block
Alpha ,
Beta ,
Gamma


So, no indent, and also a whitespace before the comma I would like to remove if possible.
Re: Formatting comma-separated list with new line & indent [message #1849596 is a reply to message #1849586] Wed, 26 January 2022 13:42 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14353
Registered: July 2009
Senior Member
maybe something like

[code]
block.defs.forEach [ def |
def.surround[indent]
def.regionFor.keyword(",").prepend[noSpace]
]
[code]

(add. to newlines) will help


Need professional support for Xtext, Xpand, EMF?
Go to: https://www.itemis.com/en/it-services/methods-and-tools/xtext
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Formatting comma-separated list with new line & indent [message #1849600 is a reply to message #1849596] Wed, 26 January 2022 14:37 Go to previous messageGo to next message
Tamas Miklossy is currently offline Tamas MiklossyFriend
Messages: 146
Registered: February 2016
Senior Member
Hello Carl,

You may get some inspirations from the following forum threads:

https://www.eclipse.org/forums/index.php/t/1103866/
https://www.eclipse.org/forums/index.php/t/1106697/

Hope that helps,
Tamás
Re: Formatting comma-separated list with new line & indent [message #1849603 is a reply to message #1849596] Wed, 26 January 2022 15:27 Go to previous message
Carl Weathers is currently offline Carl WeathersFriend
Messages: 2
Registered: January 2022
Junior Member
Thanks Christian,

the part
def.surround[indent]
is actually getting the indent right :)

The part concerning the comma can't work cause the keyword is not part of the def, IIUC, but part of the Block rule definition, so this one has no effect.
Thanks to your input, I was able to find this solution that works for me, though:

		block.defs.forEach [ def |
			def.prepend[newLine]
			def.surround[indent]
			def.append[noSpace]
		]
Previous Topic:Position of Comments in a Line
Next Topic:How to prevent a dsl file to be built if it is not in the classpath
Goto Forum:
  


Current Time: Mon Oct 03 02:11:59 GMT 2022

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

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

Back to the top