2 assist_context::{AssistContext, Assists},
6 ast::{self, NameOwner},
7 AstNode, SyntaxKind, SyntaxNode, SyntaxText,
11 // Assist: generate_default_from_new
13 // Generates default implementation from new method
16 // struct Example { _inner: () }
19 // pu|b fn new() -> 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: ast::Fn = ctx.find_node_at_offset()?;
42 let fn_name = fn_node.name()?.to_string();
44 if !fn_name.eq("new") {
45 mark::hit!(other_function_than_new);
49 if fn_node.param_list()?.params().count() != 0 {
50 mark::hit!(new_function_with_parameters);
54 let insert_after = scope_for_fn_insertion_node(&fn_node.syntax())?;
55 let impl_obj = ast::Impl::cast(insert_after)?;
56 let struct_name = impl_obj.self_ty()?.syntax().text();
58 let default_fn_syntax = default_fn_node_for_new(struct_name);
61 AssistId("generate_default_from_new", crate::AssistKind::Generate),
62 "Generate a Default impl from a new fn",
63 impl_obj.syntax().text_range(),
65 // TODO: indentation logic can also go here.
66 // let new_indent = IndentLevel::from_node(&insert_after);
67 let insert_location = impl_obj.syntax().text_range().end();
68 builder.insert(insert_location, default_fn_syntax);
73 fn scope_for_fn_insertion_node(node: &SyntaxNode) -> Option<SyntaxNode> {
74 node.ancestors().into_iter().find(|node| node.kind() == SyntaxKind::IMPL)
77 fn default_fn_node_for_new(struct_name: SyntaxText) -> String {
78 // TODO: Update the implementation to consider the code indentation.
82 impl Default for {} {{
83 fn default() -> Self {{
93 use crate::tests::{check_assist, check_assist_not_applicable};
98 fn generate_default() {
100 generate_default_from_new,
102 struct Example { _inner: () }
105 pub fn ne$0w() -> Self {
113 struct Example { _inner: () }
116 pub fn new() -> Self {
121 impl Default for Example {
122 fn default() -> Self {
133 fn generate_default2() {
135 generate_default_from_new,
137 struct Test { value: u32 }
140 pub fn ne$0w() -> Self {
146 struct Test { value: u32 }
149 pub fn new() -> Self {
154 impl Default for Test {
155 fn default() -> Self {
164 fn new_function_with_parameters() {
165 mark::check!(new_function_with_parameters);
166 check_assist_not_applicable(
167 generate_default_from_new,
169 struct Example { _inner: () }
172 pub fn $0new(value: ()) -> Self {
173 Self { _inner: value }
181 fn other_function_than_new() {
182 mark::check!(other_function_than_new);
183 check_assist_not_applicable(
184 generate_default_from_new,
186 struct Example { _inner: () }
189 pub fn a$0dd() -> Self {
199 // fn default_block_is_already_present() {
200 // check_assist_not_applicable(generate_default_from_new,
202 // struct Example { _inner: () }
205 // pub fn n$0ew() -> Self {
206 // Self { _inner: () }
210 // impl Default for Example {
211 // fn default() -> Self {
220 fn standalone_new_function() {
221 check_assist_not_applicable(
222 generate_default_from_new,