rust/crates/ra_assists/src/assists/add_derive.rs

124 lines
3.0 KiB
Rust
Raw Normal View History

2019-02-03 12:26:35 -06:00
use hir::db::HirDatabase;
2019-01-03 06:08:32 -06:00
use ra_syntax::{
ast::{self, AstNode, AttrsOwner},
SyntaxKind::{COMMENT, WHITESPACE},
2019-01-03 06:08:32 -06:00
TextUnit,
};
use crate::{Assist, AssistCtx, AssistId};
2019-01-03 06:08:32 -06:00
2019-10-25 15:38:15 -05:00
// Assist: add_derive
2019-10-26 09:27:47 -05:00
//
2019-10-25 15:38:15 -05:00
// Adds a new `#[derive()]` clause to a struct or enum.
2019-10-26 09:27:47 -05:00
//
2019-10-25 15:38:15 -05:00
// ```
// struct Point {
// x: u32,
// y: u32,<|>
// }
// ```
// ->
// ```
// #[derive()]
// struct Point {
// x: u32,
// y: u32,
// }
// ```
pub(crate) fn add_derive(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> {
2019-01-03 09:59:17 -06:00
let nominal = ctx.node_at_offset::<ast::NominalDef>()?;
2019-07-19 03:24:41 -05:00
let node_start = derive_insertion_offset(&nominal)?;
2019-02-24 04:53:35 -06:00
ctx.add_action(AssistId("add_derive"), "add `#[derive]`", |edit| {
2019-01-03 06:08:32 -06:00
let derive_attr = nominal
.attrs()
2019-09-29 16:15:03 -05:00
.filter_map(|x| x.as_simple_call())
2019-01-03 06:08:32 -06:00
.filter(|(name, _arg)| name == "derive")
.map(|(_name, arg)| arg)
.next();
let offset = match derive_attr {
None => {
2019-01-03 09:59:17 -06:00
edit.insert(node_start, "#[derive()]\n");
2019-01-03 06:08:32 -06:00
node_start + TextUnit::of_str("#[derive(")
}
2019-07-20 04:58:27 -05:00
Some(tt) => tt.syntax().text_range().end() - TextUnit::of_char(')'),
2019-01-03 06:08:32 -06:00
};
2019-07-20 04:58:27 -05:00
edit.target(nominal.syntax().text_range());
2019-01-03 09:59:17 -06:00
edit.set_cursor(offset)
});
ctx.build()
2019-01-03 09:59:17 -06:00
}
2019-01-03 06:08:32 -06:00
2019-01-03 09:59:17 -06:00
// Insert `derive` after doc comments.
2019-01-07 07:53:24 -06:00
fn derive_insertion_offset(nominal: &ast::NominalDef) -> Option<TextUnit> {
2019-03-30 05:25:53 -05:00
let non_ws_child = nominal
.syntax()
.children_with_tokens()
.find(|it| it.kind() != COMMENT && it.kind() != WHITESPACE)?;
2019-07-20 04:58:27 -05:00
Some(non_ws_child.text_range().start())
2019-01-03 06:08:32 -06:00
}
#[cfg(test)]
mod tests {
use super::*;
2019-02-08 17:34:05 -06:00
use crate::helpers::{check_assist, check_assist_target};
2019-01-03 06:08:32 -06:00
#[test]
fn add_derive_new() {
2019-01-03 09:59:17 -06:00
check_assist(
add_derive,
2019-01-03 06:08:32 -06:00
"struct Foo { a: i32, <|>}",
"#[derive(<|>)]\nstruct Foo { a: i32, }",
);
2019-01-03 09:59:17 -06:00
check_assist(
add_derive,
2019-01-03 06:08:32 -06:00
"struct Foo { <|> a: i32, }",
"#[derive(<|>)]\nstruct Foo { a: i32, }",
);
}
#[test]
fn add_derive_existing() {
2019-01-03 09:59:17 -06:00
check_assist(
add_derive,
2019-01-03 06:08:32 -06:00
"#[derive(Clone)]\nstruct Foo { a: i32<|>, }",
"#[derive(Clone<|>)]\nstruct Foo { a: i32, }",
);
}
#[test]
fn add_derive_new_with_doc_comment() {
2019-01-03 09:59:17 -06:00
check_assist(
add_derive,
2019-01-03 06:08:32 -06:00
"
/// `Foo` is a pretty important struct.
/// It does stuff.
struct Foo { a: i32<|>, }
",
"
/// `Foo` is a pretty important struct.
/// It does stuff.
#[derive(<|>)]
struct Foo { a: i32, }
",
);
}
2019-02-08 17:34:05 -06:00
#[test]
fn add_derive_target() {
check_assist_target(
add_derive,
"
struct SomeThingIrrelevant;
/// `Foo` is a pretty important struct.
/// It does stuff.
struct Foo { a: i32<|>, }
struct EvenMoreIrrelevant;
",
"/// `Foo` is a pretty important struct.
/// It does stuff.
struct Foo { a: i32, }",
);
}
2019-01-03 06:08:32 -06:00
}