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 --- crates/ide/src/hover.rs | 4 +- .../handlers/generate_default_from_enum_variant.rs | 41 ++++++----- .../src/handlers/generate_default_from_new.rs | 83 +++++++++++++--------- crates/ide_completion/src/completions/record.rs | 28 ++------ crates/ide_db/src/helpers/famous_defs_fixture.rs | 6 -- crates/test_utils/src/minicore.rs | 10 +++ 6 files changed, 94 insertions(+), 78 deletions(-) diff --git a/crates/ide/src/hover.rs b/crates/ide/src/hover.rs index 529cf5f33..592610eab 100644 --- a/crates/ide/src/hover.rs +++ b/crates/ide/src/hover.rs @@ -3016,8 +3016,8 @@ fn foo() { file_id: FileId( 1, ), - full_range: 245..427, - focus_range: 284..290, + full_range: 246..428, + focus_range: 285..291, name: "Future", kind: Trait, description: "pub trait Future", 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); - } } diff --git a/crates/ide_completion/src/completions/record.rs b/crates/ide_completion/src/completions/record.rs index 47523f72f..e876337f1 100644 --- a/crates/ide_completion/src/completions/record.rs +++ b/crates/ide_completion/src/completions/record.rs @@ -48,10 +48,9 @@ pub(crate) fn complete_record(acc: &mut Completions, ctx: &CompletionContext) -> #[cfg(test)] mod tests { use expect_test::{expect, Expect}; - use ide_db::helpers::FamousDefs; use crate::{ - tests::{self, filtered_completion_list}, + tests::{check_edit, filtered_completion_list}, CompletionKind, }; @@ -61,31 +60,17 @@ mod tests { } fn check_snippet(ra_fixture: &str, expect: Expect) { - let actual = filtered_completion_list( - &format!("//- /main.rs crate:main deps:core\n{}\n{}", ra_fixture, FamousDefs::FIXTURE), - CompletionKind::Snippet, - ); + let actual = filtered_completion_list(ra_fixture, CompletionKind::Snippet); expect.assert_eq(&actual); } - fn check_edit(what: &str, ra_fixture_before: &str, ra_fixture_after: &str) { - tests::check_edit( - what, - &format!( - "//- /main.rs crate:main deps:core{}\n{}", - ra_fixture_before, - FamousDefs::FIXTURE, - ), - &(ra_fixture_after.to_owned() + "\n"), - ); - } - #[test] fn test_record_literal_field_default() { let test_code = r#" +//- minicore: default struct S { foo: u32, bar: usize } -impl core::default::Default for S { +impl Default for S { fn default() -> Self { S { foo: 0, @@ -121,9 +106,10 @@ fn process(f: S) { check_edit( "..Default::default()", r#" +//- minicore: default struct S { foo: u32, bar: usize } -impl core::default::Default for S { +impl Default for S { fn default() -> Self { S { foo: 0, @@ -142,7 +128,7 @@ fn process(f: S) { r#" struct S { foo: u32, bar: usize } -impl core::default::Default for S { +impl Default for S { fn default() -> Self { S { foo: 0, diff --git a/crates/ide_db/src/helpers/famous_defs_fixture.rs b/crates/ide_db/src/helpers/famous_defs_fixture.rs index 551203936..5e0d514cf 100644 --- a/crates/ide_db/src/helpers/famous_defs_fixture.rs +++ b/crates/ide_db/src/helpers/famous_defs_fixture.rs @@ -20,12 +20,6 @@ pub mod convert { } } -pub mod default { - pub trait Default { - fn default() -> Self; - } -} - pub mod option { pub enum Option { None, diff --git a/crates/test_utils/src/minicore.rs b/crates/test_utils/src/minicore.rs index e6d2301c7..4093a04bc 100644 --- a/crates/test_utils/src/minicore.rs +++ b/crates/test_utils/src/minicore.rs @@ -22,6 +22,7 @@ //! result: //! iterator: option //! iterators: iterator +//! default: sized pub mod marker { // region:sized @@ -37,6 +38,14 @@ pub mod marker { // endregion:unsize } +// region:default +pub mod default { + pub trait Default: Sized { + fn default() -> Self; + } +} +// endregion:default + pub mod ops { // region:coerce_unsized mod unsize { @@ -309,6 +318,7 @@ pub mod iter { pub mod prelude { pub mod v1 { pub use crate::{ + default::Default, // :default iter::{IntoIterator, Iterator}, // :iterator marker::Sized, // :sized ops::{Fn, FnMut, FnOnce}, // :fn -- cgit v1.2.3