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

133 lines
3.2 KiB
Rust
Raw Normal View History

2020-08-12 11:26:51 -05:00
use syntax::{
2021-09-27 05:54:24 -05:00
ast::{self, AstNode, HasAttrs},
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,
2021-01-06 14:15:48 -06:00
// y: u32,$0
2019-10-25 15:38:15 -05:00
// }
// ```
// ->
// ```
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?;
2021-02-07 05:15:02 -06:00
let nominal = ctx.find_node_at_offset::<ast::Adt>()?;
2019-07-19 03:24:41 -05:00
let node_start = derive_insertion_offset(&nominal)?;
let target = nominal.syntax().text_range();
2020-07-03 12:14:42 -05:00
acc.add(
AssistId("generate_derive", AssistKind::Generate),
"Add `#[derive]`",
target,
|builder| {
let derive_attr = nominal
.attrs()
.filter_map(|x| x.as_simple_call())
.filter(|(name, _arg)| name == "derive")
.map(|(_name, arg)| arg)
.next();
match derive_attr {
None => {
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 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.
2021-02-07 05:15:02 -06:00
fn derive_insertion_offset(nominal: &ast::Adt) -> 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,
2021-01-06 14:15:48 -06:00
"struct Foo { a: i32, $0}",
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,
2021-01-06 14:15:48 -06:00
"struct Foo { $0 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,
2021-01-06 14:15:48 -06:00
"#[derive(Clone)]\nstruct Foo { a: i32$0, }",
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.
2021-01-06 14:15:48 -06:00
struct Foo { a: i32$0, }
2019-01-03 06:08:32 -06:00
",
"
/// `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.
2021-01-06 14:15:48 -06:00
struct Foo { a: i32$0, }
2019-02-08 17:34:05 -06:00
struct EvenMoreIrrelevant;
",
"/// `Foo` is a pretty important struct.
/// It does stuff.
struct Foo { a: i32, }",
);
}
2019-01-03 06:08:32 -06:00
}