Skip to content

Commit 35e4606

Browse files
committed
Fix panic on blockquote
1 parent 0332da0 commit 35e4606

File tree

3 files changed

+23
-5
lines changed

3 files changed

+23
-5
lines changed

src/comment.rs

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -491,14 +491,15 @@ impl ItemizedBlock {
491491
let mut line_start = " ".repeat(indent);
492492

493493
// Markdown blockquote start with a "> "
494-
if line.trim_start().starts_with('>') {
494+
if line.trim_start().starts_with("> ") {
495495
// remove the original +2 indent because there might be multiple nested block quotes
496496
// and it's easier to reason about the final indent by just taking the length
497497
// of the new line_start. We update the indent because it effects the max width
498498
// of each formatted line.
499499
line_start = itemized_block_quote_start(line, line_start, 2);
500500
indent = line_start.len();
501501
}
502+
502503
Some(ItemizedBlock {
503504
lines: vec![line[indent..].to_string()],
504505
indent,
@@ -551,10 +552,18 @@ impl ItemizedBlock {
551552
/// The original line_start likely contains indentation (whitespaces), which we'd like to
552553
/// replace with '> ' characters.
553554
fn itemized_block_quote_start(line: &str, mut line_start: String, remove_indent: usize) -> String {
554-
let quote_level = line
555-
.chars()
556-
.take_while(|c| !c.is_alphanumeric())
557-
.fold(0, |acc, c| if c == '>' { acc + 1 } else { acc });
555+
let mut quote_level = 0;
556+
let mut chars = line.trim_start().chars().peekable();
557+
558+
while chars.peek() == Some(&'>') {
559+
chars.next();
560+
if chars.peek() == Some(&' ') {
561+
chars.next();
562+
quote_level += 1;
563+
} else {
564+
break;
565+
}
566+
}
558567

559568
for _ in 0..remove_indent {
560569
line_start.pop();
@@ -563,6 +572,7 @@ fn itemized_block_quote_start(line: &str, mut line_start: String, remove_indent:
563572
for _ in 0..quote_level {
564573
line_start.push_str("> ")
565574
}
575+
566576
line_start
567577
}
568578

tests/source/issue-6660.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// rustfmt-wrap_comments: true
2+
3+
// > >= >> >>= >>> >>>=
4+
fn block_quote() {}

tests/target/issue-6660.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// rustfmt-wrap_comments: true
2+
3+
// > >= >> >>= >>> >>>=
4+
fn block_quote() {}

0 commit comments

Comments
 (0)