diff options
author | vsrs <[email protected]> | 2020-05-08 17:34:34 +0100 |
---|---|---|
committer | vsrs <[email protected]> | 2020-05-08 17:34:34 +0100 |
commit | 0ef17ef1ee9fb0ce7149176d12f4d225f6d01401 (patch) | |
tree | fa2f168120f36f8dbef5dc1e85fea0c0071639c2 /crates/ra_assists/src/handlers/add_impl.rs | |
parent | 1be6320ea6cf7830195f80681fa0f43cc340da7e (diff) | |
parent | d3eb9d8eafbebca7da95fa8a4813b92eb5080500 (diff) |
Merge remote-tracking branch 'upstream/master' into uniformed_debug_lens
# Conflicts:
# editors/code/src/commands/runnables.ts
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)] |