1 use crate::{AssistId, assist_context::{AssistContext, Assists}};
2 use syntax::{AstNode, SyntaxKind, SyntaxNode, SyntaxText, ast::{self, NameOwner}};
5 // Assist: generate_default_from_new
7 // Generates default implementation from new method
10 // struct Example { _inner: () }
13 // pu|b fn new() -> Self {
14 // Self { _inner: () }
20 // struct Example { _inner: () }
23 // pub fn new() -> Self {
24 // Self { _inner: () }
28 // impl Default for Example {
29 // fn default() -> Self {
34 pub(crate) fn generate_default_from_new(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
35 let fn_node: ast::Fn = ctx.find_node_at_offset()?;
36 let fn_name = fn_node.name()?.to_string();
38 if !fn_name.eq("new") {
39 mark::hit!(other_function_than_new);
43 if fn_node.param_list()?.params().count() != 0 {
44 mark::hit!(new_function_with_parameters);
48 let insert_after = scope_for_fn_insertion_node(&fn_node.syntax())?;
49 let impl_obj = ast::Impl::cast(insert_after)?;
50 let struct_name = impl_obj.self_ty()?.syntax().text();
52 let default_fn_syntax = default_fn_node_for_new(struct_name);
56 AssistId("generate_default_from_new", crate::AssistKind::Generate),
57 "Generate a Default impl from a new fn",
58 impl_obj.syntax().text_range(),
60 // TODO: indentation logic can also go here.
61 // let new_indent = IndentLevel::from_node(&insert_after);
62 let insert_location = impl_obj.syntax().text_range().end();
63 builder.insert(insert_location, default_fn_syntax);
68 fn scope_for_fn_insertion_node(node: &SyntaxNode) -> Option<SyntaxNode> {
69 node.ancestors().into_iter().find(|node| node.kind() == SyntaxKind::IMPL)
72 fn default_fn_node_for_new(struct_name: SyntaxText) -> String {
73 // TODO: Update the implementation to consider the code indentation.
77 impl Default for {} {{
78 fn default() -> Self {{
87 use crate::tests::{check_assist, check_assist_not_applicable};
92 fn generate_default() {
94 generate_default_from_new,
96 struct Example { _inner: () }
99 pub fn ne$0w() -> Self {
107 struct Example { _inner: () }
110 pub fn new() -> Self {
115 impl Default for Example {
116 fn default() -> Self {
127 fn generate_default2() {
129 generate_default_from_new,
131 struct Test { value: u32 }
134 pub fn ne$0w() -> Self {
140 struct Test { value: u32 }
143 pub fn new() -> Self {
148 impl Default for Test {
149 fn default() -> Self {
158 fn new_function_with_parameters() {
159 mark::check!(new_function_with_parameters);
160 check_assist_not_applicable(generate_default_from_new,
162 struct Example { _inner: () }
165 pub fn $0new(value: ()) -> Self {
166 Self { _inner: value }
174 fn other_function_than_new() {
175 mark::check!(other_function_than_new);
176 check_assist_not_applicable(generate_default_from_new,
178 struct Example { _inner: () }
181 pub fn a$0dd() -> Self {
191 // fn default_block_is_already_present() {
192 // check_assist_not_applicable(generate_default_from_new,
194 // struct Example { _inner: () }
197 // pub fn n$0ew() -> Self {
198 // Self { _inner: () }
202 // impl Default for Example {
203 // fn default() -> Self {
212 fn standalone_new_function() {
213 check_assist_not_applicable(generate_default_from_new,