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

Represent 'let f _ = function' in the CST #2596

Merged
merged 22 commits into from
Oct 24, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Fix record patterns
  • Loading branch information
Julow committed Oct 23, 2024
commit e78ed52d40ba6a58f2fc008fa92614f9fce66fa7
6 changes: 3 additions & 3 deletions lib/Fmt_ast.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1159,7 +1159,7 @@ and fmt_pattern ?ext c ?pro ?parens ?(box = false)
in
hvbox 0 @@ Cmts.fmt c ppat_loc @@ fmt_record_field c ?typ1 ?rhs lid
in
let p = Params.get_record_pat c.conf ~ctx:ctx0 in
let p = Params.get_record_pat c.conf ~ctx:ctx0 pat in
let last_sep, fmt_underscore =
match closed_flag with
| OClosed -> (true, noop)
Expand Down Expand Up @@ -1188,13 +1188,13 @@ and fmt_pattern ?ext c ?pro ?parens ?(box = false)
hvbox 0
(wrap_fits_breaks c.conf "[|" "|]" (Cmts.fmt_within c ppat_loc))
| Ppat_array pats ->
let p = Params.get_array_pat c.conf ~ctx:ctx0 in
let p = Params.get_array_pat c.conf ~ctx:ctx0 pat in
p.box
(fmt_elements_collection c p Pat.location ppat_loc
(sub_pat ~ctx >> fmt_pattern c >> hvbox 0)
pats )
| Ppat_list pats ->
let p = Params.get_list_pat c.conf ~ctx:ctx0 in
let p = Params.get_list_pat c.conf ~ctx:ctx0 pat in
p.box
(fmt_elements_collection c p Pat.location ppat_loc
(sub_pat ~ctx >> fmt_pattern c >> hvbox 0)
Expand Down
26 changes: 15 additions & 11 deletions lib/Params.ml
Original file line number Diff line number Diff line change
Expand Up @@ -663,44 +663,48 @@ let get_list_expr (c : Conf.t) =
let get_array_expr (c : Conf.t) =
collection_expr c ~space_around:c.fmt_opts.space_around_arrays.v "[|" "|]"

let box_pattern_docked (c : Conf.t) ~ctx ~space_around opn cls k =
let box_pattern_docked (c : Conf.t) ~ctx ~space_around ~pat opn cls k =
let space = if space_around then 1 else 0 in
let indent_opn, indent_cls =
match (ctx, c.fmt_opts.break_separators.v) with
| Ast.Exp {pexp_desc= Pexp_match _ | Pexp_try _; _}, `Before ->
(String.length opn - 3, 1 - String.length opn)
| Ast.Exp {pexp_desc= Pexp_match _ | Pexp_try _; _}, `After -> (-3, 1)
| Ast.Exp {pexp_desc= Pexp_let _; _}, _ -> (-4, 0)
| Ast.Exp {pexp_desc= Pexp_let ({pvbs_bindings; _}, _, _); _}, _
when List.exists pvbs_bindings ~f:(fun b -> phys_equal b.pvb_pat pat)
->
(-4, 0)
| _ -> (0, 0)
in
hvbox indent_opn
(wrap (str opn) (str cls) (break space 2 $ k $ break space indent_cls))

let get_record_pat (c : Conf.t) ~ctx =
let get_record_pat (c : Conf.t) ~ctx pat =
let params, _ = get_record_expr c in
let box =
if c.fmt_opts.dock_collection_brackets.v then
box_pattern_docked c ~ctx
~space_around:c.fmt_opts.space_around_records.v "{" "}"
~space_around:c.fmt_opts.space_around_records.v ~pat "{" "}"
else params.box
in
{params with box}

let collection_pat (c : Conf.t) ~ctx ~space_around opn cls =
let collection_pat (c : Conf.t) ~ctx ~space_around ~pat opn cls =
let params = collection_expr c ~space_around opn cls in
let box =
if c.fmt_opts.dock_collection_brackets.v then
box_collec c 0 >> box_pattern_docked c ~ctx ~space_around opn cls
box_collec c 0 >> box_pattern_docked c ~ctx ~space_around ~pat opn cls
else params.box
in
{params with box}

let get_list_pat (c : Conf.t) ~ctx =
collection_pat c ~ctx ~space_around:c.fmt_opts.space_around_lists.v "[" "]"
let get_list_pat (c : Conf.t) ~ctx pat =
collection_pat c ~ctx ~space_around:c.fmt_opts.space_around_lists.v ~pat
"[" "]"

let get_array_pat (c : Conf.t) ~ctx =
collection_pat c ~ctx ~space_around:c.fmt_opts.space_around_arrays.v "[|"
"|]"
let get_array_pat (c : Conf.t) ~ctx pat =
collection_pat c ~ctx ~space_around:c.fmt_opts.space_around_arrays.v ~pat
"[|" "|]"

type if_then_else =
{ box_branch: Fmt.t -> Fmt.t
Expand Down
6 changes: 3 additions & 3 deletions lib/Params.mli
Original file line number Diff line number Diff line change
Expand Up @@ -163,11 +163,11 @@ val get_list_expr : Conf.t -> elements_collection

val get_array_expr : Conf.t -> elements_collection

val get_record_pat : Conf.t -> ctx:Ast.t -> elements_collection
val get_record_pat : Conf.t -> ctx:Ast.t -> pattern -> elements_collection

val get_list_pat : Conf.t -> ctx:Ast.t -> elements_collection
val get_list_pat : Conf.t -> ctx:Ast.t -> pattern -> elements_collection

val get_array_pat : Conf.t -> ctx:Ast.t -> elements_collection
val get_array_pat : Conf.t -> ctx:Ast.t -> pattern -> elements_collection

type if_then_else =
{ box_branch: Fmt.t -> Fmt.t
Expand Down
18 changes: 18 additions & 0 deletions test/passing/dune.inc
Original file line number Diff line number Diff line change
Expand Up @@ -4711,6 +4711,24 @@
(package ocamlformat)
(action (diff tests/record-402.ml.err record-402.ml.stderr)))

(rule
(deps tests/.ocamlformat )
(package ocamlformat)
(action
(with-stdout-to record-default.ml.stdout
(with-stderr-to record-default.ml.stderr
(run %{bin:ocamlformat} --margin-check --profile=default --max-iter=3 %{dep:tests/record.ml})))))

(rule
(alias runtest)
(package ocamlformat)
(action (diff tests/record-default.ml.ref record-default.ml.stdout)))

(rule
(alias runtest)
(package ocamlformat)
(action (diff tests/record-default.ml.err record-default.ml.stderr)))

(rule
(deps tests/.ocamlformat )
(package ocamlformat)
Expand Down
9 changes: 9 additions & 0 deletions test/passing/tests/record-402.ml.ref
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,12 @@ type t =
{ xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx:
YYYYYYYYYYYYYYYYYYYYY.t
(* ____________________________________ *) }

let _ =
let _ = function
| { foooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo
; foooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo }
->
()
in
()
2 changes: 2 additions & 0 deletions test/passing/tests/record-default.ml.err
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Warning: tests/record.ml:8 exceeds the margin
Warning: tests/record.ml:16 exceeds the margin
2 changes: 2 additions & 0 deletions test/passing/tests/record-default.ml.opts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
--profile=default
--max-iter=3
68 changes: 68 additions & 0 deletions test/passing/tests/record-default.ml.ref
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
type t = { x : int; y : int }

let _ = { x = 1; y = 2 }
let _ = { !e with a; b = c }
let _ = { !(f e) with a; b = c }

let _ =
{
!looooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo
with
a;
b = c;
}

let _ =
{
!looooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo
with
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;
b = c;
}

let _ = { (a : t) with a; b; c }
let _ = { (f a) with a; b; c }

let _ =
{
(a;
a)
with
a;
b;
c;
}

let _ = { (if x then e else e) with e1; e2 }
let _ = { (match x with x -> e) with e1; e2 }
let _ = { (x : x) with e1; e2 }
let _ = { (x :> x) with e1; e2 }
let _ = { (x#x) with e1; e2 }
let f ~l:{ f; g } = e
let f ?l:({ f; g }) = e
let _ = { a; b = (match b with `A -> A | `B -> B | `C -> C : c); c }
let a () = A { A.a : t }
let x = { aaaaaaaaaa (* b *); b }
let x = { aaaaaaaaaa (* b *); b }

type t = { a : (module S); b : (module S) }

let _ = { a = (module M : S); b = (module M : S) }
let to_string { x; _ (* we should print y *) } = string_of_int x
let { x : t } = x

type t = {
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx :
YYYYYYYYYYYYYYYYYYYYY.t;
(* ____________________________________ *)
}

let _ =
let _ = function
| {
foooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo;
foooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo;
} ->
()
in
()
9 changes: 9 additions & 0 deletions test/passing/tests/record-loose.ml.ref
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,12 @@ type t =
{ xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx :
YYYYYYYYYYYYYYYYYYYYY.t
(* ____________________________________ *) }

let _ =
let _ = function
| { foooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo
; foooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo }
->
()
in
()
9 changes: 9 additions & 0 deletions test/passing/tests/record-tight_decl.ml.ref
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,12 @@ type t =
{ xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx:
YYYYYYYYYYYYYYYYYYYYY.t
(* ____________________________________ *) }

let _ =
let _ = function
| { foooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo
; foooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo }
->
()
in
()
10 changes: 10 additions & 0 deletions test/passing/tests/record.ml
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,13 @@ type t =
{ xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx : YYYYYYYYYYYYYYYYYYYYY.t
(* ____________________________________ *)
}

let _ =
let _ = function
| {
foooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo;
foooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo;
} ->
()
in
()
9 changes: 9 additions & 0 deletions test/passing/tests/record.ml.ref
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,12 @@ type t =
{ xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx:
YYYYYYYYYYYYYYYYYYYYY.t
(* ____________________________________ *) }

let _ =
let _ = function
| { foooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo
; foooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo }
->
()
in
()
Loading