Skip to content

Commit 6620833

Browse files
authored
Add an ocaml-version option, format punned labelled arguments when OCaml 4.14+ syntax is selected (#1759)
1 parent 68e3256 commit 6620833

20 files changed

+95
-13
lines changed

CHANGES.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,13 @@
2525
+ Handle merlin typed holes (#1698, @gpetiot)
2626

2727
+ Handle punned labelled arguments with type constraint in function applications.
28-
For example, function application of the form `foo ~(x:int)` instead of the explicit `foo ~x:(x:int)`. (ocaml#10434) (#1756, @gpetiot)
28+
For example, function application of the form `foo ~(x:int)` instead of the explicit `foo ~x:(x:int)`. (ocaml#10434) (#1756, #1759, @gpetiot)
29+
This syntax is only produced when the output syntax is at least OCaml 4.14.
2930

3031
+ Allow explicit binders for type variables (ocaml#10437) (#1757, @gpetiot)
3132

33+
+ Add a new `ocaml-version` option to select the version of OCaml syntax of the output (#1759, @gpetiot)
34+
3235
### 0.19.0 (2021-07-16)
3336

3437
#### Bug fixes

dune-project

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@
5252
(>= 20201216))
5353
(menhirSdk
5454
(>= 20201216))
55+
(ocaml-version
56+
(>= 3.2.0))
5557
ocp-indent
5658
(bisect_ppx
5759
(and
@@ -95,6 +97,8 @@
9597
(>= 20201216))
9698
(menhirSdk
9799
(>= 20201216))
100+
(ocaml-version
101+
(>= 3.2.0))
98102
ocp-indent
99103
(bisect_ppx
100104
(and

lib/Conf.ml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ type t =
5959
; max_iters: int
6060
; module_item_spacing: [`Compact | `Preserve | `Sparse]
6161
; nested_match: [`Wrap | `Align]
62+
; ocaml_version: Ocaml_version.t
6263
; ocp_indent_compat: bool
6364
; parens_ite: bool
6465
; parens_tuple: [`Always | `Multi_line_only]
@@ -172,6 +173,14 @@ let info =
172173
in
173174
Term.info "ocamlformat" ~version:Version.version ~doc ~man
174175

176+
let ocaml_version_conv =
177+
let parse x =
178+
match Ocaml_version.of_string x with
179+
| Ok x -> `Ok x
180+
| Error (`Msg x) -> `Error x
181+
in
182+
(parse, Ocaml_version.pp)
183+
175184
(** Options affecting formatting *)
176185
module Formatting = struct
177186
let section = `Formatting
@@ -935,6 +944,16 @@ module Formatting = struct
935944
(fun conf x -> {conf with nested_match= x})
936945
(fun conf -> conf.nested_match)
937946

947+
let ocaml_version =
948+
let docv = "V" in
949+
let doc = "Version of OCaml syntax of the output." in
950+
let default = Ocaml_version.sys_version in
951+
let default_doc = "the currently installed OCaml version" in
952+
C.any ocaml_version_conv ~names:["ocaml-version"] ~default ~default_doc
953+
~doc ~docv ~section
954+
(fun conf x -> {conf with ocaml_version= x})
955+
(fun conf -> conf.ocaml_version)
956+
938957
let ocp_indent_compat =
939958
let doc =
940959
"Attempt to generate output which does not change (much) when \
@@ -1456,6 +1475,7 @@ let ocamlformat_profile =
14561475
; max_iters= 10
14571476
; module_item_spacing= `Sparse
14581477
; nested_match= `Wrap
1478+
; ocaml_version= C.default Formatting.ocaml_version
14591479
; ocp_indent_compat= false
14601480
; parens_ite= false
14611481
; parens_tuple= `Always
@@ -1528,6 +1548,7 @@ let conventional_profile =
15281548
; max_iters= C.default max_iters
15291549
; module_item_spacing= C.default Formatting.module_item_spacing
15301550
; nested_match= C.default Formatting.nested_match
1551+
; ocaml_version= C.default Formatting.ocaml_version
15311552
; ocp_indent_compat= C.default Formatting.ocp_indent_compat
15321553
; parens_ite= C.default Formatting.parens_ite
15331554
; parens_tuple= C.default Formatting.parens_tuple
@@ -1653,6 +1674,7 @@ let janestreet_profile =
16531674
; max_iters= ocamlformat_profile.max_iters
16541675
; module_item_spacing= `Compact
16551676
; nested_match= `Wrap
1677+
; ocaml_version= C.default Formatting.ocaml_version
16561678
; ocp_indent_compat= true
16571679
; parens_ite= true
16581680
; parens_tuple= `Multi_line_only

lib/Conf.mli

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ type t =
6363
[max_iters] iterations. *)
6464
; module_item_spacing: [`Compact | `Preserve | `Sparse]
6565
; nested_match: [`Wrap | `Align]
66+
; ocaml_version: Ocaml_version.t
6667
; ocp_indent_compat: bool (** Try to indent like ocp-indent *)
6768
; parens_ite: bool
6869
; parens_tuple: [`Always | `Multi_line_only]

lib/Config_option.ml

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,16 @@ module Make (C : CONFIG) = struct
8080
(in_attributes ~section allow_inline)
8181
deprecated_doc deprecated
8282

83-
let generated_doc conv ~allow_inline ~doc ~section ~default ~deprecated =
84-
let default = Format.asprintf "%a" (Arg.conv_printer conv) default in
85-
let default = if String.is_empty default then "none" else default in
83+
let generated_doc ?default_doc conv ~allow_inline ~doc ~section ~default
84+
~deprecated =
85+
let default_doc =
86+
match default_doc with
87+
| Some x -> x
88+
| None -> Format.asprintf "%a" (Arg.conv_printer conv) default
89+
in
90+
let default =
91+
if String.is_empty default_doc then "none" else default_doc
92+
in
8693
Format.asprintf "%s The default value is $(b,%s).%s%a" doc default
8794
(in_attributes ~section allow_inline)
8895
deprecated_doc deprecated
@@ -134,13 +141,13 @@ module Make (C : CONFIG) = struct
134141
store := Pack opt :: !store ;
135142
opt
136143

137-
let any converter ~default ~docv ~names ~doc ~section
144+
let any ?default_doc converter ~default ~docv ~names ~doc ~section
138145
?(allow_inline = Poly.(section = `Formatting)) ?deprecated update
139146
get_value =
140147
let open Cmdliner in
141148
let doc =
142-
generated_doc converter ~allow_inline ~doc ~section ~default
143-
~deprecated
149+
generated_doc converter ?default_doc ~allow_inline ~doc ~section
150+
~default ~deprecated
144151
in
145152
let docs = section_name section in
146153
let term =

lib/Config_option.mli

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,11 @@ module Make (C : CONFIG) : sig
6464
val flag : default:bool -> bool option_decl
6565

6666
val any :
67-
'a Cmdliner.Arg.conv -> default:'a -> docv:string -> 'a option_decl
67+
?default_doc:string
68+
-> 'a Cmdliner.Arg.conv
69+
-> default:'a
70+
-> docv:string
71+
-> 'a option_decl
6872

6973
val removed_option :
7074
names:string list -> version:string -> msg:string -> unit

lib/Fmt_ast.ml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1352,7 +1352,10 @@ and fmt_label_arg ?(box = true) ?epi ?parens ?eol c
13521352
Cmts.fmt c loc @@ Cmts.fmt c ?eol arg.pexp_loc @@ fmt_label lbl ""
13531353
| ( (Labelled l | Optional l)
13541354
, Pexp_constraint ({pexp_desc= Pexp_ident {txt= Lident i; _}; _}, _) )
1355-
when String.equal l i && List.is_empty arg.pexp_attributes ->
1355+
when String.equal l i
1356+
&& List.is_empty arg.pexp_attributes
1357+
&& Ocaml_version.(compare c.conf.ocaml_version Releases.v4_14 >= 0)
1358+
->
13561359
let lbl =
13571360
match lbl with
13581361
| Labelled _ -> str "~"

lib/dune

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
format_
2222
ocaml_413
2323
ocamlformat_stdlib
24+
ocaml-version
2425
ocp-indent.lib
2526
odoc-parser
2627
parse_wyc

ocamlformat-help.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,10 @@ OPTIONS (CODE FORMATTING STYLE)
364364
--no-wrap-fun-args
365365
Unset wrap-fun-args.
366366

367+
--ocaml-version=V
368+
Version of OCaml syntax of the output. The default value is the
369+
currently installed OCaml version.
370+
367371
--ocp-indent-compat
368372
Attempt to generate output which does not change (much) when
369373
post-processing with ocp-indent. The flag is unset by default.

ocamlformat-rpc.opam

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ depends: [
2121
"menhir" {>= "20201216"}
2222
"menhirLib" {>= "20201216"}
2323
"menhirSdk" {>= "20201216"}
24+
"ocaml-version" {>= "3.2.0"}
2425
"ocp-indent"
2526
"bisect_ppx" {with-test & >= "2.5.0"}
2627
"odoc-parser" {>= "0.9.0"}

0 commit comments

Comments
 (0)