Skip to content

Commit 1646c68

Browse files
Improve indentation of end keyword in begin end enclosed submatch (#2742)
1 parent 96cc58c commit 1646c68

21 files changed

+261
-83
lines changed

CHANGES.md

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,43 @@ profile. This started with version 0.26.0.
1010

1111
- Fix dropped comment in `(function _ -> x (* cmt *))` (#2739, @Julow)
1212

13+
- \* `cases-matching-exp-indent=compact` does not impact `begin end` nodes that
14+
don't have a match inside. (#2742, @EmileTrotignon)
15+
```ocaml
16+
(* before *)
17+
begin match () with
18+
| () -> begin
19+
f x
20+
end
21+
end
22+
(* after *)
23+
begin match () with
24+
| () -> begin
25+
f x
26+
end
27+
end
28+
```
29+
30+
31+
### Changed
32+
33+
- indentation of the `end` keyword in a match-case is now always at least 2. (#2742, @EmileTrotignon)
34+
```ocaml
35+
(* before *)
36+
begin match () with
37+
| () -> begin
38+
match () with
39+
| () -> ()
40+
end
41+
end
42+
(* after *)
43+
begin match () with
44+
| () -> begin
45+
match () with
46+
| () -> ()
47+
end
48+
end
49+
```
1350
## 0.28.1
1451

1552
### Highlight
@@ -38,7 +75,7 @@ profile. This started with version 0.26.0.
3875
### Added
3976

4077
- Added option `module-indent` option (#2711, @HPRIOR) to control the indentation
41-
of items within modules. This affects modules and signatures. For example,
78+
of items within modules. This affects modules and signatures. For example,
4279
module-indent=4:
4380
```ocaml
4481
module type M = sig
@@ -146,7 +183,7 @@ profile. This started with version 0.26.0.
146183
- Fix a crash where `type%e nonrec t = t` was formatted as `type nonrec%e t = t`,
147184
which is invalid syntax. (#2712, @EmileTrotignon)
148185

149-
- Fix commandline parsing being quadratic in the number of arguments
186+
- Fix commandline parsing being quadratic in the number of arguments
150187
(#2724, @let-def)
151188

152189
- \* Fix `;;` being added after a documentation comment (#2683, @EmileTrotignon)

lib/Params.ml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -512,7 +512,9 @@ let get_cases (c : Conf.t) ~fmt_infix_ext_attrs ~ctx ~first ~last
512512
Pexp_function _ | Pexp_match _ | Pexp_try _ | Pexp_let _
513513
; _ }
514514
| Lb {pvb_body= Pfunction_cases _; _} )
515-
, (Pexp_match _ | Pexp_try _ | Pexp_beginend _) ) ) ->
515+
, ( Pexp_match _ | Pexp_try _
516+
| Pexp_beginend ({pexp_desc= Pexp_match _ | Pexp_try _; _}, _) ) )
517+
) ->
516518
2
517519
| _, _ -> c.fmt_opts.cases_exp_indent.v
518520
in
@@ -542,9 +544,7 @@ let get_cases (c : Conf.t) ~fmt_infix_ext_attrs ~ctx ~first ~last
542544
; _ }
543545
when not cmts_before ->
544546
let close_paren =
545-
let offset =
546-
match c.fmt_opts.break_cases.v with `Nested -> 0 | _ -> -2
547-
in
547+
let offset = if indent >= 2 then 2 - indent else 0 in
548548
fits_breaks " end" ~level:1 ~hint:(1000, offset) "end"
549549
in
550550
( break 1 0 $ fmt_infix_ext_attrs ~pro:(str "begin") infix_ext_attrs

test/passing/gen/dune.inc

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -872,6 +872,24 @@
872872
(package ocamlformat)
873873
(action (diff cases_exp_grouping.ml.err cases_exp_grouping.ml.stderr)))
874874

875+
(rule
876+
(deps .ocamlformat)
877+
(package ocamlformat)
878+
(action
879+
(with-stdout-to cases_exp_indent.ml.stdout
880+
(with-stderr-to cases_exp_indent.ml.stderr
881+
(run %{bin:ocamlformat} --name cases_exp_indent.ml --margin-check --nested-match=align --cases-exp-indent=4 %{dep:../tests/cases_exp_indent.ml})))))
882+
883+
(rule
884+
(alias runtest)
885+
(package ocamlformat)
886+
(action (diff cases_exp_indent.ml.ref cases_exp_indent.ml.stdout)))
887+
888+
(rule
889+
(alias runtest)
890+
(package ocamlformat)
891+
(action (diff cases_exp_indent.ml.err cases_exp_indent.ml.stderr)))
892+
875893
(rule
876894
(deps .ocamlformat)
877895
(enabled_if (<> %{os_type} Win32))

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

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ let _ =
88
| B -> fooooooooooooo
99
| C -> fooooooooooooo
1010
| D -> fooooooooooooo
11-
end
11+
end
1212
[@@ocamlformat "break-cases=fit"]
1313

1414
let _ =
@@ -37,59 +37,59 @@ let _ =
3737
| A -> begin
3838
match B with
3939
| A -> fooooooooooooo
40-
end
40+
end
4141
| A -> begin
4242
match B with
4343
| A -> fooooooooooooo
4444
| B -> fooooooooooooo
45-
end
45+
end
4646
| A -> begin
4747
match B with
4848
| A -> fooooooooooooo
4949
| B -> fooooooooooooo
5050
| C -> fooooooooooooo
5151
| D -> fooooooooooooo
52-
end
52+
end
5353
[@@ocamlformat "break-cases=toplevel"]
5454

5555
let _ =
5656
match x with
5757
| A -> begin
5858
match B with
5959
| A -> fooooooooooooo
60-
end
60+
end
6161
| A -> begin
6262
match B with
6363
| A -> fooooooooooooo
6464
| B -> fooooooooooooo
65-
end
65+
end
6666
| A -> begin
6767
match B with
6868
| A -> fooooooooooooo
6969
| B -> fooooooooooooo
7070
| C -> fooooooooooooo
7171
| D -> fooooooooooooo
72-
end
72+
end
7373
[@@ocamlformat "break-cases=fit-or-vertical"]
7474

7575
let _ =
7676
match x with
7777
| A -> begin
7878
match B with
7979
| A -> fooooooooooooo
80-
end
80+
end
8181
| A -> begin
8282
match B with
8383
| A -> fooooooooooooo
8484
| B -> fooooooooooooo
85-
end
85+
end
8686
| A -> begin
8787
match B with
8888
| A -> fooooooooooooo
8989
| B -> fooooooooooooo
9090
| C -> fooooooooooooo
9191
| D -> fooooooooooooo
92-
end
92+
end
9393
[@@ocamlformat "break-cases=all"]
9494

9595
let a =
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
[@@@ocamlformat "break-cases=fit-or-vertical"]
2+
3+
let () =
4+
begin match () with
5+
| () -> begin
6+
aaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaaaaaaa aaaaaaaaaaaaaa aaaaaaaaaaaa
7+
aaaaaaaaaaaaa ()
8+
end
9+
| () ->
10+
aaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaaaaaaa aaaaaaaaaaaaaa aaaaaaaaaaaa
11+
aaaaaaaaaaaaa ()
12+
| () -> begin
13+
match () with
14+
| () -> ()
15+
end
16+
| () ->
17+
(match () with
18+
| () -> ())
19+
| () -> begin
20+
match () with
21+
| () -> ()
22+
end
23+
end

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ let run (main : unit -> unit) : unit =
4242
| None ->
4343
exchanger := Some (n, k);
4444
dequeue ()
45-
end
45+
end
4646
in
4747
spawn main
4848

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -284,19 +284,19 @@ let _ =
284284
| A -> begin
285285
match B with
286286
| A -> fooooooooooooo
287-
end
287+
end
288288
| A -> begin
289289
match B with
290290
| A -> fooooooooooooo
291291
| B -> fooooooooooooo
292-
end
292+
end
293293
| A -> begin
294294
match B with
295295
| A -> fooooooooooooo
296296
| B -> fooooooooooooo
297297
| C -> fooooooooooooo
298298
| D -> fooooooooooooo
299-
end
299+
end
300300

301301
let () =
302302
begin

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ let f = function
22
| zoo -> begin
33
foo;
44
bar
5-
end
5+
end
66
let g = function
77
| zoo ->
88
foo;

0 commit comments

Comments
 (0)