Skip to content

Commit 1ade941

Browse files
committed
Format parameter packs.
1 parent 9362431 commit 1ade941

File tree

2 files changed

+69
-0
lines changed

2 files changed

+69
-0
lines changed

Sources/SwiftFormat/PrettyPrint/TokenStreamCreator.swift

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2293,6 +2293,7 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor {
22932293

22942294
override func visit(_ node: GenericParameterSyntax) -> SyntaxVisitorContinueKind {
22952295
before(node.firstToken(viewMode: .sourceAccurate), tokens: .open)
2296+
after(node.eachKeyword, tokens: .break)
22962297
after(node.colon, tokens: .break)
22972298
if let trailingComma = node.trailingComma {
22982299
after(trailingComma, tokens: .close, .break(.same))
@@ -2312,6 +2313,28 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor {
23122313
return .visitChildren
23132314
}
23142315

2316+
override func visit(_ node: PackElementExprSyntax) -> SyntaxVisitorContinueKind {
2317+
// `each` cannot be separated from the following token, or it is parsed as an identifier itself.
2318+
after(node.eachKeyword, tokens: .space)
2319+
return .visitChildren
2320+
}
2321+
2322+
override func visit(_ node: PackElementTypeSyntax) -> SyntaxVisitorContinueKind {
2323+
// `each` cannot be separated from the following token, or it is parsed as an identifier itself.
2324+
after(node.eachKeyword, tokens: .space)
2325+
return .visitChildren
2326+
}
2327+
2328+
override func visit(_ node: PackExpansionExprSyntax) -> SyntaxVisitorContinueKind {
2329+
after(node.repeatKeyword, tokens: .break)
2330+
return .visitChildren
2331+
}
2332+
2333+
override func visit(_ node: PackExpansionTypeSyntax) -> SyntaxVisitorContinueKind {
2334+
after(node.repeatKeyword, tokens: .break)
2335+
return .visitChildren
2336+
}
2337+
23152338
override func visit(_ node: ExpressionPatternSyntax) -> SyntaxVisitorContinueKind {
23162339
return .visitChildren
23172340
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
final class ParameterPackTests: PrettyPrintTestCase {
2+
func testGenericPackArgument() {
3+
assertPrettyPrintEqual(
4+
input: """
5+
func someFunction<each P>() {}
6+
struct SomeStruct<each P> {}
7+
""",
8+
expected: """
9+
func someFunction<
10+
each P
11+
>() {}
12+
struct SomeStruct<
13+
each P
14+
> {}
15+
16+
""",
17+
linelength: 22)
18+
}
19+
20+
func testPackExpansionsAndElements() {
21+
assertPrettyPrintEqual(
22+
input: """
23+
repeat checkNilness(of: each value)
24+
""",
25+
expected: """
26+
repeat checkNilness(
27+
of: each value)
28+
29+
""",
30+
linelength: 25)
31+
32+
assertPrettyPrintEqual(
33+
input: """
34+
repeat f(of: each v)
35+
""",
36+
expected: """
37+
repeat
38+
f(
39+
of:
40+
each v
41+
)
42+
43+
""",
44+
linelength: 7)
45+
}
46+
}

0 commit comments

Comments
 (0)