rust-analyzer/crates/ra_assists/src/handlers/add_derive.rs

128 lines
3.1 KiB
Rust
Raw Normal View History

2019-01-03 12:08:32 +00:00
use ra_syntax::{
ast::{self, AstNode, AttrsOwner},
SyntaxKind::{COMMENT, WHITESPACE},
2020-04-24 21:40:41 +00:00
TextSize,
2019-01-03 12:08:32 +00:00
};
2020-06-28 22:36:05 +00:00
use crate::{AssistContext, AssistId, AssistKind, Assists};
2019-01-03 12:08:32 +00:00
2019-10-25 20:38:15 +00:00
// Assist: add_derive
2019-10-26 14:27:47 +00:00
//
2019-10-25 20:38:15 +00:00
// Adds a new `#[derive()]` clause to a struct or enum.
2019-10-26 14:27:47 +00:00
//
2019-10-25 20:38:15 +00:00
// ```
// struct Point {
// x: u32,
// y: u32,<|>
// }
// ```
// ->
// ```
2020-05-17 12:21:24 +00:00
// #[derive($0)]
2019-10-25 20:38:15 +00:00
// struct Point {
// x: u32,
// y: u32,
// }
// ```
pub(crate) fn add_derive(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
2020-05-17 12:21:24 +00:00
let cap = ctx.config.snippet_cap?;
let nominal = ctx.find_node_at_offset::<ast::NominalDef>()?;
2019-07-19 08:24:41 +00:00
let node_start = derive_insertion_offset(&nominal)?;
let target = nominal.syntax().text_range();
acc.add(AssistId("add_derive", AssistKind::None), "Add `#[derive]`", target, |builder| {
2019-01-03 12:08:32 +00:00
let derive_attr = nominal
.attrs()
2019-09-29 21:15:03 +00:00
.filter_map(|x| x.as_simple_call())
2019-01-03 12:08:32 +00:00
.filter(|(name, _arg)| name == "derive")
.map(|(_name, arg)| arg)
.next();
2020-05-17 12:21:24 +00:00
match derive_attr {
2019-01-03 12:08:32 +00:00
None => {
2020-05-17 12:21:24 +00: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 12:08:32 +00:00
}
};
})
2019-01-03 15:59:17 +00:00
}
2019-01-03 12:08:32 +00:00
2019-01-03 15:59:17 +00:00
// Insert `derive` after doc comments.
2020-04-24 21:40:41 +00:00
fn derive_insertion_offset(nominal: &ast::NominalDef) -> Option<TextSize> {
2019-03-30 10:25:53 +00:00
let non_ws_child = nominal
.syntax()
.children_with_tokens()
.find(|it| it.kind() != COMMENT && it.kind() != WHITESPACE)?;
2019-07-20 09:58:27 +00:00
Some(non_ws_child.text_range().start())
2019-01-03 12:08:32 +00:00
}
#[cfg(test)]
mod tests {
2020-05-06 08:16:55 +00:00
use crate::tests::{check_assist, check_assist_target};
2019-01-03 12:08:32 +00:00
use super::*;
2019-01-03 12:08:32 +00:00
#[test]
fn add_derive_new() {
2019-01-03 15:59:17 +00:00
check_assist(
add_derive,
2019-01-03 12:08:32 +00:00
"struct Foo { a: i32, <|>}",
2020-05-17 12:21:24 +00:00
"#[derive($0)]\nstruct Foo { a: i32, }",
2019-01-03 12:08:32 +00:00
);
2019-01-03 15:59:17 +00:00
check_assist(
add_derive,
2019-01-03 12:08:32 +00:00
"struct Foo { <|> a: i32, }",
2020-05-17 12:21:24 +00:00
"#[derive($0)]\nstruct Foo { a: i32, }",
2019-01-03 12:08:32 +00:00
);
}
#[test]
fn add_derive_existing() {
2019-01-03 15:59:17 +00:00
check_assist(
add_derive,
2019-01-03 12:08:32 +00:00
"#[derive(Clone)]\nstruct Foo { a: i32<|>, }",
2020-05-17 12:21:24 +00:00
"#[derive(Clone$0)]\nstruct Foo { a: i32, }",
2019-01-03 12:08:32 +00:00
);
}
#[test]
fn add_derive_new_with_doc_comment() {
2019-01-03 15:59:17 +00:00
check_assist(
add_derive,
2019-01-03 12:08:32 +00: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 12:21:24 +00:00
#[derive($0)]
2019-01-03 12:08:32 +00:00
struct Foo { a: i32, }
",
);
}
2019-02-08 23:34:05 +00: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 12:08:32 +00:00
}