Skip to content

Commit

Permalink
method descriptions
Browse files Browse the repository at this point in the history
  • Loading branch information
TitanNano committed Nov 20, 2023
1 parent 542aaa9 commit e36235c
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 11 deletions.
12 changes: 11 additions & 1 deletion derive/src/impl_attribute.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use proc_macro2::TokenStream;
use quote::{quote, quote_spanned};
use quote::{quote, quote_spanned, ToTokens};
use syn::{parse_macro_input, spanned::Spanned, FnArg, ImplItem, ItemImpl, ReturnType, Type};

use crate::{
Expand Down Expand Up @@ -95,6 +95,15 @@ pub fn godot_script_impl(
quote!(#godot_types::engine::global::MethodFlags::METHOD_FLAG_NORMAL)
};

let description = fnc.attrs.iter()
.filter(|attr| attr.path().is_ident("doc"))
.map(|attr| attr.meta.require_name_value().unwrap().value.to_token_stream())
.reduce(|mut acc, ident| {
acc.extend(quote!(, "\n", ));
acc.extend(ident);
acc
});

let metadata = quote_spanned! {
fnc.span() =>
::godot_rust_script::RustScriptMethodDesc {
Expand All @@ -109,6 +118,7 @@ pub fn godot_script_impl(
description: "",
},
flags: #method_flag,
description: concat!(#description),
},
};

Expand Down
2 changes: 2 additions & 0 deletions rust-script/src/library.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ pub struct RustScriptMethodDesc {
pub return_type: RustScriptPropDesc,
pub arguments: Vec<RustScriptPropDesc>,
pub flags: MethodFlags,
pub description: &'static str,
}

impl RustScriptMethodDesc {
Expand All @@ -150,6 +151,7 @@ impl RustScriptMethodDesc {
.into_iter()
.map(|arg| arg.into_property_info(class_name))
.collect(),
description: RStr::from_str(self.description),
}
}
}
Expand Down
37 changes: 29 additions & 8 deletions rust-script/src/runtime/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ pub struct ScriptMetaData {
base_type_name: StringName,
properties_documented: Vec<Documented<PropertyInfo>>,
properties: Rc<Vec<PropertyInfo>>,
methods_documented: Vec<Documented<MethodInfo>>,
methods: Rc<Vec<MethodInfo>>,
create_data: CreateScriptInstanceData_TO<'static, RBox<()>>,
description: &'static str,
Expand Down Expand Up @@ -51,25 +52,28 @@ impl ScriptMetaData {
self.methods.clone()
}

pub fn methods_documented(&self) -> &[Documented<MethodInfo>] {
&self.methods_documented
}

pub fn description(&self) -> &'static str {
self.description
}
}

impl From<RemoteScriptMetaData> for ScriptMetaData {
fn from(value: RemoteScriptMetaData) -> Self {
let properties: Vec<Documented<PropertyInfo>> = value
.properties
.clone()
.into_iter()
.map(Into::into)
.collect();

Self {
class_name: ClassName::from_ascii_cstr(value.class_name.as_str().as_bytes()),
base_type_name: StringName::from(&value.base_type_name.as_str()),
properties_documented: properties,
properties_documented: value
.properties
.clone()
.into_iter()
.map(Into::into)
.collect(),
properties: Rc::new(value.properties.into_iter().map(Into::into).collect()),
methods_documented: value.methods.clone().into_iter().map(Into::into).collect(),
methods: Rc::new(value.methods.into_iter().map(Into::into).collect()),
create_data: value.create_data,
description: value.description.as_str(),
Expand Down Expand Up @@ -181,6 +185,14 @@ impl ToMethodDoc for MethodInfo {
}
}

impl<T: ToMethodDoc> ToMethodDoc for Documented<T> {
fn to_method_doc(&self) -> Dictionary {
self.inner
.to_method_doc()
.apply(|dict| dict.set("description", self.description))
}
}

#[derive(Debug)]
pub struct Documented<T> {
inner: T,
Expand All @@ -196,6 +208,15 @@ impl From<crate::script_registry::RemoteScriptPropertyInfo> for Documented<Prope
}
}

impl From<crate::script_registry::RemoteScriptMethodInfo> for Documented<MethodInfo> {
fn from(value: crate::script_registry::RemoteScriptMethodInfo) -> Self {
Self {
description: value.description.as_str(),
inner: value.into(),
}
}
}

impl<T> Deref for Documented<T> {
type Target = T;

Expand Down
2 changes: 1 addition & 1 deletion rust-script/src/runtime/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ macro_rules! setup {
}
};
}

#[cfg(not(all(feature = "hot-reload", debug_assertions)))]
#[macro_export]
macro_rules! setup {
Expand Down
2 changes: 1 addition & 1 deletion rust-script/src/runtime/rust_script.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ impl ScriptExtensionVirtual for RustScript {

reg.get(&self.class_name).map(|class| {
let methods = class
.methods()
.methods_documented()
.iter()
.map(|method| method.to_method_doc())
.collect();
Expand Down
1 change: 1 addition & 0 deletions rust-script/src/script_registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ pub struct RemoteScriptMethodInfo {
pub return_type: RemoteScriptPropertyInfo,
pub arguments: RVec<RemoteScriptPropertyInfo>,
pub flags: i32,
pub description: RStr<'static>,
}

impl From<RemoteScriptMethodInfo> for MethodInfo {
Expand Down

0 comments on commit e36235c

Please sign in to comment.