Skip to content

Commit e43a1b1

Browse files
authored
Merge pull request #1042 from swiftlang/automerge/merge-main-2025-06-30_09-02
Merge `main` into `release/6.2`
2 parents e9bd3c8 + cbe4886 commit e43a1b1

File tree

7 files changed

+87
-17
lines changed

7 files changed

+87
-17
lines changed

.swift-format

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
"NoBlockComments": false,
1414
"OrderedImports": true,
1515
"UseLetInEveryBoundCaseVariable": false,
16-
"UseSynthesizedInitializer": false
16+
"UseSynthesizedInitializer": false,
17+
"ReturnVoidInsteadOfEmptyTuple": true,
18+
"NoVoidReturnOnFunctionSignature": true,
1719
}
1820
}

Sources/SwiftFormat/Core/Trivia+Convenience.swift

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -80,13 +80,19 @@ extension Trivia {
8080
})
8181
}
8282

83-
/// Returns `true` if this trivia contains any backslashes (used for multiline string newline
84-
/// suppression).
85-
var containsBackslashes: Bool {
86-
return contains(
87-
where: {
88-
if case .backslashes = $0 { return true }
89-
return false
90-
})
83+
/// Returns the prefix of this trivia that corresponds to the backslash and pound signs used to
84+
/// represent a non-line-break continuation of a multiline string, or nil if the trivia does not
85+
/// represent such a continuation.
86+
var multilineStringContinuation: String? {
87+
var result = ""
88+
for piece in pieces {
89+
switch piece {
90+
case .backslashes, .pounds:
91+
piece.write(to: &result)
92+
default:
93+
break
94+
}
95+
}
96+
return result.isEmpty ? nil : result
9197
}
9298
}

Sources/SwiftFormat/PrettyPrint/TokenStreamCreator.swift

Lines changed: 9 additions & 5 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

@@ -2635,11 +2637,13 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor {
26352637
emitSegmentTextTokens(segmentText[...])
26362638
}
26372639

2638-
if node.trailingTrivia.containsBackslashes && !config.reflowMultilineStringLiterals.isAlways {
2639-
// Segments with trailing backslashes won't end with a literal newline; the backslash is
2640-
// considered trivia. To preserve the original text and wrapping, we need to manually render
2641-
// the backslash and a break into the token stream.
2642-
appendToken(.syntax("\\"))
2640+
if !config.reflowMultilineStringLiterals.isAlways,
2641+
let continuation = node.trailingTrivia.multilineStringContinuation
2642+
{
2643+
// Segments with trailing backslashes won't end with a literal newline; the backslash and any
2644+
// `#` delimiters for raw strings are considered trivia. To preserve the original text and
2645+
// wrapping, we need to manually render them break into the token stream.
2646+
appendToken(.syntax(continuation))
26432647
appendToken(.break(breakKind, newlines: .hard(count: 1)))
26442648
}
26452649
return .skipChildren

Sources/swift-format/Frontend/FormatFrontend.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ class FormatFrontend: Frontend {
4040
return
4141
}
4242

43-
let diagnosticHandler: (SwiftDiagnostics.Diagnostic, SourceLocation) -> () = {
43+
let diagnosticHandler: (SwiftDiagnostics.Diagnostic, SourceLocation) -> Void = {
4444
(diagnostic, location) in
4545
guard !self.lintFormatOptions.ignoreUnparsableFiles else {
4646
// No diagnostics should be emitted in this mode.

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:

Tests/SwiftFormatTests/PrettyPrint/StringTests.swift

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -739,4 +739,41 @@ final class StringTests: PrettyPrintTestCase {
739739

740740
assertPrettyPrintEqual(input: input, expected: input + "\n", linelength: 20)
741741
}
742+
743+
func testMultilineStringWithContinuations() {
744+
let input =
745+
##"""
746+
let someString =
747+
"""
748+
lines \
749+
\nare \
750+
short.
751+
"""
752+
let someString =
753+
#"""
754+
lines \#
755+
\#nare \#
756+
short.
757+
"""#
758+
"""##
759+
760+
let expected =
761+
##"""
762+
let someString =
763+
"""
764+
lines \
765+
\nare \
766+
short.
767+
"""
768+
let someString =
769+
#"""
770+
lines \#
771+
\#nare \#
772+
short.
773+
"""#
774+
775+
"""##
776+
777+
assertPrettyPrintEqual(input: input, expected: expected, linelength: 30)
778+
}
742779
}

0 commit comments

Comments
 (0)