2 assist_context::{AssistContext, Assists},
5 use ide_db::helpers::FamousDefs;
7 ast::{self, Impl, NameOwner},
11 // Assist: generate_default_from_new
13 // Generates default implementation from new method.
16 // struct Example { _inner: () }
19 // pub fn n$0ew() -> Self {
20 // Self { _inner: () }
26 // struct Example { _inner: () }
29 // pub fn new() -> Self {
30 // Self { _inner: () }
34 // impl Default for Example {
35 // fn default() -> Self {
40 pub(crate) fn generate_default_from_new(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
41 let fn_node = ctx.find_node_at_offset::<ast::Fn>()?;
42 let fn_name = fn_node.name()?;
44 if fn_name.text() != "new" {
45 cov_mark::hit!(other_function_than_new);
49 if fn_node.param_list()?.params().next().is_some() {
50 cov_mark::hit!(new_function_with_parameters);
54 let impl_ = fn_node.syntax().ancestors().into_iter().find_map(ast::Impl::cast)?;
55 if is_default_implemented(ctx, &impl_) {
56 cov_mark::hit!(default_block_is_already_present);
57 cov_mark::hit!(struct_in_module_with_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) -> bool {
89 let impl_ = ctx.sema.to_def(impl_);
90 let impl_def = match impl_ {
95 let ty = impl_def.target_ty(db);
96 let krate = impl_def.module(db).krate();
97 let default = FamousDefs(&ctx.sema, Some(krate)).core_default_Default();
98 let default_trait = match default {
100 None => return false,
103 ty.impls_trait(db, default_trait, &[])
108 use ide_db::helpers::FamousDefs;
110 use crate::tests::{check_assist, check_assist_not_applicable};
115 fn generate_default() {
118 struct Example { _inner: () }
121 pub fn ne$0w() -> Self {
129 struct Example { _inner: () }
132 pub fn new() -> Self {
137 impl Default for Example {
138 fn default() -> Self {
149 fn generate_default2() {
152 struct Test { value: u32 }
155 pub fn ne$0w() -> Self {
161 struct Test { value: u32 }
164 pub fn new() -> Self {
169 impl Default for Test {
170 fn default() -> Self {
179 fn new_function_with_parameters() {
180 cov_mark::check!(new_function_with_parameters);
181 check_not_applicable(
183 struct Example { _inner: () }
186 pub fn $0new(value: ()) -> Self {
187 Self { _inner: value }
195 fn other_function_than_new() {
196 cov_mark::check!(other_function_than_new);
197 check_not_applicable(
199 struct Example { _inner: () }
202 pub fn a$0dd() -> Self {
212 fn default_block_is_already_present() {
213 cov_mark::check!(default_block_is_already_present);
214 check_not_applicable(
216 struct Example { _inner: () }
219 pub fn n$0ew() -> Self {
224 impl Default for Example {
225 fn default() -> Self {
234 fn standalone_new_function() {
235 check_not_applicable(
245 fn multiple_struct_blocks() {
248 struct Example { _inner: () }
249 struct Test { value: u32 }
252 pub fn new$0() -> Self {
258 struct Example { _inner: () }
259 struct Test { value: u32 }
262 pub fn new() -> Self {
267 impl Default for Example {
268 fn default() -> Self {
277 fn when_struct_is_after_impl() {
281 pub fn $0new() -> Self {
286 struct Example { _inner: () }
290 pub fn new() -> Self {
295 impl Default for Example {
296 fn default() -> Self {
301 struct Example { _inner: () }
307 fn struct_in_module() {
311 struct Example { _inner: () }
314 pub fn n$0ew() -> Self {
322 struct Example { _inner: () }
325 pub fn new() -> Self {
330 impl Default for Example {
331 fn default() -> Self {
341 fn struct_in_module_with_default() {
342 cov_mark::check!(struct_in_module_with_default);
343 check_not_applicable(
346 struct Example { _inner: () }
349 pub fn n$0ew() -> Self {
354 impl Default for Example {
355 fn default() -> Self {
364 fn check_pass(before: &str, after: &str) {
365 let before = &format!("//- /main.rs crate:main deps:core{}{}", before, FamousDefs::FIXTURE);
366 check_assist(generate_default_from_new, before, after);
369 fn check_not_applicable(before: &str) {
370 let before = &format!("//- /main.rs crate:main deps:core{}{}", before, FamousDefs::FIXTURE);
371 check_assist_not_applicable(generate_default_from_new, before);