Add doc-comments on plugin modules to Module::doc field.

This commit is contained in:
Stephen Chung 2022-12-01 23:29:42 +08:00
parent ed423740c9
commit fcc363af60
5 changed files with 36 additions and 2 deletions

View File

@ -44,6 +44,7 @@ Enhancements
* `Engine::set_XXX` API can now be chained. * `Engine::set_XXX` API can now be chained.
* `EvalContext::scope_mut` now returns `&mut Scope` instead of `&mut &mut Scope`. * `EvalContext::scope_mut` now returns `&mut Scope` instead of `&mut &mut Scope`.
* Line-style doc-comments are now merged into a single string to avoid creating many strings. Block-style doc-comments continue to be independent strings. * Line-style doc-comments are now merged into a single string to avoid creating many strings. Block-style doc-comments continue to be independent strings.
* Doc-comments on plugin modules are now captured in the module's `doc` field.
Version 1.11.0 Version 1.11.0

View File

@ -1,11 +1,11 @@
[package] [package]
name = "rhai_codegen" name = "rhai_codegen"
version = "1.4.3" version = "1.4.4"
edition = "2018" edition = "2018"
resolver = "2" resolver = "2"
authors = ["jhwgh1968", "Stephen Chung"] authors = ["jhwgh1968", "Stephen Chung"]
description = "Procedural macros support package for Rhai, a scripting language and engine for Rust" description = "Procedural macros support package for Rhai, a scripting language and engine for Rust"
keywords = ["rhai", "scripting", "scripting-engine", "scripting-language", "embedded", "plugin", "macros", "code-generation"] keywords = ["scripting", "scripting-engine", "scripting-language", "embedded", "plugin"]
categories = ["no-std", "embedded", "wasm", "parser-implementations"] categories = ["no-std", "embedded", "wasm", "parser-implementations"]
homepage = "https://rhai.rs/book/plugins/index.html" homepage = "https://rhai.rs/book/plugins/index.html"
repository = "https://github.com/rhaiscript/rhai" repository = "https://github.com/rhaiscript/rhai"

View File

@ -106,6 +106,7 @@ impl Module {
impl Parse for Module { impl Parse for Module {
fn parse(input: ParseStream) -> syn::Result<Self> { fn parse(input: ParseStream) -> syn::Result<Self> {
let mut mod_all: syn::ItemMod = input.parse()?; let mut mod_all: syn::ItemMod = input.parse()?;
let fns: Vec<_>; let fns: Vec<_>;
let mut consts = Vec::new(); let mut consts = Vec::new();
let mut custom_types = Vec::new(); let mut custom_types = Vec::new();
@ -269,11 +270,17 @@ impl Module {
let (.., orig_content) = mod_all.content.take().unwrap(); let (.., orig_content) = mod_all.content.take().unwrap();
let mod_attrs = mem::take(&mut mod_all.attrs); let mod_attrs = mem::take(&mut mod_all.attrs);
#[cfg(feature = "metadata")]
let mod_doc = crate::attrs::doc_attributes(&mod_attrs)?.join("\n");
#[cfg(not(feature = "metadata"))]
let mod_doc = String::new();
if !params.skip { if !params.skip {
// Generate new module items. // Generate new module items.
// //
// This is done before inner module recursive generation, because that is destructive. // This is done before inner module recursive generation, because that is destructive.
let mod_gen = crate::rhai_module::generate_body( let mod_gen = crate::rhai_module::generate_body(
&mod_doc,
&mut fns, &mut fns,
&consts, &consts,
&custom_types, &custom_types,

View File

@ -26,6 +26,7 @@ pub struct ExportedType {
} }
pub fn generate_body( pub fn generate_body(
doc: &str,
fns: &mut [ExportedFn], fns: &mut [ExportedFn],
consts: &[ExportedConst], consts: &[ExportedConst],
custom_types: &[ExportedType], custom_types: &[ExportedType],
@ -246,6 +247,17 @@ pub fn generate_body(
gen_fn_tokens.push(function.generate_impl(&fn_token_name.to_string())); gen_fn_tokens.push(function.generate_impl(&fn_token_name.to_string()));
} }
let module_docs = if doc.is_empty() {
None
} else {
Some(
syn::parse2::<syn::Stmt>(quote! {
m.set_doc(#doc);
})
.unwrap(),
)
};
let mut generate_fn_call = syn::parse2::<syn::ItemMod>(quote! { let mut generate_fn_call = syn::parse2::<syn::ItemMod>(quote! {
pub mod generate_info { pub mod generate_info {
#[allow(unused_imports)] #[allow(unused_imports)]
@ -254,6 +266,7 @@ pub fn generate_body(
#[doc(hidden)] #[doc(hidden)]
pub fn rhai_module_generate() -> Module { pub fn rhai_module_generate() -> Module {
let mut m = Module::new(); let mut m = Module::new();
#module_docs
rhai_generate_into_module(&mut m, false); rhai_generate_into_module(&mut m, false);
m.build_index(); m.build_index();
m m

View File

@ -403,6 +403,12 @@ mod generate_tests {
#[test] #[test]
fn one_factory_fn_with_comments_module() { fn one_factory_fn_with_comments_module() {
let input_tokens: TokenStream = quote! { let input_tokens: TokenStream = quote! {
/// This is the one_fn module!
/** block doc-comment
* multi-line
*/
/// Another line!
/// Final line!
pub mod one_fn { pub mod one_fn {
/// This is a doc-comment. /// This is a doc-comment.
/// Another line. /// Another line.
@ -419,6 +425,12 @@ mod generate_tests {
}; };
let expected_tokens = quote! { let expected_tokens = quote! {
/// This is the one_fn module!
/** block doc-comment
* multi-line
*/
/// Another line!
/// Final line!
pub mod one_fn { pub mod one_fn {
/// This is a doc-comment. /// This is a doc-comment.
/// Another line. /// Another line.
@ -437,6 +449,7 @@ mod generate_tests {
#[doc(hidden)] #[doc(hidden)]
pub fn rhai_module_generate() -> Module { pub fn rhai_module_generate() -> Module {
let mut m = Module::new(); let mut m = Module::new();
m.set_doc("/// This is the one_fn module!\n/** block doc-comment\n * multi-line\n */\n/// Another line!\n/// Final line!");
rhai_generate_into_module(&mut m, false); rhai_generate_into_module(&mut m, false);
m.build_index(); m.build_index();
m m