@@ -5,6 +5,17 @@ module Token = Res_token
5
5
module Parens = Res_parens
6
6
module ParsetreeViewer = Res_parsetree_viewer
7
7
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
+
8
19
let addParens doc =
9
20
Doc. group (
10
21
Doc. concat [
@@ -1901,6 +1912,7 @@ and printValueBinding ~recFlag vb cmtTbl i =
1901
1912
| Braced braces -> printBraces doc expr braces
1902
1913
| Nothing -> doc
1903
1914
in
1915
+ let patternDoc = printPattern vb.pvb_pat cmtTbl in
1904
1916
(*
1905
1917
* we want to optimize the layout of one pipe:
1906
1918
* let tbl = data->Js.Array2.reduce((map, curr) => {
@@ -1919,7 +1931,7 @@ and printValueBinding ~recFlag vb cmtTbl i =
1919
1931
Doc. concat [
1920
1932
attrs;
1921
1933
header;
1922
- printPattern vb.pvb_pat cmtTbl ;
1934
+ patternDoc ;
1923
1935
Doc. text " =" ;
1924
1936
Doc. space;
1925
1937
printedExpr;
@@ -1929,7 +1941,7 @@ and printValueBinding ~recFlag vb cmtTbl i =
1929
1941
Doc. concat [
1930
1942
attrs;
1931
1943
header;
1932
- printPattern vb.pvb_pat cmtTbl ;
1944
+ patternDoc ;
1933
1945
Doc. text " =" ;
1934
1946
Doc. indent (
1935
1947
Doc. concat [
@@ -1962,7 +1974,7 @@ and printValueBinding ~recFlag vb cmtTbl i =
1962
1974
Doc. concat [
1963
1975
attrs;
1964
1976
header;
1965
- printPattern vb.pvb_pat cmtTbl ;
1977
+ patternDoc ;
1966
1978
Doc. text " =" ;
1967
1979
if shouldIndent then
1968
1980
Doc. indent (
@@ -3019,7 +3031,7 @@ and printExpression (e : Parsetree.expression) cmtTbl =
3019
3031
in
3020
3032
let hasConstraint = match typConstraint with | Some _ -> true | None -> false in
3021
3033
let parametersDoc = printExprFunParameters
3022
- ~in Callback:false
3034
+ ~in Callback:NoCallback
3023
3035
~uncurried
3024
3036
~has Constraint
3025
3037
parameters
@@ -3224,7 +3236,9 @@ and printPexpFun ~inCallback e cmtTbl =
3224
3236
returnDoc;
3225
3237
]
3226
3238
);
3227
- if inCallback then Doc. softLine else Doc. nil;
3239
+ (match inCallback with
3240
+ | FitsOnOneLine | ArgumentsFitOnOneLine -> Doc. softLine
3241
+ | _ -> Doc. nil);
3228
3242
]
3229
3243
else
3230
3244
Doc. concat [
@@ -3240,15 +3254,13 @@ and printPexpFun ~inCallback e cmtTbl =
3240
3254
]
3241
3255
| _ -> Doc. nil
3242
3256
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
+ ]
3252
3264
3253
3265
and printTernaryOperand expr cmtTbl =
3254
3266
let doc = printExpressionWithComments expr cmtTbl in
@@ -4010,11 +4022,12 @@ and printArgumentsWithCallbackInFirstPosition ~uncurried args cmtTbl =
4010
4022
in
4011
4023
let callback = Doc. concat [
4012
4024
lblDoc;
4013
- printPexpFun ~in Callback:true expr cmtTbl
4025
+ printPexpFun ~in Callback:FitsOnOneLine expr cmtTbl
4014
4026
] 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
+ )
4018
4031
in
4019
4032
(callback, printedArgs)
4020
4033
| _ -> assert false
@@ -4051,8 +4064,9 @@ and printArgumentsWithCallbackInLastPosition ~uncurried args cmtTbl =
4051
4064
* consumed comments need to be marked not-consumed and reprinted…
4052
4065
* Cheng's different comment algorithm will solve this. *)
4053
4066
let cmtTblCopy = CommentTable. copy cmtTbl in
4067
+ let cmtTblCopy2 = CommentTable. copy cmtTbl in
4054
4068
let rec loop acc args = match args with
4055
- | [] -> (Doc. nil, Doc. nil)
4069
+ | [] -> (Doc. nil, Doc. nil, Doc. nil )
4056
4070
| [lbl, expr] ->
4057
4071
let lblDoc = match lbl with
4058
4072
| Asttypes. Nolabel -> Doc. nil
@@ -4065,13 +4079,20 @@ and printArgumentsWithCallbackInLastPosition ~uncurried args cmtTbl =
4065
4079
Doc. tilde; printIdentLike txt; Doc. equal; Doc. question;
4066
4080
]
4067
4081
in
4068
- let callback = printPexpFun ~in Callback:true expr cmtTbl in
4069
- (Doc. concat (List. rev acc), Doc. concat [lblDoc; callback])
4082
+ let callbackFitsOnOneLine =
4083
+ printPexpFun ~in Callback:FitsOnOneLine expr cmtTbl in
4084
+ let callbackArgumetnsFitsOnOneLine =
4085
+ printPexpFun ~in Callback:ArgumentsFitOnOneLine expr cmtTblCopy in
4086
+ (
4087
+ Doc. concat (List. rev acc),
4088
+ Doc. concat [lblDoc; callbackFitsOnOneLine],
4089
+ Doc. concat [lblDoc; callbackArgumetnsFitsOnOneLine]
4090
+ )
4070
4091
| arg ::args ->
4071
4092
let argDoc = printArgument arg cmtTbl in
4072
4093
loop (Doc. line::Doc. comma::argDoc::acc) args
4073
4094
in
4074
- let (printedArgs, callback) = loop [] args in
4095
+ let (printedArgs, callback, callback2 ) = loop [] args in
4075
4096
4076
4097
(* Thing.map(foo, (arg1, arg2) => MyModuleBlah.toList(argument)) *)
4077
4098
let fitsOnOneLine = Doc. concat [
@@ -4088,10 +4109,8 @@ and printArgumentsWithCallbackInLastPosition ~uncurried args cmtTbl =
4088
4109
let arugmentsFitOnOneLine =
4089
4110
Doc. concat [
4090
4111
if uncurried then Doc. text " (." else Doc. lparen;
4091
- Doc. softLine;
4092
4112
printedArgs;
4093
- Doc. breakableGroup ~force Break:true callback;
4094
- Doc. softLine;
4113
+ Doc. breakableGroup ~force Break:true callback2;
4095
4114
Doc. rparen;
4096
4115
]
4097
4116
in
@@ -4103,7 +4122,7 @@ and printArgumentsWithCallbackInLastPosition ~uncurried args cmtTbl =
4103
4122
* (param1, parm2) => doStuff(param1, parm2)
4104
4123
* )
4105
4124
*)
4106
- let breakAllArgs = printArguments ~uncurried args cmtTblCopy in
4125
+ let breakAllArgs = printArguments ~uncurried args cmtTblCopy2 in
4107
4126
Doc. customLayout [
4108
4127
fitsOnOneLine;
4109
4128
arugmentsFitOnOneLine;
@@ -4362,11 +4381,20 @@ and printExprFunParameters ~inCallback ~uncurried ~hasConstraint parameters cmtT
4362
4381
Doc. text " ()"
4363
4382
(* let f = (~greeting, ~from as hometown, ~x=?) => () *)
4364
4383
| parameters ->
4384
+ let inCallback = match inCallback with
4385
+ | FitsOnOneLine -> true
4386
+ | _ -> false
4387
+ in
4365
4388
let lparen = if uncurried then Doc. text " (. " else Doc. lparen in
4366
4389
let shouldHug = ParsetreeViewer. parametersShouldHug parameters in
4367
4390
let printedParamaters = Doc. concat [
4368
4391
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
+ )
4370
4398
(List. map (fun p -> printExpFunParameter p cmtTbl) parameters)
4371
4399
] in
4372
4400
Doc. group (
0 commit comments