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

121 lines
2.9 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
};
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,
// }
// ```
2020-02-06 09:58:57 -06:00
pub(crate) fn add_derive(ctx: AssistCtx) -> Option<Assist> {
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();
ctx.add_assist(AssistId("add_derive"), "Add `#[derive]`", target, |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");
2020-04-24 16:40:41 -05:00
node_start + TextSize::of("#[derive(")
2019-01-03 06:08:32 -06:00
}
2020-04-24 16:40:41 -05:00
Some(tt) => tt.syntax().text_range().end() - TextSize::of(')'),
2019-01-03 06:08:32 -06:00
};
2019-01-03 09:59:17 -06:00
edit.set_cursor(offset)
})
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 {
use super::*;
2020-05-06 03:16:55 -05:00
use crate::tests::{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
}