Skip to content

Commit 155f6ee

Browse files
davidBar-Oncalebcartwright
authored andcommitted
Fix for issue #4546 - proper indent multiline post-comment in a list
1 parent 2610312 commit 155f6ee

File tree

3 files changed

+77
-1
lines changed

3 files changed

+77
-1
lines changed

src/formatting/lists.rs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -461,6 +461,13 @@ where
461461

462462
let mut formatted_comment = rewrite_post_comment(&mut item_max_width)?;
463463

464+
// Mmultiline comments are not included in a previous "indentation group".
465+
// Each multiline comment is a considered as a separage group.
466+
if formatted_comment.contains('\n') {
467+
item_max_width = None;
468+
formatted_comment = rewrite_post_comment(&mut item_max_width)?;
469+
}
470+
464471
if !starts_with_newline(comment) {
465472
if formatting.align_comments {
466473
let mut comment_alignment = post_comment_alignment(
@@ -537,10 +544,21 @@ where
537544
for item in items.clone().into_iter().skip(i) {
538545
let item = item.as_ref();
539546
let inner_item_width = UnicodeSegmentation::graphemes(item.inner_as_ref(), true).count();
547+
let post_comment_is_multiline = item
548+
.post_comment
549+
.as_ref()
550+
.map_or(false, |s| s.trim().contains('\n'));
551+
552+
// Each multiline comment is an "indentation group" on its own
553+
if first && post_comment_is_multiline {
554+
return inner_item_width;
555+
}
556+
540557
if !first
541558
&& (item.is_different_group()
542559
|| item.post_comment.is_none()
543-
|| inner_item_width + overhead > max_budget)
560+
|| inner_item_width + overhead > max_budget
561+
|| post_comment_is_multiline)
544562
{
545563
return max_width;
546564
}

tests/source/issue-4546.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
fn main() {
2+
let v = [
3+
"A", /* item A comment */
4+
"BBB", /* item B comment */
5+
"CCCCCC", /* item C comment line 1
6+
* item C comment line 2 */
7+
"D", /* item D comment line 1
8+
* item D comment line 2 */
9+
"EEEEE", /* item E comment */
10+
"FFF", /* item F comment */
11+
"GG", /* item G comment line 1
12+
* item G comment line 2 */
13+
];
14+
}
15+
16+
fn main() {
17+
let v = [
18+
"GG", /* item G comment line 1
19+
* item G comment line 2 */
20+
"AAAAA", /* item A comment */
21+
"BBB", /* item B comment */
22+
"CCCCCC", /* item C comment line 1
23+
* item C comment line 2 */
24+
"D", /* item D comment line 1
25+
* item D comment line 2 */
26+
"E", /* item E comment */
27+
"FFF", /* item F comment */
28+
];
29+
}

tests/target/issue-4546.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
fn main() {
2+
let v = [
3+
"A", /* item A comment */
4+
"BBB", /* item B comment */
5+
"CCCCCC", /* item C comment line 1
6+
* item C comment line 2 */
7+
"D", /* item D comment line 1
8+
* item D comment line 2 */
9+
"EEEEE", /* item E comment */
10+
"FFF", /* item F comment */
11+
"GG", /* item G comment line 1
12+
* item G comment line 2 */
13+
];
14+
}
15+
16+
fn main() {
17+
let v = [
18+
"GG", /* item G comment line 1
19+
* item G comment line 2 */
20+
"AAAAA", /* item A comment */
21+
"BBB", /* item B comment */
22+
"CCCCCC", /* item C comment line 1
23+
* item C comment line 2 */
24+
"D", /* item D comment line 1
25+
* item D comment line 2 */
26+
"E", /* item E comment */
27+
"FFF", /* item F comment */
28+
];
29+
}

0 commit comments

Comments
 (0)