Skip to content

Commit ed30ab8

Browse files
authored
Consistent break after string constant argument (#2453)
A break is added after wrapping string constants in argument lists. This break was missing for string constants containing explicit line breaks or format hints.
1 parent bc91e42 commit ed30ab8

File tree

7 files changed

+52
-13
lines changed

7 files changed

+52
-13
lines changed

CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ profile. This started with version 0.26.0.
1515

1616
- Remove trailing space inside a wrapping empty signature (#2443, @Julow)
1717
- Fix extension-point spacing in structures (#2450, @Julow)
18+
- \* Consistent break after string constant argument (#2453, @Julow)
1819

1920
## 0.26.1 (2023-09-15)
2021

lib/Fmt_ast.ml

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1417,7 +1417,7 @@ and fmt_fun ?force_closing_paren
14171417
$ body $ closing
14181418
$ Cmts.fmt_after c ast.pexp_loc )
14191419
1420-
and fmt_label_arg ?(box = true) ?epi ?eol c (lbl, ({ast= arg; _} as xarg)) =
1420+
and fmt_label_arg ?(box = true) ?eol c (lbl, ({ast= arg; _} as xarg)) =
14211421
match (lbl, arg.pexp_desc) with
14221422
| (Labelled l | Optional l), Pexp_ident {txt= Lident i; loc}
14231423
when String.equal l.txt i && List.is_empty arg.pexp_attributes ->
@@ -1435,23 +1435,21 @@ and fmt_label_arg ?(box = true) ?epi ?eol c (lbl, ({ast= arg; _} as xarg)) =
14351435
| Optional _ -> str "?"
14361436
| Nolabel -> noop
14371437
in
1438-
lbl $ fmt_expression c ~box ?epi xarg
1438+
lbl $ fmt_expression c ~box xarg
14391439
| (Labelled _ | Optional _), _ when Cmts.has_after c.cmts xarg.ast.pexp_loc
14401440
->
14411441
let cmts_after = Cmts.fmt_after c xarg.ast.pexp_loc in
14421442
hvbox_if box 2
14431443
( hvbox_if box 0
1444-
(fmt_expression c
1445-
~pro:(fmt_label lbl ":@;<0 2>")
1446-
~box ?epi xarg )
1444+
(fmt_expression c ~pro:(fmt_label lbl ":@;<0 2>") ~box xarg)
14471445
$ cmts_after )
14481446
| (Labelled _ | Optional _), (Pexp_fun _ | Pexp_newtype _) ->
14491447
fmt_fun ~box ~label:lbl ~parens:true c xarg
14501448
| _ ->
14511449
let label_sep : s =
14521450
if box || c.conf.fmt_opts.wrap_fun_args.v then ":@," else ":"
14531451
in
1454-
fmt_label lbl label_sep $ fmt_expression c ~box ?epi xarg
1452+
fmt_label lbl label_sep $ fmt_expression c ~box xarg
14551453
14561454
and expression_width c xe =
14571455
String.length
@@ -1467,15 +1465,15 @@ and fmt_args_grouped ?epi:(global_epi = noop) c ctx args =
14671465
| Pexp_fun _ | Pexp_function _ -> Some false
14681466
| _ -> None
14691467
in
1470-
let epi =
1471-
match (lbl, last) with
1472-
| _, true -> None
1473-
| Nolabel, _ -> Some (fits_breaks "" ~hint:(1000, -1) "")
1474-
| _ -> Some (fits_breaks "" ~hint:(1000, -3) "")
1468+
let break_after =
1469+
match (ast.pexp_desc, c.conf.fmt_opts.break_string_literals.v) with
1470+
| Pexp_constant _, `Auto when not last ->
1471+
fits_breaks "" ~hint:(1000, -2) ""
1472+
| _ -> noop
14751473
in
14761474
hovbox
14771475
(Params.Indent.fun_args_group c.conf ~lbl ast)
1478-
(fmt_label_arg c ?box ?epi (lbl, xarg))
1476+
(fmt_label_arg c ?box (lbl, xarg) $ break_after)
14791477
$ fmt_if_k (not last) (break_unless_newline 1 0)
14801478
in
14811479
let fmt_args ~first ~last args =

test/passing/tests/break_string_literals-never.ml.err

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,5 @@ Warning: tests/break_string_literals.ml:7 exceeds the margin
33
Warning: tests/break_string_literals.ml:11 exceeds the margin
44
Warning: tests/break_string_literals.ml:48 exceeds the margin
55
Warning: tests/break_string_literals.ml:51 exceeds the margin
6+
Warning: tests/break_string_literals.ml:63 exceeds the margin
7+
Warning: tests/break_string_literals.ml:68 exceeds the margin

test/passing/tests/break_string_literals-never.ml.ref

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,3 +58,13 @@ let _ = "abc@,def\n\n ghi"
5858
let _ = "abc@,def\n\n"
5959

6060
let _ = "abc@,def@\n\n"
61+
62+
let _ =
63+
Pp.textf
64+
"Failed to parse environment variable: %s=%s\nPermitted values: if-exists always never\nDefault: %s"
65+
var v (to_string default)
66+
67+
let _ =
68+
Pp.textf
69+
"Failed to parse environment variable: %s=%s Permitted values: if-exists always never Default: %s"
70+
var v (to_string default)

test/passing/tests/break_string_literals.ml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,3 +69,17 @@ let _ = "abc@,def\n\nghi"
6969
let _ = "abc@,def\n\n ghi"
7070
let _ = "abc@,def\n\n"
7171
let _ = "abc@,def@\n\n"
72+
73+
let _ =
74+
Pp.textf
75+
"Failed to parse environment variable: %s=%s\n\
76+
Permitted values: if-exists always never\n\
77+
Default: %s"
78+
var v (to_string default)
79+
80+
let _ =
81+
Pp.textf
82+
"Failed to parse environment variable: %s=%s \
83+
Permitted values: if-exists always never \
84+
Default: %s"
85+
var v (to_string default)

test/passing/tests/break_string_literals.ml.ref

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,3 +92,16 @@ let _ = "abc@,def\n\n ghi"
9292
let _ = "abc@,def\n\n"
9393

9494
let _ = "abc@,def@\n\n"
95+
96+
let _ =
97+
Pp.textf
98+
"Failed to parse environment variable: %s=%s\n\
99+
Permitted values: if-exists always never\n\
100+
Default: %s"
101+
var v (to_string default)
102+
103+
let _ =
104+
Pp.textf
105+
"Failed to parse environment variable: %s=%s Permitted values: \
106+
if-exists always never Default: %s"
107+
var v (to_string default)

test/passing/tests/js_args.ml.ref

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ let () =
3333
messages :=
3434
Message_store.create (Session_id.of_string "")
3535
(* Tuareg indents these lines too far to the left. *)
36-
"herd-retransmitter" Message_store.Message_size.Byte
36+
"herd-retransmitter"
37+
Message_store.Message_size.Byte
3738

3839
let () =
3940
raise

0 commit comments

Comments
 (0)