Skip to main content



      Home
Home » Modeling » TMF (Xtext) » How to implement Formatter maxLineWidth?
How to implement Formatter maxLineWidth? [message #1857027] Fri, 13 January 2023 09:19 Go to next message
Eclipse UserFriend
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 09:43 Go to previous messageGo to next message
Eclipse UserFriend
Did you check the slides on slideshare on the formatter I assume it has some hints on this as well as your other question
Re: How to implement Formatter maxLineWidth? [message #1857030 is a reply to message #1857029] Fri, 13 January 2023 09:53 Go to previous messageGo to next message
Eclipse UserFriend
https://www.slideshare.net/meysholdt/xtexts-new-formatter-api
Eg slide 132
Re: How to implement Formatter maxLineWidth? [message #1857032 is a reply to message #1857030] Fri, 13 January 2023 10:44 Go to previous messageGo to next message
Eclipse UserFriend
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 03:42 Go to previous messageGo to next message
Eclipse UserFriend
@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 08:13 Go to previous messageGo to next message
Eclipse UserFriend
yes this is oudated and replaced by org.eclipse.xtext.testing.formatter.FormatterTestHelper
Re: How to implement Formatter maxLineWidth? [message #1857084 is a reply to message #1857079] Mon, 16 January 2023 11:07 Go to previous message
Eclipse UserFriend
Thanks!
Previous Topic:Formatting: respect newlines
Next Topic:Import a dsl language which is not present in the same workspace
Goto Forum:
  


Current Time: Wed Apr 30 17:06:38 EDT 2025

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

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

Back to the top