From 6b4ef7457a54103817e8afb3dba3828b60b1e286 Mon Sep 17 00:00:00 2001 From: Tim Kuehn Date: Tue, 20 Oct 2015 02:12:52 -0700 Subject: [PATCH] Fix doc comment regression and add tests. --- src/comment.rs | 11 +++++++++-- tests/source/comment2.rs | 2 ++ tests/source/comment3.rs | 3 +++ tests/target/comment2.rs | 4 ++++ tests/target/comment3.rs | 5 +++++ 5 files changed, 23 insertions(+), 2 deletions(-) create mode 100644 tests/source/comment2.rs create mode 100644 tests/source/comment3.rs create mode 100644 tests/target/comment2.rs create mode 100644 tests/target/comment3.rs diff --git a/src/comment.rs b/src/comment.rs index e4ce5a7ed35..045d1cb5047 100644 --- a/src/comment.rs +++ b/src/comment.rs @@ -27,6 +27,10 @@ pub fn rewrite_comment(orig: &str, // Edge case: block comments. Let's not trim their lines (for now). let (opener, closer, line_start) = if block_style { ("/* ", " */", " * ") + } else if orig.starts_with("///") { + ("/// ", "", "/// ") + } else if orig.starts_with("//!") { + ("//! ", "", "//! ") } else { ("// ", "", "// ") }; @@ -81,7 +85,7 @@ pub fn rewrite_comment(orig: &str, let rewrite = try_opt!(rewrite_string(line, &fmt)); result.push_str(&rewrite); } else { - if line.len() == 0 || line.starts_with('!') { + if line.len() == 0 { // Remove space if this is an empty comment or a doc comment. result.pop(); } @@ -97,7 +101,10 @@ pub fn rewrite_comment(orig: &str, } fn left_trim_comment_line(line: &str) -> &str { - if line.starts_with("/* ") || line.starts_with("// ") { + if line.starts_with("//! ") || line.starts_with("/// ") { + &line[4..] + } else if line.starts_with("/* ") || line.starts_with("// ") || line.starts_with("//!") || + line.starts_with("///") { &line[3..] } else if line.starts_with("/*") || line.starts_with("* ") || line.starts_with("//") { &line[2..] diff --git a/tests/source/comment2.rs b/tests/source/comment2.rs new file mode 100644 index 00000000000..81478621bd9 --- /dev/null +++ b/tests/source/comment2.rs @@ -0,0 +1,2 @@ +/// This is a long line that angers rustfmt. Rustfmt shall deal with it swiftly and justly. +pub mod foo {} diff --git a/tests/source/comment3.rs b/tests/source/comment3.rs new file mode 100644 index 00000000000..ed54605f727 --- /dev/null +++ b/tests/source/comment3.rs @@ -0,0 +1,3 @@ +//! This is a long line that angers rustfmt. Rustfmt shall deal with it swiftly and justly. + +pub mod foo {} diff --git a/tests/target/comment2.rs b/tests/target/comment2.rs new file mode 100644 index 00000000000..885964afee2 --- /dev/null +++ b/tests/target/comment2.rs @@ -0,0 +1,4 @@ +/// This is a long line that angers rustfmt. Rustfmt shall deal with it swiftly +/// and justly. +pub mod foo { +} diff --git a/tests/target/comment3.rs b/tests/target/comment3.rs new file mode 100644 index 00000000000..c9123d4e076 --- /dev/null +++ b/tests/target/comment3.rs @@ -0,0 +1,5 @@ +//! This is a long line that angers rustfmt. Rustfmt shall deal with it swiftly +//! and justly. + +pub mod foo { +}