aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_assists/src/handlers/add_new.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_assists/src/handlers/add_new.rs')
-rw-r--r--crates/ra_assists/src/handlers/add_new.rs39
1 files changed, 17 insertions, 22 deletions
diff --git a/crates/ra_assists/src/handlers/add_new.rs b/crates/ra_assists/src/handlers/add_new.rs
index 729a223e0..240b19fa3 100644
--- a/crates/ra_assists/src/handlers/add_new.rs
+++ b/crates/ra_assists/src/handlers/add_new.rs
@@ -1,14 +1,11 @@
1use std::fmt::Write;
2
3use format_buf::format;
4use hir::Adt; 1use hir::Adt;
5use join_to_string::join;
6use ra_syntax::{ 2use ra_syntax::{
7 ast::{ 3 ast::{
8 self, AstNode, NameOwner, StructKind, TypeAscriptionOwner, TypeParamsOwner, VisibilityOwner, 4 self, AstNode, NameOwner, StructKind, TypeAscriptionOwner, TypeParamsOwner, VisibilityOwner,
9 }, 5 },
10 TextUnit, T, 6 TextUnit, T,
11}; 7};
8use stdx::{format_to, SepBy};
12 9
13use crate::{Assist, AssistCtx, AssistId}; 10use crate::{Assist, AssistCtx, AssistId};
14 11
@@ -53,24 +50,22 @@ pub(crate) fn add_new(ctx: AssistCtx) -> Option<Assist> {
53 buf.push('\n'); 50 buf.push('\n');
54 } 51 }
55 52
56 let vis = strukt.visibility().map(|v| format!("{} ", v.syntax())); 53 let vis = strukt.visibility().map(|v| format!("{} ", v));
57 let vis = vis.as_deref().unwrap_or(""); 54 let vis = vis.as_deref().unwrap_or("");
58 write!(&mut buf, " {}fn new(", vis).unwrap();
59
60 join(field_list.fields().filter_map(|f| {
61 Some(format!("{}: {}", f.name()?.syntax().text(), f.ascribed_type()?.syntax().text()))
62 }))
63 .separator(", ")
64 .to_buf(&mut buf);
65 55
66 buf.push_str(") -> Self { Self {"); 56 let params = field_list
67 57 .fields()
68 join(field_list.fields().filter_map(|f| Some(f.name()?.syntax().text()))) 58 .filter_map(|f| {
69 .separator(", ") 59 Some(format!(
70 .surround_with(" ", " ") 60 "{}: {}",
71 .to_buf(&mut buf); 61 f.name()?.syntax().text(),
62 f.ascribed_type()?.syntax().text()
63 ))
64 })
65 .sep_by(", ");
66 let fields = field_list.fields().filter_map(|f| f.name()).sep_by(", ");
72 67
73 buf.push_str("} }"); 68 format_to!(buf, " {}fn new({}) -> Self {{ Self {{ {} }} }}", vis, params, fields);
74 69
75 let (start_offset, end_offset) = impl_def 70 let (start_offset, end_offset) = impl_def
76 .and_then(|impl_def| { 71 .and_then(|impl_def| {
@@ -103,7 +98,7 @@ fn generate_impl_text(strukt: &ast::StructDef, code: &str) -> String {
103 let mut buf = String::with_capacity(code.len()); 98 let mut buf = String::with_capacity(code.len());
104 buf.push_str("\n\nimpl"); 99 buf.push_str("\n\nimpl");
105 if let Some(type_params) = &type_params { 100 if let Some(type_params) = &type_params {
106 format!(buf, "{}", type_params.syntax()); 101 format_to!(buf, "{}", type_params.syntax());
107 } 102 }
108 buf.push_str(" "); 103 buf.push_str(" ");
109 buf.push_str(strukt.name().unwrap().text().as_str()); 104 buf.push_str(strukt.name().unwrap().text().as_str());
@@ -114,10 +109,10 @@ fn generate_impl_text(strukt: &ast::StructDef, code: &str) -> String {
114 .map(|it| it.text().clone()); 109 .map(|it| it.text().clone());
115 let type_params = 110 let type_params =
116 type_params.type_params().filter_map(|it| it.name()).map(|it| it.text().clone()); 111 type_params.type_params().filter_map(|it| it.name()).map(|it| it.text().clone());
117 join(lifetime_params.chain(type_params)).surround_with("<", ">").to_buf(&mut buf); 112 format_to!(buf, "<{}>", lifetime_params.chain(type_params).sep_by(", "))
118 } 113 }
119 114
120 format!(&mut buf, " {{\n{}\n}}\n", code); 115 format_to!(buf, " {{\n{}\n}}\n", code);
121 116
122 buf 117 buf
123} 118}