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,23 +473,31 @@ 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) {
let mut comment_alignment = if formatting.align_comments {
post_comment_alignment(item_max_width, inner_item.len()); let mut comment_alignment =
if first_line_width(&formatted_comment) post_comment_alignment(item_max_width, inner_item.len());
+ last_line_width(&result) if first_line_width(&formatted_comment)
+ comment_alignment + last_line_width(&result)
+ 1 + comment_alignment
> formatting.config.max_width() + 1
{ > formatting.config.max_width()
item_max_width = None; {
formatted_comment = rewrite_post_comment(&mut item_max_width)?; item_max_width = None;
comment_alignment = post_comment_alignment(item_max_width, inner_item.len()); 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 { // An additional space for the missing trailing separator (or
result.push(' '); // if we skipped alignment above).
} if !formatting.align_comments
// An additional space for the missing trailing separator. || (last
if last && item_max_width.is_some() && !separate && !formatting.separator.is_empty() && 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)
} }