Skip to content

Commit 25b2d9c

Browse files
committed
print: bypass the multiline mode of pretty_string_literal for merely trailing \n.
1 parent 5ce8e28 commit 25b2d9c

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

src/print/mod.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1723,12 +1723,16 @@ impl Printer<'_> {
17231723

17241724
/// Pretty-print a string literal with escaping and styling.
17251725
fn pretty_string_literal(&self, s: &str) -> pretty::Fragment {
1726+
// HACK(eddyb) avoid using multiline formatting for trailing `\n`, which
1727+
// may be common in e.g. `debugPrintf("foo\n")`-style messages.
1728+
let use_multiline_format = s.trim_end_matches('\n').contains('\n');
1729+
17261730
// HACK(eddyb) this is somewhat inefficient, but we need to allocate a
17271731
// `String` for every piece anyway, so might as well make it convenient.
17281732
pretty::Fragment::new(
17291733
// HACK(eddyb) this allows aligning the actual string contents,
17301734
// (see `c == '\n'` special-casing below for when this applies).
1731-
(s.contains('\n').then_some(Either::Left(' ')).into_iter())
1735+
(use_multiline_format.then_some(Either::Left(' ')).into_iter())
17321736
.chain([Either::Left('"')])
17331737
.chain(s.chars().flat_map(|c| {
17341738
let escaped = c.escape_debug();
@@ -1747,7 +1751,8 @@ impl Printer<'_> {
17471751
// HACK(eddyb) move escaped `\n` to the start of a new line,
17481752
// using Rust's trailing `\` on the previous line, which eats
17491753
// all following whitespace (and only stops at the escape).
1750-
let extra_prefix_unescaped = if c == '\n' { "\\\n" } else { "" };
1754+
let extra_prefix_unescaped =
1755+
if c == '\n' && use_multiline_format { "\\\n" } else { "" };
17511756

17521757
(extra_prefix_unescaped.chars().map(Either::Left)).chain([maybe_escaped])
17531758
}))

0 commit comments

Comments
 (0)