Skip to content

Commit 9f9398b

Browse files
authored
Merge pull request swiftlang#181 from dabelknap/associatedtype
Support associated types
2 parents e5f5dd9 + c6aedaf commit 9f9398b

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed

Sources/SwiftFormatPrettyPrint/TokenStreamCreator.swift

+9
Original file line numberDiff line numberDiff line change
@@ -1232,6 +1232,15 @@ private final class TokenStreamCreator: SyntaxVisitor {
12321232
}
12331233

12341234
override func visit(_ node: AssociatedtypeDeclSyntax) {
1235+
if let attributes = node.attributes {
1236+
before(node.firstToken, tokens: .space(size: 0), .open(.consistent, 0))
1237+
after(attributes.lastToken, tokens: .open)
1238+
} else {
1239+
before(node.firstToken, tokens: .space(size: 0), .open(.consistent, 0), .open)
1240+
}
1241+
after(node.lastToken, tokens: .close, .close)
1242+
after(node.associatedtypeKeyword, tokens: .break)
1243+
before(node.genericWhereClause?.firstToken, tokens: .break)
12351244
super.visit(node)
12361245
}
12371246

Tests/SwiftFormatPrettyPrintTests/ProtocolDeclTests.swift

+37
Original file line numberDiff line numberDiff line change
@@ -189,4 +189,41 @@ public class ProtocolDeclTests: PrettyPrintTestCase {
189189

190190
assertPrettyPrintEqual(input: input, expected: expected, linelength: 30)
191191
}
192+
193+
public func testProtocolWithAssociatedtype() {
194+
let input =
195+
"""
196+
protocol MyProtocol {
197+
var A: Int
198+
199+
associatedtype TypeOne
200+
201+
associatedtype TypeTwo: AnotherType
202+
203+
associatedtype TypeThree: SomeType where TypeThree.Item == Item
204+
205+
@available(swift 4.0)
206+
associatedtype TypeFour
207+
}
208+
"""
209+
210+
let expected =
211+
"""
212+
protocol MyProtocol {
213+
var A: Int
214+
215+
associatedtype TypeOne
216+
217+
associatedtype TypeTwo: AnotherType
218+
219+
associatedtype TypeThree: SomeType where TypeThree.Item == Item
220+
221+
@available(swift 4.0)
222+
associatedtype TypeFour
223+
}
224+
225+
"""
226+
227+
assertPrettyPrintEqual(input: input, expected: expected, linelength: 65)
228+
}
192229
}

0 commit comments

Comments
 (0)