@@ -7,8 +7,8 @@ use crate::{utils::generate_impl_text, AssistContext, AssistId, AssistKind, Assi
77// Adds a new inherent impl for a type.
88//
99// ```
10- // struct Ctx<T: Clone> {
11- // data: T,$0
10+ // struct Ctx$0 <T: Clone> {
11+ // data: T,
1212// }
1313// ```
1414// ->
@@ -26,6 +26,10 @@ pub(crate) fn generate_impl(acc: &mut Assists, ctx: &AssistContext<'_>) -> Optio
2626 let name = nominal. name ( ) ?;
2727 let target = nominal. syntax ( ) . text_range ( ) ;
2828
29+ if let Some ( _) = ctx. find_node_at_offset :: < ast:: RecordFieldList > ( ) {
30+ return None ;
31+ }
32+
2933 acc. add (
3034 AssistId ( "generate_impl" , AssistKind :: Generate ) ,
3135 format ! ( "Generate impl for `{name}`" ) ,
@@ -52,139 +56,171 @@ mod tests {
5256
5357 use super :: * ;
5458
55- // FIXME: break up into separate test fns
5659 #[ test]
5760 fn test_add_impl ( ) {
5861 check_assist (
5962 generate_impl,
60- "struct Foo {$0}\n " ,
61- "struct Foo {}\n \n impl Foo {\n $0\n }\n " ,
62- ) ;
63- check_assist (
64- generate_impl,
65- "struct Foo<T: Clone> {$0}" ,
66- "struct Foo<T: Clone> {}\n \n impl<T: Clone> Foo<T> {\n $0\n }" ,
67- ) ;
68- check_assist (
69- generate_impl,
70- "struct Foo<'a, T: Foo<'a>> {$0}" ,
71- "struct Foo<'a, T: Foo<'a>> {}\n \n impl<'a, T: Foo<'a>> Foo<'a, T> {\n $0\n }" ,
63+ r#"
64+ struct Foo$0 {}
65+ "# ,
66+ r#"
67+ struct Foo {}
68+
69+ impl Foo {
70+ $0
71+ }
72+ "# ,
7273 ) ;
74+ }
75+
76+ #[ test]
77+ fn test_add_impl_with_generics ( ) {
7378 check_assist (
7479 generate_impl,
7580 r#"
76- struct MyOwnArray<T, const S: usize> {}$0"# ,
81+ struct Foo$0<T: Clone> {}
82+ "# ,
7783 r#"
78- struct MyOwnArray<T, const S: usize > {}
84+ struct Foo<T: Clone > {}
7985
80- impl<T, const S: usize> MyOwnArray<T, S> {
81- $0
82- }"# ,
86+ impl<T: Clone> Foo<T> {
87+ $0
88+ }
89+ "# ,
8390 ) ;
91+ }
92+
93+ #[ test]
94+ fn test_add_impl_with_generics_and_lifetime_parameters ( ) {
8495 check_assist (
8596 generate_impl,
8697 r#"
87- #[cfg(feature = "foo")]
88- struct Foo<'a, T: Foo<'a>> {$0} "# ,
98+ struct Foo<'a, T: Foo<'a>>$0 {}
99+ "# ,
89100 r#"
90- #[cfg(feature = "foo")]
91- struct Foo<'a, T: Foo<'a>> {}
101+ struct Foo<'a, T: Foo<'a>> {}
92102
93- #[cfg(feature = "foo")]
94- impl<'a, T: Foo<'a>> Foo<'a, T> {
95- $0
96- } "# ,
103+ impl<'a, T: Foo<'a>> Foo<'a, T> {
104+ $0
105+ }
106+ "# ,
97107 ) ;
108+ }
98109
110+ #[ test]
111+ fn test_add_impl_with_attributes ( ) {
99112 check_assist (
100113 generate_impl,
101114 r#"
102- #[cfg(not(feature = "foo"))]
103- struct Foo<'a, T: Foo<'a>> {$0}"# ,
115+ #[cfg(feature = "foo")]
116+ struct Foo<'a, T: Foo$0<'a>> {}
117+ "# ,
104118 r#"
105- #[cfg(not( feature = "foo") )]
106- struct Foo<'a, T: Foo<'a>> {}
119+ #[cfg(feature = "foo")]
120+ struct Foo<'a, T: Foo<'a>> {}
107121
108- #[cfg(not(feature = "foo"))]
109- impl<'a, T: Foo<'a>> Foo<'a, T> {
110- $0
111- }"# ,
122+ #[cfg(feature = "foo")]
123+ impl<'a, T: Foo<'a>> Foo<'a, T> {
124+ $0
125+ }
126+ "# ,
112127 ) ;
128+ }
113129
130+ #[ test]
131+ fn test_add_impl_with_default_generic ( ) {
114132 check_assist (
115133 generate_impl,
116134 r#"
117- struct Defaulted<T = i32> {}$0"# ,
135+ struct Defaulted$0<T = i32> {}
136+ "# ,
118137 r#"
119- struct Defaulted<T = i32> {}
138+ struct Defaulted<T = i32> {}
120139
121- impl<T> Defaulted<T> {
122- $0
123- }"# ,
140+ impl<T> Defaulted<T> {
141+ $0
142+ }
143+ "# ,
124144 ) ;
145+ }
125146
147+ #[ test]
148+ fn test_add_impl_with_constrained_default_generic ( ) {
126149 check_assist (
127150 generate_impl,
128151 r#"
129- struct Defaulted<'a, 'b: 'a, T: Debug + Clone + 'a + 'b = String, const S: usize> {}$0"# ,
152+ struct Defaulted$0<'a, 'b: 'a, T: Debug + Clone + 'a + 'b = String, const S: usize> {}
153+ "# ,
130154 r#"
131- struct Defaulted<'a, 'b: 'a, T: Debug + Clone + 'a + 'b = String, const S: usize> {}
155+ struct Defaulted<'a, 'b: 'a, T: Debug + Clone + 'a + 'b = String, const S: usize> {}
132156
133- impl<'a, 'b: 'a, T: Debug + Clone + 'a + 'b, const S: usize> Defaulted<'a, 'b, T, S> {
134- $0
135- }"# ,
157+ impl<'a, 'b: 'a, T: Debug + Clone + 'a + 'b, const S: usize> Defaulted<'a, 'b, T, S> {
158+ $0
159+ }
160+ "# ,
136161 ) ;
162+ }
137163
164+ #[ test]
165+ fn test_add_impl_with_const_defaulted_generic ( ) {
138166 check_assist (
139167 generate_impl,
140168 r#"
141- struct Defaulted<const N: i32 = 0> {}$0"# ,
169+ struct Defaulted$0<const N: i32 = 0> {}
170+ "# ,
142171 r#"
143- struct Defaulted<const N: i32 = 0> {}
172+ struct Defaulted<const N: i32 = 0> {}
144173
145- impl<const N: i32> Defaulted<N> {
146- $0
147- }"# ,
174+ impl<const N: i32> Defaulted<N> {
175+ $0
176+ }
177+ "# ,
148178 ) ;
179+ }
149180
181+ #[ test]
182+ fn test_add_impl_with_trait_constraint ( ) {
150183 check_assist (
151184 generate_impl,
152- r#"pub trait Trait {}
153- struct Struct<T>$0
154- where
155- T: Trait,
156- {
157- inner: T,
158- }"# ,
159- r#"pub trait Trait {}
160- struct Struct<T>
161- where
162- T: Trait,
163- {
164- inner: T,
165- }
185+ r#"
186+ pub trait Trait {}
187+ struct Struct$0<T>
188+ where
189+ T: Trait,
190+ {
191+ inner: T,
192+ }
193+ "# ,
194+ r#"
195+ pub trait Trait {}
196+ struct Struct<T>
197+ where
198+ T: Trait,
199+ {
200+ inner: T,
201+ }
166202
167- impl<T> Struct<T>
168- where
169- T: Trait,
170- {
171- $0
172- }"# ,
203+ impl<T> Struct<T>
204+ where
205+ T: Trait,
206+ {
207+ $0
208+ }
209+ "# ,
173210 ) ;
174211 }
175212
176213 #[ test]
177- fn add_impl_target ( ) {
214+ fn add_trait_impl_target ( ) {
178215 check_assist_target (
179216 generate_impl,
180- "
181- struct SomeThingIrrelevant;
182- /// Has a lifetime parameter
183- struct Foo<'a, T: Foo<'a>> {$0}
184- struct EvenMoreIrrelevant;
185- " ,
186- "/// Has a lifetime parameter
187- struct Foo<'a, T: Foo<'a>> {}" ,
217+ r#"
218+ struct SomeThingIrrelevant;
219+ /// Has a lifetime parameter
220+ struct Foo$0<'a, T: Foo<'a>> {}
221+ struct EvenMoreIrrelevant;
222+ "# ,
223+ "/// Has a lifetime parameter\n struct Foo<'a, T: Foo<'a>> {}" ,
188224 ) ;
189225 }
190226}
0 commit comments