]> git.lizzy.rs Git - rust.git/commitdiff
Add a more lightweight method for rewriting comments when we are not normalising
authorNick Cameron <ncameron@mozilla.com>
Mon, 16 Jan 2017 04:58:51 +0000 (17:58 +1300)
committerNick Cameron <ncameron@mozilla.com>
Mon, 16 Jan 2017 21:17:48 +0000 (10:17 +1300)
Fixes #652

12 files changed:
src/comment.rs
tests/config/issue-1124.toml
tests/source/doc.rs
tests/source/imports-reorder.rs
tests/source/issue-1177.rs
tests/source/paths.rs
tests/source/pattern.rs
tests/target/doc.rs
tests/target/imports-reorder.rs
tests/target/issue-1177.rs
tests/target/paths.rs
tests/target/pattern.rs

index f831a039b955d665ead6d0d35bc6f9eaa70efb4e..8eaf2b2fb4fd9a83766746913283a230e412d7f8 100644 (file)
@@ -38,9 +38,21 @@ pub fn rewrite_comment(orig: &str,
                        offset: Indent,
                        config: &Config)
                        -> Option<String> {
                        offset: Indent,
                        config: &Config)
                        -> Option<String> {
-    let s = orig.trim();
+    // If there are lines without a starting sigil, we won't format them correctly
+    // so in that case we won't even re-align (if !config.normalize_comments) and
+    // we should stop now.
+    let num_bare_lines = orig.lines()
+        .map(|line| line.trim())
+        .filter(|l| !(l.starts_with('*') || l.starts_with("//") || l.starts_with("/*")))
+        .count();
+    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, offset, config);
+    }
 
 
-    // Edge case: block comments. Let's not trim their lines (for now).
     let (opener, closer, line_start) =
         if block_style {
             ("/* ", " */", " * ")
     let (opener, closer, line_start) =
         if block_style {
             ("/* ", " */", " * ")
@@ -74,7 +86,7 @@ pub fn rewrite_comment(orig: &str,
         };
 
     let max_chars = width.checked_sub(closer.len() + opener.len()).unwrap_or(1);
         };
 
     let max_chars = width.checked_sub(closer.len() + opener.len()).unwrap_or(1);
-
+    let indent_str = offset.to_string(config);
     let fmt = StringFormat {
         opener: "",
         closer: "",
     let fmt = StringFormat {
         opener: "",
         closer: "",
@@ -86,29 +98,17 @@ pub fn rewrite_comment(orig: &str,
         config: config,
     };
 
         config: config,
     };
 
-    let indent_str = offset.to_string(config);
-    let line_breaks = s.chars().filter(|&c| c == '\n').count();
-
-    let num_bare_lines = s.lines()
-        .enumerate()
-        .map(|(_, line)| line.trim())
-        .filter(|l| !(l.starts_with('*') || l.starts_with("//") || l.starts_with("/*")))
-        .count();
-
-    if num_bare_lines > 0 && !config.normalize_comments {
-        return Some(orig.to_owned());
-    }
-
-    let lines = s.lines()
+    let line_breaks = orig.trim_right().chars().filter(|&c| c == '\n').count();
+    let lines = orig.lines()
         .enumerate()
         .map(|(i, mut line)| {
             line = line.trim();
             // Drop old closer.
             if i == line_breaks && line.ends_with("*/") && !line.starts_with("//") {
         .enumerate()
         .map(|(i, mut line)| {
             line = line.trim();
             // Drop old closer.
             if i == line_breaks && line.ends_with("*/") && !line.starts_with("//") {
-                line = &line[..(line.len() - 2)];
+                line = &line[..(line.len() - 2)].trim_right();
             }
 
             }
 
-            line.trim_right()
+            line
         })
         .map(left_trim_comment_line)
         .map(|line| if orig.starts_with("/*") && line_breaks == 0 {
         })
         .map(left_trim_comment_line)
         .map(|line| if orig.starts_with("/*") && line_breaks == 0 {
@@ -150,6 +150,31 @@ pub fn rewrite_comment(orig: &str,
     Some(result)
 }
 
     Some(result)
 }
 
+/// Trims whitespace and aligns to indent, but otherwise does not change comments.
+fn light_rewrite_comment(orig: &str, offset: Indent, config: &Config) -> Option<String> {
+    let lines: Vec<&str> = orig.lines()
+        .map(|l| {
+            // This is basically just l.trim(), but in the case that a line starts
+            // with `*` we want to leave one space before it, so it aligns with the
+            // `*` in `/*`.
+            let first_non_whitespace = l.find(|c| !char::is_whitespace(c));
+            if let Some(fnw) = first_non_whitespace {
+                    if l.as_bytes()[fnw] == '*' as u8 && fnw > 0 {
+                        &l[fnw - 1..]
+                    } else {
+                        &l[fnw..]
+                    }
+                } else {
+                    ""
+                }
+                .trim_right()
+        })
+        .collect();
+    Some(lines.join(&format!("\n{}", offset.to_string(config))))
+}
+
+/// Trims comment characters and possibly a single space from the left of a string.
+/// Does not trim all whitespace.
 fn left_trim_comment_line(line: &str) -> &str {
     if line.starts_with("//! ") || line.starts_with("/// ") || line.starts_with("/*! ") ||
        line.starts_with("/** ") {
 fn left_trim_comment_line(line: &str) -> &str {
     if line.starts_with("//! ") || line.starts_with("/// ") || line.starts_with("/*! ") ||
        line.starts_with("/** ") {
@@ -708,6 +733,7 @@ fn comment_code_slices_three() {
     fn format_comments() {
         let mut config: ::config::Config = Default::default();
         config.wrap_comments = true;
     fn format_comments() {
         let mut config: ::config::Config = Default::default();
         config.wrap_comments = true;
+        config.normalize_comments = true;
 
         let comment = rewrite_comment(" //test", true, 100, Indent::new(0, 100), &config).unwrap();
         assert_eq!("/* test */", comment);
 
         let comment = rewrite_comment(" //test", true, 100, Indent::new(0, 100), &config).unwrap();
         assert_eq!("/* test */", comment);
index 44148a2d3c3ed6bc75e28e1c55eec63f4d720698..ca31820723542465dd434eb8388767c606991735 100644 (file)
@@ -1 +1,2 @@
 reorder_imports = true
 reorder_imports = true
+normalize_comments = true
index f325337c75871cda39b075aa8d5eda7b7a22d06d..e03933e4516109a1553fe6abce81b431f64367ef 100644 (file)
@@ -1,3 +1,4 @@
+// rustfmt-normalize_comments: true
 
 // sadfsdfa
 //sdffsdfasdf
 
 // sadfsdfa
 //sdffsdfasdf
index 4ad9e4b08d31b6b2ab173ae790017ea0670b733c..200cad2d13cae967790b2ef2fb3a19681440c307 100644 (file)
@@ -1,3 +1,4 @@
+// rustfmt-normalize_comments: true
 // rustfmt-reorder_imported_names: true
 
 use path::{C,/*A*/ A, B /* B */, self /* self */};
 // rustfmt-reorder_imported_names: true
 
 use path::{C,/*A*/ A, B /* B */, self /* self */};
index 053c73267fcc3aa00af06410edd2e8a323a85f7c..3ac423c5aef9b8c6b9e0e6ebe077a162ff12b01e 100644 (file)
@@ -1,3 +1,4 @@
+// rustfmt-normalize_comments: true
 fn main() {
     // Line Comment
     /* Block Comment */
 fn main() {
     // Line Comment
     /* Block Comment */
index af61ba89b1cb613a6406171dc46719b09d3d88fb..ebc26f146e4ab84739ad40c4acc8f37b61c675fe 100644 (file)
@@ -1,3 +1,4 @@
+// rustfmt-normalize_comments: true
 
 fn main() {
    let constellation_chan = Constellation::<layout::layout_task::LayoutTask,  script::script_task::ScriptTask> ::start(
 
 fn main() {
    let constellation_chan = Constellation::<layout::layout_task::LayoutTask,  script::script_task::ScriptTask> ::start(
index 75f65806167b5aee82b2be66b129bae93b6c49a0..a6c25225db2e7d86d6a726caa3a21d7fd108e3e5 100644 (file)
@@ -1,3 +1,4 @@
+// rustfmt-normalize_comments: true
 fn main() {
     let z = match x {
         "pat1" => 1,
 fn main() {
     let z = match x {
         "pat1" => 1,
index 9e883c50afa19b19710e95cf367d995a24718554..99d2ae7873f4fbe16723e8aab55e34bb4a26d687 100644 (file)
@@ -1,3 +1,4 @@
+// rustfmt-normalize_comments: true
 
 // sadfsdfa
 // sdffsdfasdf
 
 // sadfsdfa
 // sdffsdfasdf
index 32b5ee156cc5fa32691be600a10360cd62d1a9d3..fbdef3630e8b2426b0d3d893a724d1c5a75f30fa 100644 (file)
@@ -1,3 +1,4 @@
+// rustfmt-normalize_comments: true
 // rustfmt-reorder_imported_names: true
 
 use path::{self /* self */, /* A */ A, B /* B */, C};
 // rustfmt-reorder_imported_names: true
 
 use path::{self /* self */, /* A */ A, B /* B */, C};
index 377540e1bfeb6934dfd2a0b208acadaa0946971e..dcda397281439fbab52ec2d3041a0a84649ec227 100644 (file)
@@ -1,3 +1,4 @@
+// rustfmt-normalize_comments: true
 fn main() {
     // Line Comment
     // Block Comment
 fn main() {
     // Line Comment
     // Block Comment
index adae879e647cb957d3927cc2749fd62ed80efb71..6535463ebe3dd1fa4f152769d79eb9f34acc668e 100644 (file)
@@ -1,3 +1,4 @@
+// rustfmt-normalize_comments: true
 
 fn main() {
     let constellation_chan =
 
 fn main() {
     let constellation_chan =
index d77cb59f4984738b7d8b1b4147424b8e9582a308..b809253aa855518a1ca395b6c84560e77d6b5501 100644 (file)
@@ -1,3 +1,4 @@
+// rustfmt-normalize_comments: true
 fn main() {
     let z = match x {
         "pat1" => 1,
 fn main() {
     let z = match x {
         "pat1" => 1,