Merge multiple doc-comment lines into one.

This commit is contained in:
Stephen Chung
2022-12-01 14:35:45 +08:00
parent 28640a6fe4
commit 0c4935febb
8 changed files with 120 additions and 91 deletions

View File

@@ -145,6 +145,7 @@ pub fn inner_item_attributes<T: ExportedParams>(
pub fn doc_attributes(attrs: &[syn::Attribute]) -> syn::Result<Vec<String>> {
// Find the #[doc] attribute which will turn be read for function documentation.
let mut comments = Vec::new();
let mut buf = String::new();
for attr in attrs {
if let Some(i) = attr.path.get_ident() {
@@ -158,19 +159,30 @@ pub fn doc_attributes(attrs: &[syn::Attribute]) -> syn::Result<Vec<String>> {
if line.contains('\n') {
// Must be a block comment `/** ... */`
if !buf.is_empty() {
comments.push(buf.clone());
buf.clear();
}
line.insert_str(0, "/**");
line.push_str("*/");
comments.push(line);
} else {
// Single line - assume it is `///`
line.insert_str(0, "///");
if !buf.is_empty() {
buf.push('\n');
}
buf.push_str("///");
buf.push_str(&line);
}
comments.push(line);
}
}
}
}
if !buf.is_empty() {
comments.push(buf);
}
Ok(comments)
}

View File

@@ -230,7 +230,7 @@ pub fn generate_body(
syn::parse2::<syn::Stmt>(quote! {
#(#cfg_attrs)*
m.set_fn_with_comments(#fn_literal, FnNamespace::#ns_str, FnAccess::Public,
#param_names, &[#(#fn_input_types),*], &[#(#comments),*], #fn_token_name().into());
#param_names, [#(#fn_input_types),*], [#(#comments),*], #fn_token_name().into());
})
.unwrap()
});

View File

@@ -92,10 +92,11 @@ mod module_tests {
.cloned()
.collect::<Vec<_>>(),
vec![
"/// This is a doc-comment.",
"/// Another line.",
"/// block doc-comment ",
"/// Final line.",
"\
/// This is a doc-comment.\n\
/// Another line.\n\
/// block doc-comment \n\
/// Final line.",
"/** doc-comment\n in multiple lines\n */"
]
);
@@ -444,11 +445,8 @@ mod generate_tests {
#[doc(hidden)]
pub fn rhai_generate_into_module(m: &mut Module, flatten: bool) {
m.set_fn_with_comments("get_mystic_number", FnNamespace::Internal, FnAccess::Public,
Some(get_mystic_number_token::PARAM_NAMES), &[], &[
"/// This is a doc-comment.",
"/// Another line.",
"/// block doc-comment ",
"/// Final line.",
Some(get_mystic_number_token::PARAM_NAMES), [], [
"/// This is a doc-comment.\n/// Another line.\n/// block doc-comment \n/// Final line.",
"/** doc-comment\n in multiple lines\n */"
], get_mystic_number_token().into());
if flatten {} else {}