2 assist_context::{AssistContext, Assists},
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_).is_some() {
60 let insert_location = impl_.syntax().text_range();
63 AssistId("generate_default_from_new", crate::AssistKind::Generate),
64 "Generate a Default impl from a new fn",
67 let code = default_fn_node_for_new(impl_);
68 builder.insert(insert_location.end(), code);
73 fn default_fn_node_for_new(impl_: Impl) -> String {
77 impl Default for {} {{
78 fn default() -> Self {{
82 impl_.self_ty().unwrap().syntax().text()
86 fn is_default_implemented(ctx: &AssistContext, impl_: &Impl) -> Option<bool> {
88 let module = impl_.syntax().parent()?;
89 let sema_scope = ctx.sema.scope(&module);
90 let impls = sema_scope.module()?.impl_defs(db);
93 if let Some(TypeRef::Path(p)) = i.target_trait(db) {
94 name = p.segments().iter().map(|s| s.name.to_string()).find(|n| n == "Default");
98 name.map(|n| !n.is_empty())
103 use crate::tests::{check_assist, check_assist_not_applicable};
108 fn generate_default() {
110 generate_default_from_new,
112 struct Example { _inner: () }
115 pub fn ne$0w() -> Self {
123 struct Example { _inner: () }
126 pub fn new() -> Self {
131 impl Default for Example {
132 fn default() -> Self {
143 fn generate_default2() {
145 generate_default_from_new,
147 struct Test { value: u32 }
150 pub fn ne$0w() -> Self {
156 struct Test { value: u32 }
159 pub fn new() -> Self {
164 impl Default for Test {
165 fn default() -> Self {
174 fn new_function_with_parameters() {
175 mark::check!(new_function_with_parameters);
176 check_assist_not_applicable(
177 generate_default_from_new,
179 struct Example { _inner: () }
182 pub fn $0new(value: ()) -> Self {
183 Self { _inner: value }
191 fn other_function_than_new() {
192 mark::check!(other_function_than_new);
193 check_assist_not_applicable(
194 generate_default_from_new,
196 struct Example { _inner: () }
199 pub fn a$0dd() -> Self {
209 fn default_block_is_already_present() {
210 check_assist_not_applicable(
211 generate_default_from_new,
213 struct Example { _inner: () }
216 pub fn n$0ew() -> Self {
221 impl Default for Example {
222 fn default() -> Self {
231 fn standalone_new_function() {
232 check_assist_not_applicable(
233 generate_default_from_new,
243 fn multiple_struct_blocks() {
245 generate_default_from_new,
247 struct Example { _inner: () }
248 struct Test { value: u32 }
251 pub fn new$0() -> Self {
257 struct Example { _inner: () }
258 struct Test { value: u32 }
261 pub fn new() -> Self {
266 impl Default for Example {
267 fn default() -> Self {
276 fn when_struct_is_after_impl() {
278 generate_default_from_new,
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() {
309 generate_default_from_new,
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 check_assist_not_applicable(
344 generate_default_from_new,
347 struct Example { _inner: () }
350 pub fn n$0ew() -> Self {
355 impl Default for Example {
356 fn default() -> Self {