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_pos::Mark;
use comment::{filter_normal_code, CharClasses, FullCodeCharKind, LineClasses};
use config::Config;
use config::{Config, Version};
use rewrite::RewriteContext;
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))
};
let new_veto_trim_value =
(kind.is_string() || kind.is_commented_string()) && !line.ends_with('\\');
let new_veto_trim_value = (kind.is_string()
|| (config.version() == Version::Two && kind.is_commented_string()))
&& !line.ends_with('\\');
let line = if veto_trim || new_veto_trim_value {
veto_trim = new_veto_trim_value;
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,
// such lines should not be taken into account when computing the minimum.
match kind {
FullCodeCharKind::InString
| FullCodeCharKind::EndString
| FullCodeCharKind::InStringCommented
| FullCodeCharKind::EndStringCommented => None,
FullCodeCharKind::InStringCommented | FullCodeCharKind::EndStringCommented
if config.version() == Version::Two =>
{
None
}
FullCodeCharKind::InString | FullCodeCharKind::EndString => None,
_ => prefix_space_width,
}
})

View File

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

View File

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

View File

@ -1,3 +1,5 @@
// rustfmt-version: Two
fn test() {
/*
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-version: Two
// check that a line below max_width does not get over the limit when wrapping
// it in a block comment
fn func() {
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
three lines";
*/