2 assist_context::{AssistContext, Assists},
5 use ide_db::helpers::FamousDefs;
7 ast::{self, Impl, NameOwner},
10 use crate::utils::generate_trait_impl_text;
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 cov_mark::hit!(other_function_than_new);
50 if fn_node.param_list()?.params().next().is_some() {
51 cov_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 cov_mark::hit!(default_block_is_already_present);
58 cov_mark::hit!(struct_in_module_with_default);
62 let insert_location = impl_.syntax().text_range();
63 let code = match ast::Struct::cast(impl_.self_ty().unwrap().syntax().clone()){
65 default_fn_node_for_new(impl_)
68 generate_trait_impl_text(&ast::Adt::Struct(strukt),"core:default:Default"," fn default() -> Self {{
74 AssistId("generate_default_from_new", crate::AssistKind::Generate),
75 "Generate a Default impl from a new fn",
78 builder.insert(insert_location.end(), code);
83 fn default_fn_node_for_new(impl_: Impl) -> String {
87 impl Default for {} {{
88 fn default() -> Self {{
92 impl_.self_ty().unwrap().syntax().text()
96 fn is_default_implemented(ctx: &AssistContext, impl_: &Impl) -> bool {
98 let impl_ = ctx.sema.to_def(impl_);
99 let impl_def = match impl_ {
100 Some(value) => value,
101 None => return false,
104 let ty = impl_def.self_ty(db);
105 let krate = impl_def.module(db).krate();
106 let default = FamousDefs(&ctx.sema, Some(krate)).core_default_Default();
107 let default_trait = match default {
108 Some(value) => value,
109 None => return false,
112 ty.impls_trait(db, default_trait, &[])
117 use ide_db::helpers::FamousDefs;
119 use crate::tests::{check_assist, check_assist_not_applicable};
124 fn generate_default() {
127 struct Example { _inner: () }
130 pub fn ne$0w() -> Self {
138 struct Example { _inner: () }
141 pub fn new() -> Self {
146 impl Default for Example {
147 fn default() -> Self {
158 fn generate_default2() {
161 struct Test { value: u32 }
164 pub fn ne$0w() -> Self {
170 struct Test { value: u32 }
173 pub fn new() -> Self {
178 impl Default for Test {
179 fn default() -> Self {
188 fn generate_default3() {
196 pub fn ne$0w() -> Self {
207 pub fn new() -> Self {
212 impl<T> Default for Foo<T> {
213 fn default() -> Self {
222 fn new_function_with_parameters() {
223 cov_mark::check!(new_function_with_parameters);
224 check_not_applicable(
226 struct Example { _inner: () }
229 pub fn $0new(value: ()) -> Self {
230 Self { _inner: value }
238 fn other_function_than_new() {
239 cov_mark::check!(other_function_than_new);
240 check_not_applicable(
242 struct Example { _inner: () }
245 pub fn a$0dd() -> Self {
255 fn default_block_is_already_present() {
256 cov_mark::check!(default_block_is_already_present);
257 check_not_applicable(
259 struct Example { _inner: () }
262 pub fn n$0ew() -> Self {
267 impl Default for Example {
268 fn default() -> Self {
277 fn standalone_new_function() {
278 check_not_applicable(
288 fn multiple_struct_blocks() {
291 struct Example { _inner: () }
292 struct Test { value: u32 }
295 pub fn new$0() -> Self {
301 struct Example { _inner: () }
302 struct Test { value: u32 }
305 pub fn new() -> Self {
310 impl Default for Example {
311 fn default() -> Self {
320 fn when_struct_is_after_impl() {
324 pub fn $0new() -> Self {
329 struct Example { _inner: () }
333 pub fn new() -> Self {
338 impl Default for Example {
339 fn default() -> Self {
344 struct Example { _inner: () }
350 fn struct_in_module() {
354 struct Example { _inner: () }
357 pub fn n$0ew() -> Self {
365 struct Example { _inner: () }
368 pub fn new() -> Self {
373 impl Default for Example {
374 fn default() -> Self {
384 fn struct_in_module_with_default() {
385 cov_mark::check!(struct_in_module_with_default);
386 check_not_applicable(
389 struct Example { _inner: () }
392 pub fn n$0ew() -> Self {
397 impl Default for Example {
398 fn default() -> Self {
407 fn check_pass(before: &str, after: &str) {
408 let before = &format!("//- /main.rs crate:main deps:core{}{}", before, FamousDefs::FIXTURE);
409 check_assist(generate_default_from_new, before, after);
412 fn check_not_applicable(before: &str) {
413 let before = &format!("//- /main.rs crate:main deps:core{}{}", before, FamousDefs::FIXTURE);
414 check_assist_not_applicable(generate_default_from_new, before);