]> git.lizzy.rs Git - rust.git/commitdiff
Preserve trailing spaces in doc comments even when options are set
authorSeiichi Uchida <seuchida@gmail.com>
Fri, 16 Feb 2018 01:14:06 +0000 (10:14 +0900)
committerSeiichi Uchida <seuchida@gmail.com>
Fri, 16 Feb 2018 01:14:06 +0000 (10:14 +0900)
Closes #37.

rustfmt-core/src/comment.rs
rustfmt-core/tests/source/markdown-comment-with-options.rs [new file with mode: 0644]
rustfmt-core/tests/target/markdown-comment-with-options.rs [new file with mode: 0644]

index dc0c59a617e9dd8be0792b173c9720f9655370ca..512863c88bec49e2668c1aff81d6c053e8832999 100644 (file)
@@ -50,6 +50,13 @@ fn custom_opener(s: &str) -> &str {
 }
 
 impl<'a> CommentStyle<'a> {
+    pub fn is_doc_comment(&self) -> bool {
+        match *self {
+            CommentStyle::TripleSlash | CommentStyle::Doc => true,
+            _ => false,
+        }
+    }
+
     pub fn opener(&self) -> &'a str {
         match *self {
             CommentStyle::DoubleSlash => "// ",
@@ -248,7 +255,7 @@ fn _rewrite_comment(
         return light_rewrite_comment(orig, shape.indent, config, is_doc_comment);
     }
 
-    identify_comment(orig, block_style, shape, config)
+    identify_comment(orig, block_style, shape, config, is_doc_comment)
 }
 
 fn identify_comment(
@@ -256,6 +263,7 @@ fn identify_comment(
     block_style: bool,
     shape: Shape,
     config: &Config,
+    is_doc_comment: bool,
 ) -> Option<String> {
     let style = comment_style(orig, false);
     let first_group = orig.lines()
@@ -267,11 +275,18 @@ fn identify_comment(
         .collect::<Vec<_>>()
         .join("\n");
 
-    let first_group_str = rewrite_comment_inner(&first_group, block_style, style, shape, config)?;
+    let first_group_str = rewrite_comment_inner(
+        &first_group,
+        block_style,
+        style,
+        shape,
+        config,
+        is_doc_comment || style.is_doc_comment(),
+    )?;
     if rest.is_empty() {
         Some(first_group_str)
     } else {
-        identify_comment(&rest, block_style, shape, config).map(|rest_str| {
+        identify_comment(&rest, block_style, shape, config, is_doc_comment).map(|rest_str| {
             format!(
                 "{}\n{}{}",
                 first_group_str,
@@ -288,6 +303,7 @@ fn rewrite_comment_inner(
     style: CommentStyle,
     shape: Shape,
     config: &Config,
+    is_doc_comment: bool,
 ) -> Option<String> {
     let (opener, closer, line_start) = if block_style {
         CommentStyle::SingleBullet.to_str_tuplet()
@@ -315,7 +331,7 @@ fn rewrite_comment_inner(
     let lines = orig.lines()
         .enumerate()
         .map(|(i, mut line)| {
-            line = line.trim();
+            line = trim_right_unless_two_whitespaces(line.trim_left(), is_doc_comment);
             // Drop old closer.
             if i == line_breaks && line.ends_with("*/") && !line.starts_with("//") {
                 line = line[..(line.len() - 2)].trim_right();
diff --git a/rustfmt-core/tests/source/markdown-comment-with-options.rs b/rustfmt-core/tests/source/markdown-comment-with-options.rs
new file mode 100644 (file)
index 0000000..2c4d6a5
--- /dev/null
@@ -0,0 +1,17 @@
+// rustfmt-wrap_comments: true
+
+// Preserve two trailing whitespaces in doc comment,
+// but trim any whitespaces in normal comment.
+
+//! hello world  
+//! hello world 
+
+/// hello world    
+/// hello world 
+/// hello world  
+fn foo() {
+    // hello world  
+    // hello world 
+    let x = 3;
+    println!("x = {}", x);
+}
diff --git a/rustfmt-core/tests/target/markdown-comment-with-options.rs b/rustfmt-core/tests/target/markdown-comment-with-options.rs
new file mode 100644 (file)
index 0000000..ede2bc0
--- /dev/null
@@ -0,0 +1,17 @@
+// rustfmt-wrap_comments: true
+
+// Preserve two trailing whitespaces in doc comment,
+// but trim any whitespaces in normal comment.
+
+//! hello world  
+//! hello world
+
+/// hello world    
+/// hello world
+/// hello world  
+fn foo() {
+    // hello world
+    // hello world
+    let x = 3;
+    println!("x = {}", x);
+}