1 use ide_db::assists::GroupLabel;
2 use itertools::Itertools;
3 use stdx::to_lower_snake_case;
4 use syntax::ast::HasVisibility;
5 use syntax::ast::{self, AstNode, HasName};
8 utils::{add_method_to_adt, find_struct_impl},
9 AssistContext, AssistId, AssistKind, Assists,
12 // Assist: generate_enum_try_into_method
14 // Generate a `try_into_` method for this enum variant.
30 // fn try_into_text(self) -> Result<String, Self> {
31 // if let Self::Text(v) = self {
39 pub(crate) fn generate_enum_try_into_method(
41 ctx: &AssistContext<'_>,
43 generate_enum_projection_method(
46 "generate_enum_try_into_method",
47 "Generate a `try_into_` method for this enum variant",
49 fn_name_prefix: "try_into",
51 return_prefix: "Result<",
52 return_suffix: ", Self>",
54 sad_case: "Err(self)",
59 // Assist: generate_enum_as_method
61 // Generate an `as_` method for this enum variant.
77 // fn as_text(&self) -> Option<&String> {
78 // if let Self::Text(v) = self {
86 pub(crate) fn generate_enum_as_method(acc: &mut Assists, ctx: &AssistContext<'_>) -> Option<()> {
87 generate_enum_projection_method(
90 "generate_enum_as_method",
91 "Generate an `as_` method for this enum variant",
95 return_prefix: "Option<&",
103 struct ProjectionProps {
104 fn_name_prefix: &'static str,
105 self_param: &'static str,
106 return_prefix: &'static str,
107 return_suffix: &'static str,
108 happy_case: &'static str,
109 sad_case: &'static str,
112 fn generate_enum_projection_method(
114 ctx: &AssistContext<'_>,
115 assist_id: &'static str,
116 assist_description: &str,
117 props: ProjectionProps,
119 let ProjectionProps {
128 let variant = ctx.find_node_at_offset::<ast::Variant>()?;
129 let variant_name = variant.name()?;
130 let parent_enum = ast::Adt::Enum(variant.parent_enum());
132 let (pattern_suffix, field_type, bound_name) = match variant.kind() {
133 ast::StructKind::Record(record) => {
134 let (field,) = record.fields().collect_tuple()?;
135 let name = field.name()?.to_string();
136 let ty = field.ty()?;
137 let pattern_suffix = format!(" {{ {name} }}");
138 (pattern_suffix, ty, name)
140 ast::StructKind::Tuple(tuple) => {
141 let (field,) = tuple.fields().collect_tuple()?;
142 let ty = field.ty()?;
143 ("(v)".to_owned(), ty, "v".to_owned())
145 ast::StructKind::Unit => return None,
148 let fn_name = format!("{fn_name_prefix}_{}", &to_lower_snake_case(&variant_name.text()));
150 // Return early if we've found an existing new fn
151 let impl_def = find_struct_impl(ctx, &parent_enum, &[fn_name.clone()])?;
153 let target = variant.syntax().text_range();
155 &GroupLabel("Generate an `is_`,`as_`, or `try_into_` for this enum variant".to_owned()),
156 AssistId(assist_id, AssistKind::Generate),
160 let vis = parent_enum.visibility().map_or(String::new(), |v| format!("{} ", v));
162 let field_type_syntax = field_type.syntax();
164 let must_use = if ctx.config.assist_emit_must_use {
170 let method = format!(
171 " {must_use}{vis}fn {fn_name}({self_param}) -> {return_prefix}{field_type_syntax}{return_suffix} {{
172 if let Self::{variant_name}{pattern_suffix} = self {{
173 {happy_case}({bound_name})
180 add_method_to_adt(builder, &parent_enum, impl_def, &method);
187 use crate::tests::{check_assist, check_assist_not_applicable};
192 fn test_generate_enum_try_into_tuple_variant() {
194 generate_enum_try_into_method,
206 fn try_into_text(self) -> Result<String, Self> {
207 if let Self::Text(v) = self {
218 fn test_generate_enum_try_into_already_implemented() {
219 check_assist_not_applicable(
220 generate_enum_try_into_method,
227 fn try_into_text(self) -> Result<String, Self> {
228 if let Self::Text(v) = self {
239 fn test_generate_enum_try_into_unit_variant() {
240 check_assist_not_applicable(
241 generate_enum_try_into_method,
251 fn test_generate_enum_try_into_record_with_multiple_fields() {
252 check_assist_not_applicable(
253 generate_enum_try_into_method,
257 Both { first: i32, second: String }$0,
263 fn test_generate_enum_try_into_tuple_with_multiple_fields() {
264 check_assist_not_applicable(
265 generate_enum_try_into_method,
268 Text(String, String)$0,
274 fn test_generate_enum_try_into_record_variant() {
276 generate_enum_try_into_method,
279 Text { text: String }$0,
283 Text { text: String },
287 fn try_into_text(self) -> Result<String, Self> {
288 if let Self::Text { text } = self {
299 fn test_generate_enum_as_tuple_variant() {
301 generate_enum_as_method,
313 fn as_text(&self) -> Option<&String> {
314 if let Self::Text(v) = self {
325 fn test_generate_enum_as_record_variant() {
327 generate_enum_as_method,
330 Text { text: String }$0,
334 Text { text: String },
338 fn as_text(&self) -> Option<&String> {
339 if let Self::Text { text } = self {