aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_assists/src/handlers/add_impl.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_assists/src/handlers/add_impl.rs')
-rw-r--r--crates/ra_assists/src/handlers/add_impl.rs61
1 files changed, 27 insertions, 34 deletions
diff --git a/crates/ra_assists/src/handlers/add_impl.rs b/crates/ra_assists/src/handlers/add_impl.rs
index 557344ebb..df114a0d8 100644
--- a/crates/ra_assists/src/handlers/add_impl.rs
+++ b/crates/ra_assists/src/handlers/add_impl.rs
@@ -4,7 +4,7 @@ use ra_syntax::{
4}; 4};
5use stdx::{format_to, SepBy}; 5use stdx::{format_to, SepBy};
6 6
7use crate::{Assist, AssistCtx, AssistId}; 7use crate::{AssistContext, AssistId, Assists};
8 8
9// Assist: add_impl 9// Assist: add_impl
10// 10//
@@ -25,43 +25,36 @@ use crate::{Assist, AssistCtx, AssistId};
25// 25//
26// } 26// }
27// ``` 27// ```
28pub(crate) fn add_impl(ctx: AssistCtx) -> Option<Assist> { 28pub(crate) fn add_impl(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 name = nominal.name()?; 30 let name = nominal.name()?;
31 let target = nominal.syntax().text_range(); 31 let target = nominal.syntax().text_range();
32 ctx.add_assist( 32 acc.add(AssistId("add_impl"), format!("Implement {}", name.text().as_str()), target, |edit| {
33 AssistId("add_impl"), 33 let type_params = nominal.type_param_list();
34 format!("Implement {}", name.text().as_str()), 34 let start_offset = nominal.syntax().text_range().end();
35 target, 35 let mut buf = String::new();
36 |edit| { 36 buf.push_str("\n\nimpl");
37 let type_params = nominal.type_param_list(); 37 if let Some(type_params) = &type_params {
38 let start_offset = nominal.syntax().text_range().end(); 38 format_to!(buf, "{}", type_params.syntax());
39 let mut buf = String::new(); 39 }
40 buf.push_str("\n\nimpl"); 40 buf.push_str(" ");
41 if let Some(type_params) = &type_params { 41 buf.push_str(name.text().as_str());
42 format_to!(buf, "{}", type_params.syntax()); 42 if let Some(type_params) = type_params {
43 } 43 let lifetime_params = type_params
44 buf.push_str(" "); 44 .lifetime_params()
45 buf.push_str(name.text().as_str()); 45 .filter_map(|it| it.lifetime_token())
46 if let Some(type_params) = type_params { 46 .map(|it| it.text().clone());
47 let lifetime_params = type_params 47 let type_params =
48 .lifetime_params() 48 type_params.type_params().filter_map(|it| it.name()).map(|it| it.text().clone());
49 .filter_map(|it| it.lifetime_token())
50 .map(|it| it.text().clone());
51 let type_params = type_params
52 .type_params()
53 .filter_map(|it| it.name())
54 .map(|it| it.text().clone());
55 49
56 let generic_params = lifetime_params.chain(type_params).sep_by(", "); 50 let generic_params = lifetime_params.chain(type_params).sep_by(", ");
57 format_to!(buf, "<{}>", generic_params) 51 format_to!(buf, "<{}>", generic_params)
58 } 52 }
59 buf.push_str(" {\n"); 53 buf.push_str(" {\n");
60 edit.set_cursor(start_offset + TextSize::of(&buf)); 54 edit.set_cursor(start_offset + TextSize::of(&buf));
61 buf.push_str("\n}"); 55 buf.push_str("\n}");
62 edit.insert(start_offset, buf); 56 edit.insert(start_offset, buf);
63 }, 57 })
64 )
65} 58}
66 59
67#[cfg(test)] 60#[cfg(test)]