aboutsummaryrefslogtreecommitdiff
path: root/crates
diff options
context:
space:
mode:
authorChetan Khilosiya <[email protected]>2021-03-01 20:04:18 +0000
committerChetan Khilosiya <[email protected]>2021-03-06 19:19:03 +0000
commita59a97ae04224b1de719d0f4d505f46e5e4ecb96 (patch)
tree4a9f1e2cabd03ff668339556757cc7124f58a17e /crates
parent54b4727fa3a4628bac6a29fc0b3034da8c5824c0 (diff)
7708: Updated generate default fn logic.
Diffstat (limited to 'crates')
-rw-r--r--crates/ide_assists/src/handlers/generate_default_from_new.rs70
1 files changed, 48 insertions, 22 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 b4f80fec9..9592876b4 100644
--- a/crates/ide_assists/src/handlers/generate_default_from_new.rs
+++ b/crates/ide_assists/src/handlers/generate_default_from_new.rs
@@ -1,10 +1,10 @@
1use crate::{ 1use crate::{
2 assist_context::{AssistContext, Assists}, 2 assist_context::{AssistContext, Assists},
3 utils, AssistId, 3 AssistId,
4}; 4};
5use syntax::{ 5use syntax::{
6 ast::{self, Adt, Impl, NameOwner}, 6 ast::{self, Impl, NameOwner},
7 AstNode, Direction, 7 AstNode,
8}; 8};
9use test_utils::mark; 9use test_utils::mark;
10 10
@@ -60,28 +60,23 @@ pub(crate) fn generate_default_from_new(acc: &mut Assists, ctx: &AssistContext)
60 "Generate a Default impl from a new fn", 60 "Generate a Default impl from a new fn",
61 insert_location, 61 insert_location,
62 move |builder| { 62 move |builder| {
63 let default_fn_syntax = default_fn_node_for_new(impl_); 63 let code = default_fn_node_for_new(impl_);
64 if let Some(code) = default_fn_syntax { 64 builder.insert(insert_location.end(), code);
65 builder.insert(insert_location.end(), code)
66 }
67 }, 65 },
68 ) 66 )
69} 67}
70 68
71fn default_fn_node_for_new(impl_: Impl) -> Option<String> { 69fn default_fn_node_for_new(impl_: Impl) -> String {
72 // the code string is this way due to formatting reason 70 format!(
73 let code = r#" fn default() -> Self { 71 "
72
73impl Default for {} {{
74 fn default() -> Self {{
74 Self::new() 75 Self::new()
75 }"#; 76 }}
76 let struct_name = impl_.self_ty()?.syntax().to_string(); 77}}",
77 let struct_ = impl_ 78 impl_.self_ty().unwrap().syntax().text()
78 .syntax() 79 )
79 .siblings(Direction::Prev)
80 .filter_map(ast::Struct::cast)
81 .find(|struct_| struct_.name().unwrap().text() == struct_name)?;
82
83 let adt = Adt::cast(struct_.syntax().clone())?;
84 Some(utils::generate_trait_impl_text(&adt, "Default", code))
85} 80}
86 81
87#[cfg(test)] 82#[cfg(test)]
@@ -233,7 +228,7 @@ struct Example { _inner: () }
233struct Test { value: u32 } 228struct Test { value: u32 }
234 229
235impl Example { 230impl Example {
236 pub fn new$0 () -> Self { 231 pub fn new$0() -> Self {
237 Self { _inner: () } 232 Self { _inner: () }
238 } 233 }
239} 234}
@@ -243,7 +238,36 @@ struct Example { _inner: () }
243struct Test { value: u32 } 238struct Test { value: u32 }
244 239
245impl Example { 240impl Example {
246 pub fn new () -> Self { 241 pub fn new() -> Self {
242 Self { _inner: () }
243 }
244}
245
246impl Default for Example {
247 fn default() -> Self {
248 Self::new()
249 }
250}
251"#,
252 );
253 }
254
255 #[test]
256 fn when_struct_is_after_impl() {
257 check_assist(
258 generate_default_from_new,
259 r#"
260impl Example {
261 pub fn $0new() -> Self {
262 Self { _inner: () }
263 }
264}
265
266struct Example { _inner: () }
267"#,
268 r#"
269impl Example {
270 pub fn new() -> Self {
247 Self { _inner: () } 271 Self { _inner: () }
248 } 272 }
249} 273}
@@ -253,6 +277,8 @@ impl Default for Example {
253 Self::new() 277 Self::new()
254 } 278 }
255} 279}
280
281struct Example { _inner: () }
256"#, 282"#,
257 ); 283 );
258 } 284 }