Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changes/list-pattern-spread-comment.fix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- Keep the comments around the spread of a list pattern without other items when formatting, as in `list{/* c */ ...r}`.
5 changes: 5 additions & 0 deletions compiler/syntax/src/res_core.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1659,6 +1659,11 @@ and parse_list_pattern ~start_pos ~attrs p =
else pattern
in
match list_patterns with
| [(true, pattern)] ->
(* [list{...p}] is [p] itself. Like [list{...e}] in expressions, it keeps
the location of [p], so that the comments around the spread stay
outside it and are printed with [p]. *)
{pattern with ppat_attributes = attrs}
| (true, pattern) :: patterns ->
let patterns = patterns |> List.map filter_spread |> List.rev in
let pat = make_list_pattern loc patterns (Some pattern) in
Expand Down
10 changes: 10 additions & 0 deletions tests/syntax_tests/data/printer/comments/expected/pattern.res.txt
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,16 @@ let list{/* inside */} = list{}
let /* before */ list{/* a1 */ a /* a2 */, /* b1 */ b /* b2 */} /* after */ = list{1, 2}
let /* before */ list{/* a1 */ a /* a2 */, /* b1 */ ...b /* b2 */} /* after */ = list{1, 2}

// A list pattern that is only a spread is the spread's operand
let /* d */ r = list{1, 2}
let r /* after */ = list{1, 2}
let /* d1 */ /* d2 */ /* e */ r = list{1, 2}
let f = /* d */ r => r
let f = x =>
switch x {
/* d */ | r => r
}

// Ppat_record
let /* before */ {/* c0 */ name /* c1 */, /* c2 */ age /* c3 */} /* after */ = {
name: "steve",
Expand Down
10 changes: 10 additions & 0 deletions tests/syntax_tests/data/printer/comments/pattern.res
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,16 @@ let list{/* inside */} = list{}
let /* before */ list{/* a1 */ a /* a2 */, /* b1 */ b /* b2 */} /* after */ = list{1, 2}
let /* before */ list{/* a1 */ a /* a2 */, /* b1 */ ...b /* b2 */} /* after */ = list{1, 2}

// A list pattern that is only a spread is the spread's operand
let list{/* d */ ...r} = list{1, 2}
let list{...r /* after */} = list{1, 2}
let list{/* d1 */ /* d2 */ ...list{/* e */ ...r}} = list{1, 2}
let f = (list{/* d */ ...r}) => r
let f = x =>
switch x {
| list{/* d */ ...r} => r
}

// Ppat_record
let /* before */ {/* c0 */ name /* c1 */, /* c2 */ age /* c3 */} /* after */ = {
name: "steve",
Expand Down
Loading