Skip to content

Commit 74215ea

Browse files
authored
janestreet: Always treat multiline {|delimited strings|} as though they are long (#2480)
Co-authored-by: David Vulakh <[email protected]>
1 parent a727b4b commit 74215ea

File tree

8 files changed

+62
-29
lines changed

8 files changed

+62
-29
lines changed

Diff for: lib/Conf.ml

+3
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ let conventional_profile from =
5050
let elt content = Elt.make content from in
5151
{ align_symbol_open_paren= elt true
5252
; assignment_operator= elt `End_line
53+
; break_around_multiline_strings= elt false
5354
; break_before_in= elt `Fit_or_vertical
5455
; break_cases= elt `Fit
5556
; break_collection_expressions= elt `Fit_or_vertical
@@ -118,6 +119,7 @@ let ocamlformat_profile from =
118119
let elt content = Elt.make content from in
119120
{ align_symbol_open_paren= elt true
120121
; assignment_operator= elt `End_line
122+
; break_around_multiline_strings= elt false
121123
; break_before_in= elt `Fit_or_vertical
122124
; break_cases= elt `Nested
123125
; break_collection_expressions= elt `Fit_or_vertical
@@ -184,6 +186,7 @@ let janestreet_profile from =
184186
let elt content = Elt.make content from in
185187
{ align_symbol_open_paren= elt false
186188
; assignment_operator= elt `Begin_line
189+
; break_around_multiline_strings= elt true
187190
; break_before_in= elt `Fit_or_vertical
188191
; break_cases= elt `Fit_or_vertical
189192
; break_collection_expressions=

Diff for: lib/Conf_t.ml

+1
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ type 'a elt = 'a Elt.t
5555
type fmt_opts =
5656
{ align_symbol_open_paren: bool elt
5757
; assignment_operator: [`Begin_line | `End_line] elt
58+
; break_around_multiline_strings: bool elt
5859
; break_before_in: [`Fit_or_vertical | `Auto] elt
5960
; break_cases:
6061
[`Fit | `Nested | `Toplevel | `Fit_or_vertical | `All | `Vertical] elt

Diff for: lib/Conf_t.mli

+1
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ type 'a elt = 'a Elt.t
5151
type fmt_opts =
5252
{ align_symbol_open_paren: bool elt
5353
; assignment_operator: [`Begin_line | `End_line] elt
54+
; break_around_multiline_strings: bool elt
5455
; break_before_in: [`Fit_or_vertical | `Auto] elt
5556
; break_cases:
5657
[`Fit | `Nested | `Toplevel | `Fit_or_vertical | `Vertical | `All] elt

Diff for: lib/Fmt.mli

+3
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,9 @@ val char : char -> t
7676
val str : string -> t
7777
(** Format a string. *)
7878

79+
val str_as : int -> string -> t
80+
(** [str_as a len] formats a string as if it were of length [len]. *)
81+
7982
(** Primitive containers ------------------------------------------------*)
8083

8184
val opt : 'a option -> ('a -> t) -> t

Diff for: lib/Fmt_ast.ml

+18-9
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,15 @@ let fmt_constant c ?epi {pconst_desc; pconst_loc= loc} =
262262
| Pconst_char (_, s) -> wrap "'" "'" @@ str s
263263
| Pconst_string (s, loc', Some delim) ->
264264
Cmts.fmt c loc'
265-
@@ wrap_k (str ("{" ^ delim ^ "|")) (str ("|" ^ delim ^ "}")) (str s)
265+
@@ (* If a multiline string has newlines in it, the configuration might
266+
specify it should get treated as a "long" box element. To do so,
267+
we pretend it is 1000 characters long. *)
268+
( if
269+
c.conf.fmt_opts.break_around_multiline_strings.v
270+
&& String.mem s '\n'
271+
then str_as 1000
272+
else str )
273+
(Format_.sprintf "{%s|%s|%s}" delim s delim)
266274
| Pconst_string (_, loc', None) -> (
267275
let delim = ["@,"; "@;"] in
268276
let contains_pp_commands s =
@@ -513,18 +521,19 @@ let sequence_blank_line c (l1 : Location.t) (l2 : Location.t) =
513521
loop l1.loc_end (Cmts.remaining_before c.cmts l2)
514522
| `Compact -> false
515523

516-
let fmt_quoted_string key ext s = function
517-
| None ->
518-
wrap_k (str (Format_.sprintf "{%s%s|" key ext)) (str "|}") (str s)
524+
let fmt_quoted_string c key ext s maybe_delim =
525+
( if c.conf.fmt_opts.break_around_multiline_strings.v && String.mem s '\n'
526+
then str_as 1000
527+
else str )
528+
@@
529+
match maybe_delim with
530+
| None -> Format_.sprintf "{%s%s|%s|}" key ext s
519531
| Some delim ->
520532
let ext_and_delim =
521533
if String.is_empty delim then ext
522534
else Format_.sprintf "%s %s" ext delim
523535
in
524-
wrap_k
525-
(str (Format_.sprintf "{%s%s|" key ext_and_delim))
526-
(str (Format_.sprintf "|%s}" delim))
527-
(str s)
536+
Format_.sprintf "{%s%s|%s|%s}" key ext_and_delim s delim
528537

529538
let fmt_type_var s =
530539
str "'"
@@ -557,7 +566,7 @@ let rec fmt_extension_aux c ctx ~key (ext, pld) =
557566
assert (not (Cmts.has_after c.cmts pexp_loc)) ;
558567
assert (not (Cmts.has_before c.cmts pstr_loc)) ;
559568
assert (not (Cmts.has_after c.cmts pstr_loc)) ;
560-
hvbox 0 (fmt_quoted_string (Ext.Key.to_string key) ext str delim)
569+
hvbox 0 (fmt_quoted_string c (Ext.Key.to_string key) ext str delim)
561570
| _, PStr [({pstr_loc; _} as si)], (Pld _ | Str _ | Top)
562571
when Source.extension_using_sugar ~name:ext ~payload:pstr_loc ->
563572
fmt_structure_item c ~last:true ~ext ~semisemi:false (sub_str ~ctx si)

Diff for: test/passing/tests/js_source.ml.err

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
Warning: tests/js_source.ml:155 exceeds the margin
2-
Warning: tests/js_source.ml:9531 exceeds the margin
3-
Warning: tests/js_source.ml:9634 exceeds the margin
4-
Warning: tests/js_source.ml:9693 exceeds the margin
5-
Warning: tests/js_source.ml:9775 exceeds the margin
2+
Warning: tests/js_source.ml:9537 exceeds the margin
3+
Warning: tests/js_source.ml:9640 exceeds the margin
4+
Warning: tests/js_source.ml:9699 exceeds the margin
5+
Warning: tests/js_source.ml:9781 exceeds the margin

Diff for: test/passing/tests/js_source.ml.ocp

+16-8
Original file line numberDiff line numberDiff line change
@@ -3154,7 +3154,8 @@ module FM_valid = F (struct
31543154
type t = int
31553155
end)
31563156

3157-
[%%expect {|
3157+
[%%expect
3158+
{|
31583159
module M_valid : S
31593160
module FM_valid : S
31603161
|}]
@@ -3170,7 +3171,8 @@ end = struct
31703171
let x = ref 0
31713172
end
31723173

3173-
[%%expect {|
3174+
[%%expect
3175+
{|
31743176
module Foo : sig type t val x : t ref end
31753177
|}]
31763178

@@ -3184,7 +3186,8 @@ end = struct
31843186
let x = ref 0
31853187
end
31863188

3187-
[%%expect {|
3189+
[%%expect
3190+
{|
31883191
module Bar : sig type t [@@immediate] val x : t ref end
31893192
|}]
31903193

@@ -3194,7 +3197,8 @@ let test f =
31943197
Sys.time () -. start
31953198
;;
31963199

3197-
[%%expect {|
3200+
[%%expect
3201+
{|
31983202
val test : (unit -> 'a) -> float = <fun>
31993203
|}]
32003204

@@ -3204,7 +3208,8 @@ let test_foo () =
32043208
done
32053209
;;
32063210

3207-
[%%expect {|
3211+
[%%expect
3212+
{|
32083213
val test_foo : unit -> unit = <fun>
32093214
|}]
32103215

@@ -3214,7 +3219,8 @@ let test_bar () =
32143219
done
32153220
;;
32163221

3217-
[%%expect {|
3222+
[%%expect
3223+
{|
32183224
val test_bar : unit -> unit = <fun>
32193225
|}]
32203226

@@ -10330,8 +10336,10 @@ zzzzzzzzzzzzzzzzzzzzzzzzzzzz
1033010336
*)
1033110337
(*$*)
1033210338

10333-
(*$ {|
10334-
f|} *)
10339+
(*$
10340+
{|
10341+
f|}
10342+
*)
1033510343

1033610344
let () =
1033710345
match () with

Diff for: test/passing/tests/js_source.ml.ref

+16-8
Original file line numberDiff line numberDiff line change
@@ -3154,7 +3154,8 @@ module FM_valid = F (struct
31543154
type t = int
31553155
end)
31563156

3157-
[%%expect {|
3157+
[%%expect
3158+
{|
31583159
module M_valid : S
31593160
module FM_valid : S
31603161
|}]
@@ -3170,7 +3171,8 @@ end = struct
31703171
let x = ref 0
31713172
end
31723173

3173-
[%%expect {|
3174+
[%%expect
3175+
{|
31743176
module Foo : sig type t val x : t ref end
31753177
|}]
31763178

@@ -3184,7 +3186,8 @@ end = struct
31843186
let x = ref 0
31853187
end
31863188

3187-
[%%expect {|
3189+
[%%expect
3190+
{|
31883191
module Bar : sig type t [@@immediate] val x : t ref end
31893192
|}]
31903193

@@ -3194,7 +3197,8 @@ let test f =
31943197
Sys.time () -. start
31953198
;;
31963199

3197-
[%%expect {|
3200+
[%%expect
3201+
{|
31983202
val test : (unit -> 'a) -> float = <fun>
31993203
|}]
32003204

@@ -3204,7 +3208,8 @@ let test_foo () =
32043208
done
32053209
;;
32063210

3207-
[%%expect {|
3211+
[%%expect
3212+
{|
32083213
val test_foo : unit -> unit = <fun>
32093214
|}]
32103215

@@ -3214,7 +3219,8 @@ let test_bar () =
32143219
done
32153220
;;
32163221

3217-
[%%expect {|
3222+
[%%expect
3223+
{|
32183224
val test_bar : unit -> unit = <fun>
32193225
|}]
32203226

@@ -10330,8 +10336,10 @@ zzzzzzzzzzzzzzzzzzzzzzzzzzzz
1033010336
*)
1033110337
(*$*)
1033210338

10333-
(*$ {|
10334-
f|} *)
10339+
(*$
10340+
{|
10341+
f|}
10342+
*)
1033510343

1033610344
let () =
1033710345
match () with

0 commit comments

Comments
 (0)