Skip to content

Commit 021f922

Browse files
committed
Keep the pre-comment on the same line with item if it fits max width
1 parent b1b2dc8 commit 021f922

File tree

3 files changed

+81
-11
lines changed

3 files changed

+81
-11
lines changed

src/lists.rs

Lines changed: 57 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,20 @@ impl AsRef<ListItem> for ListItem {
7676
}
7777
}
7878

79+
#[derive(PartialEq, Eq)]
80+
pub enum ListItemCommentStyle {
81+
// Try to keep the comment on the same line with the item.
82+
SameLine,
83+
// Put the comment on the previous or the next line of the item.
84+
DifferentLine,
85+
// No comment available.
86+
None,
87+
}
88+
7989
pub struct ListItem {
8090
// None for comments mean that they are not present.
8191
pub pre_comment: Option<String>,
92+
pub pre_comment_style: ListItemCommentStyle,
8293
// Item should include attributes and doc comments. None indicates a failed
8394
// rewrite.
8495
pub item: Option<String>,
@@ -111,6 +122,7 @@ impl ListItem {
111122
pub fn from_str<S: Into<String>>(s: S) -> ListItem {
112123
ListItem {
113124
pre_comment: None,
125+
pre_comment_style: ListItemCommentStyle::None,
114126
item: Some(s.into()),
115127
post_comment: None,
116128
new_lines: false,
@@ -279,8 +291,23 @@ where
279291
result.push_str(&comment);
280292

281293
if tactic == DefinitiveListTactic::Vertical {
282-
result.push('\n');
283-
result.push_str(indent_str);
294+
// We cannot keep pre-comments on the same line if the comment if normalized.
295+
let keep_comment = if formatting.config.normalize_comments() {
296+
false
297+
} else if item.pre_comment_style == ListItemCommentStyle::DifferentLine {
298+
false
299+
} else {
300+
// We will try to keep the comment on the same line with the item here.
301+
// 1 = ` `
302+
let total_width = total_item_width(item) + item_sep_len + 1;
303+
total_width <= formatting.shape.width
304+
};
305+
if keep_comment {
306+
result.push(' ');
307+
} else {
308+
result.push('\n');
309+
result.push_str(indent_str);
310+
}
284311
} else {
285312
result.push(' ');
286313
}
@@ -448,12 +475,34 @@ where
448475
.span_to_snippet(mk_sp(self.prev_span_end, (self.get_lo)(&item)))
449476
.unwrap();
450477
let trimmed_pre_snippet = pre_snippet.trim();
451-
let has_pre_comment =
452-
trimmed_pre_snippet.contains("//") || trimmed_pre_snippet.contains("/*");
453-
let pre_comment = if has_pre_comment {
454-
Some(trimmed_pre_snippet.to_owned())
478+
let has_single_line_comment = trimmed_pre_snippet.starts_with("//");
479+
let has_block_comment = trimmed_pre_snippet.starts_with("/*");
480+
let (pre_comment, pre_comment_style) = if has_single_line_comment {
481+
(
482+
Some(trimmed_pre_snippet.to_owned()),
483+
ListItemCommentStyle::DifferentLine,
484+
)
485+
} else if has_block_comment {
486+
let comment_end = pre_snippet.chars().rev().position(|c| c == '/').unwrap();
487+
if pre_snippet
488+
.chars()
489+
.rev()
490+
.take(comment_end + 1)
491+
.find(|c| *c == '\n')
492+
.is_some()
493+
{
494+
(
495+
Some(trimmed_pre_snippet.to_owned()),
496+
ListItemCommentStyle::DifferentLine,
497+
)
498+
} else {
499+
(
500+
Some(trimmed_pre_snippet.to_owned()),
501+
ListItemCommentStyle::SameLine,
502+
)
503+
}
455504
} else {
456-
None
505+
(None, ListItemCommentStyle::None)
457506
};
458507

459508
// Post-comment
@@ -542,6 +591,7 @@ where
542591

543592
ListItem {
544593
pre_comment: pre_comment,
594+
pre_comment_style: pre_comment_style,
545595
item: (self.get_item_string)(&item),
546596
post_comment: post_comment,
547597
new_lines: new_lines,

tests/source/expr-block.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,3 +272,13 @@ fn combine_block() {
272272
),
273273
}
274274
}
275+
276+
fn issue_1862() {
277+
foo(
278+
/* bar = */ None ,
279+
something_something,
280+
/* baz = */ None ,
281+
/* This comment waaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaay too long to be kept on the same line */ None ,
282+
/* com */ this_last_arg_is_tooooooooooooooooooooooooooooooooo_long_to_be_kept_with_the_pre_comment ,
283+
)
284+
}

tests/target/expr-block.rs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,7 @@ fn arrays() {
8181
];
8282

8383
let y = [
84-
/* comment */
85-
1,
84+
/* comment */ 1,
8685
2, /* post comment */
8786
3,
8887
];
@@ -92,8 +91,7 @@ fn arrays() {
9291
test123: value_one_two_three_four,
9392
turbo: coolio(),
9493
},
95-
/* comment */
96-
1,
94+
/* comment */ 1,
9795
];
9896

9997
let a = WeightedChoice::new(&mut [
@@ -323,3 +321,15 @@ fn combine_block() {
323321
),
324322
}
325323
}
324+
325+
fn issue_1862() {
326+
foo(
327+
/* bar = */ None,
328+
something_something,
329+
/* baz = */ None,
330+
/* This comment waaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaay too long to be kept on the same line */
331+
None,
332+
/* com */
333+
this_last_arg_is_tooooooooooooooooooooooooooooooooo_long_to_be_kept_with_the_pre_comment,
334+
)
335+
}

0 commit comments

Comments
 (0)