diff options
author | bors[bot] <26634292+bors[bot]@users.noreply.github.com> | 2021-06-17 18:50:15 +0100 |
---|---|---|
committer | GitHub <[email protected]> | 2021-06-17 18:50:15 +0100 |
commit | 53d26164c6608fd5149180c91fa49c7cde79706f (patch) | |
tree | e65e8eeb44c21a0f1e378c0c32a565c3c09375bc /crates/ide_assists/src/handlers/generate_default_from_new.rs | |
parent | ce926aebc4461e38535047958c0b6f72b7a0c0ea (diff) | |
parent | 08c220ab2c4651f38d3029898706f2a996bf2c6b (diff) |
Merge #9316
9316: internal: add default to minicore r=matklad a=matklad
bors r+
🤖
Co-authored-by: Aleksey Kladov <[email protected]>
Diffstat (limited to 'crates/ide_assists/src/handlers/generate_default_from_new.rs')
-rw-r--r-- | crates/ide_assists/src/handlers/generate_default_from_new.rs | 83 |
1 files changed, 51 insertions, 32 deletions
diff --git a/crates/ide_assists/src/handlers/generate_default_from_new.rs b/crates/ide_assists/src/handlers/generate_default_from_new.rs index bad826366..b54ec59da 100644 --- a/crates/ide_assists/src/handlers/generate_default_from_new.rs +++ b/crates/ide_assists/src/handlers/generate_default_from_new.rs | |||
@@ -1,7 +1,3 @@ | |||
1 | use crate::{ | ||
2 | assist_context::{AssistContext, Assists}, | ||
3 | AssistId, | ||
4 | }; | ||
5 | use ide_db::helpers::FamousDefs; | 1 | use ide_db::helpers::FamousDefs; |
6 | use itertools::Itertools; | 2 | use itertools::Itertools; |
7 | use stdx::format_to; | 3 | use stdx::format_to; |
@@ -10,6 +6,11 @@ use syntax::{ | |||
10 | AstNode, | 6 | AstNode, |
11 | }; | 7 | }; |
12 | 8 | ||
9 | use crate::{ | ||
10 | assist_context::{AssistContext, Assists}, | ||
11 | AssistId, | ||
12 | }; | ||
13 | |||
13 | // Assist: generate_default_from_new | 14 | // Assist: generate_default_from_new |
14 | // | 15 | // |
15 | // Generates default implementation from new method. | 16 | // Generates default implementation from new method. |
@@ -140,16 +141,16 @@ fn is_default_implemented(ctx: &AssistContext, impl_: &Impl) -> bool { | |||
140 | 141 | ||
141 | #[cfg(test)] | 142 | #[cfg(test)] |
142 | mod tests { | 143 | mod tests { |
143 | use ide_db::helpers::FamousDefs; | ||
144 | |||
145 | use crate::tests::{check_assist, check_assist_not_applicable}; | 144 | use crate::tests::{check_assist, check_assist_not_applicable}; |
146 | 145 | ||
147 | use super::*; | 146 | use super::*; |
148 | 147 | ||
149 | #[test] | 148 | #[test] |
150 | fn generate_default() { | 149 | fn generate_default() { |
151 | check_pass( | 150 | check_assist( |
151 | generate_default_from_new, | ||
152 | r#" | 152 | r#" |
153 | //- minicore: default | ||
153 | struct Example { _inner: () } | 154 | struct Example { _inner: () } |
154 | 155 | ||
155 | impl Example { | 156 | impl Example { |
@@ -182,8 +183,10 @@ fn main() {} | |||
182 | 183 | ||
183 | #[test] | 184 | #[test] |
184 | fn generate_default2() { | 185 | fn generate_default2() { |
185 | check_pass( | 186 | check_assist( |
187 | generate_default_from_new, | ||
186 | r#" | 188 | r#" |
189 | //- minicore: default | ||
187 | struct Test { value: u32 } | 190 | struct Test { value: u32 } |
188 | 191 | ||
189 | impl Test { | 192 | impl Test { |
@@ -212,8 +215,10 @@ impl Default for Test { | |||
212 | 215 | ||
213 | #[test] | 216 | #[test] |
214 | fn new_function_with_generic() { | 217 | fn new_function_with_generic() { |
215 | check_pass( | 218 | check_assist( |
219 | generate_default_from_new, | ||
216 | r#" | 220 | r#" |
221 | //- minicore: default | ||
217 | pub struct Foo<T> { | 222 | pub struct Foo<T> { |
218 | _bar: *mut T, | 223 | _bar: *mut T, |
219 | } | 224 | } |
@@ -246,8 +251,10 @@ impl<T> Default for Foo<T> { | |||
246 | 251 | ||
247 | #[test] | 252 | #[test] |
248 | fn new_function_with_generics() { | 253 | fn new_function_with_generics() { |
249 | check_pass( | 254 | check_assist( |
255 | generate_default_from_new, | ||
250 | r#" | 256 | r#" |
257 | //- minicore: default | ||
251 | pub struct Foo<T, B> { | 258 | pub struct Foo<T, B> { |
252 | _tars: *mut T, | 259 | _tars: *mut T, |
253 | _bar: *mut B, | 260 | _bar: *mut B, |
@@ -282,8 +289,10 @@ impl<T, B> Default for Foo<T, B> { | |||
282 | 289 | ||
283 | #[test] | 290 | #[test] |
284 | fn new_function_with_generic_and_bound() { | 291 | fn new_function_with_generic_and_bound() { |
285 | check_pass( | 292 | check_assist( |
293 | generate_default_from_new, | ||
286 | r#" | 294 | r#" |
295 | //- minicore: default | ||
287 | pub struct Foo<T> { | 296 | pub struct Foo<T> { |
288 | t: T, | 297 | t: T, |
289 | } | 298 | } |
@@ -316,8 +325,10 @@ impl<T: From<i32>> Default for Foo<T> { | |||
316 | 325 | ||
317 | #[test] | 326 | #[test] |
318 | fn new_function_with_generics_and_bounds() { | 327 | fn new_function_with_generics_and_bounds() { |
319 | check_pass( | 328 | check_assist( |
329 | generate_default_from_new, | ||
320 | r#" | 330 | r#" |
331 | //- minicore: default | ||
321 | pub struct Foo<T, B> { | 332 | pub struct Foo<T, B> { |
322 | _tars: T, | 333 | _tars: T, |
323 | _bar: B, | 334 | _bar: B, |
@@ -352,8 +363,10 @@ impl<T: From<i32>, B: From<i64>> Default for Foo<T, B> { | |||
352 | 363 | ||
353 | #[test] | 364 | #[test] |
354 | fn new_function_with_generic_and_where() { | 365 | fn new_function_with_generic_and_where() { |
355 | check_pass( | 366 | check_assist( |
367 | generate_default_from_new, | ||
356 | r#" | 368 | r#" |
369 | //- minicore: default | ||
357 | pub struct Foo<T> { | 370 | pub struct Foo<T> { |
358 | t: T, | 371 | t: T, |
359 | } | 372 | } |
@@ -395,8 +408,10 @@ where | |||
395 | 408 | ||
396 | #[test] | 409 | #[test] |
397 | fn new_function_with_generics_and_wheres() { | 410 | fn new_function_with_generics_and_wheres() { |
398 | check_pass( | 411 | check_assist( |
412 | generate_default_from_new, | ||
399 | r#" | 413 | r#" |
414 | //- minicore: default | ||
400 | pub struct Foo<T, B> { | 415 | pub struct Foo<T, B> { |
401 | _tars: T, | 416 | _tars: T, |
402 | _bar: B, | 417 | _bar: B, |
@@ -441,8 +456,10 @@ where | |||
441 | #[test] | 456 | #[test] |
442 | fn new_function_with_parameters() { | 457 | fn new_function_with_parameters() { |
443 | cov_mark::check!(new_function_with_parameters); | 458 | cov_mark::check!(new_function_with_parameters); |
444 | check_not_applicable( | 459 | check_assist_not_applicable( |
460 | generate_default_from_new, | ||
445 | r#" | 461 | r#" |
462 | //- minicore: default | ||
446 | struct Example { _inner: () } | 463 | struct Example { _inner: () } |
447 | 464 | ||
448 | impl Example { | 465 | impl Example { |
@@ -457,7 +474,8 @@ impl Example { | |||
457 | #[test] | 474 | #[test] |
458 | fn other_function_than_new() { | 475 | fn other_function_than_new() { |
459 | cov_mark::check!(other_function_than_new); | 476 | cov_mark::check!(other_function_than_new); |
460 | check_not_applicable( | 477 | check_assist_not_applicable( |
478 | generate_default_from_new, | ||
461 | r#" | 479 | r#" |
462 | struct Example { _inner: () } | 480 | struct Example { _inner: () } |
463 | 481 | ||
@@ -474,8 +492,10 @@ impl Example { | |||
474 | #[test] | 492 | #[test] |
475 | fn default_block_is_already_present() { | 493 | fn default_block_is_already_present() { |
476 | cov_mark::check!(default_block_is_already_present); | 494 | cov_mark::check!(default_block_is_already_present); |
477 | check_not_applicable( | 495 | check_assist_not_applicable( |
496 | generate_default_from_new, | ||
478 | r#" | 497 | r#" |
498 | //- minicore: default | ||
479 | struct Example { _inner: () } | 499 | struct Example { _inner: () } |
480 | 500 | ||
481 | impl Example { | 501 | impl Example { |
@@ -495,7 +515,8 @@ impl Default for Example { | |||
495 | 515 | ||
496 | #[test] | 516 | #[test] |
497 | fn standalone_new_function() { | 517 | fn standalone_new_function() { |
498 | check_not_applicable( | 518 | check_assist_not_applicable( |
519 | generate_default_from_new, | ||
499 | r#" | 520 | r#" |
500 | fn n$0ew() -> u32 { | 521 | fn n$0ew() -> u32 { |
501 | 0 | 522 | 0 |
@@ -506,8 +527,10 @@ fn n$0ew() -> u32 { | |||
506 | 527 | ||
507 | #[test] | 528 | #[test] |
508 | fn multiple_struct_blocks() { | 529 | fn multiple_struct_blocks() { |
509 | check_pass( | 530 | check_assist( |
531 | generate_default_from_new, | ||
510 | r#" | 532 | r#" |
533 | //- minicore: default | ||
511 | struct Example { _inner: () } | 534 | struct Example { _inner: () } |
512 | struct Test { value: u32 } | 535 | struct Test { value: u32 } |
513 | 536 | ||
@@ -538,8 +561,10 @@ impl Default for Example { | |||
538 | 561 | ||
539 | #[test] | 562 | #[test] |
540 | fn when_struct_is_after_impl() { | 563 | fn when_struct_is_after_impl() { |
541 | check_pass( | 564 | check_assist( |
565 | generate_default_from_new, | ||
542 | r#" | 566 | r#" |
567 | //- minicore: default | ||
543 | impl Example { | 568 | impl Example { |
544 | pub fn $0new() -> Self { | 569 | pub fn $0new() -> Self { |
545 | Self { _inner: () } | 570 | Self { _inner: () } |
@@ -568,8 +593,10 @@ struct Example { _inner: () } | |||
568 | 593 | ||
569 | #[test] | 594 | #[test] |
570 | fn struct_in_module() { | 595 | fn struct_in_module() { |
571 | check_pass( | 596 | check_assist( |
597 | generate_default_from_new, | ||
572 | r#" | 598 | r#" |
599 | //- minicore: default | ||
573 | mod test { | 600 | mod test { |
574 | struct Example { _inner: () } | 601 | struct Example { _inner: () } |
575 | 602 | ||
@@ -603,8 +630,10 @@ impl Default for Example { | |||
603 | #[test] | 630 | #[test] |
604 | fn struct_in_module_with_default() { | 631 | fn struct_in_module_with_default() { |
605 | cov_mark::check!(struct_in_module_with_default); | 632 | cov_mark::check!(struct_in_module_with_default); |
606 | check_not_applicable( | 633 | check_assist_not_applicable( |
634 | generate_default_from_new, | ||
607 | r#" | 635 | r#" |
636 | //- minicore: default | ||
608 | mod test { | 637 | mod test { |
609 | struct Example { _inner: () } | 638 | struct Example { _inner: () } |
610 | 639 | ||
@@ -623,14 +652,4 @@ mod test { | |||
623 | "#, | 652 | "#, |
624 | ); | 653 | ); |
625 | } | 654 | } |
626 | |||
627 | fn check_pass(before: &str, after: &str) { | ||
628 | let before = &format!("//- /main.rs crate:main deps:core{}{}", before, FamousDefs::FIXTURE); | ||
629 | check_assist(generate_default_from_new, before, after); | ||
630 | } | ||
631 | |||
632 | fn check_not_applicable(before: &str) { | ||
633 | let before = &format!("//- /main.rs crate:main deps:core{}{}", before, FamousDefs::FIXTURE); | ||
634 | check_assist_not_applicable(generate_default_from_new, before); | ||
635 | } | ||
636 | } | 655 | } |