mirror of
https://github.com/rust-lang/rust.git
synced 2025-04-28 11:07:42 +00:00
Merge pull request #3326 from scampi/issue-3302
fix formatting of strings within a macro
This commit is contained in:
commit
c4611a0e6b
@ -50,6 +50,7 @@ use crate::comment::LineClasses;
|
|||||||
use crate::formatting::{FormatErrorMap, FormattingError, ReportedErrors, SourceFile};
|
use crate::formatting::{FormatErrorMap, FormattingError, ReportedErrors, SourceFile};
|
||||||
use crate::issues::Issue;
|
use crate::issues::Issue;
|
||||||
use crate::shape::Indent;
|
use crate::shape::Indent;
|
||||||
|
use crate::utils::indent_next_line;
|
||||||
|
|
||||||
pub use crate::config::{
|
pub use crate::config::{
|
||||||
load_config, CliOptions, Color, Config, Edition, EmitMode, FileLines, FileName, NewlineStyle,
|
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
|
|||||||
}
|
}
|
||||||
result.push_str(&line);
|
result.push_str(&line);
|
||||||
result.push('\n');
|
result.push('\n');
|
||||||
need_indent = !kind.is_string() || line.ends_with('\\');
|
need_indent = indent_next_line(kind, &line, config);
|
||||||
}
|
}
|
||||||
result.push('}');
|
result.push('}');
|
||||||
result
|
result
|
||||||
@ -499,7 +500,7 @@ fn format_code_block(code_snippet: &str, config: &Config) -> Option<FormattedSni
|
|||||||
line
|
line
|
||||||
};
|
};
|
||||||
result.push_str(trimmed_line);
|
result.push_str(trimmed_line);
|
||||||
is_indented = !kind.is_string() || line.ends_with('\\');
|
is_indented = indent_next_line(kind, line, config);
|
||||||
}
|
}
|
||||||
Some(FormattedSnippet {
|
Some(FormattedSnippet {
|
||||||
snippet: result,
|
snippet: result,
|
||||||
|
@ -43,8 +43,8 @@ use crate::shape::{Indent, Shape};
|
|||||||
use crate::source_map::SpanUtils;
|
use crate::source_map::SpanUtils;
|
||||||
use crate::spanned::Spanned;
|
use crate::spanned::Spanned;
|
||||||
use crate::utils::{
|
use crate::utils::{
|
||||||
format_visibility, is_empty_line, mk_sp, remove_trailing_white_spaces, rewrite_ident,
|
format_visibility, indent_next_line, is_empty_line, mk_sp, remove_trailing_white_spaces,
|
||||||
trim_left_preserve_layout, wrap_str, NodeIdExt,
|
rewrite_ident, trim_left_preserve_layout, wrap_str, NodeIdExt,
|
||||||
};
|
};
|
||||||
use crate::visitor::FmtVisitor;
|
use crate::visitor::FmtVisitor;
|
||||||
|
|
||||||
@ -1299,7 +1299,7 @@ impl MacroBranch {
|
|||||||
{
|
{
|
||||||
s += &indent_str;
|
s += &indent_str;
|
||||||
}
|
}
|
||||||
(s + l + "\n", !kind.is_string() || l.ends_with('\\'))
|
(s + l + "\n", indent_next_line(kind, &l, &config))
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
.0;
|
.0;
|
||||||
|
13
src/utils.rs
13
src/utils.rs
@ -526,8 +526,10 @@ pub fn trim_left_preserve_layout(orig: &str, indent: Indent, config: &Config) ->
|
|||||||
Some(get_prefix_space_width(config, &line))
|
Some(get_prefix_space_width(config, &line))
|
||||||
};
|
};
|
||||||
|
|
||||||
let new_veto_trim_value = (kind.is_string()
|
// just InString{Commented} in order to allow the start of a string to be indented
|
||||||
|| (config.version() == Version::Two && kind.is_commented_string()))
|
let new_veto_trim_value = (kind == FullCodeCharKind::InString
|
||||||
|
|| (config.version() == Version::Two
|
||||||
|
&& kind == FullCodeCharKind::InStringCommented))
|
||||||
&& !line.ends_with('\\');
|
&& !line.ends_with('\\');
|
||||||
let line = if veto_trim || new_veto_trim_value {
|
let line = if veto_trim || new_veto_trim_value {
|
||||||
veto_trim = new_veto_trim_value;
|
veto_trim = new_veto_trim_value;
|
||||||
@ -574,6 +576,13 @@ pub fn trim_left_preserve_layout(orig: &str, indent: Indent, config: &Config) ->
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Based on the given line, determine if the next line can be indented or not.
|
||||||
|
/// This allows to preserve the indentation of multi-line literals.
|
||||||
|
pub fn indent_next_line(kind: FullCodeCharKind, line: &str, config: &Config) -> bool {
|
||||||
|
!(kind.is_string() || (config.version() == Version::Two && kind.is_commented_string()))
|
||||||
|
|| line.ends_with('\\')
|
||||||
|
}
|
||||||
|
|
||||||
pub fn is_empty_line(s: &str) -> bool {
|
pub fn is_empty_line(s: &str) -> bool {
|
||||||
s.is_empty() || s.chars().all(char::is_whitespace)
|
s.is_empty() || s.chars().all(char::is_whitespace)
|
||||||
}
|
}
|
||||||
|
43
tests/source/issue-3302.rs
Normal file
43
tests/source/issue-3302.rs
Normal file
@ -0,0 +1,43 @@
|
|||||||
|
// rustfmt-version: Two
|
||||||
|
|
||||||
|
macro_rules! moo1 {
|
||||||
|
() => {
|
||||||
|
bar! {
|
||||||
|
"
|
||||||
|
"
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
macro_rules! moo2 {
|
||||||
|
() => {
|
||||||
|
bar! {
|
||||||
|
"
|
||||||
|
"
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
macro_rules! moo3 {
|
||||||
|
() => {
|
||||||
|
42
|
||||||
|
/*
|
||||||
|
bar! {
|
||||||
|
"
|
||||||
|
toto
|
||||||
|
tata"
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
macro_rules! moo4 {
|
||||||
|
() => {
|
||||||
|
bar! {
|
||||||
|
"
|
||||||
|
foo
|
||||||
|
bar
|
||||||
|
baz"
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
43
tests/target/issue-3302.rs
Normal file
43
tests/target/issue-3302.rs
Normal file
@ -0,0 +1,43 @@
|
|||||||
|
// rustfmt-version: Two
|
||||||
|
|
||||||
|
macro_rules! moo1 {
|
||||||
|
() => {
|
||||||
|
bar! {
|
||||||
|
"
|
||||||
|
"
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
macro_rules! moo2 {
|
||||||
|
() => {
|
||||||
|
bar! {
|
||||||
|
"
|
||||||
|
"
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
macro_rules! moo3 {
|
||||||
|
() => {
|
||||||
|
42
|
||||||
|
/*
|
||||||
|
bar! {
|
||||||
|
"
|
||||||
|
toto
|
||||||
|
tata"
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
macro_rules! moo4 {
|
||||||
|
() => {
|
||||||
|
bar! {
|
||||||
|
"
|
||||||
|
foo
|
||||||
|
bar
|
||||||
|
baz"
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user