@@ -47,6 +47,11 @@ pub(crate) fn replace_derive_with_manual_impl(
47
47
return None ;
48
48
}
49
49
50
+ if !args. syntax ( ) . text_range ( ) . contains ( ctx. offset ( ) ) {
51
+ cov_mark:: hit!( outside_of_attr_args) ;
52
+ return None ;
53
+ }
54
+
50
55
let trait_token = args. syntax ( ) . token_at_offset ( ctx. offset ( ) ) . find ( |t| t. kind ( ) == IDENT ) ?;
51
56
let trait_name = trait_token. text ( ) ;
52
57
@@ -207,7 +212,7 @@ mod tests {
207
212
fn add_custom_impl_debug ( ) {
208
213
check_assist (
209
214
replace_derive_with_manual_impl,
210
- "
215
+ r# "
211
216
mod fmt {
212
217
pub struct Error;
213
218
pub type Result = Result<(), Error>;
@@ -221,8 +226,8 @@ mod fmt {
221
226
struct Foo {
222
227
bar: String,
223
228
}
224
- " ,
225
- "
229
+ "# ,
230
+ r# "
226
231
mod fmt {
227
232
pub struct Error;
228
233
pub type Result = Result<(), Error>;
@@ -241,14 +246,14 @@ impl fmt::Debug for Foo {
241
246
${0:todo!()}
242
247
}
243
248
}
244
- " ,
249
+ "# ,
245
250
)
246
251
}
247
252
#[ test]
248
253
fn add_custom_impl_all ( ) {
249
254
check_assist (
250
255
replace_derive_with_manual_impl,
251
- "
256
+ r# "
252
257
mod foo {
253
258
pub trait Bar {
254
259
type Qux;
@@ -263,8 +268,8 @@ mod foo {
263
268
struct Foo {
264
269
bar: String,
265
270
}
266
- " ,
267
- "
271
+ "# ,
272
+ r# "
268
273
mod foo {
269
274
pub trait Bar {
270
275
type Qux;
@@ -290,110 +295,122 @@ impl foo::Bar for Foo {
290
295
todo!()
291
296
}
292
297
}
293
- " ,
298
+ "# ,
294
299
)
295
300
}
296
301
#[ test]
297
302
fn add_custom_impl_for_unique_input ( ) {
298
303
check_assist (
299
304
replace_derive_with_manual_impl,
300
- "
305
+ r# "
301
306
#[derive(Debu$0g)]
302
307
struct Foo {
303
308
bar: String,
304
309
}
305
- " ,
306
- "
310
+ "# ,
311
+ r# "
307
312
struct Foo {
308
313
bar: String,
309
314
}
310
315
311
316
impl Debug for Foo {
312
317
$0
313
318
}
314
- " ,
319
+ "# ,
315
320
)
316
321
}
317
322
318
323
#[ test]
319
324
fn add_custom_impl_for_with_visibility_modifier ( ) {
320
325
check_assist (
321
326
replace_derive_with_manual_impl,
322
- "
327
+ r# "
323
328
#[derive(Debug$0)]
324
329
pub struct Foo {
325
330
bar: String,
326
331
}
327
- " ,
328
- "
332
+ "# ,
333
+ r# "
329
334
pub struct Foo {
330
335
bar: String,
331
336
}
332
337
333
338
impl Debug for Foo {
334
339
$0
335
340
}
336
- " ,
341
+ "# ,
337
342
)
338
343
}
339
344
340
345
#[ test]
341
346
fn add_custom_impl_when_multiple_inputs ( ) {
342
347
check_assist (
343
348
replace_derive_with_manual_impl,
344
- "
349
+ r# "
345
350
#[derive(Display, Debug$0, Serialize)]
346
351
struct Foo {}
347
- " ,
348
- "
352
+ "# ,
353
+ r# "
349
354
#[derive(Display, Serialize)]
350
355
struct Foo {}
351
356
352
357
impl Debug for Foo {
353
358
$0
354
359
}
355
- " ,
360
+ "# ,
356
361
)
357
362
}
358
363
359
364
#[ test]
360
365
fn test_ignore_derive_macro_without_input ( ) {
361
366
check_assist_not_applicable (
362
367
replace_derive_with_manual_impl,
363
- "
368
+ r# "
364
369
#[derive($0)]
365
370
struct Foo {}
366
- " ,
371
+ "# ,
367
372
)
368
373
}
369
374
370
375
#[ test]
371
376
fn test_ignore_if_cursor_on_param ( ) {
372
377
check_assist_not_applicable (
373
378
replace_derive_with_manual_impl,
374
- "
379
+ r# "
375
380
#[derive$0(Debug)]
376
381
struct Foo {}
377
- " ,
382
+ "# ,
378
383
) ;
379
384
380
385
check_assist_not_applicable (
381
386
replace_derive_with_manual_impl,
382
- "
387
+ r# "
383
388
#[derive(Debug)$0]
384
389
struct Foo {}
385
- " ,
390
+ "# ,
386
391
)
387
392
}
388
393
389
394
#[ test]
390
395
fn test_ignore_if_not_derive ( ) {
391
396
check_assist_not_applicable (
392
397
replace_derive_with_manual_impl,
393
- "
398
+ r# "
394
399
#[allow(non_camel_$0case_types)]
395
400
struct Foo {}
396
- " ,
401
+ "# ,
397
402
)
398
403
}
404
+
405
+ #[ test]
406
+ fn works_at_start_of_file ( ) {
407
+ cov_mark:: check!( outside_of_attr_args) ;
408
+ check_assist_not_applicable (
409
+ replace_derive_with_manual_impl,
410
+ r#"
411
+ $0#[derive(Debug)]
412
+ struct S;
413
+ "# ,
414
+ ) ;
415
+ }
399
416
}
0 commit comments