ocaml-ppx / ocamlformat

Auto-formatter for OCaml code
MIT License
636 stars 179 forks source link

Indentation of `fun` arguments is inconsistent #2397

Open Julow opened 1 year ago

Julow commented 1 year ago

Function arguments are indented in different ways depending on context:

let _ =
 fun (module Store : Irmin.Generic_key.S with type repo = repo)
     (module Store : Irmin.Generic_key.S with type repo = repo) ->
  body

let _ =
  f
    (fun
      (module Store : Irmin.Generic_key.S with type repo = repo)
      (module Store : Irmin.Generic_key.S with type repo = repo)
      -> body )

let f (module Store : Irmin.Generic_key.S with type repo = repo)
    (module Store : Irmin.Generic_key.S with type repo = repo) =
  body

This example contains a regression: (red is 0.25.1)

@@ -8,7 +8,7 @@ let _ =
     (fun
       (module Store : Irmin.Generic_key.S with type repo = repo)
       (module Store : Irmin.Generic_key.S with type repo = repo)
-    -> body )
+      -> body )

 let f (module Store : Irmin.Generic_key.S with type repo = repo)
     (module Store : Irmin.Generic_key.S with type repo = repo) =

I propose the following formatting:

let _ = fun
    (module Store : Irmin.Generic_key.S with type repo = repo)
    (module Store : Irmin.Generic_key.S with type repo = repo) ->
  body

let _ =
  f
    (fun
       (module Store : Irmin.Generic_key.S with type repo = repo)
       (module Store : Irmin.Generic_key.S with type repo = repo) ->
     body )

let f
    (module Store : Irmin.Generic_key.S with type repo = repo)
    (module Store : Irmin.Generic_key.S with type repo = repo) =
  body
Julow commented 1 year ago

The regression I mention is fixed by https://github.com/ocaml-ppx/ocamlformat/pull/2398 The indentation of the arguments of a fun that precedes a label has to follow partly the proposed indentation:

let () =
  very_long_function_name
    ~very_long_argument_label:(fun
        very_long_argument_name_one
        very_long_argument_name_two
        very_long_argument_name_three
      -> () )

let () =
  very_long_function_name
    ~very_long_argument_label:(* foo *)
      (fun
        very_long_argument_name_one
        very_long_argument_name_two
        very_long_argument_name_three
      -> () )