From 72c04facd259a386a7a95c180674927ed2e72c22 Mon Sep 17 00:00:00 2001 From: topecongiro Date: Mon, 8 May 2017 06:44:48 +0900 Subject: [PATCH 1/2] Add tests for issues which can be closed on the current master This PR adds tests for #325, #1092, #1214, #1278, #1329 and #1427. --- tests/source/closure.rs | 12 ++++++++++++ tests/source/issue-1278.rs | 9 +++++++++ tests/source/macros.rs | 3 +++ tests/target/closure.rs | 14 ++++++++++++++ .../configs-fn_call_style-block-tab_spaces-2.rs | 14 ++++++++++++++ tests/target/issue-1214.rs | 8 ++++++++ tests/target/issue-1278.rs | 9 +++++++++ tests/target/macros.rs | 3 +++ 8 files changed, 72 insertions(+) create mode 100644 tests/source/issue-1278.rs create mode 100644 tests/target/configs-fn_call_style-block-tab_spaces-2.rs create mode 100644 tests/target/issue-1214.rs create mode 100644 tests/target/issue-1278.rs diff --git a/tests/source/closure.rs b/tests/source/closure.rs index a4395d0286a..fd3876720a8 100644 --- a/tests/source/closure.rs +++ b/tests/source/closure.rs @@ -131,3 +131,15 @@ impl Foo { }) } } + +fn issue1329() { + aaaaaaaaaaaaaaaa.map(|x| { + x += 1; + x + }) + .filter +} + +fn issue325() { + let f = || unsafe { xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx }; +} diff --git a/tests/source/issue-1278.rs b/tests/source/issue-1278.rs new file mode 100644 index 00000000000..1a458060bdd --- /dev/null +++ b/tests/source/issue-1278.rs @@ -0,0 +1,9 @@ +// rustfmt-fn_args_layout = "block" + +#![feature(pub_restricted)] + +mod inner_mode { + pub(super) fn func_name(abc: i32) -> i32 { + abc + } +} diff --git a/tests/source/macros.rs b/tests/source/macros.rs index 85ec41ddb0f..b5098fb9972 100644 --- a/tests/source/macros.rs +++ b/tests/source/macros.rs @@ -83,6 +83,9 @@ fn main() { let json = json!({ "foo": "bar", }); + + // #1092 + chain!(input, a:take!(max_size), || []); } impl X { diff --git a/tests/target/closure.rs b/tests/target/closure.rs index 143f59b5962..d51c76b9e48 100644 --- a/tests/target/closure.rs +++ b/tests/target/closure.rs @@ -150,3 +150,17 @@ impl Foo { }) } } + +fn issue1329() { + aaaaaaaaaaaaaaaa + .map(|x| { + x += 1; + x + }) + .filter +} + +fn issue325() { + let f = + || unsafe { xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx }; +} diff --git a/tests/target/configs-fn_call_style-block-tab_spaces-2.rs b/tests/target/configs-fn_call_style-block-tab_spaces-2.rs new file mode 100644 index 00000000000..482c5c0e82a --- /dev/null +++ b/tests/target/configs-fn_call_style-block-tab_spaces-2.rs @@ -0,0 +1,14 @@ +// rustfmt-fn_call_style: Block +// rustfmt-max_width: 80 +// rustfmt-tab_spaces: 2 + +// #1427 +fn main() { + exceptaions::config(move || { + ( + NmiConfig {}, + HardFaultConfig {}, + SysTickConfig { gpio_sbsrr }, + ) + }); +} diff --git a/tests/target/issue-1214.rs b/tests/target/issue-1214.rs new file mode 100644 index 00000000000..c622abb3a85 --- /dev/null +++ b/tests/target/issue-1214.rs @@ -0,0 +1,8 @@ +/*! +# Example + +``` + // Here goes some example +``` + */ +struct Item; diff --git a/tests/target/issue-1278.rs b/tests/target/issue-1278.rs new file mode 100644 index 00000000000..1a458060bdd --- /dev/null +++ b/tests/target/issue-1278.rs @@ -0,0 +1,9 @@ +// rustfmt-fn_args_layout = "block" + +#![feature(pub_restricted)] + +mod inner_mode { + pub(super) fn func_name(abc: i32) -> i32 { + abc + } +} diff --git a/tests/target/macros.rs b/tests/target/macros.rs index a1fa3f74368..5fd2051f5e6 100644 --- a/tests/target/macros.rs +++ b/tests/target/macros.rs @@ -88,6 +88,9 @@ fn main() { let json = json!({ "foo": "bar", }); + + // #1092 + chain!(input, a:take!(max_size), || []); } impl X { From bcebe9e7def08e987516fb59a46234acd7ef5d2c Mon Sep 17 00:00:00 2001 From: topecongiro Date: Sun, 28 May 2017 19:10:05 +0900 Subject: [PATCH 2/2] Use different style when rewriting comment with different opener --- src/comment.rs | 185 ++++++++++++++++++++++++++++++++------ tests/target/issue-691.rs | 9 ++ 2 files changed, 166 insertions(+), 28 deletions(-) create mode 100644 tests/target/issue-691.rs diff --git a/src/comment.rs b/src/comment.rs index 9591ffc4427..5b3ee179e2e 100644 --- a/src/comment.rs +++ b/src/comment.rs @@ -32,6 +32,122 @@ fn is_custom_comment(comment: &str) -> bool { } } +#[derive(PartialEq, Eq)] +pub enum CommentStyle { + DoubleSlash, + TripleSlash, + Doc, + SingleBullet, + DoubleBullet, + Exclamation, + Custom, +} + +impl CommentStyle { + pub fn opener<'a>(&self, orig: &'a str) -> &'a str { + match *self { + CommentStyle::DoubleSlash => "// ", + CommentStyle::TripleSlash => "/// ", + CommentStyle::Doc => "//! ", + CommentStyle::SingleBullet => "/* ", + CommentStyle::DoubleBullet => "/** ", + CommentStyle::Exclamation => "/*! ", + CommentStyle::Custom => { + if orig.chars().nth(3) == Some(' ') { + &orig[0..4] + } else { + &orig[0..3] + } + } + } + } + + pub fn closer<'a>(&self) -> &'a str { + match *self { + CommentStyle::DoubleSlash | + CommentStyle::TripleSlash | + CommentStyle::Custom | + CommentStyle::Doc => "", + CommentStyle::DoubleBullet => " **/", + CommentStyle::SingleBullet | + CommentStyle::Exclamation => " */", + } + } + + pub fn line_start<'a>(&self, orig: &'a str) -> &'a str { + match *self { + CommentStyle::DoubleSlash => "// ", + CommentStyle::TripleSlash => "/// ", + CommentStyle::Doc => "//! ", + CommentStyle::SingleBullet | + CommentStyle::Exclamation => " * ", + CommentStyle::DoubleBullet => " ** ", + CommentStyle::Custom => { + if orig.chars().nth(3) == Some(' ') { + &orig[0..4] + } else { + &orig[0..3] + } + } + } + } + + pub fn to_str_tuplet<'a>(&self, orig: &'a str) -> (&'a str, &'a str, &'a str) { + (self.opener(orig), self.closer(), self.line_start(orig)) + } + + pub fn line_with_same_comment_style<'a>(&self, + line: &str, + orig: &'a str, + normalize_comments: bool) + -> bool { + match *self { + CommentStyle::DoubleSlash | + CommentStyle::TripleSlash | + CommentStyle::Custom | + CommentStyle::Doc => { + line.trim_left() + .starts_with(self.line_start(orig).trim_left()) || + comment_style(line, normalize_comments) == *self + } + CommentStyle::DoubleBullet | + CommentStyle::SingleBullet | + CommentStyle::Exclamation => { + line.trim_left().starts_with(self.closer().trim_left()) || + line.trim_left() + .starts_with(self.line_start(orig).trim_left()) || + comment_style(line, normalize_comments) == *self + } + } + } +} + +fn comment_style(orig: &str, normalize_comments: bool) -> CommentStyle { + if !normalize_comments { + if orig.starts_with("/**") && !orig.starts_with("/**/") { + CommentStyle::DoubleBullet + } else if orig.starts_with("/*!") { + CommentStyle::Exclamation + } else if orig.starts_with("/*") { + CommentStyle::SingleBullet + } else if orig.starts_with("///") { + CommentStyle::TripleSlash + } else if orig.starts_with("//!") { + CommentStyle::Doc + } else { + CommentStyle::DoubleSlash + } + } else if orig.starts_with("///") || (orig.starts_with("/**") && !orig.starts_with("/**/")) { + CommentStyle::TripleSlash + } else if orig.starts_with("//!") || orig.starts_with("/*!") { + CommentStyle::Doc + } else if is_custom_comment(orig) { + CommentStyle::Custom + } else { + CommentStyle::DoubleSlash + } +} + pub fn rewrite_comment(orig: &str, block_style: bool, shape: Shape, @@ -47,39 +163,52 @@ pub fn rewrite_comment(orig: &str, if num_bare_lines > 0 && !config.normalize_comments() { return Some(orig.to_owned()); } - if !config.normalize_comments() && !config.wrap_comments() { return light_rewrite_comment(orig, shape.indent, config); } - let (opener, closer, line_start) = if block_style { - ("/* ", " */", " * ") - } else if !config.normalize_comments() { - if orig.starts_with("/**") && !orig.starts_with("/**/") { - ("/** ", " **/", " ** ") - } else if orig.starts_with("/*!") { - ("/*! ", " */", " * ") - } else if orig.starts_with("/*") { - ("/* ", " */", " * ") - } else if orig.starts_with("///") { - ("/// ", "", "/// ") - } else if orig.starts_with("//!") { - ("//! ", "", "//! ") - } else { - ("// ", "", "// ") - } - } else if orig.starts_with("///") || (orig.starts_with("/**") && !orig.starts_with("/**/")) { - ("/// ", "", "/// ") - } else if orig.starts_with("//!") || orig.starts_with("/*!") { - ("//! ", "", "//! ") - } else if is_custom_comment(orig) { - if orig.chars().nth(3) == Some(' ') { - (&orig[0..4], "", &orig[0..4]) - } else { - (&orig[0..3], "", &orig[0..3]) - } + identify_comment(orig, block_style, shape, config) +} + +fn identify_comment(orig: &str, + block_style: bool, + shape: Shape, + config: &Config) + -> Option { + let style = comment_style(orig, false); + let first_group = orig.lines() + .take_while(|l| style.line_with_same_comment_style(l, orig, false)) + .collect::>() + .join("\n"); + let rest = orig.lines() + .skip(first_group.lines().count()) + .collect::>() + .join("\n"); + + let first_group_str = try_opt!(rewrite_comment_inner(&first_group, block_style, shape, config)); + if rest.is_empty() { + Some(first_group_str) } else { - ("// ", "", "// ") + identify_comment(&rest, block_style, shape, config).map(|rest_str| { + format!("{}\n{}{}", + first_group_str, + shape + .indent + .to_string(config), + rest_str) + }) + } +} + +fn rewrite_comment_inner(orig: &str, + block_style: bool, + shape: Shape, + config: &Config) + -> Option { + let (opener, closer, line_start) = if block_style { + CommentStyle::SingleBullet.to_str_tuplet("") + } else { + comment_style(orig, config.normalize_comments()).to_str_tuplet(orig) }; let max_chars = shape diff --git a/tests/target/issue-691.rs b/tests/target/issue-691.rs new file mode 100644 index 00000000000..7473d070eff --- /dev/null +++ b/tests/target/issue-691.rs @@ -0,0 +1,9 @@ +// rustfmt-normalize_comments: true + +//! `std` or `core` and simply link to this library. In case the target +//! platform has no hardware +//! support for some operation, software implementations provided by this +//! library will be used automagically. +// TODO: provide instructions to override default libm link and how to link to +// this library. +fn foo() {}