Skip to content

Commit 35e8687

Browse files
committed
Support attributes in closure signatures.
1 parent 427d314 commit 35e8687

File tree

2 files changed

+39
-2
lines changed

2 files changed

+39
-2
lines changed

Sources/SwiftFormatPrettyPrint/TokenStreamCreator.swift

+11-2
Original file line numberDiff line numberDiff line change
@@ -1068,6 +1068,8 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor {
10681068
override func visit(_ node: ClosureSignatureSyntax) -> SyntaxVisitorContinueKind {
10691069
before(node.firstToken, tokens: .open)
10701070

1071+
arrangeAttributeList(node.attributes, suppressFinalBreak: node.input == nil)
1072+
10711073
if let input = node.input {
10721074
// We unconditionally put a break before the `in` keyword below, so we should only put a break
10731075
// after the capture list's right bracket if there are arguments following it or we'll end up
@@ -2531,11 +2533,18 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor {
25312533
// MARK: - Various other helper methods
25322534

25332535
/// Applies formatting tokens around and between the attributes in an attribute list.
2534-
private func arrangeAttributeList(_ attributes: AttributeListSyntax?) {
2536+
private func arrangeAttributeList(
2537+
_ attributes: AttributeListSyntax?,
2538+
suppressFinalBreak: Bool = false
2539+
) {
25352540
if let attributes = attributes {
25362541
before(attributes.firstToken, tokens: .open)
25372542
insertTokens(.break(.same), betweenElementsOf: attributes)
2538-
after(attributes.lastToken, tokens: .close, .break(.same))
2543+
var afterAttributeTokens = [Token.close]
2544+
if !suppressFinalBreak {
2545+
afterAttributeTokens.append(.break(.same))
2546+
}
2547+
after(attributes.lastToken, tokens: afterAttributeTokens)
25392548
}
25402549
}
25412550

Tests/SwiftFormatPrettyPrintTests/ClosureExprTests.swift

+28
Original file line numberDiff line numberDiff line change
@@ -484,4 +484,32 @@ final class ClosureExprTests: PrettyPrintTestCase {
484484
assertPrettyPrintEqual(
485485
input: input, expected: expectedKeepingOutputTogether, linelength: 50, configuration: config)
486486
}
487+
488+
func testClosureSignatureAttributes() {
489+
let input =
490+
"""
491+
let a = { @MainActor in print("hi") }
492+
let b = { @MainActor in print("hello world") }
493+
let c = { @MainActor param in print("hi") }
494+
let d = { @MainActor (a: Int) async -> Int in print("hi") }
495+
"""
496+
497+
let expected =
498+
"""
499+
let a = { @MainActor in print("hi") }
500+
let b = { @MainActor in
501+
print("hello world")
502+
}
503+
let c = { @MainActor param in
504+
print("hi")
505+
}
506+
let d = {
507+
@MainActor (a: Int) async -> Int in
508+
print("hi")
509+
}
510+
511+
"""
512+
513+
assertPrettyPrintEqual(input: input, expected: expected, linelength: 40)
514+
}
487515
}

0 commit comments

Comments
 (0)