Skip to content

Commit ec46ffd

Browse files
ytmimicalebcartwright
authored andcommitted
Determine when new comment lines are needed for itemized blocks
Fixes 5088 Previously, rustfmt would add a new comment line anytime it reformatted an itemized block within a comment when ``wrap_comments=true``. This would lead to rustfmt adding empty comments with trailing whitespace. Now, new comment lines are only added if the original comment spanned multiple lines, if the comment needs to be wrapped, or if the comment originally started with an empty comment line.
1 parent 1f28683 commit ec46ffd

18 files changed

+424
-6
lines changed

src/comment.rs

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -519,6 +519,7 @@ struct CommentRewrite<'a> {
519519
opener: String,
520520
closer: String,
521521
line_start: String,
522+
style: CommentStyle<'a>,
522523
}
523524

524525
impl<'a> CommentRewrite<'a> {
@@ -528,10 +529,14 @@ impl<'a> CommentRewrite<'a> {
528529
shape: Shape,
529530
config: &'a Config,
530531
) -> CommentRewrite<'a> {
531-
let (opener, closer, line_start) = if block_style {
532-
CommentStyle::SingleBullet.to_str_tuplet()
532+
let ((opener, closer, line_start), style) = if block_style {
533+
(
534+
CommentStyle::SingleBullet.to_str_tuplet(),
535+
CommentStyle::SingleBullet,
536+
)
533537
} else {
534-
comment_style(orig, config.normalize_comments()).to_str_tuplet()
538+
let style = comment_style(orig, config.normalize_comments());
539+
(style.to_str_tuplet(), style)
535540
};
536541

537542
let max_width = shape
@@ -564,6 +569,7 @@ impl<'a> CommentRewrite<'a> {
564569
opener: opener.to_owned(),
565570
closer: closer.to_owned(),
566571
line_start: line_start.to_owned(),
572+
style,
567573
};
568574
cr.result.push_str(opener);
569575
cr
@@ -583,6 +589,15 @@ impl<'a> CommentRewrite<'a> {
583589
result
584590
}
585591

592+
/// Check if any characters were written to the result buffer after the start of the comment.
593+
/// when calling [`CommentRewrite::new()`] the result buffer is initiazlied with the opening
594+
/// characters for the comment.
595+
fn buffer_contains_comment(&self) -> bool {
596+
// if self.result.len() < self.opener.len() then an empty comment is in the buffer
597+
// if self.result.len() > self.opener.len() then a non empty comment is in the buffer
598+
self.result.len() != self.opener.len()
599+
}
600+
586601
fn finish(mut self) -> String {
587602
if !self.code_block_buffer.is_empty() {
588603
// There is a code block that is not properly enclosed by backticks.
@@ -598,7 +613,12 @@ impl<'a> CommentRewrite<'a> {
598613
// the last few lines are part of an itemized block
599614
self.fmt.shape = Shape::legacy(self.max_width, self.fmt_indent);
600615
let item_fmt = ib.create_string_format(&self.fmt);
601-
self.result.push_str(&self.comment_line_separator);
616+
617+
// only push a comment_line_separator for ItemizedBlocks if the comment is not empty
618+
if self.buffer_contains_comment() {
619+
self.result.push_str(&self.comment_line_separator);
620+
}
621+
602622
self.result.push_str(&ib.opener);
603623
match rewrite_string(
604624
&ib.trimmed_block_as_string(),
@@ -632,7 +652,13 @@ impl<'a> CommentRewrite<'a> {
632652
line: &'a str,
633653
has_leading_whitespace: bool,
634654
) -> bool {
635-
let is_last = i == count_newlines(orig);
655+
let num_newlines = count_newlines(orig);
656+
let is_last = i == num_newlines;
657+
let needs_new_comment_line = if self.style.is_block_comment() {
658+
num_newlines > 0 || self.buffer_contains_comment()
659+
} else {
660+
self.buffer_contains_comment()
661+
};
636662

637663
if let Some(ref mut ib) = self.item_block {
638664
if ib.add_line(line) {
@@ -641,7 +667,12 @@ impl<'a> CommentRewrite<'a> {
641667
self.is_prev_line_multi_line = false;
642668
self.fmt.shape = Shape::legacy(self.max_width, self.fmt_indent);
643669
let item_fmt = ib.create_string_format(&self.fmt);
644-
self.result.push_str(&self.comment_line_separator);
670+
671+
// only push a comment_line_separator if we need to start a new comment line
672+
if needs_new_comment_line {
673+
self.result.push_str(&self.comment_line_separator);
674+
}
675+
645676
self.result.push_str(&ib.opener);
646677
match rewrite_string(
647678
&ib.trimmed_block_as_string(),
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// rustfmt-wrap_comments: true
2+
3+
fn main() {
4+
{
5+
{
6+
{
7+
{
8+
{
9+
{
10+
{
11+
{
12+
{
13+
{
14+
{
15+
// - aaaa aaaaaaaaa aaaaaaaaa aaaaaaaaa aaaaaaaaa bbbbbbbbbb bbbbbbbbb bbbbbbbbb ccc cccccccccc ccccccc cccccccc
16+
17+
// * aaaa aaaaaaaaa aaaaaaaaa aaaaaaaaa aaaaaaaaa bbbbbbbbbb bbbbbbbbb bbbbbbbbb ccc cccccccccc ccccccc cccccccc
18+
19+
/* - aaaa aaaaaaaaa aaaaaaaaa aaaaaaaaa aaaaaaaaa bbbbbbbbbb bbbbbbbbb bbbbbbbbb ccc cccccccccc ccccccc cccccccc */
20+
21+
/* * aaaa aaaaaaaaa aaaaaaaaa aaaaaaaaa aaaaaaaaa bbbbbbbbbb bbbbbbbbb bbbbbbbbb ccc cccccccccc ccccccc cccccccc */
22+
};
23+
};
24+
};
25+
};
26+
};
27+
};
28+
};
29+
};
30+
};
31+
};
32+
};
33+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// rustfmt-wrap_comments: true
2+
3+
//
4+
// - Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
5+
// - Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
6+
7+
//
8+
// * Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
9+
// * Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
10+
11+
/*
12+
* - Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.*/
13+
/*
14+
* - Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.*/
15+
16+
/*
17+
* * Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.*/
18+
/*
19+
* * Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.*/
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// rustfmt-wrap_comments: true
2+
3+
// - Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
4+
// - Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
5+
6+
// * Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
7+
// * Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
8+
9+
/* - Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.*/
10+
/* - Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.*/
11+
12+
/* * Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.*/
13+
/* * Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.*/
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// rustfmt-wrap_comments: false
2+
3+
fn main() {
4+
{
5+
{
6+
{
7+
{
8+
{
9+
{
10+
{
11+
{
12+
{
13+
{
14+
{
15+
// - aaaa aaaaaaaaa aaaaaaaaa aaaaaaaaa aaaaaaaaa bbbbbbbbbb bbbbbbbbb bbbbbbbbb ccc cccccccccc ccccccc cccccccc
16+
17+
// * aaaa aaaaaaaaa aaaaaaaaa aaaaaaaaa aaaaaaaaa bbbbbbbbbb bbbbbbbbb bbbbbbbbb ccc cccccccccc ccccccc cccccccc
18+
19+
/* - aaaa aaaaaaaaa aaaaaaaaa aaaaaaaaa aaaaaaaaa bbbbbbbbbb bbbbbbbbb bbbbbbbbb ccc cccccccccc ccccccc cccccccc */
20+
21+
/* * aaaa aaaaaaaaa aaaaaaaaa aaaaaaaaa aaaaaaaaa bbbbbbbbbb bbbbbbbbb bbbbbbbbb ccc cccccccccc ccccccc cccccccc */
22+
};
23+
};
24+
};
25+
};
26+
};
27+
};
28+
};
29+
};
30+
};
31+
};
32+
};
33+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// rustfmt-wrap_comments: true
2+
3+
fn main() {
4+
{
5+
{
6+
{
7+
{
8+
{
9+
{
10+
{
11+
{
12+
{
13+
{
14+
{
15+
// - aaaa aaaaaaaaa aaaaaaaaa
16+
// aaaaaaaaa aaaaaaaaa
17+
// bbbbbbbbbb bbbbbbbbb
18+
// bbbbbbbbb ccc cccccccccc
19+
// ccccccc cccccccc
20+
21+
// * aaaa aaaaaaaaa aaaaaaaaa
22+
// aaaaaaaaa aaaaaaaaa
23+
// bbbbbbbbbb bbbbbbbbb
24+
// bbbbbbbbb ccc cccccccccc
25+
// ccccccc cccccccc
26+
27+
/* - aaaa aaaaaaaaa aaaaaaaaa
28+
* aaaaaaaaa aaaaaaaaa
29+
* bbbbbbbbbb bbbbbbbbb
30+
* bbbbbbbbb ccc cccccccccc
31+
* ccccccc cccccccc */
32+
33+
/* * aaaa aaaaaaaaa aaaaaaaaa
34+
* aaaaaaaaa aaaaaaaaa
35+
* bbbbbbbbbb bbbbbbbbb
36+
* bbbbbbbbb ccc cccccccccc
37+
* ccccccc cccccccc */
38+
};
39+
};
40+
};
41+
};
42+
};
43+
};
44+
};
45+
};
46+
};
47+
};
48+
};
49+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// rustfmt-wrap_comments: false
2+
3+
// - some itemized block 1
4+
// - some itemized block 2
5+
6+
// * some itemized block 3
7+
// * some itemized block 4
8+
9+
/*
10+
* - some itemized block 5
11+
* - some itemized block 6
12+
*/
13+
14+
/*
15+
* * some itemized block 7
16+
* * some itemized block 8
17+
*/
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// rustfmt-wrap_comments: true
2+
3+
// - some itemized block 1
4+
// - some itemized block 2
5+
6+
// * some itemized block 3
7+
// * some itemized block 4
8+
9+
/*
10+
* - some itemized block 5
11+
* - some itemized block 6
12+
*/
13+
14+
/*
15+
* * some itemized block 7
16+
* * some itemized block 8
17+
*/
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// rustfmt-wrap_comments: false
2+
3+
// Some text
4+
// - some itemized block 1
5+
// - some itemized block 2
6+
// Some more text
7+
// - some itemized block 3
8+
// - some itemized block 4
9+
// Even more text
10+
11+
// Some text
12+
// * some itemized block 5
13+
// * some itemized block 6
14+
// Some more text
15+
// * some itemized block 7
16+
// * some itemized block 8
17+
// Even more text
18+
19+
/*
20+
* Some text
21+
* - some itemized block 9
22+
* - some itemized block 10
23+
* Some more text
24+
* - some itemized block 11
25+
* - some itemized block 12
26+
* Even more text
27+
*/
28+
29+
/*
30+
* Some text
31+
* * some itemized block 13
32+
* * some itemized block 14
33+
* Some more text
34+
* * some itemized block 15
35+
* * some itemized block 16
36+
* Even more text
37+
*/
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// rustfmt-wrap_comments: true
2+
3+
// Some text
4+
// - some itemized block 1
5+
// - some itemized block 2
6+
// Some more text
7+
// - some itemized block 3
8+
// - some itemized block 4
9+
// Even more text
10+
11+
// Some text
12+
// * some itemized block 5
13+
// * some itemized block 6
14+
// Some more text
15+
// * some itemized block 7
16+
// * some itemized block 8
17+
// Even more text
18+
19+
/*
20+
* Some text
21+
* - some itemized block 9
22+
* - some itemized block 10
23+
* Some more text
24+
* - some itemized block 11
25+
* - some itemized block 12
26+
* Even more text
27+
*/
28+
29+
/*
30+
* Some text
31+
* * some itemized block 13
32+
* * some itemized block 14
33+
* Some more text
34+
* * some itemized block 15
35+
* * some itemized block 16
36+
* Even more text
37+
*/
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// rustfmt-wrap_comments: false
2+
3+
// - some itemized block 1
4+
5+
// * some itemized block 2
6+
7+
/* - some itemized block 3 */
8+
9+
/* * some itemized block 4 */
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// rustfmt-wrap_comments: true
2+
3+
// - some itemized block 1
4+
5+
// * some itemized block 2
6+
7+
/* - some itemized block 3 */
8+
9+
/* * some itemized block 4 */
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// rustfmt-wrap_comments: false
2+
3+
//
4+
// - Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
5+
// - Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
6+
7+
//
8+
// * Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
9+
// * Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
10+
11+
/*
12+
* - Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.*/
13+
/*
14+
* - Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.*/
15+
16+
/*
17+
* * Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.*/
18+
/*
19+
* * Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.*/

0 commit comments

Comments
 (0)