Formatting comma-separated list with new line & indent [message #1849586] |
Wed, 26 January 2022 05:36  |
Eclipse User |
|
|
|
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:
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 #1849603 is a reply to message #1849596] |
Wed, 26 January 2022 10:27  |
Eclipse User |
|
|
|
Thanks Christian,
the part 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]
]
|
|
|
Powered by
FUDForum. Page generated in 0.25004 seconds