Don't align comments on extern crates

Closes #3128
This commit is contained in:
Nick Cameron 2018-11-15 16:45:16 +13:00
parent 97fb3f8dc1
commit dd7add784b
3 changed files with 44 additions and 17 deletions

View file

@ -163,6 +163,14 @@ pub fn combine_strs_with_missing_comments(
shape: Shape, shape: Shape,
allow_extend: bool, allow_extend: bool,
) -> Option<String> { ) -> Option<String> {
trace!(
"combine_strs_with_missing_comments `{}` `{}` {:?} {:?}",
prev_str,
next_str,
span,
shape
);
let mut result = let mut result =
String::with_capacity(prev_str.len() + next_str.len() + shape.indent.width() + 128); String::with_capacity(prev_str.len() + next_str.len() + shape.indent.width() + 128);
result.push_str(prev_str); result.push_str(prev_str);

View file

@ -36,6 +36,8 @@ pub struct ListFormatting<'a> {
preserve_newline: bool, preserve_newline: bool,
// Nested import lists get some special handling for the "Mixed" list type // Nested import lists get some special handling for the "Mixed" list type
nested: bool, nested: bool,
// Whether comments should be visually aligned.
align_comments: bool,
config: &'a Config, config: &'a Config,
} }
@ -50,6 +52,7 @@ impl<'a> ListFormatting<'a> {
ends_with_newline: true, ends_with_newline: true,
preserve_newline: false, preserve_newline: false,
nested: false, nested: false,
align_comments: true,
config, config,
} }
} }
@ -89,6 +92,11 @@ impl<'a> ListFormatting<'a> {
self self
} }
pub fn align_comments(mut self, align_comments: bool) -> Self {
self.align_comments = align_comments;
self
}
pub fn needs_trailing_separator(&self) -> bool { pub fn needs_trailing_separator(&self) -> bool {
match self.trailing_separator { match self.trailing_separator {
// We always put separator in front. // We always put separator in front.
@ -465,6 +473,7 @@ where
let mut formatted_comment = rewrite_post_comment(&mut item_max_width)?; let mut formatted_comment = rewrite_post_comment(&mut item_max_width)?;
if !starts_with_newline(comment) { if !starts_with_newline(comment) {
if formatting.align_comments {
let mut comment_alignment = let mut comment_alignment =
post_comment_alignment(item_max_width, inner_item.len()); post_comment_alignment(item_max_width, inner_item.len());
if first_line_width(&formatted_comment) if first_line_width(&formatted_comment)
@ -475,13 +484,20 @@ where
{ {
item_max_width = None; item_max_width = None;
formatted_comment = rewrite_post_comment(&mut item_max_width)?; formatted_comment = rewrite_post_comment(&mut item_max_width)?;
comment_alignment = post_comment_alignment(item_max_width, inner_item.len()); comment_alignment =
post_comment_alignment(item_max_width, inner_item.len());
} }
for _ in 0..=comment_alignment { for _ in 0..=comment_alignment {
result.push(' '); 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(' '); result.push(' ');
} }
@ -902,6 +918,7 @@ pub fn struct_lit_formatting<'a>(
ends_with_newline, ends_with_newline,
preserve_newline: true, preserve_newline: true,
nested: false, nested: false,
align_comments: true,
config: context.config, config: context.config,
} }
} }

View file

@ -69,7 +69,9 @@ fn wrap_reorderable_items(
list_items: &[ListItem], list_items: &[ListItem],
shape: Shape, shape: Shape,
) -> Option<String> { ) -> Option<String> {
let fmt = ListFormatting::new(shape, context.config).separator(""); let fmt = ListFormatting::new(shape, context.config)
.separator("")
.align_comments(false);
write_list(list_items, &fmt) write_list(list_items, &fmt)
} }