Skip to content

Commit

Permalink
Merge pull request rust-lang#3326 from scampi/issue-3302
Browse files Browse the repository at this point in the history
fix formatting of strings within a macro
  • Loading branch information
topecongiro committed Feb 10, 2019
2 parents e28fae9 + b86dd16 commit c4611a0
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 7 deletions.
5 changes: 3 additions & 2 deletions src/lib.rs
Expand Up @@ -50,6 +50,7 @@ use crate::comment::LineClasses;
use crate::formatting::{FormatErrorMap, FormattingError, ReportedErrors, SourceFile};
use crate::issues::Issue;
use crate::shape::Indent;
use crate::utils::indent_next_line;

pub use crate::config::{
load_config, CliOptions, Color, Config, Edition, EmitMode, FileLines, FileName, NewlineStyle,
Expand Down Expand Up @@ -438,7 +439,7 @@ fn format_code_block(code_snippet: &str, config: &Config) -> Option<FormattedSni
}
result.push_str(&line);
result.push('\n');
need_indent = !kind.is_string() || line.ends_with('\\');
need_indent = indent_next_line(kind, &line, config);
}
result.push('}');
result
Expand Down Expand Up @@ -499,7 +500,7 @@ fn format_code_block(code_snippet: &str, config: &Config) -> Option<FormattedSni
line
};
result.push_str(trimmed_line);
is_indented = !kind.is_string() || line.ends_with('\\');
is_indented = indent_next_line(kind, line, config);
}
Some(FormattedSnippet {
snippet: result,
Expand Down
6 changes: 3 additions & 3 deletions src/macros.rs
Expand Up @@ -43,8 +43,8 @@ use crate::shape::{Indent, Shape};
use crate::source_map::SpanUtils;
use crate::spanned::Spanned;
use crate::utils::{
format_visibility, is_empty_line, mk_sp, remove_trailing_white_spaces, rewrite_ident,
trim_left_preserve_layout, wrap_str, NodeIdExt,
format_visibility, indent_next_line, is_empty_line, mk_sp, remove_trailing_white_spaces,
rewrite_ident, trim_left_preserve_layout, wrap_str, NodeIdExt,
};
use crate::visitor::FmtVisitor;

Expand Down Expand Up @@ -1299,7 +1299,7 @@ impl MacroBranch {
{
s += &indent_str;
}
(s + l + "\n", !kind.is_string() || l.ends_with('\\'))
(s + l + "\n", indent_next_line(kind, &l, &config))
},
)
.0;
Expand Down
13 changes: 11 additions & 2 deletions src/utils.rs
Expand Up @@ -526,8 +526,10 @@ pub fn trim_left_preserve_layout(orig: &str, indent: Indent, config: &Config) ->
Some(get_prefix_space_width(config, &line))
};

let new_veto_trim_value = (kind.is_string()
|| (config.version() == Version::Two && kind.is_commented_string()))
// just InString{Commented} in order to allow the start of a string to be indented
let new_veto_trim_value = (kind == FullCodeCharKind::InString
|| (config.version() == Version::Two
&& kind == FullCodeCharKind::InStringCommented))
&& !line.ends_with('\\');
let line = if veto_trim || new_veto_trim_value {
veto_trim = new_veto_trim_value;
Expand Down Expand Up @@ -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 {
s.is_empty() || s.chars().all(char::is_whitespace)
}
Expand Down
43 changes: 43 additions & 0 deletions tests/source/issue-3302.rs
@@ -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 changes: 43 additions & 0 deletions tests/target/issue-3302.rs
@@ -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"
}
};
}

0 comments on commit c4611a0

Please sign in to comment.