Skip to content

Commit fe9df7d

Browse files
committed
Support InlineArray type sugar
1 parent e70ab31 commit fe9df7d

File tree

2 files changed

+66
-0
lines changed

2 files changed

+66
-0
lines changed

Sources/SwiftFormat/PrettyPrint/TokenStreamCreator.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1732,6 +1732,12 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor {
17321732
return .visitChildren
17331733
}
17341734

1735+
override func visit(_ node: InlineArrayTypeSyntax) -> SyntaxVisitorContinueKind {
1736+
before(node.separator, tokens: .space)
1737+
after(node.separator, tokens: .break)
1738+
return .visitChildren
1739+
}
1740+
17351741
override func visit(_ node: TupleTypeSyntax) -> SyntaxVisitorContinueKind {
17361742
after(node.leftParen, tokens: .break(.open, size: 0), .open)
17371743
before(node.rightParen, tokens: .break(.close, size: 0), .close)

Tests/SwiftFormatTests/PrettyPrint/ArrayDeclTests.swift

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,4 +302,64 @@ final class ArrayDeclTests: PrettyPrintTestCase {
302302

303303
assertPrettyPrintEqual(input: input, expected: expected, linelength: 32)
304304
}
305+
306+
func testInlineArrayTypeSugar() {
307+
let input =
308+
"""
309+
let a: [3 of Int]
310+
let a: [[3 of Int]]
311+
let a: [3 of [3 of Int]]
312+
let a: [n of Int]
313+
let fiveIntegers: [5 of _] = .init(repeating: 99)
314+
let fourBytes: [_ of Int8] = [1, 2, 3, 4]
315+
let fourIntegers: [_ of _] = [1, 2, 3, 4]
316+
let fiveDoubles = [5 of _](repeating: 1.23)
317+
318+
"""
319+
320+
let expected =
321+
"""
322+
let a: [3 of Int]
323+
let a: [[3 of Int]]
324+
let a: [3 of [3 of Int]]
325+
let a: [n of Int]
326+
let fiveIntegers: [5 of _] = .init(repeating: 99)
327+
let fourBytes: [_ of Int8] = [1, 2, 3, 4]
328+
let fourIntegers: [_ of _] = [1, 2, 3, 4]
329+
let fiveDoubles = [5 of _](repeating: 1.23)
330+
331+
"""
332+
333+
assertPrettyPrintEqual(input: input, expected: expected, linelength: 80)
334+
}
335+
336+
func testInlineArrayTypeSugarWhenLineLengthExceeded() {
337+
let input =
338+
"""
339+
let a: [3 of VeryLongGenericTypeNameThatCausesWrapping]
340+
let a: [3 of [3 of VeryLongGenericTypeNameThatCausesWrapping]]
341+
let a = [3 of VeryLongGenericTypeNameThatCausesWrapping](repeating: foo)
342+
343+
"""
344+
345+
let expected =
346+
"""
347+
let a:
348+
[3 of
349+
VeryLongGenericTypeNameThatCausesWrapping]
350+
let a:
351+
[3 of
352+
[3 of
353+
VeryLongGenericTypeNameThatCausesWrapping]]
354+
let a =
355+
[3 of
356+
VeryLongGenericTypeNameThatCausesWrapping](
357+
repeating:
358+
foo
359+
)
360+
361+
"""
362+
363+
assertPrettyPrintEqual(input: input, expected: expected, linelength: 5)
364+
}
305365
}

0 commit comments

Comments
 (0)