Skip to content

Commit dd7add7

Browse files
committed
Don't align comments on extern crates
Closes rust-lang#3128
1 parent 97fb3f8 commit dd7add7

File tree

3 files changed

+44
-17
lines changed

3 files changed

+44
-17
lines changed

Diff for: src/comment.rs

+8
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,14 @@ pub fn combine_strs_with_missing_comments(
163163
shape: Shape,
164164
allow_extend: bool,
165165
) -> Option<String> {
166+
trace!(
167+
"combine_strs_with_missing_comments `{}` `{}` {:?} {:?}",
168+
prev_str,
169+
next_str,
170+
span,
171+
shape
172+
);
173+
166174
let mut result =
167175
String::with_capacity(prev_str.len() + next_str.len() + shape.indent.width() + 128);
168176
result.push_str(prev_str);

Diff for: src/lists.rs

+33-16
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ pub struct ListFormatting<'a> {
3636
preserve_newline: bool,
3737
// Nested import lists get some special handling for the "Mixed" list type
3838
nested: bool,
39+
// Whether comments should be visually aligned.
40+
align_comments: bool,
3941
config: &'a Config,
4042
}
4143

@@ -50,6 +52,7 @@ impl<'a> ListFormatting<'a> {
5052
ends_with_newline: true,
5153
preserve_newline: false,
5254
nested: false,
55+
align_comments: true,
5356
config,
5457
}
5558
}
@@ -89,6 +92,11 @@ impl<'a> ListFormatting<'a> {
8992
self
9093
}
9194

95+
pub fn align_comments(mut self, align_comments: bool) -> Self {
96+
self.align_comments = align_comments;
97+
self
98+
}
99+
92100
pub fn needs_trailing_separator(&self) -> bool {
93101
match self.trailing_separator {
94102
// We always put separator in front.
@@ -465,23 +473,31 @@ where
465473
let mut formatted_comment = rewrite_post_comment(&mut item_max_width)?;
466474

467475
if !starts_with_newline(comment) {
468-
let mut comment_alignment =
469-
post_comment_alignment(item_max_width, inner_item.len());
470-
if first_line_width(&formatted_comment)
471-
+ last_line_width(&result)
472-
+ comment_alignment
473-
+ 1
474-
> formatting.config.max_width()
475-
{
476-
item_max_width = None;
477-
formatted_comment = rewrite_post_comment(&mut item_max_width)?;
478-
comment_alignment = post_comment_alignment(item_max_width, inner_item.len());
479-
}
480-
for _ in 0..=comment_alignment {
481-
result.push(' ');
476+
if formatting.align_comments {
477+
let mut comment_alignment =
478+
post_comment_alignment(item_max_width, inner_item.len());
479+
if first_line_width(&formatted_comment)
480+
+ last_line_width(&result)
481+
+ comment_alignment
482+
+ 1
483+
> formatting.config.max_width()
484+
{
485+
item_max_width = None;
486+
formatted_comment = rewrite_post_comment(&mut item_max_width)?;
487+
comment_alignment =
488+
post_comment_alignment(item_max_width, inner_item.len());
489+
}
490+
for _ in 0..=comment_alignment {
491+
result.push(' ');
492+
}
482493
}
483-
// An additional space for the missing trailing separator.
484-
if last && item_max_width.is_some() && !separate && !formatting.separator.is_empty()
494+
// An additional space for the missing trailing separator (or
495+
// if we skipped alignment above).
496+
if !formatting.align_comments
497+
|| (last
498+
&& item_max_width.is_some()
499+
&& !separate
500+
&& !formatting.separator.is_empty())
485501
{
486502
result.push(' ');
487503
}
@@ -902,6 +918,7 @@ pub fn struct_lit_formatting<'a>(
902918
ends_with_newline,
903919
preserve_newline: true,
904920
nested: false,
921+
align_comments: true,
905922
config: context.config,
906923
}
907924
}

Diff for: src/reorder.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,9 @@ fn wrap_reorderable_items(
6969
list_items: &[ListItem],
7070
shape: Shape,
7171
) -> Option<String> {
72-
let fmt = ListFormatting::new(shape, context.config).separator("");
72+
let fmt = ListFormatting::new(shape, context.config)
73+
.separator("")
74+
.align_comments(false);
7375
write_list(list_items, &fmt)
7476
}
7577

0 commit comments

Comments
 (0)