Skip to content

Fix line breaking for attributes in inheritance clauses by grouping them with their type #1039

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 26, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Sources/SwiftFormat/PrettyPrint/TokenStreamCreator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2385,13 +2385,15 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor {
}

override func visit(_ node: AttributedTypeSyntax) -> SyntaxVisitorContinueKind {
before(node.firstToken(viewMode: .sourceAccurate), tokens: .open)
arrangeAttributeList(node.attributes)
for specifier in node.specifiers {
after(
specifier.firstToken(viewMode: .sourceAccurate),
tokens: .break(.continue, newlines: .elective(ignoresDiscretionary: true))
)
}
after(node.lastToken(viewMode: .sourceAccurate), tokens: .close)
return .visitChildren
}

Expand Down
21 changes: 21 additions & 0 deletions Tests/SwiftFormatTests/PrettyPrint/AttributeTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -623,4 +623,25 @@ final class AttributeTests: PrettyPrintTestCase {

assertPrettyPrintEqual(input: input, expected: expected, linelength: 45)
}

func testAttributeLineBreakInInheritanceClause() {
let input =
"""
public class MyClass: Foo, @unchecked Sendable, Bar {
// …
}

"""
let expected =
"""
public class MyClass: Foo,
@unchecked Sendable, Bar
{
// …
}

"""

assertPrettyPrintEqual(input: input, expected: expected, linelength: 45)
}
}
4 changes: 2 additions & 2 deletions Tests/SwiftFormatTests/PrettyPrint/FunctionDeclTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1129,8 +1129,8 @@ final class FunctionDeclTests: PrettyPrintTestCase {
.InnerMember,
) {}
func foo(
cmp: @escaping (R) ->
()
cmp:
@escaping (R) -> ()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! I was hoping we'd find some others with that change.

) {}
func foo(
cmp:
Expand Down
Loading