From 334b4107859637ebad2e65dcd37502aa48622495 Mon Sep 17 00:00:00 2001 From: topecongiro Date: Thu, 23 Nov 2017 12:36:57 +0900 Subject: [PATCH 1/4] Add a test for #2111 --- tests/source/issue-2111.rs | 28 ++++++++++++++++++++++++++++ tests/target/issue-2111.rs | 26 ++++++++++++++++++++++++++ 2 files changed, 54 insertions(+) create mode 100644 tests/source/issue-2111.rs create mode 100644 tests/target/issue-2111.rs diff --git a/tests/source/issue-2111.rs b/tests/source/issue-2111.rs new file mode 100644 index 00000000000..d1b02053e2a --- /dev/null +++ b/tests/source/issue-2111.rs @@ -0,0 +1,28 @@ +// rustfmt-normalize_comments: false + +// An import with single line comments. +use super::{ + SCHEMA_VERSIONS, + LodaModel, + ModelProperties, + StringMap, + ModelSelector, + RequestDescription, + MethodDescription, + ModelBehaviour, + ModelRequestGraph, + DelayChoice, + Holding, + Destinations, + ModelEdges, + Switch, + // ModelMetaData, + // Generated, + // SecondsString, + // DateString, + // ModelConfiguration, + // ModelRequests, + // RestResponse, + // RestResponseCode, + // UniformHolding +}; diff --git a/tests/target/issue-2111.rs b/tests/target/issue-2111.rs new file mode 100644 index 00000000000..13303a736a3 --- /dev/null +++ b/tests/target/issue-2111.rs @@ -0,0 +1,26 @@ +// rustfmt-normalize_comments: false + +// An import with single line comments. +use super::{DelayChoice, + Destinations, + Holding, + LodaModel, + MethodDescription, + ModelBehaviour, + ModelEdges, + ModelProperties, + ModelRequestGraph, + ModelSelector, + RequestDescription, + StringMap, + Switch, + // ModelMetaData, + // Generated, + // SecondsString, + // DateString, + // ModelConfiguration, + // ModelRequests, + // RestResponse, + // RestResponseCode, + // UniformHolding + SCHEMA_VERSIONS}; From 53d7489221e4d6fa25bf624b78a80608c27aedf5 Mon Sep 17 00:00:00 2001 From: topecongiro Date: Thu, 23 Nov 2017 12:37:12 +0900 Subject: [PATCH 2/4] Force vertical layout when we find any kind of single line comments --- src/lists.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/lists.rs b/src/lists.rs index b80d8d6541e..1e073945c0b 100644 --- a/src/lists.rs +++ b/src/lists.rs @@ -137,10 +137,10 @@ impl ListItem { pub fn has_comment(&self) -> bool { self.pre_comment .as_ref() - .map_or(false, |comment| comment.starts_with("//")) + .map_or(false, |comment| comment.trim_left().starts_with("//")) || self.post_comment .as_ref() - .map_or(false, |comment| comment.starts_with("//")) + .map_or(false, |comment| comment.trim_left().starts_with("//")) } pub fn from_str>(s: S) -> ListItem { From d92cfff43b2dc1ce43fdac172855a8d34bb79c8c Mon Sep 17 00:00:00 2001 From: topecongiro Date: Thu, 23 Nov 2017 12:38:34 +0900 Subject: [PATCH 3/4] Align post comments with items when the it starts with newline --- src/lists.rs | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/src/lists.rs b/src/lists.rs index 1e073945c0b..bf0d276574e 100644 --- a/src/lists.rs +++ b/src/lists.rs @@ -410,7 +410,9 @@ where formatting.config.max_width(), )); } - let overhead = if let Some(max_width) = *item_max_width { + let overhead = if starts_with_newline(comment) { + 0 + } else if let Some(max_width) = *item_max_width { max_width + 2 } else { // 1 = space between item and comment. @@ -425,12 +427,17 @@ where || comment.trim().contains('\n') || comment.trim().len() > width; - rewrite_comment(comment, block_style, comment_shape, formatting.config) + rewrite_comment( + comment.trim_left(), + block_style, + comment_shape, + formatting.config, + ) }; let mut formatted_comment = rewrite_post_comment(&mut item_max_width)?; - if !starts_with_newline(&formatted_comment) { + if !starts_with_newline(&comment) { let mut comment_alignment = post_comment_alignment(item_max_width, inner_item.len()); if first_line_width(&formatted_comment) + last_line_width(&result) @@ -448,6 +455,9 @@ where { result.push(' '); } + } else { + result.push('\n'); + result.push_str(&indent_str); } if formatted_comment.contains('\n') { item_max_width = None; From 5314426bc28a8165a00367e8ed3ecf46cfad29a9 Mon Sep 17 00:00:00 2001 From: topecongiro Date: Thu, 23 Nov 2017 12:39:00 +0900 Subject: [PATCH 4/4] Update tests --- tests/target/fn-args-with-last-line-comment.rs | 2 +- tests/target/type_alias.rs | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/tests/target/fn-args-with-last-line-comment.rs b/tests/target/fn-args-with-last-line-comment.rs index ef40e040ea8..27e0e09653e 100644 --- a/tests/target/fn-args-with-last-line-comment.rs +++ b/tests/target/fn-args-with-last-line-comment.rs @@ -18,7 +18,7 @@ fn foo( arg5: LongTypeName, arg6: LongTypeName, arg7: LongTypeName, - //arg8: LongTypeName, + //arg8: LongTypeName, ) { // do stuff } diff --git a/tests/target/type_alias.rs b/tests/target/type_alias.rs index 97a88bb6372..5aef092934e 100644 --- a/tests/target/type_alias.rs +++ b/tests/target/type_alias.rs @@ -47,7 +47,8 @@ pub type GenericsFitButNotEqualTest< pub type CommentTest< // Lifetime - 'a, // Type + 'a, + // Type T, > = ();