Skip to content

Commit 29c4364

Browse files
allow begin end on one line in safe cases
1 parent 96cc58c commit 29c4364

37 files changed

+170
-517
lines changed

lib/Fmt_ast.ml

Lines changed: 67 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -222,10 +222,10 @@ let fmt_item_list c ctx0 update_config ast fmt_item items =
222222
let loc = Ast.location ctx in
223223
maybe_disabled c loc [] (fun c -> fmt_item c ctx ~prev ~next itm)
224224
$ opt next (fun (i_n, c_n) ->
225-
fmt_or
226-
(break_between c (ctx, c.conf) (ast i_n, c_n.conf))
227-
(str "\n" $ force_break)
228-
(fmt_or break_struct force_break space_break) )
225+
fmt_or
226+
(break_between c (ctx, c.conf) (ast i_n, c_n.conf))
227+
(str "\n" $ force_break)
228+
(fmt_or break_struct force_break space_break) )
229229

230230
let fmt_recmodule c ctx items fmt_item ast sub =
231231
let update_config c i = update_config c (Ast.attributes (ast i)) in
@@ -473,7 +473,7 @@ let fmt_docstring_around_item' ?(is_val = false) ?(force_before = false)
473473
let floating_doc, doc =
474474
doc
475475
|> List.map ~f:(fun (({txt; loc}, _) as doc) ->
476-
(Docstring.parse ~loc txt, doc) )
476+
(Docstring.parse ~loc txt, doc) )
477477
|> List.partition_tf ~f:(fun (_, (_, floating)) -> floating)
478478
in
479479
let placement =
@@ -860,8 +860,8 @@ and fmt_core_type c ?(box = true) ?pro ?(pro_space = true) ?constraint_ctx
860860
update_config_maybe_disabled c ptyp_loc ptyp_attributes
861861
@@ fun c ->
862862
( match pro with
863-
| Some pro -> fmt_constraint_sep ~pro_space c pro
864-
| None -> noop )
863+
| Some pro -> fmt_constraint_sep ~pro_space c pro
864+
| None -> noop )
865865
$
866866
let doc, atrs = doc_atrs ptyp_attributes in
867867
Cmts.fmt c ptyp_loc
@@ -1131,8 +1131,8 @@ and fmt_pattern ?ext c ?pro ?parens ?(box = false)
11311131
let parens = match parens with Some b -> b | None -> parenze_pat xpat in
11321132
(match ctx0 with Pat {ppat_desc= Ppat_tuple _; _} -> hvbox 0 | _ -> Fn.id)
11331133
@@ ( match ppat_desc with
1134-
| Ppat_or _ -> fun k -> Cmts.fmt c ppat_loc @@ k
1135-
| _ -> fun k -> Cmts.fmt c ppat_loc @@ (fmt_opt pro $ k) )
1134+
| Ppat_or _ -> fun k -> Cmts.fmt c ppat_loc @@ k
1135+
| _ -> fun k -> Cmts.fmt c ppat_loc @@ (fmt_opt pro $ k) )
11361136
@@ hovbox_if box 0
11371137
@@ fmt_pattern_attributes c xpat
11381138
@@
@@ -1533,7 +1533,7 @@ and fmt_indexop_access c ctx ~fmt_atrs ~has_attr ~parens x =
15331533
(str ";" $ space_break)
15341534
(sub_exp ~ctx >> fmt_expression c) ) )
15351535
$ opt pia_rhs (fun e ->
1536-
fmt_assign_arrow c $ fmt_expression c (sub_exp ~ctx e) ) )
1536+
fmt_assign_arrow c $ fmt_expression c (sub_exp ~ctx e) ) )
15371537
$ fmt_atrs ) )
15381538

15391539
(** Format a [Pexp_function]. [wrap_intro] wraps up to after the [->] and is
@@ -2738,8 +2738,8 @@ and fmt_expression c ?(box = true) ?(pro = noop) ?eol ?parens
27382738
( fmt_pattern c ~pro:(if_newline "| ")
27392739
(sub_pat ~ctx pc_lhs)
27402740
$ opt pc_guard (fun g ->
2741-
space_break $ str "when "
2742-
$ fmt_expression c (sub_exp ~ctx g) )
2741+
space_break $ str "when "
2742+
$ fmt_expression c (sub_exp ~ctx g) )
27432743
$ space_break $ str "->"
27442744
$ fmt_if parens_here (str " (") ) )
27452745
$ break 1 2
@@ -3049,10 +3049,9 @@ and fmt_lazy c ~ctx ?(pro = noop) ~fmt_atrs ~infix_ext_attrs ~parens e =
30493049
and fmt_beginend c ~loc ?(box = true) ?(pro = noop) ~ctx ~ctx0 ~fmt_atrs
30503050
~infix_ext_attrs ~indent_wrap ?eol e =
30513051
let cmts_before = Cmts.fmt_before c ?eol loc in
3052-
let begin_ = fmt_infix_ext_attrs c ~pro:(str "begin") infix_ext_attrs
3053-
and end_ =
3054-
(if not box then break 1000 (-2) else break 1000 0)
3055-
$ str "end" $ fmt_atrs
3052+
let begin_ = fmt_infix_ext_attrs c ~pro:(str "begin") infix_ext_attrs in
3053+
let end_ =
3054+
(if not box then break 1000 (-2) else break 1 0) $ str "end" $ fmt_atrs
30563055
in
30573056
let box_beginend_sb = Params.Exp.box_beginend_subexpr c.conf ~ctx ~ctx0 in
30583057
let beginend_box =
@@ -3446,8 +3445,8 @@ and fmt_case c ctx ~first ~last case =
34463445
( hvbox 0
34473446
( fmt_pattern c ~pro:p.bar ~parens:paren_lhs xlhs
34483447
$ opt pc_guard (fun g ->
3449-
break 1 2 $ str "when " $ fmt_expression c (sub_exp ~ctx g) )
3450-
)
3448+
break 1 2 $ str "when "
3449+
$ fmt_expression c (sub_exp ~ctx g) ) )
34513450
$ p.break_before_arrow $ str "->" $ p.break_after_arrow
34523451
$ p.open_paren_branch )
34533452
$ p.break_after_opening_paren
@@ -3783,9 +3782,9 @@ and fmt_type_extension c ctx
37833782
$ str " +="
37843783
$ fmt_private_flag c ptyext_private
37853784
$ list_fl ptyext_constructors (fun ~first ~last:_ x ->
3786-
let bar_fits = if first then "" else "| " in
3787-
cbreak ~fits:("", 1, bar_fits) ~breaks:("", 0, "| ")
3788-
$ fmt_ctor x ) )
3785+
let bar_fits = if first then "" else "| " in
3786+
cbreak ~fits:("", 1, bar_fits) ~breaks:("", 0, "| ")
3787+
$ fmt_ctor x ) )
37893788
$ fmt_item_attributes c ~pre:(Break (1, 0)) attrs_after )
37903789

37913790
and fmt_type_exception ~pre c ctx
@@ -4106,46 +4105,46 @@ and fmt_class_types c ~pre ~sep cls =
41064105
and fmt_class_exprs c cls =
41074106
hvbox 0
41084107
@@ list_fl cls (fun ~first ~last:_ cl ->
4109-
update_config_maybe_disabled_attrs c cl.pci_loc cl.pci_attributes
4110-
@@ fun c ->
4111-
let ctx = Cd cl in
4112-
let xargs = cl.pci_args in
4113-
let ext = cl.pci_attributes.attrs_extension in
4114-
let doc_before, doc_after, attrs_before, attrs_after =
4115-
let force_before = not (Cl.is_simple cl.pci_expr) in
4116-
fmt_docstring_around_item_attrs ~force_before c cl.pci_attributes
4117-
in
4118-
let class_expr =
4119-
let pro =
4120-
box_fun_decl_args c 2
4121-
( hovbox 2
4122-
( str (if first then "class" else "and")
4123-
$ fmt_if first (fmt_extension_suffix c ext)
4124-
$ fmt_attributes c ~pre:(Break (1, 0)) attrs_before
4125-
$ fmt_virtual_flag c cl.pci_virt
4126-
$ space_break
4127-
$ fmt_class_params c ctx cl.pci_params
4128-
$ fmt_str_loc c cl.pci_name )
4129-
$ fmt_if (not (List.is_empty xargs)) space_break
4130-
$ wrap_fun_decl_args c (fmt_class_fun_args c xargs) )
4131-
in
4132-
let intro =
4133-
match cl.pci_constraint with
4134-
| Some ty ->
4135-
fmt_class_type c
4136-
~pro:(pro $ str " :" $ space_break)
4137-
(sub_cty ~ctx ty)
4138-
| None -> pro
4139-
in
4140-
hovbox 2
4141-
( hovbox 2 (intro $ space_break $ str "=")
4142-
$ space_break
4143-
$ fmt_class_expr c (sub_cl ~ctx cl.pci_expr) )
4144-
$ fmt_item_attributes c ~pre:(Break (1, 0)) attrs_after
4145-
in
4146-
fmt_if (not first) (str "\n" $ force_break)
4147-
$ hovbox 0
4148-
@@ Cmts.fmt c cl.pci_loc (doc_before $ class_expr $ doc_after) )
4108+
update_config_maybe_disabled_attrs c cl.pci_loc cl.pci_attributes
4109+
@@ fun c ->
4110+
let ctx = Cd cl in
4111+
let xargs = cl.pci_args in
4112+
let ext = cl.pci_attributes.attrs_extension in
4113+
let doc_before, doc_after, attrs_before, attrs_after =
4114+
let force_before = not (Cl.is_simple cl.pci_expr) in
4115+
fmt_docstring_around_item_attrs ~force_before c cl.pci_attributes
4116+
in
4117+
let class_expr =
4118+
let pro =
4119+
box_fun_decl_args c 2
4120+
( hovbox 2
4121+
( str (if first then "class" else "and")
4122+
$ fmt_if first (fmt_extension_suffix c ext)
4123+
$ fmt_attributes c ~pre:(Break (1, 0)) attrs_before
4124+
$ fmt_virtual_flag c cl.pci_virt
4125+
$ space_break
4126+
$ fmt_class_params c ctx cl.pci_params
4127+
$ fmt_str_loc c cl.pci_name )
4128+
$ fmt_if (not (List.is_empty xargs)) space_break
4129+
$ wrap_fun_decl_args c (fmt_class_fun_args c xargs) )
4130+
in
4131+
let intro =
4132+
match cl.pci_constraint with
4133+
| Some ty ->
4134+
fmt_class_type c
4135+
~pro:(pro $ str " :" $ space_break)
4136+
(sub_cty ~ctx ty)
4137+
| None -> pro
4138+
in
4139+
hovbox 2
4140+
( hovbox 2 (intro $ space_break $ str "=")
4141+
$ space_break
4142+
$ fmt_class_expr c (sub_cl ~ctx cl.pci_expr) )
4143+
$ fmt_item_attributes c ~pre:(Break (1, 0)) attrs_after
4144+
in
4145+
fmt_if (not first) (str "\n" $ force_break)
4146+
$ hovbox 0
4147+
@@ Cmts.fmt c cl.pci_loc (doc_before $ class_expr $ doc_after) )
41494148

41504149
and fmt_module c ctx ?rec_ ?epi ?(can_sparse = false) keyword ?(eqty = "=")
41514150
name xargs xbody xmty ~attrs ~rec_flag =
@@ -4243,13 +4242,13 @@ and fmt_module c ctx ?rec_ ?epi ?(can_sparse = false) keyword ?(eqty = "=")
42434242
$ fmt_item_attributes c ~pre:(Break (1, 0)) attrs_after
42444243
$ doc_after
42454244
$ opt epi (fun epi ->
4246-
fmt_or compact
4247-
(fmt_or
4248-
( Option.is_some blk_b.epi
4249-
&& not c.conf.fmt_opts.ocp_indent_compat.v )
4250-
(str " ") space_break )
4251-
(break 1 (-2))
4252-
$ epi ) )
4245+
fmt_or compact
4246+
(fmt_or
4247+
( Option.is_some blk_b.epi
4248+
&& not c.conf.fmt_opts.ocp_indent_compat.v )
4249+
(str " ") space_break )
4250+
(break 1 (-2))
4251+
$ epi ) )
42534252

42544253
and fmt_module_declaration c ~rec_flag ~first {ast= pmd; _} =
42554254
protect c (Md pmd)

test/passing/refs.ahrefs/attributes.ml.ref

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -576,11 +576,7 @@ let _ =
576576

577577
let _ = ((module[@a] X) [@b])
578578

579-
let _ =
580-
begin[@a]
581-
x
582-
end
583-
[@b]
579+
let _ = begin[@a] x end [@b]
584580

585581
let fold input ~init ~f =
586582
foldi input ~init ~f:(fun (_ : int) acc x -> f acc x) [@nontail]

test/passing/refs.ahrefs/exp_grouping-parens.ml.ref

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -283,19 +283,10 @@ let _ = begin%ext end
283283
let _ = begin%ext end (* foo *)
284284
let _ = x y
285285
let _ = (* foo *) x y
286-
let _ =
287-
begin%ext
288-
x y
289-
end
290-
let _ =
291-
begin%ext
292-
(* foo *) x y
293-
end
286+
let _ = begin%ext x y end
287+
let _ = begin%ext (* foo *) x y end
294288

295-
let _ =
296-
begin[@landmark "parse_constant_dividends"]
297-
market_data_items := ()
298-
end
289+
let _ = begin[@landmark "parse_constant_dividends"] market_data_items := () end
299290

300291
let () = if a then b (* asd *)
301292

@@ -531,11 +522,7 @@ let main =
531522
| A -> aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
532523
| B -> bbbbbbbbbbbbbbbbbbbbbb
533524

534-
let _ =
535-
begin
536-
a
537-
end
538-
[@a]
525+
let _ = begin a end [@a]
539526

540527
let () =
541528
fooooo

test/passing/refs.ahrefs/exp_grouping.ml.ref

Lines changed: 6 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -316,27 +316,12 @@ let _ = begin end
316316
let _ = begin end (* foo *)
317317
let _ = begin%ext end
318318
let _ = begin%ext end (* foo *)
319-
let _ =
320-
begin
321-
x y
322-
end
323-
let _ =
324-
begin
325-
(* foo *) x y
326-
end
327-
let _ =
328-
begin%ext
329-
x y
330-
end
331-
let _ =
332-
begin%ext
333-
(* foo *) x y
334-
end
319+
let _ = begin x y end
320+
let _ = begin (* foo *) x y end
321+
let _ = begin%ext x y end
322+
let _ = begin%ext (* foo *) x y end
335323

336-
let _ =
337-
begin[@landmark "parse_constant_dividends"]
338-
market_data_items := ()
339-
end
324+
let _ = begin[@landmark "parse_constant_dividends"] market_data_items := () end
340325

341326
let () =
342327
if a then begin
@@ -611,11 +596,7 @@ let main =
611596
| A -> aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
612597
| B -> bbbbbbbbbbbbbbbbbbbbbb
613598

614-
let _ =
615-
begin
616-
a
617-
end
618-
[@a]
599+
let _ = begin a end [@a]
619600

620601
let () =
621602
fooooo

test/passing/refs.ahrefs/extensions_exp_grouping.ml.ref

Lines changed: 6 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,9 @@
1-
let _ =
2-
begin%ext
3-
y >>= z
4-
end
5-
let _ =
6-
[%ext
7-
begin
8-
y >>= z
9-
end]
10-
let _ =
11-
x
12-
>>= begin%ext
13-
y >>= z
14-
end
15-
let _ =
16-
x
17-
>>= [%ext
18-
begin
19-
y >>= z
20-
end]
21-
let _ =
22-
f
23-
begin%ext
24-
y >>= z
25-
end
26-
let _ =
27-
f
28-
[%ext
29-
begin
30-
y >>= z
31-
end]
1+
let _ = begin%ext y >>= z end
2+
let _ = [%ext begin y >>= z end]
3+
let _ = x >>= begin%ext y >>= z end
4+
let _ = x >>= [%ext begin y >>= z end]
5+
let _ = f begin%ext y >>= z end
6+
let _ = f [%ext begin y >>= z end]
327

338
let _ = (module%ext S)
349
let _ = [%ext (module S)]

test/passing/refs.ahrefs/reformat_string.ml.ref

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,9 @@ let _ = (* test *) "asd"
66

77
let _ = "asd"
88

9-
let _ =
10-
begin
11-
(* te""st *) "asd"
12-
end
13-
14-
let _ =
15-
begin
16-
"asd"
17-
end
9+
let _ = begin (* te""st *) "asd" end
10+
11+
let _ = begin "asd" end
1812

1913
let _ = 'a'
2014

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Warning: source.ml:3419 exceeds the margin
2-
Warning: source.ml:6553 exceeds the margin
3-
Warning: source.ml:6987 exceeds the margin
4-
Warning: source.ml:7819 exceeds the margin
1+
Warning: source.ml:3417 exceeds the margin
2+
Warning: source.ml:6551 exceeds the margin
3+
Warning: source.ml:6985 exceeds the margin
4+
Warning: source.ml:7817 exceeds the margin

test/passing/refs.ahrefs/source.ml.ref

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,7 @@ let () =
9696
assert%foo[@foo] true;
9797
lazy%foo[@foo] x;
9898
object%foo[@foo] end;
99-
begin%foo[@foo]
100-
3
101-
end;
99+
begin%foo[@foo] 3 end;
102100
new%foo[@foo] x;
103101

104102
match%foo[@foo] () with

0 commit comments

Comments
 (0)