Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit c4611a0

Browse files
authored
Merge pull request rust-lang#3326 from scampi/issue-3302
fix formatting of strings within a macro
2 parents e28fae9 + b86dd16 commit c4611a0

File tree

5 files changed

+103
-7
lines changed

5 files changed

+103
-7
lines changed

src/lib.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ use crate::comment::LineClasses;
5050
use crate::formatting::{FormatErrorMap, FormattingError, ReportedErrors, SourceFile};
5151
use crate::issues::Issue;
5252
use crate::shape::Indent;
53+
use crate::utils::indent_next_line;
5354

5455
pub use crate::config::{
5556
load_config, CliOptions, Color, Config, Edition, EmitMode, FileLines, FileName, NewlineStyle,
@@ -438,7 +439,7 @@ fn format_code_block(code_snippet: &str, config: &Config) -> Option<FormattedSni
438439
}
439440
result.push_str(&line);
440441
result.push('\n');
441-
need_indent = !kind.is_string() || line.ends_with('\\');
442+
need_indent = indent_next_line(kind, &line, config);
442443
}
443444
result.push('}');
444445
result
@@ -499,7 +500,7 @@ fn format_code_block(code_snippet: &str, config: &Config) -> Option<FormattedSni
499500
line
500501
};
501502
result.push_str(trimmed_line);
502-
is_indented = !kind.is_string() || line.ends_with('\\');
503+
is_indented = indent_next_line(kind, line, config);
503504
}
504505
Some(FormattedSnippet {
505506
snippet: result,

src/macros.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ use crate::shape::{Indent, Shape};
4343
use crate::source_map::SpanUtils;
4444
use crate::spanned::Spanned;
4545
use crate::utils::{
46-
format_visibility, is_empty_line, mk_sp, remove_trailing_white_spaces, rewrite_ident,
47-
trim_left_preserve_layout, wrap_str, NodeIdExt,
46+
format_visibility, indent_next_line, is_empty_line, mk_sp, remove_trailing_white_spaces,
47+
rewrite_ident, trim_left_preserve_layout, wrap_str, NodeIdExt,
4848
};
4949
use crate::visitor::FmtVisitor;
5050

@@ -1299,7 +1299,7 @@ impl MacroBranch {
12991299
{
13001300
s += &indent_str;
13011301
}
1302-
(s + l + "\n", !kind.is_string() || l.ends_with('\\'))
1302+
(s + l + "\n", indent_next_line(kind, &l, &config))
13031303
},
13041304
)
13051305
.0;

src/utils.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -526,8 +526,10 @@ pub fn trim_left_preserve_layout(orig: &str, indent: Indent, config: &Config) ->
526526
Some(get_prefix_space_width(config, &line))
527527
};
528528

529-
let new_veto_trim_value = (kind.is_string()
530-
|| (config.version() == Version::Two && kind.is_commented_string()))
529+
// just InString{Commented} in order to allow the start of a string to be indented
530+
let new_veto_trim_value = (kind == FullCodeCharKind::InString
531+
|| (config.version() == Version::Two
532+
&& kind == FullCodeCharKind::InStringCommented))
531533
&& !line.ends_with('\\');
532534
let line = if veto_trim || new_veto_trim_value {
533535
veto_trim = new_veto_trim_value;
@@ -574,6 +576,13 @@ pub fn trim_left_preserve_layout(orig: &str, indent: Indent, config: &Config) ->
574576
)
575577
}
576578

579+
/// Based on the given line, determine if the next line can be indented or not.
580+
/// This allows to preserve the indentation of multi-line literals.
581+
pub fn indent_next_line(kind: FullCodeCharKind, line: &str, config: &Config) -> bool {
582+
!(kind.is_string() || (config.version() == Version::Two && kind.is_commented_string()))
583+
|| line.ends_with('\\')
584+
}
585+
577586
pub fn is_empty_line(s: &str) -> bool {
578587
s.is_empty() || s.chars().all(char::is_whitespace)
579588
}

tests/source/issue-3302.rs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// rustfmt-version: Two
2+
3+
macro_rules! moo1 {
4+
() => {
5+
bar! {
6+
"
7+
"
8+
}
9+
};
10+
}
11+
12+
macro_rules! moo2 {
13+
() => {
14+
bar! {
15+
"
16+
"
17+
}
18+
};
19+
}
20+
21+
macro_rules! moo3 {
22+
() => {
23+
42
24+
/*
25+
bar! {
26+
"
27+
toto
28+
tata"
29+
}
30+
*/
31+
};
32+
}
33+
34+
macro_rules! moo4 {
35+
() => {
36+
bar! {
37+
"
38+
foo
39+
bar
40+
baz"
41+
}
42+
};
43+
}

tests/target/issue-3302.rs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// rustfmt-version: Two
2+
3+
macro_rules! moo1 {
4+
() => {
5+
bar! {
6+
"
7+
"
8+
}
9+
};
10+
}
11+
12+
macro_rules! moo2 {
13+
() => {
14+
bar! {
15+
"
16+
"
17+
}
18+
};
19+
}
20+
21+
macro_rules! moo3 {
22+
() => {
23+
42
24+
/*
25+
bar! {
26+
"
27+
toto
28+
tata"
29+
}
30+
*/
31+
};
32+
}
33+
34+
macro_rules! moo4 {
35+
() => {
36+
bar! {
37+
"
38+
foo
39+
bar
40+
baz"
41+
}
42+
};
43+
}

0 commit comments

Comments
 (0)