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

nest-match=align now works with match%ext. #2648

Merged
merged 3 commits into from
Feb 3, 2025
Merged
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
2 changes: 2 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ profile. This started with version 0.26.0.
Asterisk-prefixed comments are also now formatted the same way as with the
default profile.

- Fixed `nested-match=align` not working with `match%ext` (#2648, @EmileTrotignon)

## 0.27.0

### Highlight
Expand Down
14 changes: 13 additions & 1 deletion lib/Params.ml
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,17 @@ let get_cases (c : Conf.t) ~ctx ~first ~last ~cmts_before
let align_nested_match =
match (ast.pexp_desc, c.fmt_opts.nested_match.v) with
| (Pexp_match _ | Pexp_try _), `Align -> last
| ( Pexp_extension
( ext
, PStr
[ { pstr_loc= _
; pstr_desc=
Pstr_eval
({pexp_desc= Pexp_match _ | Pexp_try _; pexp_loc; _}, _)
} ] )
, `Align )
when Source.extension_using_sugar ~name:ext ~payload:pexp_loc ->
Copy link
Collaborator

Choose a reason for hiding this comment

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

This function is used quite often, at some point we could consider putting this information in the Pexp_extension node.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yes of course, but that turns a simple patch into a complicated project. I don't think my employer would be willing to let me do it.

Copy link
Collaborator

Choose a reason for hiding this comment

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

I'm mentioning this in case it spawns ideas. Of course, I want this fix to go in first.

last
| _ -> false
in
let body_has_parens =
Expand Down Expand Up @@ -930,7 +941,8 @@ module Align = struct

let module_pack (c : Conf.t) ~me =
if not c.fmt_opts.ocp_indent_compat.v then false
else (* Align when the constraint is not desugared. *)
else
(* Align when the constraint is not desugared. *)
match me.pmod_desc with
| Pmod_structure _ | Pmod_ident _ -> false
| _ -> true
Expand Down
48 changes: 48 additions & 0 deletions test/passing/refs.default/match.ml.ref
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,51 @@ let x =

let _ = match x with _ -> b >>= fun () -> c
let () = match () with _ -> ( fun _ : _ -> match () with _ -> ()) | _ -> ()

[@@@ocamlformat "nested-match=align"]

let () =
match f x with
| _ :: _ -> aaaaa aaaa a aa aaaaaa aaaa
| _ ->
match g y with
| _ :: _ :: _ :: _ -> bbb bbbbb bbbbbbb bbbb bbbb
| _ :: _ :: _ -> cc cccc cccc cccc cccc ccc cccccc cccc
| [] -> ff dda asa

let () =
match%ext1 f x with
| _ :: _ -> aaaaa aaaa a aa aaaaaa aaaa
| _ ->
match g y with
| _ :: _ :: _ :: _ -> bbb bbbbb bbbbbbb bbbb bbbb
| _ :: _ :: _ -> cc cccc cccc cccc cccc ccc cccccc cccc
| [] -> ff dda asa

let () =
match f x with
| _ :: _ -> aaaaa aaaa a aa aaaaaa aaaa
| _ ->
match%ext2 g y with
| _ :: _ :: _ :: _ -> bbb bbbbb bbbbbbb bbbb bbbb
| _ :: _ :: _ -> cc cccc cccc cccc cccc ccc cccccc cccc
| [] -> ff dda asa

let () =
match f x with
| _ :: _ -> aaaaa aaaa a aa aaaaaa aaaa
| _ ->
[%ext2
match g y with
| _ :: _ :: _ :: _ -> bbb bbbbb bbbbbbb bbbb bbbb
| _ :: _ :: _ -> cc cccc cccc cccc cccc ccc cccccc cccc
| [] -> ff dda asa]

let () =
match%ext1 f x with
| _ :: _ -> aaaaa aaaa a aa aaaaaa aaaa
| _ ->
match%ext2 g y with
| _ :: _ :: _ :: _ -> bbb bbbbb bbbbbbb bbbb bbbb
| _ :: _ :: _ -> cc cccc cccc cccc cccc ccc cccccc cccc
| [] -> ff dda asa
53 changes: 53 additions & 0 deletions test/passing/refs.janestreet/match.ml.ref
Original file line number Diff line number Diff line change
Expand Up @@ -115,3 +115,56 @@ let () =
| _ -> ()))
| _ -> ()
;;

[@@@ocamlformat "nested-match=align"]

let () =
match f x with
| _ :: _ -> aaaaa aaaa a aa aaaaaa aaaa
| _ ->
match g y with
| _ :: _ :: _ :: _ -> bbb bbbbb bbbbbbb bbbb bbbb
| _ :: _ :: _ -> cc cccc cccc cccc cccc ccc cccccc cccc
| [] -> ff dda asa
;;

let () =
match%ext1 f x with
| _ :: _ -> aaaaa aaaa a aa aaaaaa aaaa
| _ ->
match g y with
| _ :: _ :: _ :: _ -> bbb bbbbb bbbbbbb bbbb bbbb
| _ :: _ :: _ -> cc cccc cccc cccc cccc ccc cccccc cccc
| [] -> ff dda asa
;;

let () =
match f x with
| _ :: _ -> aaaaa aaaa a aa aaaaaa aaaa
| _ ->
match%ext2 g y with
| _ :: _ :: _ :: _ -> bbb bbbbb bbbbbbb bbbb bbbb
| _ :: _ :: _ -> cc cccc cccc cccc cccc ccc cccccc cccc
| [] -> ff dda asa
;;

let () =
match f x with
| _ :: _ -> aaaaa aaaa a aa aaaaaa aaaa
| _ ->
[%ext2
match g y with
| _ :: _ :: _ :: _ -> bbb bbbbb bbbbbbb bbbb bbbb
| _ :: _ :: _ -> cc cccc cccc cccc cccc ccc cccccc cccc
| [] -> ff dda asa]
;;

let () =
match%ext1 f x with
| _ :: _ -> aaaaa aaaa a aa aaaaaa aaaa
| _ ->
match%ext2 g y with
| _ :: _ :: _ :: _ -> bbb bbbbb bbbbbbb bbbb bbbb
| _ :: _ :: _ -> cc cccc cccc cccc cccc ccc cccccc cccc
| [] -> ff dda asa
;;
68 changes: 68 additions & 0 deletions test/passing/refs.ocamlformat/match.ml.ref
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,71 @@ let x =
let _ = match x with _ -> b >>= fun () -> c

let () = match () with _ -> ( fun _ : _ -> match () with _ -> () ) | _ -> ()

[@@@ocamlformat "nested-match=align"]

let () =
match f x with
| _ :: _ ->
aaaaa aaaa a aa aaaaaa aaaa
| _ ->
match g y with
| _ :: _ :: _ :: _ ->
bbb bbbbb bbbbbbb bbbb bbbb
| _ :: _ :: _ ->
cc cccc cccc cccc cccc ccc cccccc cccc
| [] ->
ff dda asa

let () =
match%ext1 f x with
| _ :: _ ->
aaaaa aaaa a aa aaaaaa aaaa
| _ ->
match g y with
| _ :: _ :: _ :: _ ->
bbb bbbbb bbbbbbb bbbb bbbb
| _ :: _ :: _ ->
cc cccc cccc cccc cccc ccc cccccc cccc
| [] ->
ff dda asa

let () =
match f x with
| _ :: _ ->
aaaaa aaaa a aa aaaaaa aaaa
| _ ->
match%ext2 g y with
| _ :: _ :: _ :: _ ->
bbb bbbbb bbbbbbb bbbb bbbb
| _ :: _ :: _ ->
cc cccc cccc cccc cccc ccc cccccc cccc
| [] ->
ff dda asa

let () =
match f x with
| _ :: _ ->
aaaaa aaaa a aa aaaaaa aaaa
| _ ->
[%ext2
match g y with
| _ :: _ :: _ :: _ ->
bbb bbbbb bbbbbbb bbbb bbbb
| _ :: _ :: _ ->
cc cccc cccc cccc cccc ccc cccccc cccc
| [] ->
ff dda asa]

let () =
match%ext1 f x with
| _ :: _ ->
aaaaa aaaa a aa aaaaaa aaaa
| _ ->
match%ext2 g y with
| _ :: _ :: _ :: _ ->
bbb bbbbb bbbbbbb bbbb bbbb
| _ :: _ :: _ ->
cc cccc cccc cccc cccc ccc cccccc cccc
| [] ->
ff dda asa
49 changes: 49 additions & 0 deletions test/passing/tests/match.ml
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,52 @@ let () =
| _ -> ())
| _ -> ()
;;

[@@@ocamlformat "nested-match=align"]

let () =
match f x with
| _ :: _ -> aaaaa aaaa a aa aaaaaa aaaa
| _ ->
match g y with
| _ :: _ :: _ :: _ -> bbb bbbbb bbbbbbb bbbb bbbb
| _ :: _ :: _ -> cc cccc cccc cccc cccc ccc cccccc cccc
| [] -> ff dda asa

let () =
match%ext1 f x with
| _ :: _ -> aaaaa aaaa a aa aaaaaa aaaa
| _ ->
match g y with
| _ :: _ :: _ :: _ -> bbb bbbbb bbbbbbb bbbb bbbb
| _ :: _ :: _ -> cc cccc cccc cccc cccc ccc cccccc cccc
| [] -> ff dda asa

let () =
match f x with
| _ :: _ -> aaaaa aaaa a aa aaaaaa aaaa
| _ ->
match%ext2 g y with
| _ :: _ :: _ :: _ -> bbb bbbbb bbbbbbb bbbb bbbb
| _ :: _ :: _ -> cc cccc cccc cccc cccc ccc cccccc cccc
| [] -> ff dda asa

let () =
match f x with
| _ :: _ -> aaaaa aaaa a aa aaaaaa aaaa
| _ ->
[%ext2
match g y with
| _ :: _ :: _ :: _ -> bbb bbbbb bbbbbbb bbbb bbbb
| _ :: _ :: _ -> cc cccc cccc cccc cccc ccc cccccc cccc
| [] -> ff dda asa]

let () =
match%ext1 f x with
| _ :: _ -> aaaaa aaaa a aa aaaaaa aaaa
| _ ->
match%ext2 g y with
| _ :: _ :: _ :: _ -> bbb bbbbb bbbbbbb bbbb bbbb
| _ :: _ :: _ -> cc cccc cccc cccc cccc ccc cccccc cccc
| [] -> ff dda asa