Skip to content

Commit 3d1b831

Browse files
committed
fix(items): Take into account outer documentation blocks
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.
1 parent 277cb90 commit 3d1b831

File tree

2 files changed

+38
-2
lines changed

2 files changed

+38
-2
lines changed

src/items.rs

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -654,12 +654,47 @@ impl<'a> FmtVisitor<'a> {
654654
return variant_str.contains('\n');
655655
}
656656

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

662662
let mut variant_str = lines.join("\n");
663+
664+
// Then exclude all outer documentation blocks
665+
// Exclude one block per loop iteration
666+
loop {
667+
let mut block_found = false;
668+
let mut chars = variant_str.chars().enumerate();
669+
'block: while let Some((i, c)) = chars.next() {
670+
if c != '/' {
671+
continue;
672+
}
673+
let block_start = i;
674+
if let Some((_, '*')) = chars.next() {
675+
if let Some((_, '*')) = chars.next() {
676+
while let Some((_, c)) = chars.next() {
677+
if c == '*' {
678+
if let Some((i, '/')) = chars.next() {
679+
// block was found and ends at the i-th position
680+
// We remove it from variant_str
681+
let mut s = variant_str[..block_start].trim().to_owned();
682+
s.push_str(variant_str[(i + 1)..].trim());
683+
variant_str = s;
684+
block_found = true;
685+
break 'block;
686+
}
687+
}
688+
}
689+
}
690+
}
691+
}
692+
693+
if !block_found {
694+
break;
695+
}
696+
}
697+
663698
// Skip macro attributes in variant_str
664699
// We skip one macro attribute per loop iteration
665700
loop {

tests/target/attribute-in-enum/horizontal-with-doc.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
enum MyType {
33
A { field1: bool, field2: bool },
44
B { field1: bool, field2: bool },
5-
/// OMG a comment
5+
/// One-line doc comment
66
C { field1: bool, field2: bool },
7+
/** Documentation block */
78
D { field1: bool, field2: bool },
89
}

0 commit comments

Comments
 (0)