File tree Expand file tree Collapse file tree 3 files changed +58
-13
lines changed
Tests/SwiftFormatTests/PrettyPrint Expand file tree Collapse file tree 3 files changed +58
-13
lines changed Original file line number Diff line number Diff line change @@ -80,13 +80,19 @@ extension Trivia {
80
80
} )
81
81
}
82
82
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
91
97
}
92
98
}
Original file line number Diff line number Diff line change @@ -2637,11 +2637,13 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor {
2637
2637
emitSegmentTextTokens ( segmentText [ ... ] )
2638
2638
}
2639
2639
2640
- if node. trailingTrivia. containsBackslashes && !config. reflowMultilineStringLiterals. isAlways {
2641
- // Segments with trailing backslashes won't end with a literal newline; the backslash is
2642
- // considered trivia. To preserve the original text and wrapping, we need to manually render
2643
- // the backslash and a break into the token stream.
2644
- 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) )
2645
2647
appendToken ( . break( breakKind, newlines: . hard( count: 1 ) ) )
2646
2648
}
2647
2649
return . skipChildren
Original file line number Diff line number Diff line change @@ -739,4 +739,41 @@ final class StringTests: PrettyPrintTestCase {
739
739
740
740
assertPrettyPrintEqual ( input: input, expected: input + " \n " , linelength: 20 )
741
741
}
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
+ }
742
779
}
You can’t perform that action at this time.
0 commit comments