aboutsummaryrefslogtreecommitdiff
path: root/crates
diff options
context:
space:
mode:
Diffstat (limited to 'crates')
-rw-r--r--crates/ide_assists/src/handlers/generate_default_from_new.rs47
1 files changed, 45 insertions, 2 deletions
diff --git a/crates/ide_assists/src/handlers/generate_default_from_new.rs b/crates/ide_assists/src/handlers/generate_default_from_new.rs
index dc14552d6..374611fbf 100644
--- a/crates/ide_assists/src/handlers/generate_default_from_new.rs
+++ b/crates/ide_assists/src/handlers/generate_default_from_new.rs
@@ -7,6 +7,7 @@ use syntax::{
7 ast::{self, Impl, NameOwner}, 7 ast::{self, Impl, NameOwner},
8 AstNode, 8 AstNode,
9}; 9};
10use crate::utils::generate_trait_impl_text;
10 11
11// Assist: generate_default_from_new 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 let insert_location = impl_.syntax().text_range(); 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 acc.add( 73 acc.add(
64 AssistId("generate_default_from_new", crate::AssistKind::Generate), 74 AssistId("generate_default_from_new", crate::AssistKind::Generate),
65 "Generate a Default impl from a new fn", 75 "Generate a Default impl from a new fn",
66 insert_location, 76 insert_location,
67 move |builder| { 77 move |builder| {
68 let code = default_fn_node_for_new(impl_);
69 builder.insert(insert_location.end(), code); 78 builder.insert(insert_location.end(), code);
70 }, 79 },
71 ) 80 )
@@ -176,6 +185,40 @@ impl Default for Test {
176 } 185 }
177 186
178 #[test] 187 #[test]
188 fn generate_default3() {
189 check_pass(
190 r#"
191pub struct Foo<T> {
192 _bar: *mut T,
193}
194
195impl<T> Foo<T> {
196 pub fn ne$0w() -> Self {
197 todo!()
198 }
199}
200"#,
201 r#"
202pub struct Foo<T> {
203 _bar: *mut T,
204}
205
206impl<T> Foo<T> {
207 pub fn new() -> Self {
208 todo!()
209 }
210}
211
212impl<T> Default for Foo<T> {
213 fn default() -> Self {
214 Self::new()
215 }
216}
217"#,
218 );
219 }
220
221 #[test]
179 fn new_function_with_parameters() { 222 fn new_function_with_parameters() {
180 cov_mark::check!(new_function_with_parameters); 223 cov_mark::check!(new_function_with_parameters);
181 check_not_applicable( 224 check_not_applicable(