Skip to content

Commit d1f6706

Browse files
authored
Exclude code block delimiters when wrapping comments (#4165)
* Exclude code block delimiters when wrapping comments Closes #4158 * fixup! Exclude code block delimiters when wrapping comments
1 parent 2836f9b commit d1f6706

File tree

3 files changed

+74
-1
lines changed

3 files changed

+74
-1
lines changed

rustfmt-core/rustfmt-lib/src/comment.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -290,15 +290,24 @@ fn identify_comment(
290290
) -> (bool, usize) {
291291
let mut first_group_ending = 0;
292292
let mut hbl = false;
293+
let mut seen_cb_delimiters = 0;
293294

294-
for line in orig.lines() {
295+
for (i, line) in orig.lines().enumerate() {
295296
let trimmed_line = line.trim_start();
296297
if trimmed_line.is_empty() {
297298
hbl = true;
298299
break;
299300
} else if trimmed_line.starts_with(line_start)
300301
|| comment_style(trimmed_line, false) == style
301302
{
303+
if line.starts_with(&format!("{}```", line_start)) {
304+
seen_cb_delimiters += 1;
305+
if seen_cb_delimiters % 2 != 0 && i != 0 {
306+
// Next line is the start of a new code block. Stop here to avoid wrapping
307+
// the delimiter up when we format the comment group.
308+
break;
309+
}
310+
}
302311
first_group_ending += compute_len(&orig[first_group_ending..], line);
303312
} else {
304313
break;
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// rustfmt-wrap_comments: true
2+
// rustfmt-max_width: 50
3+
4+
// This is a long line that will be wrapped on the next line.
5+
// This line will be wrapped with it.
6+
// ```
7+
// this is code that won't
8+
// even if it also is very very very very very very very very very very very very long
9+
// ```
10+
// This is a second long line that will be wrapped on the next line.
11+
// ```
12+
// this is code that won't
13+
// ```
14+
15+
/// This is a long line that will be wrapped on the next line.
16+
/// ```
17+
/// Should handle code blocks with no end
18+
fn outer() {
19+
//! This is a long line that will be wrapped on the next line.
20+
//! ```rust
21+
//! assert!(true);
22+
//! ```
23+
fn inner() {
24+
/* This is a long line that will be wrapped on the next line.
25+
* ```rust
26+
* assert!(true);
27+
* ```
28+
*/
29+
}
30+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// rustfmt-wrap_comments: true
2+
// rustfmt-max_width: 50
3+
4+
// This is a long line that will be wrapped on the
5+
// next line. This line will be wrapped with it.
6+
// ```
7+
// this is code that won't
8+
// even if it also is very very very very very very very very very very very very long
9+
// ```
10+
// This is a second long line that will be wrapped
11+
// on the next line.
12+
// ```
13+
// this is code that won't
14+
// ```
15+
16+
/// This is a long line that will be wrapped on
17+
/// the next line.
18+
/// ```
19+
/// Should handle code blocks with no end
20+
fn outer() {
21+
//! This is a long line that will be wrapped
22+
//! on the next line.
23+
//! ```rust
24+
//! assert!(true);
25+
//! ```
26+
fn inner() {
27+
/* This is a long line that will be
28+
* wrapped on the next line.
29+
* ```rust
30+
* assert!(true);
31+
* ```
32+
*/
33+
}
34+
}

0 commit comments

Comments
 (0)