Skip to content

Commit 9bf5d9c

Browse files
authored
Revert infix-form list formatting to pre-0.17.0 (#1717)
The formatting of infix-form lists looks like it used to pre- #1567
1 parent 808a68d commit 9bf5d9c

File tree

6 files changed

+95
-14
lines changed

6 files changed

+95
-14
lines changed

CHANGES.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@
5151
+ Depend on `odoc-parser` instead of `odoc` (#1683, #1713, @kit-ty-kate, @jonludlam, @julow)
5252
The parser from odoc has been split from the main odoc package and put into its own package, `odoc-parser`.
5353

54+
+ Revert infix-form list formatting to pre-0.17.0 (#1717, @gpetiot)
55+
5456
#### New features
5557

5658
+ Implement OCaml 4.13 features

lib/Fmt_ast.ml

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2004,9 +2004,8 @@ and fmt_expression c ?(box = true) ?pro ?epi ?eol ?parens ?(indent_wrap = 0)
20042004
| Pexp_construct (lid, None) ->
20052005
Params.parens_if parens c.conf (fmt_longident_loc c lid $ fmt_atrs)
20062006
| Pexp_construct
2007-
( {txt= Lident "::"; loc}
2008-
, Some {pexp_desc= Pexp_tuple [x; y]; pexp_attributes= []; pexp_loc; _}
2009-
) -> (
2007+
( {txt= Lident "::"; loc= _}
2008+
, Some {pexp_desc= Pexp_tuple [_; _]; pexp_attributes= []; _} ) -> (
20102009
match Sugar.list_exp c.cmts exp with
20112010
| Some (loc_xes, nil_loc) ->
20122011
let p = Params.get_list_expr c.conf in
@@ -2025,14 +2024,13 @@ and fmt_expression c ?(box = true) ?pro ?epi ?eol ?parens ?(indent_wrap = 0)
20252024
$ Cmts.fmt_after c ~pro:(fmt "@ ") ~epi:noop nil_loc )
20262025
$ fmt_atrs ) )
20272026
| None ->
2028-
Params.parens_if parens c.conf
2029-
@@ Cmts.fmt c pexp_loc
2027+
let loc_args = Sugar.infix_cons c.cmts xexp in
2028+
Cmts.fmt c pexp_loc
20302029
@@ hvbox indent_wrap
2031-
( fmt_expression c (sub_exp ~ctx x)
2032-
$ fmt "@ "
2033-
$ hovbox 0
2034-
( Cmts.fmt c ~pro:noop loc (fmt "::@ ")
2035-
$ fmt_expression c (sub_exp ~ctx y) )
2030+
( fmt_infix_op_args c ~parens xexp
2031+
(List.map loc_args ~f:(fun (loc, arg) ->
2032+
let fmt_op = opt loc (fmt_longident_loc c) in
2033+
(false, noop, noop, (fmt_op, [(Nolabel, arg)])) ) )
20362034
$ fmt_atrs ) )
20372035
| Pexp_construct (({txt= Lident "::"; loc= _} as lid), Some arg) ->
20382036
let opn, cls =

lib/Sugar.ml

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,36 @@ let list_exp cmts exp =
204204
in
205205
list_exp_ exp []
206206

207+
let infix_cons cmts xexp =
208+
let rec infix_cons_ ?cons_opt ({ast= exp; _} as xexp) acc =
209+
let ctx = Exp exp in
210+
let {pexp_desc; pexp_loc= l1; _} = exp in
211+
match pexp_desc with
212+
| Pexp_construct
213+
( ({txt= Lident "::"; _} as cons)
214+
, Some
215+
{ pexp_desc= Pexp_tuple [hd; tl]
216+
; pexp_loc= l3
217+
; pexp_attributes= []
218+
; _ } ) -> (
219+
( match acc with
220+
| [] -> ()
221+
| _ ->
222+
Cmts.relocate cmts ~src:l1 ~before:hd.pexp_loc ~after:tl.pexp_loc
223+
) ;
224+
Cmts.relocate cmts ~src:l3 ~before:hd.pexp_loc ~after:tl.pexp_loc ;
225+
match tl.pexp_attributes with
226+
| [] ->
227+
infix_cons_ ~cons_opt:cons (sub_exp ~ctx tl)
228+
((cons_opt, sub_exp ~ctx hd) :: acc)
229+
| _ ->
230+
(Some cons, sub_exp ~ctx tl)
231+
:: (cons_opt, sub_exp ~ctx hd)
232+
:: acc )
233+
| _ -> (cons_opt, xexp) :: acc
234+
in
235+
List.rev @@ infix_cons_ xexp []
236+
207237
let rec ite cmts ({ast= exp; _} as xexp) =
208238
let ctx = Exp exp in
209239
let {pexp_desc; pexp_loc; pexp_attributes; _} = exp in

lib/Sugar.mli

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,13 @@ val list_exp :
8181
(** [list_exp cmts exp] returns a list of expressions if [exp] is an
8282
expression corresponding to a list (empty list or (::) application). *)
8383

84+
val infix_cons :
85+
Cmts.t
86+
-> expression Ast.xt
87+
-> (Longident.t loc option * expression Ast.xt) list
88+
(** [infix_cons exp] returns a list of expressions if [exp] is an expression
89+
corresponding to a list ((::) application). *)
90+
8491
val ite :
8592
Cmts.t
8693
-> expression Ast.xt

test/passing/tests/list-space_around.ml.ref

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,30 @@ let x = function
6161
] ->
6262
()
6363

64-
let _ = f ~x:(a :: b) (* comment *) ~y
64+
let _ = f (* A *) ~x:(a :: b) (* B *) ~y
6565

66-
let _ = f ((* comment *) x :: y)
66+
let _ = f (* A *) ~x:((* B *) a :: b (* C *)) (* D *) ~y
67+
68+
let _ = f ~x:((* A *) a (* B *) :: (* C *) b (* D *) :: (* E *) c (* F *)) ~y
69+
70+
let _ = f ((* A *) x (* B *) :: (* C *) y (* D *) :: (* E *) z (* F *))
71+
72+
let _ = abc :: (* def :: *) ghi :: jkl
6773

6874
let _ = abc :: def (* :: ghi *) :: jkl
75+
76+
let _ = (c :: l1) @ foo (l2 @ l)
77+
78+
let _ =
79+
make_single_trace create_loc message
80+
:: make_single_trace create_loc create_message
81+
:: List.map call_chain ~f:(fun foooooooooooooooooooooooooooo ->
82+
fooooooooooooooooooooooooooooooo foooooooooooo [] )
83+
:: foooooooo :: fooooooooooooooooo
84+
85+
let _ =
86+
fooooooo
87+
( mk_var i (tfo_combine (nuc_p_o3'_60_tfo n) align) n
88+
:: mk_var i (tfo_combine (nuc_p_o3'_180_tfo n) align) n
89+
:: mk_var i (tfo_combine (nuc_p_o3'_275_tfo n) align) n
90+
:: domains )

test/passing/tests/list.ml

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,30 @@ let x = function
6161
] ->
6262
()
6363

64-
let _ = f ~x:(a :: b) (* comment *) ~y
64+
let _ = f (* A *) ~x:(a :: b) (* B *) ~y
6565

66-
let _ = f ((* comment *) x :: y)
66+
let _ = f (* A *) ~x:((* B *) a :: b (* C *)) (* D *) ~y
67+
68+
let _ = f ~x:((* A *) a (* B *) :: (* C *) b (* D *) :: (* E *) c (* F *)) ~y
69+
70+
let _ = f ((* A *) x (* B *) :: (* C *) y (* D *) :: (* E *) z (* F *))
71+
72+
let _ = abc :: (* def :: *) ghi :: jkl
6773

6874
let _ = abc :: def (* :: ghi *) :: jkl
75+
76+
let _ = (c :: l1) @ foo (l2 @ l)
77+
78+
let _ =
79+
make_single_trace create_loc message
80+
:: make_single_trace create_loc create_message
81+
:: List.map call_chain ~f:(fun foooooooooooooooooooooooooooo ->
82+
fooooooooooooooooooooooooooooooo foooooooooooo [] )
83+
:: foooooooo :: fooooooooooooooooo
84+
85+
let _ =
86+
fooooooo
87+
( mk_var i (tfo_combine (nuc_p_o3'_60_tfo n) align) n
88+
:: mk_var i (tfo_combine (nuc_p_o3'_180_tfo n) align) n
89+
:: mk_var i (tfo_combine (nuc_p_o3'_275_tfo n) align) n
90+
:: domains )

0 commit comments

Comments
 (0)