]> git.lizzy.rs Git - rust.git/commitdiff
Align post comments with items when the it starts with newline
authortopecongiro <seuchida@gmail.com>
Thu, 23 Nov 2017 03:38:34 +0000 (12:38 +0900)
committertopecongiro <seuchida@gmail.com>
Thu, 23 Nov 2017 03:38:34 +0000 (12:38 +0900)
src/lists.rs

index 1e073945c0b1eb79dfe06a9204a51c92c9dc62ee..bf0d276574eb4ad8a176f28d780fe462ad25900d 100644 (file)
@@ -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;