diff options
author | bors[bot] <26634292+bors[bot]@users.noreply.github.com> | 2020-05-07 15:28:47 +0100 |
---|---|---|
committer | GitHub <[email protected]> | 2020-05-07 15:28:47 +0100 |
commit | c7e305731c922a2d32eda89ff22cb636059bc4e7 (patch) | |
tree | 4f3ab3a70fbbb901ccec3cd162da00eaa9cbad09 /crates/ra_assists/src/handlers/add_impl.rs | |
parent | f4cd75ac06dff7f5a95065a6c3868669e5c2ab27 (diff) | |
parent | 4867968d22899395e6551f22641b3617e995140c (diff) |
Merge #4350
4350: Refactor assists API to be more convenient for adding new assists r=matklad a=matklad
Co-authored-by: Aleksey Kladov <[email protected]>
Diffstat (limited to 'crates/ra_assists/src/handlers/add_impl.rs')
-rw-r--r-- | crates/ra_assists/src/handlers/add_impl.rs | 61 |
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 | }; |
5 | use stdx::{format_to, SepBy}; | 5 | use stdx::{format_to, SepBy}; |
6 | 6 | ||
7 | use crate::{Assist, AssistCtx, AssistId}; | 7 | use 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 | // ``` |
28 | pub(crate) fn add_impl(ctx: AssistCtx) -> Option<Assist> { | 28 | pub(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)] |