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,
allow_extend: bool,
) -> Option<String> {
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);

View File

@ -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,
}
}

View File

@ -69,7 +69,9 @@ fn wrap_reorderable_items(
list_items: &[ListItem],
shape: Shape,
) -> 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)
}