Skip to content

Commit e8757ad

Browse files
authored
print: bypass the multiline mode of pretty_string_literal for merely trailing \n. (#27)
At some point I added a multiline pretty string format using Rust-compatible string continuations, i.e. `\` at the end of a line ignores all whitespace following it, so these are equivalent Rust literals: ```rust "foo\ \n" ``` ```rust "foo\n" ``` ```rust "foo " ``` (the last one is never used by `spirt::pretty`, because it cannot be indented without changing the meaning of the string literal, and `spirt::print::pretty` assumes *everything* can be indented) This PR ignores trailing `\n` when determining whether the multiline format should be used, i.e. only `\n` *in the middle* of a string will use `\` continuations (with the `\n` moved to the start of following lines). --- Quick example (the very one that made me think it'd be worth a minor aesthetic improvement): |Before|After| |-|-| |<img width="1276" height="1127" alt="image" src="https://github.com/user-attachments/assets/7f6bdb20-79ff-4f25-b2f9-e9b961578bac" />|<img width="2066" height="566" alt="image" src="https://github.com/user-attachments/assets/afca8cba-0d00-45fd-ba66-bbd059238a88" />|
2 parents 5ce8e28 + 25b2d9c commit e8757ad

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)