2021-09-27 05:54:24 -05:00
|
|
|
use syntax::ast::{self, AstNode, HasName};
|
2019-01-03 06:08:32 -06:00
|
|
|
|
2022-11-21 14:58:01 -06:00
|
|
|
use crate::{
|
2022-12-14 06:08:20 -06:00
|
|
|
utils::{generate_impl_text, generate_trait_impl_text_intransitive},
|
2022-11-21 14:58:01 -06:00
|
|
|
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
|
|
|
// ```
|
2022-11-09 16:01:29 -06:00
|
|
|
// struct Ctx$0<T: Clone> {
|
|
|
|
// data: T,
|
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
|
|
|
// }
|
|
|
|
// ```
|
2022-07-20 08:02:08 -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()?;
|
2020-05-06 05:51:28 -05:00
|
|
|
let target = nominal.syntax().text_range();
|
2020-10-21 14:29:15 -05:00
|
|
|
|
2022-11-09 16:01:29 -06:00
|
|
|
if let Some(_) = ctx.find_node_at_offset::<ast::RecordFieldList>() {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
2020-06-28 17:36:05 -05:00
|
|
|
acc.add(
|
2020-07-03 12:14:42 -05:00
|
|
|
AssistId("generate_impl", AssistKind::Generate),
|
2022-10-10 13:22:01 -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) => {
|
2021-02-13 14:38:52 -06:00
|
|
|
let snippet = generate_impl_text(&nominal, " $0");
|
|
|
|
edit.insert_snippet(cap, start_offset, snippet);
|
2020-06-28 17:36:05 -05:00
|
|
|
}
|
|
|
|
None => {
|
2021-02-13 14:38:52 -06:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2022-11-21 14:58:01 -06:00
|
|
|
// Assist: generate_trait_impl
|
|
|
|
//
|
|
|
|
// Adds a new trait impl for a type.
|
|
|
|
//
|
|
|
|
// ```
|
|
|
|
// struct $0Ctx<T: Clone> {
|
|
|
|
// data: T,
|
|
|
|
// }
|
|
|
|
// ```
|
|
|
|
// ->
|
|
|
|
// ```
|
|
|
|
// struct Ctx<T: Clone> {
|
|
|
|
// data: T,
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// impl<T: Clone> $0 for Ctx<T> {
|
|
|
|
//
|
|
|
|
// }
|
|
|
|
// ```
|
|
|
|
pub(crate) fn generate_trait_impl(acc: &mut Assists, ctx: &AssistContext<'_>) -> Option<()> {
|
|
|
|
let nominal = ctx.find_node_at_offset::<ast::Adt>()?;
|
|
|
|
let name = nominal.name()?;
|
|
|
|
let target = nominal.syntax().text_range();
|
|
|
|
|
|
|
|
if let Some(_) = ctx.find_node_at_offset::<ast::RecordFieldList>() {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
|
|
|
acc.add(
|
|
|
|
AssistId("generate_trait_impl", AssistKind::Generate),
|
|
|
|
format!("Generate trait impl for `{name}`"),
|
|
|
|
target,
|
|
|
|
|edit| {
|
|
|
|
let start_offset = nominal.syntax().text_range().end();
|
|
|
|
match ctx.config.snippet_cap {
|
|
|
|
Some(cap) => {
|
2022-12-14 06:08:20 -06:00
|
|
|
let snippet = generate_trait_impl_text_intransitive(&nominal, "$0", "");
|
2022-11-21 14:58:01 -06:00
|
|
|
edit.insert_snippet(cap, start_offset, snippet);
|
|
|
|
}
|
|
|
|
None => {
|
2022-12-14 06:08:20 -06:00
|
|
|
let text = generate_trait_impl_text_intransitive(&nominal, "", "");
|
2022-11-21 14:58:01 -06:00
|
|
|
edit.insert(start_offset, text);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
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,
|
2022-11-09 16:01:29 -06:00
|
|
|
r#"
|
|
|
|
struct Foo$0 {}
|
|
|
|
"#,
|
|
|
|
r#"
|
|
|
|
struct Foo {}
|
|
|
|
|
|
|
|
impl Foo {
|
|
|
|
$0
|
|
|
|
}
|
|
|
|
"#,
|
2019-01-03 06:08:32 -06:00
|
|
|
);
|
2022-11-09 16:01:29 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_add_impl_with_generics() {
|
2021-03-27 04:37:39 -05:00
|
|
|
check_assist(
|
|
|
|
generate_impl,
|
|
|
|
r#"
|
2022-11-09 16:01:29 -06:00
|
|
|
struct Foo$0<T: Clone> {}
|
|
|
|
"#,
|
2021-03-27 04:37:39 -05:00
|
|
|
r#"
|
2022-11-09 16:01:29 -06:00
|
|
|
struct Foo<T: Clone> {}
|
2021-03-27 04:37:39 -05:00
|
|
|
|
2022-11-09 16:01:29 -06:00
|
|
|
impl<T: Clone> Foo<T> {
|
|
|
|
$0
|
|
|
|
}
|
|
|
|
"#,
|
2021-03-27 04:37:39 -05:00
|
|
|
);
|
2022-11-09 16:01:29 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_add_impl_with_generics_and_lifetime_parameters() {
|
2020-10-21 14:29:15 -05:00
|
|
|
check_assist(
|
|
|
|
generate_impl,
|
|
|
|
r#"
|
2022-11-09 16:01:29 -06:00
|
|
|
struct Foo<'a, T: Foo<'a>>$0 {}
|
|
|
|
"#,
|
2020-10-21 14:29:15 -05:00
|
|
|
r#"
|
2022-11-09 16:01:29 -06:00
|
|
|
struct Foo<'a, T: Foo<'a>> {}
|
2020-10-21 14:29:15 -05:00
|
|
|
|
2022-11-09 16:01:29 -06:00
|
|
|
impl<'a, T: Foo<'a>> Foo<'a, T> {
|
|
|
|
$0
|
|
|
|
}
|
|
|
|
"#,
|
2020-10-21 14:29:15 -05:00
|
|
|
);
|
2022-11-09 16:01:29 -06:00
|
|
|
}
|
2020-10-21 14:29:15 -05:00
|
|
|
|
2022-11-09 16:01:29 -06:00
|
|
|
#[test]
|
|
|
|
fn test_add_impl_with_attributes() {
|
2020-10-21 14:29:15 -05:00
|
|
|
check_assist(
|
|
|
|
generate_impl,
|
|
|
|
r#"
|
2022-11-09 16:01:29 -06:00
|
|
|
#[cfg(feature = "foo")]
|
|
|
|
struct Foo<'a, T: Foo$0<'a>> {}
|
|
|
|
"#,
|
2020-10-21 14:29:15 -05:00
|
|
|
r#"
|
2022-11-09 16:01:29 -06:00
|
|
|
#[cfg(feature = "foo")]
|
|
|
|
struct Foo<'a, T: Foo<'a>> {}
|
2020-10-21 14:29:15 -05:00
|
|
|
|
2022-11-09 16:01:29 -06:00
|
|
|
#[cfg(feature = "foo")]
|
|
|
|
impl<'a, T: Foo<'a>> Foo<'a, T> {
|
|
|
|
$0
|
|
|
|
}
|
|
|
|
"#,
|
2020-10-21 14:29:15 -05:00
|
|
|
);
|
2022-11-09 16:01:29 -06:00
|
|
|
}
|
2021-02-13 15:59:51 -06:00
|
|
|
|
2022-11-09 16:01:29 -06:00
|
|
|
#[test]
|
|
|
|
fn test_add_impl_with_default_generic() {
|
2021-02-13 15:59:51 -06:00
|
|
|
check_assist(
|
|
|
|
generate_impl,
|
|
|
|
r#"
|
2022-11-09 16:01:29 -06:00
|
|
|
struct Defaulted$0<T = i32> {}
|
|
|
|
"#,
|
2021-02-13 15:59:51 -06:00
|
|
|
r#"
|
2022-11-09 16:01:29 -06:00
|
|
|
struct Defaulted<T = i32> {}
|
2021-02-13 15:59:51 -06:00
|
|
|
|
2022-11-09 16:01:29 -06:00
|
|
|
impl<T> Defaulted<T> {
|
|
|
|
$0
|
|
|
|
}
|
|
|
|
"#,
|
2021-02-13 15:59:51 -06:00
|
|
|
);
|
2022-11-09 16:01:29 -06:00
|
|
|
}
|
2021-02-13 15:59:51 -06:00
|
|
|
|
2022-11-09 16:01:29 -06:00
|
|
|
#[test]
|
|
|
|
fn test_add_impl_with_constrained_default_generic() {
|
2021-02-13 15:59:51 -06:00
|
|
|
check_assist(
|
|
|
|
generate_impl,
|
|
|
|
r#"
|
2022-11-09 16:01:29 -06:00
|
|
|
struct Defaulted$0<'a, 'b: 'a, T: Debug + Clone + 'a + 'b = String, const S: usize> {}
|
|
|
|
"#,
|
2021-02-13 15:59:51 -06:00
|
|
|
r#"
|
2022-11-09 16:01:29 -06:00
|
|
|
struct Defaulted<'a, 'b: 'a, T: Debug + Clone + 'a + 'b = String, const S: usize> {}
|
2021-02-13 15:59:51 -06:00
|
|
|
|
2022-11-09 16:01:29 -06:00
|
|
|
impl<'a, 'b: 'a, T: Debug + Clone + 'a + 'b, const S: usize> Defaulted<'a, 'b, T, S> {
|
|
|
|
$0
|
|
|
|
}
|
|
|
|
"#,
|
2021-02-13 15:59:51 -06:00
|
|
|
);
|
2022-11-09 16:01:29 -06:00
|
|
|
}
|
2021-02-20 08:05:01 -06:00
|
|
|
|
2022-11-09 16:01:29 -06:00
|
|
|
#[test]
|
|
|
|
fn test_add_impl_with_const_defaulted_generic() {
|
2022-10-09 17:12:08 -05:00
|
|
|
check_assist(
|
|
|
|
generate_impl,
|
|
|
|
r#"
|
2022-11-09 16:01:29 -06:00
|
|
|
struct Defaulted$0<const N: i32 = 0> {}
|
|
|
|
"#,
|
2022-10-09 17:12:08 -05:00
|
|
|
r#"
|
2022-11-09 16:01:29 -06:00
|
|
|
struct Defaulted<const N: i32 = 0> {}
|
2022-10-09 17:12:08 -05:00
|
|
|
|
2022-11-09 16:01:29 -06:00
|
|
|
impl<const N: i32> Defaulted<N> {
|
|
|
|
$0
|
|
|
|
}
|
|
|
|
"#,
|
2022-10-09 17:12:08 -05:00
|
|
|
);
|
2022-11-09 16:01:29 -06:00
|
|
|
}
|
2022-10-09 17:12:08 -05:00
|
|
|
|
2022-11-09 16:01:29 -06:00
|
|
|
#[test]
|
|
|
|
fn test_add_impl_with_trait_constraint() {
|
2021-02-20 08:05:01 -06:00
|
|
|
check_assist(
|
|
|
|
generate_impl,
|
2022-11-09 16:01:29 -06:00
|
|
|
r#"
|
|
|
|
pub trait Trait {}
|
|
|
|
struct Struct$0<T>
|
|
|
|
where
|
|
|
|
T: Trait,
|
|
|
|
{
|
|
|
|
inner: T,
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
r#"
|
|
|
|
pub trait Trait {}
|
|
|
|
struct Struct<T>
|
|
|
|
where
|
|
|
|
T: Trait,
|
|
|
|
{
|
|
|
|
inner: T,
|
|
|
|
}
|
2021-02-20 08:05:01 -06:00
|
|
|
|
2022-11-09 16:01:29 -06:00
|
|
|
impl<T> Struct<T>
|
|
|
|
where
|
|
|
|
T: Trait,
|
|
|
|
{
|
|
|
|
$0
|
|
|
|
}
|
|
|
|
"#,
|
2021-02-20 08:05:01 -06:00
|
|
|
);
|
2019-01-03 06:08:32 -06:00
|
|
|
}
|
|
|
|
|
2019-02-08 17:34:05 -06:00
|
|
|
#[test]
|
2022-11-21 14:58:01 -06:00
|
|
|
fn add_impl_target() {
|
2019-02-08 17:34:05 -06:00
|
|
|
check_assist_target(
|
2020-07-03 11:15:03 -05:00
|
|
|
generate_impl,
|
2022-11-09 16:01:29 -06:00
|
|
|
r#"
|
|
|
|
struct SomeThingIrrelevant;
|
|
|
|
/// Has a lifetime parameter
|
|
|
|
struct Foo$0<'a, T: Foo<'a>> {}
|
|
|
|
struct EvenMoreIrrelevant;
|
|
|
|
"#,
|
|
|
|
"/// Has a lifetime parameter\nstruct Foo<'a, T: Foo<'a>> {}",
|
2019-02-08 17:34:05 -06:00
|
|
|
);
|
|
|
|
}
|
2022-11-21 14:58:01 -06:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_add_trait_impl() {
|
|
|
|
check_assist(
|
|
|
|
generate_trait_impl,
|
|
|
|
r#"
|
|
|
|
struct Foo$0 {}
|
|
|
|
"#,
|
|
|
|
r#"
|
|
|
|
struct Foo {}
|
|
|
|
|
|
|
|
impl $0 for Foo {
|
|
|
|
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_add_trait_impl_with_generics() {
|
|
|
|
check_assist(
|
|
|
|
generate_trait_impl,
|
|
|
|
r#"
|
|
|
|
struct Foo$0<T: Clone> {}
|
|
|
|
"#,
|
|
|
|
r#"
|
|
|
|
struct Foo<T: Clone> {}
|
|
|
|
|
|
|
|
impl<T: Clone> $0 for Foo<T> {
|
|
|
|
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_add_trait_impl_with_generics_and_lifetime_parameters() {
|
|
|
|
check_assist(
|
|
|
|
generate_trait_impl,
|
|
|
|
r#"
|
|
|
|
struct Foo<'a, T: Foo<'a>>$0 {}
|
|
|
|
"#,
|
|
|
|
r#"
|
|
|
|
struct Foo<'a, T: Foo<'a>> {}
|
|
|
|
|
|
|
|
impl<'a, T: Foo<'a>> $0 for Foo<'a, T> {
|
|
|
|
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_add_trait_impl_with_attributes() {
|
|
|
|
check_assist(
|
|
|
|
generate_trait_impl,
|
|
|
|
r#"
|
|
|
|
#[cfg(feature = "foo")]
|
|
|
|
struct Foo<'a, T: Foo$0<'a>> {}
|
|
|
|
"#,
|
|
|
|
r#"
|
|
|
|
#[cfg(feature = "foo")]
|
|
|
|
struct Foo<'a, T: Foo<'a>> {}
|
|
|
|
|
|
|
|
#[cfg(feature = "foo")]
|
|
|
|
impl<'a, T: Foo<'a>> $0 for Foo<'a, T> {
|
|
|
|
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_add_trait_impl_with_default_generic() {
|
|
|
|
check_assist(
|
|
|
|
generate_trait_impl,
|
|
|
|
r#"
|
|
|
|
struct Defaulted$0<T = i32> {}
|
|
|
|
"#,
|
|
|
|
r#"
|
|
|
|
struct Defaulted<T = i32> {}
|
|
|
|
|
|
|
|
impl<T> $0 for Defaulted<T> {
|
|
|
|
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_add_trait_impl_with_constrained_default_generic() {
|
|
|
|
check_assist(
|
|
|
|
generate_trait_impl,
|
|
|
|
r#"
|
|
|
|
struct Defaulted$0<'a, 'b: 'a, T: Debug + Clone + 'a + 'b = String, const S: usize> {}
|
|
|
|
"#,
|
|
|
|
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> $0 for Defaulted<'a, 'b, T, S> {
|
|
|
|
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_add_trait_impl_with_const_defaulted_generic() {
|
|
|
|
check_assist(
|
|
|
|
generate_trait_impl,
|
|
|
|
r#"
|
|
|
|
struct Defaulted$0<const N: i32 = 0> {}
|
|
|
|
"#,
|
|
|
|
r#"
|
|
|
|
struct Defaulted<const N: i32 = 0> {}
|
|
|
|
|
|
|
|
impl<const N: i32> $0 for Defaulted<N> {
|
|
|
|
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_add_trait_impl_with_trait_constraint() {
|
|
|
|
check_assist(
|
|
|
|
generate_trait_impl,
|
|
|
|
r#"
|
|
|
|
pub trait Trait {}
|
|
|
|
struct Struct$0<T>
|
|
|
|
where
|
|
|
|
T: Trait,
|
|
|
|
{
|
|
|
|
inner: T,
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
r#"
|
|
|
|
pub trait Trait {}
|
|
|
|
struct Struct<T>
|
|
|
|
where
|
|
|
|
T: Trait,
|
|
|
|
{
|
|
|
|
inner: T,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> $0 for Struct<T>
|
|
|
|
where
|
|
|
|
T: Trait,
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn add_trait_impl_target() {
|
|
|
|
check_assist_target(
|
|
|
|
generate_trait_impl,
|
|
|
|
r#"
|
|
|
|
struct SomeThingIrrelevant;
|
|
|
|
/// Has a lifetime parameter
|
|
|
|
struct Foo$0<'a, T: Foo<'a>> {}
|
|
|
|
struct EvenMoreIrrelevant;
|
|
|
|
"#,
|
|
|
|
"/// Has a lifetime parameter\nstruct Foo<'a, T: Foo<'a>> {}",
|
|
|
|
);
|
|
|
|
}
|
2019-01-03 06:08:32 -06:00
|
|
|
}
|