diff options
Diffstat (limited to 'crates/ra_assists/src/handlers/generate_derive.rs')
-rw-r--r-- | crates/ra_assists/src/handlers/generate_derive.rs | 47 |
1 files changed, 26 insertions, 21 deletions
diff --git a/crates/ra_assists/src/handlers/generate_derive.rs b/crates/ra_assists/src/handlers/generate_derive.rs index 8fc522eef..6ccf39900 100644 --- a/crates/ra_assists/src/handlers/generate_derive.rs +++ b/crates/ra_assists/src/handlers/generate_derive.rs | |||
@@ -29,27 +29,32 @@ pub(crate) fn generate_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("generate_derive", AssistKind::None), "Add `#[derive]`", target, |builder| { | 32 | acc.add( |
33 | let derive_attr = nominal | 33 | AssistId("generate_derive", AssistKind::Generate), |
34 | .attrs() | 34 | "Add `#[derive]`", |
35 | .filter_map(|x| x.as_simple_call()) | 35 | target, |
36 | .filter(|(name, _arg)| name == "derive") | 36 | |builder| { |
37 | .map(|(_name, arg)| arg) | 37 | let derive_attr = nominal |
38 | .next(); | 38 | .attrs() |
39 | match derive_attr { | 39 | .filter_map(|x| x.as_simple_call()) |
40 | None => { | 40 | .filter(|(name, _arg)| name == "derive") |
41 | builder.insert_snippet(cap, node_start, "#[derive($0)]\n"); | 41 | .map(|(_name, arg)| arg) |
42 | } | 42 | .next(); |
43 | Some(tt) => { | 43 | match derive_attr { |
44 | // Just move the cursor. | 44 | None => { |
45 | builder.insert_snippet( | 45 | builder.insert_snippet(cap, node_start, "#[derive($0)]\n"); |
46 | cap, | 46 | } |
47 | tt.syntax().text_range().end() - TextSize::of(')'), | 47 | Some(tt) => { |
48 | "$0", | 48 | // Just move the cursor. |
49 | ) | 49 | builder.insert_snippet( |
50 | } | 50 | cap, |
51 | }; | 51 | tt.syntax().text_range().end() - TextSize::of(')'), |
52 | }) | 52 | "$0", |
53 | ) | ||
54 | } | ||
55 | }; | ||
56 | }, | ||
57 | ) | ||
53 | } | 58 | } |
54 | 59 | ||
55 | // Insert `derive` after doc comments. | 60 | // Insert `derive` after doc comments. |