Skip to content

Commit 116f1f9

Browse files
authored
Merge pull request #93 from dylansturg/trailing_closing_paren
Allow the closing paren for function decls in protocols on the same l…
2 parents b0a8d9a + 4da0ecb commit 116f1f9

File tree

2 files changed

+46
-5
lines changed

2 files changed

+46
-5
lines changed

Sources/SwiftFormatPrettyPrint/TokenStreamCreator.swift

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,9 @@ private final class TokenStreamCreator: SyntaxVisitor {
262262
after(node.identifier.lastToken, tokens: .space)
263263
}
264264

265+
let mustBreak = node.body != nil || node.signature.output != nil
266+
arrangeParameterClause(node.signature.input, forcesBreakBeforeRightParen: mustBreak)
267+
265268
arrangeFunctionLikeDecl(
266269
node,
267270
attributes: node.attributes,
@@ -283,6 +286,8 @@ private final class TokenStreamCreator: SyntaxVisitor {
283286

284287
before(node.throwsOrRethrowsKeyword, tokens: .break)
285288

289+
arrangeParameterClause(node.parameters, forcesBreakBeforeRightParen: node.body != nil)
290+
286291
arrangeFunctionLikeDecl(
287292
node,
288293
attributes: node.attributes,
@@ -333,6 +338,8 @@ private final class TokenStreamCreator: SyntaxVisitor {
333338

334339
after(node.lastToken, tokens: .close)
335340

341+
arrangeParameterClause(node.indices, forcesBreakBeforeRightParen: true)
342+
336343
return .visitChildren
337344
}
338345

@@ -870,9 +877,6 @@ private final class TokenStreamCreator: SyntaxVisitor {
870877
}
871878

872879
func visit(_ node: ParameterClauseSyntax) -> SyntaxVisitorContinueKind {
873-
after(node.leftParen, tokens: .break(.open, size: 0), .open(argumentListConsistency()))
874-
before(node.rightParen, tokens: .break(.close, size: 0), .close)
875-
876880
// Prioritize keeping ") throws -> <return_type>" together.
877881
if config.prioritizeKeepingFunctionOutputTogether {
878882
// Due to visitation order, this .open corresponds to a .close added in FunctionDeclSyntax
@@ -990,6 +994,11 @@ private final class TokenStreamCreator: SyntaxVisitor {
990994

991995
func visit(_ node: EnumCaseElementSyntax) -> SyntaxVisitorContinueKind {
992996
after(node.trailingComma, tokens: .break)
997+
998+
if let associatedValue = node.associatedValue {
999+
arrangeParameterClause(associatedValue, forcesBreakBeforeRightParen: true)
1000+
}
1001+
9931002
return .visitChildren
9941003
}
9951004

@@ -1972,6 +1981,22 @@ private final class TokenStreamCreator: SyntaxVisitor {
19721981
return contentsIterator.next() == nil && !commentPrecedesRightBrace
19731982
}
19741983

1984+
/// Applies formatting to a collection of parameters for a decl.
1985+
///
1986+
/// - Parameters:
1987+
/// - parameters: A node that contains the parameters that can be passed to a decl when its
1988+
/// called.
1989+
/// - forcesBreakBeforeRightParen: Whether a break should be required before the right paren
1990+
/// when the right paren is on a different line than the corresponding left paren.
1991+
private func arrangeParameterClause(
1992+
_ parameters: ParameterClauseSyntax, forcesBreakBeforeRightParen: Bool
1993+
) {
1994+
after(parameters.leftParen, tokens: .break(.open, size: 0), .open(argumentListConsistency()))
1995+
before(
1996+
parameters.rightParen,
1997+
tokens: .break(.close(mustBreak: forcesBreakBeforeRightParen), size: 0), .close)
1998+
}
1999+
19752000
/// Applies consistent formatting to the braces and contents of the given node.
19762001
///
19772002
/// - Parameters:

Tests/SwiftFormatPrettyPrintTests/ProtocolDeclTests.swift

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,10 @@ public class ProtocolDeclTests: PrettyPrintTestCase {
144144
protocol MyProtocol {
145145
func foo(bar: Int) -> Int
146146
func reallyLongName(reallyLongLabel: Int, anotherLongLabel: Bool) -> Float
147+
func doAProtoThing(first: Foo, second s: Bar)
148+
func doAThing(first: Foo) -> ResultType
149+
func doSomethingElse(firstArg: Foo, second secondArg: Bar, third thirdArg: Baz)
150+
func doStuff(firstArg: Foo, second second: Bar, third third: Baz) -> Output
147151
}
148152
"""
149153

@@ -155,6 +159,19 @@ public class ProtocolDeclTests: PrettyPrintTestCase {
155159
reallyLongLabel: Int,
156160
anotherLongLabel: Bool
157161
) -> Float
162+
func doAProtoThing(
163+
first: Foo, second s: Bar)
164+
func doAThing(first: Foo)
165+
-> ResultType
166+
func doSomethingElse(
167+
firstArg: Foo,
168+
second secondArg: Bar,
169+
third thirdArg: Baz)
170+
func doStuff(
171+
firstArg: Foo,
172+
second second: Bar,
173+
third third: Baz
174+
) -> Output
158175
}
159176
160177
"""
@@ -177,8 +194,7 @@ public class ProtocolDeclTests: PrettyPrintTestCase {
177194
init(bar: Int)
178195
init(
179196
reallyLongLabel: Int,
180-
anotherLongLabel: Bool
181-
)
197+
anotherLongLabel: Bool)
182198
}
183199
184200
"""

0 commit comments

Comments
 (0)