Skip to content

Commit 3041452

Browse files
committed
Fix line breaking for attributes by grouping them with their type
1 parent e70ab31 commit 3041452

File tree

3 files changed

+25
-2
lines changed

3 files changed

+25
-2
lines changed

Sources/SwiftFormat/PrettyPrint/TokenStreamCreator.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2385,13 +2385,15 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor {
23852385
}
23862386

23872387
override func visit(_ node: AttributedTypeSyntax) -> SyntaxVisitorContinueKind {
2388+
before(node.firstToken(viewMode: .sourceAccurate), tokens: .open)
23882389
arrangeAttributeList(node.attributes)
23892390
for specifier in node.specifiers {
23902391
after(
23912392
specifier.firstToken(viewMode: .sourceAccurate),
23922393
tokens: .break(.continue, newlines: .elective(ignoresDiscretionary: true))
23932394
)
23942395
}
2396+
after(node.lastToken(viewMode: .sourceAccurate), tokens: .close)
23952397
return .visitChildren
23962398
}
23972399

Tests/SwiftFormatTests/PrettyPrint/AttributeTests.swift

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -623,4 +623,25 @@ final class AttributeTests: PrettyPrintTestCase {
623623

624624
assertPrettyPrintEqual(input: input, expected: expected, linelength: 45)
625625
}
626+
627+
func testAttributeLineBreakInInheritanceClause() {
628+
let input =
629+
"""
630+
public class MyClass: Foo, @unchecked Sendable, Bar {
631+
// …
632+
}
633+
634+
"""
635+
let expected =
636+
"""
637+
public class MyClass: Foo,
638+
@unchecked Sendable, Bar
639+
{
640+
// …
641+
}
642+
643+
"""
644+
645+
assertPrettyPrintEqual(input: input, expected: expected, linelength: 45)
646+
}
626647
}

Tests/SwiftFormatTests/PrettyPrint/FunctionDeclTests.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1129,8 +1129,8 @@ final class FunctionDeclTests: PrettyPrintTestCase {
11291129
.InnerMember,
11301130
) {}
11311131
func foo(
1132-
cmp: @escaping (R) ->
1133-
()
1132+
cmp:
1133+
@escaping (R) -> ()
11341134
) {}
11351135
func foo(
11361136
cmp:

0 commit comments

Comments
 (0)