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.rs89
1 files changed, 42 insertions, 47 deletions
diff --git a/crates/ra_assists/src/handlers/add_new.rs b/crates/ra_assists/src/handlers/add_new.rs
index 84eda5486..0b3d29c7c 100644
--- a/crates/ra_assists/src/handlers/add_new.rs
+++ b/crates/ra_assists/src/handlers/add_new.rs
@@ -42,55 +42,50 @@ pub(crate) fn add_new(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
42 let impl_def = find_struct_impl(&ctx, &strukt)?; 42 let impl_def = find_struct_impl(&ctx, &strukt)?;
43 43
44 let target = strukt.syntax().text_range(); 44 let target = strukt.syntax().text_range();
45 acc.add( 45 acc.add(AssistId("add_new", AssistKind::None), "Add default constructor", target, |builder| {
46 AssistId("add_new", AssistKind::Refactor), 46 let mut buf = String::with_capacity(512);
47 "Add default constructor",
48 target,
49 |builder| {
50 let mut buf = String::with_capacity(512);
51
52 if impl_def.is_some() {
53 buf.push('\n');
54 }
55 47
56 let vis = strukt.visibility().map_or(String::new(), |v| format!("{} ", v)); 48 if impl_def.is_some() {
57 49 buf.push('\n');
58 let params = field_list 50 }
59 .fields() 51
60 .filter_map(|f| { 52 let vis = strukt.visibility().map_or(String::new(), |v| format!("{} ", v));
61 Some(format!("{}: {}", f.name()?.syntax(), f.ascribed_type()?.syntax())) 53
62 }) 54 let params = field_list
63 .sep_by(", "); 55 .fields()
64 let fields = field_list.fields().filter_map(|f| f.name()).sep_by(", "); 56 .filter_map(|f| {
65 57 Some(format!("{}: {}", f.name()?.syntax(), f.ascribed_type()?.syntax()))
66 format_to!(buf, " {}fn new({}) -> Self {{ Self {{ {} }} }}", vis, params, fields); 58 })
67 59 .sep_by(", ");
68 let start_offset = impl_def 60 let fields = field_list.fields().filter_map(|f| f.name()).sep_by(", ");
69 .and_then(|impl_def| { 61
70 buf.push('\n'); 62 format_to!(buf, " {}fn new({}) -> Self {{ Self {{ {} }} }}", vis, params, fields);
71 let start = impl_def 63
72 .syntax() 64 let start_offset = impl_def
73 .descendants_with_tokens() 65 .and_then(|impl_def| {
74 .find(|t| t.kind() == T!['{'])? 66 buf.push('\n');
75 .text_range() 67 let start = impl_def
76 .end(); 68 .syntax()
77 69 .descendants_with_tokens()
78 Some(start) 70 .find(|t| t.kind() == T!['{'])?
79 }) 71 .text_range()
80 .unwrap_or_else(|| { 72 .end();
81 buf = generate_impl_text(&strukt, &buf); 73
82 strukt.syntax().text_range().end() 74 Some(start)
83 }); 75 })
84 76 .unwrap_or_else(|| {
85 match ctx.config.snippet_cap { 77 buf = generate_impl_text(&strukt, &buf);
86 None => builder.insert(start_offset, buf), 78 strukt.syntax().text_range().end()
87 Some(cap) => { 79 });
88 buf = buf.replace("fn new", "fn $0new"); 80
89 builder.insert_snippet(cap, start_offset, buf); 81 match ctx.config.snippet_cap {
90 } 82 None => builder.insert(start_offset, buf),
83 Some(cap) => {
84 buf = buf.replace("fn new", "fn $0new");
85 builder.insert_snippet(cap, start_offset, buf);
91 } 86 }
92 }, 87 }
93 ) 88 })
94} 89}
95 90
96// Generates the surrounding `impl Type { <code> }` including type and lifetime 91// Generates the surrounding `impl Type { <code> }` including type and lifetime