2022-03-31 22:19:45 -05:00
|
|
|
use ide_db::assists::GroupLabel;
|
2021-02-15 15:25:33 -06:00
|
|
|
use itertools::Itertools;
|
|
|
|
use stdx::to_lower_snake_case;
|
2021-09-27 05:54:24 -05:00
|
|
|
use syntax::ast::HasVisibility;
|
|
|
|
use syntax::ast::{self, AstNode, HasName};
|
2021-02-15 15:25:33 -06:00
|
|
|
|
|
|
|
use crate::{
|
|
|
|
utils::{add_method_to_adt, find_struct_impl},
|
|
|
|
AssistContext, AssistId, AssistKind, Assists,
|
|
|
|
};
|
|
|
|
|
2021-02-16 15:43:21 -06:00
|
|
|
// Assist: generate_enum_try_into_method
|
2021-02-15 15:25:33 -06:00
|
|
|
//
|
2021-02-16 15:43:21 -06:00
|
|
|
// Generate an `try_into_` method for an enum variant.
|
2021-02-15 15:25:33 -06:00
|
|
|
//
|
|
|
|
// ```
|
|
|
|
// enum Value {
|
|
|
|
// Number(i32),
|
|
|
|
// Text(String)$0,
|
|
|
|
// }
|
|
|
|
// ```
|
|
|
|
// ->
|
|
|
|
// ```
|
|
|
|
// enum Value {
|
|
|
|
// Number(i32),
|
|
|
|
// Text(String),
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// impl Value {
|
2021-02-16 15:43:21 -06:00
|
|
|
// fn try_into_text(self) -> Result<String, Self> {
|
2021-02-15 15:25:33 -06:00
|
|
|
// if let Self::Text(v) = self {
|
2021-02-16 15:43:21 -06:00
|
|
|
// Ok(v)
|
2021-02-15 15:25:33 -06:00
|
|
|
// } else {
|
2021-02-16 15:43:21 -06:00
|
|
|
// Err(self)
|
2021-02-15 15:25:33 -06:00
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// ```
|
2021-02-16 15:43:21 -06:00
|
|
|
pub(crate) fn generate_enum_try_into_method(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
|
2021-02-15 15:25:33 -06:00
|
|
|
generate_enum_projection_method(
|
|
|
|
acc,
|
|
|
|
ctx,
|
2021-02-16 15:43:21 -06:00
|
|
|
"generate_enum_try_into_method",
|
|
|
|
"Generate an `try_into_` method for an enum variant",
|
|
|
|
ProjectionProps {
|
|
|
|
fn_name_prefix: "try_into",
|
|
|
|
self_param: "self",
|
|
|
|
return_prefix: "Result<",
|
|
|
|
return_suffix: ", Self>",
|
|
|
|
happy_case: "Ok",
|
|
|
|
sad_case: "Err(self)",
|
|
|
|
},
|
2021-02-15 15:25:33 -06:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Assist: generate_enum_as_method
|
|
|
|
//
|
|
|
|
// Generate an `as_` method for an enum variant.
|
|
|
|
//
|
|
|
|
// ```
|
|
|
|
// enum Value {
|
|
|
|
// Number(i32),
|
|
|
|
// Text(String)$0,
|
|
|
|
// }
|
|
|
|
// ```
|
|
|
|
// ->
|
|
|
|
// ```
|
|
|
|
// enum Value {
|
|
|
|
// Number(i32),
|
|
|
|
// Text(String),
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// impl Value {
|
|
|
|
// fn as_text(&self) -> Option<&String> {
|
|
|
|
// if let Self::Text(v) = self {
|
|
|
|
// Some(v)
|
|
|
|
// } else {
|
|
|
|
// None
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// ```
|
|
|
|
pub(crate) fn generate_enum_as_method(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
|
|
|
|
generate_enum_projection_method(
|
|
|
|
acc,
|
|
|
|
ctx,
|
|
|
|
"generate_enum_as_method",
|
|
|
|
"Generate an `as_` method for an enum variant",
|
2021-02-16 15:43:21 -06:00
|
|
|
ProjectionProps {
|
|
|
|
fn_name_prefix: "as",
|
|
|
|
self_param: "&self",
|
|
|
|
return_prefix: "Option<&",
|
|
|
|
return_suffix: ">",
|
|
|
|
happy_case: "Some",
|
|
|
|
sad_case: "None",
|
|
|
|
},
|
2021-02-15 15:25:33 -06:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-02-16 15:43:21 -06:00
|
|
|
struct ProjectionProps {
|
|
|
|
fn_name_prefix: &'static str,
|
|
|
|
self_param: &'static str,
|
|
|
|
return_prefix: &'static str,
|
|
|
|
return_suffix: &'static str,
|
|
|
|
happy_case: &'static str,
|
|
|
|
sad_case: &'static str,
|
|
|
|
}
|
|
|
|
|
|
|
|
fn generate_enum_projection_method(
|
2021-02-15 15:25:33 -06:00
|
|
|
acc: &mut Assists,
|
|
|
|
ctx: &AssistContext,
|
|
|
|
assist_id: &'static str,
|
|
|
|
assist_description: &str,
|
2021-02-16 15:43:21 -06:00
|
|
|
props: ProjectionProps,
|
2021-02-15 15:25:33 -06:00
|
|
|
) -> Option<()> {
|
|
|
|
let variant = ctx.find_node_at_offset::<ast::Variant>()?;
|
|
|
|
let variant_name = variant.name()?;
|
|
|
|
let parent_enum = ast::Adt::Enum(variant.parent_enum());
|
|
|
|
|
|
|
|
let (pattern_suffix, field_type, bound_name) = match variant.kind() {
|
|
|
|
ast::StructKind::Record(record) => {
|
|
|
|
let (field,) = record.fields().collect_tuple()?;
|
|
|
|
let name = field.name()?.to_string();
|
|
|
|
let ty = field.ty()?;
|
|
|
|
let pattern_suffix = format!(" {{ {} }}", name);
|
|
|
|
(pattern_suffix, ty, name)
|
|
|
|
}
|
|
|
|
ast::StructKind::Tuple(tuple) => {
|
|
|
|
let (field,) = tuple.fields().collect_tuple()?;
|
|
|
|
let ty = field.ty()?;
|
|
|
|
("(v)".to_owned(), ty, "v".to_owned())
|
|
|
|
}
|
|
|
|
ast::StructKind::Unit => return None,
|
|
|
|
};
|
|
|
|
|
2021-03-26 12:30:59 -05:00
|
|
|
let fn_name =
|
|
|
|
format!("{}_{}", props.fn_name_prefix, &to_lower_snake_case(&variant_name.text()));
|
2021-02-15 15:25:33 -06:00
|
|
|
|
|
|
|
// Return early if we've found an existing new fn
|
2021-06-12 22:54:16 -05:00
|
|
|
let impl_def = find_struct_impl(ctx, &parent_enum, &fn_name)?;
|
2021-02-15 15:25:33 -06:00
|
|
|
|
|
|
|
let target = variant.syntax().text_range();
|
2022-03-31 22:19:45 -05:00
|
|
|
acc.add_group(
|
|
|
|
&GroupLabel("Generate `is_`,`as_`,`try_into_`".to_owned()),
|
|
|
|
AssistId(assist_id, AssistKind::Generate),
|
|
|
|
assist_description,
|
|
|
|
target,
|
|
|
|
|builder| {
|
|
|
|
let vis = parent_enum.visibility().map_or(String::new(), |v| format!("{} ", v));
|
|
|
|
let method = format!(
|
|
|
|
" {0}fn {1}({2}) -> {3}{4}{5} {{
|
2021-02-16 15:43:21 -06:00
|
|
|
if let Self::{6}{7} = self {{
|
|
|
|
{8}({9})
|
2021-02-15 15:25:33 -06:00
|
|
|
}} else {{
|
2021-02-16 15:43:21 -06:00
|
|
|
{10}
|
2021-02-15 15:25:33 -06:00
|
|
|
}}
|
|
|
|
}}",
|
2022-03-31 22:19:45 -05:00
|
|
|
vis,
|
|
|
|
fn_name,
|
|
|
|
props.self_param,
|
|
|
|
props.return_prefix,
|
|
|
|
field_type.syntax(),
|
|
|
|
props.return_suffix,
|
|
|
|
variant_name,
|
|
|
|
pattern_suffix,
|
|
|
|
props.happy_case,
|
|
|
|
bound_name,
|
|
|
|
props.sad_case,
|
|
|
|
);
|
2021-02-15 15:25:33 -06:00
|
|
|
|
2022-03-31 22:19:45 -05:00
|
|
|
add_method_to_adt(builder, &parent_enum, impl_def, &method);
|
|
|
|
},
|
|
|
|
)
|
2021-02-15 15:25:33 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use crate::tests::{check_assist, check_assist_not_applicable};
|
|
|
|
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
2021-02-16 15:43:21 -06:00
|
|
|
fn test_generate_enum_try_into_tuple_variant() {
|
2021-02-15 15:25:33 -06:00
|
|
|
check_assist(
|
2021-02-16 15:43:21 -06:00
|
|
|
generate_enum_try_into_method,
|
2021-02-15 15:25:33 -06:00
|
|
|
r#"
|
|
|
|
enum Value {
|
|
|
|
Number(i32),
|
|
|
|
Text(String)$0,
|
|
|
|
}"#,
|
|
|
|
r#"enum Value {
|
|
|
|
Number(i32),
|
|
|
|
Text(String),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Value {
|
2021-02-16 15:43:21 -06:00
|
|
|
fn try_into_text(self) -> Result<String, Self> {
|
2021-02-15 15:25:33 -06:00
|
|
|
if let Self::Text(v) = self {
|
2021-02-16 15:43:21 -06:00
|
|
|
Ok(v)
|
2021-02-15 15:25:33 -06:00
|
|
|
} else {
|
2021-02-16 15:43:21 -06:00
|
|
|
Err(self)
|
2021-02-15 15:25:33 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2021-02-16 15:43:21 -06:00
|
|
|
fn test_generate_enum_try_into_already_implemented() {
|
2021-02-15 15:25:33 -06:00
|
|
|
check_assist_not_applicable(
|
2021-02-16 15:43:21 -06:00
|
|
|
generate_enum_try_into_method,
|
2021-02-15 15:25:33 -06:00
|
|
|
r#"enum Value {
|
|
|
|
Number(i32),
|
|
|
|
Text(String)$0,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Value {
|
2021-02-16 15:43:21 -06:00
|
|
|
fn try_into_text(self) -> Result<String, Self> {
|
2021-02-15 15:25:33 -06:00
|
|
|
if let Self::Text(v) = self {
|
2021-02-16 15:43:21 -06:00
|
|
|
Ok(v)
|
2021-02-15 15:25:33 -06:00
|
|
|
} else {
|
2021-02-16 15:43:21 -06:00
|
|
|
Err(self)
|
2021-02-15 15:25:33 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2021-02-16 15:43:21 -06:00
|
|
|
fn test_generate_enum_try_into_unit_variant() {
|
2021-02-15 15:25:33 -06:00
|
|
|
check_assist_not_applicable(
|
2021-02-16 15:43:21 -06:00
|
|
|
generate_enum_try_into_method,
|
2021-02-15 15:25:33 -06:00
|
|
|
r#"enum Value {
|
|
|
|
Number(i32),
|
|
|
|
Text(String),
|
|
|
|
Unit$0,
|
|
|
|
}"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2021-02-16 15:43:21 -06:00
|
|
|
fn test_generate_enum_try_into_record_with_multiple_fields() {
|
2021-02-15 15:25:33 -06:00
|
|
|
check_assist_not_applicable(
|
2021-02-16 15:43:21 -06:00
|
|
|
generate_enum_try_into_method,
|
2021-02-15 15:25:33 -06:00
|
|
|
r#"enum Value {
|
|
|
|
Number(i32),
|
|
|
|
Text(String),
|
|
|
|
Both { first: i32, second: String }$0,
|
|
|
|
}"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2021-02-16 15:43:21 -06:00
|
|
|
fn test_generate_enum_try_into_tuple_with_multiple_fields() {
|
2021-02-15 15:25:33 -06:00
|
|
|
check_assist_not_applicable(
|
2021-02-16 15:43:21 -06:00
|
|
|
generate_enum_try_into_method,
|
2021-02-15 15:25:33 -06:00
|
|
|
r#"enum Value {
|
|
|
|
Number(i32),
|
|
|
|
Text(String, String)$0,
|
|
|
|
}"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2021-02-16 15:43:21 -06:00
|
|
|
fn test_generate_enum_try_into_record_variant() {
|
2021-02-15 15:25:33 -06:00
|
|
|
check_assist(
|
2021-02-16 15:43:21 -06:00
|
|
|
generate_enum_try_into_method,
|
2021-02-15 15:25:33 -06:00
|
|
|
r#"enum Value {
|
|
|
|
Number(i32),
|
|
|
|
Text { text: String }$0,
|
|
|
|
}"#,
|
|
|
|
r#"enum Value {
|
|
|
|
Number(i32),
|
|
|
|
Text { text: String },
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Value {
|
2021-02-16 15:43:21 -06:00
|
|
|
fn try_into_text(self) -> Result<String, Self> {
|
2021-02-15 15:25:33 -06:00
|
|
|
if let Self::Text { text } = self {
|
2021-02-16 15:43:21 -06:00
|
|
|
Ok(text)
|
2021-02-15 15:25:33 -06:00
|
|
|
} else {
|
2021-02-16 15:43:21 -06:00
|
|
|
Err(self)
|
2021-02-15 15:25:33 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_generate_enum_as_tuple_variant() {
|
|
|
|
check_assist(
|
|
|
|
generate_enum_as_method,
|
|
|
|
r#"
|
|
|
|
enum Value {
|
|
|
|
Number(i32),
|
|
|
|
Text(String)$0,
|
|
|
|
}"#,
|
|
|
|
r#"enum Value {
|
|
|
|
Number(i32),
|
|
|
|
Text(String),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Value {
|
|
|
|
fn as_text(&self) -> Option<&String> {
|
|
|
|
if let Self::Text(v) = self {
|
|
|
|
Some(v)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_generate_enum_as_record_variant() {
|
|
|
|
check_assist(
|
|
|
|
generate_enum_as_method,
|
|
|
|
r#"enum Value {
|
|
|
|
Number(i32),
|
|
|
|
Text { text: String }$0,
|
|
|
|
}"#,
|
|
|
|
r#"enum Value {
|
|
|
|
Number(i32),
|
|
|
|
Text { text: String },
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Value {
|
|
|
|
fn as_text(&self) -> Option<&String> {
|
|
|
|
if let Self::Text { text } = self {
|
|
|
|
Some(text)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|