Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » How to implement Formatter maxLineWidth?
How to implement Formatter maxLineWidth? [message #1857027] Fri, 13 January 2023 14:19 Go to next message
Simon Cockx is currently offline Simon CockxFriend
Messages: 66
Registered: October 2021
Member
In the documentation of FormatterPreferenceKeys, it says "Formatters based on this infrastructure should honor these keys as well."

One of the keys is "maxLineWidth".

Are there any examples or guidelines on how to take this property into account?

Example: suppose we have an expression language with an expression such as
if compute(["this", "is", "a", "very", "long", "expression"]) < 10 then RunA(compute(["this", "is", "short"]) > 100) else RunB(compute(["this", "is", "a", "very", "long", "expression"]) > compute(["this", "is", "an", "even", "longer", "expression"]))


I would like to accept this expression as-is if it doesn't exceed the maxLineWidth, otherwise, I would like to reformat it to
if compute(["this", "is", "a", "very", "long", "expression"]) < 10
then RunA(compute(["this", "is", "short"]) < 5)
else RunB(
    compute(["this", "is", "a", "very", "long", "expression"]) > compute(["this", "is", "an", "even", "longer", "expression"])
)


In pseudo code:
(1) if an if-then-else expression exceeds the maxLineWidth, put the condition, the "then"-branch and the "else" branch on different lines.
(2) if a function call exceeds maxLineWidth, put its arguments on a separate line.

Note that this should run incrementally: in the example, the expression `RunA(compute(["this", "is", "short"]) < 5)` is initially exceeding maxLineWidth, but after putting the if, then and else on separate lines, that's not the case anymore.

Is something like this possible to express? Or how is this kind of thing usually done? I can't seem to find a good resource illustrating this.
Re: How to implement Formatter maxLineWidth? [message #1857029 is a reply to message #1857027] Fri, 13 January 2023 14:43 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14588
Registered: July 2009
Senior Member
Did you check the slides on slideshare on the formatter I assume it has some hints on this as well as your other question

Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: How to implement Formatter maxLineWidth? [message #1857030 is a reply to message #1857029] Fri, 13 January 2023 14:53 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14588
Registered: July 2009
Senior Member
https://www.slideshare.net/meysholdt/xtexts-new-formatter-api
Eg slide 132


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: How to implement Formatter maxLineWidth? [message #1857032 is a reply to message #1857030] Fri, 13 January 2023 15:44 Go to previous messageGo to next message
Simon Cockx is currently offline Simon CockxFriend
Messages: 66
Registered: October 2021
Member
Just watched it completely. Now I'm in awe of the formatter API; it's not every day that I encounter something that well designed. Thanks for the pointer, that's exactly what I needed!

`formatConditionally` is indeed gonna do the trick.
Re: How to implement Formatter maxLineWidth? [message #1857071 is a reply to message #1857032] Mon, 16 January 2023 08:42 Go to previous messageGo to next message
Simon Cockx is currently offline Simon CockxFriend
Messages: 66
Registered: October 2021
Member
@Christian, the slides mention a class `org.eclipse.xtext.junit4.formatter.FormatterTester` which can help with testing. This is out-dated right? Is there a new helper class, or should I implement something myself?
Re: How to implement Formatter maxLineWidth? [message #1857079 is a reply to message #1857071] Mon, 16 January 2023 13:13 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14588
Registered: July 2009
Senior Member
yes this is oudated and replaced by org.eclipse.xtext.testing.formatter.FormatterTestHelper

Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: How to implement Formatter maxLineWidth? [message #1857084 is a reply to message #1857079] Mon, 16 January 2023 16:07 Go to previous message
Simon Cockx is currently offline Simon CockxFriend
Messages: 66
Registered: October 2021
Member
Thanks!
Previous Topic:Formatting: respect newlines
Next Topic:Import a dsl language which is not present in the same workspace
Goto Forum:
  


Current Time: Sat Dec 02 06:43:14 GMT 2023

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

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

Back to the top