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

Commit fe7b9e6

Browse files
IwanKaramazowIwan
and
Iwan
committed
Fix printing of trailing comments on constructor part of Ppat_construct. (#378)
Fixes #374 These comments should print correct: ```rescript switch a { | Constr /* after constr */ | Constr /* after constr */ (a, _) => a } ``` Co-authored-by: Iwan <mvalcke@hubspot.com>
1 parent faef443 commit fe7b9e6

File tree

4 files changed

+17
-3
lines changed

4 files changed

+17
-3
lines changed

src/res_comments_table.ml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1656,7 +1656,11 @@ and walkExprArgument (_argLabel, expr) t comments =
16561656
| Ppat_construct (constr, Some pat) ->
16571657
let (leading, trailing) = partitionLeadingTrailing comments constr.loc in
16581658
attach t.leading constr.loc leading;
1659-
let (leading, inside, trailing) = partitionByLoc trailing pat.ppat_loc in
1659+
let (afterConstructor, rest) =
1660+
partitionAdjacentTrailing constr.loc trailing
1661+
in
1662+
attach t.trailing constr.loc afterConstructor;
1663+
let (leading, inside, trailing) = partitionByLoc rest pat.ppat_loc in
16601664
attach t.leading pat.ppat_loc leading;
16611665
walkPattern pat t inside;
16621666
attach t.trailing pat.ppat_loc trailing

src/res_printer.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2138,7 +2138,7 @@ and printPattern (p : Parsetree.pattern) cmtTbl =
21382138
])
21392139
)
21402140
| Ppat_construct(constrName, constructorArgs) ->
2141-
let constrName = printLongident constrName.txt in
2141+
let constrName = printLongidentLocation constrName cmtTbl in
21422142
let argsDoc = match constructorArgs with
21432143
| None -> Doc.nil
21442144
| Some({ppat_loc; ppat_desc = Ppat_construct ({txt = Longident.Lident "()"}, _)}) ->

tests/printer/comments/expected/pattern.res.txt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,17 @@ let Black(// singleLineComment
2828

2929
let module(/* c0 */ X /* c1 */: /* c2 */ X_int /* c3 */) /* c4 */ = x
3030

31-
let /* before */ Rgb(
31+
let /* before */ Rgb /* after constr */(
3232
/* red */ r /* red2 */,
3333
/* green */ g /* green2 */,
3434
/* blue */ b /* blue2 */,
3535
) /* after */ = color
3636

37+
switch a {
38+
| Constr /* after constr */
39+
| Constr /* after constr */(a, _) => a
40+
}
41+
3742
let () = ()
3843
let [/* inside */] = []
3944
let list{/* inside */} = list{}

tests/printer/comments/pattern.res

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@ let module(/* c0 */ X /* c1 */ : /* c2 */ X_int /* c3 */) /* c4 */ = x
3131

3232
let /* before */ Rgb /* after constr */ (/* red */ r /* red2 */, /* green */ g/* green2 */, /* blue */ b /* blue2 */) /* after */ = color
3333

34+
switch a {
35+
| Constr /* after constr */
36+
| Constr /* after constr */ (a, _) => a
37+
}
38+
3439
let () = ()
3540
let [/* inside */] = []
3641
let list{/* inside */} = list{}

0 commit comments

Comments
 (0)