diff --git a/src/comment.rs b/src/comment.rs index bc46ab6edc27..d126e89f3bc0 100644 --- a/src/comment.rs +++ b/src/comment.rs @@ -163,6 +163,14 @@ pub fn combine_strs_with_missing_comments( shape: Shape, allow_extend: bool, ) -> Option { + trace!( + "combine_strs_with_missing_comments `{}` `{}` {:?} {:?}", + prev_str, + next_str, + span, + shape + ); + let mut result = String::with_capacity(prev_str.len() + next_str.len() + shape.indent.width() + 128); result.push_str(prev_str); diff --git a/src/lists.rs b/src/lists.rs index cd6f363e2699..61fe6c249604 100644 --- a/src/lists.rs +++ b/src/lists.rs @@ -36,6 +36,8 @@ pub struct ListFormatting<'a> { preserve_newline: bool, // Nested import lists get some special handling for the "Mixed" list type nested: bool, + // Whether comments should be visually aligned. + align_comments: bool, config: &'a Config, } @@ -50,6 +52,7 @@ impl<'a> ListFormatting<'a> { ends_with_newline: true, preserve_newline: false, nested: false, + align_comments: true, config, } } @@ -89,6 +92,11 @@ impl<'a> ListFormatting<'a> { self } + pub fn align_comments(mut self, align_comments: bool) -> Self { + self.align_comments = align_comments; + self + } + pub fn needs_trailing_separator(&self) -> bool { match self.trailing_separator { // We always put separator in front. @@ -465,23 +473,31 @@ where let mut formatted_comment = rewrite_post_comment(&mut item_max_width)?; 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) - + comment_alignment - + 1 - > formatting.config.max_width() - { - item_max_width = None; - formatted_comment = rewrite_post_comment(&mut item_max_width)?; - comment_alignment = post_comment_alignment(item_max_width, inner_item.len()); + if formatting.align_comments { + let mut comment_alignment = + post_comment_alignment(item_max_width, inner_item.len()); + if first_line_width(&formatted_comment) + + last_line_width(&result) + + comment_alignment + + 1 + > formatting.config.max_width() + { + item_max_width = None; + formatted_comment = rewrite_post_comment(&mut item_max_width)?; + comment_alignment = + post_comment_alignment(item_max_width, inner_item.len()); + } + for _ in 0..=comment_alignment { + result.push(' '); + } } - for _ in 0..=comment_alignment { - result.push(' '); - } - // An additional space for the missing trailing separator. - if last && item_max_width.is_some() && !separate && !formatting.separator.is_empty() + // An additional space for the missing trailing separator (or + // if we skipped alignment above). + if !formatting.align_comments + || (last + && item_max_width.is_some() + && !separate + && !formatting.separator.is_empty()) { result.push(' '); } @@ -902,6 +918,7 @@ pub fn struct_lit_formatting<'a>( ends_with_newline, preserve_newline: true, nested: false, + align_comments: true, config: context.config, } } diff --git a/src/reorder.rs b/src/reorder.rs index f990e6865659..a865247e8056 100644 --- a/src/reorder.rs +++ b/src/reorder.rs @@ -69,7 +69,9 @@ fn wrap_reorderable_items( list_items: &[ListItem], shape: Shape, ) -> Option { - let fmt = ListFormatting::new(shape, context.config).separator(""); + let fmt = ListFormatting::new(shape, context.config) + .separator("") + .align_comments(false); write_list(list_items, &fmt) }