Skip to content

Commit 3ba04b7

Browse files
authored
Merge pull request swiftlang#138 from google/throwing-functions
Support throws/rethrows in function decls.
2 parents d029f42 + 569928f commit 3ba04b7

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

Sources/SwiftFormatPrettyPrint/TokenStreamCreator.swift

+4
Original file line numberDiff line numberDiff line change
@@ -767,6 +767,8 @@ private final class TokenStreamCreator: SyntaxVisitor {
767767
}
768768

769769
override func visit(_ node: FunctionDeclSyntax) {
770+
before(node.firstToken, tokens: .open(.inconsistent, 0))
771+
770772
if let attributes = node.attributes {
771773
before(node.firstToken, tokens: .space(size: 0), .open(.consistent, 0))
772774
after(attributes.lastToken, tokens: .open)
@@ -790,6 +792,7 @@ private final class TokenStreamCreator: SyntaxVisitor {
790792
before(body.rightBrace, tokens: .break(offset: -2), .close)
791793
}
792794

795+
after(node.lastToken, tokens: .close)
793796
super.visit(node)
794797
}
795798

@@ -824,6 +827,7 @@ private final class TokenStreamCreator: SyntaxVisitor {
824827
}
825828

826829
override func visit(_ node: FunctionSignatureSyntax) {
830+
before(node.throwsOrRethrowsKeyword, tokens: .break)
827831
before(node.output?.firstToken, tokens: .break)
828832
super.visit(node)
829833
}

Tests/SwiftFormatPrettyPrintTests/FunctionDeclTests.swift

+33
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,39 @@ public class FunctionDeclTests: PrettyPrintTestCase {
7373
assertPrettyPrintEqual(input: input, expected: expected, linelength: 50)
7474
}
7575

76+
public func testFunctionDeclThrows() {
77+
let input =
78+
"""
79+
func myFun(var1: Int) throws -> Double {
80+
print("Hello World")
81+
return 1.0
82+
}
83+
func reallyLongName(var1: Int, var2: Double, var3: Bool) throws -> Double {
84+
print("Hello World")
85+
return 1.0
86+
}
87+
"""
88+
89+
let expected =
90+
"""
91+
func myFun(var1: Int) throws -> Double {
92+
print("Hello World")
93+
return 1.0
94+
}
95+
func reallyLongName(
96+
var1: Int,
97+
var2: Double,
98+
var3: Bool
99+
) throws -> Double {
100+
print("Hello World")
101+
return 1.0
102+
}
103+
104+
"""
105+
106+
assertPrettyPrintEqual(input: input, expected: expected, linelength: 50)
107+
}
108+
76109
public func testFunctionGenericParameters() {
77110
let input =
78111
"""

0 commit comments

Comments
 (0)