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

Commit ecfa77f

Browse files
authored
462: Fix disappearing comments in nested Ppat_or (#508)
1 parent 032bf40 commit ecfa77f

File tree

3 files changed

+36
-17
lines changed

3 files changed

+36
-17
lines changed

src/res_comments_table.ml

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1682,22 +1682,13 @@ and walkExprArgument (_argLabel, expr) t comments =
16821682
recordRows
16831683
t
16841684
comments
1685-
| Ppat_or (pattern1, pattern2) ->
1686-
let (beforePattern1, insidePattern1, afterPattern1) =
1687-
partitionByLoc comments pattern1.ppat_loc
1688-
in
1689-
attach t.leading pattern1.ppat_loc beforePattern1;
1690-
walkPattern pattern1 t insidePattern1;
1691-
let (afterPattern1, rest) =
1692-
partitionAdjacentTrailing pattern1.ppat_loc afterPattern1
1693-
in
1694-
attach t.trailing pattern1.ppat_loc afterPattern1;
1695-
let (beforePattern2, insidePattern2, afterPattern2) =
1696-
partitionByLoc rest pattern2.ppat_loc
1697-
in
1698-
attach t.leading pattern2.ppat_loc beforePattern2;
1699-
walkPattern pattern2 t insidePattern2;
1700-
attach t.trailing pattern2.ppat_loc afterPattern2
1685+
| Ppat_or _->
1686+
walkList
1687+
~getLoc: (fun pattern -> pattern.Parsetree.ppat_loc)
1688+
~walkNode: (fun pattern -> walkPattern pattern)
1689+
(Res_parsetree_viewer.collectOrPatternChain pat)
1690+
t
1691+
comments
17011692
| Ppat_constraint (pattern, typ) ->
17021693
let (beforePattern, insidePattern, afterPattern) =
17031694
partitionByLoc comments pattern.ppat_loc

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

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,21 @@ let /* before */ {
5959

6060
// Ppat_or
6161
let /* b1 */ Blue /* b2 */ | /* c1 */ Red /* c2 */ = color
62-
let /* b1 */ Blue /* b2 */ | /* c1 */ Red | /* d1 */ Green /* d2 */ = color
62+
let /* b1 */ Blue /* b2 */ | /* c1 */ Red /* c2 */ | /* d1 */ Green /* d2 */ = color
63+
64+
// #462: Comments in nested Ppat_or must not disappear.
65+
// (comment positions not optimal yet because `|` has no AST location)
66+
let _ = switch "anything" {
67+
// above 1
68+
| "1"
69+
| // below 1
70+
"2"
71+
| // below 2
72+
"3"
73+
| // below 3
74+
_ => "4"
75+
// below 4
76+
}
6377

6478
// Ppat_constraint
6579
let /* c0 */ number /* c1 */: /* c2 */ int /* c3 */ = 123

tests/printer/comments/pattern.res

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,20 @@ let /* before */ {
5858
let /* b1 */ Blue /* b2 */ | /* c1 */ Red /* c2 */ = color
5959
let /* b1 */ Blue /* b2 */ | /* c1 */ Red /* c2 */ | /* d1 */ Green /* d2 */ = color
6060

61+
// #462: Comments in nested Ppat_or must not disappear.
62+
// (comment positions not optimal yet because `|` has no AST location)
63+
let _ = switch "anything" {
64+
// above 1
65+
| "1"
66+
// below 1
67+
| "2"
68+
// below 2
69+
| "3"
70+
// below 3
71+
| _ => "4"
72+
// below 4
73+
}
74+
6175
// Ppat_constraint
6276
let /* c0 */ number /* c1 */: /* c2 */ int /* c3 */ = 123
6377

0 commit comments

Comments
 (0)