[ANN] Smart linebreaks for pipe chains

As you may know, ReScript’s formatter has no configuration possibilities. ReScript code should look very similar no matter who wrote it.

However, with line length as the main constraint, you often end up with funny formatting. That’s why @Maxim implemented “smart printing” for records and variants when he wrote the ReScript parser to give the user at least a little bit of control.

If you have a variant type

type animal = Dog | Cat | Mouse

and put a newline anywhere in between the variants

type animal = Dog | Cat 
| Mouse

the formatter will yield

type animal =
  | Dog
  | Cat
  | Mouse

Similarly, if you have a record (type or value)

type dog = {name: string, age: int, weight: int}

and put a newline anywhere in between the attributes

type dog = {name: string, 
age: int, weight: int}

the formatter will yield

type dog = {
  name: string,
  age: int,
  weight: int,
}

New: Smart linebreaks for pipes

We are happy to announce that the same logic is applied to pipe-chains with the -> operator now!

For instance, if you have a pipe-chain like this one

let _ = "P"->String.concat("I")->String.concat("P")->String.concat("E")

and put a newline anywhere in between the pipes

let _ = "P"->String.concat("I")
->String.concat("P")->String.concat("E")

the formatter will yield

let _ =
  "P"
  ->String.concat("I")
  ->String.concat("P")
  ->String.concat("E")

All of the above examples that are one-liners stay one-liners (since they do not hit the 100 character threshold).

Please enjoy!

20 Likes

Thanks go out to @Maxim for the idea of smart printing and @aspeddro for giving me a good pointer where to implement this with his initial pipe-printing PR.

3 Likes

This is a great addition that people have been asking for for a long time. Thanks for building it @fham !

3 Likes

YESSS. Thanks so much!

1 Like

Oh my goodness this is such a big deal, thanks!

Is this live already or is it a V11 thing? Also, does it work with JSX?

2 Likes

Smart printing of everything that are not pipe-chains is in the compiler since V9 I think. The pipe-chain change will be in the next V11 release candidate. I did not touch JSX formatting though.