From fe30dab01785e047b98d89cb27aed84b8137adb2 Mon Sep 17 00:00:00 2001 From: Seiichi Uchida Date: Tue, 13 Feb 2018 18:27:54 +0900 Subject: [PATCH 1/3] Preserve trailing two whitespace in comments Closes #2434. --- rustfmt-core/src/comment.rs | 15 +++++++++++++-- rustfmt-core/tests/source/markdown-comment.rs | 11 +++++++++++ rustfmt-core/tests/target/markdown-comment.rs | 11 +++++++++++ 3 files changed, 35 insertions(+), 2 deletions(-) create mode 100644 rustfmt-core/tests/source/markdown-comment.rs create mode 100644 rustfmt-core/tests/target/markdown-comment.rs diff --git a/rustfmt-core/src/comment.rs b/rustfmt-core/src/comment.rs index 9c0322bcaeb1..ccb69442ff7d 100644 --- a/rustfmt-core/src/comment.rs +++ b/rustfmt-core/src/comment.rs @@ -494,6 +494,15 @@ pub fn recover_missing_comment_in_span( } } +/// Trim trailing whitespaces unless they consist of two whitespaces. +fn trim_right_unless_two_whitespaces(s: &str) -> &str { + if s.ends_with(" ") && !s.chars().rev().nth(2).map_or(true, char::is_whitespace) { + s + } else { + s.trim_right() + } +} + /// 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() @@ -502,7 +511,7 @@ fn light_rewrite_comment(orig: &str, offset: Indent, config: &Config) -> Option< // 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 { + let left_trimmed = if let Some(fnw) = first_non_whitespace { if l.as_bytes()[fnw] == b'*' && fnw > 0 { &l[fnw - 1..] } else { @@ -510,7 +519,9 @@ fn light_rewrite_comment(orig: &str, offset: Indent, config: &Config) -> Option< } } else { "" - }.trim_right() + }; + // Preserve markdown's double-space line break syntax. + trim_right_unless_two_whitespaces(left_trimmed) }) .collect(); Some(lines.join(&format!("\n{}", offset.to_string(config)))) diff --git a/rustfmt-core/tests/source/markdown-comment.rs b/rustfmt-core/tests/source/markdown-comment.rs new file mode 100644 index 000000000000..265f97faa0f7 --- /dev/null +++ b/rustfmt-core/tests/source/markdown-comment.rs @@ -0,0 +1,11 @@ +//! 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.rs b/rustfmt-core/tests/target/markdown-comment.rs new file mode 100644 index 000000000000..20dc7260d2f3 --- /dev/null +++ b/rustfmt-core/tests/target/markdown-comment.rs @@ -0,0 +1,11 @@ +//! hello world +//! hello world + +/// hello world +/// hello world +fn foo() { + // hello world + // hello world + let x = 3; + println!("x = {}", x); +} From 296018afe7df972ca1d2f607064b528ce9a0db51 Mon Sep 17 00:00:00 2001 From: topecongiro Date: Thu, 15 Feb 2018 16:34:22 +0900 Subject: [PATCH 2/3] Preserve trailing whitespaces only for doc comment --- rustfmt-core/src/comment.rs | 31 ++++++++++++++++--- rustfmt-core/src/visitor.rs | 6 ++-- rustfmt-core/tests/source/markdown-comment.rs | 4 +++ rustfmt-core/tests/target/markdown-comment.rs | 6 +++- 4 files changed, 38 insertions(+), 9 deletions(-) diff --git a/rustfmt-core/src/comment.rs b/rustfmt-core/src/comment.rs index ccb69442ff7d..523282396d04 100644 --- a/rustfmt-core/src/comment.rs +++ b/rustfmt-core/src/comment.rs @@ -214,11 +214,25 @@ pub fn combine_strs_with_missing_comments( )) } +pub fn rewrite_doc_comment(orig: &str, shape: Shape, config: &Config) -> Option { + _rewrite_comment(orig, false, shape, config, true) +} + pub fn rewrite_comment( orig: &str, block_style: bool, shape: Shape, config: &Config, +) -> Option { + _rewrite_comment(orig, block_style, shape, config, false) +} + +fn _rewrite_comment( + orig: &str, + block_style: bool, + shape: Shape, + config: &Config, + is_doc_comment: bool, ) -> Option { // 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 @@ -231,7 +245,7 @@ pub fn rewrite_comment( return Some(orig.to_owned()); } if !config.normalize_comments() && !config.wrap_comments() { - return light_rewrite_comment(orig, shape.indent, config); + return light_rewrite_comment(orig, shape.indent, config, is_doc_comment); } identify_comment(orig, block_style, shape, config) @@ -495,8 +509,10 @@ pub fn recover_missing_comment_in_span( } /// Trim trailing whitespaces unless they consist of two whitespaces. -fn trim_right_unless_two_whitespaces(s: &str) -> &str { - if s.ends_with(" ") && !s.chars().rev().nth(2).map_or(true, char::is_whitespace) { +fn trim_right_unless_two_whitespaces(s: &str, is_doc_comment: bool) -> &str { + if is_doc_comment && s.ends_with(" ") + && !s.chars().rev().nth(2).map_or(true, char::is_whitespace) + { s } else { s.trim_right() @@ -504,7 +520,12 @@ fn trim_right_unless_two_whitespaces(s: &str) -> &str { } /// Trims whitespace and aligns to indent, but otherwise does not change comments. -fn light_rewrite_comment(orig: &str, offset: Indent, config: &Config) -> Option { +fn light_rewrite_comment( + orig: &str, + offset: Indent, + config: &Config, + is_doc_comment: bool, +) -> Option { let lines: Vec<&str> = orig.lines() .map(|l| { // This is basically just l.trim(), but in the case that a line starts @@ -521,7 +542,7 @@ fn light_rewrite_comment(orig: &str, offset: Indent, config: &Config) -> Option< "" }; // Preserve markdown's double-space line break syntax. - trim_right_unless_two_whitespaces(left_trimmed) + trim_right_unless_two_whitespaces(left_trimmed, is_doc_comment) }) .collect(); Some(lines.join(&format!("\n{}", offset.to_string(config)))) diff --git a/rustfmt-core/src/visitor.rs b/rustfmt-core/src/visitor.rs index 0a0d59d0f02c..2d10be39d092 100644 --- a/rustfmt-core/src/visitor.rs +++ b/rustfmt-core/src/visitor.rs @@ -19,7 +19,7 @@ use syntax::parse::ParseSess; use codemap::{LineRangeUtils, SpanUtils}; use comment::{combine_strs_with_missing_comments, contains_comment, CodeCharKind, CommentCodeSlices, FindUncommented}; -use comment::rewrite_comment; +use comment::rewrite_doc_comment; use config::{BraceStyle, Config}; use expr::rewrite_literal; use items::{format_impl, format_trait, format_trait_alias, rewrite_associated_impl_type, @@ -892,7 +892,7 @@ impl Rewrite for ast::Attribute { .unwrap_or(0), ..shape }; - rewrite_comment(snippet, false, doc_shape, context.config) + rewrite_doc_comment(snippet, doc_shape, context.config) } else { if contains_comment(snippet) { return Some(snippet.to_owned()); @@ -957,7 +957,7 @@ fn rewrite_first_group_attrs( .join("\n"); return Some(( sugared_docs.len(), - rewrite_comment(&snippet, false, shape, context.config)?, + rewrite_doc_comment(&snippet, shape, context.config)?, )); } // Rewrite `#[derive(..)]`s. diff --git a/rustfmt-core/tests/source/markdown-comment.rs b/rustfmt-core/tests/source/markdown-comment.rs index 265f97faa0f7..c3633141ba5d 100644 --- a/rustfmt-core/tests/source/markdown-comment.rs +++ b/rustfmt-core/tests/source/markdown-comment.rs @@ -1,8 +1,12 @@ +// 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 diff --git a/rustfmt-core/tests/target/markdown-comment.rs b/rustfmt-core/tests/target/markdown-comment.rs index 20dc7260d2f3..5c4c413bddf5 100644 --- a/rustfmt-core/tests/target/markdown-comment.rs +++ b/rustfmt-core/tests/target/markdown-comment.rs @@ -1,10 +1,14 @@ +// 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 // hello world let x = 3; println!("x = {}", x); From 6f38a4aeab697dd6d52ca4cdcdec6d7d03c960f2 Mon Sep 17 00:00:00 2001 From: topecongiro Date: Thu, 15 Feb 2018 16:41:47 +0900 Subject: [PATCH 3/3] Preserve two or more trailing spaces in doc comment rustdoc treats two or more trailing spaces as a line break. --- rustfmt-core/src/comment.rs | 8 +++----- rustfmt-core/tests/source/markdown-comment.rs | 2 +- rustfmt-core/tests/target/markdown-comment.rs | 2 +- 3 files changed, 5 insertions(+), 7 deletions(-) diff --git a/rustfmt-core/src/comment.rs b/rustfmt-core/src/comment.rs index 523282396d04..dc0c59a617e9 100644 --- a/rustfmt-core/src/comment.rs +++ b/rustfmt-core/src/comment.rs @@ -508,11 +508,9 @@ pub fn recover_missing_comment_in_span( } } -/// Trim trailing whitespaces unless they consist of two whitespaces. +/// Trim trailing whitespaces unless they consist of two or more whitespaces. fn trim_right_unless_two_whitespaces(s: &str, is_doc_comment: bool) -> &str { - if is_doc_comment && s.ends_with(" ") - && !s.chars().rev().nth(2).map_or(true, char::is_whitespace) - { + if is_doc_comment && s.ends_with(" ") { s } else { s.trim_right() @@ -541,7 +539,7 @@ fn light_rewrite_comment( } else { "" }; - // Preserve markdown's double-space line break syntax. + // Preserve markdown's double-space line break syntax in doc comment. trim_right_unless_two_whitespaces(left_trimmed, is_doc_comment) }) .collect(); diff --git a/rustfmt-core/tests/source/markdown-comment.rs b/rustfmt-core/tests/source/markdown-comment.rs index c3633141ba5d..1ec26562fe28 100644 --- a/rustfmt-core/tests/source/markdown-comment.rs +++ b/rustfmt-core/tests/source/markdown-comment.rs @@ -4,7 +4,7 @@ //! hello world //! hello world -/// hello world +/// hello world /// hello world /// hello world fn foo() { diff --git a/rustfmt-core/tests/target/markdown-comment.rs b/rustfmt-core/tests/target/markdown-comment.rs index 5c4c413bddf5..71a9921d2862 100644 --- a/rustfmt-core/tests/target/markdown-comment.rs +++ b/rustfmt-core/tests/target/markdown-comment.rs @@ -4,7 +4,7 @@ //! hello world //! hello world -/// hello world +/// hello world /// hello world /// hello world fn foo() {