ocamlformat icon indicating copy to clipboard operation
ocamlformat copied to clipboard

Bug: infix operator splitting fails with anonymous functions

Open craigfe opened this issue 6 years ago • 5 comments

Describe the bug break-infix = fit-or-vertical not respected when the expression after the infix operator is a fun or a function. Expected behaviour of this option:

let () =
  aaaaaaaaaaaaaaaaaaaaaaaaaaaa
  |> bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
  |> cccccc
  |> ddddddddd

but for anonymous functions, the infix operators are not broken onto separate lines:

let () =
  aaaaaaaaaaaaaaaaaaaaaaaaaaaa |> bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb |> cccccc
  |> fun x -> ddddddddddd

let () =
  aaaaaaaaaaaaaaaaaaaaaaaaaaaa |> bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb |> cccccc
  |> function
  | x -> ddddddddddd

How to Reproduce break-infix = fit-or-vertical

craigfe avatar Oct 23 '19 13:10 craigfe

I'm looking at it but the interaction with break-infix-before-func=false (which is set to false by the conventional profile) is unclear:

for break-infix-before-func=false and break-infix=fit-or-vertical, do we want:

let _ =
  aaaaaaaaaaaaaaaa
  |> bbbbbbbbbbbbbbbb
  |> ccccccccccccccccc
  |> fun x -> ddddddddddddddd

or

let _ =
  aaaaaaaaaaaaaaaa
  |> bbbbbbbbbbbbbbbb
  |> ccccccccccccccccc |> fun x ->
  ddddddddddddddd

the last one being the closer to the expected behavior of break-infix-before-func=false:

let _ =
   aaaaaaaaaaaa >>= fun x ->
   bbbbbbbbbbbbbbbb >|= fun y ->
   ccccccccccccccccc

gpetiot avatar Nov 06 '19 09:11 gpetiot

I think the second one respects the intent of break-infix-before-func (which I didn't know about), so I recommend that :slightly_smiling_face:

Personally, I'd rather have the first one for consistency (so looks like I'm going to be setting break-infix-before-func=true from now on.

craigfe avatar Nov 06 '19 10:11 craigfe

This gets formatted as:

let () =
  aaaaaaaaaaaaaaaaaaaaaaaaaaaa
  |> bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
  |> cccccc
  |> fun x -> ddddddddddd

let () =
  aaaaaaaaaaaaaaaaaaaaaaaaaaaa
  |> bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
  |> cccccc
  |> function x -> ddddddddddd

shall we close this issue or did you witness it since then?

gpetiot avatar Dec 02 '20 16:12 gpetiot

Hmm, I don't observe the formatting that you describe.

Here's a branch that adds a test case containing my examples above, and I believe it demonstrates that the issue still exists: https://github.com/CraigFe/ocamlformat/tree/repro-issue-1094

craigfe avatar Dec 02 '20 17:12 craigfe

Ah yes my bad, my config was applying profile=ocamlformat which sets --break-infix-before-func

gpetiot avatar Dec 02 '20 17:12 gpetiot