From 08c220ab2c4651f38d3029898706f2a996bf2c6b Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 17 Jun 2021 20:49:49 +0300 Subject: internal: add default to minicore --- .../handlers/generate_default_from_enum_variant.rs | 41 ++++++----- .../src/handlers/generate_default_from_new.rs | 83 +++++++++++++--------- 2 files changed, 75 insertions(+), 49 deletions(-) (limited to 'crates/ide_assists/src/handlers') 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 @@ -use ide_db::helpers::FamousDefs; -use ide_db::RootDatabase; +use ide_db::{helpers::FamousDefs, RootDatabase}; use syntax::ast::{self, AstNode, NameOwner}; use crate::{AssistContext, AssistId, AssistKind, Assists}; @@ -92,23 +91,20 @@ mod tests { use super::*; - fn check_not_applicable(ra_fixture: &str) { - let fixture = - format!("//- /main.rs crate:main deps:core\n{}\n{}", ra_fixture, FamousDefs::FIXTURE); - check_assist_not_applicable(generate_default_from_enum_variant, &fixture) - } - #[test] fn test_generate_default_from_variant() { check_assist( generate_default_from_enum_variant, r#" +//- minicore: default enum Variant { Undefined, Minor$0, Major, -}"#, - r#"enum Variant { +} +"#, + r#" +enum Variant { Undefined, Minor, Major, @@ -118,15 +114,18 @@ impl Default for Variant { fn default() -> Self { Self::Minor } -}"#, +} +"#, ); } #[test] fn test_generate_default_already_implemented() { cov_mark::check!(test_gen_default_impl_already_exists); - check_not_applicable( + check_assist_not_applicable( + generate_default_from_enum_variant, r#" +//- minicore: default enum Variant { Undefined, Minor$0, @@ -137,20 +136,24 @@ impl Default for Variant { fn default() -> Self { Self::Minor } -}"#, +} +"#, ); } #[test] fn test_add_from_impl_no_element() { cov_mark::check!(test_gen_default_on_non_unit_variant_not_implemented); - check_not_applicable( + check_assist_not_applicable( + generate_default_from_enum_variant, r#" +//- minicore: default enum Variant { Undefined, Minor(u32)$0, Major, -}"#, +} +"#, ); } @@ -158,7 +161,10 @@ enum Variant { fn test_generate_default_from_variant_with_one_variant() { check_assist( generate_default_from_enum_variant, - r#"enum Variant { Undefi$0ned }"#, + r#" +//- minicore: default +enum Variant { Undefi$0ned } +"#, r#" enum Variant { Undefined } @@ -166,7 +172,8 @@ impl Default for Variant { fn default() -> Self { Self::Undefined } -}"#, +} +"#, ); } } 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 @@ -use crate::{ - assist_context::{AssistContext, Assists}, - AssistId, -}; use ide_db::helpers::FamousDefs; use itertools::Itertools; use stdx::format_to; @@ -10,6 +6,11 @@ use syntax::{ AstNode, }; +use crate::{ + assist_context::{AssistContext, Assists}, + AssistId, +}; + // Assist: generate_default_from_new // // Generates default implementation from new method. @@ -140,16 +141,16 @@ fn is_default_implemented(ctx: &AssistContext, impl_: &Impl) -> bool { #[cfg(test)] mod tests { - use ide_db::helpers::FamousDefs; - use crate::tests::{check_assist, check_assist_not_applicable}; use super::*; #[test] fn generate_default() { - check_pass( + check_assist( + generate_default_from_new, r#" +//- minicore: default struct Example { _inner: () } impl Example { @@ -182,8 +183,10 @@ fn main() {} #[test] fn generate_default2() { - check_pass( + check_assist( + generate_default_from_new, r#" +//- minicore: default struct Test { value: u32 } impl Test { @@ -212,8 +215,10 @@ impl Default for Test { #[test] fn new_function_with_generic() { - check_pass( + check_assist( + generate_default_from_new, r#" +//- minicore: default pub struct Foo { _bar: *mut T, } @@ -246,8 +251,10 @@ impl Default for Foo { #[test] fn new_function_with_generics() { - check_pass( + check_assist( + generate_default_from_new, r#" +//- minicore: default pub struct Foo { _tars: *mut T, _bar: *mut B, @@ -282,8 +289,10 @@ impl Default for Foo { #[test] fn new_function_with_generic_and_bound() { - check_pass( + check_assist( + generate_default_from_new, r#" +//- minicore: default pub struct Foo { t: T, } @@ -316,8 +325,10 @@ impl> Default for Foo { #[test] fn new_function_with_generics_and_bounds() { - check_pass( + check_assist( + generate_default_from_new, r#" +//- minicore: default pub struct Foo { _tars: T, _bar: B, @@ -352,8 +363,10 @@ impl, B: From> Default for Foo { #[test] fn new_function_with_generic_and_where() { - check_pass( + check_assist( + generate_default_from_new, r#" +//- minicore: default pub struct Foo { t: T, } @@ -395,8 +408,10 @@ where #[test] fn new_function_with_generics_and_wheres() { - check_pass( + check_assist( + generate_default_from_new, r#" +//- minicore: default pub struct Foo { _tars: T, _bar: B, @@ -441,8 +456,10 @@ where #[test] fn new_function_with_parameters() { cov_mark::check!(new_function_with_parameters); - check_not_applicable( + check_assist_not_applicable( + generate_default_from_new, r#" +//- minicore: default struct Example { _inner: () } impl Example { @@ -457,7 +474,8 @@ impl Example { #[test] fn other_function_than_new() { cov_mark::check!(other_function_than_new); - check_not_applicable( + check_assist_not_applicable( + generate_default_from_new, r#" struct Example { _inner: () } @@ -474,8 +492,10 @@ impl Example { #[test] fn default_block_is_already_present() { cov_mark::check!(default_block_is_already_present); - check_not_applicable( + check_assist_not_applicable( + generate_default_from_new, r#" +//- minicore: default struct Example { _inner: () } impl Example { @@ -495,7 +515,8 @@ impl Default for Example { #[test] fn standalone_new_function() { - check_not_applicable( + check_assist_not_applicable( + generate_default_from_new, r#" fn n$0ew() -> u32 { 0 @@ -506,8 +527,10 @@ fn n$0ew() -> u32 { #[test] fn multiple_struct_blocks() { - check_pass( + check_assist( + generate_default_from_new, r#" +//- minicore: default struct Example { _inner: () } struct Test { value: u32 } @@ -538,8 +561,10 @@ impl Default for Example { #[test] fn when_struct_is_after_impl() { - check_pass( + check_assist( + generate_default_from_new, r#" +//- minicore: default impl Example { pub fn $0new() -> Self { Self { _inner: () } @@ -568,8 +593,10 @@ struct Example { _inner: () } #[test] fn struct_in_module() { - check_pass( + check_assist( + generate_default_from_new, r#" +//- minicore: default mod test { struct Example { _inner: () } @@ -603,8 +630,10 @@ impl Default for Example { #[test] fn struct_in_module_with_default() { cov_mark::check!(struct_in_module_with_default); - check_not_applicable( + check_assist_not_applicable( + generate_default_from_new, r#" +//- minicore: default mod test { struct Example { _inner: () } @@ -623,14 +652,4 @@ mod test { "#, ); } - - fn check_pass(before: &str, after: &str) { - let before = &format!("//- /main.rs crate:main deps:core{}{}", before, FamousDefs::FIXTURE); - check_assist(generate_default_from_new, before, after); - } - - fn check_not_applicable(before: &str) { - let before = &format!("//- /main.rs crate:main deps:core{}{}", before, FamousDefs::FIXTURE); - check_assist_not_applicable(generate_default_from_new, before); - } } -- cgit v1.2.3