aboutsummaryrefslogtreecommitdiff
path: root/crates/ide_assists/src/handlers
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ide_assists/src/handlers')
-rw-r--r--crates/ide_assists/src/handlers/generate_enum_match_method.rs55
1 files changed, 20 insertions, 35 deletions
diff --git a/crates/ide_assists/src/handlers/generate_enum_match_method.rs b/crates/ide_assists/src/handlers/generate_enum_match_method.rs
index 45a08acad..670c82200 100644
--- a/crates/ide_assists/src/handlers/generate_enum_match_method.rs
+++ b/crates/ide_assists/src/handlers/generate_enum_match_method.rs
@@ -3,7 +3,11 @@ use stdx::to_lower_snake_case;
3use syntax::ast::VisibilityOwner; 3use syntax::ast::VisibilityOwner;
4use syntax::ast::{self, AstNode, NameOwner}; 4use syntax::ast::{self, AstNode, NameOwner};
5 5
6use crate::{AssistContext, AssistId, AssistKind, Assists, assist_context::AssistBuilder, utils::{find_impl_block_end, find_struct_impl, generate_impl_text}}; 6use crate::{
7 assist_context::AssistBuilder,
8 utils::{find_impl_block_end, find_struct_impl, generate_impl_text},
9 AssistContext, AssistId, AssistKind, Assists,
10};
7 11
8// Assist: generate_enum_is_method 12// Assist: generate_enum_is_method
9// 13//
@@ -41,11 +45,7 @@ pub(crate) fn generate_enum_is_method(acc: &mut Assists, ctx: &AssistContext) ->
41 let fn_name = format!("is_{}", &to_lower_snake_case(variant_name.text())); 45 let fn_name = format!("is_{}", &to_lower_snake_case(variant_name.text()));
42 46
43 // Return early if we've found an existing new fn 47 // Return early if we've found an existing new fn
44 let impl_def = find_struct_impl( 48 let impl_def = find_struct_impl(&ctx, &parent_enum, &fn_name)?;
45 &ctx,
46 &parent_enum,
47 &fn_name,
48 )?;
49 49
50 let target = variant.syntax().text_range(); 50 let target = variant.syntax().text_range();
51 acc.add( 51 acc.add(
@@ -108,11 +108,7 @@ pub(crate) fn generate_enum_into_method(acc: &mut Assists, ctx: &AssistContext)
108 let fn_name = format!("into_{}", &to_lower_snake_case(variant_name.text())); 108 let fn_name = format!("into_{}", &to_lower_snake_case(variant_name.text()));
109 109
110 // Return early if we've found an existing new fn 110 // Return early if we've found an existing new fn
111 let impl_def = find_struct_impl( 111 let impl_def = find_struct_impl(&ctx, &parent_enum, &fn_name)?;
112 &ctx,
113 &parent_enum,
114 &fn_name,
115 )?;
116 112
117 let field_type = variant_kind.single_field_type()?; 113 let field_type = variant_kind.single_field_type()?;
118 let (pattern_suffix, bound_name) = variant_kind.binding_pattern()?; 114 let (pattern_suffix, bound_name) = variant_kind.binding_pattern()?;
@@ -181,11 +177,7 @@ pub(crate) fn generate_enum_as_method(acc: &mut Assists, ctx: &AssistContext) ->
181 let fn_name = format!("as_{}", &to_lower_snake_case(variant_name.text())); 177 let fn_name = format!("as_{}", &to_lower_snake_case(variant_name.text()));
182 178
183 // Return early if we've found an existing new fn 179 // Return early if we've found an existing new fn
184 let impl_def = find_struct_impl( 180 let impl_def = find_struct_impl(&ctx, &parent_enum, &fn_name)?;
185 &ctx,
186 &parent_enum,
187 &fn_name,
188 )?;
189 181
190 let field_type = variant_kind.single_field_type()?; 182 let field_type = variant_kind.single_field_type()?;
191 let (pattern_suffix, bound_name) = variant_kind.binding_pattern()?; 183 let (pattern_suffix, bound_name) = variant_kind.binding_pattern()?;
@@ -243,7 +235,9 @@ fn add_method_to_adt(
243enum VariantKind { 235enum VariantKind {
244 Unit, 236 Unit,
245 /// Tuple with a single field 237 /// Tuple with a single field
246 NewtypeTuple { ty: Option<ast::Type> }, 238 NewtypeTuple {
239 ty: Option<ast::Type>,
240 },
247 /// Tuple with 0 or more than 2 fields 241 /// Tuple with 0 or more than 2 fields
248 Tuple, 242 Tuple,
249 /// Record with a single field 243 /// Record with a single field
@@ -259,36 +253,27 @@ impl VariantKind {
259 fn pattern_suffix(&self) -> &'static str { 253 fn pattern_suffix(&self) -> &'static str {
260 match self { 254 match self {
261 VariantKind::Unit => "", 255 VariantKind::Unit => "",
262 VariantKind::NewtypeTuple { .. } | 256 VariantKind::NewtypeTuple { .. } | VariantKind::Tuple => "(..)",
263 VariantKind::Tuple => "(..)", 257 VariantKind::NewtypeRecord { .. } | VariantKind::Record => " { .. }",
264 VariantKind::NewtypeRecord { .. } |
265 VariantKind::Record => " { .. }",
266 } 258 }
267 } 259 }
268 260
269 fn binding_pattern(&self) -> Option<(String, String)> { 261 fn binding_pattern(&self) -> Option<(String, String)> {
270 match self { 262 match self {
271 VariantKind::Unit | 263 VariantKind::Unit
272 VariantKind::Tuple | 264 | VariantKind::Tuple
273 VariantKind::Record | 265 | VariantKind::Record
274 VariantKind::NewtypeRecord { field_name: None, .. } => None, 266 | VariantKind::NewtypeRecord { field_name: None, .. } => None,
275 VariantKind::NewtypeTuple { .. } => { 267 VariantKind::NewtypeTuple { .. } => Some(("(v)".to_owned(), "v".to_owned())),
276 Some(("(v)".to_owned(), "v".to_owned()))
277 }
278 VariantKind::NewtypeRecord { field_name: Some(name), .. } => { 268 VariantKind::NewtypeRecord { field_name: Some(name), .. } => {
279 Some(( 269 Some((format!(" {{ {} }}", name.syntax()), name.syntax().to_string()))
280 format!(" {{ {} }}", name.syntax()),
281 name.syntax().to_string(),
282 ))
283 } 270 }
284 } 271 }
285 } 272 }
286 273
287 fn single_field_type(&self) -> Option<&ast::Type> { 274 fn single_field_type(&self) -> Option<&ast::Type> {
288 match self { 275 match self {
289 VariantKind::Unit | 276 VariantKind::Unit | VariantKind::Tuple | VariantKind::Record => None,
290 VariantKind::Tuple |
291 VariantKind::Record => None,
292 VariantKind::NewtypeTuple { ty } => ty.as_ref(), 277 VariantKind::NewtypeTuple { ty } => ty.as_ref(),
293 VariantKind::NewtypeRecord { field_type, .. } => field_type.as_ref(), 278 VariantKind::NewtypeRecord { field_type, .. } => field_type.as_ref(),
294 } 279 }