@@ -7,6 +7,7 @@ use syntax::{
7
7
ast:: { self , Impl , NameOwner } ,
8
8
AstNode ,
9
9
} ;
10
+ use crate :: utils:: generate_trait_impl_text;
10
11
11
12
// Assist: generate_default_from_new
12
13
//
@@ -59,13 +60,21 @@ pub(crate) fn generate_default_from_new(acc: &mut Assists, ctx: &AssistContext)
59
60
}
60
61
61
62
let insert_location = impl_. syntax ( ) . text_range ( ) ;
62
-
63
+ let code = match ast:: Struct :: cast ( impl_. self_ty ( ) . unwrap ( ) . syntax ( ) . clone ( ) ) {
64
+ None => {
65
+ default_fn_node_for_new ( impl_)
66
+ }
67
+ Some ( strukt) => {
68
+ generate_trait_impl_text ( & ast:: Adt :: Struct ( strukt) , "core:default:Default" , " fn default() -> Self {{
69
+ Self::new()
70
+ }}" )
71
+ }
72
+ } ;
63
73
acc. add (
64
74
AssistId ( "generate_default_from_new" , crate :: AssistKind :: Generate ) ,
65
75
"Generate a Default impl from a new fn" ,
66
76
insert_location,
67
77
move |builder| {
68
- let code = default_fn_node_for_new ( impl_) ;
69
78
builder. insert ( insert_location. end ( ) , code) ;
70
79
} ,
71
80
)
@@ -175,6 +184,40 @@ impl Default for Test {
175
184
) ;
176
185
}
177
186
187
+ #[ test]
188
+ fn generate_default3 ( ) {
189
+ check_pass (
190
+ r#"
191
+ pub struct Foo<T> {
192
+ _bar: *mut T,
193
+ }
194
+
195
+ impl<T> Foo<T> {
196
+ pub fn ne$0w() -> Self {
197
+ todo!()
198
+ }
199
+ }
200
+ "# ,
201
+ r#"
202
+ pub struct Foo<T> {
203
+ _bar: *mut T,
204
+ }
205
+
206
+ impl<T> Foo<T> {
207
+ pub fn new() -> Self {
208
+ todo!()
209
+ }
210
+ }
211
+
212
+ impl<T> Default for Foo<T> {
213
+ fn default() -> Self {
214
+ Self::new()
215
+ }
216
+ }
217
+ "# ,
218
+ ) ;
219
+ }
220
+
178
221
#[ test]
179
222
fn new_function_with_parameters ( ) {
180
223
cov_mark:: check!( new_function_with_parameters) ;
0 commit comments