Skip to content

Commit fb9b70f

Browse files
authored
Improve indentation of attributes in patterns (#2613)
* Improve indentation of attributes in patterns Small bug fix: let pp f ({cf_assign_last_arg; cf_injected_destructor; cf_interface} - [@warning "+9"] ) = + [@warning "+9"] ) = () * Avoid breaking after a lone '}'
1 parent 624eb83 commit fb9b70f

22 files changed

+90
-33
lines changed

CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ profile. This started with version 0.26.0.
110110
- Fix missing parentheses around a let in class expressions (#2599, @Julow)
111111
- Fix formatting of paragraphs in lists in documentation (#2607, @Julow)
112112
- Avoid unwanted space in references and links text in documentation (#2608, @Julow)
113+
- \* Improve the indentation of attributes in patterns (#2613, @Julow)
113114
- \* Avoid large indentation in patterns after `let%ext` (#2615, @Julow)
114115

115116
## 0.26.2 (2024-04-18)

lib/Fmt_ast.ml

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1086,8 +1086,15 @@ and fmt_pattern_attributes c xpat k =
10861086
false
10871087
| _ -> true )
10881088
in
1089-
Params.parens_if parens_attr c.conf
1090-
(k $ fmt_attributes c ~pre:Space attrs)
1089+
let box =
1090+
match (xpat.ast.ppat_desc, c.conf.fmt_opts.sequence_style.v) with
1091+
| (Ppat_record _ | Ppat_array _ | Ppat_list _), `Terminator -> hovbox
1092+
| _ -> hvbox
1093+
in
1094+
box
1095+
(if parens_attr then 1 else 0)
1096+
(Params.parens_if parens_attr c.conf
1097+
(k $ fmt_attributes c ~pre:Space attrs) )
10911098

10921099
and fmt_pattern ?ext c ?pro ?parens ?(box = false)
10931100
({ctx= ctx0; ast= pat} as xpat) =

test/passing/tests/attributes.ml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -364,14 +364,15 @@ let pp f ({cf_interface; cf_is_objc_block; cf_virtual} [@warning "+9"]) = ()
364364

365365
let pp f
366366
({cf_assign_last_arg; cf_injected_destructor; cf_interface}
367-
[@warning "+9"] ) =
367+
[@warning "+9"] ) =
368368
()
369369

370370
let pp f
371371
({ cf_assign_last_arg
372372
; cf_injected_destructor
373373
; cf_interface
374-
; cf_is_objc_block } [@warning "+9"] ) =
374+
; cf_is_objc_block }
375+
[@warning "+9"] ) =
375376
()
376377

377378
let _ = f ((* comments *) "c" [@attributes])
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
Warning: tests/js_source.ml:9564 exceeds the margin
2-
Warning: tests/js_source.ml:9668 exceeds the margin
3-
Warning: tests/js_source.ml:9727 exceeds the margin
4-
Warning: tests/js_source.ml:9810 exceeds the margin
5-
Warning: tests/js_source.ml:10308 exceeds the margin
1+
Warning: tests/js_source.ml:9563 exceeds the margin
2+
Warning: tests/js_source.ml:9667 exceeds the margin
3+
Warning: tests/js_source.ml:9726 exceeds the margin
4+
Warning: tests/js_source.ml:9809 exceeds the margin
5+
Warning: tests/js_source.ml:10307 exceeds the margin

test/passing/tests/js_source.ml.ocp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,8 +118,7 @@ let () =
118118
match[@foo] () with
119119
| [%foo?
120120
(* Pattern expressions *)
121-
((lazy x)
122-
[@foo])] -> ()
121+
((lazy x) [@foo])] -> ()
123122
| [%foo? ((exception x) [@foo])] -> ()]
124123
;;
125124

test/passing/tests/js_source.ml.ref

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,8 +118,7 @@ let () =
118118
match[@foo] () with
119119
| [%foo?
120120
(* Pattern expressions *)
121-
((lazy x)
122-
[@foo])] -> ()
121+
((lazy x) [@foo])] -> ()
123122
| [%foo? ((exception x) [@foo])] -> ()]
124123
;;
125124

test/passing/tests/let_binding-deindent-fun.ml.ref

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,8 @@ let copy from ~into : unit =
248248
; pulse_summaries_count
249249
; topl_reachable_calls
250250
; timeouts
251-
; timings } [@warning "+9"] ) =
251+
; timings }
252+
[@warning "+9"] ) =
252253
()
253254
in
254255
()

test/passing/tests/let_binding-in_indent.ml.ref

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,8 @@ let copy from ~into : unit =
248248
; pulse_summaries_count
249249
; topl_reachable_calls
250250
; timeouts
251-
; timings } [@warning "+9"] ) =
251+
; timings }
252+
[@warning "+9"] ) =
252253
()
253254
in
254255
()

test/passing/tests/let_binding-indent.ml.ref

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,8 @@ let copy from ~into : unit =
248248
; pulse_summaries_count
249249
; topl_reachable_calls
250250
; timeouts
251-
; timings } [@warning "+9"] ) =
251+
; timings }
252+
[@warning "+9"] ) =
252253
()
253254
in
254255
()

test/passing/tests/let_binding.ml.ref

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,8 @@ let copy from ~into : unit =
248248
; pulse_summaries_count
249249
; topl_reachable_calls
250250
; timeouts
251-
; timings } [@warning "+9"] ) =
251+
; timings }
252+
[@warning "+9"] ) =
252253
()
253254
in
254255
()

0 commit comments

Comments
 (0)