diff --git a/crates/compiler/fmt/src/expr.rs b/crates/compiler/fmt/src/expr.rs index ac97d812aa..04a23a06be 100644 --- a/crates/compiler/fmt/src/expr.rs +++ b/crates/compiler/fmt/src/expr.rs @@ -484,24 +484,20 @@ pub fn fmt_str_literal<'buf>(buf: &mut Buf<'buf>, literal: StrLiteral, indent: u buf.push('"'); match literal { PlainLine(string) => { - // When a PlainLine contains "\n" it is formatted as a block string using """ - let mut lines = string.split('\n'); - match (lines.next(), lines.next()) { - (Some(first), Some(second)) => { - buf.push_str("\"\""); - buf.newline(); - - for line in [first, second].into_iter().chain(lines) { - buf.indent(indent); - buf.push_str_allow_spaces(line); - buf.newline(); - } - + // When a PlainLine contains '\n' or '"', format as a block string + if string.contains('"') || string.contains('\n') { + buf.push_str("\"\""); + buf.newline(); + for line in string.split('\n') { buf.indent(indent); - buf.push_str("\"\""); + buf.push_str_allow_spaces(line); + buf.newline(); } - _ => buf.push_str_allow_spaces(string), - } + buf.indent(indent); + buf.push_str("\"\""); + } else { + buf.push_str_allow_spaces(string); + }; } Line(segments) => { for seg in segments.iter() {