@@ -613,7 +613,7 @@ let rec fmt_extension_aux c ctx ~key (ext, pld) =
613613 | _, PPat (({ppat_loc; _} as pat), _), (Pld _ | Top )
614614 when Source. extension_using_sugar ~name: ext ~payload: ppat_loc ->
615615 fmt_pattern c ~ext (sub_pat ~ctx pat)
616- | _ ->
616+ | _ -> (
617617 let box =
618618 if c.conf.fmt_opts.ocp_indent_compat.v then
619619 match pld with
@@ -623,12 +623,40 @@ let rec fmt_extension_aux c ctx ~key (ext, pld) =
623623 hvbox c.conf.fmt_opts.stritem_extension_indent.v
624624 else Fn. id
625625 in
626- box
627- (wrap (str " [" ) (str " ]" )
628- ( str (Ext.Key. to_string key)
629- $ fmt_str_loc c ext
630- $ fmt_payload c (Pld pld) pld
631- $ fmt_if (Exposed.Right. payload pld) (str " " ) ) )
626+ let is_metaocaml_sugar =
627+ if
628+ String. is_prefix ~prefix: " metaocaml." ext.txt
629+ && Location. is_none ext.loc
630+ then
631+ match pld with
632+ | PStr [({pstr_desc= Pstr_eval (e, [] ); _} as pstr)] ->
633+ let node =
634+ match ext.txt with
635+ | "metaocaml.escape" -> `Escape
636+ | "metaocaml.bracket" -> `Bracket
637+ | _ -> assert false
638+ in
639+ Some (node, e, Str pstr)
640+ | _ -> assert false
641+ else None
642+ in
643+ match is_metaocaml_sugar with
644+ | Some (`Escape, e , ctx ) ->
645+ let parens =
646+ match e.pexp_desc with Pexp_ident _ -> false | _ -> true
647+ in
648+ box (str " .~" $ fmt_expression c ~parens (sub_exp ~ctx e))
649+ | Some (`Bracket, e , ctx ) ->
650+ box
651+ (wrap (str " .< " ) (str " >." )
652+ (fmt_expression c (sub_exp ~ctx e)) )
653+ | None ->
654+ box
655+ (wrap (str " [" ) (str " ]" )
656+ ( str (Ext.Key. to_string key)
657+ $ fmt_str_loc c ext
658+ $ fmt_payload c (Pld pld) pld
659+ $ fmt_if (Exposed.Right. payload pld) (str " " ) ) ) )
632660
633661and fmt_extension = fmt_extension_aux ~key: Ext.Key. Regular
634662
@@ -2974,7 +3002,8 @@ and fmt_class_signature c ~ctx ~pro ~epi ?ext self_ fields =
29743002 in
29753003 let ast x = Ctf x in
29763004 let cmts_within =
2977- if List. is_empty fields then (* Side effect order is important. *)
3005+ if List. is_empty fields then
3006+ (* Side effect order is important. *)
29783007 Cmts. fmt_within ~pro: noop c (Ast. location ctx)
29793008 else noop
29803009 in
0 commit comments