Skip to content

Commit

Permalink
Dock indent a fun passed as a labelled argument (#2271)
Browse files Browse the repository at this point in the history
Avoid adding breaks inside '~label:(fun' and base the indentation on the
label. This is similar to what we've done to functions in 92ba086.

Some care is needed for comments between the ':' and the '(fun'
  • Loading branch information
Julow authored Mar 9, 2023
1 parent 9167713 commit 869ea50
Show file tree
Hide file tree
Showing 8 changed files with 76 additions and 8 deletions.
2 changes: 2 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

### Bug fixes

- Avoid adding breaks inside `~label:(fun` and base the indentation on the label. (#2271, @Julow)

### Changes

### New features
Expand Down
4 changes: 2 additions & 2 deletions lib/Conf_decl.ml
Original file line number Diff line number Diff line change
Expand Up @@ -438,8 +438,8 @@ let removed_option ~names ~since ~msg =
let update store ~config ~from:new_from ~name ~value ~inline =
List.find_map store
~f:(fun
(Pack {names; parse; update; allow_inline; get_value; to_string; _})
->
(Pack {names; parse; update; allow_inline; get_value; to_string; _})
->
if List.exists names ~f:(String.equal name) then
if inline && not allow_inline then
Some (Error (Error.Misplaced (name, value)))
Expand Down
18 changes: 12 additions & 6 deletions lib/Fmt_ast.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1822,9 +1822,13 @@ and fmt_expression c ?(box = true) ?pro ?epi ?eol ?parens ?(indent_wrap = 0)
when List.for_all rev_e1N ~f:(fun (_, eI) ->
is_simple c.conf (fun _ -> 0) (sub_exp ~ctx eI) ) ->
let e1N = List.rev rev_e1N in
(* side effects of Cmts.fmt c.cmts before Sugar.fun_ is
important *)
let cmts_before = Cmts.fmt_before c pexp_loc in
(* Make sure the comment is placed after the eventual label but not
into the inner box if no label is present. Side effects of
Cmts.fmt c.cmts before Sugar.fun_ is important. *)
let cmts_outer, cmts_inner =
let cmt = Cmts.fmt_before c pexp_loc in
match lbl with Nolabel -> (cmt, noop) | _ -> (noop, cmt)
in
let xargs, xbody = Sugar.fun_ c.cmts (sub_exp ~ctx eN1) in
let fmt_cstr, xbody = type_constr_and_body c xbody in
let box =
Expand All @@ -1842,11 +1846,13 @@ and fmt_expression c ?(box = true) ?pro ?epi ?eol ?parens ?(indent_wrap = 0)
(hovbox 0
( hovbox 2
( wrap
( fmt_args_grouped e0 e1N $ fmt "@ "
$ fmt_label lbl ":" $ cmts_before
( fmt_args_grouped e0 e1N $ fmt "@ " $ cmts_outer
$ hvbox 0
( hvbox 2
( fmt "(fun@ "
( hvbox 0
( fmt_label lbl ":" $ cmts_inner
$ fmt "(fun" )
$ fmt "@ "
$ fmt_attributes c eN1.pexp_attributes
~suf:" "
$ fmt_fun_args c xargs $ fmt_opt fmt_cstr
Expand Down
8 changes: 8 additions & 0 deletions test/passing/tests/js_source.ml
Original file line number Diff line number Diff line change
Expand Up @@ -7642,3 +7642,11 @@ let bind t ~f =
| Yield { value = a; state = s } ->
Yield { value = a; state = Sequence { state = s; next }, rest }))
~init:(empty, t)

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
-> () )
9 changes: 9 additions & 0 deletions test/passing/tests/js_source.ml.ocp
Original file line number Diff line number Diff line change
Expand Up @@ -9865,3 +9865,12 @@ let bind t ~f =
Yield { value = a; state = Sequence { state = s; next }, rest }))
~init:(empty, t)
;;

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
-> ())
;;
9 changes: 9 additions & 0 deletions test/passing/tests/js_source.ml.ref
Original file line number Diff line number Diff line change
Expand Up @@ -9865,3 +9865,12 @@ let bind t ~f =
Yield { value = a; state = Sequence { state = s; next }, rest }))
~init:(empty, t)
;;

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
-> ())
;;
17 changes: 17 additions & 0 deletions test/passing/tests/labelled_args-414.ml.ref
Original file line number Diff line number Diff line change
@@ -1,3 +1,20 @@
let _ =
let f ~y = y + 1 in
f ~(y : int)

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
-> () )
17 changes: 17 additions & 0 deletions test/passing/tests/labelled_args.ml
Original file line number Diff line number Diff line change
@@ -1,3 +1,20 @@
let _ =
let f ~y = y + 1 in
f ~y:(y : int)

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
-> () )

0 comments on commit 869ea50

Please sign in to comment.