Skip to content

Commit f1ae7e7

Browse files
committed
update pretty printer to print line comments while forcing multiline
comments for lists that break `IfNeed`ed
1 parent 6775f1f commit f1ae7e7

File tree

1 file changed

+36
-8
lines changed

1 file changed

+36
-8
lines changed

src/reason_pprint_ast.ml

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1215,12 +1215,21 @@ let smallestLeadingSpaces strs =
12151215
in
12161216
smallestLeadingSpaces 99999 strs
12171217

1218-
let formatItemComment (str, commLoc) =
1218+
(*
1219+
forceMultiline should be set to true in cases where the comment
1220+
is part of a list that could potentially break to prevent inline
1221+
comments from being rendered as line comments
1222+
*)
1223+
let formatItemComment ?(forceMultiline=false) (str, commLoc) =
12191224
let commLines = Str.split_delim (Str.regexp "\n") ("/*" ^ str ^ "*/") in
12201225
match commLines with
12211226
| [] -> easyAtom ""
12221227
| [hd] ->
1223-
makeEasyList ~inline:(true, true) ~postSpace:true ~preSpace:true ~indent:0 ~break:IfNeed [easyAtom hd]
1228+
if forceMultiline then
1229+
makeEasyList ~inline:(true, true) ~postSpace:true ~preSpace:true ~indent:0 ~break:IfNeed [easyAtom hd]
1230+
(* caveat: single line comment . *)
1231+
else
1232+
makeEasyList ~inline:(true, true) ~postSpace:true ~preSpace:true ~indent:0 ~break:IfNeed [easyAtom ("//" ^ str)]
12241233
| zero::one::tl ->
12251234
let lineZero = List.nth commLines 0 in
12261235
let lineOne = List.nth commLines 1 in
@@ -1240,7 +1249,7 @@ let formatItemComment (str, commLoc) =
12401249
(* Use the Str module for regex splitting *)
12411250
makeEasyList ~inline:(true, true) ~indent:0 ~break:Always_rec (List.map easyAtom lines)
12421251

1243-
let formatComments comms = List.map formatItemComment comms
1252+
let formatComments ?(forceMultiline=false) comms = List.map (formatItemComment ~forceMultiline:forceMultiline) comms
12441253

12451254
let convertIsListyToIsSequencey isListyImpl =
12461255
let rec isSequencey layoutNode = match layoutNode with
@@ -1279,7 +1288,16 @@ let rec interleaveComments listConfig layoutListItems comments =
12791288
| _ -> ([], layoutToEasyFormatAndSurplus hd comments)
12801289
in
12811290
let (recItems, recUnconsumedComms) = interleaveComments listConfig tl unconsumedComms in
1282-
let easyComments = List.map (fun c -> Comment (formatItemComment c)) itemComments in
1291+
let easyComments = match listConfig.break with
1292+
(*
1293+
* If the comment is part of a list that isn't meant to break, or potentially might not break,
1294+
* we need to force it to be a multiline string due to interleaving
1295+
*)
1296+
| IfNeed | Never ->
1297+
List.map (fun c -> Comment (formatItemComment ~forceMultiline:true c)) itemComments
1298+
| _ ->
1299+
List.map (fun c -> Comment (formatItemComment ~forceMultiline:false c)) itemComments
1300+
in
12831301
(List.concat [easyComments; [Item easyItem]; recItems], recUnconsumedComms)
12841302

12851303
and attach listConfig ~useSep easyItem = match (listConfig.sep, listConfig.preSpace, listConfig.postSpace) with
@@ -1343,10 +1361,20 @@ and layoutToEasyFormatAndSurplus layoutNode comments = match layoutNode with
13431361
listConfig
13441362
[subLayout]
13451363
afterStart in
1346-
match (beforeStart, items, listConfig.wrap) with
1347-
| (_, [], _) -> raise (NotPossible "No items")
1348-
| ([], [singleItem], ("", "")) -> (singleItem, unconsumed)
1349-
| (leadingComms, items, _) -> (
1364+
match (beforeStart, items, listConfig.wrap, listConfig.break) with
1365+
| (_, [], _, _) -> raise (NotPossible "No items")
1366+
| ([], [singleItem], ("", ""), _) -> (singleItem, unconsumed)
1367+
(*
1368+
* If the comment is part of a list that isn't meant to break, or potentially might not break,
1369+
* we need to force it to be a multiline string due to interleaving
1370+
*)
1371+
| (leadingComms, items, _, IfNeed) | (leadingComms, items, _, Never)-> (
1372+
easyListWithConfig
1373+
listConfig
1374+
(List.concat [formatComments ~forceMultiline:true leadingComms; items;]),
1375+
unconsumed
1376+
)
1377+
| (leadingComms, items, _, _) -> (
13501378
easyListWithConfig
13511379
listConfig
13521380
(List.concat [formatComments leadingComms; items;]),

0 commit comments

Comments
 (0)