Make comments multiline.

This commit is contained in:
Stephen Chung
2021-12-21 22:16:03 +08:00
parent f9f3615878
commit 7ff50451cc
7 changed files with 120 additions and 49 deletions

View File

@@ -142,27 +142,31 @@ pub fn inner_item_attributes<T: ExportedParams>(
}
#[cfg(feature = "metadata")]
pub fn doc_attribute(attrs: &mut Vec<syn::Attribute>) -> syn::Result<String> {
pub fn doc_attributes(attrs: &mut Vec<syn::Attribute>) -> syn::Result<Vec<String>> {
// Find the #[doc] attribute which will turn be read for function documentation.
let mut comments = String::new();
let mut comments = Vec::new();
while let Some(index) = attrs
.iter()
.position(|attr| attr.path.get_ident().map(|i| *i == "doc").unwrap_or(false))
{
let attr = attrs.remove(index);
let meta = attr.parse_meta()?;
match meta {
match attrs.remove(index).parse_meta()? {
syn::Meta::NameValue(syn::MetaNameValue {
lit: syn::Lit::Str(s),
..
}) => {
if !comments.is_empty() {
comments += "\n";
let mut line = s.value();
if line.contains('\n') {
// Must be a block comment `/** ... */`
line.insert_str(0, "/**");
line.push_str("*/");
} else {
// Single line - assume it is `///`
line.insert_str(0, "///");
}
comments += &s.value();
comments.push(line);
}
_ => continue,
}

View File

@@ -283,7 +283,7 @@ pub struct ExportedFn {
params: ExportedFnParams,
cfg_attrs: Vec<syn::Attribute>,
#[cfg(feature = "metadata")]
comment: String,
comments: Vec<String>,
}
impl Parse for ExportedFn {
@@ -407,7 +407,7 @@ impl Parse for ExportedFn {
params: Default::default(),
cfg_attrs,
#[cfg(feature = "metadata")]
comment: Default::default(),
comments: Default::default(),
})
}
}
@@ -508,13 +508,13 @@ impl ExportedFn {
}
#[cfg(feature = "metadata")]
pub fn comment(&self) -> &str {
&self.comment
pub fn comments(&self) -> &[String] {
&self.comments
}
#[cfg(feature = "metadata")]
pub fn set_comment(&mut self, comment: String) {
self.comment = comment
pub fn set_comments(&mut self, comments: Vec<String>) {
self.comments = comments
}
pub fn set_cfg_attrs(&mut self, cfg_attrs: Vec<syn::Attribute>) {

View File

@@ -127,7 +127,7 @@ impl Parse for Module {
f.set_cfg_attrs(crate::attrs::collect_cfg_attr(&item_fn.attrs));
#[cfg(feature = "metadata")]
f.set_comment(crate::attrs::doc_attribute(&mut item_fn.attrs)?);
f.set_comments(crate::attrs::doc_attributes(&mut item_fn.attrs)?);
Ok(f)
})?;

View File

@@ -166,14 +166,18 @@ pub fn generate_body(
);
#[cfg(feature = "metadata")]
let (param_names, comment) = (
let (param_names, comments) = (
quote! { Some(#fn_token_name::PARAM_NAMES) },
function.comment(),
function
.comments()
.iter()
.map(|s| syn::LitStr::new(s, Span::call_site()))
.collect::<Vec<_>>(),
);
#[cfg(not(feature = "metadata"))]
let (param_names, comment) = (quote! { None }, "");
let (param_names, comments) = (quote! { None }, Vec::<syn::LitStr>::new());
set_fn_statements.push(if comment.is_empty() {
set_fn_statements.push(if comments.is_empty() {
syn::parse2::<syn::Stmt>(quote! {
#(#cfg_attrs)*
m.set_fn(#fn_literal, FnNamespace::#ns_str, FnAccess::Public,
@@ -181,12 +185,10 @@ pub fn generate_body(
})
.unwrap()
} else {
let comment_literal = syn::LitStr::new(comment, Span::call_site());
syn::parse2::<syn::Stmt>(quote! {
#(#cfg_attrs)*
m.set_fn_with_comment(#fn_literal, FnNamespace::#ns_str, FnAccess::Public,
#param_names, &[#(#fn_input_types),*], #comment_literal, #fn_token_name().into());
m.set_fn_with_comments(#fn_literal, FnNamespace::#ns_str, FnAccess::Public,
#param_names, &[#(#fn_input_types),*], &[#(#comments),*], #fn_token_name().into());
})
.unwrap()
});

View File

@@ -38,7 +38,7 @@ mod module_tests {
}
#[test]
fn one_factory_fn_with_comment_module() {
fn one_factory_fn_with_comments_module() {
let input_tokens: TokenStream = quote! {
pub mod one_fn {
/// This is a doc-comment.
@@ -59,7 +59,20 @@ mod module_tests {
assert!(item_mod.consts().is_empty());
assert_eq!(item_mod.fns().len(), 1);
assert_eq!(item_mod.fns()[0].name().to_string(), "get_mystic_number");
assert_eq!(item_mod.fns()[0].comment(), " This is a doc-comment.\n Another line.\n block doc-comment \n Final line.\n doc-comment\n in multiple lines\n ");
assert_eq!(
item_mod.fns()[0]
.comments()
.iter()
.cloned()
.collect::<Vec<_>>(),
vec![
"/// This is a doc-comment.",
"/// Another line.",
"/// block doc-comment ",
"/// Final line.",
"/** doc-comment\n in multiple lines\n */"
]
);
assert_eq!(item_mod.fns()[0].arg_count(), 0);
assert_eq!(
item_mod.fns()[0].return_type().unwrap(),
@@ -354,7 +367,7 @@ mod generate_tests {
}
#[test]
fn one_factory_fn_with_comment_module() {
fn one_factory_fn_with_comments_module() {
let input_tokens: TokenStream = quote! {
pub mod one_fn {
/// This is a doc-comment.
@@ -387,8 +400,8 @@ mod generate_tests {
}
#[allow(unused_mut)]
pub fn rhai_generate_into_module(m: &mut Module, flatten: bool) {
m.set_fn_with_comment("get_mystic_number", FnNamespace::Internal, FnAccess::Public,
Some(get_mystic_number_token::PARAM_NAMES), &[], " This is a doc-comment.\n Another line.\n block doc-comment \n Final line.\n doc-comment\n in multiple lines\n ",
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.","/** doc-comment\n in multiple lines\n */"],
get_mystic_number_token().into());
if flatten {} else {}
}