Skip to content

Commit a443182

Browse files
committed
u
1 parent 7895507 commit a443182

File tree

3 files changed

+65
-47
lines changed

3 files changed

+65
-47
lines changed

src/comment.rs

Lines changed: 45 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -460,11 +460,15 @@ impl ItemizedBlock {
460460
/// 1868. He wes buried in...
461461
/// ```
462462
fn get_marker_length(trimmed: &str) -> Option<usize> {
463+
if trimmed.starts_with('>') {
464+
return Some(0);
465+
}
466+
463467
// https://spec.commonmark.org/0.30/#bullet-list-marker or
464468
// https://spec.commonmark.org/0.30/#block-quote-marker
465-
let itemized_start = ["* ", "- ", ">", "+ "];
466-
if let Some(s) = itemized_start.iter().find(|&&s| trimmed.starts_with(s)) {
467-
return Some(s.len());
469+
let itemized_start = ["* ", "- ", "+ "];
470+
if itemized_start.iter().any(|&s| trimmed.starts_with(s)) {
471+
return Some(2);
468472
}
469473

470474
// https://spec.commonmark.org/0.30/#ordered-list-marker, where at most 2 digits are
@@ -485,25 +489,47 @@ impl ItemizedBlock {
485489
/// Creates a new `ItemizedBlock` described with the given `line`.
486490
/// Returns `None` if `line` doesn't start an item.
487491
fn new(line: &str) -> Option<ItemizedBlock> {
488-
let marker_length = ItemizedBlock::get_marker_length(line.trim_start())?;
489-
let space_to_marker = line.chars().take_while(|c| c.is_whitespace()).count();
490-
let mut indent = space_to_marker + marker_length;
491-
let mut line_start = " ".repeat(indent);
492-
493-
// Markdown blockquote start with a "> "
494-
if line.trim_start().starts_with('>') {
495-
// remove the original +1 indent because there might be multiple nested block quotes
496-
// and it's easier to reason about the final indent by just taking the length
497-
// of the new line_start. We update the indent because it effects the max width
498-
// of each formatted line.
499-
line_start = itemized_block_quote_start(line, line_start, 1);
500-
indent = line_start.len();
492+
let trimmed = line.trim_start();
493+
let marker_length = ItemizedBlock::get_marker_length(trimmed)?;
494+
let space_to_marker = line
495+
.chars()
496+
.take_while(|&c| c == ' ' || c == '\t')
497+
.fold(0, |acc, c| acc + if c == ' ' { 1 } else { 4 });
498+
499+
let mut line_start = " ".repeat(space_to_marker + marker_length);
500+
let mut i = line.len() - trimmed.len() + marker_length;
501+
502+
// Markdown blockquote start with a '>'
503+
if trimmed.starts_with('>') {
504+
// Determine the line_start when formatting markdown block quotes.
505+
// The original line_start likely contains indentation (whitespaces),
506+
// which we'd like to replace with '> ' characters.
507+
let mut quote_level = 0;
508+
509+
let bytes = line.as_bytes();
510+
while i < bytes.len() && bytes[i] == b'>' {
511+
i += 1;
512+
quote_level += 1;
513+
514+
let mut whitespace_width = 0;
515+
while i < bytes.len() && (bytes[i] == b' ' || bytes[i] == b'\t') {
516+
whitespace_width += if bytes[i] == b' ' { 1 } else { 4 };
517+
if whitespace_width > 4 {
518+
break;
519+
}
520+
i += 1;
521+
}
522+
}
523+
524+
for _ in 0..quote_level {
525+
line_start.push_str("> ");
526+
}
501527
}
502528

503529
Some(ItemizedBlock {
504-
lines: vec![line[indent..].to_string()],
505-
indent,
506-
opener: line[..indent].to_string(),
530+
lines: vec![line[i..].to_string()],
531+
indent: line_start.len(),
532+
opener: line[..i].to_string(),
507533
line_start,
508534
})
509535
}
@@ -548,27 +574,6 @@ impl ItemizedBlock {
548574
}
549575
}
550576

551-
/// Determine the line_start when formatting markdown block quotes.
552-
/// The original line_start likely contains indentation (whitespaces), which we'd like to
553-
/// replace with '> ' characters.
554-
fn itemized_block_quote_start(line: &str, mut line_start: String, remove_indent: usize) -> String {
555-
let quote_level = line
556-
.chars()
557-
.take_while(|&c| matches!(c, '>' | ' '))
558-
.filter(|&c| c == '>')
559-
.count();
560-
561-
for _ in 0..remove_indent {
562-
line_start.pop();
563-
}
564-
565-
for _ in 0..quote_level {
566-
line_start.push_str("> ");
567-
}
568-
569-
line_start
570-
}
571-
572577
struct CommentRewrite<'a> {
573578
result: String,
574579
code_block_buffer: String,

tests/source/itemized-blocks/wrap.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,3 +96,8 @@ fn func3() {}
9696
/// > >= >> >>= >>> >>>= This is a blockquote comment that is very long and should be wrapped according to max_width setting
9797
/// > > = >> >>= >>> >>>= This is a blockquote comment that is very long and should be wrapped according to max_width setting
9898
fn func4() {}
99+
100+
/// > >This is a blockquote comment that is very long and should be wrapped according to max_width setting
101+
/// > > This is a blockquote comment that is very long and should be wrapped according to max_width setting
102+
fn func5() {}
103+

tests/target/itemized-blocks/wrap.rs

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -157,16 +157,24 @@ fn func3() {}
157157
/// > >This is a blockquote comment that is very
158158
/// > > long and should be wrapped according to
159159
/// > > max_width setting
160-
/// > > This is a blockquote comment that is
161-
/// > > very long and should be wrapped according
162-
/// > > to max_width setting
163-
/// > > This is a blockquote comment that is
164-
/// > > very long and should be wrapped according
165-
/// > > to max_width setting
166-
/// > >=>> >>= >>> >>>= This is a blockquote
160+
/// > > This is a blockquote comment that is very
161+
/// > > long and should be wrapped according to
162+
/// > > max_width setting
163+
/// > > This is a blockquote comment that is very
164+
/// > > long and should be wrapped according to
165+
/// > > max_width setting
166+
/// > >= >> >>= >>> >>>= This is a blockquote
167167
/// > > comment that is very long and should be
168168
/// > > wrapped according to max_width setting
169169
/// > > = >> >>= >>> >>>= This is a blockquote
170170
/// > > comment that is very long and should be
171171
/// > > wrapped according to max_width setting
172172
fn func4() {}
173+
174+
/// > >This is a blockquote comment that is very
175+
/// > long and should be wrapped according to
176+
/// > max_width setting
177+
/// > > This is a blockquote comment that is very
178+
/// > > long and should be wrapped according to
179+
/// > > max_width setting
180+
fn func5() {}

0 commit comments

Comments
 (0)