Skip to content

Commit 769ebb3

Browse files
authored
Minimal Script Documentation (#10)
Nothing fancy, just the minimal metadata that is available any way.
1 parent 80b730c commit 769ebb3

File tree

2 files changed

+149
-2
lines changed

2 files changed

+149
-2
lines changed

rust-script/src/runtime/metadata.rs

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use godot::{
77
meta::{ClassName, MethodInfo, PropertyInfo},
88
Array, Dictionary, Gd, Object, StringName, ToGodot,
99
},
10+
sys::VariantType,
1011
};
1112

1213
use crate::{
@@ -90,3 +91,101 @@ impl ToDictionary for MethodInfo {
9091
})
9192
}
9293
}
94+
95+
fn variant_type_to_str(var_type: VariantType) -> &'static str {
96+
use VariantType as V;
97+
98+
match var_type {
99+
V::Nil => "void",
100+
V::Bool => "Bool",
101+
V::Int => "Int",
102+
V::Float => "Float",
103+
V::String => "String",
104+
V::Vector2 => "Vector2",
105+
V::Vector2i => "Vector2i",
106+
V::Rect2 => "Rect2",
107+
V::Rect2i => "Rect2i",
108+
V::Vector3 => "Vector3",
109+
V::Vector3i => "Vector3i",
110+
V::Transform2D => "Transform2D",
111+
V::Vector4 => "Vector4",
112+
V::Vector4i => "Vector4i",
113+
V::Plane => "Plane",
114+
V::Quaternion => "Quaternion",
115+
V::Aabb => "Aabb",
116+
V::Basis => "Basis",
117+
V::Transform3D => "Transform3D",
118+
V::Projection => "Projection",
119+
V::Color => "Color",
120+
V::StringName => "StringName",
121+
V::NodePath => "NodePath",
122+
V::Rid => "Rid",
123+
V::Object => "Object",
124+
V::Callable => "Callable",
125+
V::Signal => "Signal",
126+
V::Dictionary => "Dictionary",
127+
V::Array => "Array",
128+
V::PackedByteArray => "PackedByteArray",
129+
V::PackedInt32Array => "PackedInt32Array",
130+
V::PackedInt64Array => "PackedInt64Array",
131+
V::PackedColorArray => "PackedColorArray",
132+
V::PackedStringArray => "PackedStringArray",
133+
V::PackedVector3Array => "PackedVector3Array",
134+
V::PackedVector2Array => "PackedVector2Array",
135+
V::PackedFloat64Array => "PackedFloat64Array",
136+
V::PackedFloat32Array => "PackedFloat32Array",
137+
}
138+
}
139+
140+
pub trait ToMethodDoc {
141+
fn to_method_doc(&self) -> Dictionary;
142+
}
143+
144+
impl ToMethodDoc for MethodInfo {
145+
fn to_method_doc(&self) -> Dictionary {
146+
let args: Array<Dictionary> = self
147+
.arguments
148+
.iter()
149+
.map(|arg| arg.to_argument_doc())
150+
.collect();
151+
152+
Dictionary::new().apply(|dict| {
153+
dict.set("name", self.method_name.clone());
154+
dict.set(
155+
"return_type",
156+
variant_type_to_str(self.return_type.variant_type),
157+
);
158+
dict.set("is_deprecated", false);
159+
dict.set("is_experimental", false);
160+
dict.set("arguments", args);
161+
})
162+
}
163+
}
164+
165+
pub trait ToArgumentDoc {
166+
fn to_argument_doc(&self) -> Dictionary;
167+
}
168+
169+
impl ToArgumentDoc for PropertyInfo {
170+
fn to_argument_doc(&self) -> Dictionary {
171+
Dictionary::new().apply(|dict| {
172+
dict.set("name", self.property_name.clone());
173+
dict.set("type", variant_type_to_str(self.variant_type));
174+
})
175+
}
176+
}
177+
178+
pub trait ToPropertyDoc {
179+
fn to_property_doc(&self) -> Dictionary;
180+
}
181+
182+
impl ToPropertyDoc for PropertyInfo {
183+
fn to_property_doc(&self) -> Dictionary {
184+
Dictionary::new().apply(|dict| {
185+
dict.set("name", self.property_name.clone());
186+
dict.set("type", variant_type_to_str(self.variant_type));
187+
dict.set("is_deprecated", false);
188+
dict.set("is_experimental", false);
189+
})
190+
}
191+
}

rust-script/src/runtime/rust_script.rs

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,15 @@ use godot::{
66
engine::{Engine, Script, ScriptExtension, ScriptExtensionVirtual, ScriptLanguage},
77
prelude::{
88
godot_api, Array, Base, Dictionary, Gd, GodotClass, GodotString, Object, StringName,
9+
VariantArray,
910
},
1011
};
1112
use RemoteGodotScript_trait::RemoteGodotScript_TO;
1213

13-
use crate::script_registry::RemoteGodotScript_trait;
14+
use crate::{apply::Apply, script_registry::RemoteGodotScript_trait};
1415

1516
use super::{
16-
metadata::ToDictionary,
17+
metadata::{ToDictionary, ToMethodDoc, ToPropertyDoc},
1718
rust_script_instance::{RustScriptInstance, RustScriptPlaceholder},
1819
rust_script_language::RustScriptLanguage,
1920
SCRIPT_REGISTRY,
@@ -194,4 +195,51 @@ impl ScriptExtensionVirtual for RustScript {
194195
.unwrap_or_default()
195196
})
196197
}
198+
199+
fn get_documentation(&self) -> Array<Dictionary> {
200+
let (methods, props): (Array<Dictionary>, Array<Dictionary>) = SCRIPT_REGISTRY
201+
.with(|lock| {
202+
let reg = lock.read().expect("unable to obtain read lock");
203+
204+
reg.get(&self.class_name).map(|class| {
205+
let methods = class
206+
.methods()
207+
.iter()
208+
.map(|method| method.to_method_doc())
209+
.collect();
210+
211+
let props = class
212+
.properties()
213+
.iter()
214+
.map(|prop| prop.to_property_doc())
215+
.collect();
216+
217+
(methods, props)
218+
})
219+
})
220+
.unwrap_or_default();
221+
222+
let class_doc = Dictionary::new().apply(|dict| {
223+
dict.set(GodotString::from("name"), self.class_name());
224+
dict.set(GodotString::from("inherits"), self.get_instance_base_type());
225+
dict.set(GodotString::from("brief_description"), GodotString::new());
226+
dict.set(GodotString::from("description"), GodotString::new());
227+
dict.set(GodotString::from("tutorials"), VariantArray::new());
228+
dict.set(GodotString::from("constructors"), VariantArray::new());
229+
dict.set(GodotString::from("methods"), methods);
230+
dict.set(GodotString::from("operators"), VariantArray::new());
231+
dict.set(GodotString::from("signals"), VariantArray::new());
232+
dict.set(GodotString::from("constants"), VariantArray::new());
233+
dict.set(GodotString::from("enums"), VariantArray::new());
234+
dict.set(GodotString::from("properties"), props);
235+
dict.set(GodotString::from("theme_properties"), VariantArray::new());
236+
dict.set(GodotString::from("annotations"), VariantArray::new());
237+
dict.set(GodotString::from("is_deprecated"), false);
238+
dict.set(GodotString::from("is_experimental"), false);
239+
dict.set(GodotString::from("is_script_doc"), true);
240+
dict.set(GodotString::from("script_path"), self.base.get_path());
241+
});
242+
243+
Array::from(&[class_doc])
244+
}
197245
}

0 commit comments

Comments
 (0)