rust/crates/ra_assists/src/handlers/generate_derive.rs

128 lines
3.1 KiB
Rust
Raw Normal View History

2019-01-03 06:08:32 -06:00
use ra_syntax::{
ast::{self, AstNode, AttrsOwner},
SyntaxKind::{COMMENT, WHITESPACE},
2020-04-24 16:40:41 -05:00
TextSize,
2019-01-03 06:08:32 -06:00
};
2020-06-28 17:36:05 -05:00
use crate::{AssistContext, AssistId, AssistKind, Assists};
2019-01-03 06:08:32 -06:00
2020-07-03 11:15:03 -05:00
// Assist: generate_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,<|>
// }
// ```
// ->
// ```
2020-05-17 07:21:24 -05:00
// #[derive($0)]
2019-10-25 15:38:15 -05:00
// struct Point {
// x: u32,
// y: u32,
// }
// ```
2020-07-03 11:15:03 -05:00
pub(crate) fn generate_derive(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
2020-05-17 07:21:24 -05:00
let cap = ctx.config.snippet_cap?;
let nominal = ctx.find_node_at_offset::<ast::NominalDef>()?;
2019-07-19 03:24:41 -05:00
let node_start = derive_insertion_offset(&nominal)?;
let target = nominal.syntax().text_range();
2020-07-03 11:15:03 -05:00
acc.add(AssistId("generate_derive", AssistKind::None), "Add `#[derive]`", target, |builder| {
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();
2020-05-17 07:21:24 -05:00
match derive_attr {
2019-01-03 06:08:32 -06:00
None => {
2020-05-17 07:21:24 -05:00
builder.insert_snippet(cap, node_start, "#[derive($0)]\n");
}
Some(tt) => {
// Just move the cursor.
builder.insert_snippet(
cap,
tt.syntax().text_range().end() - TextSize::of(')'),
"$0",
)
2019-01-03 06:08:32 -06:00
}
};
})
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.
2020-04-24 16:40:41 -05:00
fn derive_insertion_offset(nominal: &ast::NominalDef) -> Option<TextSize> {
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 {
2020-05-06 03:16:55 -05:00
use crate::tests::{check_assist, check_assist_target};
2019-01-03 06:08:32 -06:00
use super::*;
2019-01-03 06:08:32 -06:00
#[test]
fn add_derive_new() {
2019-01-03 09:59:17 -06:00
check_assist(
2020-07-03 11:15:03 -05:00
generate_derive,
2019-01-03 06:08:32 -06:00
"struct Foo { a: i32, <|>}",
2020-05-17 07:21:24 -05:00
"#[derive($0)]\nstruct Foo { a: i32, }",
2019-01-03 06:08:32 -06:00
);
2019-01-03 09:59:17 -06:00
check_assist(
2020-07-03 11:15:03 -05:00
generate_derive,
2019-01-03 06:08:32 -06:00
"struct Foo { <|> a: i32, }",
2020-05-17 07:21:24 -05:00
"#[derive($0)]\nstruct Foo { a: i32, }",
2019-01-03 06:08:32 -06:00
);
}
#[test]
fn add_derive_existing() {
2019-01-03 09:59:17 -06:00
check_assist(
2020-07-03 11:15:03 -05:00
generate_derive,
2019-01-03 06:08:32 -06:00
"#[derive(Clone)]\nstruct Foo { a: i32<|>, }",
2020-05-17 07:21:24 -05:00
"#[derive(Clone$0)]\nstruct Foo { a: i32, }",
2019-01-03 06:08:32 -06:00
);
}
#[test]
fn add_derive_new_with_doc_comment() {
2019-01-03 09:59:17 -06:00
check_assist(
2020-07-03 11:15:03 -05:00
generate_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.
2020-05-17 07:21:24 -05:00
#[derive($0)]
2019-01-03 06:08:32 -06:00
struct Foo { a: i32, }
",
);
}
2019-02-08 17:34:05 -06:00
#[test]
fn add_derive_target() {
check_assist_target(
2020-07-03 11:15:03 -05:00
generate_derive,
2019-02-08 17:34:05 -06:00
"
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
}