tweag / topiary

https://topiary.tweag.io/
MIT License
575 stars 29 forks source link

Function dangling on multi-line call looks weird #731

Closed Niols closed 1 month ago

Niols commented 2 months ago

As of https://github.com/tweag/topiary/commit/1720fa963fdf65e9c767edbe02132ccab4078f5a, Topiary formats:

foo bar baz @@ fun x ->
x
;;
foo
  bar
  baz
  @@ fun x ->
  x

as

foo bar baz @@ fun x ->
x
;;
foo
  bar
  baz @@ fun x ->
x

The single-line one makes sense, I believe, but the multi-line one is just wrong. I do not really have a better solution, though. Tuareg/ocp-indent indent as:

foo
  bar
  baz @@ fun x ->
x

and

foo
  bar
  baz
@@ fun x ->
x

I am thinking maybe the latter is the best way to go?

Related to https://github.com/tweag/topiary/issues/726

yannham commented 2 months ago

Out of curiosity, how would be formatted the same examples if you changed @@ RHS by (RHS) ? I find the fact that the content of the function isn't indented to be a bit strange, but if ocp-indent does it, I guess there's a rationale (maybe chains of @@ where you don't want to indent each function to look like JS' callback hell?)

Niols commented 2 months ago

Yes, it is rather classic in OCaml to write something like

a_function @@ fun x ->
another_function @@ fun y ->
(x + y) / 2

(and similarly with >>=) However, it is not super obvious how to have this style and proper multi-line support. With parentheses around the right-hand side, this looks like:

foo bar baz (fun x ->
  x
)
;;
foo
  bar
  baz
(fun x ->
  x
)

Related issue: https://github.com/tweag/topiary/issues/726