Skip to main content



      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 05:36 Go to next message
Eclipse UserFriend
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 08:42 Go to previous messageGo to next message
Eclipse UserFriend
maybe something like

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

(add. to newlines) will help
Re: Formatting comma-separated list with new line & indent [message #1849600 is a reply to message #1849596] Wed, 26 January 2022 09:37 Go to previous messageGo to next message
Eclipse UserFriend
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 10:27 Go to previous message
Eclipse UserFriend
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: Wed Jul 23 21:27:49 EDT 2025

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

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

Back to the top