Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve begin/end position in match branches #2442

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions lib/Fmt_ast.ml
Original file line number Diff line number Diff line change
Expand Up @@ -3079,10 +3079,11 @@ and fmt_case c ctx ~first ~last case =
fmt "@;<1 2>when " $ fmt_expression c (sub_exp ~ctx g) )
)
$ p.break_before_arrow $ str "->" $ p.break_after_arrow
$ p.open_paren_branch )
$ p.open_paren_branch_eol )
$ p.break_after_opening_paren
$ hovbox 0
( fmt_expression ?eol c ?parens:p.expr_parens p.branch_expr
( p.open_paren_branch_bol
$ fmt_expression ?eol c ?parens:p.expr_parens p.branch_expr
$ p.close_paren_branch ) )

and fmt_value_description ?ext c ctx vd =
Expand Down
35 changes: 22 additions & 13 deletions lib/Params.ml
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,8 @@ type cases =
; box_pattern_arrow: Fmt.t -> Fmt.t
; break_before_arrow: Fmt.t
; break_after_arrow: Fmt.t
; open_paren_branch: Fmt.t
; open_paren_branch_eol: Fmt.t
; open_paren_branch_bol: Fmt.t
; break_after_opening_paren: Fmt.t
; expr_parens: bool option
; branch_expr: expression Ast.xt
Expand Down Expand Up @@ -211,16 +212,19 @@ let get_cases (c : Conf.t) ~ctx ~first ~last ~xbch:({ast; _} as xast) =
else (parenze_exp xast && not body_has_parens, Some false)
in
let indent = if align_nested_match then 0 else indent in
let open_paren_branch, close_paren_branch, branch_expr =
let ( open_paren_branch_eol
, open_paren_branch_bol
, close_paren_branch
, branch_expr ) =
match ast with
| {pexp_desc= Pexp_beginend nested_exp; pexp_attributes= []; _} ->
let close_paren =
let offset =
match c.fmt_opts.break_cases.v with `Nested -> 0 | _ -> -2
in
fits_breaks " end" ~level:1 ~hint:(1000, offset) "end"
fits_breaks " end" ~level:1 ~hint:(1000, -2) "end"
in
(fmt "@;<1 0>begin", close_paren, sub_exp ~ctx:(Exp ast) nested_exp)
( noop
, open_hvbox 2 $ fmt "begin@;"
, close_paren $ close_box
, sub_exp ~ctx:(Exp ast) nested_exp )
| _ ->
let close_paren =
fmt_if_k parens_branch
Expand All @@ -229,7 +233,7 @@ let get_cases (c : Conf.t) ~ctx ~first ~last ~xbch:({ast; _} as xast) =
| `No -> fmt "@,)"
| `Closing_on_separate_line -> fmt "@;<1000 -2>)" )
in
(fmt_if parens_branch " (", close_paren, xast)
(fmt_if parens_branch " (", noop, close_paren, xast)
in
match c.fmt_opts.break_cases.v with
| `Fit ->
Expand All @@ -239,7 +243,8 @@ let get_cases (c : Conf.t) ~ctx ~first ~last ~xbch:({ast; _} as xast) =
; box_pattern_arrow= hovbox 2
; break_before_arrow= fmt "@;<1 0>"
; break_after_arrow= noop
; open_paren_branch
; open_paren_branch_eol
; open_paren_branch_bol
; break_after_opening_paren= fmt "@ "
; expr_parens
; branch_expr
Expand All @@ -251,7 +256,8 @@ let get_cases (c : Conf.t) ~ctx ~first ~last ~xbch:({ast; _} as xast) =
; box_pattern_arrow= hovbox 0
; break_before_arrow= fmt "@;<1 2>"
; break_after_arrow= fmt_if (not parens_branch) "@;<0 3>"
; open_paren_branch
; open_paren_branch_eol
; open_paren_branch_bol
; break_after_opening_paren= fmt_or (indent > 2) "@;<1 4>" "@;<1 2>"
; expr_parens
; branch_expr
Expand All @@ -263,7 +269,8 @@ let get_cases (c : Conf.t) ~ctx ~first ~last ~xbch:({ast; _} as xast) =
; box_pattern_arrow= hovbox 0
; break_before_arrow= fmt "@;<1 2>"
; break_after_arrow= fmt_if (not parens_branch) "@;<0 3>"
; open_paren_branch
; open_paren_branch_eol
; open_paren_branch_bol
; break_after_opening_paren= fmt "@ "
; expr_parens
; branch_expr
Expand All @@ -275,7 +282,8 @@ let get_cases (c : Conf.t) ~ctx ~first ~last ~xbch:({ast; _} as xast) =
; box_pattern_arrow= hovbox 0
; break_before_arrow= fmt "@;<1 2>"
; break_after_arrow= fmt_if (not parens_branch) "@;<0 3>"
; open_paren_branch
; open_paren_branch_eol
; open_paren_branch_bol
; break_after_opening_paren= fmt "@ "
; expr_parens
; branch_expr
Expand All @@ -287,7 +295,8 @@ let get_cases (c : Conf.t) ~ctx ~first ~last ~xbch:({ast; _} as xast) =
; box_pattern_arrow= hovbox 0
; break_before_arrow= fmt "@;<1 2>"
; break_after_arrow= fmt_if (not parens_branch) "@;<0 3>"
; open_paren_branch
; open_paren_branch_eol
; open_paren_branch_bol
; break_after_opening_paren= break 1000 0
; expr_parens
; branch_expr
Expand Down
3 changes: 2 additions & 1 deletion lib/Params.mli
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,8 @@ type cases =
; box_pattern_arrow: Fmt.t -> Fmt.t
; break_before_arrow: Fmt.t
; break_after_arrow: Fmt.t
; open_paren_branch: Fmt.t
; open_paren_branch_eol: Fmt.t
; open_paren_branch_bol: Fmt.t
; break_after_opening_paren: Fmt.t
; expr_parens: bool option
; branch_expr: expression Ast.xt (** Expression on the RHS of the [->]. *)
Expand Down
151 changes: 80 additions & 71 deletions test/passing/tests/cases_exp_grouping.ml.ref
Original file line number Diff line number Diff line change
Expand Up @@ -2,92 +2,101 @@ let _ =
match x with
| A -> begin match B with A -> fooooooooooooo end
| A -> begin match B with A -> fooooooooooooo | B -> fooooooooooooo end
| A -> begin
match B with
| A -> fooooooooooooo
| B -> fooooooooooooo
| C -> fooooooooooooo
| D -> fooooooooooooo
end
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This shows that for many values of break-cases, the end is aligned with the | of the pattern, which is ambiguous.

| A ->
begin
match B with
| A -> fooooooooooooo
| B -> fooooooooooooo
| C -> fooooooooooooo
| D -> fooooooooooooo
end
[@@ocamlformat "break-cases=fit"]

let _ =
match x with
| A -> begin
match B with A -> fooooooooooooo
end
| A -> begin
match B with A -> fooooooooooooo | B -> fooooooooooooo
end
| A -> begin
match B with
| A ->
fooooooooooooo
| B ->
fooooooooooooo
| C ->
fooooooooooooo
| D ->
fooooooooooooo
| A ->
begin match B with A -> fooooooooooooo end
| A ->
begin match B with A -> fooooooooooooo | B -> fooooooooooooo end
| A ->
begin
match B with
| A ->
fooooooooooooo
| B ->
fooooooooooooo
| C ->
fooooooooooooo
| D ->
fooooooooooooo
end
[@@ocamlformat "break-cases=nested"]

let _ =
match x with
| A -> begin
match B with
| A -> fooooooooooooo
end
| A -> begin
match B with
| A -> fooooooooooooo
| B -> fooooooooooooo
end
| A -> begin
match B with
| A -> fooooooooooooo
| B -> fooooooooooooo
| C -> fooooooooooooo
| D -> fooooooooooooo
end
| A ->
begin
match B with
| A -> fooooooooooooo
end
| A ->
begin
match B with
| A -> fooooooooooooo
| B -> fooooooooooooo
end
| A ->
begin
match B with
| A -> fooooooooooooo
| B -> fooooooooooooo
| C -> fooooooooooooo
| D -> fooooooooooooo
end
[@@ocamlformat "break-cases=toplevel"]

let _ =
match x with
| A -> begin
match B with
| A -> fooooooooooooo
end
| A -> begin
match B with
| A -> fooooooooooooo
| B -> fooooooooooooo
end
| A -> begin
match B with
| A -> fooooooooooooo
| B -> fooooooooooooo
| C -> fooooooooooooo
| D -> fooooooooooooo
end
| A ->
begin
match B with
| A -> fooooooooooooo
end
| A ->
begin
match B with
| A -> fooooooooooooo
| B -> fooooooooooooo
end
| A ->
begin
match B with
| A -> fooooooooooooo
| B -> fooooooooooooo
| C -> fooooooooooooo
| D -> fooooooooooooo
end
[@@ocamlformat "break-cases=fit-or-vertical"]

let _ =
match x with
| A -> begin
match B with
| A -> fooooooooooooo
end
| A -> begin
match B with
| A -> fooooooooooooo
| B -> fooooooooooooo
end
| A -> begin
match B with
| A -> fooooooooooooo
| B -> fooooooooooooo
| C -> fooooooooooooo
| D -> fooooooooooooo
end
| A ->
begin
match B with
| A -> fooooooooooooo
end
| A ->
begin
match B with
| A -> fooooooooooooo
| B -> fooooooooooooo
end
| A ->
begin
match B with
| A -> fooooooooooooo
| B -> fooooooooooooo
| C -> fooooooooooooo
| D -> fooooooooooooo
end
[@@ocamlformat "break-cases=all"]
15 changes: 8 additions & 7 deletions test/passing/tests/exp_grouping.ml.ref
Original file line number Diff line number Diff line change
Expand Up @@ -300,13 +300,14 @@ let _ =
match x with
| A -> begin match B with A -> fooooooooooooo end
| A -> begin match B with A -> fooooooooooooo | B -> fooooooooooooo end
| A -> begin
match B with
| A -> fooooooooooooo
| B -> fooooooooooooo
| C -> fooooooooooooo
| D -> fooooooooooooo
end
| A ->
begin
match B with
| A -> fooooooooooooo
| B -> fooooooooooooo
| C -> fooooooooooooo
| D -> fooooooooooooo
end

let () =
begin
Expand Down
Loading