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