]> git.lizzy.rs Git - rust.git/commitdiff
Merge pull request #2181 from topecongiro/issue-2111
authorNick Cameron <nrc@ncameron.org>
Thu, 23 Nov 2017 22:03:00 +0000 (11:03 +1300)
committerGitHub <noreply@github.com>
Thu, 23 Nov 2017 22:03:00 +0000 (11:03 +1300)
Use vertical layout when there are single line comments in import

src/lists.rs
tests/source/issue-2111.rs [new file with mode: 0644]
tests/target/fn-args-with-last-line-comment.rs
tests/target/issue-2111.rs [new file with mode: 0644]
tests/target/type_alias.rs

index b80d8d6541eb7a6334953760c5f248df1e891255..bf0d276574eb4ad8a176f28d780fe462ad25900d 100644 (file)
@@ -137,10 +137,10 @@ pub fn is_multiline(&self) -> bool {
     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: Into<String>>(s: S) -> ListItem {
@@ -410,7 +410,9 @@ pub fn write_list<I, T>(items: I, formatting: &ListFormatting) -> Option<String>
                         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 @@ pub fn write_list<I, T>(items: I, formatting: &ListFormatting) -> Option<String>
                     || 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 @@ pub fn write_list<I, T>(items: I, formatting: &ListFormatting) -> Option<String>
                 {
                     result.push(' ');
                 }
+            } else {
+                result.push('\n');
+                result.push_str(&indent_str);
             }
             if formatted_comment.contains('\n') {
                 item_max_width = None;
diff --git a/tests/source/issue-2111.rs b/tests/source/issue-2111.rs
new file mode 100644 (file)
index 0000000..d1b0205
--- /dev/null
@@ -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
+};
index ef40e040ea8e6effa93048dd3d235dc28046df9e..27e0e09653eee3a08076480400cdc7bfa55c0a81 100644 (file)
@@ -18,7 +18,7 @@ fn foo(
     arg5: LongTypeName,
     arg6: LongTypeName,
     arg7: LongTypeName,
-       //arg8: LongTypeName,
+    //arg8: LongTypeName,
 ) {
     // do stuff
 }
diff --git a/tests/target/issue-2111.rs b/tests/target/issue-2111.rs
new file mode 100644 (file)
index 0000000..13303a7
--- /dev/null
@@ -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};
index 97a88bb6372be4adf7d72c4c3ec8ae760b401e6b..5aef092934e1b336bde15ad74777df20098d77e3 100644 (file)
@@ -47,7 +47,8 @@
 
 pub type CommentTest<
     // Lifetime
-    'a, // Type
+    'a,
+    // Type
     T,
 > = ();