1 use syntax::{
2     ast::{self, AstNode, HasAttrs},
3     SyntaxKind::{COMMENT, WHITESPACE},
4     TextSize,
5 };
6 
7 use crate::{AssistContext, AssistId, AssistKind, Assists};
8 
9 // Assist: generate_derive
10 //
11 // Adds a new `#[derive()]` clause to a struct or enum.
12 //
13 // ```
14 // struct Point {
15 //     x: u32,
16 //     y: u32,$0
17 // }
18 // ```
19 // ->
20 // ```
21 // #[derive($0)]
22 // struct Point {
23 //     x: u32,
24 //     y: u32,
25 // }
26 // ```
generate_derive(acc: &mut Assists, ctx: &AssistContext) -> Option<()>27 pub(crate) fn generate_derive(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
28     let cap = ctx.config.snippet_cap?;
29     let nominal = ctx.find_node_at_offset::<ast::Adt>()?;
30     let node_start = derive_insertion_offset(&nominal)?;
31     let target = nominal.syntax().text_range();
32     acc.add(
33         AssistId("generate_derive", AssistKind::Generate),
34         "Add `#[derive]`",
35         target,
36         |builder| {
37             let derive_attr = nominal
38                 .attrs()
39                 .filter_map(|x| x.as_simple_call())
40                 .filter(|(name, _arg)| name == "derive")
41                 .map(|(_name, arg)| arg)
42                 .next();
43             match derive_attr {
44                 None => {
45                     builder.insert_snippet(cap, node_start, "#[derive($0)]\n");
46                 }
47                 Some(tt) => {
48                     // Just move the cursor.
49                     builder.insert_snippet(
50                         cap,
51                         tt.syntax().text_range().end() - TextSize::of(')'),
52                         "$0",
53                     )
54                 }
55             };
56         },
57     )
58 }
59 
60 // Insert `derive` after doc comments.
derive_insertion_offset(nominal: &ast::Adt) -> Option<TextSize>61 fn derive_insertion_offset(nominal: &ast::Adt) -> Option<TextSize> {
62     let non_ws_child = nominal
63         .syntax()
64         .children_with_tokens()
65         .find(|it| it.kind() != COMMENT && it.kind() != WHITESPACE)?;
66     Some(non_ws_child.text_range().start())
67 }
68 
69 #[cfg(test)]
70 mod tests {
71     use crate::tests::{check_assist, check_assist_target};
72 
73     use super::*;
74 
75     #[test]
add_derive_new()76     fn add_derive_new() {
77         check_assist(
78             generate_derive,
79             "struct Foo { a: i32, $0}",
80             "#[derive($0)]\nstruct Foo { a: i32, }",
81         );
82         check_assist(
83             generate_derive,
84             "struct Foo { $0 a: i32, }",
85             "#[derive($0)]\nstruct Foo {  a: i32, }",
86         );
87     }
88 
89     #[test]
add_derive_existing()90     fn add_derive_existing() {
91         check_assist(
92             generate_derive,
93             "#[derive(Clone)]\nstruct Foo { a: i32$0, }",
94             "#[derive(Clone$0)]\nstruct Foo { a: i32, }",
95         );
96     }
97 
98     #[test]
add_derive_new_with_doc_comment()99     fn add_derive_new_with_doc_comment() {
100         check_assist(
101             generate_derive,
102             "
103 /// `Foo` is a pretty important struct.
104 /// It does stuff.
105 struct Foo { a: i32$0, }
106             ",
107             "
108 /// `Foo` is a pretty important struct.
109 /// It does stuff.
110 #[derive($0)]
111 struct Foo { a: i32, }
112             ",
113         );
114     }
115 
116     #[test]
add_derive_target()117     fn add_derive_target() {
118         check_assist_target(
119             generate_derive,
120             "
121 struct SomeThingIrrelevant;
122 /// `Foo` is a pretty important struct.
123 /// It does stuff.
124 struct Foo { a: i32$0, }
125 struct EvenMoreIrrelevant;
126             ",
127             "/// `Foo` is a pretty important struct.
128 /// It does stuff.
129 struct Foo { a: i32, }",
130         );
131     }
132 }
133