Skip to content

Commit 206b944

Browse files
committed
Wrap multi lined trailing block comments (rust-lang#6339)
1 parent 5f48fe9 commit 206b944

File tree

4 files changed

+92
-3
lines changed

4 files changed

+92
-3
lines changed

src/comment.rs

+6
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,12 @@ pub(crate) fn is_last_comment_block(s: &str) -> bool {
146146
s.trim_end().ends_with("*/")
147147
}
148148

149+
/// Return true if a block-comment contains endline character
150+
pub(crate) fn is_multi_lined_block_comment(s: &str) -> bool {
151+
let style = comment_style(s, false);
152+
style.is_block_comment() && count_newlines(s) >= 1
153+
}
154+
149155
/// Combine `prev_str` and `next_str` into a single `String`. `span` may contain
150156
/// comments between two strings. If there are such comments, then that will be
151157
/// recovered. If `allow_extend` is true and there is no comment between the two

src/missed_spans.rs

+12-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
use rustc_span::{BytePos, Pos, Span};
22
use tracing::debug;
33

4-
use crate::comment::{CodeCharKind, CommentCodeSlices, is_last_comment_block, rewrite_comment};
4+
use crate::comment::{
5+
CodeCharKind, CommentCodeSlices, is_last_comment_block, is_multi_lined_block_comment,
6+
rewrite_comment,
7+
};
58
use crate::config::FileName;
69
use crate::config::StyleEdition;
710
use crate::config::file_lines::FileLines;
@@ -239,6 +242,7 @@ impl<'a> FmtVisitor<'a> {
239242

240243
let fix_indent = last_char.map_or(true, |rev_c| ['{', '\n'].contains(&rev_c));
241244
let mut on_same_line = false;
245+
let multi_lined_block_comment = is_multi_lined_block_comment(subslice);
242246

243247
let comment_indent = if fix_indent {
244248
if let Some('{') = last_char {
@@ -255,7 +259,12 @@ impl<'a> FmtVisitor<'a> {
255259
// the same level and avoid mixing it with possible other comment.
256260
on_same_line = true;
257261
self.push_str(" ");
258-
self.block_indent
262+
263+
if multi_lined_block_comment {
264+
Indent::from_width(self.config, last_line_width(&self.buffer))
265+
} else {
266+
self.block_indent
267+
}
259268
} else {
260269
self.push_str(" ");
261270
Indent::from_width(self.config, last_line_width(&self.buffer))
@@ -267,7 +276,7 @@ impl<'a> FmtVisitor<'a> {
267276
);
268277
let comment_shape = Shape::legacy(comment_width, comment_indent);
269278

270-
if on_same_line {
279+
if on_same_line && !multi_lined_block_comment {
271280
match subslice.find('\n') {
272281
None => {
273282
self.push_str(subslice);

tests/source/issue-6339.rs

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// rustfmt-style_edition: 2024
2+
// rustfmt-wrap_comments: true
3+
4+
use MultiLinedBlockComment; /* a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a
5+
* b b b b b b */
6+
7+
use MultiLinedNestedBlockComment1; /* a a a a a a a /* a a a a a a a a a a a a a a a a a a a a a a a a a a a a */ a a a a
8+
* b b b b b b */
9+
10+
use MultiLinedNestedBlockComment2; /* a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a // a
11+
* b b b b b b */
12+
13+
use SingleLinedBlockComment1; /* a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a */
14+
15+
use SingleLinedBlockComment2; /* a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a */
16+
/* b b b b b b b b b b b b b b b b b b b b b b b b */
17+
18+
use MultiLinedLineComment; // a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a
19+
// b b b b b b
20+
21+
use MultiLinedBlockAndLineComment; /* a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a
22+
* b b b b b b */
23+
// c c c c c
24+
// d d d d d
25+
26+
use ShortMultiLineBlockComment; /* a a a a a a a a a a
27+
* a a a a a a a a a a
28+
* a a a a a a a a a a
29+
* a a a a a a a a a a
30+
* a a a a a a a a a a
31+
* a a a a a a a a a a
32+
* a a a a a a a a a a
33+
* a a a a a a a a a a
34+
* a a a a a a a a a a
35+
* a a a a a a a a a a
36+
* a a a a a a a a a a
37+
*/

tests/target/issue-6339.rs

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// rustfmt-style_edition: 2024
2+
// rustfmt-wrap_comments: true
3+
4+
use MultiLinedBlockComment; /* a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a
5+
* a a b b b b b b */
6+
7+
use MultiLinedNestedBlockComment1; /* a a a a a a a /* a a a a a a a a a a a a a a a a a a a a a a a a a a a a
8+
* */ a a a a b b b b b b */
9+
10+
use MultiLinedNestedBlockComment2; /* a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a
11+
* a // a b b b b b b */
12+
13+
use SingleLinedBlockComment1; /* a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a */
14+
15+
use SingleLinedBlockComment2; /* a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a */
16+
/* b b b b b b b b b b b b b b b b b b b b b b b b */
17+
18+
use MultiLinedLineComment; // a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a
19+
// b b b b b b
20+
21+
use MultiLinedBlockAndLineComment; /* a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a
22+
* a a b b b b b b */
23+
// c c c c c
24+
// d d d d d
25+
26+
use ShortMultiLineBlockComment; /* a a a a a a a a a a
27+
* a a a a a a a a a a
28+
* a a a a a a a a a a
29+
* a a a a a a a a a a
30+
* a a a a a a a a a a
31+
* a a a a a a a a a a
32+
* a a a a a a a a a a
33+
* a a a a a a a a a a
34+
* a a a a a a a a a a
35+
* a a a a a a a a a a
36+
* a a a a a a a a a a
37+
*/

0 commit comments

Comments
 (0)