Skip to content

Merge main into release/6.2 #1042

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 5 commits into from
Jun 30, 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
4 changes: 3 additions & 1 deletion .swift-format
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
"NoBlockComments": false,
"OrderedImports": true,
"UseLetInEveryBoundCaseVariable": false,
"UseSynthesizedInitializer": false
"UseSynthesizedInitializer": false,
"ReturnVoidInsteadOfEmptyTuple": true,
"NoVoidReturnOnFunctionSignature": true,
}
}
22 changes: 14 additions & 8 deletions Sources/SwiftFormat/Core/Trivia+Convenience.swift
Original file line number Diff line number Diff line change
Expand Up @@ -80,13 +80,19 @@ extension Trivia {
})
}

/// Returns `true` if this trivia contains any backslashes (used for multiline string newline
/// suppression).
var containsBackslashes: Bool {
return contains(
where: {
if case .backslashes = $0 { return true }
return false
})
/// Returns the prefix of this trivia that corresponds to the backslash and pound signs used to
/// represent a non-line-break continuation of a multiline string, or nil if the trivia does not
/// represent such a continuation.
var multilineStringContinuation: String? {
var result = ""
for piece in pieces {
switch piece {
case .backslashes, .pounds:
piece.write(to: &result)
default:
break
}
}
return result.isEmpty ? nil : result
}
}
14 changes: 9 additions & 5 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 Expand Up @@ -2635,11 +2637,13 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor {
emitSegmentTextTokens(segmentText[...])
}

if node.trailingTrivia.containsBackslashes && !config.reflowMultilineStringLiterals.isAlways {
// Segments with trailing backslashes won't end with a literal newline; the backslash is
// considered trivia. To preserve the original text and wrapping, we need to manually render
// the backslash and a break into the token stream.
appendToken(.syntax("\\"))
if !config.reflowMultilineStringLiterals.isAlways,
let continuation = node.trailingTrivia.multilineStringContinuation
{
// Segments with trailing backslashes won't end with a literal newline; the backslash and any
// `#` delimiters for raw strings are considered trivia. To preserve the original text and
// wrapping, we need to manually render them break into the token stream.
appendToken(.syntax(continuation))
appendToken(.break(breakKind, newlines: .hard(count: 1)))
}
return .skipChildren
Expand Down
2 changes: 1 addition & 1 deletion Sources/swift-format/Frontend/FormatFrontend.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class FormatFrontend: Frontend {
return
}

let diagnosticHandler: (SwiftDiagnostics.Diagnostic, SourceLocation) -> () = {
let diagnosticHandler: (SwiftDiagnostics.Diagnostic, SourceLocation) -> Void = {
(diagnostic, location) in
guard !self.lintFormatOptions.ignoreUnparsableFiles else {
// No diagnostics should be emitted in this mode.
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) -> ()
) {}
func foo(
cmp:
Expand Down
37 changes: 37 additions & 0 deletions Tests/SwiftFormatTests/PrettyPrint/StringTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -739,4 +739,41 @@ final class StringTests: PrettyPrintTestCase {

assertPrettyPrintEqual(input: input, expected: input + "\n", linelength: 20)
}

func testMultilineStringWithContinuations() {
let input =
##"""
let someString =
"""
lines \
\nare \
short.
"""
let someString =
#"""
lines \#
\#nare \#
short.
"""#
"""##

let expected =
##"""
let someString =
"""
lines \
\nare \
short.
"""
let someString =
#"""
lines \#
\#nare \#
short.
"""#

"""##

assertPrettyPrintEqual(input: input, expected: expected, linelength: 30)
}
}
Loading