Skip to content

Commit 039519a

Browse files
authored
Merge pull request swiftlang#187 from google/format-func-signature-throws
Add a break before the `(re)throws` keyword in a function type.
2 parents 3b43386 + ec796ac commit 039519a

File tree

2 files changed

+64
-0
lines changed

2 files changed

+64
-0
lines changed

Sources/SwiftFormatPrettyPrint/TokenStreamCreator.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -601,6 +601,7 @@ private final class TokenStreamCreator: SyntaxVisitor {
601601
override func visit(_ node: FunctionTypeSyntax) {
602602
after(node.leftParen, tokens: .break(size: 0, offset: 2), .open(.consistent, 0))
603603
before(node.rightParen, tokens: .break(size: 0, offset: -2), .close)
604+
before(node.throwsOrRethrowsKeyword, tokens: .break)
604605
before(node.arrow, tokens: .break)
605606
before(node.returnType.firstToken, tokens: .break)
606607
super.visit(node)

Tests/SwiftFormatPrettyPrintTests/FunctionTypeTests.swift

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,4 +61,67 @@ public class FunctionTypeTests: PrettyPrintTestCase {
6161

6262
assertPrettyPrintEqual(input: input, expected: expected, linelength: 60)
6363
}
64+
65+
public func testFunctionTypeThrows() {
66+
let input =
67+
"""
68+
func f(g: (_ somevalue: Int) throws -> String?) {
69+
let a = 123
70+
let b = "abc"
71+
}
72+
func f(g: (currentLevel: Int) throws -> String?) {
73+
let a = 123
74+
let b = "abc"
75+
}
76+
func f(g: (currentLevel: inout Int) throws -> String?) {
77+
let a = 123
78+
let b = "abc"
79+
}
80+
func f(g: (variable1: Int, variable2: Double, variable3: Bool) throws -> Double) {
81+
let a = 123
82+
let b = "abc"
83+
}
84+
func f(g: (variable1: Int, variable2: Double, variable3: Bool, variable4: String) throws -> Double) {
85+
let a = 123
86+
let b = "abc"
87+
}
88+
"""
89+
90+
let expected =
91+
"""
92+
func f(g: (_ somevalue: Int) throws -> String?) {
93+
let a = 123
94+
let b = "abc"
95+
}
96+
func f(g: (currentLevel: Int) throws -> String?) {
97+
let a = 123
98+
let b = "abc"
99+
}
100+
func f(g: (currentLevel: inout Int) throws -> String?) {
101+
let a = 123
102+
let b = "abc"
103+
}
104+
func f(
105+
g: (variable1: Int, variable2: Double, variable3: Bool) throws ->
106+
Double
107+
) {
108+
let a = 123
109+
let b = "abc"
110+
}
111+
func f(
112+
g: (
113+
variable1: Int,
114+
variable2: Double,
115+
variable3: Bool,
116+
variable4: String
117+
) throws -> Double
118+
) {
119+
let a = 123
120+
let b = "abc"
121+
}
122+
123+
"""
124+
125+
assertPrettyPrintEqual(input: input, expected: expected, linelength: 67)
126+
}
64127
}

0 commit comments

Comments
 (0)