2 assist_context::{AssistContext, Assists},
6 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 mark::hit!(other_function_than_new);
49 if fn_node.param_list()?.params().next().is_some() {
50 mark::hit!(new_function_with_parameters);
54 let impl_ = fn_node.syntax().ancestors().into_iter().find_map(ast::Impl::cast)?;
56 let insert_location = impl_.syntax().text_range();
59 AssistId("generate_default_from_new", crate::AssistKind::Generate),
60 "Generate a Default impl from a new fn",
63 let code = default_fn_node_for_new(impl_);
64 builder.insert(insert_location.end(), code);
69 fn default_fn_node_for_new(impl_: Impl) -> String {
73 impl Default for {} {{
74 fn default() -> Self {{
78 impl_.self_ty().unwrap().syntax().text()
84 use crate::tests::{check_assist, check_assist_not_applicable};
89 fn generate_default() {
91 generate_default_from_new,
93 struct Example { _inner: () }
96 pub fn ne$0w() -> Self {
104 struct Example { _inner: () }
107 pub fn new() -> Self {
112 impl Default for Example {
113 fn default() -> Self {
124 fn generate_default2() {
126 generate_default_from_new,
128 struct Test { value: u32 }
131 pub fn ne$0w() -> Self {
137 struct Test { value: u32 }
140 pub fn new() -> Self {
145 impl Default for Test {
146 fn default() -> Self {
155 fn new_function_with_parameters() {
156 mark::check!(new_function_with_parameters);
157 check_assist_not_applicable(
158 generate_default_from_new,
160 struct Example { _inner: () }
163 pub fn $0new(value: ()) -> Self {
164 Self { _inner: value }
172 fn other_function_than_new() {
173 mark::check!(other_function_than_new);
174 check_assist_not_applicable(
175 generate_default_from_new,
177 struct Example { _inner: () }
180 pub fn a$0dd() -> Self {
190 // fn default_block_is_already_present() {
191 // check_assist_not_applicable(generate_default_from_new,
193 // struct Example { _inner: () }
196 // pub fn n$0ew() -> Self {
197 // Self { _inner: () }
201 // impl Default for Example {
202 // fn default() -> Self {
211 fn standalone_new_function() {
212 check_assist_not_applicable(
213 generate_default_from_new,
223 fn multiple_struct_blocks() {
225 generate_default_from_new,
227 struct Example { _inner: () }
228 struct Test { value: u32 }
231 pub fn new$0() -> Self {
237 struct Example { _inner: () }
238 struct Test { value: u32 }
241 pub fn new() -> Self {
246 impl Default for Example {
247 fn default() -> Self {
256 fn when_struct_is_after_impl() {
258 generate_default_from_new,
261 pub fn $0new() -> Self {
266 struct Example { _inner: () }
270 pub fn new() -> Self {
275 impl Default for Example {
276 fn default() -> Self {
281 struct Example { _inner: () }