Skip to content

Commit 6a4b647

Browse files
Boris Yakobowskiclaude
andcommitted
Fix comment-before-match/begin-match in if-then-else branches
Two fixes for ITE branch formatting with if-then-else=fit-or-vertical (and partially if-then-else=vertical): 1. When a comment appears between `else` and `match` with if-then-else=fit-or-vertical, the match keyword was pushed far to the right (e.g. col 31 instead of col 4) due to a missing trailing break after the comment in branch_pro_with_cmts. 2. `begin match expr with` in ITE branches had its scrutinee breaking onto a new line (with if-then-else=fit-or-vertical and if-then-else=vertical) because the ITE `pro` (containing break_unless_newline 1000 2) was passed as inner pro to the match keyword hvbox, inflating its size. Fix: separate `pro` from the `begin` prefix when parent is an ITE expression. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 1e2f288 commit 6a4b647

17 files changed

Lines changed: 135 additions & 185 deletions

lib/Fmt_ast.ml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3033,6 +3033,16 @@ and fmt_beginend c ~loc ?(box = true) ?(pro = noop) ~ctx ~ctx0 ~fmt_atrs
30333033
cmts_before
30343034
$
30353035
match e.pexp_desc with
3036+
| (Pexp_match _ | Pexp_try _ | Pexp_function _)
3037+
when match ctx0 with
3038+
| Exp {pexp_desc= Pexp_ifthenelse _; _} -> true
3039+
| _ -> false ->
3040+
pro
3041+
$ beginend_box
3042+
(fmt_expression c
3043+
~pro:(begin_ $ str " ")
3044+
~box:false ?eol ~parens:false ~indent_wrap (sub_exp ~ctx e) )
3045+
$ end_
30363046
| Pexp_match _ | Pexp_try _ | Pexp_function _ | Pexp_ifthenelse _ ->
30373047
beginend_box
30383048
(fmt_expression c

lib/Params.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -989,13 +989,13 @@ let get_if_then_else (c : Conf.t) ~cmts_before_opt ~pro ~first ~last
989989
&& not has_cmts_after_kw
990990
then
991991
match cmts_before_opt xbch.ast.pexp_loc with
992-
| Some cmts -> break 1000 2 $ cmts
992+
| Some cmts -> break 1000 2 $ cmts $ break 1000 2
993993
| None -> (
994994
match xbch.ast.pexp_desc with
995995
| Pexp_beginend ({pexp_loc; pexp_desc; _}, _)
996996
when is_special_beginend pexp_desc -> (
997997
match cmts_before_opt pexp_loc with
998-
| Some cmts -> break 1000 2 $ cmts
998+
| Some cmts -> break 1000 2 $ cmts $ break 1000 2
999999
| None -> branch_pro ~begin_end_offset:0 () )
10001000
| _ -> branch_pro ~begin_end_offset:0 () )
10011001
else branch_pro ~begin_end_offset:0 ()

test/passing/refs.ahrefs/ite-fit_or_vertical.ml.ref

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -246,10 +246,10 @@ let f =
246246
| A ->
247247
if gf then
248248
(* aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *)
249-
begin if
250-
a
251-
then
252-
b
249+
begin if
250+
a
251+
then
252+
b
253253
end
254254

255255
(* Long comment before match in else branch (regression test for
@@ -258,7 +258,8 @@ let _ =
258258
if a then
259259
b
260260
else
261-
(* aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *)(
261+
(* aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *)
262+
(
262263
match x with
263264
| A -> f)
264265

@@ -279,7 +280,8 @@ let _ =
279280
if a then
280281
b
281282
else
282-
(* comment *)(
283+
(* comment *)
284+
(
283285
match x with
284286
| A -> f
285287
| B -> g)
@@ -289,9 +291,7 @@ let _ =
289291
if a then
290292
b
291293
else
292-
begin match
293-
x
294-
with
294+
begin match x with
295295
| A -> f
296296
| B -> g
297297
end
@@ -301,7 +301,5 @@ let _ =
301301
if a then
302302
b
303303
else
304-
begin try
305-
f x
306-
with Not_found -> g
304+
begin try f x with Not_found -> g
307305
end

test/passing/refs.ahrefs/ite-fit_or_vertical_closing.ml.ref

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -256,10 +256,10 @@ let f =
256256
| A ->
257257
if gf then
258258
(* aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *)
259-
begin if
260-
a
261-
then
262-
b
259+
begin if
260+
a
261+
then
262+
b
263263
end
264264

265265
(* Long comment before match in else branch (regression test for
@@ -268,7 +268,8 @@ let _ =
268268
if a then
269269
b
270270
else
271-
(* aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *)(
271+
(* aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *)
272+
(
272273
match x with
273274
| A -> f
274275
)
@@ -290,7 +291,8 @@ let _ =
290291
if a then
291292
b
292293
else
293-
(* comment *)(
294+
(* comment *)
295+
(
294296
match x with
295297
| A -> f
296298
| B -> g
@@ -301,9 +303,7 @@ let _ =
301303
if a then
302304
b
303305
else
304-
begin match
305-
x
306-
with
306+
begin match x with
307307
| A -> f
308308
| B -> g
309309
end
@@ -313,7 +313,5 @@ let _ =
313313
if a then
314314
b
315315
else
316-
begin try
317-
f x
318-
with Not_found -> g
316+
begin try f x with Not_found -> g
319317
end

test/passing/refs.ahrefs/ite-fit_or_vertical_no_indicate.ml.ref

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -246,10 +246,10 @@ let f =
246246
| A ->
247247
if gf then
248248
(* aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *)
249-
begin if
250-
a
251-
then
252-
b
249+
begin if
250+
a
251+
then
252+
b
253253
end
254254

255255
(* Long comment before match in else branch (regression test for
@@ -258,7 +258,8 @@ let _ =
258258
if a then
259259
b
260260
else
261-
(* aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *)(
261+
(* aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *)
262+
(
262263
match x with
263264
| A -> f)
264265

@@ -279,7 +280,8 @@ let _ =
279280
if a then
280281
b
281282
else
282-
(* comment *)(
283+
(* comment *)
284+
(
283285
match x with
284286
| A -> f
285287
| B -> g)
@@ -289,9 +291,7 @@ let _ =
289291
if a then
290292
b
291293
else
292-
begin match
293-
x
294-
with
294+
begin match x with
295295
| A -> f
296296
| B -> g
297297
end
@@ -301,7 +301,5 @@ let _ =
301301
if a then
302302
b
303303
else
304-
begin try
305-
f x
306-
with Not_found -> g
304+
begin try f x with Not_found -> g
307305
end

test/passing/refs.ahrefs/ite-vertical.ml.ref

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -326,9 +326,7 @@ let _ =
326326
if a then
327327
b
328328
else
329-
begin match
330-
x
331-
with
329+
begin match x with
332330
| A -> f
333331
| B -> g
334332
end
@@ -338,7 +336,5 @@ let _ =
338336
if a then
339337
b
340338
else
341-
begin try
342-
f x
343-
with Not_found -> g
339+
begin try f x with Not_found -> g
344340
end

test/passing/refs.default/ite-fit_or_vertical.ml.ref

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -246,10 +246,10 @@ let f =
246246
| A ->
247247
if gf then
248248
(* aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *)
249-
begin if
250-
a
251-
then
252-
b
249+
begin if
250+
a
251+
then
252+
b
253253
end
254254

255255
(* Long comment before match in else branch (regression test for
@@ -259,8 +259,8 @@ let _ =
259259
b
260260
else
261261
(* aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *)
262-
match x with
263-
| A -> f
262+
match x with
263+
| A -> f
264264

265265
(* Comment before nested if in then branch - Compact mode oscillation *)
266266
let _ =
@@ -280,28 +280,22 @@ let _ =
280280
b
281281
else
282282
(* comment *)
283-
match x with
284-
| A -> f
285-
| B -> g
283+
match x with
284+
| A -> f
285+
| B -> g
286286

287287
(* begin match in else branch should keep scrutinee on same line *)
288288
let _ =
289289
if a then
290290
b
291291
else
292-
begin match
293-
x
294-
with
295-
| A -> f
296-
| B -> g
292+
begin match x with A -> f | B -> g
297293
end
298294

299295
(* begin try in else branch should keep body on same line *)
300296
let _ =
301297
if a then
302298
b
303299
else
304-
begin try
305-
f x
306-
with Not_found -> g
300+
begin try f x with Not_found -> g
307301
end

test/passing/refs.default/ite-fit_or_vertical_closing.ml.ref

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -255,10 +255,10 @@ let f =
255255
| A ->
256256
if gf then
257257
(* aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *)
258-
begin if
259-
a
260-
then
261-
b
258+
begin if
259+
a
260+
then
261+
b
262262
end
263263

264264
(* Long comment before match in else branch (regression test for
@@ -268,8 +268,8 @@ let _ =
268268
b
269269
else
270270
(* aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *)
271-
match x with
272-
| A -> f
271+
match x with
272+
| A -> f
273273

274274
(* Comment before nested if in then branch - Compact mode oscillation *)
275275
let _ =
@@ -289,28 +289,22 @@ let _ =
289289
b
290290
else
291291
(* comment *)
292-
match x with
293-
| A -> f
294-
| B -> g
292+
match x with
293+
| A -> f
294+
| B -> g
295295

296296
(* begin match in else branch should keep scrutinee on same line *)
297297
let _ =
298298
if a then
299299
b
300300
else
301-
begin match
302-
x
303-
with
304-
| A -> f
305-
| B -> g
301+
begin match x with A -> f | B -> g
306302
end
307303

308304
(* begin try in else branch should keep body on same line *)
309305
let _ =
310306
if a then
311307
b
312308
else
313-
begin try
314-
f x
315-
with Not_found -> g
309+
begin try f x with Not_found -> g
316310
end

test/passing/refs.default/ite-fit_or_vertical_no_indicate.ml.ref

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -246,10 +246,10 @@ let f =
246246
| A ->
247247
if gf then
248248
(* aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *)
249-
begin if
250-
a
251-
then
252-
b
249+
begin if
250+
a
251+
then
252+
b
253253
end
254254

255255
(* Long comment before match in else branch (regression test for
@@ -259,8 +259,8 @@ let _ =
259259
b
260260
else
261261
(* aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *)
262-
match x with
263-
| A -> f
262+
match x with
263+
| A -> f
264264

265265
(* Comment before nested if in then branch - Compact mode oscillation *)
266266
let _ =
@@ -280,28 +280,22 @@ let _ =
280280
b
281281
else
282282
(* comment *)
283-
match x with
284-
| A -> f
285-
| B -> g
283+
match x with
284+
| A -> f
285+
| B -> g
286286

287287
(* begin match in else branch should keep scrutinee on same line *)
288288
let _ =
289289
if a then
290290
b
291291
else
292-
begin match
293-
x
294-
with
295-
| A -> f
296-
| B -> g
292+
begin match x with A -> f | B -> g
297293
end
298294

299295
(* begin try in else branch should keep body on same line *)
300296
let _ =
301297
if a then
302298
b
303299
else
304-
begin try
305-
f x
306-
with Not_found -> g
300+
begin try f x with Not_found -> g
307301
end

0 commit comments

Comments
 (0)