From: Nick Cameron Date: Mon, 16 Jan 2017 04:58:51 +0000 (+1300) Subject: Add a more lightweight method for rewriting comments when we are not normalising X-Git-Url: https://git.lizzy.rs/?a=commitdiff_plain;ds=sidebyside;h=0218a41d73f3fa0be4f2952c4a903f91072823c4;p=rust.git Add a more lightweight method for rewriting comments when we are not normalising Fixes #652 --- diff --git a/src/comment.rs b/src/comment.rs index f831a039b95..8eaf2b2fb4f 100644 --- a/src/comment.rs +++ b/src/comment.rs @@ -38,9 +38,21 @@ pub fn rewrite_comment(orig: &str, offset: Indent, config: &Config) -> Option { - 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 { ("/* ", " */", " * ") @@ -74,7 +86,7 @@ pub fn rewrite_comment(orig: &str, }; 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: "", @@ -86,29 +98,17 @@ pub fn rewrite_comment(orig: &str, 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("//") { - 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 { @@ -150,6 +150,31 @@ pub fn rewrite_comment(orig: &str, 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 { + 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("/** ") { @@ -708,6 +733,7 @@ fn comment_code_slices_three() { 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); diff --git a/tests/config/issue-1124.toml b/tests/config/issue-1124.toml index 44148a2d3c3..ca318207235 100644 --- a/tests/config/issue-1124.toml +++ b/tests/config/issue-1124.toml @@ -1 +1,2 @@ reorder_imports = true +normalize_comments = true diff --git a/tests/source/doc.rs b/tests/source/doc.rs index f325337c758..e03933e4516 100644 --- a/tests/source/doc.rs +++ b/tests/source/doc.rs @@ -1,3 +1,4 @@ +// rustfmt-normalize_comments: true // sadfsdfa //sdffsdfasdf diff --git a/tests/source/imports-reorder.rs b/tests/source/imports-reorder.rs index 4ad9e4b08d3..200cad2d13c 100644 --- a/tests/source/imports-reorder.rs +++ b/tests/source/imports-reorder.rs @@ -1,3 +1,4 @@ +// rustfmt-normalize_comments: true // rustfmt-reorder_imported_names: true use path::{C,/*A*/ A, B /* B */, self /* self */}; diff --git a/tests/source/issue-1177.rs b/tests/source/issue-1177.rs index 053c73267fc..3ac423c5aef 100644 --- a/tests/source/issue-1177.rs +++ b/tests/source/issue-1177.rs @@ -1,3 +1,4 @@ +// rustfmt-normalize_comments: true fn main() { // Line Comment /* Block Comment */ diff --git a/tests/source/paths.rs b/tests/source/paths.rs index af61ba89b1c..ebc26f146e4 100644 --- a/tests/source/paths.rs +++ b/tests/source/paths.rs @@ -1,3 +1,4 @@ +// rustfmt-normalize_comments: true fn main() { let constellation_chan = Constellation:: ::start( diff --git a/tests/source/pattern.rs b/tests/source/pattern.rs index 75f65806167..a6c25225db2 100644 --- a/tests/source/pattern.rs +++ b/tests/source/pattern.rs @@ -1,3 +1,4 @@ +// rustfmt-normalize_comments: true fn main() { let z = match x { "pat1" => 1, diff --git a/tests/target/doc.rs b/tests/target/doc.rs index 9e883c50afa..99d2ae7873f 100644 --- a/tests/target/doc.rs +++ b/tests/target/doc.rs @@ -1,3 +1,4 @@ +// rustfmt-normalize_comments: true // sadfsdfa // sdffsdfasdf diff --git a/tests/target/imports-reorder.rs b/tests/target/imports-reorder.rs index 32b5ee156cc..fbdef3630e8 100644 --- a/tests/target/imports-reorder.rs +++ b/tests/target/imports-reorder.rs @@ -1,3 +1,4 @@ +// rustfmt-normalize_comments: true // rustfmt-reorder_imported_names: true use path::{self /* self */, /* A */ A, B /* B */, C}; diff --git a/tests/target/issue-1177.rs b/tests/target/issue-1177.rs index 377540e1bfe..dcda3972814 100644 --- a/tests/target/issue-1177.rs +++ b/tests/target/issue-1177.rs @@ -1,3 +1,4 @@ +// rustfmt-normalize_comments: true fn main() { // Line Comment // Block Comment diff --git a/tests/target/paths.rs b/tests/target/paths.rs index adae879e647..6535463ebe3 100644 --- a/tests/target/paths.rs +++ b/tests/target/paths.rs @@ -1,3 +1,4 @@ +// rustfmt-normalize_comments: true fn main() { let constellation_chan = diff --git a/tests/target/pattern.rs b/tests/target/pattern.rs index d77cb59f498..b809253aa85 100644 --- a/tests/target/pattern.rs +++ b/tests/target/pattern.rs @@ -1,3 +1,4 @@ +// rustfmt-normalize_comments: true fn main() { let z = match x { "pat1" => 1,