use crate::{AssistCtx, Assist};
-pub(crate) fn add_derive(ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> {
+pub(crate) fn add_derive(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> {
let nominal = ctx.node_at_offset::<ast::NominalDef>()?;
let node_start = derive_insertion_offset(nominal)?;
- ctx.build("add `#[derive]`", |edit| {
+ ctx.add_action("add `#[derive]`", |edit| {
let derive_attr = nominal
.attrs()
.filter_map(|x| x.as_call())
}
Some(tt) => tt.syntax().range().end() - TextUnit::of_char(')'),
};
+ edit.target(nominal.syntax().range());
edit.set_cursor(offset)
- })
+ });
+
+ ctx.build()
}
// Insert `derive` after doc comments.
fn derive_insertion_offset(nominal: &ast::NominalDef) -> Option<TextUnit> {
- let non_ws_child = nominal
- .syntax()
- .children()
- .find(|it| it.kind() != COMMENT && it.kind() != WHITESPACE)?;
+ let non_ws_child =
+ nominal.syntax().children().find(|it| it.kind() != COMMENT && it.kind() != WHITESPACE)?;
Some(non_ws_child.range().start())
}
#[cfg(test)]
mod tests {
use super::*;
- use crate::helpers::check_assist;
+ use crate::helpers::{check_assist, check_assist_target};
#[test]
fn add_derive_new() {
",
);
}
+
+ #[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, }",
+ );
+ }
}