Skip to content

Commit 07106d4

Browse files
authored
Merge pull request #2222 from topecongiro/issue-2197
Do not format fenced code blocks in comment
2 parents 36d1bd7 + b9126fa commit 07106d4

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

src/comment.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,7 @@ fn rewrite_comment_inner(
319319

320320
let mut result = opener.to_owned();
321321
let mut is_prev_line_multi_line = false;
322+
let mut inside_code_block = false;
322323
let comment_line_separator = format!("\n{}{}", indent_str, line_start);
323324
for line in lines {
324325
if result == opener {
@@ -331,6 +332,14 @@ fn rewrite_comment_inner(
331332
result.push_str(&comment_line_separator);
332333
}
333334

335+
if line.starts_with("```") {
336+
inside_code_block = !inside_code_block;
337+
}
338+
if inside_code_block {
339+
result.push_str(line);
340+
continue;
341+
}
342+
334343
if config.wrap_comments() && line.len() > fmt.shape.width && !has_url(line) {
335344
match rewrite_string(line, &fmt, Some(max_chars)) {
336345
Some(ref s) => {

tests/target/issue-2197.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// rustfmt-max_width: 79
2+
// rustfmt-wrap_comments: true
3+
// rustfmt-error_on_line_overflow: false
4+
5+
/// ```rust
6+
/// unsafe fn sum_sse2(x: i32x4) -> i32 {
7+
/// let x = vendor::_mm_add_epi32(x, vendor::_mm_srli_si128(x.into(), 8).into());
8+
/// let x = vendor::_mm_add_epi32(x, vendor::_mm_srli_si128(x.into(), 4).into());
9+
/// vendor::_mm_cvtsi128_si32(x)
10+
/// }
11+
/// ```
12+
fn foo() {}

0 commit comments

Comments
 (0)