ocamlformat icon indicating copy to clipboard operation
ocamlformat copied to clipboard

Linebreaks in function formatting

Open jfeser opened this issue 7 years ago • 4 comments

I have some code which looks like this:

let myfunc : int -> int = fun x ->
  let y = x + 1 in
  y

It gets reformatted to:

let myfunc : int -> int =
 fun x ->
  let y = x + 1 in
  y

It'd be nice to avoid the unnecessary line break after the equals. I can take a shot at fixing this but I'd need some pointers on where to look in the code.

jfeser avatar Jun 14 '18 19:06 jfeser

This code is formatted by Fmt_ast.fmt_value_binding which itself produces the code from let to =, and then uses fmt_expression to produce from fun onward. To achieve what you suggest, I think the easiest approach is to add a special case to fmt_value_binding which looks at the body of such bindings with type constraints and splits the Pexp_fun into the function arguments and body, analogously to how it is currently done for the case where there are no type constraints.

jberdine avatar Jul 25 '18 14:07 jberdine

Easier after #406

jberdine avatar Sep 08 '18 14:09 jberdine

I too have a bunch of code that looks like @jfeser's example, and would like this feature. Out of curiosity, what makes issues like this and #364 problematic, while --break-separators=after-and-docked is not so?

enaudon avatar Aug 24 '19 23:08 enaudon

I'm not sure the fix for this one would be radically different but I don't have the time to look at this one right now, I'm working on the bugs reported lately these days.

gpetiot avatar Aug 26 '19 04:08 gpetiot