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

178 lines
4.2 KiB
Rust
Raw Normal View History

use syntax::ast::{self, AstNode, NameOwner};
2019-01-03 06:08:32 -06:00
2021-02-13 14:51:48 -06:00
use crate::{utils::generate_impl_text, AssistContext, AssistId, AssistKind, Assists};
2019-01-03 06:08:32 -06:00
2020-07-03 11:15:03 -05:00
// Assist: generate_impl
2019-10-26 09:27:47 -05:00
//
2019-10-26 11:08:13 -05:00
// Adds a new inherent impl for a type.
2019-10-26 09:27:47 -05:00
//
2019-10-25 15:38:15 -05:00
// ```
// struct Ctx<T: Clone> {
2021-01-06 14:15:48 -06:00
// data: T,$0
2019-10-25 15:38:15 -05:00
// }
// ```
// ->
// ```
// struct Ctx<T: Clone> {
2020-05-17 07:21:24 -05:00
// data: T,
2019-10-25 15:38:15 -05:00
// }
//
// impl<T: Clone> Ctx<T> {
2020-05-17 07:21:24 -05:00
// $0
2019-10-25 15:38:15 -05:00
// }
// ```
2020-07-03 11:15:03 -05:00
pub(crate) fn generate_impl(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
2021-02-07 05:15:02 -06:00
let nominal = ctx.find_node_at_offset::<ast::Adt>()?;
2019-01-03 06:08:32 -06:00
let name = nominal.name()?;
let target = nominal.syntax().text_range();
2020-06-28 17:36:05 -05:00
acc.add(
2020-07-03 12:14:42 -05:00
AssistId("generate_impl", AssistKind::Generate),
2020-07-03 11:15:03 -05:00
format!("Generate impl for `{}`", name),
2020-06-28 17:36:05 -05:00
target,
|edit| {
let start_offset = nominal.syntax().text_range().end();
match ctx.config.snippet_cap {
Some(cap) => {
let snippet = generate_impl_text(&nominal, " $0");
edit.insert_snippet(cap, start_offset, snippet);
2020-06-28 17:36:05 -05:00
}
None => {
let snippet = generate_impl_text(&nominal, "");
edit.insert(start_offset, snippet);
2020-06-28 17:36:05 -05:00
}
2020-05-17 07:21:24 -05:00
}
2020-06-28 17:36:05 -05:00
},
)
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
2020-05-17 07:21:24 -05:00
use super::*;
2019-01-03 06:08:32 -06:00
#[test]
fn test_add_impl() {
2019-01-03 09:59:17 -06:00
check_assist(
2020-07-03 11:15:03 -05:00
generate_impl,
2021-01-06 14:15:48 -06:00
"struct Foo {$0}\n",
2020-07-03 11:15:03 -05:00
"struct Foo {}\n\nimpl Foo {\n $0\n}\n",
);
check_assist(
generate_impl,
2021-01-06 14:15:48 -06:00
"struct Foo<T: Clone> {$0}",
2020-05-17 07:21:24 -05:00
"struct Foo<T: Clone> {}\n\nimpl<T: Clone> Foo<T> {\n $0\n}",
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_impl,
2021-01-06 14:15:48 -06:00
"struct Foo<'a, T: Foo<'a>> {$0}",
2020-05-17 07:21:24 -05:00
"struct Foo<'a, T: Foo<'a>> {}\n\nimpl<'a, T: Foo<'a>> Foo<'a, T> {\n $0\n}",
2019-01-03 06:08:32 -06:00
);
check_assist(
generate_impl,
r#"
struct MyOwnArray<T, const S: usize> {}$0"#,
r#"
struct MyOwnArray<T, const S: usize> {}
impl<T, const S: usize> MyOwnArray<T, S> {
$0
}"#,
);
check_assist(
generate_impl,
r#"
#[cfg(feature = "foo")]
2021-01-06 14:15:48 -06:00
struct Foo<'a, T: Foo<'a>> {$0}"#,
r#"
#[cfg(feature = "foo")]
struct Foo<'a, T: Foo<'a>> {}
#[cfg(feature = "foo")]
impl<'a, T: Foo<'a>> Foo<'a, T> {
$0
}"#,
);
check_assist(
generate_impl,
r#"
#[cfg(not(feature = "foo"))]
2021-01-06 14:15:48 -06:00
struct Foo<'a, T: Foo<'a>> {$0}"#,
r#"
#[cfg(not(feature = "foo"))]
struct Foo<'a, T: Foo<'a>> {}
#[cfg(not(feature = "foo"))]
impl<'a, T: Foo<'a>> Foo<'a, T> {
$0
}"#,
);
check_assist(
generate_impl,
r#"
struct Defaulted<T = i32> {}$0"#,
r#"
struct Defaulted<T = i32> {}
impl<T> Defaulted<T> {
$0
}"#,
);
check_assist(
generate_impl,
r#"
struct Defaulted<'a, 'b: 'a, T: Debug + Clone + 'a + 'b = String, const S: usize> {}$0"#,
r#"
struct Defaulted<'a, 'b: 'a, T: Debug + Clone + 'a + 'b = String, const S: usize> {}
impl<'a, 'b: 'a, T: Debug + Clone + 'a + 'b, const S: usize> Defaulted<'a, 'b, T, S> {
$0
}"#,
);
check_assist(
generate_impl,
r#"pub trait Trait {}
struct Struct<T>$0
where
T: Trait,
{
inner: T,
}"#,
r#"pub trait Trait {}
struct Struct<T>
where
T: Trait,
{
inner: T,
}
impl<T> Struct<T>
where
T: Trait,
{
$0
}"#,
);
2019-01-03 06:08:32 -06:00
}
2019-02-08 17:34:05 -06:00
#[test]
fn add_impl_target() {
check_assist_target(
2020-07-03 11:15:03 -05:00
generate_impl,
2019-02-08 17:34:05 -06:00
"
struct SomeThingIrrelevant;
/// Has a lifetime parameter
2021-01-06 14:15:48 -06:00
struct Foo<'a, T: Foo<'a>> {$0}
2019-02-08 17:34:05 -06:00
struct EvenMoreIrrelevant;
",
"/// Has a lifetime parameter
struct Foo<'a, T: Foo<'a>> {}",
);
}
2019-01-03 06:08:32 -06:00
}