@@ -178,6 +178,10 @@ let update_config_maybe_disabled c loc l f =
178
178
let c = update_config c l in
179
179
maybe_disabled c loc l f
180
180
181
+ let update_config_maybe_disabled_attrs c loc attrs f =
182
+ let l = attrs.attrs_before @ attrs.attrs_after in
183
+ update_config_maybe_disabled c loc l f
184
+
181
185
let update_config_maybe_disabled_block c loc l f =
182
186
let fmt bdy = {empty with opn= Some (open_vbox 2 ); bdy; cls= close_box} in
183
187
let c = update_config c l in
@@ -463,6 +467,15 @@ let fmt_docstring_around_item ?is_val ?force_before ?fit c attrs =
463
467
in
464
468
(doc_before, doc_after, attrs)
465
469
470
+ (* * Returns the documentation before and after the item as well as the
471
+ [ext_attrs] before and after attributes, modified.
472
+ It is assumed that docstrings can only occurs in [attrs_after]. *)
473
+ let fmt_docstring_around_item_attrs ?is_val ?force_before ?fit c attrs =
474
+ let doc_before, doc_after, attrs_after =
475
+ fmt_docstring_around_item ?is_val ?force_before ?fit c attrs.attrs_after
476
+ in
477
+ (doc_before, doc_after, attrs.attrs_before, attrs_after)
478
+
466
479
let fmt_extension_suffix c ext =
467
480
opt ext (fun name -> str " %" $ fmt_str_loc c name)
468
481
@@ -2289,7 +2302,9 @@ and fmt_expression c ?(box = true) ?pro ?epi ?eol ?parens ?(indent_wrap = 0)
2289
2302
c.conf
2290
2303
( hvbox 2
2291
2304
(fmt_module c ctx keyword ~eqty: " :" name args (Some xbody)
2292
- xmty [] ~epi: (str " in" ) ~can_sparse ?ext ~rec_flag: false )
2305
+ xmty
2306
+ ~attrs: (Ast_helper.Attr. ext_attrs ?ext () )
2307
+ ~epi: (str " in" ) ~can_sparse ~rec_flag: false )
2293
2308
$ fmt " @;<1000 0>"
2294
2309
$ fmt_expression c (sub_exp ~ctx exp) )
2295
2310
$ fmt_atrs )
@@ -3622,20 +3637,16 @@ and fmt_signature_item c ?ext {ast= si; _} =
3622
3637
$ esp $ fmt_opt epi
3623
3638
$ fmt_item_attributes c ~pre: (Break (1 , 0 )) atrs )
3624
3639
$ doc_after )
3625
- | Psig_modtype mtd -> fmt_module_type_declaration ?ext c ctx mtd
3626
- | Psig_modtypesubst mtd ->
3627
- fmt_module_type_declaration ?ext ~eqty: " :=" c ctx mtd
3640
+ | Psig_modtype mtd -> fmt_module_type_declaration c ctx mtd
3641
+ | Psig_modtypesubst mtd -> fmt_module_type_declaration ~eqty: " :=" c ctx mtd
3628
3642
| Psig_module md ->
3629
3643
hvbox 0
3630
- (fmt_module_declaration ?ext c ~rec_flag: false ~first: true
3644
+ (fmt_module_declaration c ~rec_flag: false ~first: true
3631
3645
(sub_md ~ctx md) )
3632
- | Psig_modsubst ms -> hvbox 0 (fmt_module_substitution ?ext c ctx ms)
3646
+ | Psig_modsubst ms -> hvbox 0 (fmt_module_substitution c ctx ms)
3633
3647
| Psig_open od -> fmt_open_description ?ext c ~kw_attributes: [] od
3634
3648
| Psig_recmodule mds ->
3635
- fmt_recmodule c ctx mds
3636
- (fmt_module_declaration ?ext)
3637
- (fun x -> Md x)
3638
- sub_md
3649
+ fmt_recmodule c ctx mds fmt_module_declaration (fun x -> Md x) sub_md
3639
3650
| Psig_type (rec_flag , decls ) -> fmt_type c ?ext rec_flag decls ctx
3640
3651
| Psig_typext te -> fmt_type_extension ?ext c ctx te
3641
3652
| Psig_value vd -> fmt_value_description ?ext c ctx vd
@@ -3720,8 +3731,9 @@ and fmt_class_exprs ?ext c ctx cls =
3720
3731
$ hovbox 0
3721
3732
@@ Cmts. fmt c cl.pci_loc (doc_before $ class_exprs $ doc_after) )
3722
3733
3723
- and fmt_module c ctx ?rec_ ?ext ?epi ?(can_sparse = false ) keyword
3724
- ?(eqty = " =" ) name xargs xbody xmty attributes ~rec_flag =
3734
+ and fmt_module c ctx ?rec_ ?epi ?(can_sparse = false ) keyword ?(eqty = " =" )
3735
+ name xargs xbody xmty ~attrs ~rec_flag =
3736
+ let ext = attrs.attrs_extension in
3725
3737
let blk_t =
3726
3738
Option. value_map xmty ~default: empty ~f: (fun xmty ->
3727
3739
let blk = fmt_module_type ?rec_ c xmty in
@@ -3763,24 +3775,25 @@ and fmt_module c ctx ?rec_ ?ext ?epi ?(can_sparse = false) keyword
3763
3775
let bdy, epi = fmt_arg ~pro hd in
3764
3776
bdy $ fmt_args ~pro: epi tl
3765
3777
in
3766
- let intro =
3767
- str keyword
3768
- $ fmt_extension_suffix c ext
3769
- $ fmt_if rec_flag " rec" $ str " " $ fmt_str_loc_opt c name
3770
- in
3771
3778
let single_line =
3772
3779
Option. for_all xbody ~f: (fun x -> Mod. is_simple x.ast)
3773
3780
&& Option. for_all xmty ~f: (fun x -> Mty. is_simple x.ast)
3774
3781
&& List. for_all xargs ~f: (function {txt = Unit ; _} -> true | _ -> false )
3775
3782
in
3783
+ let doc_before, doc_after, attrs_before, attrs_after =
3784
+ fmt_docstring_around_item_attrs c ~force_before: (not single_line)
3785
+ ~fit: true attrs
3786
+ in
3787
+ let intro =
3788
+ str keyword
3789
+ $ fmt_extension_suffix c ext
3790
+ $ fmt_attributes c ~pre: (Break (1 , 0 )) attrs_before
3791
+ $ fmt_if rec_flag " rec" $ str " " $ fmt_str_loc_opt c name
3792
+ in
3776
3793
let compact =
3777
3794
Poly. (c.conf.fmt_opts.let_module.v = `Compact ) || not can_sparse
3778
3795
in
3779
3796
let fmt_pro = opt blk_b.pro (fun pro -> fmt " @ " $ pro) in
3780
- let doc_before, doc_after, atrs =
3781
- fmt_docstring_around_item c ~force_before: (not single_line) ~fit: true
3782
- attributes
3783
- in
3784
3797
hvbox
3785
3798
(if compact then 0 else 2 )
3786
3799
( doc_before
@@ -3799,7 +3812,7 @@ and fmt_module c ctx ?rec_ ?ext ?epi ?(can_sparse = false) keyword
3799
3812
$ fmt_if (Option. is_none blk_b.pro && Option. is_some xbody) " @ "
3800
3813
$ blk_b.bdy )
3801
3814
$ blk_b.esp $ fmt_opt blk_b.epi
3802
- $ fmt_item_attributes c ~pre: (Break (1 , 0 )) atrs
3815
+ $ fmt_item_attributes c ~pre: (Break (1 , 0 )) attrs_after
3803
3816
$ doc_after
3804
3817
$ opt epi (fun epi ->
3805
3818
fmt_or_k compact
@@ -3810,26 +3823,25 @@ and fmt_module c ctx ?rec_ ?ext ?epi ?(can_sparse = false) keyword
3810
3823
(fmt " @;<1 -2>" )
3811
3824
$ epi ) )
3812
3825
3813
- and fmt_module_declaration ? ext c ~rec_flag ~first {ast = pmd ; _} =
3826
+ and fmt_module_declaration c ~rec_flag ~first {ast = pmd ; _} =
3814
3827
protect c (Md pmd)
3815
3828
@@
3816
- let {pmd_name; pmd_args; pmd_type; pmd_attributes ; pmd_loc} = pmd in
3817
- update_config_maybe_disabled c pmd_loc pmd_attributes
3829
+ let {pmd_name; pmd_args; pmd_type; pmd_ext_attrs = attrs ; pmd_loc} = pmd in
3830
+ update_config_maybe_disabled_attrs c pmd_loc attrs
3818
3831
@@ fun c ->
3819
3832
let ctx = Md pmd in
3820
- let ext = if first then ext else None in
3821
3833
let keyword = if first then " module" else " and" in
3822
3834
let xmty = sub_mty ~ctx pmd_type in
3823
3835
let eqty =
3824
3836
match xmty.ast.pmty_desc with Pmty_alias _ -> None | _ -> Some " :"
3825
3837
in
3826
3838
Cmts. fmt c pmd_loc
3827
- (fmt_module ~rec_: rec_flag ?ext c ctx keyword pmd_name pmd_args None
3828
- ?eqty (Some xmty) ~rec_flag: (rec_flag && first) pmd_attributes )
3839
+ (fmt_module ~rec_: rec_flag c ctx keyword pmd_name pmd_args None ?eqty
3840
+ (Some xmty) ~rec_flag: (rec_flag && first) ~attrs )
3829
3841
3830
- and fmt_module_substitution ? ext c ctx pms =
3831
- let {pms_name; pms_manifest; pms_attributes ; pms_loc} = pms in
3832
- update_config_maybe_disabled c pms_loc pms_attributes
3842
+ and fmt_module_substitution c ctx pms =
3843
+ let {pms_name; pms_manifest; pms_ext_attrs = attrs ; pms_loc} = pms in
3844
+ update_config_maybe_disabled_attrs c pms_loc attrs
3833
3845
@@ fun c ->
3834
3846
let xmty =
3835
3847
(* TODO: improve *)
@@ -3840,17 +3852,17 @@ and fmt_module_substitution ?ext c ctx pms =
3840
3852
in
3841
3853
let pms_name = {pms_name with txt= Some pms_name.txt} in
3842
3854
Cmts. fmt c pms_loc
3843
- (fmt_module ?ext c ctx " module" ~eqty: " :=" pms_name [] None (Some xmty)
3844
- pms_attributes ~rec_flag: false )
3855
+ (fmt_module c ctx " module" ~eqty: " :=" pms_name [] None (Some xmty) ~attrs
3856
+ ~rec_flag: false )
3845
3857
3846
- and fmt_module_type_declaration ?ext ? eqty c ctx pmtd =
3847
- let {pmtd_name; pmtd_type; pmtd_attributes ; pmtd_loc} = pmtd in
3848
- update_config_maybe_disabled c pmtd_loc pmtd_attributes
3858
+ and fmt_module_type_declaration ?eqty c ctx pmtd =
3859
+ let {pmtd_name; pmtd_type; pmtd_ext_attrs = attrs ; pmtd_loc} = pmtd in
3860
+ update_config_maybe_disabled_attrs c pmtd_loc attrs
3849
3861
@@ fun c ->
3850
3862
let pmtd_name = {pmtd_name with txt= Some pmtd_name.txt} in
3851
- fmt_module ?ext ? eqty c ctx " module type" pmtd_name [] None ~rec_flag: false
3863
+ fmt_module ?eqty c ctx " module type" pmtd_name [] None ~rec_flag: false
3852
3864
(Option. map pmtd_type ~f: (sub_mty ~ctx ))
3853
- pmtd_attributes
3865
+ ~attrs
3854
3866
3855
3867
and fmt_open_description ?ext c ?(keyword = " open" ) ~kw_attributes
3856
3868
{popen_expr = popen_lid ; popen_override; popen_attributes; popen_loc} =
@@ -3909,13 +3921,15 @@ and fmt_with_constraint c ctx ~pre = function
3909
3921
let m1 = {m1 with txt= Some (str_longident m1.txt)} in
3910
3922
let m2 = Some (sub_mty ~ctx m2) in
3911
3923
str pre $ break 1 2
3912
- $ fmt_module c ctx " module type" m1 [] None ~rec_flag: false m2 []
3924
+ $ fmt_module c ctx " module type" m1 [] None ~rec_flag: false m2
3925
+ ~attrs: (Ast_helper.Attr. ext_attrs () )
3913
3926
| Pwith_modtypesubst (m1 , m2 ) ->
3914
3927
let m1 = {m1 with txt= Some (str_longident m1.txt)} in
3915
3928
let m2 = Some (sub_mty ~ctx m2) in
3916
3929
str pre $ break 1 2
3917
3930
$ fmt_module c ctx ~eqty: " :=" " module type" m1 [] None ~rec_flag: false
3918
- m2 []
3931
+ m2
3932
+ ~attrs: (Ast_helper.Attr. ext_attrs () )
3919
3933
3920
3934
and fmt_mod_apply c ctx loc attrs ~parens ~dock_struct me_f arg =
3921
3935
match me_f.pmod_desc with
@@ -4184,7 +4198,7 @@ and fmt_structure_item c ~last:last_item ?ext ~semisemi
4184
4198
let keyword = str " include" $ fmt_extension_suffix c ext $ fmt " @ " in
4185
4199
fmt_module_statement c ~attributes ~keyword (sub_mod ~ctx pincl_mod)
4186
4200
| Pstr_module mb ->
4187
- fmt_module_binding ?ext c ~rec_flag: false ~first: true (sub_mb ~ctx mb)
4201
+ fmt_module_binding c ~rec_flag: false ~first: true (sub_mb ~ctx mb)
4188
4202
| Pstr_open
4189
4203
{popen_expr; popen_override; popen_attributes= attributes; popen_loc}
4190
4204
->
@@ -4201,9 +4215,7 @@ and fmt_structure_item c ~last:last_item ?ext ~semisemi
4201
4215
fmt_module_statement c ~attributes ~keyword (sub_mod ~ctx popen_expr)
4202
4216
| Pstr_primitive vd -> fmt_value_description ?ext c ctx vd
4203
4217
| Pstr_recmodule mbs ->
4204
- fmt_recmodule c ctx mbs (fmt_module_binding ?ext)
4205
- (fun x -> Mb x)
4206
- sub_mb
4218
+ fmt_recmodule c ctx mbs fmt_module_binding (fun x -> Mb x) sub_mb
4207
4219
| Pstr_type (rec_flag , decls ) -> fmt_type c ?ext rec_flag decls ctx
4208
4220
| Pstr_typext te -> fmt_type_extension ?ext c ctx te
4209
4221
| Pstr_value {pvbs_rec= rec_flag; pvbs_bindings= bindings; pvbs_extension}
@@ -4228,7 +4240,7 @@ and fmt_structure_item c ~last:last_item ?ext ~semisemi
4228
4240
fmt_value_binding c ~rec_flag ?ext ?epi b
4229
4241
in
4230
4242
fmt_item_list c ctx update_config ast fmt_item bindings
4231
- | Pstr_modtype mtd -> fmt_module_type_declaration ?ext c ctx mtd
4243
+ | Pstr_modtype mtd -> fmt_module_type_declaration c ctx mtd
4232
4244
| Pstr_extension (ext , atrs ) ->
4233
4245
let doc_before, doc_after, atrs = fmt_docstring_around_item c atrs in
4234
4246
let box =
@@ -4376,12 +4388,12 @@ and fmt_value_binding c ~rec_flag ?ext ?in_ ?epi
4376
4388
$ epi )
4377
4389
$ fmt_docstring c ~pro: (fmt " @\n " ) doc2
4378
4390
4379
- and fmt_module_binding ?ext c ~rec_flag ~first {ast = pmb ; _} =
4391
+ and fmt_module_binding c ~rec_flag ~first {ast = pmb ; _} =
4392
+ let {pmb_name; pmb_ext_attrs= attrs; _} = pmb in
4380
4393
protect c (Mb pmb)
4381
- @@ update_config_maybe_disabled c pmb.pmb_loc pmb.pmb_attributes
4394
+ @@ update_config_maybe_disabled_attrs c pmb.pmb_loc attrs
4382
4395
@@ fun c ->
4383
4396
let ctx = Mb pmb in
4384
- let ext = if first then ext else None in
4385
4397
let keyword = if first then " module" else " and" in
4386
4398
let xbody = sub_mod ~ctx pmb.pmb_expr in
4387
4399
let xbody, xmty =
@@ -4395,9 +4407,8 @@ and fmt_module_binding ?ext c ~rec_flag ~first {ast= pmb; _} =
4395
4407
| _ -> (xbody, None )
4396
4408
in
4397
4409
Cmts. fmt c pmb.pmb_loc
4398
- (fmt_module ~rec_: rec_flag ?ext c ctx keyword
4399
- ~rec_flag: (rec_flag && first) ~eqty: " :" pmb.pmb_name pmb.pmb_args
4400
- (Some xbody) xmty pmb.pmb_attributes )
4410
+ (fmt_module ~rec_: rec_flag c ctx keyword ~rec_flag: (rec_flag && first)
4411
+ ~eqty: " :" pmb_name pmb.pmb_args (Some xbody) xmty ~attrs )
4401
4412
4402
4413
let fmt_toplevel_directive c ~semisemi dir =
4403
4414
let fmt_dir_arg = function
0 commit comments