1 use syntax::ast::{self, AstNode, HasName};
3 use crate::{utils::generate_impl_text, AssistContext, AssistId, AssistKind, Assists};
5 // Assist: generate_impl
7 // Adds a new inherent impl for a type.
10 // struct Ctx<T: Clone> {
16 // struct Ctx<T: Clone> {
20 // impl<T: Clone> Ctx<T> {
24 pub(crate) fn generate_impl(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
25 let nominal = ctx.find_node_at_offset::<ast::Adt>()?;
26 let name = nominal.name()?;
27 let target = nominal.syntax().text_range();
30 AssistId("generate_impl", AssistKind::Generate),
31 format!("Generate impl for `{}`", name),
34 let start_offset = nominal.syntax().text_range().end();
35 match ctx.config.snippet_cap {
37 let snippet = generate_impl_text(&nominal, " $0");
38 edit.insert_snippet(cap, start_offset, snippet);
41 let snippet = generate_impl_text(&nominal, "");
42 edit.insert(start_offset, snippet);
51 use crate::tests::{check_assist, check_assist_target};
60 "struct Foo {}\n\nimpl Foo {\n $0\n}\n",
64 "struct Foo<T: Clone> {$0}",
65 "struct Foo<T: Clone> {}\n\nimpl<T: Clone> Foo<T> {\n $0\n}",
69 "struct Foo<'a, T: Foo<'a>> {$0}",
70 "struct Foo<'a, T: Foo<'a>> {}\n\nimpl<'a, T: Foo<'a>> Foo<'a, T> {\n $0\n}",
75 struct MyOwnArray<T, const S: usize> {}$0"#,
77 struct MyOwnArray<T, const S: usize> {}
79 impl<T, const S: usize> MyOwnArray<T, S> {
86 #[cfg(feature = "foo")]
87 struct Foo<'a, T: Foo<'a>> {$0}"#,
89 #[cfg(feature = "foo")]
90 struct Foo<'a, T: Foo<'a>> {}
92 #[cfg(feature = "foo")]
93 impl<'a, T: Foo<'a>> Foo<'a, T> {
101 #[cfg(not(feature = "foo"))]
102 struct Foo<'a, T: Foo<'a>> {$0}"#,
104 #[cfg(not(feature = "foo"))]
105 struct Foo<'a, T: Foo<'a>> {}
107 #[cfg(not(feature = "foo"))]
108 impl<'a, T: Foo<'a>> Foo<'a, T> {
116 struct Defaulted<T = i32> {}$0"#,
118 struct Defaulted<T = i32> {}
120 impl<T> Defaulted<T> {
128 struct Defaulted<'a, 'b: 'a, T: Debug + Clone + 'a + 'b = String, const S: usize> {}$0"#,
130 struct Defaulted<'a, 'b: 'a, T: Debug + Clone + 'a + 'b = String, const S: usize> {}
132 impl<'a, 'b: 'a, T: Debug + Clone + 'a + 'b, const S: usize> Defaulted<'a, 'b, T, S> {
139 r#"pub trait Trait {}
146 r#"pub trait Trait {}
164 fn add_impl_target() {
168 struct SomeThingIrrelevant;
169 /// Has a lifetime parameter
170 struct Foo<'a, T: Foo<'a>> {$0}
171 struct EvenMoreIrrelevant;
173 "/// Has a lifetime parameter
174 struct Foo<'a, T: Foo<'a>> {}",