Skip to content

Commit cbc37a0

Browse files
authored
Merge pull request #121 from dylansturg/types_need_spaces
Use breaks around type annotations of ForInStmts
2 parents 8991740 + 889d135 commit cbc37a0

File tree

3 files changed

+50
-0
lines changed

3 files changed

+50
-0
lines changed

Sources/SwiftFormatPrettyPrint/TokenStreamCreator.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -511,6 +511,11 @@ private final class TokenStreamCreator: SyntaxVisitor {
511511
before(node.inKeyword, tokens: .break)
512512
after(node.inKeyword, tokens: .space)
513513

514+
if let typeAnnotation = node.typeAnnotation {
515+
after(typeAnnotation.colon, tokens: .break(.open(kind: .continuation)))
516+
after(typeAnnotation.lastToken, tokens: .break(.close(mustBreak: false), size: 0))
517+
}
518+
514519
arrangeBracesAndContents(of: node.body, contentsKeyPath: \.statements)
515520

516521
return .visitChildren

Tests/SwiftFormatPrettyPrintTests/ForInStmtTests.swift

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,4 +262,48 @@ public class ForInStmtTests: PrettyPrintTestCase {
262262

263263
assertPrettyPrintEqual(input: input, expected: expected, linelength: 30)
264264
}
265+
266+
public func testExplicitTypeAnnotation() {
267+
let input =
268+
"""
269+
for i: ExplicitType in mycontainer {
270+
let a = 123
271+
let b = i
272+
}
273+
274+
for i:ExplicitType in mycontainer {
275+
let a = 123
276+
let b = i
277+
}
278+
279+
for i: [ExplicitKeyType: ExplicitValueType] in myverylongcontainername {
280+
let a = 123
281+
let b = i
282+
}
283+
"""
284+
285+
let expected =
286+
"""
287+
for i: ExplicitType in mycontainer {
288+
let a = 123
289+
let b = i
290+
}
291+
292+
for i: ExplicitType in mycontainer {
293+
let a = 123
294+
let b = i
295+
}
296+
297+
for i:
298+
[ExplicitKeyType: ExplicitValueType]
299+
in myverylongcontainername
300+
{
301+
let a = 123
302+
let b = i
303+
}
304+
305+
"""
306+
307+
assertPrettyPrintEqual(input: input, expected: expected, linelength: 40)
308+
}
265309
}

Tests/SwiftFormatPrettyPrintTests/XCTestManifests.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,7 @@ extension ForInStmtTests {
229229
// to regenerate.
230230
static let __allTests__ForInStmtTests = [
231231
("testBasicForLoop", testBasicForLoop),
232+
("testExplicitTypeAnnotation", testExplicitTypeAnnotation),
232233
("testForCase", testForCase),
233234
("testForLabels", testForLabels),
234235
("testForLoopFullWrap", testForLoopFullWrap),

0 commit comments

Comments
 (0)