diff options
Diffstat (limited to 'crates/ide_assists')
-rw-r--r-- | crates/ide_assists/src/handlers/generate_default_from_enum_variant.rs | 41 | ||||
-rw-r--r-- | crates/ide_assists/src/handlers/generate_default_from_new.rs | 83 |
2 files changed, 75 insertions, 49 deletions
diff --git a/crates/ide_assists/src/handlers/generate_default_from_enum_variant.rs b/crates/ide_assists/src/handlers/generate_default_from_enum_variant.rs index 588ee1350..e55c38502 100644 --- a/crates/ide_assists/src/handlers/generate_default_from_enum_variant.rs +++ b/crates/ide_assists/src/handlers/generate_default_from_enum_variant.rs | |||
@@ -1,5 +1,4 @@ | |||
1 | use ide_db::helpers::FamousDefs; | 1 | use ide_db::{helpers::FamousDefs, RootDatabase}; |
2 | use ide_db::RootDatabase; | ||
3 | use syntax::ast::{self, AstNode, NameOwner}; | 2 | use syntax::ast::{self, AstNode, NameOwner}; |
4 | 3 | ||
5 | use crate::{AssistContext, AssistId, AssistKind, Assists}; | 4 | use crate::{AssistContext, AssistId, AssistKind, Assists}; |
@@ -92,23 +91,20 @@ mod tests { | |||
92 | 91 | ||
93 | use super::*; | 92 | use super::*; |
94 | 93 | ||
95 | fn check_not_applicable(ra_fixture: &str) { | ||
96 | let fixture = | ||
97 | format!("//- /main.rs crate:main deps:core\n{}\n{}", ra_fixture, FamousDefs::FIXTURE); | ||
98 | check_assist_not_applicable(generate_default_from_enum_variant, &fixture) | ||
99 | } | ||
100 | |||
101 | #[test] | 94 | #[test] |
102 | fn test_generate_default_from_variant() { | 95 | fn test_generate_default_from_variant() { |
103 | check_assist( | 96 | check_assist( |
104 | generate_default_from_enum_variant, | 97 | generate_default_from_enum_variant, |
105 | r#" | 98 | r#" |
99 | //- minicore: default | ||
106 | enum Variant { | 100 | enum Variant { |
107 | Undefined, | 101 | Undefined, |
108 | Minor$0, | 102 | Minor$0, |
109 | Major, | 103 | Major, |
110 | }"#, | 104 | } |
111 | r#"enum Variant { | 105 | "#, |
106 | r#" | ||
107 | enum Variant { | ||
112 | Undefined, | 108 | Undefined, |
113 | Minor, | 109 | Minor, |
114 | Major, | 110 | Major, |
@@ -118,15 +114,18 @@ impl Default for Variant { | |||
118 | fn default() -> Self { | 114 | fn default() -> Self { |
119 | Self::Minor | 115 | Self::Minor |
120 | } | 116 | } |
121 | }"#, | 117 | } |
118 | "#, | ||
122 | ); | 119 | ); |
123 | } | 120 | } |
124 | 121 | ||
125 | #[test] | 122 | #[test] |
126 | fn test_generate_default_already_implemented() { | 123 | fn test_generate_default_already_implemented() { |
127 | cov_mark::check!(test_gen_default_impl_already_exists); | 124 | cov_mark::check!(test_gen_default_impl_already_exists); |
128 | check_not_applicable( | 125 | check_assist_not_applicable( |
126 | generate_default_from_enum_variant, | ||
129 | r#" | 127 | r#" |
128 | //- minicore: default | ||
130 | enum Variant { | 129 | enum Variant { |
131 | Undefined, | 130 | Undefined, |
132 | Minor$0, | 131 | Minor$0, |
@@ -137,20 +136,24 @@ impl Default for Variant { | |||
137 | fn default() -> Self { | 136 | fn default() -> Self { |
138 | Self::Minor | 137 | Self::Minor |
139 | } | 138 | } |
140 | }"#, | 139 | } |
140 | "#, | ||
141 | ); | 141 | ); |
142 | } | 142 | } |
143 | 143 | ||
144 | #[test] | 144 | #[test] |
145 | fn test_add_from_impl_no_element() { | 145 | fn test_add_from_impl_no_element() { |
146 | cov_mark::check!(test_gen_default_on_non_unit_variant_not_implemented); | 146 | cov_mark::check!(test_gen_default_on_non_unit_variant_not_implemented); |
147 | check_not_applicable( | 147 | check_assist_not_applicable( |
148 | generate_default_from_enum_variant, | ||
148 | r#" | 149 | r#" |
150 | //- minicore: default | ||
149 | enum Variant { | 151 | enum Variant { |
150 | Undefined, | 152 | Undefined, |
151 | Minor(u32)$0, | 153 | Minor(u32)$0, |
152 | Major, | 154 | Major, |
153 | }"#, | 155 | } |
156 | "#, | ||
154 | ); | 157 | ); |
155 | } | 158 | } |
156 | 159 | ||
@@ -158,7 +161,10 @@ enum Variant { | |||
158 | fn test_generate_default_from_variant_with_one_variant() { | 161 | fn test_generate_default_from_variant_with_one_variant() { |
159 | check_assist( | 162 | check_assist( |
160 | generate_default_from_enum_variant, | 163 | generate_default_from_enum_variant, |
161 | r#"enum Variant { Undefi$0ned }"#, | 164 | r#" |
165 | //- minicore: default | ||
166 | enum Variant { Undefi$0ned } | ||
167 | "#, | ||
162 | r#" | 168 | r#" |
163 | enum Variant { Undefined } | 169 | enum Variant { Undefined } |
164 | 170 | ||
@@ -166,7 +172,8 @@ impl Default for Variant { | |||
166 | fn default() -> Self { | 172 | fn default() -> Self { |
167 | Self::Undefined | 173 | Self::Undefined |
168 | } | 174 | } |
169 | }"#, | 175 | } |
176 | "#, | ||
170 | ); | 177 | ); |
171 | } | 178 | } |
172 | } | 179 | } |
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 | } |