Rust side snippet completions (#2096)
* updates; Signed-off-by: Jess Frazelle <github@jessfraz.com> docs Signed-off-by: Jess Frazelle <github@jessfraz.com> remove descriptions Signed-off-by: Jess Frazelle <github@jessfraz.com> get snippet tests Signed-off-by: Jess Frazelle <github@jessfraz.com> updates Signed-off-by: Jess Frazelle <github@jessfraz.com> more autocomplete tests Signed-off-by: Jess Frazelle <github@jessfraz.com> updates Signed-off-by: Jess Frazelle <github@jessfraz.com> updates Signed-off-by: Jess Frazelle <github@jessfraz.com> updates Signed-off-by: Jess Frazelle <github@jessfraz.com> updates Signed-off-by: Jess Frazelle <github@jessfraz.com> tab to end of snippets Signed-off-by: Jess Frazelle <github@jessfraz.com> updates 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) A snapshot a day keeps the bugs away! 📷🐛 (OS: ubuntu) * 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> * A snapshot a day keeps the bugs away! 📷🐛 (OS: ubuntu) * emptu * A snapshot a day keeps the bugs away! 📷🐛 (OS: ubuntu) * empty * A snapshot a day keeps the bugs away! 📷🐛 (OS: ubuntu) * empty * A snapshot a day keeps the bugs away! 📷🐛 (OS: ubuntu) * empty --------- 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:
@ -57,11 +57,17 @@ impl StdLibFnArg {
|
||||
get_type_string_from_schema(&self.schema.clone())
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
pub fn get_autocomplete_string(&self) -> Result<String> {
|
||||
get_autocomplete_string_from_schema(&self.schema.clone())
|
||||
}
|
||||
|
||||
pub fn get_autocomplete_snippet(&self, index: usize) -> Result<Option<(usize, String)>> {
|
||||
if self.type_ == "SketchGroup" || self.type_ == "ExtrudeGroup" || self.type_ == "SketchSurface" {
|
||||
return Ok(Some((index, format!("${{{}:{}}}", index, "%"))));
|
||||
}
|
||||
get_autocomplete_snippet_from_schema(&self.schema.clone(), index)
|
||||
}
|
||||
|
||||
pub fn description(&self) -> Option<String> {
|
||||
get_description_string_from_schema(&self.schema.clone())
|
||||
}
|
||||
@ -153,8 +159,8 @@ pub trait StdLibFn: std::fmt::Debug + Send + Sync {
|
||||
signature
|
||||
}
|
||||
|
||||
fn to_completion_item(&self) -> CompletionItem {
|
||||
CompletionItem {
|
||||
fn to_completion_item(&self) -> Result<CompletionItem> {
|
||||
Ok(CompletionItem {
|
||||
label: self.name(),
|
||||
label_details: Some(CompletionItemLabelDetails {
|
||||
detail: Some(self.fn_signature().replace(&self.name(), "")),
|
||||
@ -174,25 +180,7 @@ pub trait StdLibFn: std::fmt::Debug + Send + Sync {
|
||||
preselect: None,
|
||||
sort_text: None,
|
||||
filter_text: None,
|
||||
insert_text: Some(format!(
|
||||
"{}({})",
|
||||
self.name(),
|
||||
self.args()
|
||||
.iter()
|
||||
.enumerate()
|
||||
// It is okay to unwrap here since in the `kcl-lib` tests, we would have caught
|
||||
// any errors in the `self`'s signature.
|
||||
.map(|(index, item)| {
|
||||
let format = item.get_autocomplete_string().unwrap();
|
||||
if item.type_ == "SketchGroup" || item.type_ == "ExtrudeGroup" {
|
||||
format!("${{{}:{}}}", index + 1, "%")
|
||||
} else {
|
||||
format!("${{{}:{}}}", index + 1, format)
|
||||
}
|
||||
})
|
||||
.collect::<Vec<_>>()
|
||||
.join(",")
|
||||
)),
|
||||
insert_text: Some(self.to_autocomplete_snippet()?),
|
||||
insert_text_format: Some(InsertTextFormat::SNIPPET),
|
||||
insert_text_mode: None,
|
||||
text_edit: None,
|
||||
@ -201,7 +189,21 @@ pub trait StdLibFn: std::fmt::Debug + Send + Sync {
|
||||
commit_characters: None,
|
||||
data: None,
|
||||
tags: None,
|
||||
})
|
||||
}
|
||||
|
||||
fn to_autocomplete_snippet(&self) -> Result<String> {
|
||||
let mut args = Vec::new();
|
||||
let mut index = 0;
|
||||
for arg in self.args().iter() {
|
||||
if let Some((i, arg_str)) = arg.get_autocomplete_snippet(index)? {
|
||||
index = i + 1;
|
||||
args.push(arg_str);
|
||||
}
|
||||
}
|
||||
// We end with ${} so you can jump to the end of the snippet.
|
||||
// After the last argument.
|
||||
Ok(format!("{}({})${{}}", self.name(), args.join(", ")))
|
||||
}
|
||||
|
||||
fn to_signature_help(&self) -> SignatureHelp {
|
||||
@ -350,7 +352,13 @@ pub fn get_type_string_from_schema(schema: &schemars::schema::Schema) -> Result<
|
||||
match array_val.max_items {
|
||||
Some(val) => {
|
||||
return Ok((
|
||||
format!("[{}]", (0..val).map(|_| "number").collect::<Vec<_>>().join(", ")),
|
||||
format!(
|
||||
"[{}]",
|
||||
(0..val)
|
||||
.map(|_| get_type_string_from_schema(items).unwrap().0)
|
||||
.collect::<Vec<_>>()
|
||||
.join(", ")
|
||||
),
|
||||
false,
|
||||
));
|
||||
}
|
||||
@ -429,9 +437,176 @@ pub fn get_type_string_from_schema(schema: &schemars::schema::Schema) -> Result<
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_autocomplete_snippet_from_schema(
|
||||
schema: &schemars::schema::Schema,
|
||||
index: usize,
|
||||
) -> Result<Option<(usize, String)>> {
|
||||
match schema {
|
||||
schemars::schema::Schema::Object(o) => {
|
||||
if let Some(serde_json::Value::Bool(nullable)) = o.extensions.get("nullable") {
|
||||
if *nullable {
|
||||
return Ok(None);
|
||||
}
|
||||
}
|
||||
if o.enum_values.is_some() {
|
||||
let auto_str = get_autocomplete_string_from_schema(schema)?;
|
||||
return Ok(Some((index, format!("${{{}:{}}}", index, auto_str))));
|
||||
}
|
||||
|
||||
if let Some(format) = &o.format {
|
||||
if format == "uuid" {
|
||||
return Ok(Some((index, format!(r#"${{{}:"tag_or_edge_fn"}}"#, index))));
|
||||
} else if format == "double" || format == "uint" || format == "int64" || format == "uint32" {
|
||||
return Ok(Some((index, format!(r#"${{{}:3.14}}"#, index))));
|
||||
} else {
|
||||
anyhow::bail!("unknown format: {}", format);
|
||||
}
|
||||
}
|
||||
|
||||
if let Some(obj_val) = &o.object {
|
||||
let mut fn_docs = String::new();
|
||||
fn_docs.push_str("{\n");
|
||||
// Let's print out the object's properties.
|
||||
for (i, (prop_name, prop)) in obj_val.properties.iter().enumerate() {
|
||||
if prop_name.starts_with('_') {
|
||||
continue;
|
||||
}
|
||||
|
||||
if let Some((_, snippet)) = get_autocomplete_snippet_from_schema(prop, index + i)? {
|
||||
fn_docs.push_str(&format!("\t{}: {},\n", prop_name, snippet));
|
||||
}
|
||||
}
|
||||
|
||||
fn_docs.push('}');
|
||||
|
||||
return Ok(Some((index + obj_val.properties.len() - 1, fn_docs)));
|
||||
}
|
||||
|
||||
if let Some(array_val) = &o.array {
|
||||
if let Some(schemars::schema::SingleOrVec::Single(items)) = &array_val.items {
|
||||
// Let's print out the object's properties.
|
||||
match array_val.max_items {
|
||||
Some(val) => {
|
||||
return Ok(Some((
|
||||
index + (val as usize) - 1,
|
||||
format!(
|
||||
"[{}]",
|
||||
(0..val)
|
||||
.map(|v| get_autocomplete_snippet_from_schema(items, index + (v as usize))
|
||||
.unwrap()
|
||||
.unwrap()
|
||||
.1)
|
||||
.collect::<Vec<_>>()
|
||||
.join(", ")
|
||||
),
|
||||
)));
|
||||
}
|
||||
None => {
|
||||
return Ok(Some((
|
||||
index,
|
||||
format!(
|
||||
"[{}]",
|
||||
get_autocomplete_snippet_from_schema(items, index)?
|
||||
.ok_or_else(|| anyhow::anyhow!("expected snippet"))?
|
||||
.1
|
||||
),
|
||||
)));
|
||||
}
|
||||
};
|
||||
} else if let Some(items) = &array_val.contains {
|
||||
return Ok(Some((
|
||||
index,
|
||||
format!(
|
||||
"[{}]",
|
||||
get_autocomplete_snippet_from_schema(items, index)?
|
||||
.ok_or_else(|| anyhow::anyhow!("expected snippet"))?
|
||||
.1
|
||||
),
|
||||
)));
|
||||
}
|
||||
}
|
||||
|
||||
if let Some(subschemas) = &o.subschemas {
|
||||
let mut fn_docs = String::new();
|
||||
if let Some(items) = &subschemas.one_of {
|
||||
let mut had_enum_string = false;
|
||||
let mut parsed_enum_values: Vec<String> = Vec::new();
|
||||
for item in items {
|
||||
if let schemars::schema::Schema::Object(o) = item {
|
||||
if let Some(enum_values) = &o.enum_values {
|
||||
for enum_value in enum_values {
|
||||
if let serde_json::value::Value::String(enum_value) = enum_value {
|
||||
had_enum_string = true;
|
||||
parsed_enum_values.push(format!("\"{}\"", enum_value));
|
||||
} else {
|
||||
had_enum_string = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if !had_enum_string {
|
||||
break;
|
||||
}
|
||||
} else {
|
||||
had_enum_string = false;
|
||||
break;
|
||||
}
|
||||
} else {
|
||||
had_enum_string = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if had_enum_string && !parsed_enum_values.is_empty() {
|
||||
return Ok(Some((index, parsed_enum_values[0].to_string())));
|
||||
} else if let Some(item) = items.iter().next() {
|
||||
if let Some((_, snippet)) = get_autocomplete_snippet_from_schema(item, index)? {
|
||||
fn_docs.push_str(&snippet);
|
||||
}
|
||||
}
|
||||
} else if let Some(items) = &subschemas.any_of {
|
||||
if let Some(item) = items.iter().next() {
|
||||
if let Some((_, snippet)) = get_autocomplete_snippet_from_schema(item, index)? {
|
||||
fn_docs.push_str(&snippet);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
anyhow::bail!("unknown subschemas: {:#?}", subschemas);
|
||||
}
|
||||
|
||||
return Ok(Some((index, fn_docs)));
|
||||
}
|
||||
|
||||
if let Some(schemars::schema::SingleOrVec::Single(_string)) = &o.instance_type {
|
||||
return Ok(Some((index, format!(r#"${{{}:"string"}}"#, index))));
|
||||
}
|
||||
|
||||
anyhow::bail!("unknown type: {:#?}", o)
|
||||
}
|
||||
schemars::schema::Schema::Bool(_) => Ok(Some((index, format!(r#"${{{}:false}}"#, index)))),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_autocomplete_string_from_schema(schema: &schemars::schema::Schema) -> Result<String> {
|
||||
match schema {
|
||||
schemars::schema::Schema::Object(o) => {
|
||||
if let Some(enum_values) = &o.enum_values {
|
||||
let mut parsed_enum_values: Vec<String> = Default::default();
|
||||
let mut had_enum_string = false;
|
||||
for enum_value in enum_values {
|
||||
if let serde_json::value::Value::String(enum_value) = enum_value {
|
||||
had_enum_string = true;
|
||||
parsed_enum_values.push(format!("\"{}\"", enum_value));
|
||||
} else {
|
||||
had_enum_string = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if had_enum_string && !parsed_enum_values.is_empty() {
|
||||
return Ok(parsed_enum_values[0].to_string());
|
||||
}
|
||||
}
|
||||
|
||||
if let Some(format) = &o.format {
|
||||
if format == "uuid" {
|
||||
return Ok(Primitive::Uuid.to_string());
|
||||
@ -451,9 +626,6 @@ pub fn get_autocomplete_string_from_schema(schema: &schemars::schema::Schema) ->
|
||||
continue;
|
||||
}
|
||||
|
||||
if let Some(description) = get_description_string_from_schema(prop) {
|
||||
fn_docs.push_str(&format!("\t// {}\n", description));
|
||||
}
|
||||
fn_docs.push_str(&format!(
|
||||
"\t{}: {},\n",
|
||||
prop_name,
|
||||
@ -469,7 +641,17 @@ pub fn get_autocomplete_string_from_schema(schema: &schemars::schema::Schema) ->
|
||||
if let Some(array_val) = &o.array {
|
||||
if let Some(schemars::schema::SingleOrVec::Single(items)) = &array_val.items {
|
||||
// Let's print out the object's properties.
|
||||
return Ok(format!("[{}]", get_autocomplete_string_from_schema(items)?));
|
||||
match array_val.max_items {
|
||||
Some(val) => {
|
||||
return Ok(format!(
|
||||
"[{}]",
|
||||
(0..val).map(|_| "number").collect::<Vec<_>>().join(", ")
|
||||
));
|
||||
}
|
||||
None => {
|
||||
return Ok(format!("[{}]", get_autocomplete_string_from_schema(items)?));
|
||||
}
|
||||
};
|
||||
} else if let Some(items) = &array_val.contains {
|
||||
return Ok(format!("[{}]", get_autocomplete_string_from_schema(items)?));
|
||||
}
|
||||
@ -478,7 +660,36 @@ pub fn get_autocomplete_string_from_schema(schema: &schemars::schema::Schema) ->
|
||||
if let Some(subschemas) = &o.subschemas {
|
||||
let mut fn_docs = String::new();
|
||||
if let Some(items) = &subschemas.one_of {
|
||||
if let Some(item) = items.iter().next() {
|
||||
let mut had_enum_string = false;
|
||||
let mut parsed_enum_values: Vec<String> = Vec::new();
|
||||
for item in items {
|
||||
if let schemars::schema::Schema::Object(o) = item {
|
||||
if let Some(enum_values) = &o.enum_values {
|
||||
for enum_value in enum_values {
|
||||
if let serde_json::value::Value::String(enum_value) = enum_value {
|
||||
had_enum_string = true;
|
||||
parsed_enum_values.push(format!("\"{}\"", enum_value));
|
||||
} else {
|
||||
had_enum_string = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if !had_enum_string {
|
||||
break;
|
||||
}
|
||||
} else {
|
||||
had_enum_string = false;
|
||||
break;
|
||||
}
|
||||
} else {
|
||||
had_enum_string = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if had_enum_string && !parsed_enum_values.is_empty() {
|
||||
return Ok(parsed_enum_values[0].to_string());
|
||||
} else if let Some(item) = items.iter().next() {
|
||||
// Let's print out the object's properties.
|
||||
fn_docs.push_str(&get_autocomplete_string_from_schema(item)?);
|
||||
}
|
||||
@ -556,6 +767,8 @@ pub fn completion_item_from_enum_schema(
|
||||
mod tests {
|
||||
use pretty_assertions::assert_eq;
|
||||
|
||||
use super::StdLibFn;
|
||||
|
||||
#[test]
|
||||
fn test_serialize_function() {
|
||||
let some_function = crate::ast::types::Function::StdLib {
|
||||
@ -577,4 +790,66 @@ mod tests {
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn get_autocomplete_snippet_line() {
|
||||
let line_fn: Box<dyn StdLibFn> = Box::new(crate::std::sketch::Line);
|
||||
let snippet = line_fn.to_autocomplete_snippet().unwrap();
|
||||
assert_eq!(snippet, r#"line([${0:3.14}, ${1:3.14}], ${2:%})${}"#);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn get_autocomplete_snippet_extrude() {
|
||||
let extrude_fn: Box<dyn StdLibFn> = Box::new(crate::std::extrude::Extrude);
|
||||
let snippet = extrude_fn.to_autocomplete_snippet().unwrap();
|
||||
assert_eq!(snippet, r#"extrude(${0:3.14}, ${1:%})${}"#);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn get_autocomplete_snippet_fillet() {
|
||||
let fillet_fn: Box<dyn StdLibFn> = Box::new(crate::std::fillet::Fillet);
|
||||
let snippet = fillet_fn.to_autocomplete_snippet().unwrap();
|
||||
assert_eq!(
|
||||
snippet,
|
||||
r#"fillet({
|
||||
radius: ${0:3.14},
|
||||
tags: [${1:"tag_or_edge_fn"}],
|
||||
}, ${2:%})${}"#
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn get_autocomplete_snippet_start_sketch_on() {
|
||||
let start_sketch_on_fn: Box<dyn StdLibFn> = Box::new(crate::std::sketch::StartSketchOn);
|
||||
let snippet = start_sketch_on_fn.to_autocomplete_snippet().unwrap();
|
||||
assert_eq!(snippet, r#"startSketchOn(${0:"XY"})${}"#);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn get_autocomplete_snippet_pattern_circular_3d() {
|
||||
let pattern_fn: Box<dyn StdLibFn> = Box::new(crate::std::patterns::PatternCircular3D);
|
||||
let snippet = pattern_fn.to_autocomplete_snippet().unwrap();
|
||||
assert_eq!(
|
||||
snippet,
|
||||
r#"patternCircular3d({
|
||||
arcDegrees: ${0:3.14},
|
||||
axis: [${1:3.14}, ${2:3.14}, ${3:3.14}],
|
||||
center: [${2:3.14}, ${3:3.14}, ${4:3.14}],
|
||||
repetitions: ${3:3.14},
|
||||
rotateDuplicates: ${4:"string"},
|
||||
}, ${5:%})${}"#
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn get_autocomplete_snippet_revolve() {
|
||||
let revolve_fn: Box<dyn StdLibFn> = Box::new(crate::std::revolve::Revolve);
|
||||
let snippet = revolve_fn.to_autocomplete_snippet().unwrap();
|
||||
assert_eq!(
|
||||
snippet,
|
||||
r#"revolve({
|
||||
axis: ${1:"X"},
|
||||
}, ${2:%})${}"#
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user