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