Skip to content

Commit 22de444

Browse files
authored
JaneStreet profile: improve indentation of function body when max-indent is used (ocaml-ppx#2214)
1 parent eda1ede commit 22de444

File tree

15 files changed

+222
-67
lines changed

15 files changed

+222
-67
lines changed

CHANGES.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020
- Restore short form for first-class modules: `((module M) : (module S))` is formatted as `(module M : S)`) (#2280, #2300, @gpetiot, @Julow)
2121
- Restore short form formatting of record field aliases (#2282, @gpetiot)
22-
- Tweaks the JaneStreet profile to be more consistent with ocp-indent (#2281, #2284, #2289, #2299, #2302, #2309, #2310, #2311, #2313, @gpetiot, @Julow)
22+
- Tweaks the JaneStreet profile to be more consistent with ocp-indent (#2214, #2281, #2284, #2289, #2299, #2302, #2309, #2310, #2311, #2313, @gpetiot, @Julow)
2323
- Improve formatting of class signatures (#2301, @gpetiot, @Julow)
2424

2525
### New features

lib/Cmts.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ module Layout_cache = struct
2828

2929
let sexp_of_arg_label = function
3030
| Asttypes.Nolabel -> Sexp.Atom "Nolabel"
31-
| Labelled label -> List [Atom "Labelled"; sexp_of_string label]
32-
| Optional label -> List [Atom "Optional"; sexp_of_string label]
31+
| Labelled label -> List [Atom "Labelled"; sexp_of_string label.txt]
32+
| Optional label -> List [Atom "Optional"; sexp_of_string label.txt]
3333

3434
let sexp_of_t = function
3535
| Arg (label, expression) ->

lib/Fmt_ast.ml

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -338,10 +338,11 @@ let fmt_constant c ?epi {pconst_desc; pconst_loc= loc} =
338338
let fmt_variance_injectivity c vc = hvbox 0 (list vc "" (fmt_str_loc c))
339339

340340
let fmt_label lbl sep =
341+
(* No comment can be attached here. *)
341342
match lbl with
342343
| Nolabel -> noop
343-
| Labelled l -> str "~" $ str l $ fmt sep
344-
| Optional l -> str "?" $ str l $ fmt sep
344+
| Labelled l -> str "~" $ str l.txt $ fmt sep
345+
| Optional l -> str "?" $ str l.txt $ fmt sep
345346

346347
let fmt_direction_flag = function
347348
| Upto -> fmt "@ to "
@@ -721,8 +722,8 @@ and fmt_arrow_param c ctx {pap_label= lI; pap_loc= locI; pap_type= tI} =
721722
let arg_label lbl =
722723
match lbl with
723724
| Nolabel -> None
724-
| Labelled l -> Some (str l $ fmt ":@,")
725-
| Optional l -> Some (str "?" $ str l $ fmt ":@,")
725+
| Labelled l -> Some (str l.txt $ fmt ":@,")
726+
| Optional l -> Some (str "?" $ str l.txt $ fmt ":@,")
726727
in
727728
let xtI = sub_typ ~ctx tI in
728729
let arg =
@@ -1219,7 +1220,7 @@ and fmt_fun_args c args =
12191220
; _ }
12201221
; _ } as xpat )
12211222
, None )
1222-
when String.equal l txt ->
1223+
when String.equal l.txt txt ->
12231224
let symbol = match lbl with Labelled _ -> "~" | _ -> "?" in
12241225
cbox 0 (str symbol $ fmt_pattern ~box:true c xpat)
12251226
| Val ((Optional _ as lbl), xpat, None) ->
@@ -1244,7 +1245,7 @@ and fmt_fun_args c args =
12441245
, ( { ast= {ppat_desc= Ppat_var {txt; loc= _}; ppat_attributes= []; _}
12451246
; _ } as xpat )
12461247
, Some xexp )
1247-
when String.equal l txt ->
1248+
when String.equal l.txt txt ->
12481249
cbox 0
12491250
(wrap "?(" ")"
12501251
( fmt_pattern c ~box:true xpat
@@ -1260,7 +1261,7 @@ and fmt_fun_args c args =
12601261
; _ }
12611262
; _ } as xpat )
12621263
, Some xexp )
1263-
when String.equal l txt ->
1264+
when String.equal l.txt txt ->
12641265
cbox 0
12651266
(wrap "?(" ")"
12661267
( fmt_pattern c ~parens:false ~box:true xpat
@@ -1272,7 +1273,7 @@ and fmt_fun_args c args =
12721273
| _ -> Some false
12731274
in
12741275
cbox 2
1275-
( str "?" $ str l
1276+
( str "?" $ str l.txt
12761277
$ wrap_k (fmt ":@,(") (str ")")
12771278
( fmt_pattern c ?parens ~box:true xpat
12781279
$ fmt " =@;<1 2>" $ fmt_expression c xexp ) )
@@ -1386,11 +1387,11 @@ and fmt_fun ?force_closing_paren
13861387
and fmt_label_arg ?(box = true) ?epi ?eol c (lbl, ({ast= arg; _} as xarg)) =
13871388
match (lbl, arg.pexp_desc) with
13881389
| (Labelled l | Optional l), Pexp_ident {txt= Lident i; loc}
1389-
when String.equal l i && List.is_empty arg.pexp_attributes ->
1390+
when String.equal l.txt i && List.is_empty arg.pexp_attributes ->
13901391
Cmts.fmt c loc @@ Cmts.fmt c ?eol arg.pexp_loc @@ fmt_label lbl ""
13911392
| ( (Labelled l | Optional l)
13921393
, Pexp_constraint ({pexp_desc= Pexp_ident {txt= Lident i; _}; _}, _) )
1393-
when String.equal l i
1394+
when String.equal l.txt i
13941395
&& List.is_empty arg.pexp_attributes
13951396
&& Ocaml_version.(
13961397
compare c.conf.opr_opts.ocaml_version.v Releases.v4_14_0 >= 0 )
@@ -1897,15 +1898,25 @@ and fmt_expression c ?(box = true) ?pro ?epi ?eol ?parens ?(indent_wrap = 0)
18971898
let args_before = List.rev rev_args_before in
18981899
let xlast_arg = sub_exp ~ctx eN1 in
18991900
let args =
1901+
let begin_arg_loc =
1902+
match lbl with
1903+
| Nolabel -> eN1.pexp_loc
1904+
| Optional x | Labelled x -> x.loc
1905+
in
19001906
let break_body =
19011907
match eN1_body.pexp_desc with
19021908
| Pexp_function _ -> fmt "@ "
19031909
| _ -> (
19041910
(* Avoid the "double indentation" of the application and the
19051911
function matching when the [max-indent] option is set. *)
19061912
match c.conf.fmt_opts.max_indent.v with
1907-
| Some i when i <= 2 -> fmt "@ "
1908-
| _ -> fmt "@;<1 2>" )
1913+
| Some indent when indent <= 2 ->
1914+
if
1915+
Source.begins_line ~ignore_spaces:true c.source
1916+
begin_arg_loc
1917+
then break 1 indent
1918+
else fmt "@ "
1919+
| _ -> break 1 2 )
19091920
in
19101921
let wrap_intro x =
19111922
wrap

lib/Loc_tree.ml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ let of_ast fragment ast =
2525
locs := loc :: !locs ;
2626
loc
2727
in
28-
let mapper = Ast_mapper.{default_mapper with location; attribute} in
28+
(* Ignore locations of arg_labels *)
29+
let arg_label _ lbl = lbl in
30+
let mapper =
31+
Ast_mapper.{default_mapper with location; attribute; arg_label}
32+
in
2933
map fragment mapper ast |> ignore ;
3034
(of_list !locs, !locs)

test/passing/tests/js_source.ml

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7625,6 +7625,34 @@ let x =
76257625
^ "Yet another string _________")
76267626
;;
76277627

7628+
let x =
7629+
some_fun________________________________
7630+
some_arg______________________________ (fun param ->
7631+
do_something ();
7632+
do_something_else ();
7633+
return_this_value)
7634+
7635+
let x =
7636+
some_fun________________________________
7637+
some_arg______________________________ ~f:(fun param ->
7638+
do_something ();
7639+
do_something_else ();
7640+
return_this_value)
7641+
7642+
let x =
7643+
some_value
7644+
|> some_fun (fun x ->
7645+
do_something ();
7646+
do_something_else ();
7647+
return_this_value)
7648+
7649+
let x =
7650+
some_value
7651+
^ some_fun (fun x ->
7652+
do_something ();
7653+
do_something_else ();
7654+
return_this_value)
7655+
76287656
let bind t ~f =
76297657
unfold_step
76307658
~f:(function

test/passing/tests/js_source.ml.ocp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9815,6 +9815,40 @@ let x =
98159815
^ "Yet another string _________")
98169816
;;
98179817

9818+
let x =
9819+
some_fun________________________________
9820+
some_arg______________________________
9821+
(fun param ->
9822+
do_something ();
9823+
do_something_else ();
9824+
return_this_value)
9825+
;;
9826+
9827+
let x =
9828+
some_fun________________________________
9829+
some_arg______________________________
9830+
~f:(fun param ->
9831+
do_something ();
9832+
do_something_else ();
9833+
return_this_value)
9834+
;;
9835+
9836+
let x =
9837+
some_value
9838+
|> some_fun (fun x ->
9839+
do_something ();
9840+
do_something_else ();
9841+
return_this_value)
9842+
;;
9843+
9844+
let x =
9845+
some_value
9846+
^ some_fun (fun x ->
9847+
do_something ();
9848+
do_something_else ();
9849+
return_this_value)
9850+
;;
9851+
98189852
let bind t ~f =
98199853
unfold_step
98209854
~f:(function

test/passing/tests/js_source.ml.ref

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9815,6 +9815,40 @@ let x =
98159815
^ "Yet another string _________")
98169816
;;
98179817

9818+
let x =
9819+
some_fun________________________________
9820+
some_arg______________________________
9821+
(fun param ->
9822+
do_something ();
9823+
do_something_else ();
9824+
return_this_value)
9825+
;;
9826+
9827+
let x =
9828+
some_fun________________________________
9829+
some_arg______________________________
9830+
~f:(fun param ->
9831+
do_something ();
9832+
do_something_else ();
9833+
return_this_value)
9834+
;;
9835+
9836+
let x =
9837+
some_value
9838+
|> some_fun (fun x ->
9839+
do_something ();
9840+
do_something_else ();
9841+
return_this_value)
9842+
;;
9843+
9844+
let x =
9845+
some_value
9846+
^ some_fun (fun x ->
9847+
do_something ();
9848+
do_something_else ();
9849+
return_this_value)
9850+
;;
9851+
98189852
let bind t ~f =
98199853
unfold_step
98209854
~f:(function

test/passing/tests/max_indent.ml

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,11 @@ let () =
99
|> List.iter
1010
(fun
1111
some_really_really_really_long_name_that_doesn't_fit_on_the_line ->
12-
let x =
13-
some_really_really_really_long_name_that_doesn't_fit_on_the_line $ y
14-
in
15-
fooooooooooo x )
12+
let x =
13+
some_really_really_really_long_name_that_doesn't_fit_on_the_line
14+
$ y
15+
in
16+
fooooooooooo x )
1617

1718
let foooooooooo =
1819
foooooooooooooooooooooo
@@ -68,3 +69,23 @@ let foooooooooooooooooooooooooooooooo =
6869
| Tpat_variant (lab', Some arg, _) when lab = lab' -> (p, arg :: rem)
6970
| Tpat_any -> (p, omega :: rem)
7071
| _ -> raise NoMatch )
72+
73+
let x =
74+
some_fun________________________________
75+
some_arg______________________________ (fun param ->
76+
do_something () ; do_something_else () ; return_this_value )
77+
78+
let x =
79+
some_fun________________________________
80+
some_arg______________________________ ~f:(fun param ->
81+
do_something () ; do_something_else () ; return_this_value )
82+
83+
let x =
84+
some_value
85+
|> some_fun (fun x ->
86+
do_something () ; do_something_else () ; return_this_value )
87+
88+
let x =
89+
some_value
90+
^ some_fun (fun x ->
91+
do_something () ; do_something_else () ; return_this_value )

vendor/diff-parsers-ext-parsewyc.patch

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -699,7 +699,7 @@
699699
;
700700
labeled_simple_expr:
701701
simple_expr %prec below_HASH
702-
{ (Nolabel, $1) }
702+
{ Nolabel, $1 }
703703
| LABEL simple_expr %prec below_HASH
704704
@@@@
705705
;

vendor/parser-extended/ast_mapper.ml

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ open Location
2727
module String = Misc.Stdlib.String
2828

2929
type mapper = {
30+
arg_label: mapper -> Asttypes.arg_label -> Asttypes.arg_label;
3031
attribute: mapper -> attribute -> attribute;
3132
attributes: mapper -> attribute list -> attribute list;
3233
binding_op: mapper -> binding_op -> binding_op;
@@ -84,7 +85,7 @@ type mapper = {
8485
}
8586

8687
let map_fst f (x, y) = (f x, y)
87-
let map_snd f (x, y) = (x, f y)
88+
(*let map_snd f (x, y) = (x, f y)*)
8889
let map_tuple f1 f2 (x, y) = (f1 x, f2 y)
8990
let map_tuple3 f1 f2 f3 (x, y, z) = (f1 x, f2 y, f3 z)
9091
let map_opt f = function None -> None | Some x -> Some (f x)
@@ -97,6 +98,11 @@ let variant_var sub x =
9798
let map_package_type sub (lid, l) =
9899
(map_loc sub lid), (List.map (map_tuple (map_loc sub) (sub.typ sub)) l)
99100

101+
let map_arg_label sub = function
102+
| Asttypes.Nolabel -> Asttypes.Nolabel
103+
| Labelled x -> Labelled (map_loc sub x)
104+
| Optional x -> Optional (map_loc sub x)
105+
100106
module Flag = struct
101107
open Asttypes
102108

@@ -174,6 +180,7 @@ module T = struct
174180
Of.mk ~loc ~attrs desc
175181

176182
let map_arrow_param sub {pap_label; pap_loc; pap_type} =
183+
let pap_label = sub.arg_label sub pap_label in
177184
let pap_loc = sub.location sub pap_loc in
178185
let pap_type = sub.typ sub pap_type in
179186
{pap_label; pap_loc; pap_type}
@@ -467,11 +474,16 @@ module E = struct
467474
let_ ~loc ~attrs (sub.let_bindings sub lbs)
468475
(sub.expr sub e)
469476
| Pexp_fun (lab, def, p, e) ->
470-
fun_ ~loc ~attrs lab (map_opt (sub.expr sub) def) (sub.pat sub p)
477+
fun_ ~loc ~attrs
478+
(sub.arg_label sub lab)
479+
(map_opt (sub.expr sub) def)
480+
(sub.pat sub p)
471481
(sub.expr sub e)
472482
| Pexp_function pel -> function_ ~loc ~attrs (sub.cases sub pel)
473483
| Pexp_apply (e, l) ->
474-
apply ~loc ~attrs (sub.expr sub e) (List.map (map_snd (sub.expr sub)) l)
484+
apply ~loc ~attrs
485+
(sub.expr sub e)
486+
(List.map (map_tuple (sub.arg_label sub) (sub.expr sub)) l)
475487
| Pexp_match (e, pel) ->
476488
match_ ~loc ~attrs (sub.expr sub e) (sub.cases sub pel)
477489
| Pexp_try (e, pel) -> try_ ~loc ~attrs (sub.expr sub e) (sub.cases sub pel)
@@ -648,13 +660,14 @@ module CE = struct
648660
| Pcl_structure s ->
649661
structure ~loc ~attrs (sub.class_structure sub s)
650662
| Pcl_fun (lab, e, p, ce) ->
651-
fun_ ~loc ~attrs lab
663+
fun_ ~loc ~attrs
664+
(sub.arg_label sub lab)
652665
(map_opt (sub.expr sub) e)
653666
(sub.pat sub p)
654667
(sub.class_expr sub ce)
655668
| Pcl_apply (ce, l) ->
656669
apply ~loc ~attrs (sub.class_expr sub ce)
657-
(List.map (map_snd (sub.expr sub)) l)
670+
(List.map (map_tuple (sub.arg_label sub) (sub.expr sub)) l)
658671
| Pcl_let (lbs, ce) ->
659672
let_ ~loc ~attrs (sub.let_bindings sub lbs)
660673
(sub.class_expr sub ce)
@@ -711,6 +724,7 @@ end
711724

712725
let default_mapper =
713726
{
727+
arg_label = map_arg_label;
714728
constant = C.map;
715729
structure = (fun this l -> List.map (this.structure_item this) l);
716730
structure_item = M.map_structure_item;

0 commit comments

Comments
 (0)