aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_assists
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_assists')
-rw-r--r--crates/ra_assists/src/handlers/add_derive.rs2
-rw-r--r--crates/ra_assists/src/handlers/add_function.rs23
-rw-r--r--crates/ra_assists/src/handlers/add_new.rs89
-rw-r--r--crates/ra_assists/src/lib.rs2
4 files changed, 52 insertions, 64 deletions
diff --git a/crates/ra_assists/src/handlers/add_derive.rs b/crates/ra_assists/src/handlers/add_derive.rs
index 12acfbbe4..e2b94e7f8 100644
--- a/crates/ra_assists/src/handlers/add_derive.rs
+++ b/crates/ra_assists/src/handlers/add_derive.rs
@@ -29,7 +29,7 @@ pub(crate) fn add_derive(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
29 let nominal = ctx.find_node_at_offset::<ast::NominalDef>()?; 29 let nominal = ctx.find_node_at_offset::<ast::NominalDef>()?;
30 let node_start = derive_insertion_offset(&nominal)?; 30 let node_start = derive_insertion_offset(&nominal)?;
31 let target = nominal.syntax().text_range(); 31 let target = nominal.syntax().text_range();
32 acc.add(AssistId("add_derive", AssistKind::Refactor), "Add `#[derive]`", target, |builder| { 32 acc.add(AssistId("add_derive", AssistKind::None), "Add `#[derive]`", target, |builder| {
33 let derive_attr = nominal 33 let derive_attr = nominal
34 .attrs() 34 .attrs()
35 .filter_map(|x| x.as_simple_call()) 35 .filter_map(|x| x.as_simple_call())
diff --git a/crates/ra_assists/src/handlers/add_function.rs b/crates/ra_assists/src/handlers/add_function.rs
index 6f2950daf..7150eb53a 100644
--- a/crates/ra_assists/src/handlers/add_function.rs
+++ b/crates/ra_assists/src/handlers/add_function.rs
@@ -62,20 +62,15 @@ pub(crate) fn add_function(acc: &mut Assists, ctx: &AssistContext) -> Option<()>
62 let function_builder = FunctionBuilder::from_call(&ctx, &call, &path, target_module)?; 62 let function_builder = FunctionBuilder::from_call(&ctx, &call, &path, target_module)?;
63 63
64 let target = call.syntax().text_range(); 64 let target = call.syntax().text_range();
65 acc.add( 65 acc.add(AssistId("add_function", AssistKind::None), "Add function", target, |builder| {
66 AssistId("add_function", AssistKind::RefactorExtract), 66 let function_template = function_builder.render();
67 "Add function", 67 builder.edit_file(function_template.file);
68 target, 68 let new_fn = function_template.to_string(ctx.config.snippet_cap);
69 |builder| { 69 match ctx.config.snippet_cap {
70 let function_template = function_builder.render(); 70 Some(cap) => builder.insert_snippet(cap, function_template.insert_offset, new_fn),
71 builder.edit_file(function_template.file); 71 None => builder.insert(function_template.insert_offset, new_fn),
72 let new_fn = function_template.to_string(ctx.config.snippet_cap); 72 }
73 match ctx.config.snippet_cap { 73 })
74 Some(cap) => builder.insert_snippet(cap, function_template.insert_offset, new_fn),
75 None => builder.insert(function_template.insert_offset, new_fn),
76 }
77 },
78 )
79} 74}
80 75
81struct FunctionTemplate { 76struct FunctionTemplate {
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
diff --git a/crates/ra_assists/src/lib.rs b/crates/ra_assists/src/lib.rs
index 201213c73..65cda95ee 100644
--- a/crates/ra_assists/src/lib.rs
+++ b/crates/ra_assists/src/lib.rs
@@ -34,8 +34,6 @@ pub enum AssistKind {
34 RefactorExtract, 34 RefactorExtract,
35 RefactorInline, 35 RefactorInline,
36 RefactorRewrite, 36 RefactorRewrite,
37 Source,
38 OrganizeImports,
39} 37}
40 38
41/// Unique identifier of the assist, should not be shown to the user 39/// Unique identifier of the assist, should not be shown to the user