Skip to content
This repository was archived by the owner on Jun 15, 2023. It is now read-only.

Commit 266a343

Browse files
committed
More lazy.
1 parent 42ac73b commit 266a343

File tree

3 files changed

+114
-27
lines changed

3 files changed

+114
-27
lines changed

src/res_printer.ml

Lines changed: 30 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -4180,11 +4180,12 @@ and printArgumentsWithCallbackInFirstPosition ~uncurried ~customLayout args
41804180
printPexpFun ~customLayout ~inCallback:FitsOnOneLine expr cmtTbl;
41814181
]
41824182
in
4183-
let callback = printComments callback cmtTbl expr.pexp_loc in
4183+
let callback = lazy (printComments callback cmtTbl expr.pexp_loc) in
41844184
let printedArgs =
4185-
Doc.join
4186-
~sep:(Doc.concat [Doc.comma; Doc.line])
4187-
(List.map (fun arg -> printArgument ~customLayout arg cmtTbl) args)
4185+
lazy
4186+
(Doc.join
4187+
~sep:(Doc.concat [Doc.comma; Doc.line])
4188+
(List.map (fun arg -> printArgument ~customLayout arg cmtTbl) args))
41884189
in
41894190
(callback, printedArgs)
41904191
| _ -> assert false
@@ -4200,10 +4201,10 @@ and printArgumentsWithCallbackInFirstPosition ~uncurried ~customLayout args
42004201
(Doc.concat
42014202
[
42024203
(if uncurried then Doc.text "(. " else Doc.lparen);
4203-
callback;
4204+
Lazy.force callback;
42044205
Doc.comma;
42054206
Doc.line;
4206-
printedArgs;
4207+
Lazy.force printedArgs;
42074208
Doc.rparen;
42084209
])
42094210
in
@@ -4234,8 +4235,8 @@ and printArgumentsWithCallbackInFirstPosition ~uncurried ~customLayout args
42344235
* In this case, we always want the arguments broken over multiple lines,
42354236
* like a normal function call.
42364237
*)
4237-
if Doc.willBreak printedArgs then Lazy.force breakAllArgs
4238-
else if customLayout > customLayoutThreshold then Lazy.force fitsOnOneLine
4238+
if customLayout > customLayoutThreshold then Lazy.force breakAllArgs
4239+
else if Doc.willBreak (Lazy.force printedArgs) then Lazy.force breakAllArgs
42394240
else Doc.customLayout [Lazy.force fitsOnOneLine; Lazy.force breakAllArgs]
42404241

42414242
and printArgumentsWithCallbackInLastPosition ~customLayout ~uncurried args
@@ -4248,7 +4249,7 @@ and printArgumentsWithCallbackInLastPosition ~customLayout ~uncurried args
42484249
let cmtTblCopy2 = CommentTable.copy cmtTbl in
42494250
let rec loop acc args =
42504251
match args with
4251-
| [] -> (Doc.nil, Doc.nil, Doc.nil)
4252+
| [] -> (lazy Doc.nil, lazy Doc.nil, lazy Doc.nil)
42524253
| [(lbl, expr)] ->
42534254
let lblDoc =
42544255
match lbl with
@@ -4259,21 +4260,23 @@ and printArgumentsWithCallbackInLastPosition ~customLayout ~uncurried args
42594260
Doc.concat [Doc.tilde; printIdentLike txt; Doc.equal; Doc.question]
42604261
in
42614262
let callbackFitsOnOneLine =
4262-
let pexpFunDoc =
4263-
printPexpFun ~customLayout ~inCallback:FitsOnOneLine expr cmtTbl
4264-
in
4265-
let doc = Doc.concat [lblDoc; pexpFunDoc] in
4266-
printComments doc cmtTbl expr.pexp_loc
4263+
lazy
4264+
(let pexpFunDoc =
4265+
printPexpFun ~customLayout ~inCallback:FitsOnOneLine expr cmtTbl
4266+
in
4267+
let doc = Doc.concat [lblDoc; pexpFunDoc] in
4268+
printComments doc cmtTbl expr.pexp_loc)
42674269
in
42684270
let callbackArgumentsFitsOnOneLine =
4269-
let pexpFunDoc =
4270-
printPexpFun ~customLayout ~inCallback:ArgumentsFitOnOneLine expr
4271-
cmtTblCopy
4272-
in
4273-
let doc = Doc.concat [lblDoc; pexpFunDoc] in
4274-
printComments doc cmtTblCopy expr.pexp_loc
4271+
lazy
4272+
(let pexpFunDoc =
4273+
printPexpFun ~customLayout ~inCallback:ArgumentsFitOnOneLine expr
4274+
cmtTblCopy
4275+
in
4276+
let doc = Doc.concat [lblDoc; pexpFunDoc] in
4277+
printComments doc cmtTblCopy expr.pexp_loc)
42754278
in
4276-
( Doc.concat (List.rev acc),
4279+
( lazy (Doc.concat (List.rev acc)),
42774280
callbackFitsOnOneLine,
42784281
callbackArgumentsFitsOnOneLine )
42794282
| arg :: args ->
@@ -4288,8 +4291,8 @@ and printArgumentsWithCallbackInLastPosition ~customLayout ~uncurried args
42884291
(Doc.concat
42894292
[
42904293
(if uncurried then Doc.text "(." else Doc.lparen);
4291-
printedArgs;
4292-
callback;
4294+
Lazy.force printedArgs;
4295+
Lazy.force callback;
42934296
Doc.rparen;
42944297
])
42954298
in
@@ -4303,8 +4306,8 @@ and printArgumentsWithCallbackInLastPosition ~customLayout ~uncurried args
43034306
(Doc.concat
43044307
[
43054308
(if uncurried then Doc.text "(." else Doc.lparen);
4306-
printedArgs;
4307-
Doc.breakableGroup ~forceBreak:true callback2;
4309+
Lazy.force printedArgs;
4310+
Doc.breakableGroup ~forceBreak:true (Lazy.force callback2);
43084311
Doc.rparen;
43094312
])
43104313
in
@@ -4335,8 +4338,8 @@ and printArgumentsWithCallbackInLastPosition ~customLayout ~uncurried args
43354338
* In this case, we always want the arguments broken over multiple lines,
43364339
* like a normal function call.
43374340
*)
4338-
if Doc.willBreak printedArgs then Lazy.force breakAllArgs
4339-
else if customLayout > customLayoutThreshold then Lazy.force fitsOnOneLine
4341+
if customLayout > customLayoutThreshold then Lazy.force breakAllArgs
4342+
else if Doc.willBreak (Lazy.force printedArgs) then Lazy.force breakAllArgs
43404343
else
43414344
Doc.customLayout
43424345
[
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
let foo = () =>
2+
bar(x =>
3+
bar(x =>
4+
bar(
5+
x =>
6+
bar(
7+
x =>
8+
bar(
9+
x =>
10+
bar(
11+
x =>
12+
bar(
13+
x =>
14+
bar(
15+
x =>
16+
bar(
17+
x =>
18+
bar(
19+
x =>
20+
bar(
21+
x =>
22+
bar(
23+
x =>
24+
bar(
25+
x =>
26+
bar(
27+
x =>
28+
bar(
29+
x =>
30+
bar(
31+
x =>
32+
bar(
33+
x =>
34+
bar(
35+
x =>
36+
bar(
37+
x =>
38+
bar(
39+
x =>
40+
bar(x => bar(x => x)),
41+
),
42+
),
43+
),
44+
),
45+
),
46+
),
47+
),
48+
),
49+
),
50+
),
51+
),
52+
),
53+
),
54+
),
55+
),
56+
),
57+
),
58+
)
59+
)
60+
)
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
let foo = () =>
2+
bar(x =>
3+
bar(x =>
4+
bar(x =>
5+
bar(x =>
6+
bar(x =>
7+
bar(x =>
8+
bar(x =>
9+
bar(x =>
10+
bar(x =>
11+
bar(x =>
12+
bar(x =>
13+
bar(x =>
14+
bar(x =>
15+
bar(x =>
16+
bar(x =>
17+
bar(x =>
18+
bar(x =>
19+
bar(x =>
20+
bar(x =>
21+
bar(x =>
22+
bar(x =>
23+
bar(x => x)
24+
)))))))))))))))))))))

0 commit comments

Comments
 (0)