@@ -847,8 +847,8 @@ type if_then_else =
847847 ; break_end_branch : Fmt .t
848848 ; space_between_branches : Fmt .t }
849849
850- let get_if_then_else (c : Conf.t ) ~pro ~first ~last ~ parens_bch
851- ~parens_prev_bch ~xcond ~xbch ~expr_loc ~fmt_infix_ext_attrs
850+ let get_if_then_else (c : Conf.t ) ~cmts_before_opt ~ pro ~first ~last
851+ ~parens_bch ~ parens_prev_bch ~xcond ~xbch ~expr_loc ~fmt_infix_ext_attrs
852852 ~infix_ext_attrs ~fmt_cond ~cmts_before_kw ~cmts_after_kw =
853853 let imd = c.fmt_opts.indicate_multiline_delimiters.v in
854854 let beginend_loc, infix_ext_attrs_beginend, branch_expr =
@@ -889,10 +889,10 @@ let get_if_then_else (c : Conf.t) ~pro ~first ~last ~parens_bch
889889 | `Closing_on_separate_line ->
890890 wrap (brk (0 , opn_hint_indent)) (brk ch_sl)
891891 in
892- let cond () =
892+ let cond ?( box = hvbox 2 ) () =
893893 match xcond with
894894 | Some xcnd ->
895- hvbox 2
895+ box
896896 ( hvbox 0
897897 ( hvbox 2
898898 ( pro
@@ -902,12 +902,21 @@ let get_if_then_else (c : Conf.t) ~pro ~first ~last ~parens_bch
902902 $ space_break $ cmts_before_kw $ str " then" )
903903 $ opt cmts_after_kw Fn. id )
904904 | None ->
905- cmts_before_kw $ hvbox 2 (pro $ str " else" $ opt cmts_after_kw Fn. id)
905+ cmts_before_kw $ box (pro $ str " else" $ opt cmts_after_kw Fn. id)
906906 in
907- let branch_pro ?(indent = 2 ) () =
908- if Option. is_some cmts_after_kw then break 1000 indent
909- else if has_beginend || parens_bch then str " "
910- else break 1 indent
907+ let has_cmts_after_kw = Option. is_some cmts_after_kw in
908+ let branch_pro ?(begin_end_offset = 2 ) ?(indent = 2 )
909+ ?(break_before_cmts = break 1000 indent) () =
910+ match beginend_loc with
911+ | Some loc -> (
912+ match cmts_before_opt loc with
913+ | Some cmts -> break_before_cmts $ cmts $ break 1000 ~- begin_end_offset
914+ | None ->
915+ if has_cmts_after_kw then break 1000 ~- begin_end_offset
916+ else str " " )
917+ | None when has_cmts_after_kw -> break 1000 indent
918+ | None when parens_bch -> str " "
919+ | None -> break 1 indent
911920 in
912921 match c.fmt_opts.if_then_else.v with
913922 | `Compact ->
@@ -931,7 +940,7 @@ let get_if_then_else (c : Conf.t) ~pro ~first ~last ~parens_bch
931940 { box_branch= Fn. id
932941 ; cond= cond ()
933942 ; box_keyword_and_expr= Fn. id
934- ; branch_pro= branch_pro ()
943+ ; branch_pro= branch_pro ~begin_end_offset: 0 ()
935944 ; wrap_parens= wrap_parens ~wrap_breaks: (wrap (break 1000 2 ) noop)
936945 ; beginend_loc
937946 ; box_expr= Some has_beginend
@@ -950,7 +959,7 @@ let get_if_then_else (c : Conf.t) ~pro ~first ~last ~parens_bch
950959 | _ -> 0 )
951960 ; cond= cond ()
952961 ; box_keyword_and_expr= Fn. id
953- ; branch_pro= branch_pro ()
962+ ; branch_pro= branch_pro ~begin_end_offset: 0 ()
954963 ; wrap_parens=
955964 wrap_parens
956965 ~wrap_breaks:
@@ -975,7 +984,7 @@ let get_if_then_else (c : Conf.t) ~pro ~first ~last ~parens_bch
975984 { box_branch= Fn. id
976985 ; cond= cond ()
977986 ; box_keyword_and_expr= Fn. id
978- ; branch_pro= branch_pro ()
987+ ; branch_pro= branch_pro ~begin_end_offset: 0 ()
979988 ; wrap_parens=
980989 wrap_parens
981990 ~wrap_breaks:
@@ -1010,7 +1019,7 @@ let get_if_then_else (c : Conf.t) ~pro ~first ~last ~parens_bch
10101019 { box_branch= Fn. id
10111020 ; cond
10121021 ; box_keyword_and_expr= (fun k -> hovbox 2 (keyword $ k))
1013- ; branch_pro= branch_pro ~indent: 0 ()
1022+ ; branch_pro= branch_pro ~break_before_cmts: (str " " ) ~ indent:0 ()
10141023 ; wrap_parens=
10151024 wrap_parens
10161025 ~wrap_breaks:
0 commit comments