Skip to content

Commit

Permalink
Fix extension shorthand for type nonrec (#94)
Browse files Browse the repository at this point in the history
  • Loading branch information
dvulakh authored Dec 2, 2024
1 parent 525979e commit efd15ae
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 11 deletions.
25 changes: 14 additions & 11 deletions lib/Fmt_ast.ml
Original file line number Diff line number Diff line change
Expand Up @@ -3722,8 +3722,7 @@ and fmt_class_params c ctx params =
(hvbox 0
(wrap_fits_breaks c.conf "[" "]" (list_fl params fmt_param) $ fmt "@ ") )

and fmt_type_declaration c ?ext ?(pre = "") ?name ?(eq = "=") {ast= decl; _}
=
and fmt_type_declaration c ?(pre = noop) ?name ?(eq = "=") {ast= decl; _} =
protect c (Td decl)
@@
let decl =
Expand Down Expand Up @@ -3762,9 +3761,7 @@ and fmt_type_declaration c ?ext ?(pre = "") ?name ?(eq = "=") {ast= decl; _}
( hvbox_if
(Option.is_some ptype_jkind)
c.conf.fmt_opts.type_decl_indent.v
( str pre
$ fmt_extension_suffix c ext
$ str " "
( pre $ str " "
$ hvbox_if
(not (List.is_empty ptype_params))
0
Expand Down Expand Up @@ -4584,13 +4581,16 @@ and fmt_module_statement c ~attributes ?keyword mod_expr =

and fmt_with_constraint c ctx ~pre = function
| Pwith_type (lid, td) ->
fmt_type_declaration ~pre:(pre ^ " type") c ~name:lid (sub_td ~ctx td)
fmt_type_declaration
~pre:(str (pre ^ " type"))
c ~name:lid (sub_td ~ctx td)
| Pwith_module (m1, m2) ->
str pre $ str " module " $ fmt_longident_loc c m1 $ str " = "
$ fmt_longident_loc c m2
| Pwith_typesubst (lid, td) ->
fmt_type_declaration ~pre:(pre ^ " type") c ~eq:":=" ~name:lid
(sub_td ~ctx td)
fmt_type_declaration
~pre:(str (pre ^ " type"))
c ~eq:":=" ~name:lid (sub_td ~ctx td)
| Pwith_modsubst (m1, m2) ->
str pre $ str " module " $ fmt_longident_loc c m1 $ str " := "
$ fmt_longident_loc c m2
Expand Down Expand Up @@ -4840,10 +4840,13 @@ and fmt_type c ?ext ?eq rec_flag decls ctx =
let fmt_decl c ctx ~prev ~next:_ decl =
let first = Option.is_none prev in
let pre =
if first then if is_rec then "type" else "type nonrec" else "and"
if first then
str "type"
$ fmt_extension_suffix c ext
$ fmt_if (not is_rec) " nonrec"
else str "and"
in
let ext = if first then ext else None in
fmt_type_declaration c ~pre ?eq ?ext (sub_td ~ctx decl)
fmt_type_declaration c ~pre ?eq (sub_td ~ctx decl)
in
let ast x = Td x in
fmt_item_list c ctx update_config ast fmt_decl decls
Expand Down
3 changes: 3 additions & 0 deletions test/passing/tests/extensions-indent.ml.js-ref
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,9 @@ let%lwt f = function
;;

type%any_extension t = < a : 'a >
[%%any_extension type t = < a : 'a > ]
type%any_extension nonrec t = < a : 'a >
[%%any_extension type nonrec t = < a : 'a > ]

let value =
f
Expand Down
6 changes: 6 additions & 0 deletions test/passing/tests/extensions-indent.ml.ref
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,12 @@ let%lwt f = function _ -> ()

type%any_extension t = < a: 'a >

[%%any_extension type t = < a: 'a > ]

type%any_extension nonrec t = < a: 'a >

[%%any_extension type nonrec t = < a: 'a > ]

let value = f [%any_extension function 0 -> false | _ -> true]

let value = [%any_extension fun x -> y] x
Expand Down
11 changes: 11 additions & 0 deletions test/passing/tests/extensions.ml
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,17 @@ let%lwt f = function
type%any_extension t =
< a: 'a >

[%%any_extension
type t = < a: 'a >
]

type%any_extension nonrec t =
< a: 'a >

[%%any_extension
type nonrec t = < a: 'a >
]

let value =
f
[%any_extension
Expand Down
3 changes: 3 additions & 0 deletions test/passing/tests/extensions.ml.js-ref
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,9 @@ let%lwt f = function
;;

type%any_extension t = < a : 'a >
[%%any_extension type t = < a : 'a > ]
type%any_extension nonrec t = < a : 'a >
[%%any_extension type nonrec t = < a : 'a > ]

let value =
f
Expand Down
6 changes: 6 additions & 0 deletions test/passing/tests/extensions.ml.ref
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,12 @@ let%lwt f = function _ -> ()

type%any_extension t = < a: 'a >

[%%any_extension type t = < a: 'a > ]

type%any_extension nonrec t = < a: 'a >

[%%any_extension type nonrec t = < a: 'a > ]

let value = f [%any_extension function 0 -> false | _ -> true]

let value = [%any_extension fun x -> y] x
Expand Down

0 comments on commit efd15ae

Please sign in to comment.