Skip to content

Commit a3c2012

Browse files
committed
Support designated types for operator declarations.
Also, add a space between the operator and the colon; it should have always been there, for consistency with `func #OP# (...)`. Fixes #311.
1 parent dc5564e commit a3c2012

File tree

2 files changed

+41
-6
lines changed

2 files changed

+41
-6
lines changed

Sources/SwiftFormatPrettyPrint/TokenStreamCreator.swift

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1375,10 +1375,14 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor {
13751375
}
13761376

13771377
override func visit(_ node: OperatorPrecedenceAndTypesSyntax) -> SyntaxVisitorContinueKind {
1378-
// Despite being an `IdentifierListSyntax`, the language grammar currently only allows a single
1379-
// precedence group here, so we don't worry about breaks at any interior commas.
1380-
after(node.colon, tokens: .break(.open))
1381-
after(node.lastToken, tokens: .break(.close, size: 0))
1378+
before(node.colon, tokens: .space)
1379+
after(node.colon, tokens: .break(.open), .open)
1380+
after(node.designatedTypes.lastToken ?? node.lastToken, tokens: .break(.close, size: 0), .close)
1381+
return .visitChildren
1382+
}
1383+
1384+
override func visit(_ node: DesignatedTypeElementSyntax) -> SyntaxVisitorContinueKind {
1385+
after(node.leadingComma, tokens: .break(.same))
13821386
return .visitChildren
13831387
}
13841388

Tests/SwiftFormatPrettyPrintTests/OperatorDeclTests.swift

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ final class OperatorDeclTests: PrettyPrintTestCase {
1515
prefix operator ^*^
1616
postfix operator !**!
1717
infix operator *%*
18-
infix operator *%*:
18+
infix operator *%* :
1919
PrecedenceGroup
2020
2121
"""
@@ -35,7 +35,7 @@ final class OperatorDeclTests: PrettyPrintTestCase {
3535
*%*
3636
infix
3737
operator
38-
*%*:
38+
*%* :
3939
PrecedenceGroup
4040
4141
"""
@@ -83,4 +83,35 @@ final class OperatorDeclTests: PrettyPrintTestCase {
8383

8484
assertPrettyPrintEqual(input: input, expected: expectedShorter, linelength: 10)
8585
}
86+
87+
func testDesignatedTypes() {
88+
let input =
89+
"""
90+
infix operator *%*: PrecedenceGroup, Bool, Int, String
91+
"""
92+
93+
let expected =
94+
"""
95+
infix operator *%* :
96+
PrecedenceGroup, Bool,
97+
Int, String
98+
99+
"""
100+
101+
assertPrettyPrintEqual(input: input, expected: expected, linelength: 25)
102+
103+
let expectedShorter =
104+
"""
105+
infix
106+
operator
107+
*%* :
108+
PrecedenceGroup,
109+
Bool,
110+
Int,
111+
String
112+
113+
"""
114+
115+
assertPrettyPrintEqual(input: input, expected: expectedShorter, linelength: 10)
116+
}
86117
}

0 commit comments

Comments
 (0)