2 assist_context::{AssistContext, Assists},
5 use ide_db::helpers::FamousDefs;
7 ast::{self, Impl, NameOwner},
12 // Assist: generate_default_from_new
14 // Generates default implementation from new method.
17 // struct Example { _inner: () }
20 // pub fn n$0ew() -> Self {
21 // Self { _inner: () }
27 // struct Example { _inner: () }
30 // pub fn new() -> Self {
31 // Self { _inner: () }
35 // impl Default for Example {
36 // fn default() -> Self {
41 pub(crate) fn generate_default_from_new(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
42 let fn_node = ctx.find_node_at_offset::<ast::Fn>()?;
43 let fn_name = fn_node.name()?;
45 if fn_name.text() != "new" {
46 mark::hit!(other_function_than_new);
50 if fn_node.param_list()?.params().next().is_some() {
51 mark::hit!(new_function_with_parameters);
55 let impl_ = fn_node.syntax().ancestors().into_iter().find_map(ast::Impl::cast)?;
56 let implements_default = is_default_implemented(ctx, &impl_)?;
57 if implements_default {
61 let insert_location = impl_.syntax().text_range();
64 AssistId("generate_default_from_new", crate::AssistKind::Generate),
65 "Generate a Default impl from a new fn",
68 let code = default_fn_node_for_new(impl_);
69 builder.insert(insert_location.end(), code);
74 fn default_fn_node_for_new(impl_: Impl) -> String {
78 impl Default for {} {{
79 fn default() -> Self {{
83 impl_.self_ty().unwrap().syntax().text()
87 fn is_default_implemented(ctx: &AssistContext, impl_: &Impl) -> Option<bool> {
89 let impl_def = ctx.sema.to_def(impl_)?;
90 let ty = impl_def.target_ty(db);
91 let krate = impl_def.module(db).krate();
92 let default_trait = FamousDefs(&ctx.sema, Some(krate)).core_default_Default()?;
93 let implements_default = ty.impls_trait(db, default_trait, &[]);
94 Some(implements_default)
99 use ide_db::helpers::FamousDefs;
101 use crate::tests::{check_assist, check_assist_not_applicable};
106 fn generate_default() {
109 struct Example { _inner: () }
112 pub fn ne$0w() -> Self {
120 struct Example { _inner: () }
123 pub fn new() -> Self {
128 impl Default for Example {
129 fn default() -> Self {
140 fn generate_default2() {
143 struct Test { value: u32 }
146 pub fn ne$0w() -> Self {
152 struct Test { value: u32 }
155 pub fn new() -> Self {
160 impl Default for Test {
161 fn default() -> Self {
170 fn new_function_with_parameters() {
171 mark::check!(new_function_with_parameters);
172 check_not_applicable(
174 struct Example { _inner: () }
177 pub fn $0new(value: ()) -> Self {
178 Self { _inner: value }
186 fn other_function_than_new() {
187 mark::check!(other_function_than_new);
188 check_not_applicable(
190 struct Example { _inner: () }
193 pub fn a$0dd() -> Self {
203 fn default_block_is_already_present() {
204 check_not_applicable(
206 struct Example { _inner: () }
209 pub fn n$0ew() -> Self {
214 impl Default for Example {
215 fn default() -> Self {
224 fn standalone_new_function() {
225 check_not_applicable(
235 fn multiple_struct_blocks() {
238 struct Example { _inner: () }
239 struct Test { value: u32 }
242 pub fn new$0() -> Self {
248 struct Example { _inner: () }
249 struct Test { value: u32 }
252 pub fn new() -> Self {
257 impl Default for Example {
258 fn default() -> Self {
267 fn when_struct_is_after_impl() {
271 pub fn $0new() -> Self {
276 struct Example { _inner: () }
280 pub fn new() -> Self {
285 impl Default for Example {
286 fn default() -> Self {
291 struct Example { _inner: () }
297 fn struct_in_module() {
301 struct Example { _inner: () }
304 pub fn n$0ew() -> Self {
312 struct Example { _inner: () }
315 pub fn new() -> Self {
320 impl Default for Example {
321 fn default() -> Self {
331 fn struct_in_module_with_default() {
332 check_not_applicable(
335 struct Example { _inner: () }
338 pub fn n$0ew() -> Self {
343 impl Default for Example {
344 fn default() -> Self {
353 fn check_pass(before: &str, after: &str) {
354 let before = &format!("//- /main.rs crate:main deps:core{}{}", before, FamousDefs::FIXTURE);
355 check_assist(generate_default_from_new, before, after);
358 fn check_not_applicable(before: &str) {
359 let before = &format!("//- /main.rs crate:main deps:core{}{}", before, FamousDefs::FIXTURE);
360 check_assist_not_applicable(generate_default_from_new, before);