Skip to content

Commit 466ac82

Browse files
fix(iota-open-rpc-macros): proper handling of empty line doc comments (#5495)
* fix fn extract_doc_comments * Update crates/iota-open-rpc-macros/src/lib.rs * fmt --------- Co-authored-by: Thibault Martinez <[email protected]>
1 parent cc81b74 commit 466ac82

File tree

1 file changed

+36
-14
lines changed
  • crates/iota-open-rpc-macros/src

1 file changed

+36
-14
lines changed

crates/iota-open-rpc-macros/src/lib.rs

Lines changed: 36 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -336,21 +336,43 @@ fn respan_token_stream(stream: TokenStream2, span: Span) -> TokenStream2 {
336336
.collect()
337337
}
338338

339+
/// Find doc comments by looking for #[doc = "..."] attributes.
340+
///
341+
/// Consecutive attributes are combined together. If there is a leading space,
342+
/// it will be removed, and if there is trailing whitespace it will also be
343+
/// removed. Single newlines in doc comments are replaced by spaces (soft
344+
/// wrapping), but double newlines (an empty line) are preserved.
339345
fn extract_doc_comments(attrs: &[Attribute]) -> String {
340-
let s = attrs
341-
.iter()
342-
.filter(|attr| {
343-
attr.path.is_ident("doc")
344-
&& match attr.parse_meta() {
345-
Ok(syn::Meta::NameValue(meta)) => matches!(&meta.lit, syn::Lit::Str(_)),
346-
_ => false,
347-
}
348-
})
349-
.map(|attr| {
350-
let s = attr.tokens.to_string();
351-
s[4..s.len() - 1].to_string()
352-
})
353-
.join(" ");
346+
let mut s = String::new();
347+
let mut sep = "";
348+
349+
for attr in attrs {
350+
if !attr.path.is_ident("doc") {
351+
continue;
352+
}
353+
354+
let Ok(syn::Meta::NameValue(meta)) = attr.parse_meta() else {
355+
continue;
356+
};
357+
358+
let syn::Lit::Str(lit) = &meta.lit else {
359+
continue;
360+
};
361+
362+
let token = lit.value();
363+
let line = token.strip_prefix(" ").unwrap_or(&token).trim_end();
364+
365+
if line.is_empty() {
366+
s.push_str("\n\n");
367+
sep = "";
368+
} else {
369+
s.push_str(sep);
370+
sep = " ";
371+
}
372+
373+
s.push_str(line);
374+
}
375+
354376
unescape(&s).unwrap_or_else(|| panic!("Cannot unescape doc comments : [{s}]"))
355377
}
356378

0 commit comments

Comments
 (0)