From d92cfff43b2dc1ce43fdac172855a8d34bb79c8c Mon Sep 17 00:00:00 2001 From: topecongiro Date: Thu, 23 Nov 2017 12:38:34 +0900 Subject: [PATCH] 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 @@ pub fn write_list(items: I, formatting: &ListFormatting) -> Option 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(items: I, formatting: &ListFormatting) -> Option || 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(items: I, formatting: &ListFormatting) -> Option { result.push(' '); } + } else { + result.push('\n'); + result.push_str(&indent_str); } if formatted_comment.contains('\n') { item_max_width = None; -- 2.44.0