You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Change how PrettyPrinter emits StringSegmentSyntax's so that they can be broken up over multiple lines within multiline string literals. This does not change the behavior of single line string literals, which cannot contain newlines. This also does not change interpolations which are still emitted as verbatims and are never line broken by the pretty printer. Line breaking is done exclusively through escaped newlines. A literal containing escaped newlines will remove all escaped newlines and then reinsert them based on line length. Hard newlines are respected by the formatter and will not be moved, even if it causes short lines. Escaped newlines will be reformatted by the pretty printer so that lines ending in an escaped newline are at line length.
Wrapping is implemented by introducing a new newlinebreak behavior `.escaped`. `.escaped` acts very similarly to `.elective` but has slightly different length calculation logic and printing behavior. An escaped newline is printed including it's preceeding whitespace followed by "\\\n". So a break of `.break(_, 2, .escaped)` will print as " \\\n". Because an escaped line break takes up characters when broken (unlike other breaks), length calculation must be handled differently for `.escaped` breaks.
/// Determines how multiline string literals should reflow when formatted.
199
+
publicenumMultilineStringReflowBehavior:Codable{
200
+
/// Never reflow multiline string literals.
201
+
case never
202
+
/// Reflow lines in string literal that exceed the maximum line length. For example with a line length of 10:
203
+
/// ```swift
204
+
/// """
205
+
/// an escape\
206
+
/// line break
207
+
/// a hard line break
208
+
/// """
209
+
/// ```
210
+
/// will be formatted as:
211
+
/// ```swift
212
+
/// """
213
+
/// an esacpe\
214
+
/// line break
215
+
/// a hard \
216
+
/// line break
217
+
/// """
218
+
/// ```
219
+
/// The existing `\` is left in place, but the line over line length is broken.
220
+
case onlyLinesOverLength
221
+
/// Always reflow multiline string literals, this will ignore existing escaped newlines in the literal and reflow each line. Hard linebreaks are still respected.
Copy file name to clipboardExpand all lines: Sources/SwiftFormat/PrettyPrint/Token.swift
+4
Original file line number
Diff line number
Diff line change
@@ -147,6 +147,10 @@ enum NewlineBehavior {
147
147
/// newlines and the configured maximum number of blank lines.
148
148
case hard(count:Int)
149
149
150
+
/// Break onto a new line is allowed if neccessary. If a line break is emitted, it will be escaped with a '\', and this breaks whitespace will be printed prior to the
151
+
/// escaped line break. This is useful in multiline strings where we don't want newlines printed in syntax to appear in the literal.
152
+
case escaped
153
+
150
154
/// An elective newline that respects discretionary newlines from the user-entered text.
0 commit comments