Skip to content

Commit

Permalink
Don't align comments on extern crates
Browse files Browse the repository at this point in the history
  • Loading branch information
nrc committed Nov 15, 2018
1 parent 97fb3f8 commit dd7add7
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 17 deletions.
8 changes: 8 additions & 0 deletions src/comment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
49 changes: 33 additions & 16 deletions src/lists.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}

Expand All @@ -50,6 +52,7 @@ impl<'a> ListFormatting<'a> {
ends_with_newline: true,
preserve_newline: false,
nested: false,
align_comments: true,
config,
}
}
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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());
}
for _ in 0..=comment_alignment {
result.push(' ');
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(' ');
}
}
// 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(' ');
}
Expand Down Expand Up @@ -902,6 +918,7 @@ pub fn struct_lit_formatting<'a>(
ends_with_newline,
preserve_newline: true,
nested: false,
align_comments: true,
config: context.config,
}
}
4 changes: 3 additions & 1 deletion src/reorder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down

0 comments on commit dd7add7

Please sign in to comment.