1
1
use itertools:: Itertools ;
2
- use stdx:: { format_to , to_lower_snake_case} ;
2
+ 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 :: {
7
- utils:: { find_impl_block_end, find_struct_impl, generate_impl_text} ,
8
- AssistContext , AssistId , AssistKind , Assists ,
9
- } ;
6
+ use crate :: { AssistContext , AssistId , AssistKind , Assists , assist_context:: AssistBuilder , utils:: { find_impl_block_end, find_struct_impl, generate_impl_text} } ;
10
7
11
8
// Assist: generate_enum_is_method
12
9
//
@@ -56,15 +53,8 @@ pub(crate) fn generate_enum_is_method(acc: &mut Assists, ctx: &AssistContext) ->
56
53
"Generate an `is_` method for an enum variant" ,
57
54
target,
58
55
|builder| {
59
- let mut buf = String :: with_capacity ( 512 ) ;
60
-
61
- if impl_def. is_some ( ) {
62
- buf. push ( '\n' ) ;
63
- }
64
-
65
56
let vis = parent_enum. visibility ( ) . map_or ( String :: new ( ) , |v| format ! ( "{} " , v) ) ;
66
- format_to ! (
67
- buf,
57
+ let method = format ! (
68
58
" /// Returns `true` if the {} is [`{}`].
69
59
{}fn {}(&self) -> bool {{
70
60
matches!(self, Self::{}{})
@@ -77,14 +67,7 @@ pub(crate) fn generate_enum_is_method(acc: &mut Assists, ctx: &AssistContext) ->
77
67
variant_kind. pattern_suffix( ) ,
78
68
) ;
79
69
80
- let start_offset = impl_def
81
- . and_then ( |impl_def| find_impl_block_end ( impl_def, & mut buf) )
82
- . unwrap_or_else ( || {
83
- buf = generate_impl_text ( & parent_enum, & buf) ;
84
- parent_enum. syntax ( ) . text_range ( ) . end ( )
85
- } ) ;
86
-
87
- builder. insert ( start_offset, buf) ;
70
+ add_method_to_adt ( builder, & parent_enum, impl_def, & method) ;
88
71
} ,
89
72
)
90
73
}
@@ -140,15 +123,8 @@ pub(crate) fn generate_enum_into_method(acc: &mut Assists, ctx: &AssistContext)
140
123
"Generate an `into_` method for an enum variant" ,
141
124
target,
142
125
|builder| {
143
- let mut buf = String :: with_capacity ( 512 ) ;
144
-
145
- if impl_def. is_some ( ) {
146
- buf. push ( '\n' ) ;
147
- }
148
-
149
126
let vis = parent_enum. visibility ( ) . map_or ( String :: new ( ) , |v| format ! ( "{} " , v) ) ;
150
- format_to ! (
151
- buf,
127
+ let method = format ! (
152
128
" {}fn {}(self) -> Option<{}> {{
153
129
if let Self::{}{} = self {{
154
130
Some({})
@@ -164,18 +140,33 @@ pub(crate) fn generate_enum_into_method(acc: &mut Assists, ctx: &AssistContext)
164
140
bound_name,
165
141
) ;
166
142
167
- let start_offset = impl_def
168
- . and_then ( |impl_def| find_impl_block_end ( impl_def, & mut buf) )
169
- . unwrap_or_else ( || {
170
- buf = generate_impl_text ( & parent_enum, & buf) ;
171
- parent_enum. syntax ( ) . text_range ( ) . end ( )
172
- } ) ;
173
-
174
- builder. insert ( start_offset, buf) ;
143
+ add_method_to_adt ( builder, & parent_enum, impl_def, & method) ;
175
144
} ,
176
145
)
177
146
}
178
147
148
+ fn add_method_to_adt (
149
+ builder : & mut AssistBuilder ,
150
+ adt : & ast:: Adt ,
151
+ impl_def : Option < ast:: Impl > ,
152
+ method : & str ,
153
+ ) {
154
+ let mut buf = String :: with_capacity ( method. len ( ) + 2 ) ;
155
+ if impl_def. is_some ( ) {
156
+ buf. push ( '\n' ) ;
157
+ }
158
+ buf. push_str ( method) ;
159
+
160
+ let start_offset = impl_def
161
+ . and_then ( |impl_def| find_impl_block_end ( impl_def, & mut buf) )
162
+ . unwrap_or_else ( || {
163
+ buf = generate_impl_text ( & adt, & buf) ;
164
+ adt. syntax ( ) . text_range ( ) . end ( )
165
+ } ) ;
166
+
167
+ builder. insert ( start_offset, buf) ;
168
+ }
169
+
179
170
enum VariantKind {
180
171
Unit ,
181
172
/// Tuple with a single field
0 commit comments