diff options
Diffstat (limited to 'crates/ide_assists')
-rw-r--r-- | crates/ide_assists/src/handlers/replace_derive_with_manual_impl.rs | 73 |
1 files changed, 45 insertions, 28 deletions
diff --git a/crates/ide_assists/src/handlers/replace_derive_with_manual_impl.rs b/crates/ide_assists/src/handlers/replace_derive_with_manual_impl.rs index 870a8d4ff..694d897d1 100644 --- a/crates/ide_assists/src/handlers/replace_derive_with_manual_impl.rs +++ b/crates/ide_assists/src/handlers/replace_derive_with_manual_impl.rs | |||
@@ -47,6 +47,11 @@ pub(crate) fn replace_derive_with_manual_impl( | |||
47 | return None; | 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 | let trait_token = args.syntax().token_at_offset(ctx.offset()).find(|t| t.kind() == IDENT)?; | 55 | let trait_token = args.syntax().token_at_offset(ctx.offset()).find(|t| t.kind() == IDENT)?; |
51 | let trait_name = trait_token.text(); | 56 | let trait_name = trait_token.text(); |
52 | 57 | ||
@@ -207,7 +212,7 @@ mod tests { | |||
207 | fn add_custom_impl_debug() { | 212 | fn add_custom_impl_debug() { |
208 | check_assist( | 213 | check_assist( |
209 | replace_derive_with_manual_impl, | 214 | replace_derive_with_manual_impl, |
210 | " | 215 | r#" |
211 | mod fmt { | 216 | mod fmt { |
212 | pub struct Error; | 217 | pub struct Error; |
213 | pub type Result = Result<(), Error>; | 218 | pub type Result = Result<(), Error>; |
@@ -221,8 +226,8 @@ mod fmt { | |||
221 | struct Foo { | 226 | struct Foo { |
222 | bar: String, | 227 | bar: String, |
223 | } | 228 | } |
224 | ", | 229 | "#, |
225 | " | 230 | r#" |
226 | mod fmt { | 231 | mod fmt { |
227 | pub struct Error; | 232 | pub struct Error; |
228 | pub type Result = Result<(), Error>; | 233 | pub type Result = Result<(), Error>; |
@@ -241,14 +246,14 @@ impl fmt::Debug for Foo { | |||
241 | ${0:todo!()} | 246 | ${0:todo!()} |
242 | } | 247 | } |
243 | } | 248 | } |
244 | ", | 249 | "#, |
245 | ) | 250 | ) |
246 | } | 251 | } |
247 | #[test] | 252 | #[test] |
248 | fn add_custom_impl_all() { | 253 | fn add_custom_impl_all() { |
249 | check_assist( | 254 | check_assist( |
250 | replace_derive_with_manual_impl, | 255 | replace_derive_with_manual_impl, |
251 | " | 256 | r#" |
252 | mod foo { | 257 | mod foo { |
253 | pub trait Bar { | 258 | pub trait Bar { |
254 | type Qux; | 259 | type Qux; |
@@ -263,8 +268,8 @@ mod foo { | |||
263 | struct Foo { | 268 | struct Foo { |
264 | bar: String, | 269 | bar: String, |
265 | } | 270 | } |
266 | ", | 271 | "#, |
267 | " | 272 | r#" |
268 | mod foo { | 273 | mod foo { |
269 | pub trait Bar { | 274 | pub trait Bar { |
270 | type Qux; | 275 | type Qux; |
@@ -290,20 +295,20 @@ impl foo::Bar for Foo { | |||
290 | todo!() | 295 | todo!() |
291 | } | 296 | } |
292 | } | 297 | } |
293 | ", | 298 | "#, |
294 | ) | 299 | ) |
295 | } | 300 | } |
296 | #[test] | 301 | #[test] |
297 | fn add_custom_impl_for_unique_input() { | 302 | fn add_custom_impl_for_unique_input() { |
298 | check_assist( | 303 | check_assist( |
299 | replace_derive_with_manual_impl, | 304 | replace_derive_with_manual_impl, |
300 | " | 305 | r#" |
301 | #[derive(Debu$0g)] | 306 | #[derive(Debu$0g)] |
302 | struct Foo { | 307 | struct Foo { |
303 | bar: String, | 308 | bar: String, |
304 | } | 309 | } |
305 | ", | 310 | "#, |
306 | " | 311 | r#" |
307 | struct Foo { | 312 | struct Foo { |
308 | bar: String, | 313 | bar: String, |
309 | } | 314 | } |
@@ -311,7 +316,7 @@ struct Foo { | |||
311 | impl Debug for Foo { | 316 | impl Debug for Foo { |
312 | $0 | 317 | $0 |
313 | } | 318 | } |
314 | ", | 319 | "#, |
315 | ) | 320 | ) |
316 | } | 321 | } |
317 | 322 | ||
@@ -319,13 +324,13 @@ impl Debug for Foo { | |||
319 | fn add_custom_impl_for_with_visibility_modifier() { | 324 | fn add_custom_impl_for_with_visibility_modifier() { |
320 | check_assist( | 325 | check_assist( |
321 | replace_derive_with_manual_impl, | 326 | replace_derive_with_manual_impl, |
322 | " | 327 | r#" |
323 | #[derive(Debug$0)] | 328 | #[derive(Debug$0)] |
324 | pub struct Foo { | 329 | pub struct Foo { |
325 | bar: String, | 330 | bar: String, |
326 | } | 331 | } |
327 | ", | 332 | "#, |
328 | " | 333 | r#" |
329 | pub struct Foo { | 334 | pub struct Foo { |
330 | bar: String, | 335 | bar: String, |
331 | } | 336 | } |
@@ -333,7 +338,7 @@ pub struct Foo { | |||
333 | impl Debug for Foo { | 338 | impl Debug for Foo { |
334 | $0 | 339 | $0 |
335 | } | 340 | } |
336 | ", | 341 | "#, |
337 | ) | 342 | ) |
338 | } | 343 | } |
339 | 344 | ||
@@ -341,18 +346,18 @@ impl Debug for Foo { | |||
341 | fn add_custom_impl_when_multiple_inputs() { | 346 | fn add_custom_impl_when_multiple_inputs() { |
342 | check_assist( | 347 | check_assist( |
343 | replace_derive_with_manual_impl, | 348 | replace_derive_with_manual_impl, |
344 | " | 349 | r#" |
345 | #[derive(Display, Debug$0, Serialize)] | 350 | #[derive(Display, Debug$0, Serialize)] |
346 | struct Foo {} | 351 | struct Foo {} |
347 | ", | 352 | "#, |
348 | " | 353 | r#" |
349 | #[derive(Display, Serialize)] | 354 | #[derive(Display, Serialize)] |
350 | struct Foo {} | 355 | struct Foo {} |
351 | 356 | ||
352 | impl Debug for Foo { | 357 | impl Debug for Foo { |
353 | $0 | 358 | $0 |
354 | } | 359 | } |
355 | ", | 360 | "#, |
356 | ) | 361 | ) |
357 | } | 362 | } |
358 | 363 | ||
@@ -360,10 +365,10 @@ impl Debug for Foo { | |||
360 | fn test_ignore_derive_macro_without_input() { | 365 | fn test_ignore_derive_macro_without_input() { |
361 | check_assist_not_applicable( | 366 | check_assist_not_applicable( |
362 | replace_derive_with_manual_impl, | 367 | replace_derive_with_manual_impl, |
363 | " | 368 | r#" |
364 | #[derive($0)] | 369 | #[derive($0)] |
365 | struct Foo {} | 370 | struct Foo {} |
366 | ", | 371 | "#, |
367 | ) | 372 | ) |
368 | } | 373 | } |
369 | 374 | ||
@@ -371,18 +376,18 @@ struct Foo {} | |||
371 | fn test_ignore_if_cursor_on_param() { | 376 | fn test_ignore_if_cursor_on_param() { |
372 | check_assist_not_applicable( | 377 | check_assist_not_applicable( |
373 | replace_derive_with_manual_impl, | 378 | replace_derive_with_manual_impl, |
374 | " | 379 | r#" |
375 | #[derive$0(Debug)] | 380 | #[derive$0(Debug)] |
376 | struct Foo {} | 381 | struct Foo {} |
377 | ", | 382 | "#, |
378 | ); | 383 | ); |
379 | 384 | ||
380 | check_assist_not_applicable( | 385 | check_assist_not_applicable( |
381 | replace_derive_with_manual_impl, | 386 | replace_derive_with_manual_impl, |
382 | " | 387 | r#" |
383 | #[derive(Debug)$0] | 388 | #[derive(Debug)$0] |
384 | struct Foo {} | 389 | struct Foo {} |
385 | ", | 390 | "#, |
386 | ) | 391 | ) |
387 | } | 392 | } |
388 | 393 | ||
@@ -390,10 +395,22 @@ struct Foo {} | |||
390 | fn test_ignore_if_not_derive() { | 395 | fn test_ignore_if_not_derive() { |
391 | check_assist_not_applicable( | 396 | check_assist_not_applicable( |
392 | replace_derive_with_manual_impl, | 397 | replace_derive_with_manual_impl, |
393 | " | 398 | r#" |
394 | #[allow(non_camel_$0case_types)] | 399 | #[allow(non_camel_$0case_types)] |
395 | struct Foo {} | 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 | } |