Skip to content

Commit c2c0c4a

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

File tree

2 files changed

+76
-0
lines changed

2 files changed

+76
-0
lines changed

Sources/SwiftFormat/PrettyPrint/TokenStreamCreator.swift

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

1735+
override func visit(_ node: InlineArrayTypeSyntax) -> SyntaxVisitorContinueKind {
1736+
after(node.leftSquare, tokens: .break(.open, size: 0), .open)
1737+
before(node.separator, tokens: .space)
1738+
after(node.separator, tokens: .break)
1739+
before(node.rightSquare, tokens: .break(.close, size: 0), .close)
1740+
return .visitChildren
1741+
}
1742+
17351743
override func visit(_ node: TupleTypeSyntax) -> SyntaxVisitorContinueKind {
17361744
after(node.leftParen, tokens: .break(.open, size: 0), .open)
17371745
before(node.rightParen, tokens: .break(.close, size: 0), .close)

Tests/SwiftFormatTests/PrettyPrint/ArrayDeclTests.swift

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,4 +302,72 @@ 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+
[
349+
3 of
350+
VeryLongGenericTypeNameThatCausesWrapping
351+
]
352+
let a:
353+
[
354+
3 of
355+
[
356+
3 of
357+
VeryLongGenericTypeNameThatCausesWrapping
358+
]
359+
]
360+
let a =
361+
[
362+
3 of
363+
VeryLongGenericTypeNameThatCausesWrapping
364+
](
365+
repeating:
366+
foo
367+
)
368+
369+
"""
370+
371+
assertPrettyPrintEqual(input: input, expected: expected, linelength: 5)
372+
}
305373
}

0 commit comments

Comments
 (0)