Skip to content
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 15 additions & 5 deletions src/comment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -491,14 +491,15 @@ impl ItemizedBlock {
let mut line_start = " ".repeat(indent);

// Markdown blockquote start with a "> "
if line.trim_start().starts_with('>') {
if line.trim_start().starts_with("> ") {
// remove the original +2 indent because there might be multiple nested block quotes
// and it's easier to reason about the final indent by just taking the length
// of the new line_start. We update the indent because it effects the max width
// of each formatted line.
line_start = itemized_block_quote_start(line, line_start, 2);
indent = line_start.len();
}

Some(ItemizedBlock {
lines: vec![line[indent..].to_string()],
indent,
Expand Down Expand Up @@ -551,10 +552,18 @@ impl ItemizedBlock {
/// The original line_start likely contains indentation (whitespaces), which we'd like to
/// replace with '> ' characters.
fn itemized_block_quote_start(line: &str, mut line_start: String, remove_indent: usize) -> String {
let quote_level = line
.chars()
.take_while(|c| !c.is_alphanumeric())
.fold(0, |acc, c| if c == '>' { acc + 1 } else { acc });
let mut quote_level = 0;
let mut chars = line.trim_start().chars().peekable();

while chars.peek() == Some(&'>') {
chars.next();
if chars.peek() == Some(&' ') {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it can be multiple spaces, yes?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should have tests for the nested blockquote code that include quotes with multiple spaces (if that does indeed work in rust's markdown)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and if it's multiple spaces then we can't just do line[indent..] above, we actually need to count

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, cases like >>text or > > text are actually valid syntax (verified via the GitHub Markdown).

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For now, I’ve addressed this issue based on the original design, but we should extend support for these syntax variations in the future.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If possible, I’d like to add support for them in a follow-up PR, since doing so would introduce a breaking change to the existing logic.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The existing logic counts > regardless of spaces, so it should have been handling it, yes?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You’re right — that was probably the original design intention. However, the line_start.push_str("> ") part misled me, let me improve it.

chars.next();
quote_level += 1;
} else {
break;
}
}

for _ in 0..remove_indent {
line_start.pop();
Expand All @@ -563,6 +572,7 @@ fn itemized_block_quote_start(line: &str, mut line_start: String, remove_indent:
for _ in 0..quote_level {
line_start.push_str("> ")
}

line_start
}

Expand Down
4 changes: 4 additions & 0 deletions tests/source/issue-6660.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// rustfmt-wrap_comments: true

// > >= >> >>= >>> >>>=
fn block_quote() {}
4 changes: 4 additions & 0 deletions tests/target/issue-6660.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// rustfmt-wrap_comments: true

// > >= >> >>= >>> >>>=
fn block_quote() {}
Loading