@@ -7,8 +7,8 @@ use crate::{utils::generate_impl_text, AssistContext, AssistId, AssistKind, Assi
7
7
// Adds a new inherent impl for a type.
8
8
//
9
9
// ```
10
- // struct Ctx<T: Clone> {
11
- // data: T,$0
10
+ // struct Ctx$0 <T: Clone> {
11
+ // data: T,
12
12
// }
13
13
// ```
14
14
// ->
@@ -26,6 +26,10 @@ pub(crate) fn generate_impl(acc: &mut Assists, ctx: &AssistContext<'_>) -> Optio
26
26
let name = nominal. name ( ) ?;
27
27
let target = nominal. syntax ( ) . text_range ( ) ;
28
28
29
+ if let Some ( _) = ctx. find_node_at_offset :: < ast:: RecordFieldList > ( ) {
30
+ return None ;
31
+ }
32
+
29
33
acc. add (
30
34
AssistId ( "generate_impl" , AssistKind :: Generate ) ,
31
35
format ! ( "Generate impl for `{name}`" ) ,
@@ -52,139 +56,171 @@ mod tests {
52
56
53
57
use super :: * ;
54
58
55
- // FIXME: break up into separate test fns
56
59
#[ test]
57
60
fn test_add_impl ( ) {
58
61
check_assist (
59
62
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
+ "# ,
72
73
) ;
74
+ }
75
+
76
+ #[ test]
77
+ fn test_add_impl_with_generics ( ) {
73
78
check_assist (
74
79
generate_impl,
75
80
r#"
76
- struct MyOwnArray<T, const S: usize> {}$0"# ,
81
+ struct Foo$0<T: Clone> {}
82
+ "# ,
77
83
r#"
78
- struct MyOwnArray<T, const S: usize > {}
84
+ struct Foo<T: Clone > {}
79
85
80
- impl<T, const S: usize> MyOwnArray<T, S> {
81
- $0
82
- }"# ,
86
+ impl<T: Clone> Foo<T> {
87
+ $0
88
+ }
89
+ "# ,
83
90
) ;
91
+ }
92
+
93
+ #[ test]
94
+ fn test_add_impl_with_generics_and_lifetime_parameters ( ) {
84
95
check_assist (
85
96
generate_impl,
86
97
r#"
87
- #[cfg(feature = "foo")]
88
- struct Foo<'a, T: Foo<'a>> {$0} "# ,
98
+ struct Foo<'a, T: Foo<'a>>$0 {}
99
+ "# ,
89
100
r#"
90
- #[cfg(feature = "foo")]
91
- struct Foo<'a, T: Foo<'a>> {}
101
+ struct Foo<'a, T: Foo<'a>> {}
92
102
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
+ "# ,
97
107
) ;
108
+ }
98
109
110
+ #[ test]
111
+ fn test_add_impl_with_attributes ( ) {
99
112
check_assist (
100
113
generate_impl,
101
114
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
+ "# ,
104
118
r#"
105
- #[cfg(not( feature = "foo") )]
106
- struct Foo<'a, T: Foo<'a>> {}
119
+ #[cfg(feature = "foo")]
120
+ struct Foo<'a, T: Foo<'a>> {}
107
121
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
+ "# ,
112
127
) ;
128
+ }
113
129
130
+ #[ test]
131
+ fn test_add_impl_with_default_generic ( ) {
114
132
check_assist (
115
133
generate_impl,
116
134
r#"
117
- struct Defaulted<T = i32> {}$0"# ,
135
+ struct Defaulted$0<T = i32> {}
136
+ "# ,
118
137
r#"
119
- struct Defaulted<T = i32> {}
138
+ struct Defaulted<T = i32> {}
120
139
121
- impl<T> Defaulted<T> {
122
- $0
123
- }"# ,
140
+ impl<T> Defaulted<T> {
141
+ $0
142
+ }
143
+ "# ,
124
144
) ;
145
+ }
125
146
147
+ #[ test]
148
+ fn test_add_impl_with_constrained_default_generic ( ) {
126
149
check_assist (
127
150
generate_impl,
128
151
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
+ "# ,
130
154
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> {}
132
156
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
+ "# ,
136
161
) ;
162
+ }
137
163
164
+ #[ test]
165
+ fn test_add_impl_with_const_defaulted_generic ( ) {
138
166
check_assist (
139
167
generate_impl,
140
168
r#"
141
- struct Defaulted<const N: i32 = 0> {}$0"# ,
169
+ struct Defaulted$0<const N: i32 = 0> {}
170
+ "# ,
142
171
r#"
143
- struct Defaulted<const N: i32 = 0> {}
172
+ struct Defaulted<const N: i32 = 0> {}
144
173
145
- impl<const N: i32> Defaulted<N> {
146
- $0
147
- }"# ,
174
+ impl<const N: i32> Defaulted<N> {
175
+ $0
176
+ }
177
+ "# ,
148
178
) ;
179
+ }
149
180
181
+ #[ test]
182
+ fn test_add_impl_with_trait_constraint ( ) {
150
183
check_assist (
151
184
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
+ }
166
202
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
+ "# ,
173
210
) ;
174
211
}
175
212
176
213
#[ test]
177
- fn add_impl_target ( ) {
214
+ fn add_trait_impl_target ( ) {
178
215
check_assist_target (
179
216
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>> {}" ,
188
224
) ;
189
225
}
190
226
}
0 commit comments