Skip to content

Commit

Permalink
Minimal Script Documentation (#10)
Browse files Browse the repository at this point in the history
Nothing fancy, just the minimal metadata that is available any way.
  • Loading branch information
TitanNano committed Nov 6, 2023
1 parent 80b730c commit 769ebb3
Show file tree
Hide file tree
Showing 2 changed files with 149 additions and 2 deletions.
99 changes: 99 additions & 0 deletions rust-script/src/runtime/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use godot::{
meta::{ClassName, MethodInfo, PropertyInfo},
Array, Dictionary, Gd, Object, StringName, ToGodot,
},
sys::VariantType,
};

use crate::{
Expand Down Expand Up @@ -90,3 +91,101 @@ impl ToDictionary for MethodInfo {
})
}
}

fn variant_type_to_str(var_type: VariantType) -> &'static str {
use VariantType as V;

match var_type {
V::Nil => "void",
V::Bool => "Bool",
V::Int => "Int",
V::Float => "Float",
V::String => "String",
V::Vector2 => "Vector2",
V::Vector2i => "Vector2i",
V::Rect2 => "Rect2",
V::Rect2i => "Rect2i",
V::Vector3 => "Vector3",
V::Vector3i => "Vector3i",
V::Transform2D => "Transform2D",
V::Vector4 => "Vector4",
V::Vector4i => "Vector4i",
V::Plane => "Plane",
V::Quaternion => "Quaternion",
V::Aabb => "Aabb",
V::Basis => "Basis",
V::Transform3D => "Transform3D",
V::Projection => "Projection",
V::Color => "Color",
V::StringName => "StringName",
V::NodePath => "NodePath",
V::Rid => "Rid",
V::Object => "Object",
V::Callable => "Callable",
V::Signal => "Signal",
V::Dictionary => "Dictionary",
V::Array => "Array",
V::PackedByteArray => "PackedByteArray",
V::PackedInt32Array => "PackedInt32Array",
V::PackedInt64Array => "PackedInt64Array",
V::PackedColorArray => "PackedColorArray",
V::PackedStringArray => "PackedStringArray",
V::PackedVector3Array => "PackedVector3Array",
V::PackedVector2Array => "PackedVector2Array",
V::PackedFloat64Array => "PackedFloat64Array",
V::PackedFloat32Array => "PackedFloat32Array",
}
}

pub trait ToMethodDoc {
fn to_method_doc(&self) -> Dictionary;
}

impl ToMethodDoc for MethodInfo {
fn to_method_doc(&self) -> Dictionary {
let args: Array<Dictionary> = self
.arguments
.iter()
.map(|arg| arg.to_argument_doc())
.collect();

Dictionary::new().apply(|dict| {
dict.set("name", self.method_name.clone());
dict.set(
"return_type",
variant_type_to_str(self.return_type.variant_type),
);
dict.set("is_deprecated", false);
dict.set("is_experimental", false);
dict.set("arguments", args);
})
}
}

pub trait ToArgumentDoc {
fn to_argument_doc(&self) -> Dictionary;
}

impl ToArgumentDoc for PropertyInfo {
fn to_argument_doc(&self) -> Dictionary {
Dictionary::new().apply(|dict| {
dict.set("name", self.property_name.clone());
dict.set("type", variant_type_to_str(self.variant_type));
})
}
}

pub trait ToPropertyDoc {
fn to_property_doc(&self) -> Dictionary;
}

impl ToPropertyDoc for PropertyInfo {
fn to_property_doc(&self) -> Dictionary {
Dictionary::new().apply(|dict| {
dict.set("name", self.property_name.clone());
dict.set("type", variant_type_to_str(self.variant_type));
dict.set("is_deprecated", false);
dict.set("is_experimental", false);
})
}
}
52 changes: 50 additions & 2 deletions rust-script/src/runtime/rust_script.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@ use godot::{
engine::{Engine, Script, ScriptExtension, ScriptExtensionVirtual, ScriptLanguage},
prelude::{
godot_api, Array, Base, Dictionary, Gd, GodotClass, GodotString, Object, StringName,
VariantArray,
},
};
use RemoteGodotScript_trait::RemoteGodotScript_TO;

use crate::script_registry::RemoteGodotScript_trait;
use crate::{apply::Apply, script_registry::RemoteGodotScript_trait};

use super::{
metadata::ToDictionary,
metadata::{ToDictionary, ToMethodDoc, ToPropertyDoc},
rust_script_instance::{RustScriptInstance, RustScriptPlaceholder},
rust_script_language::RustScriptLanguage,
SCRIPT_REGISTRY,
Expand Down Expand Up @@ -194,4 +195,51 @@ impl ScriptExtensionVirtual for RustScript {
.unwrap_or_default()
})
}

fn get_documentation(&self) -> Array<Dictionary> {
let (methods, props): (Array<Dictionary>, Array<Dictionary>) = SCRIPT_REGISTRY
.with(|lock| {
let reg = lock.read().expect("unable to obtain read lock");

reg.get(&self.class_name).map(|class| {
let methods = class
.methods()
.iter()
.map(|method| method.to_method_doc())
.collect();

let props = class
.properties()
.iter()
.map(|prop| prop.to_property_doc())
.collect();

(methods, props)
})
})
.unwrap_or_default();

let class_doc = Dictionary::new().apply(|dict| {
dict.set(GodotString::from("name"), self.class_name());
dict.set(GodotString::from("inherits"), self.get_instance_base_type());
dict.set(GodotString::from("brief_description"), GodotString::new());
dict.set(GodotString::from("description"), GodotString::new());
dict.set(GodotString::from("tutorials"), VariantArray::new());
dict.set(GodotString::from("constructors"), VariantArray::new());
dict.set(GodotString::from("methods"), methods);
dict.set(GodotString::from("operators"), VariantArray::new());
dict.set(GodotString::from("signals"), VariantArray::new());
dict.set(GodotString::from("constants"), VariantArray::new());
dict.set(GodotString::from("enums"), VariantArray::new());
dict.set(GodotString::from("properties"), props);
dict.set(GodotString::from("theme_properties"), VariantArray::new());
dict.set(GodotString::from("annotations"), VariantArray::new());
dict.set(GodotString::from("is_deprecated"), false);
dict.set(GodotString::from("is_experimental"), false);
dict.set(GodotString::from("is_script_doc"), true);
dict.set(GodotString::from("script_path"), self.base.get_path());
});

Array::from(&[class_doc])
}
}

0 comments on commit 769ebb3

Please sign in to comment.