Skip to content

Commit acadecf

Browse files
begin if bugfix with fit-or-vertical (#2673)
1 parent 2ed2f82 commit acadecf

File tree

10 files changed

+450
-8
lines changed

10 files changed

+450
-8
lines changed

lib/Fmt_ast.ml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2426,7 +2426,8 @@ and fmt_expression c ?(box = true) ?(pro = noop) ?eol ?parens
24262426
else None
24272427
in
24282428
let p =
2429-
Params.get_if_then_else c.conf ~first ~last
2429+
Params.get_if_then_else c.conf
2430+
~pro:(fmt_if first pro_inner) ~first ~last
24302431
~parens_bch ~parens_prev_bch:!parens_prev_bch
24312432
~xcond ~xbch ~expr_loc:pexp_loc
24322433
~fmt_extension_suffix:
@@ -2439,7 +2440,7 @@ and fmt_expression c ?(box = true) ?(pro = noop) ?eol ?parens
24392440
in
24402441
parens_prev_bch := parens_bch ;
24412442
p.box_branch
2442-
( fmt_if first pro_inner $ p.cond
2443+
( p.cond
24432444
$ p.box_keyword_and_expr
24442445
( p.branch_pro
24452446
$ p.wrap_parens

lib/Params.ml

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -769,9 +769,9 @@ type if_then_else =
769769
; break_end_branch: Fmt.t
770770
; space_between_branches: Fmt.t }
771771

772-
let get_if_then_else (c : Conf.t) ~first ~last ~parens_bch ~parens_prev_bch
773-
~xcond ~xbch ~expr_loc ~fmt_extension_suffix ~fmt_attributes ~fmt_cond
774-
~cmts_before_kw ~cmts_after_kw =
772+
let get_if_then_else (c : Conf.t) ~pro ~first ~last ~parens_bch
773+
~parens_prev_bch ~xcond ~xbch ~expr_loc ~fmt_extension_suffix
774+
~fmt_attributes ~fmt_cond ~cmts_before_kw ~cmts_after_kw =
775775
let imd = c.fmt_opts.indicate_multiline_delimiters.v in
776776
let beginend, branch_expr =
777777
let ast = xbch.Ast.ast in
@@ -803,13 +803,15 @@ let get_if_then_else (c : Conf.t) ~first ~last ~parens_bch ~parens_prev_bch
803803
hvbox 2
804804
( hvbox 0
805805
( hvbox 2
806-
( fmt_if (not first) (str "else ")
806+
( pro
807+
$ fmt_if (not first) (str "else ")
807808
$ str "if"
808809
$ fmt_if first (fmt_opt fmt_extension_suffix)
809810
$ fmt_attributes $ space_break $ fmt_cond xcnd )
810811
$ space_break $ cmts_before_kw $ str "then" )
811812
$ opt cmts_after_kw Fn.id )
812-
| None -> cmts_before_kw $ hvbox 2 (str "else" $ opt cmts_after_kw Fn.id)
813+
| None ->
814+
cmts_before_kw $ hvbox 2 (pro $ str "else" $ opt cmts_after_kw Fn.id)
813815
in
814816
let branch_pro ?(indent = 2) () =
815817
if Option.is_some cmts_after_kw then break 1000 indent
@@ -901,7 +903,8 @@ let get_if_then_else (c : Conf.t) ~first ~last ~parens_bch ~parens_prev_bch
901903
match xcond with
902904
| Some xcond ->
903905
hvbox 2
904-
( fmt_or first
906+
( pro
907+
$ fmt_or first
905908
(str "if" $ fmt_opt fmt_extension_suffix)
906909
(str "else if")
907910
$ fmt_attributes $ space_break $ fmt_cond xcond

lib/Params.mli

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,7 @@ type if_then_else =
195195

196196
val get_if_then_else :
197197
Conf.t
198+
-> pro:Fmt.t
198199
-> first:bool
199200
-> last:bool
200201
-> parens_bch:bool

test/passing/refs.default/exp_grouping-parens.ml.ref

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -417,3 +417,60 @@ let _ =
417417
(fun xxxxxxxxxxxxxxxxxxxxxxx yyyyyyyyyyyyyyyyyyy zzzzzzzzzzzzzzzzzzzzzzzz ->
418418
print_endline xxxxxxxxx;
419419
f xxxxxxxxxx yyyyyyyyyyyyy zzzzzzzzzzzzzzzzzzzzz)
420+
421+
[@@@ocamlformat "if-then-else=fit-or-vertical"]
422+
423+
let _ =
424+
if b then (
425+
e1;
426+
e2)
427+
else (
428+
something loooooooooooooooooooooooooooooooong enough to_trigger a break;
429+
this is more)
430+
431+
let _ =
432+
if b then (
433+
something loooooooooooooooooooooooooooooooong enough to_trigger a break;
434+
this is more)
435+
else if b1 then (
436+
something loooooooooooooooooooooooooooooooong enough to_trigger a break;
437+
this is more)
438+
else
439+
e
440+
;;
441+
442+
f
443+
(if loooooooooooooooooooooooooooooooooooooooooooooooooooooooooong then
444+
()
445+
else
446+
())
447+
;;
448+
449+
f
450+
(if loooooooooooooooooooooooooooooooooooooooooooooooooooooooooonger then
451+
()
452+
else
453+
())
454+
;;
455+
456+
f
457+
(if even loooooooooooooooooooooooooooooooooooooooooooooooooooooooooonger then
458+
()
459+
else
460+
())
461+
;;
462+
463+
f
464+
(if and_ even loooooooooooooooooooooooooooooooooooooooooooooooooooooooooonger
465+
then
466+
()
467+
else
468+
())
469+
470+
let () =
471+
f
472+
(if a___________________________________________________________________
473+
then
474+
b_________________________________________________________________
475+
else
476+
c_________________________________________________________________)

test/passing/refs.default/exp_grouping.ml.ref

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -483,3 +483,72 @@ let _ =
483483
print_endline xxxxxxxxx;
484484
f xxxxxxxxxx yyyyyyyyyyyyy zzzzzzzzzzzzzzzzzzzzz
485485
end
486+
487+
[@@@ocamlformat "if-then-else=fit-or-vertical"]
488+
489+
let _ =
490+
if b then begin
491+
e1;
492+
e2
493+
end
494+
else begin
495+
something loooooooooooooooooooooooooooooooong enough to_trigger a break;
496+
this is more
497+
end
498+
499+
let _ =
500+
if b then begin
501+
something loooooooooooooooooooooooooooooooong enough to_trigger a break;
502+
this is more
503+
end
504+
else if b1 then begin
505+
something loooooooooooooooooooooooooooooooong enough to_trigger a break;
506+
this is more
507+
end
508+
else
509+
e
510+
;;
511+
512+
f
513+
begin if loooooooooooooooooooooooooooooooooooooooooooooooooooooooooong then
514+
()
515+
else
516+
()
517+
end
518+
;;
519+
520+
f
521+
begin if loooooooooooooooooooooooooooooooooooooooooooooooooooooooooonger then
522+
()
523+
else
524+
()
525+
end
526+
;;
527+
528+
f
529+
begin if even loooooooooooooooooooooooooooooooooooooooooooooooooooooooooonger
530+
then
531+
()
532+
else
533+
()
534+
end
535+
;;
536+
537+
f
538+
begin if
539+
and_ even loooooooooooooooooooooooooooooooooooooooooooooooooooooooooonger
540+
then
541+
()
542+
else
543+
()
544+
end
545+
546+
let () =
547+
f
548+
begin if
549+
a___________________________________________________________________
550+
then
551+
b_________________________________________________________________
552+
else
553+
c_________________________________________________________________
554+
end

test/passing/refs.janestreet/exp_grouping-parens.ml.ref

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -477,3 +477,60 @@ let _ =
477477
print_endline xxxxxxxxx;
478478
f xxxxxxxxxx yyyyyyyyyyyyy zzzzzzzzzzzzzzzzzzzzz)
479479
;;
480+
481+
[@@@ocamlformat "if-then-else=fit-or-vertical"]
482+
483+
let _ =
484+
if b then (
485+
e1;
486+
e2)
487+
else (
488+
something loooooooooooooooooooooooooooooooong enough to_trigger a break;
489+
this is more)
490+
;;
491+
492+
let _ =
493+
if b then (
494+
something loooooooooooooooooooooooooooooooong enough to_trigger a break;
495+
this is more)
496+
else if b1 then (
497+
something loooooooooooooooooooooooooooooooong enough to_trigger a break;
498+
this is more)
499+
else
500+
e
501+
;;
502+
503+
f
504+
(if loooooooooooooooooooooooooooooooooooooooooooooooooooooooooong then
505+
()
506+
else
507+
())
508+
;;
509+
510+
f
511+
(if loooooooooooooooooooooooooooooooooooooooooooooooooooooooooonger then
512+
()
513+
else
514+
())
515+
;;
516+
517+
f
518+
(if even loooooooooooooooooooooooooooooooooooooooooooooooooooooooooonger then
519+
()
520+
else
521+
())
522+
;;
523+
524+
f
525+
(if and_ even loooooooooooooooooooooooooooooooooooooooooooooooooooooooooonger then
526+
()
527+
else
528+
())
529+
530+
let () =
531+
f
532+
(if a___________________________________________________________________ then
533+
b_________________________________________________________________
534+
else
535+
c_________________________________________________________________)
536+
;;

test/passing/refs.janestreet/exp_grouping.ml.ref

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -557,3 +557,69 @@ let _ =
557557
f xxxxxxxxxx yyyyyyyyyyyyy zzzzzzzzzzzzzzzzzzzzz
558558
end
559559
;;
560+
561+
[@@@ocamlformat "if-then-else=fit-or-vertical"]
562+
563+
let _ =
564+
if b then begin
565+
e1;
566+
e2
567+
end
568+
else begin
569+
something loooooooooooooooooooooooooooooooong enough to_trigger a break;
570+
this is more
571+
end
572+
;;
573+
574+
let _ =
575+
if b then begin
576+
something loooooooooooooooooooooooooooooooong enough to_trigger a break;
577+
this is more
578+
end
579+
else if b1 then begin
580+
something loooooooooooooooooooooooooooooooong enough to_trigger a break;
581+
this is more
582+
end
583+
else
584+
e
585+
;;
586+
587+
f
588+
begin if loooooooooooooooooooooooooooooooooooooooooooooooooooooooooong then
589+
()
590+
else
591+
()
592+
end
593+
;;
594+
595+
f
596+
begin if loooooooooooooooooooooooooooooooooooooooooooooooooooooooooonger then
597+
()
598+
else
599+
()
600+
end
601+
;;
602+
603+
f
604+
begin if even loooooooooooooooooooooooooooooooooooooooooooooooooooooooooonger then
605+
()
606+
else
607+
()
608+
end
609+
;;
610+
611+
f
612+
begin if and_ even loooooooooooooooooooooooooooooooooooooooooooooooooooooooooonger then
613+
()
614+
else
615+
()
616+
end
617+
618+
let () =
619+
f
620+
begin if a___________________________________________________________________ then
621+
b_________________________________________________________________
622+
else
623+
c_________________________________________________________________
624+
end
625+
;;

test/passing/refs.ocamlformat/exp_grouping-parens.ml.ref

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -409,3 +409,59 @@ let _ =
409409
(fun xxxxxxxxxxxxxxxxxxxxxxx yyyyyyyyyyyyyyyyyyy zzzzzzzzzzzzzzzzzzzzzzzz ->
410410
print_endline xxxxxxxxx ;
411411
f xxxxxxxxxx yyyyyyyyyyyyy zzzzzzzzzzzzzzzzzzzzz )
412+
413+
[@@@ocamlformat "if-then-else=fit-or-vertical"]
414+
415+
let _ =
416+
if b then (
417+
e1 ; e2 )
418+
else (
419+
something loooooooooooooooooooooooooooooooong enough to_trigger a break ;
420+
this is more )
421+
422+
let _ =
423+
if b then (
424+
something loooooooooooooooooooooooooooooooong enough to_trigger a break ;
425+
this is more )
426+
else if b1 then (
427+
something loooooooooooooooooooooooooooooooong enough to_trigger a break ;
428+
this is more )
429+
else
430+
e
431+
;;
432+
433+
f
434+
( if loooooooooooooooooooooooooooooooooooooooooooooooooooooooooong then
435+
()
436+
else
437+
() )
438+
;;
439+
440+
f
441+
( if loooooooooooooooooooooooooooooooooooooooooooooooooooooooooonger then
442+
()
443+
else
444+
() )
445+
;;
446+
447+
f
448+
( if even loooooooooooooooooooooooooooooooooooooooooooooooooooooooooonger then
449+
()
450+
else
451+
() )
452+
;;
453+
454+
f
455+
( if and_ even loooooooooooooooooooooooooooooooooooooooooooooooooooooooooonger
456+
then
457+
()
458+
else
459+
() )
460+
461+
let () =
462+
f
463+
( if a___________________________________________________________________
464+
then
465+
b_________________________________________________________________
466+
else
467+
c_________________________________________________________________ )

0 commit comments

Comments
 (0)