version-gate the formatting of commented strings

This commit is contained in:
Stéphane Campinas 2019-01-16 23:06:28 +01:00
parent baa62c609e
commit 083a20fb1a
No known key found for this signature in database
GPG Key ID: 6D5620D908210133
7 changed files with 44 additions and 8 deletions

View File

@ -22,7 +22,7 @@ use syntax::source_map::{BytePos, Span, NO_EXPANSION};
use syntax_pos::Mark; use syntax_pos::Mark;
use comment::{filter_normal_code, CharClasses, FullCodeCharKind, LineClasses}; use comment::{filter_normal_code, CharClasses, FullCodeCharKind, LineClasses};
use config::Config; use config::{Config, Version};
use rewrite::RewriteContext; use rewrite::RewriteContext;
use shape::{Indent, Shape}; use shape::{Indent, Shape};
@ -527,8 +527,9 @@ 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 = let new_veto_trim_value = (kind.is_string()
(kind.is_string() || kind.is_commented_string()) && !line.ends_with('\\'); || (config.version() == Version::Two && kind.is_commented_string()))
&& !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;
trimmed = false; trimmed = false;
@ -541,10 +542,12 @@ pub fn trim_left_preserve_layout(orig: &str, indent: Indent, config: &Config) ->
// Because there is a veto against trimming and indenting lines within a string, // Because there is a veto against trimming and indenting lines within a string,
// such lines should not be taken into account when computing the minimum. // such lines should not be taken into account when computing the minimum.
match kind { match kind {
FullCodeCharKind::InString FullCodeCharKind::InStringCommented | FullCodeCharKind::EndStringCommented
| FullCodeCharKind::EndString if config.version() == Version::Two =>
| FullCodeCharKind::InStringCommented {
| FullCodeCharKind::EndStringCommented => None, None
}
FullCodeCharKind::InString | FullCodeCharKind::EndString => None,
_ => prefix_space_width, _ => prefix_space_width,
} }
}) })

View File

@ -1,3 +1,5 @@
// rustfmt-version: One
pub fn main() { pub fn main() {
/* let s = String::from( /* let s = String::from(
" "

View File

@ -1,3 +1,5 @@
// rustfmt-version: Two
pub fn main() { pub fn main() {
/* let s = String::from( /* let s = String::from(
" "

View File

@ -1,3 +1,5 @@
// rustfmt-version: Two
fn test() { fn test() {
/* /*
a a

View File

@ -0,0 +1,12 @@
// rustfmt-version: One
pub fn main() {
/* let s = String::from(
"
hello
world
",
); */
assert_eq!(s, "\nhello\nworld\n");
}

View File

@ -0,0 +1,12 @@
// rustfmt-version: Two
pub fn main() {
/* let s = String::from(
"
hello
world
",
); */
assert_eq!(s, "\nhello\nworld\n");
}

View File

@ -1,9 +1,12 @@
// rustfmt-wrap_comments: true // rustfmt-wrap_comments: true
// rustfmt-version: Two
// check that a line below max_width does not get over the limit when wrapping
// it in a block comment
fn func() { fn func() {
let x = 42; let x = 42;
/* /*
let something = "one line line line line line line line line line line line line let something = "one line line line line line line line line line line line line line
two lines two lines
three lines"; three lines";
*/ */