1
- use syntax:: ast:: { self , AstNode , HasName } ;
2
-
3
- use crate :: {
4
- utils:: { generate_impl_text, generate_trait_impl_text_intransitive} ,
5
- AssistContext , AssistId , AssistKind , Assists ,
1
+ use syntax:: {
2
+ ast:: { self , make, AstNode , HasName } ,
3
+ ted,
6
4
} ;
7
5
6
+ use crate :: { utils, AssistContext , AssistId , AssistKind , Assists } ;
7
+
8
8
// Assist: generate_impl
9
9
//
10
10
// Adds a new inherent impl for a type.
@@ -20,9 +20,7 @@ use crate::{
20
20
// data: T,
21
21
// }
22
22
//
23
- // impl<T: Clone> Ctx<T> {
24
- // $0
25
- // }
23
+ // impl<T: Clone> Ctx<T> {$0}
26
24
// ```
27
25
pub ( crate ) fn generate_impl ( acc : & mut Assists , ctx : & AssistContext < ' _ > ) -> Option < ( ) > {
28
26
let nominal = ctx. find_node_at_offset :: < ast:: Adt > ( ) ?;
@@ -38,17 +36,22 @@ pub(crate) fn generate_impl(acc: &mut Assists, ctx: &AssistContext<'_>) -> Optio
38
36
format ! ( "Generate impl for `{name}`" ) ,
39
37
target,
40
38
|edit| {
41
- let start_offset = nominal. syntax ( ) . text_range ( ) . end ( ) ;
42
- match ctx. config . snippet_cap {
43
- Some ( cap) => {
44
- let snippet = generate_impl_text ( & nominal, " $0" ) ;
45
- edit. insert_snippet ( cap, start_offset, snippet) ;
46
- }
47
- None => {
48
- let snippet = generate_impl_text ( & nominal, "" ) ;
49
- edit. insert ( start_offset, snippet) ;
39
+ // Generate the impl
40
+ let impl_ = utils:: generate_impl ( & nominal) ;
41
+
42
+ // Add a tabstop after the left curly brace
43
+ if let Some ( cap) = ctx. config . snippet_cap {
44
+ if let Some ( l_curly) = impl_. assoc_item_list ( ) . and_then ( |it| it. l_curly_token ( ) ) {
45
+ edit. add_tabstop_after_token ( cap, l_curly) ;
50
46
}
51
47
}
48
+
49
+ // Add the impl after the adt
50
+ let nominal = edit. make_mut ( nominal) ;
51
+ ted:: insert_all_raw (
52
+ ted:: Position :: after ( nominal. syntax ( ) ) ,
53
+ vec ! [ make:: tokens:: blank_line( ) . into( ) , impl_. syntax( ) . clone( ) . into( ) ] ,
54
+ ) ;
52
55
} ,
53
56
)
54
57
}
@@ -68,9 +71,7 @@ pub(crate) fn generate_impl(acc: &mut Assists, ctx: &AssistContext<'_>) -> Optio
68
71
// data: T,
69
72
// }
70
73
//
71
- // impl<T: Clone> $0 for Ctx<T> {
72
- //
73
- // }
74
+ // impl<T: Clone> ${0:_} for Ctx<T> {}
74
75
// ```
75
76
pub ( crate ) fn generate_trait_impl ( acc : & mut Assists , ctx : & AssistContext < ' _ > ) -> Option < ( ) > {
76
77
let nominal = ctx. find_node_at_offset :: < ast:: Adt > ( ) ?;
@@ -86,17 +87,22 @@ pub(crate) fn generate_trait_impl(acc: &mut Assists, ctx: &AssistContext<'_>) ->
86
87
format ! ( "Generate trait impl for `{name}`" ) ,
87
88
target,
88
89
|edit| {
89
- let start_offset = nominal. syntax ( ) . text_range ( ) . end ( ) ;
90
- match ctx. config . snippet_cap {
91
- Some ( cap) => {
92
- let snippet = generate_trait_impl_text_intransitive ( & nominal, "$0" , "" ) ;
93
- edit. insert_snippet ( cap, start_offset, snippet) ;
94
- }
95
- None => {
96
- let text = generate_trait_impl_text_intransitive ( & nominal, "" , "" ) ;
97
- edit. insert ( start_offset, text) ;
90
+ // Generate the impl
91
+ let impl_ = utils:: generate_trait_impl_intransitive ( & nominal, make:: ty_placeholder ( ) ) ;
92
+
93
+ // Make the trait type a placeholder snippet
94
+ if let Some ( cap) = ctx. config . snippet_cap {
95
+ if let Some ( trait_) = impl_. trait_ ( ) {
96
+ edit. add_placeholder_snippet ( cap, trait_) ;
98
97
}
99
98
}
99
+
100
+ // Add the impl after the adt
101
+ let nominal = edit. make_mut ( nominal) ;
102
+ ted:: insert_all_raw (
103
+ ted:: Position :: after ( nominal. syntax ( ) ) ,
104
+ vec ! [ make:: tokens:: blank_line( ) . into( ) , impl_. syntax( ) . clone( ) . into( ) ] ,
105
+ ) ;
100
106
} ,
101
107
)
102
108
}
@@ -117,9 +123,7 @@ mod tests {
117
123
r#"
118
124
struct Foo {}
119
125
120
- impl Foo {
121
- $0
122
- }
126
+ impl Foo {$0}
123
127
"# ,
124
128
) ;
125
129
}
@@ -134,9 +138,7 @@ mod tests {
134
138
r#"
135
139
struct Foo<T: Clone> {}
136
140
137
- impl<T: Clone> Foo<T> {
138
- $0
139
- }
141
+ impl<T: Clone> Foo<T> {$0}
140
142
"# ,
141
143
) ;
142
144
}
@@ -151,9 +153,7 @@ mod tests {
151
153
r#"
152
154
struct Foo<'a, T: Foo<'a>> {}
153
155
154
- impl<'a, T: Foo<'a>> Foo<'a, T> {
155
- $0
156
- }
156
+ impl<'a, T: Foo<'a>> Foo<'a, T> {$0}
157
157
"# ,
158
158
) ;
159
159
}
@@ -171,9 +171,7 @@ mod tests {
171
171
struct Foo<'a, T: Foo<'a>> {}
172
172
173
173
#[cfg(feature = "foo")]
174
- impl<'a, T: Foo<'a>> Foo<'a, T> {
175
- $0
176
- }
174
+ impl<'a, T: Foo<'a>> Foo<'a, T> {$0}
177
175
"# ,
178
176
) ;
179
177
}
@@ -188,9 +186,7 @@ mod tests {
188
186
r#"
189
187
struct Defaulted<T = i32> {}
190
188
191
- impl<T> Defaulted<T> {
192
- $0
193
- }
189
+ impl<T> Defaulted<T> {$0}
194
190
"# ,
195
191
) ;
196
192
}
@@ -205,9 +201,7 @@ mod tests {
205
201
r#"
206
202
struct Defaulted<'a, 'b: 'a, T: Debug + Clone + 'a + 'b = String, const S: usize> {}
207
203
208
- impl<'a, 'b: 'a, T: Debug + Clone + 'a + 'b, const S: usize> Defaulted<'a, 'b, T, S> {
209
- $0
210
- }
204
+ impl<'a, 'b: 'a, T: Debug + Clone + 'a + 'b, const S: usize> Defaulted<'a, 'b, T, S> {$0}
211
205
"# ,
212
206
) ;
213
207
}
@@ -222,9 +216,7 @@ mod tests {
222
216
r#"
223
217
struct Defaulted<const N: i32 = 0> {}
224
218
225
- impl<const N: i32> Defaulted<N> {
226
- $0
227
- }
219
+ impl<const N: i32> Defaulted<N> {$0}
228
220
"# ,
229
221
) ;
230
222
}
@@ -254,8 +246,7 @@ mod tests {
254
246
impl<T> Struct<T>
255
247
where
256
248
T: Trait,
257
- {
258
- $0
249
+ {$0
259
250
}
260
251
"# ,
261
252
) ;
@@ -285,9 +276,7 @@ mod tests {
285
276
r#"
286
277
struct Foo {}
287
278
288
- impl $0 for Foo {
289
-
290
- }
279
+ impl ${0:_} for Foo {}
291
280
"# ,
292
281
) ;
293
282
}
@@ -302,9 +291,7 @@ mod tests {
302
291
r#"
303
292
struct Foo<T: Clone> {}
304
293
305
- impl<T: Clone> $0 for Foo<T> {
306
-
307
- }
294
+ impl<T: Clone> ${0:_} for Foo<T> {}
308
295
"# ,
309
296
) ;
310
297
}
@@ -319,9 +306,7 @@ mod tests {
319
306
r#"
320
307
struct Foo<'a, T: Foo<'a>> {}
321
308
322
- impl<'a, T: Foo<'a>> $0 for Foo<'a, T> {
323
-
324
- }
309
+ impl<'a, T: Foo<'a>> ${0:_} for Foo<'a, T> {}
325
310
"# ,
326
311
) ;
327
312
}
@@ -339,9 +324,7 @@ mod tests {
339
324
struct Foo<'a, T: Foo<'a>> {}
340
325
341
326
#[cfg(feature = "foo")]
342
- impl<'a, T: Foo<'a>> $0 for Foo<'a, T> {
343
-
344
- }
327
+ impl<'a, T: Foo<'a>> ${0:_} for Foo<'a, T> {}
345
328
"# ,
346
329
) ;
347
330
}
@@ -356,9 +339,7 @@ mod tests {
356
339
r#"
357
340
struct Defaulted<T = i32> {}
358
341
359
- impl<T> $0 for Defaulted<T> {
360
-
361
- }
342
+ impl<T> ${0:_} for Defaulted<T> {}
362
343
"# ,
363
344
) ;
364
345
}
@@ -373,9 +354,7 @@ mod tests {
373
354
r#"
374
355
struct Defaulted<'a, 'b: 'a, T: Debug + Clone + 'a + 'b = String, const S: usize> {}
375
356
376
- impl<'a, 'b: 'a, T: Debug + Clone + 'a + 'b, const S: usize> $0 for Defaulted<'a, 'b, T, S> {
377
-
378
- }
357
+ impl<'a, 'b: 'a, T: Debug + Clone + 'a + 'b, const S: usize> ${0:_} for Defaulted<'a, 'b, T, S> {}
379
358
"# ,
380
359
) ;
381
360
}
@@ -390,9 +369,7 @@ mod tests {
390
369
r#"
391
370
struct Defaulted<const N: i32 = 0> {}
392
371
393
- impl<const N: i32> $0 for Defaulted<N> {
394
-
395
- }
372
+ impl<const N: i32> ${0:_} for Defaulted<N> {}
396
373
"# ,
397
374
) ;
398
375
}
@@ -419,11 +396,10 @@ mod tests {
419
396
inner: T,
420
397
}
421
398
422
- impl<T> $0 for Struct<T>
399
+ impl<T> ${0:_} for Struct<T>
423
400
where
424
401
T: Trait,
425
402
{
426
-
427
403
}
428
404
"# ,
429
405
) ;
0 commit comments