Skip to content

Commit 1a55142

Browse files
Tweak printing of callbacks to break correctly in case of annotated labeled arguments (rescript-lang#199)
* Tweak printing of callbacks to break correctly in case of annotated labeled arguments * Fix printing of comments in pattern of let-binding and pipe-first.
1 parent fb0a437 commit 1a55142

File tree

5 files changed

+187
-37
lines changed

5 files changed

+187
-37
lines changed

syntax/src/res_doc.ml

+3-2
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ let debug t =
234234
let rec toDoc = function
235235
| Nil -> text "nil"
236236
| BreakParent -> text "breakparent"
237-
| Text txt -> text ("text(" ^ txt ^ ")")
237+
| Text txt -> text ("text(\"" ^ txt ^ "\")")
238238
| LineSuffix doc -> group(
239239
concat [
240240
text "linesuffix(";
@@ -245,6 +245,7 @@ let debug t =
245245
text ")"
246246
]
247247
)
248+
| Concat [] -> text "concat()"
248249
| Concat docs -> group(
249250
concat [
250251
text "concat(";
@@ -312,7 +313,7 @@ let debug t =
312313
indent (
313314
concat [
314315
line;
315-
text ("shouldBreak: " ^ (string_of_bool shouldBreak));
316+
text ("{shouldBreak: " ^ (string_of_bool shouldBreak) ^ "}");
316317
concat [text ","; line];
317318
toDoc doc;
318319
]

syntax/src/res_printer.ml

+55-27
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,17 @@ module Token = Res_token
55
module Parens = Res_parens
66
module ParsetreeViewer = Res_parsetree_viewer
77

8+
type callbackStyle =
9+
(* regular arrow function, example: `let f = x => x + 1` *)
10+
| NoCallback
11+
(* `Thing.map(foo, (arg1, arg2) => MyModuleBlah.toList(argument))` *)
12+
| FitsOnOneLine
13+
(* Thing.map(longArgumet, veryLooooongArgument, (arg1, arg2) =>
14+
* MyModuleBlah.toList(argument)
15+
* )
16+
*)
17+
| ArgumentsFitOnOneLine
18+
819
let addParens doc =
920
Doc.group (
1021
Doc.concat [
@@ -1901,6 +1912,7 @@ and printValueBinding ~recFlag vb cmtTbl i =
19011912
| Braced braces -> printBraces doc expr braces
19021913
| Nothing -> doc
19031914
in
1915+
let patternDoc = printPattern vb.pvb_pat cmtTbl in
19041916
(*
19051917
* we want to optimize the layout of one pipe:
19061918
* let tbl = data->Js.Array2.reduce((map, curr) => {
@@ -1919,7 +1931,7 @@ and printValueBinding ~recFlag vb cmtTbl i =
19191931
Doc.concat [
19201932
attrs;
19211933
header;
1922-
printPattern vb.pvb_pat cmtTbl;
1934+
patternDoc;
19231935
Doc.text " =";
19241936
Doc.space;
19251937
printedExpr;
@@ -1929,7 +1941,7 @@ and printValueBinding ~recFlag vb cmtTbl i =
19291941
Doc.concat [
19301942
attrs;
19311943
header;
1932-
printPattern vb.pvb_pat cmtTbl;
1944+
patternDoc;
19331945
Doc.text " =";
19341946
Doc.indent (
19351947
Doc.concat [
@@ -1962,7 +1974,7 @@ and printValueBinding ~recFlag vb cmtTbl i =
19621974
Doc.concat [
19631975
attrs;
19641976
header;
1965-
printPattern vb.pvb_pat cmtTbl;
1977+
patternDoc;
19661978
Doc.text " =";
19671979
if shouldIndent then
19681980
Doc.indent (
@@ -3019,7 +3031,7 @@ and printExpression (e : Parsetree.expression) cmtTbl =
30193031
in
30203032
let hasConstraint = match typConstraint with | Some _ -> true | None -> false in
30213033
let parametersDoc = printExprFunParameters
3022-
~inCallback:false
3034+
~inCallback:NoCallback
30233035
~uncurried
30243036
~hasConstraint
30253037
parameters
@@ -3224,7 +3236,9 @@ and printPexpFun ~inCallback e cmtTbl =
32243236
returnDoc;
32253237
]
32263238
);
3227-
if inCallback then Doc.softLine else Doc.nil;
3239+
(match inCallback with
3240+
| FitsOnOneLine | ArgumentsFitOnOneLine -> Doc.softLine
3241+
| _ -> Doc.nil);
32283242
]
32293243
else
32303244
Doc.concat [
@@ -3240,15 +3254,13 @@ and printPexpFun ~inCallback e cmtTbl =
32403254
]
32413255
| _ -> Doc.nil
32423256
in
3243-
Doc.group (
3244-
Doc.concat [
3245-
printAttributes attrs cmtTbl;
3246-
parametersDoc;
3247-
typConstraintDoc;
3248-
Doc.text " =>";
3249-
returnExprDoc;
3250-
]
3251-
)
3257+
Doc.concat [
3258+
printAttributes attrs cmtTbl;
3259+
parametersDoc;
3260+
typConstraintDoc;
3261+
Doc.text " =>";
3262+
returnExprDoc;
3263+
]
32523264

32533265
and printTernaryOperand expr cmtTbl =
32543266
let doc = printExpressionWithComments expr cmtTbl in
@@ -4010,11 +4022,12 @@ and printArgumentsWithCallbackInFirstPosition ~uncurried args cmtTbl =
40104022
in
40114023
let callback = Doc.concat [
40124024
lblDoc;
4013-
printPexpFun ~inCallback:true expr cmtTbl
4025+
printPexpFun ~inCallback:FitsOnOneLine expr cmtTbl
40144026
] in
4015-
let printedArgs = List.map (fun arg ->
4016-
printArgument arg cmtTbl
4017-
) args |> Doc.join ~sep:(Doc.concat [Doc.comma; Doc.line])
4027+
let printedArgs =
4028+
Doc.join ~sep:(Doc.concat [Doc.comma; Doc.line]) (
4029+
List.map (fun arg -> printArgument arg cmtTbl) args
4030+
)
40184031
in
40194032
(callback, printedArgs)
40204033
| _ -> assert false
@@ -4051,8 +4064,9 @@ and printArgumentsWithCallbackInLastPosition ~uncurried args cmtTbl =
40514064
* consumed comments need to be marked not-consumed and reprinted…
40524065
* Cheng's different comment algorithm will solve this. *)
40534066
let cmtTblCopy = CommentTable.copy cmtTbl in
4067+
let cmtTblCopy2 = CommentTable.copy cmtTbl in
40544068
let rec loop acc args = match args with
4055-
| [] -> (Doc.nil, Doc.nil)
4069+
| [] -> (Doc.nil, Doc.nil, Doc.nil)
40564070
| [lbl, expr] ->
40574071
let lblDoc = match lbl with
40584072
| Asttypes.Nolabel -> Doc.nil
@@ -4065,13 +4079,20 @@ and printArgumentsWithCallbackInLastPosition ~uncurried args cmtTbl =
40654079
Doc.tilde; printIdentLike txt; Doc.equal; Doc.question;
40664080
]
40674081
in
4068-
let callback = printPexpFun ~inCallback:true expr cmtTbl in
4069-
(Doc.concat (List.rev acc), Doc.concat [lblDoc; callback])
4082+
let callbackFitsOnOneLine =
4083+
printPexpFun ~inCallback:FitsOnOneLine expr cmtTbl in
4084+
let callbackArgumetnsFitsOnOneLine =
4085+
printPexpFun ~inCallback:ArgumentsFitOnOneLine expr cmtTblCopy in
4086+
(
4087+
Doc.concat (List.rev acc),
4088+
Doc.concat [lblDoc; callbackFitsOnOneLine],
4089+
Doc.concat [lblDoc; callbackArgumetnsFitsOnOneLine]
4090+
)
40704091
| arg::args ->
40714092
let argDoc = printArgument arg cmtTbl in
40724093
loop (Doc.line::Doc.comma::argDoc::acc) args
40734094
in
4074-
let (printedArgs, callback) = loop [] args in
4095+
let (printedArgs, callback, callback2) = loop [] args in
40754096

40764097
(* Thing.map(foo, (arg1, arg2) => MyModuleBlah.toList(argument)) *)
40774098
let fitsOnOneLine = Doc.concat [
@@ -4088,10 +4109,8 @@ and printArgumentsWithCallbackInLastPosition ~uncurried args cmtTbl =
40884109
let arugmentsFitOnOneLine =
40894110
Doc.concat [
40904111
if uncurried then Doc.text "(." else Doc.lparen;
4091-
Doc.softLine;
40924112
printedArgs;
4093-
Doc.breakableGroup ~forceBreak:true callback;
4094-
Doc.softLine;
4113+
Doc.breakableGroup ~forceBreak:true callback2;
40954114
Doc.rparen;
40964115
]
40974116
in
@@ -4103,7 +4122,7 @@ and printArgumentsWithCallbackInLastPosition ~uncurried args cmtTbl =
41034122
* (param1, parm2) => doStuff(param1, parm2)
41044123
* )
41054124
*)
4106-
let breakAllArgs = printArguments ~uncurried args cmtTblCopy in
4125+
let breakAllArgs = printArguments ~uncurried args cmtTblCopy2 in
41074126
Doc.customLayout [
41084127
fitsOnOneLine;
41094128
arugmentsFitOnOneLine;
@@ -4362,11 +4381,20 @@ and printExprFunParameters ~inCallback ~uncurried ~hasConstraint parameters cmtT
43624381
Doc.text "()"
43634382
(* let f = (~greeting, ~from as hometown, ~x=?) => () *)
43644383
| parameters ->
4384+
let inCallback = match inCallback with
4385+
| FitsOnOneLine -> true
4386+
| _ -> false
4387+
in
43654388
let lparen = if uncurried then Doc.text "(. " else Doc.lparen in
43664389
let shouldHug = ParsetreeViewer.parametersShouldHug parameters in
43674390
let printedParamaters = Doc.concat [
43684391
if shouldHug || inCallback then Doc.nil else Doc.softLine;
4369-
Doc.join ~sep:(Doc.concat [Doc.comma; if inCallback then Doc.space else Doc.line])
4392+
Doc.join
4393+
~sep:(
4394+
Doc.concat [
4395+
Doc.comma; if inCallback then Doc.line else Doc.line
4396+
]
4397+
)
43704398
(List.map (fun p -> printExpFunParameter p cmtTbl) parameters)
43714399
] in
43724400
Doc.group (

syntax/tests/printer/expr/__snapshots__/render.spec.js.snap

+55
Original file line numberDiff line numberDiff line change
@@ -1429,6 +1429,61 @@ let makeEntryJobsForJournal = (journalId: string, userId: string) => {
14291429
14301430
pom
14311431
}
1432+
1433+
let make = fn((
1434+
~a: option<string>=?,
1435+
~b: option<string>=?,
1436+
~c: option<string>=?,
1437+
~d: option<string>=?,
1438+
~e: option<string>=?,
1439+
x,
1440+
) => {
1441+
Js.log()
1442+
})
1443+
1444+
let make = fn((
1445+
~a: option<string>,
1446+
~b: option<string>,
1447+
~c: option<string>,
1448+
~d: option<string>,
1449+
~e: option<string>,
1450+
x,
1451+
) => {
1452+
Js.log()
1453+
})
1454+
1455+
let make = fn((
1456+
~a: optionstring,
1457+
~b: optionstring,
1458+
~c: optionstring,
1459+
~d: optionstring,
1460+
~e: optionstring,
1461+
x,
1462+
) => {
1463+
Js.log()
1464+
})
1465+
1466+
let make = fn((
1467+
aoptionstring,
1468+
boptionstring,
1469+
coptionstring,
1470+
doptionstring,
1471+
eoptionstring,
1472+
foptionstring,
1473+
x,
1474+
) => {
1475+
Js.log()
1476+
})
1477+
1478+
// comments should not disappear on the pattern
1479+
let /* a */ decoratorTags /* b */ =
1480+
items->Js.Array2.filter(items => {items.category === Decorators})
1481+
1482+
let /* a */ decoratorTags /* b */ = items->Js.Array2.filter(items => {
1483+
items.category === Decorators ||
1484+
items.category === ChristmasLighting ||
1485+
items.category === Unknown
1486+
})
14321487
"
14331488
`;
14341489

syntax/tests/printer/expr/callback.js

+63
Original file line numberDiff line numberDiff line change
@@ -256,3 +256,66 @@ let makeEntryJobsForJournal = (journalId: string, userId: string) => {
256256

257257
pom
258258
}
259+
260+
let make = fn(
261+
(
262+
~a: option<string>=?,
263+
~b: option<string>=?,
264+
~c: option<string>=?,
265+
~d: option<string>=?,
266+
~e: option<string>=?,
267+
x
268+
) => {
269+
Js.log()
270+
}
271+
)
272+
273+
let make = fn(
274+
(
275+
~a: option<string>,
276+
~b: option<string>,
277+
~c: option<string>,
278+
~d: option<string>,
279+
~e: option<string>,
280+
x
281+
) => {
282+
Js.log()
283+
}
284+
)
285+
286+
let make = fn(
287+
(
288+
~a: optionstring,
289+
~b: optionstring,
290+
~c: optionstring,
291+
~d: optionstring,
292+
~e: optionstring,
293+
x
294+
) => {
295+
Js.log()
296+
}
297+
)
298+
299+
let make = fn(
300+
(
301+
aoptionstring,
302+
boptionstring,
303+
coptionstring,
304+
doptionstring,
305+
eoptionstring,
306+
foptionstring,
307+
x
308+
) => {
309+
Js.log()
310+
}
311+
)
312+
313+
// comments should not disappear on the pattern
314+
let /* a */ decoratorTags /* b */ = items
315+
->Js.Array2.filter(items => {items.category === Decorators})
316+
317+
let /* a */ decoratorTags /* b */ = items->Js.Array2.filter(items => {
318+
items.category === Decorators
319+
|| items.category === ChristmasLighting
320+
|| items.category === Unknown
321+
})

syntax/tests/printer/other/__snapshots__/render.spec.js.snap

+11-8
Original file line numberDiff line numberDiff line change
@@ -393,16 +393,19 @@ exports[`moduleData.ml 1`] = `
393393
`;
394394
395395
exports[`nesting.res 1`] = `
396-
"let unitsCommands = state.units->Js.Array2.mapi(({
397-
unit: targetUnit,
398-
coordinates: targetCoordinates,
399-
}, i) => {
396+
"let unitsCommands = state.units->Js.Array2.mapi((
397+
{unit: targetUnit, coordinates: targetCoordinates},
398+
i,
399+
) => {
400400
// n^2
401401
let res = []
402-
state.units->Js.Array2.forEachi(({
403-
unit: unitThatMightBeAttacking,
404-
coordinates: unitThatMightBeAttackingCoordinates,
405-
}, j) => {
402+
state.units->Js.Array2.forEachi((
403+
{
404+
unit: unitThatMightBeAttacking,
405+
coordinates: unitThatMightBeAttackingCoordinates,
406+
},
407+
j,
408+
) => {
406409
if i !== j {
407410
switch Js.Array2.unsafe_get(
408411
unitThatMightBeAttacking.timeline,

0 commit comments

Comments
 (0)