Do not remove doc-comments on pluging functions.

This commit is contained in:
Stephen Chung 2022-01-20 09:06:09 +08:00
parent f92894e337
commit 6b06019265
5 changed files with 63 additions and 37 deletions

View File

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

View File

@ -407,7 +407,7 @@ impl Parse for ExportedFn {
params: Default::default(), params: Default::default(),
cfg_attrs, cfg_attrs,
#[cfg(feature = "metadata")] #[cfg(feature = "metadata")]
comments: Default::default(), comments: Vec::new(),
}) })
} }
} }

View File

@ -127,7 +127,7 @@ impl Parse for Module {
f.set_cfg_attrs(crate::attrs::collect_cfg_attr(&item_fn.attrs)); f.set_cfg_attrs(crate::attrs::collect_cfg_attr(&item_fn.attrs));
#[cfg(feature = "metadata")] #[cfg(feature = "metadata")]
f.set_comments(crate::attrs::doc_attributes(&mut item_fn.attrs)?); f.set_comments(crate::attrs::doc_attributes(&item_fn.attrs)?);
Ok(f) Ok(f)
})?; })?;
@ -144,12 +144,12 @@ impl Parse for Module {
attrs, attrs,
ty, ty,
.. ..
}) if matches!(vis, syn::Visibility::Public(_)) => consts.push(( }) if matches!(vis, syn::Visibility::Public(_)) => consts.push(ExportedConst {
ident.to_string(), name: ident.to_string(),
ty.clone(), typ: ty.clone(),
expr.as_ref().clone(), expr: expr.as_ref().clone(),
crate::attrs::collect_cfg_attr(&attrs), cfg_attrs: crate::attrs::collect_cfg_attr(&attrs),
)), }),
_ => {} _ => {}
} }
} }

View File

@ -10,7 +10,13 @@ use crate::function::{
}; };
use crate::module::Module; use crate::module::Module;
pub type ExportedConst = (String, Box<syn::Type>, syn::Expr, Vec<syn::Attribute>); #[derive(Debug)]
pub struct ExportedConst {
pub name: String,
pub typ: Box<syn::Type>,
pub expr: syn::Expr,
pub cfg_attrs: Vec<syn::Attribute>,
}
pub fn generate_body( pub fn generate_body(
fns: &mut [ExportedFn], fns: &mut [ExportedFn],
@ -25,7 +31,12 @@ pub fn generate_body(
let str_type_path = syn::parse2::<syn::Path>(quote! { str }).unwrap(); let str_type_path = syn::parse2::<syn::Path>(quote! { str }).unwrap();
let string_type_path = syn::parse2::<syn::Path>(quote! { String }).unwrap(); let string_type_path = syn::parse2::<syn::Path>(quote! { String }).unwrap();
for (const_name, _, _, cfg_attrs) in consts { for ExportedConst {
name: const_name,
cfg_attrs,
..
} in consts
{
let const_literal = syn::LitStr::new(&const_name, Span::call_site()); let const_literal = syn::LitStr::new(&const_name, Span::call_site());
let const_ref = syn::Ident::new(&const_name, Span::call_site()); let const_ref = syn::Ident::new(&const_name, Span::call_site());

View File

@ -38,6 +38,7 @@ mod module_tests {
} }
#[test] #[test]
#[cfg(feature = "metadata")]
fn one_factory_fn_with_comments_module() { fn one_factory_fn_with_comments_module() {
let input_tokens: TokenStream = quote! { let input_tokens: TokenStream = quote! {
pub mod one_fn { pub mod one_fn {
@ -150,9 +151,9 @@ mod module_tests {
assert!(item_mod.fns().is_empty()); assert!(item_mod.fns().is_empty());
assert!(item_mod.consts().is_empty()); assert!(item_mod.consts().is_empty());
assert_eq!(item_mod.sub_modules().len(), 1); assert_eq!(item_mod.sub_modules().len(), 1);
assert_eq!(&item_mod.sub_modules()[0].consts()[0].0, "MYSTIC_NUMBER"); assert_eq!(&item_mod.sub_modules()[0].consts()[0].name, "MYSTIC_NUMBER");
assert_eq!( assert_eq!(
item_mod.sub_modules()[0].consts()[0].2, item_mod.sub_modules()[0].consts()[0].expr,
syn::parse2::<syn::Expr>(quote! { 42 }).unwrap() syn::parse2::<syn::Expr>(quote! { 42 }).unwrap()
); );
} }
@ -211,9 +212,9 @@ mod module_tests {
let item_mod = syn::parse2::<Module>(input_tokens).unwrap(); let item_mod = syn::parse2::<Module>(input_tokens).unwrap();
assert!(item_mod.fns().is_empty()); assert!(item_mod.fns().is_empty());
assert_eq!(item_mod.consts().len(), 1); assert_eq!(item_mod.consts().len(), 1);
assert_eq!(&item_mod.consts()[0].0, "MYSTIC_NUMBER"); assert_eq!(&item_mod.consts()[0].name, "MYSTIC_NUMBER");
assert_eq!( assert_eq!(
item_mod.consts()[0].2, item_mod.consts()[0].expr,
syn::parse2::<syn::Expr>(quote! { 42 }).unwrap() syn::parse2::<syn::Expr>(quote! { 42 }).unwrap()
); );
} }
@ -386,6 +387,14 @@ mod generate_tests {
let expected_tokens = quote! { let expected_tokens = quote! {
pub mod one_fn { pub mod one_fn {
/// This is a doc-comment.
/// Another line.
/** block doc-comment */
// Regular comment
/// Final line.
/** doc-comment
in multiple lines
*/
pub fn get_mystic_number() -> INT { pub fn get_mystic_number() -> INT {
42 42
} }
@ -401,8 +410,13 @@ mod generate_tests {
#[allow(unused_mut)] #[allow(unused_mut)]
pub fn rhai_generate_into_module(m: &mut Module, flatten: bool) { pub fn rhai_generate_into_module(m: &mut Module, flatten: bool) {
m.set_fn_with_comments("get_mystic_number", FnNamespace::Internal, FnAccess::Public, 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 */"], Some(get_mystic_number_token::PARAM_NAMES), &[], &[
get_mystic_number_token().into()); "/// 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 {} if flatten {} else {}
} }
#[allow(non_camel_case_types)] #[allow(non_camel_case_types)]