Skip to content
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
5 changes: 3 additions & 2 deletions .github/workflows/pull_request.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,13 @@ concurrency:
jobs:
tests:
name: Test
uses: swiftlang/github-workflows/.github/workflows/swift_package_test.yml@main
uses: swiftlang/github-workflows/.github/workflows/swift_package_test.yml@0.0.2
with:
linux_exclude_swift_versions: "[{\"swift_version\": \"5.8\"}]"
windows_exclude_swift_versions: "[{\"swift_version\": \"5.9\"}]"
soundness:
name: Soundness
uses: swiftlang/github-workflows/.github/workflows/soundness.yml@main
uses: swiftlang/github-workflows/.github/workflows/soundness.yml@0.0.2
with:
license_header_check_project_name: "Swift.org"
api_breakage_check_allowlist_path: "api-breakages.txt"
102 changes: 72 additions & 30 deletions Sources/SwiftFormat/PrettyPrint/TokenStreamCreator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2394,13 +2394,22 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor {

override func visit(_ node: AttributedTypeSyntax) -> SyntaxVisitorContinueKind {
before(node.firstToken(viewMode: .sourceAccurate), tokens: .open)
arrangeAttributeList(node.attributes)

let breakToken: Token = .break(.continue, newlines: .elective(ignoresDiscretionary: true))
for specifier in node.specifiers {
after(
specifier.firstToken(viewMode: .sourceAccurate),
tokens: .break(.continue, newlines: .elective(ignoresDiscretionary: true))
specifier.lastToken(viewMode: .sourceAccurate),
tokens: breakToken
)
}
arrangeAttributeList(node.attributes, suppressFinalBreak: false, lineBreak: breakToken, shouldGroup: false)
for specifier in node.lateSpecifiers {
after(
specifier.lastToken(viewMode: .sourceAccurate),
tokens: breakToken
)
}

after(node.lastToken(viewMode: .sourceAccurate), tokens: .close)
return .visitChildren
}
Expand Down Expand Up @@ -3066,40 +3075,73 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor {
private func arrangeAttributeList(
_ attributes: AttributeListSyntax?,
suppressFinalBreak: Bool = false,
separateByLineBreaks: Bool = false
separateByLineBreaks: Bool = false,
shouldGroup: Bool = true
) {
let behavior: NewlineBehavior = separateByLineBreaks ? .hard : .elective
arrangeAttributeList(
attributes,
suppressFinalBreak: suppressFinalBreak,
lineBreak: .break(.same, newlines: behavior),
shouldGroup: shouldGroup
)
}

/// Applies formatting tokens around and between the attributes in an attribute list.
private func arrangeAttributeList(
_ attributes: AttributeListSyntax?,
suppressFinalBreak: Bool,
lineBreak: Token,
shouldGroup: Bool
) {
if let attributes = attributes {
let behavior: NewlineBehavior = separateByLineBreaks ? .hard : .elective
guard let attributes, !attributes.isEmpty else {
return
}

if shouldGroup {
before(attributes.firstToken(viewMode: .sourceAccurate), tokens: .open)
if attributes.dropLast().isEmpty,
let ifConfig = attributes.first?.as(IfConfigDeclSyntax.self)
{
for clause in ifConfig.clauses {
if let nestedAttributes = AttributeListSyntax(clause.elements) {
arrangeAttributeList(nestedAttributes, suppressFinalBreak: true, separateByLineBreaks: separateByLineBreaks)
}
}

if attributes.dropLast().isEmpty,
let ifConfig = attributes.first?.as(IfConfigDeclSyntax.self)
{
for clause in ifConfig.clauses {
if let nestedAttributes = AttributeListSyntax(clause.elements) {
arrangeAttributeList(
nestedAttributes,
suppressFinalBreak: true,
lineBreak: lineBreak,
shouldGroup: shouldGroup
)
}
} else {
for element in attributes.dropLast() {
if let ifConfig = element.as(IfConfigDeclSyntax.self) {
for clause in ifConfig.clauses {
if let nestedAttributes = AttributeListSyntax(clause.elements) {
arrangeAttributeList(
nestedAttributes,
suppressFinalBreak: true,
separateByLineBreaks: separateByLineBreaks
)
}
}
} else {
for element in attributes.dropLast() {
if let ifConfig = element.as(IfConfigDeclSyntax.self) {
for clause in ifConfig.clauses {
if let nestedAttributes = AttributeListSyntax(clause.elements) {
arrangeAttributeList(
nestedAttributes,
suppressFinalBreak: true,
lineBreak: lineBreak,
shouldGroup: shouldGroup
)
}
} else {
after(element.lastToken(viewMode: .sourceAccurate), tokens: .break(.same, newlines: behavior))
}
} else {
after(element.lastToken(viewMode: .sourceAccurate), tokens: lineBreak)
}
}
var afterAttributeTokens = [Token.close]
if !suppressFinalBreak {
afterAttributeTokens.append(.break(.same, newlines: behavior))
}
}

var afterAttributeTokens = [Token]()
if shouldGroup {
afterAttributeTokens.append(.close)
}
if !suppressFinalBreak {
afterAttributeTokens.append(lineBreak)
}
if !afterAttributeTokens.isEmpty {
after(attributes.lastToken(viewMode: .sourceAccurate), tokens: afterAttributeTokens)
}
}
Expand Down
61 changes: 61 additions & 0 deletions Tests/SwiftFormatTests/PrettyPrint/FunctionTypeTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -283,4 +283,65 @@ final class FunctionTypeTests: PrettyPrintTestCase {

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

func testFunctionTypeWithTypeSpecifier() {
let input =
"""
func f(_ body: nonisolated(nonsending) () async -> Void) {}

func f(_ body: @Foo @Bar nonisolated(nonsending) () async -> Void) {}

func f(_ body: nonisolated(nonsending) @Foo @Bar () async -> Void) {}

func f(_ body: inout @Foo @Bar nonisolated(nonsending) () async -> Void) {}
"""

assertPrettyPrintEqual(
input: input,
expected: """
func f(_ body: nonisolated(nonsending) () async -> Void) {}

func f(_ body: @Foo @Bar nonisolated(nonsending) () async -> Void) {}

func f(_ body: nonisolated(nonsending) @Foo @Bar () async -> Void) {}

func f(_ body: inout @Foo @Bar nonisolated(nonsending) () async -> Void) {}

""",
linelength: 80
)

assertPrettyPrintEqual(
input: input,
expected: """
func f(
_ body:
nonisolated(nonsending) ()
async -> Void
) {}

func f(
_ body:
@Foo @Bar
nonisolated(nonsending) ()
async -> Void
) {}

func f(
_ body:
nonisolated(nonsending)
@Foo @Bar () async -> Void
) {}

func f(
_ body:
inout @Foo @Bar
nonisolated(nonsending) ()
async -> Void
) {}

""",
linelength: 30
)
}
}