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 if is_default_implemented(ctx, &impl_) {
57 mark::hit!(default_block_is_already_present);
58 mark::hit!(struct_in_module_with_default);
62 let insert_location = impl_.syntax().text_range();
65 AssistId("generate_default_from_new", crate::AssistKind::Generate),
66 "Generate a Default impl from a new fn",
69 let code = default_fn_node_for_new(impl_);
70 builder.insert(insert_location.end(), code);
75 fn default_fn_node_for_new(impl_: Impl) -> String {
79 impl Default for {} {{
80 fn default() -> Self {{
84 impl_.self_ty().unwrap().syntax().text()
88 fn is_default_implemented(ctx: &AssistContext, impl_: &Impl) -> bool {
90 let impl_ = ctx.sema.to_def(impl_);
91 let impl_def = match impl_ {
96 let ty = impl_def.target_ty(db);
97 let krate = impl_def.module(db).krate();
98 let default = FamousDefs(&ctx.sema, Some(krate)).core_default_Default();
99 let default_trait = match default {
100 Some(value) => value,
101 None => return false,
104 ty.impls_trait(db, default_trait, &[])
109 use ide_db::helpers::FamousDefs;
111 use crate::tests::{check_assist, check_assist_not_applicable};
116 fn generate_default() {
119 struct Example { _inner: () }
122 pub fn ne$0w() -> Self {
130 struct Example { _inner: () }
133 pub fn new() -> Self {
138 impl Default for Example {
139 fn default() -> Self {
150 fn generate_default2() {
153 struct Test { value: u32 }
156 pub fn ne$0w() -> Self {
162 struct Test { value: u32 }
165 pub fn new() -> Self {
170 impl Default for Test {
171 fn default() -> Self {
180 fn new_function_with_parameters() {
181 mark::check!(new_function_with_parameters);
182 check_not_applicable(
184 struct Example { _inner: () }
187 pub fn $0new(value: ()) -> Self {
188 Self { _inner: value }
196 fn other_function_than_new() {
197 mark::check!(other_function_than_new);
198 check_not_applicable(
200 struct Example { _inner: () }
203 pub fn a$0dd() -> Self {
213 fn default_block_is_already_present() {
214 mark::check!(default_block_is_already_present);
215 check_not_applicable(
217 struct Example { _inner: () }
220 pub fn n$0ew() -> Self {
225 impl Default for Example {
226 fn default() -> Self {
235 fn standalone_new_function() {
236 check_not_applicable(
246 fn multiple_struct_blocks() {
249 struct Example { _inner: () }
250 struct Test { value: u32 }
253 pub fn new$0() -> Self {
259 struct Example { _inner: () }
260 struct Test { value: u32 }
263 pub fn new() -> Self {
268 impl Default for Example {
269 fn default() -> Self {
278 fn when_struct_is_after_impl() {
282 pub fn $0new() -> Self {
287 struct Example { _inner: () }
291 pub fn new() -> Self {
296 impl Default for Example {
297 fn default() -> Self {
302 struct Example { _inner: () }
308 fn struct_in_module() {
312 struct Example { _inner: () }
315 pub fn n$0ew() -> Self {
323 struct Example { _inner: () }
326 pub fn new() -> Self {
331 impl Default for Example {
332 fn default() -> Self {
342 fn struct_in_module_with_default() {
343 mark::check!(struct_in_module_with_default);
344 check_not_applicable(
347 struct Example { _inner: () }
350 pub fn n$0ew() -> Self {
355 impl Default for Example {
356 fn default() -> Self {
365 fn check_pass(before: &str, after: &str) {
366 let before = &format!("//- /main.rs crate:main deps:core{}{}", before, FamousDefs::FIXTURE);
367 check_assist(generate_default_from_new, before, after);
370 fn check_not_applicable(before: &str) {
371 let before = &format!("//- /main.rs crate:main deps:core{}{}", before, FamousDefs::FIXTURE);
372 check_assist_not_applicable(generate_default_from_new, before);