Skip to content

Commit

Permalink
fix(items): Take into account outer documentation blocks
Browse files Browse the repository at this point in the history
We properly handle outer documentation blocks in front of an enum
variant.

With this commit, we believe we have handled all edge-cases regarding
attributes in front of enum variants.
  • Loading branch information
malikolivier committed Aug 21, 2024
1 parent 277cb90 commit 3d1b831
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 2 deletions.
37 changes: 36 additions & 1 deletion src/items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -654,12 +654,47 @@ impl<'a> FmtVisitor<'a> {
return variant_str.contains('\n');
}

// First exclude all doc comments
// First exclude all outer one-line doc comments
let mut lines = variant_str
.split('\n')
.filter(|line| !line.trim().starts_with("///"));

let mut variant_str = lines.join("\n");

// Then exclude all outer documentation blocks
// Exclude one block per loop iteration
loop {
let mut block_found = false;
let mut chars = variant_str.chars().enumerate();
'block: while let Some((i, c)) = chars.next() {
if c != '/' {
continue;
}
let block_start = i;
if let Some((_, '*')) = chars.next() {
if let Some((_, '*')) = chars.next() {
while let Some((_, c)) = chars.next() {
if c == '*' {
if let Some((i, '/')) = chars.next() {
// block was found and ends at the i-th position
// We remove it from variant_str
let mut s = variant_str[..block_start].trim().to_owned();
s.push_str(variant_str[(i + 1)..].trim());
variant_str = s;
block_found = true;
break 'block;
}
}
}
}
}
}

if !block_found {
break;
}
}

// Skip macro attributes in variant_str
// We skip one macro attribute per loop iteration
loop {
Expand Down
3 changes: 2 additions & 1 deletion tests/target/attribute-in-enum/horizontal-with-doc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
enum MyType {
A { field1: bool, field2: bool },
B { field1: bool, field2: bool },
/// OMG a comment
/// One-line doc comment
C { field1: bool, field2: bool },
/** Documentation block */
D { field1: bool, field2: bool },
}

0 comments on commit 3d1b831

Please sign in to comment.