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