more recursive docs types (#4028)
* more recursive Signed-off-by: Jess Frazelle <github@jessfraz.com> * updates; Signed-off-by: Jess Frazelle <github@jessfraz.com> * A snapshot a day keeps the bugs away! 📷🐛 (OS: ubuntu-latest) * A snapshot a day keeps the bugs away! 📷🐛 (OS: ubuntu-latest) * A snapshot a day keeps the bugs away! 📷🐛 (OS: windows-latest) * A snapshot a day keeps the bugs away! 📷🐛 (OS: windows-latest) * updates Signed-off-by: Jess Frazelle <github@jessfraz.com> * updates Signed-off-by: Jess Frazelle <github@jessfraz.com> * fixes Signed-off-by: Jess Frazelle <github@jessfraz.com> * add the format Signed-off-by: Jess Frazelle <github@jessfraz.com> * updates Signed-off-by: Jess Frazelle <github@jessfraz.com> * get the descriptions again Signed-off-by: Jess Frazelle <github@jessfraz.com> * updates Signed-off-by: Jess Frazelle <github@jessfraz.com> * A snapshot a day keeps the bugs away! 📷🐛 (OS: ubuntu-latest) * A snapshot a day keeps the bugs away! 📷🐛 (OS: ubuntu-latest) * updates Signed-off-by: Jess Frazelle <github@jessfraz.com> --------- Signed-off-by: Jess Frazelle <github@jessfraz.com> Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
This commit is contained in:
@ -53,6 +53,9 @@ pub struct StdLibFnArg {
|
||||
/// The schema of the argument.
|
||||
#[ts(type = "any")]
|
||||
pub schema: schemars::schema::Schema,
|
||||
/// The schema definitions for the argument.
|
||||
#[ts(type = "any")]
|
||||
pub schema_definitions: schemars::Map<String, schemars::schema::Schema>,
|
||||
/// If the argument is required.
|
||||
pub required: bool,
|
||||
}
|
||||
@ -86,7 +89,7 @@ impl StdLibFnArg {
|
||||
}
|
||||
|
||||
pub fn description(&self) -> Option<String> {
|
||||
get_description_string_from_schema(&self.schema.clone())
|
||||
get_description_string_from_schema(&self.schema.clone(), &self.schema_definitions)
|
||||
}
|
||||
}
|
||||
|
||||
@ -120,10 +123,10 @@ pub trait StdLibFn: std::fmt::Debug + Send + Sync {
|
||||
fn tags(&self) -> Vec<String>;
|
||||
|
||||
/// The args of the function.
|
||||
fn args(&self) -> Vec<StdLibFnArg>;
|
||||
fn args(&self, inline_subschemas: bool) -> Vec<StdLibFnArg>;
|
||||
|
||||
/// The return value of the function.
|
||||
fn return_value(&self) -> Option<StdLibFnArg>;
|
||||
fn return_value(&self, inline_subschemas: bool) -> Option<StdLibFnArg>;
|
||||
|
||||
/// If the function is unpublished.
|
||||
fn unpublished(&self) -> bool;
|
||||
@ -147,8 +150,8 @@ pub trait StdLibFn: std::fmt::Debug + Send + Sync {
|
||||
summary: self.summary(),
|
||||
description: self.description(),
|
||||
tags: self.tags(),
|
||||
args: self.args(),
|
||||
return_value: self.return_value(),
|
||||
args: self.args(false),
|
||||
return_value: self.return_value(false),
|
||||
unpublished: self.unpublished(),
|
||||
deprecated: self.deprecated(),
|
||||
examples: self.examples(),
|
||||
@ -158,7 +161,7 @@ pub trait StdLibFn: std::fmt::Debug + Send + Sync {
|
||||
fn fn_signature(&self) -> String {
|
||||
let mut signature = String::new();
|
||||
signature.push_str(&format!("{}(", self.name()));
|
||||
for (i, arg) in self.args().iter().enumerate() {
|
||||
for (i, arg) in self.args(false).iter().enumerate() {
|
||||
if i > 0 {
|
||||
signature.push_str(", ");
|
||||
}
|
||||
@ -169,7 +172,7 @@ pub trait StdLibFn: std::fmt::Debug + Send + Sync {
|
||||
}
|
||||
}
|
||||
signature.push(')');
|
||||
if let Some(return_value) = self.return_value() {
|
||||
if let Some(return_value) = self.return_value(false) {
|
||||
signature.push_str(&format!(" -> {}", return_value.type_));
|
||||
}
|
||||
|
||||
@ -212,7 +215,7 @@ pub trait StdLibFn: std::fmt::Debug + Send + Sync {
|
||||
fn to_autocomplete_snippet(&self) -> Result<String> {
|
||||
let mut args = Vec::new();
|
||||
let mut index = 0;
|
||||
for arg in self.args().iter() {
|
||||
for arg in self.args(true).iter() {
|
||||
if let Some((i, arg_str)) = arg.get_autocomplete_snippet(index)? {
|
||||
index = i + 1;
|
||||
args.push(arg_str);
|
||||
@ -238,7 +241,7 @@ pub trait StdLibFn: std::fmt::Debug + Send + Sync {
|
||||
self.summary()
|
||||
},
|
||||
})),
|
||||
parameters: Some(self.args().into_iter().map(|arg| arg.into()).collect()),
|
||||
parameters: Some(self.args(true).into_iter().map(|arg| arg.into()).collect()),
|
||||
active_parameter,
|
||||
}],
|
||||
active_signature: Some(0),
|
||||
@ -308,13 +311,22 @@ impl Clone for Box<dyn StdLibFn> {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_description_string_from_schema(schema: &schemars::schema::Schema) -> Option<String> {
|
||||
pub fn get_description_string_from_schema(
|
||||
schema: &schemars::schema::Schema,
|
||||
definitions: &schemars::Map<String, schemars::schema::Schema>,
|
||||
) -> Option<String> {
|
||||
if let schemars::schema::Schema::Object(o) = schema {
|
||||
if let Some(metadata) = &o.metadata {
|
||||
if let Some(description) = &metadata.description {
|
||||
return Some(description.to_string());
|
||||
}
|
||||
}
|
||||
|
||||
if let Some(reference) = &o.reference {
|
||||
if let Some(definition) = definitions.get(reference.split('/').last().unwrap_or("")) {
|
||||
return get_description_string_from_schema(definition, definitions);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
None
|
||||
@ -692,7 +704,7 @@ pub fn completion_item_from_enum_schema(
|
||||
kind: CompletionItemKind,
|
||||
) -> Result<CompletionItem> {
|
||||
// Get the docs for the schema.
|
||||
let description = get_description_string_from_schema(schema).unwrap_or_default();
|
||||
let description = get_description_string_from_schema(schema, &Default::default()).unwrap_or_default();
|
||||
let schemars::schema::Schema::Object(o) = schema else {
|
||||
anyhow::bail!("expected object schema: {:#?}", schema);
|
||||
};
|
||||
@ -754,7 +766,7 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn test_deserialize_function() {
|
||||
let some_function_string = r#"{"type":"StdLib","func":{"name":"line","summary":"","description":"","tags":[],"returnValue":{"type":"","required":false,"name":"","schema":{}},"args":[],"unpublished":false,"deprecated":false, "examples": []}}"#;
|
||||
let some_function_string = r#"{"type":"StdLib","func":{"name":"line","summary":"","description":"","tags":[],"returnValue":{"type":"","required":false,"name":"","schema":{},"schemaDefinitions":{}},"args":[],"unpublished":false,"deprecated":false, "examples": []}}"#;
|
||||
let some_function: crate::ast::types::Function = serde_json::from_str(some_function_string).unwrap();
|
||||
|
||||
assert_eq!(
|
||||
|
Reference in New Issue
Block a user