aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_assists/src/assists/add_derive.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_assists/src/assists/add_derive.rs')
-rw-r--r--crates/ra_assists/src/assists/add_derive.rs30
1 files changed, 22 insertions, 8 deletions
diff --git a/crates/ra_assists/src/assists/add_derive.rs b/crates/ra_assists/src/assists/add_derive.rs
index 77ecc33c9..764b17bd8 100644
--- a/crates/ra_assists/src/assists/add_derive.rs
+++ b/crates/ra_assists/src/assists/add_derive.rs
@@ -1,5 +1,3 @@
1//! FIXME: write short doc here
2
3use hir::db::HirDatabase; 1use hir::db::HirDatabase;
4use ra_syntax::{ 2use ra_syntax::{
5 ast::{self, AstNode, AttrsOwner}, 3 ast::{self, AstNode, AttrsOwner},
@@ -9,10 +7,28 @@ use ra_syntax::{
9 7
10use crate::{Assist, AssistCtx, AssistId}; 8use crate::{Assist, AssistCtx, AssistId};
11 9
12pub(crate) fn add_derive(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> { 10// Assist: add_derive
13 let nominal = ctx.node_at_offset::<ast::NominalDef>()?; 11//
12// Adds a new `#[derive()]` clause to a struct or enum.
13//
14// ```
15// struct Point {
16// x: u32,
17// y: u32,<|>
18// }
19// ```
20// ->
21// ```
22// #[derive()]
23// struct Point {
24// x: u32,
25// y: u32,
26// }
27// ```
28pub(crate) fn add_derive(ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> {
29 let nominal = ctx.find_node_at_offset::<ast::NominalDef>()?;
14 let node_start = derive_insertion_offset(&nominal)?; 30 let node_start = derive_insertion_offset(&nominal)?;
15 ctx.add_action(AssistId("add_derive"), "add `#[derive]`", |edit| { 31 ctx.add_assist(AssistId("add_derive"), "add `#[derive]`", |edit| {
16 let derive_attr = nominal 32 let derive_attr = nominal
17 .attrs() 33 .attrs()
18 .filter_map(|x| x.as_simple_call()) 34 .filter_map(|x| x.as_simple_call())
@@ -28,9 +44,7 @@ pub(crate) fn add_derive(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist>
28 }; 44 };
29 edit.target(nominal.syntax().text_range()); 45 edit.target(nominal.syntax().text_range());
30 edit.set_cursor(offset) 46 edit.set_cursor(offset)
31 }); 47 })
32
33 ctx.build()
34} 48}
35 49
36// Insert `derive` after doc comments. 50// Insert `derive` after doc comments.