From 3070100eaaa9f16b4e287843d39d8790f79807b0 Mon Sep 17 00:00:00 2001 From: Jules Aguillon Date: Tue, 12 Nov 2024 11:05:38 +0100 Subject: [PATCH 1/5] 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"] ) = () --- lib/Fmt_ast.ml | 5 +++-- test/passing/tests/attributes.ml | 5 +++-- test/passing/tests/js_source.ml.err | 10 +++++----- test/passing/tests/js_source.ml.ocp | 3 +-- test/passing/tests/js_source.ml.ref | 3 +-- test/passing/tests/let_binding-deindent-fun.ml.ref | 3 ++- test/passing/tests/let_binding-in_indent.ml.ref | 3 ++- test/passing/tests/let_binding-indent.ml.ref | 3 ++- test/passing/tests/let_binding.ml.ref | 3 ++- test/passing/tests/list_normalized.ml.ref | 3 +-- test/passing/tests/shortcut_ext_attr.ml | 3 +-- test/passing/tests/source-conventional.ml.err | 10 +++++----- test/passing/tests/source-conventional.ml.ref | 3 +-- test/passing/tests/source.ml.err | 6 +++--- test/passing/tests/source.ml.ref | 3 +-- 15 files changed, 33 insertions(+), 33 deletions(-) diff --git a/lib/Fmt_ast.ml b/lib/Fmt_ast.ml index a39081077c..5012600916 100644 --- a/lib/Fmt_ast.ml +++ b/lib/Fmt_ast.ml @@ -1086,8 +1086,9 @@ and fmt_pattern_attributes c xpat k = false | _ -> true ) in - Params.parens_if parens_attr c.conf - (k $ fmt_attributes c ~pre:Space attrs) + hvbox_if parens_attr 1 + (Params.parens_if parens_attr c.conf + (k $ fmt_attributes c ~pre:Space attrs) ) and fmt_pattern ?ext c ?pro ?parens ?(box = false) ({ctx= ctx0; ast= pat} as xpat) = diff --git a/test/passing/tests/attributes.ml b/test/passing/tests/attributes.ml index 0027cc3557..a073261eab 100644 --- a/test/passing/tests/attributes.ml +++ b/test/passing/tests/attributes.ml @@ -364,14 +364,15 @@ let pp f ({cf_interface; cf_is_objc_block; cf_virtual} [@warning "+9"]) = () let pp f ({cf_assign_last_arg; cf_injected_destructor; cf_interface} - [@warning "+9"] ) = + [@warning "+9"] ) = () let pp f ({ cf_assign_last_arg ; cf_injected_destructor ; cf_interface - ; cf_is_objc_block } [@warning "+9"] ) = + ; cf_is_objc_block } + [@warning "+9"] ) = () let _ = f ((* comments *) "c" [@attributes]) diff --git a/test/passing/tests/js_source.ml.err b/test/passing/tests/js_source.ml.err index 2383010c63..0d99301bed 100644 --- a/test/passing/tests/js_source.ml.err +++ b/test/passing/tests/js_source.ml.err @@ -1,5 +1,5 @@ -Warning: tests/js_source.ml:9564 exceeds the margin -Warning: tests/js_source.ml:9668 exceeds the margin -Warning: tests/js_source.ml:9727 exceeds the margin -Warning: tests/js_source.ml:9810 exceeds the margin -Warning: tests/js_source.ml:10308 exceeds the margin +Warning: tests/js_source.ml:9563 exceeds the margin +Warning: tests/js_source.ml:9667 exceeds the margin +Warning: tests/js_source.ml:9726 exceeds the margin +Warning: tests/js_source.ml:9809 exceeds the margin +Warning: tests/js_source.ml:10307 exceeds the margin diff --git a/test/passing/tests/js_source.ml.ocp b/test/passing/tests/js_source.ml.ocp index daf6d6ca75..a126c2d2f6 100644 --- a/test/passing/tests/js_source.ml.ocp +++ b/test/passing/tests/js_source.ml.ocp @@ -118,8 +118,7 @@ let () = match[@foo] () with | [%foo? (* Pattern expressions *) - ((lazy x) - [@foo])] -> () + ((lazy x) [@foo])] -> () | [%foo? ((exception x) [@foo])] -> ()] ;; diff --git a/test/passing/tests/js_source.ml.ref b/test/passing/tests/js_source.ml.ref index ee7a9b2557..3b8f3ab7ad 100644 --- a/test/passing/tests/js_source.ml.ref +++ b/test/passing/tests/js_source.ml.ref @@ -118,8 +118,7 @@ let () = match[@foo] () with | [%foo? (* Pattern expressions *) - ((lazy x) - [@foo])] -> () + ((lazy x) [@foo])] -> () | [%foo? ((exception x) [@foo])] -> ()] ;; diff --git a/test/passing/tests/let_binding-deindent-fun.ml.ref b/test/passing/tests/let_binding-deindent-fun.ml.ref index 7967c86018..84e43e1975 100644 --- a/test/passing/tests/let_binding-deindent-fun.ml.ref +++ b/test/passing/tests/let_binding-deindent-fun.ml.ref @@ -248,7 +248,8 @@ let copy from ~into : unit = ; pulse_summaries_count ; topl_reachable_calls ; timeouts - ; timings } [@warning "+9"] ) = + ; timings } + [@warning "+9"] ) = () in () diff --git a/test/passing/tests/let_binding-in_indent.ml.ref b/test/passing/tests/let_binding-in_indent.ml.ref index 3ad01c8c4f..b85987d3b0 100644 --- a/test/passing/tests/let_binding-in_indent.ml.ref +++ b/test/passing/tests/let_binding-in_indent.ml.ref @@ -248,7 +248,8 @@ let copy from ~into : unit = ; pulse_summaries_count ; topl_reachable_calls ; timeouts - ; timings } [@warning "+9"] ) = + ; timings } + [@warning "+9"] ) = () in () diff --git a/test/passing/tests/let_binding-indent.ml.ref b/test/passing/tests/let_binding-indent.ml.ref index 0e4ecd6511..ad31bca71c 100644 --- a/test/passing/tests/let_binding-indent.ml.ref +++ b/test/passing/tests/let_binding-indent.ml.ref @@ -248,7 +248,8 @@ let copy from ~into : unit = ; pulse_summaries_count ; topl_reachable_calls ; timeouts - ; timings } [@warning "+9"] ) = + ; timings } + [@warning "+9"] ) = () in () diff --git a/test/passing/tests/let_binding.ml.ref b/test/passing/tests/let_binding.ml.ref index ea5f50112b..347af03abc 100644 --- a/test/passing/tests/let_binding.ml.ref +++ b/test/passing/tests/let_binding.ml.ref @@ -248,7 +248,8 @@ let copy from ~into : unit = ; pulse_summaries_count ; topl_reachable_calls ; timeouts - ; timings } [@warning "+9"] ) = + ; timings } + [@warning "+9"] ) = () in () diff --git a/test/passing/tests/list_normalized.ml.ref b/test/passing/tests/list_normalized.ml.ref index 4c49bc6cba..5ac9774761 100644 --- a/test/passing/tests/list_normalized.ml.ref +++ b/test/passing/tests/list_normalized.ml.ref @@ -47,8 +47,7 @@ let (*a*) [ x (*b*) ; (*c*) - (y - [@attr] ) + (y [@attr]) (*d*) (*e*) ] (*f*) = e diff --git a/test/passing/tests/shortcut_ext_attr.ml b/test/passing/tests/shortcut_ext_attr.ml index a7ab6c8e95..8628e83c4e 100644 --- a/test/passing/tests/shortcut_ext_attr.ml +++ b/test/passing/tests/shortcut_ext_attr.ml @@ -31,8 +31,7 @@ let () = match[@foo] () with | [%foo? (* Pattern expressions *) - ((lazy x) - [@foo] )] -> + ((lazy x) [@foo])] -> () | [%foo? ((exception x) [@foo])] -> ()] diff --git a/test/passing/tests/source-conventional.ml.err b/test/passing/tests/source-conventional.ml.err index 13e849341c..957d18a985 100644 --- a/test/passing/tests/source-conventional.ml.err +++ b/test/passing/tests/source-conventional.ml.err @@ -1,5 +1,5 @@ -Warning: tests/source.ml:926 exceeds the margin -Warning: tests/source.ml:1001 exceeds the margin -Warning: tests/source.ml:6621 exceeds the margin -Warning: tests/source.ml:7079 exceeds the margin -Warning: tests/source.ml:8656 exceeds the margin +Warning: tests/source.ml:925 exceeds the margin +Warning: tests/source.ml:1000 exceeds the margin +Warning: tests/source.ml:6620 exceeds the margin +Warning: tests/source.ml:7078 exceeds the margin +Warning: tests/source.ml:8655 exceeds the margin diff --git a/test/passing/tests/source-conventional.ml.ref b/test/passing/tests/source-conventional.ml.ref index f429b6ad23..250ac98ab2 100644 --- a/test/passing/tests/source-conventional.ml.ref +++ b/test/passing/tests/source-conventional.ml.ref @@ -115,8 +115,7 @@ let () = match[@foo] () with | [%foo? (* Pattern expressions *) - ((lazy x) - [@foo])] -> + ((lazy x) [@foo])] -> () | [%foo? ((exception x) [@foo])] -> ()] diff --git a/test/passing/tests/source.ml.err b/test/passing/tests/source.ml.err index 8c9ac74699..12cdd52191 100644 --- a/test/passing/tests/source.ml.err +++ b/test/passing/tests/source.ml.err @@ -1,3 +1,3 @@ -Warning: tests/source.ml:702 exceeds the margin -Warning: tests/source.ml:2319 exceeds the margin -Warning: tests/source.ml:9158 exceeds the margin +Warning: tests/source.ml:701 exceeds the margin +Warning: tests/source.ml:2318 exceeds the margin +Warning: tests/source.ml:9157 exceeds the margin diff --git a/test/passing/tests/source.ml.ref b/test/passing/tests/source.ml.ref index a5324a5fbb..63eb02b32f 100644 --- a/test/passing/tests/source.ml.ref +++ b/test/passing/tests/source.ml.ref @@ -128,8 +128,7 @@ let () = match[@foo] () with | [%foo? (* Pattern expressions *) - ((lazy x) - [@foo] )] -> + ((lazy x) [@foo])] -> () | [%foo? ((exception x) [@foo])] -> ()] From bed630f63d2e677f6f5725c9bfddeb2fab63026d Mon Sep 17 00:00:00 2001 From: Jules Aguillon Date: Tue, 12 Nov 2024 11:10:56 +0100 Subject: [PATCH 2/5] Update CHANGES --- CHANGES.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGES.md b/CHANGES.md index 48ee697df6..ee655b67cc 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -110,6 +110,7 @@ profile. This started with version 0.26.0. - Fix missing parentheses around a let in class expressions (#2599, @Julow) - Fix formatting of paragraphs in lists in documentation (#2607, @Julow) - Avoid unwanted space in references and links text in documentation (#2608, @Julow) +- \* Improve the indentation of attributes in patterns (#2613, @Julow) ## 0.26.2 (2024-04-18) From 2adb89c0504905ec88b2c197fb7fc3039f64da40 Mon Sep 17 00:00:00 2001 From: Jules Aguillon Date: Tue, 12 Nov 2024 12:20:07 +0100 Subject: [PATCH 3/5] Avoid breaking after a lone '}' --- lib/Fmt_ast.ml | 10 +++++++++- test/passing/tests/record-402.ml.ref | 8 ++++++++ test/passing/tests/record-default.ml.ref | 9 +++++++++ test/passing/tests/record-loose.ml.ref | 8 ++++++++ test/passing/tests/record-tight_decl.ml.ref | 8 ++++++++ test/passing/tests/record.ml | 9 +++++++++ test/passing/tests/record.ml.ref | 8 ++++++++ 7 files changed, 59 insertions(+), 1 deletion(-) diff --git a/lib/Fmt_ast.ml b/lib/Fmt_ast.ml index 5012600916..0f326664eb 100644 --- a/lib/Fmt_ast.ml +++ b/lib/Fmt_ast.ml @@ -1086,7 +1086,15 @@ and fmt_pattern_attributes c xpat k = false | _ -> true ) in - hvbox_if parens_attr 1 + let box = + match xpat.ast.ppat_desc with + | (Ppat_record _ | Ppat_array _ | Ppat_list _) + when c.conf.fmt_opts.dock_collection_brackets.v -> + hovbox + | _ -> hvbox + in + box + (if parens_attr then 1 else 0) (Params.parens_if parens_attr c.conf (k $ fmt_attributes c ~pre:Space attrs) ) diff --git a/test/passing/tests/record-402.ml.ref b/test/passing/tests/record-402.ml.ref index 2870ab75da..2819f82bcd 100644 --- a/test/passing/tests/record-402.ml.ref +++ b/test/passing/tests/record-402.ml.ref @@ -67,3 +67,11 @@ let _ = () in () + +let foo + ({ foooooooooooooooooooooo + ; invalidation_trace + ; access_trace + ; must_be_valid_reason } + [@warning "+missing-record-field-pattern"] ) = + () diff --git a/test/passing/tests/record-default.ml.ref b/test/passing/tests/record-default.ml.ref index 052da27faa..35fb6cba11 100644 --- a/test/passing/tests/record-default.ml.ref +++ b/test/passing/tests/record-default.ml.ref @@ -66,3 +66,12 @@ let _ = () in () + +let foo + ({ + foooooooooooooooooooooo; + invalidation_trace; + access_trace; + must_be_valid_reason; + } [@warning "+missing-record-field-pattern"]) = + () diff --git a/test/passing/tests/record-loose.ml.ref b/test/passing/tests/record-loose.ml.ref index d23530dca6..c1a69a7c46 100644 --- a/test/passing/tests/record-loose.ml.ref +++ b/test/passing/tests/record-loose.ml.ref @@ -67,3 +67,11 @@ let _ = () in () + +let foo + ({ foooooooooooooooooooooo + ; invalidation_trace + ; access_trace + ; must_be_valid_reason } + [@warning "+missing-record-field-pattern"] ) = + () diff --git a/test/passing/tests/record-tight_decl.ml.ref b/test/passing/tests/record-tight_decl.ml.ref index ad395ab753..402048c31d 100644 --- a/test/passing/tests/record-tight_decl.ml.ref +++ b/test/passing/tests/record-tight_decl.ml.ref @@ -67,3 +67,11 @@ let _ = () in () + +let foo + ({ foooooooooooooooooooooo + ; invalidation_trace + ; access_trace + ; must_be_valid_reason } + [@warning "+missing-record-field-pattern"] ) = + () diff --git a/test/passing/tests/record.ml b/test/passing/tests/record.ml index 14a9c28615..9ece04bbb7 100644 --- a/test/passing/tests/record.ml +++ b/test/passing/tests/record.ml @@ -73,3 +73,12 @@ let _ = () in () + +let foo + ({ + foooooooooooooooooooooo; + invalidation_trace; + access_trace; + must_be_valid_reason; + } [@warning "+missing-record-field-pattern"]) = + () diff --git a/test/passing/tests/record.ml.ref b/test/passing/tests/record.ml.ref index bd81ff95ae..f44841806c 100644 --- a/test/passing/tests/record.ml.ref +++ b/test/passing/tests/record.ml.ref @@ -67,3 +67,11 @@ let _ = () in () + +let foo + ({ foooooooooooooooooooooo + ; invalidation_trace + ; access_trace + ; must_be_valid_reason } + [@warning "+missing-record-field-pattern"] ) = + () From 51f7d7b53037a13571dfb7198a98aef60114548e Mon Sep 17 00:00:00 2001 From: Jules Aguillon Date: Tue, 12 Nov 2024 15:10:14 +0100 Subject: [PATCH 4/5] Box depends on break_separators --- lib/Fmt_ast.ml | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/lib/Fmt_ast.ml b/lib/Fmt_ast.ml index 5cac0d3fc6..dca6e9b0cf 100644 --- a/lib/Fmt_ast.ml +++ b/lib/Fmt_ast.ml @@ -1087,10 +1087,8 @@ and fmt_pattern_attributes c xpat k = | _ -> true ) in let box = - match xpat.ast.ppat_desc with - | (Ppat_record _ | Ppat_array _ | Ppat_list _) - when c.conf.fmt_opts.dock_collection_brackets.v -> - hovbox + match (xpat.ast.ppat_desc, c.conf.fmt_opts.break_separators.v) with + | (Ppat_record _ | Ppat_array _ | Ppat_list _), `After -> hovbox | _ -> hvbox in box From a85a23a945825556215c401b4bed12df7cf63a00 Mon Sep 17 00:00:00 2001 From: Jules Aguillon Date: Tue, 12 Nov 2024 16:07:47 +0100 Subject: [PATCH 5/5] Box depends on sequence_style --- lib/Fmt_ast.ml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/Fmt_ast.ml b/lib/Fmt_ast.ml index dca6e9b0cf..22a06bd2ba 100644 --- a/lib/Fmt_ast.ml +++ b/lib/Fmt_ast.ml @@ -1087,8 +1087,8 @@ and fmt_pattern_attributes c xpat k = | _ -> true ) in let box = - match (xpat.ast.ppat_desc, c.conf.fmt_opts.break_separators.v) with - | (Ppat_record _ | Ppat_array _ | Ppat_list _), `After -> hovbox + match (xpat.ast.ppat_desc, c.conf.fmt_opts.sequence_style.v) with + | (Ppat_record _ | Ppat_array _ | Ppat_list _), `Terminator -> hovbox | _ -> hvbox in box