From aa69757a01c26cfad12498053c55cbc3d66a4bdb Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Tue, 23 Jun 2020 22:27:24 +0200 Subject: [PATCH] More principled indentation trimming in fixtures --- .../ra_assists/src/handlers/add_function.rs | 1 - crates/ra_assists/src/handlers/auto_import.rs | 92 +++++----- .../ra_assists/src/handlers/fix_visibility.rs | 155 ++++++++-------- crates/ra_ide/src/call_hierarchy.rs | 172 +++++++++--------- .../complete_macro_in_item_position.rs | 16 +- .../src/completion/complete_qualified_path.rs | 112 ++++++------ .../completion/complete_unqualified_path.rs | 48 ++--- crates/ra_ide/src/diagnostics.rs | 23 +-- crates/ra_ide/src/goto_definition.rs | 146 +++++++-------- crates/ra_ide/src/hover.rs | 86 ++++----- crates/ra_ide/src/references.rs | 16 +- crates/ra_ide/src/runnables.rs | 16 +- .../rust-analyzer/tests/heavy_tests/main.rs | 20 +- crates/stdx/src/lib.rs | 82 +++++++++ crates/test_utils/src/fixture.rs | 110 ++--------- crates/test_utils/src/lib.rs | 2 +- 16 files changed, 540 insertions(+), 557 deletions(-) diff --git a/crates/ra_assists/src/handlers/add_function.rs b/crates/ra_assists/src/handlers/add_function.rs index 1cfbd75aa1..19ce81d99e 100644 --- a/crates/ra_assists/src/handlers/add_function.rs +++ b/crates/ra_assists/src/handlers/add_function.rs @@ -986,7 +986,6 @@ fn main() { ", r" - pub(crate) fn bar() { ${0:todo!()} }", diff --git a/crates/ra_assists/src/handlers/auto_import.rs b/crates/ra_assists/src/handlers/auto_import.rs index 5092bf3366..d1cafa7d9d 100644 --- a/crates/ra_assists/src/handlers/auto_import.rs +++ b/crates/ra_assists/src/handlers/auto_import.rs @@ -488,16 +488,17 @@ mod tests { check_assist( auto_import, r" - //- /lib.rs crate:crate_with_macro - #[macro_export] - macro_rules! foo { - () => () - } +//- /lib.rs crate:crate_with_macro +#[macro_export] +macro_rules! foo { + () => () +} - //- /main.rs crate:main deps:crate_with_macro - fn main() { - foo<|> - }", +//- /main.rs crate:main deps:crate_with_macro +fn main() { + foo<|> +} +", r"use crate_with_macro::foo; fn main() { @@ -847,13 +848,14 @@ fn main() { check_assist( auto_import, r" - //- /lib.rs crate:dep - pub struct Struct; +//- /lib.rs crate:dep +pub struct Struct; - //- /main.rs crate:main deps:dep - fn main() { - Struct<|> - }", +//- /main.rs crate:main deps:dep +fn main() { + Struct<|> +} +", r"use dep::Struct; fn main() { @@ -869,20 +871,22 @@ fn main() { check_assist( auto_import, r" - //- /lib.rs crate:dep - pub mod fmt { - pub trait Display {} - } +//- /lib.rs crate:dep +pub mod fmt { + pub trait Display {} +} - pub fn panic_fmt() {} +pub fn panic_fmt() {} - //- /main.rs crate:main deps:dep - struct S; +//- /main.rs crate:main deps:dep +struct S; - impl f<|>mt::Display for S {}", +impl f<|>mt::Display for S {} +", r"use dep::fmt; struct S; + impl fmt::Display for S {} ", ); @@ -894,21 +898,20 @@ impl fmt::Display for S {} check_assist( auto_import, r" - //- /lib.rs crate:dep +//- /lib.rs crate:dep +macro_rules! mac { + () => { + pub struct Cheese; + }; +} - macro_rules! mac { - () => { - pub struct Cheese; - }; - } +mac!(); - mac!(); - - //- /main.rs crate:main deps:dep - - fn main() { - Cheese<|>; - }", +//- /main.rs crate:main deps:dep +fn main() { + Cheese<|>; +} +", r"use dep::Cheese; fn main() { @@ -924,16 +927,15 @@ fn main() { check_assist( auto_import, r" - //- /lib.rs crate:dep +//- /lib.rs crate:dep +pub struct FMT; +pub struct fmt; - pub struct FMT; - pub struct fmt; - - //- /main.rs crate:main deps:dep - - fn main() { - FMT<|>; - }", +//- /main.rs crate:main deps:dep +fn main() { + FMT<|>; +} +", r"use dep::FMT; fn main() { diff --git a/crates/ra_assists/src/handlers/fix_visibility.rs b/crates/ra_assists/src/handlers/fix_visibility.rs index 531b3560f2..54601d1f39 100644 --- a/crates/ra_assists/src/handlers/fix_visibility.rs +++ b/crates/ra_assists/src/handlers/fix_visibility.rs @@ -255,15 +255,14 @@ mod tests { check_assist( fix_visibility, r" - //- /main.rs - mod foo; - fn main() { foo::Foo<|> } +//- /main.rs +mod foo; +fn main() { foo::Foo<|> } - //- /foo.rs - struct Foo; - ", +//- /foo.rs +struct Foo; +", r"$0pub(crate) struct Foo; - ", ); } @@ -279,14 +278,14 @@ mod tests { ); check_assist( fix_visibility, - r"//- /lib.rs - mod foo; - fn main() { foo::Foo { <|>bar: () }; } - //- /foo.rs - pub struct Foo { bar: () } - ", + r" +//- /lib.rs +mod foo; +fn main() { foo::Foo { <|>bar: () }; } +//- /foo.rs +pub struct Foo { bar: () } +", r"pub struct Foo { $0pub(crate) bar: () } - ", ); check_assist_not_applicable( @@ -296,12 +295,13 @@ mod tests { ); check_assist_not_applicable( fix_visibility, - r"//- /lib.rs - mod foo; - fn main() { foo::Foo { <|>bar: () }; } - //- /foo.rs - pub struct Foo { pub bar: () } - ", + r" +//- /lib.rs +mod foo; +fn main() { foo::Foo { <|>bar: () }; } +//- /foo.rs +pub struct Foo { pub bar: () } +", ); } @@ -316,14 +316,14 @@ mod tests { ); check_assist( fix_visibility, - r"//- /lib.rs - mod foo; - fn main() { foo::Foo::Bar { <|>bar: () }; } - //- /foo.rs - pub enum Foo { Bar { bar: () } } - ", + r" +//- /lib.rs +mod foo; +fn main() { foo::Foo::Bar { <|>bar: () }; } +//- /foo.rs +pub enum Foo { Bar { bar: () } } +", r"pub enum Foo { Bar { $0pub(crate) bar: () } } - ", ); check_assist_not_applicable( @@ -333,12 +333,13 @@ mod tests { ); check_assist_not_applicable( fix_visibility, - r"//- /lib.rs - mod foo; - fn main() { foo::Foo { <|>bar: () }; } - //- /foo.rs - pub struct Foo { pub bar: () } - ", + r" +//- /lib.rs +mod foo; +fn main() { foo::Foo { <|>bar: () }; } +//- /foo.rs +pub struct Foo { pub bar: () } +", ); } @@ -355,14 +356,14 @@ mod tests { ); check_assist( fix_visibility, - r"//- /lib.rs - mod foo; - fn main() { foo::Foo { <|>bar: () }; } - //- /foo.rs - pub union Foo { bar: () } - ", + r" +//- /lib.rs +mod foo; +fn main() { foo::Foo { <|>bar: () }; } +//- /foo.rs +pub union Foo { bar: () } +", r"pub union Foo { $0pub(crate) bar: () } - ", ); check_assist_not_applicable( @@ -372,12 +373,13 @@ mod tests { ); check_assist_not_applicable( fix_visibility, - r"//- /lib.rs - mod foo; - fn main() { foo::Foo { <|>bar: () }; } - //- /foo.rs - pub union Foo { pub bar: () } - ", + r" +//- /lib.rs +mod foo; +fn main() { foo::Foo { <|>bar: () }; } +//- /foo.rs +pub union Foo { pub bar: () } +", ); } @@ -458,19 +460,18 @@ mod tests { check_assist( fix_visibility, r" - //- /main.rs - mod foo; - fn main() { foo::bar<|>::baz(); } +//- /main.rs +mod foo; +fn main() { foo::bar<|>::baz(); } - //- /foo.rs - mod bar { - pub fn baz() {} - } - ", +//- /foo.rs +mod bar { + pub fn baz() {} +} +", r"$0pub(crate) mod bar { pub fn baz() {} } - ", ); @@ -486,17 +487,15 @@ mod tests { check_assist( fix_visibility, r" - //- /main.rs - mod foo; - fn main() { foo::bar<|>::baz(); } +//- /main.rs +mod foo; +fn main() { foo::bar<|>::baz(); } - //- /foo.rs - mod bar; - - //- /foo/bar.rs - pub fn baz() {} - } - ", +//- /foo.rs +mod bar; +//- /foo/bar.rs +pub fn baz() {} +", r"$0pub(crate) mod bar; ", ); @@ -506,14 +505,16 @@ mod tests { fn fix_visibility_of_module_declaration_in_other_file() { check_assist( fix_visibility, - r"//- /main.rs - mod foo; - fn main() { foo::bar<|>>::baz(); } + r" +//- /main.rs +mod foo; +fn main() { foo::bar<|>>::baz(); } - //- /foo.rs - mod bar { - pub fn baz() {} - }", +//- /foo.rs +mod bar { + pub fn baz() {} +} +", r"$0pub(crate) mod bar { pub fn baz() {} } @@ -525,10 +526,12 @@ mod tests { fn adds_pub_when_target_is_in_another_crate() { check_assist( fix_visibility, - r"//- /main.rs crate:a deps:foo - foo::Bar<|> - //- /lib.rs crate:foo - struct Bar;", + r" +//- /main.rs crate:a deps:foo +foo::Bar<|> +//- /lib.rs crate:foo +struct Bar; +", r"$0pub struct Bar; ", ) diff --git a/crates/ra_ide/src/call_hierarchy.rs b/crates/ra_ide/src/call_hierarchy.rs index defd8176ff..1e3a31602c 100644 --- a/crates/ra_ide/src/call_hierarchy.rs +++ b/crates/ra_ide/src/call_hierarchy.rs @@ -145,12 +145,12 @@ mod tests { use crate::mock_analysis::analysis_and_position; fn check_hierarchy( - fixture: &str, + ra_fixture: &str, expected: &str, expected_incoming: &[&str], expected_outgoing: &[&str], ) { - let (analysis, pos) = analysis_and_position(fixture); + let (analysis, pos) = analysis_and_position(ra_fixture); let mut navs = analysis.call_hierarchy(pos).unwrap().unwrap().info; assert_eq!(navs.len(), 1); @@ -177,12 +177,12 @@ mod tests { fn test_call_hierarchy_on_ref() { check_hierarchy( r#" - //- /lib.rs - fn callee() {} - fn caller() { - call<|>ee(); - } - "#, +//- /lib.rs +fn callee() {} +fn caller() { + call<|>ee(); +} +"#, "callee FN_DEF FileId(1) 0..14 3..9", &["caller FN_DEF FileId(1) 15..44 18..24 : [33..39]"], &[], @@ -193,12 +193,12 @@ mod tests { fn test_call_hierarchy_on_def() { check_hierarchy( r#" - //- /lib.rs - fn call<|>ee() {} - fn caller() { - callee(); - } - "#, +//- /lib.rs +fn call<|>ee() {} +fn caller() { + callee(); +} +"#, "callee FN_DEF FileId(1) 0..14 3..9", &["caller FN_DEF FileId(1) 15..44 18..24 : [33..39]"], &[], @@ -209,13 +209,13 @@ mod tests { fn test_call_hierarchy_in_same_fn() { check_hierarchy( r#" - //- /lib.rs - fn callee() {} - fn caller() { - call<|>ee(); - callee(); - } - "#, +//- /lib.rs +fn callee() {} +fn caller() { + call<|>ee(); + callee(); +} +"#, "callee FN_DEF FileId(1) 0..14 3..9", &["caller FN_DEF FileId(1) 15..58 18..24 : [33..39, 47..53]"], &[], @@ -226,20 +226,20 @@ mod tests { fn test_call_hierarchy_in_different_fn() { check_hierarchy( r#" - //- /lib.rs - fn callee() {} - fn caller1() { - call<|>ee(); - } +//- /lib.rs +fn callee() {} +fn caller1() { + call<|>ee(); +} - fn caller2() { - callee(); - } - "#, +fn caller2() { + callee(); +} +"#, "callee FN_DEF FileId(1) 0..14 3..9", &[ "caller1 FN_DEF FileId(1) 15..45 18..25 : [34..40]", - "caller2 FN_DEF FileId(1) 46..76 49..56 : [65..71]", + "caller2 FN_DEF FileId(1) 47..77 50..57 : [66..72]", ], &[], ); @@ -249,26 +249,26 @@ mod tests { fn test_call_hierarchy_in_tests_mod() { check_hierarchy( r#" - //- /lib.rs cfg:test - fn callee() {} - fn caller1() { - call<|>ee(); - } +//- /lib.rs cfg:test +fn callee() {} +fn caller1() { + call<|>ee(); +} - #[cfg(test)] - mod tests { - use super::*; +#[cfg(test)] +mod tests { + use super::*; - #[test] - fn test_caller() { - callee(); - } - } - "#, + #[test] + fn test_caller() { + callee(); + } +} +"#, "callee FN_DEF FileId(1) 0..14 3..9", &[ "caller1 FN_DEF FileId(1) 15..45 18..25 : [34..40]", - "test_caller FN_DEF FileId(1) 93..147 108..119 : [132..138]", + "test_caller FN_DEF FileId(1) 95..149 110..121 : [134..140]", ], &[], ); @@ -278,19 +278,19 @@ mod tests { fn test_call_hierarchy_in_different_files() { check_hierarchy( r#" - //- /lib.rs - mod foo; - use foo::callee; +//- /lib.rs +mod foo; +use foo::callee; - fn caller() { - call<|>ee(); - } +fn caller() { + call<|>ee(); +} - //- /foo/mod.rs - pub fn callee() {} - "#, +//- /foo/mod.rs +pub fn callee() {} +"#, "callee FN_DEF FileId(2) 0..18 7..13", - &["caller FN_DEF FileId(1) 26..55 29..35 : [44..50]"], + &["caller FN_DEF FileId(1) 27..56 30..36 : [45..51]"], &[], ); } @@ -299,13 +299,13 @@ mod tests { fn test_call_hierarchy_outgoing() { check_hierarchy( r#" - //- /lib.rs - fn callee() {} - fn call<|>er() { - callee(); - callee(); - } - "#, +//- /lib.rs +fn callee() {} +fn call<|>er() { + callee(); + callee(); +} +"#, "caller FN_DEF FileId(1) 15..58 18..24", &[], &["callee FN_DEF FileId(1) 0..14 3..9 : [33..39, 47..53]"], @@ -316,20 +316,20 @@ mod tests { fn test_call_hierarchy_outgoing_in_different_files() { check_hierarchy( r#" - //- /lib.rs - mod foo; - use foo::callee; +//- /lib.rs +mod foo; +use foo::callee; - fn call<|>er() { - callee(); - } +fn call<|>er() { + callee(); +} - //- /foo/mod.rs - pub fn callee() {} - "#, - "caller FN_DEF FileId(1) 26..55 29..35", +//- /foo/mod.rs +pub fn callee() {} +"#, + "caller FN_DEF FileId(1) 27..56 30..36", &[], - &["callee FN_DEF FileId(2) 0..18 7..13 : [44..50]"], + &["callee FN_DEF FileId(2) 0..18 7..13 : [45..51]"], ); } @@ -337,22 +337,22 @@ mod tests { fn test_call_hierarchy_incoming_outgoing() { check_hierarchy( r#" - //- /lib.rs - fn caller1() { - call<|>er2(); - } +//- /lib.rs +fn caller1() { + call<|>er2(); +} - fn caller2() { - caller3(); - } +fn caller2() { + caller3(); +} - fn caller3() { +fn caller3() { - } - "#, - "caller2 FN_DEF FileId(1) 32..63 35..42", +} +"#, + "caller2 FN_DEF FileId(1) 33..64 36..43", &["caller1 FN_DEF FileId(1) 0..31 3..10 : [19..26]"], - &["caller3 FN_DEF FileId(1) 64..80 67..74 : [51..58]"], + &["caller3 FN_DEF FileId(1) 66..83 69..76 : [52..59]"], ); } } diff --git a/crates/ra_ide/src/completion/complete_macro_in_item_position.rs b/crates/ra_ide/src/completion/complete_macro_in_item_position.rs index d9bb5fd25e..4c33f41d4f 100644 --- a/crates/ra_ide/src/completion/complete_macro_in_item_position.rs +++ b/crates/ra_ide/src/completion/complete_macro_in_item_position.rs @@ -42,8 +42,8 @@ mod tests { [ CompletionItem { label: "foo!(…)", - source_range: 46..46, - delete: 46..46, + source_range: 48..48, + delete: 48..48, insert: "foo!($0)", kind: Macro, detail: "macro_rules! foo", @@ -82,8 +82,8 @@ mod tests { [ CompletionItem { label: "vec![…]", - source_range: 280..280, - delete: 280..280, + source_range: 282..282, + delete: 282..282, insert: "vec![$0]", kind: Macro, detail: "macro_rules! vec", @@ -119,8 +119,8 @@ mod tests { [ CompletionItem { label: "foo! {…}", - source_range: 163..163, - delete: 163..163, + source_range: 164..164, + delete: 164..164, insert: "foo! {$0}", kind: Macro, detail: "macro_rules! foo", @@ -130,8 +130,8 @@ mod tests { }, CompletionItem { label: "main()", - source_range: 163..163, - delete: 163..163, + source_range: 164..164, + delete: 164..164, insert: "main()$0", kind: Function, lookup: "main", diff --git a/crates/ra_ide/src/completion/complete_qualified_path.rs b/crates/ra_ide/src/completion/complete_qualified_path.rs index 02ac0166b6..d3a1cbc1da 100644 --- a/crates/ra_ide/src/completion/complete_qualified_path.rs +++ b/crates/ra_ide/src/completion/complete_qualified_path.rs @@ -541,8 +541,8 @@ mod tests { [ CompletionItem { label: "m()", - source_range: 100..100, - delete: 100..100, + source_range: 102..102, + delete: 102..102, insert: "m()$0", kind: Function, lookup: "m", @@ -577,8 +577,8 @@ mod tests { [ CompletionItem { label: "m()", - source_range: 105..105, - delete: 105..105, + source_range: 107..107, + delete: 107..107, insert: "m()$0", kind: Method, lookup: "m", @@ -613,8 +613,8 @@ mod tests { [ CompletionItem { label: "C", - source_range: 107..107, - delete: 107..107, + source_range: 109..109, + delete: 109..109, insert: "C", kind: Const, detail: "const C: i32 = 42;", @@ -648,8 +648,8 @@ mod tests { [ CompletionItem { label: "T", - source_range: 101..101, - delete: 101..101, + source_range: 103..103, + delete: 103..103, insert: "T", kind: TypeAlias, detail: "type T = i32;", @@ -688,24 +688,24 @@ mod tests { [ CompletionItem { label: "PUBLIC_CONST", - source_range: 302..302, - delete: 302..302, + source_range: 304..304, + delete: 304..304, insert: "PUBLIC_CONST", kind: Const, detail: "pub(super) const PUBLIC_CONST: u32 = 1;", }, CompletionItem { label: "PublicType", - source_range: 302..302, - delete: 302..302, + source_range: 304..304, + delete: 304..304, insert: "PublicType", kind: TypeAlias, detail: "pub(super) type PublicType = u32;", }, CompletionItem { label: "public_method()", - source_range: 302..302, - delete: 302..302, + source_range: 304..304, + delete: 304..304, insert: "public_method()$0", kind: Function, lookup: "public_method", @@ -737,8 +737,8 @@ mod tests { [ CompletionItem { label: "m()", - source_range: 100..100, - delete: 100..100, + source_range: 102..102, + delete: 102..102, insert: "m()$0", kind: Function, lookup: "m", @@ -773,8 +773,8 @@ mod tests { [ CompletionItem { label: "m()", - source_range: 101..101, - delete: 101..101, + source_range: 103..103, + delete: 103..103, insert: "m()$0", kind: Function, lookup: "m", @@ -834,8 +834,8 @@ mod tests { [ CompletionItem { label: "m()", - source_range: 73..73, - delete: 73..73, + source_range: 74..74, + delete: 74..74, insert: "m()$0", kind: Function, lookup: "m", @@ -870,8 +870,8 @@ mod tests { [ CompletionItem { label: "m()", - source_range: 99..99, - delete: 99..99, + source_range: 101..101, + delete: 101..101, insert: "m()$0", kind: Function, lookup: "m", @@ -906,8 +906,8 @@ mod tests { [ CompletionItem { label: "m()", - source_range: 110..110, - delete: 110..110, + source_range: 112..112, + delete: 112..112, insert: "m()$0", kind: Function, lookup: "m", @@ -950,40 +950,40 @@ mod tests { [ CompletionItem { label: "C2", - source_range: 219..219, - delete: 219..219, + source_range: 221..221, + delete: 221..221, insert: "C2", kind: Const, detail: "const C2: ();", }, CompletionItem { label: "CONST", - source_range: 219..219, - delete: 219..219, + source_range: 221..221, + delete: 221..221, insert: "CONST", kind: Const, detail: "const CONST: u8;", }, CompletionItem { label: "SubTy", - source_range: 219..219, - delete: 219..219, + source_range: 221..221, + delete: 221..221, insert: "SubTy", kind: TypeAlias, detail: "type SubTy;", }, CompletionItem { label: "Ty", - source_range: 219..219, - delete: 219..219, + source_range: 221..221, + delete: 221..221, insert: "Ty", kind: TypeAlias, detail: "type Ty;", }, CompletionItem { label: "func()", - source_range: 219..219, - delete: 219..219, + source_range: 221..221, + delete: 221..221, insert: "func()$0", kind: Function, lookup: "func", @@ -991,8 +991,8 @@ mod tests { }, CompletionItem { label: "method()", - source_range: 219..219, - delete: 219..219, + source_range: 221..221, + delete: 221..221, insert: "method()$0", kind: Method, lookup: "method", @@ -1000,8 +1000,8 @@ mod tests { }, CompletionItem { label: "subfunc()", - source_range: 219..219, - delete: 219..219, + source_range: 221..221, + delete: 221..221, insert: "subfunc()$0", kind: Function, lookup: "subfunc", @@ -1009,8 +1009,8 @@ mod tests { }, CompletionItem { label: "submethod()", - source_range: 219..219, - delete: 219..219, + source_range: 221..221, + delete: 221..221, insert: "submethod()$0", kind: Method, lookup: "submethod", @@ -1055,40 +1055,40 @@ mod tests { [ CompletionItem { label: "C2", - source_range: 365..365, - delete: 365..365, + source_range: 367..367, + delete: 367..367, insert: "C2", kind: Const, detail: "const C2: () = ();", }, CompletionItem { label: "CONST", - source_range: 365..365, - delete: 365..365, + source_range: 367..367, + delete: 367..367, insert: "CONST", kind: Const, detail: "const CONST: u8 = 0;", }, CompletionItem { label: "SubTy", - source_range: 365..365, - delete: 365..365, + source_range: 367..367, + delete: 367..367, insert: "SubTy", kind: TypeAlias, detail: "type SubTy;", }, CompletionItem { label: "Ty", - source_range: 365..365, - delete: 365..365, + source_range: 367..367, + delete: 367..367, insert: "Ty", kind: TypeAlias, detail: "type Ty;", }, CompletionItem { label: "func()", - source_range: 365..365, - delete: 365..365, + source_range: 367..367, + delete: 367..367, insert: "func()$0", kind: Function, lookup: "func", @@ -1096,8 +1096,8 @@ mod tests { }, CompletionItem { label: "method()", - source_range: 365..365, - delete: 365..365, + source_range: 367..367, + delete: 367..367, insert: "method()$0", kind: Method, lookup: "method", @@ -1105,8 +1105,8 @@ mod tests { }, CompletionItem { label: "subfunc()", - source_range: 365..365, - delete: 365..365, + source_range: 367..367, + delete: 367..367, insert: "subfunc()$0", kind: Function, lookup: "subfunc", @@ -1114,8 +1114,8 @@ mod tests { }, CompletionItem { label: "submethod()", - source_range: 365..365, - delete: 365..365, + source_range: 367..367, + delete: 367..367, insert: "submethod()$0", kind: Method, lookup: "submethod", diff --git a/crates/ra_ide/src/completion/complete_unqualified_path.rs b/crates/ra_ide/src/completion/complete_unqualified_path.rs index 68032c37ed..aa2b07a2f1 100644 --- a/crates/ra_ide/src/completion/complete_unqualified_path.rs +++ b/crates/ra_ide/src/completion/complete_unqualified_path.rs @@ -781,46 +781,46 @@ mod tests { [ CompletionItem { label: "bar!(…)", - source_range: 252..252, - delete: 252..252, + source_range: 256..256, + delete: 256..256, insert: "bar!($0)", kind: Macro, detail: "macro_rules! bar", }, CompletionItem { label: "baz!(…)", - source_range: 252..252, - delete: 252..252, + source_range: 256..256, + delete: 256..256, insert: "baz!($0)", kind: Macro, detail: "#[macro_export]\nmacro_rules! baz", }, CompletionItem { label: "foo!(…)", - source_range: 252..252, - delete: 252..252, + source_range: 256..256, + delete: 256..256, insert: "foo!($0)", kind: Macro, detail: "macro_rules! foo", }, CompletionItem { label: "m1", - source_range: 252..252, - delete: 252..252, + source_range: 256..256, + delete: 256..256, insert: "m1", kind: Module, }, CompletionItem { label: "m2", - source_range: 252..252, - delete: 252..252, + source_range: 256..256, + delete: 256..256, insert: "m2", kind: Module, }, CompletionItem { label: "main()", - source_range: 252..252, - delete: 252..252, + source_range: 256..256, + delete: 256..256, insert: "main()$0", kind: Function, lookup: "main", @@ -850,16 +850,16 @@ mod tests { [ CompletionItem { label: "foo!(…)", - source_range: 49..49, - delete: 49..49, + source_range: 50..50, + delete: 50..50, insert: "foo!($0)", kind: Macro, detail: "macro_rules! foo", }, CompletionItem { label: "foo()", - source_range: 49..49, - delete: 49..49, + source_range: 50..50, + delete: 50..50, insert: "foo()$0", kind: Function, lookup: "foo", @@ -889,16 +889,16 @@ mod tests { [ CompletionItem { label: "foo!(…)", - source_range: 57..57, - delete: 57..57, + source_range: 58..58, + delete: 58..58, insert: "foo!($0)", kind: Macro, detail: "macro_rules! foo", }, CompletionItem { label: "main()", - source_range: 57..57, - delete: 57..57, + source_range: 58..58, + delete: 58..58, insert: "main()$0", kind: Function, lookup: "main", @@ -928,16 +928,16 @@ mod tests { [ CompletionItem { label: "foo!(…)", - source_range: 50..50, - delete: 50..50, + source_range: 51..51, + delete: 51..51, insert: "foo!($0)", kind: Macro, detail: "macro_rules! foo", }, CompletionItem { label: "main()", - source_range: 50..50, - delete: 50..50, + source_range: 51..51, + delete: 51..51, insert: "main()$0", kind: Function, lookup: "main", diff --git a/crates/ra_ide/src/diagnostics.rs b/crates/ra_ide/src/diagnostics.rs index 9bde1db8ec..8cb0700b94 100644 --- a/crates/ra_ide/src/diagnostics.rs +++ b/crates/ra_ide/src/diagnostics.rs @@ -283,7 +283,7 @@ fn check_struct_shorthand_initialization( mod tests { use insta::assert_debug_snapshot; use ra_syntax::SourceFile; - use stdx::SepBy; + use stdx::trim_indent; use test_utils::assert_eq_text; use crate::mock_analysis::{analysis_and_position, single_file}; @@ -325,6 +325,8 @@ mod tests { /// * this diagnostic touches the input cursor position /// * that the contents of the file containing the cursor match `after` after the diagnostic fix is applied fn check_apply_diagnostic_fix_from_position(fixture: &str, after: &str) { + let after = trim_indent(after); + let (analysis, file_position) = analysis_and_position(fixture); let diagnostic = analysis.diagnostics(file_position.file_id).unwrap().pop().unwrap(); let mut fix = diagnostic.fix.unwrap(); @@ -336,21 +338,6 @@ mod tests { actual }; - // Strip indent and empty lines from `after`, to match the behaviour of - // `parse_fixture` called from `analysis_and_position`. - let margin = fixture - .lines() - .filter(|it| it.trim_start().starts_with("//-")) - .map(|it| it.len() - it.trim_start().len()) - .next() - .expect("empty fixture"); - let after = after - .lines() - .filter_map(|line| if line.len() > margin { Some(&line[margin..]) } else { None }) - .sep_by("\n") - .suffix("\n") - .to_string(); - assert_eq_text!(&after, &actual); assert!( diagnostic.range.start() <= file_position.offset @@ -400,7 +387,6 @@ mod tests { } x / y<|> } - //- /core/lib.rs pub mod result { pub enum Result { Ok(T), Err(E) } @@ -431,7 +417,6 @@ mod tests { } <|>x } - //- /core/lib.rs pub mod result { pub enum Result { Ok(T), Err(E) } @@ -464,7 +449,6 @@ mod tests { } x <|>/ y } - //- /core/lib.rs pub mod result { pub enum Result { Ok(T), Err(E) } @@ -474,6 +458,7 @@ mod tests { use core::result::Result::{self, Ok, Err}; type MyResult = Result; + fn div(x: i32, y: i32) -> MyResult { if y == 0 { return Err(()); diff --git a/crates/ra_ide/src/goto_definition.rs b/crates/ra_ide/src/goto_definition.rs index 450ce0ba75..bea7fbfa77 100644 --- a/crates/ra_ide/src/goto_definition.rs +++ b/crates/ra_ide/src/goto_definition.rs @@ -192,27 +192,27 @@ mod tests { #[test] fn goto_def_for_module_declaration() { check_goto( - " - //- /lib.rs - mod <|>foo; + r#" +//- /lib.rs +mod <|>foo; - //- /foo.rs - // empty - ", - "foo SOURCE_FILE FileId(2) 0..10", - "// empty\n\n", +//- /foo.rs +// empty +"#, + "foo SOURCE_FILE FileId(2) 0..9", + "// empty\n", ); check_goto( - " - //- /lib.rs - mod <|>foo; + r#" +//- /lib.rs +mod <|>foo; - //- /foo/mod.rs - // empty - ", - "foo SOURCE_FILE FileId(2) 0..10", - "// empty\n\n", +//- /foo/mod.rs +// empty +"#, + "foo SOURCE_FILE FileId(2) 0..9", + "// empty\n", ); } @@ -254,14 +254,14 @@ mod tests { #[test] fn goto_def_for_use_alias() { check_goto( - " - //- /lib.rs - use foo as bar<|>; + r#" +//- /lib.rs +use foo as bar<|>; - - //- /foo/lib.rs - #[macro_export] - macro_rules! foo { () => { () } }", +//- /foo/lib.rs +#[macro_export] +macro_rules! foo { () => { () } } +"#, "SOURCE_FILE FileId(2) 0..50", "#[macro_export]\nmacro_rules! foo { () => { () } }\n", ); @@ -302,19 +302,19 @@ mod tests { #[test] fn goto_def_for_macro_defined_fn_with_arg() { check_goto( - " - //- /lib.rs - macro_rules! define_fn { - ($name:ident) => (fn $name() {}) - } + r#" +//- /lib.rs +macro_rules! define_fn { + ($name:ident) => (fn $name() {}) +} - define_fn!(foo); +define_fn!(foo); - fn bar() { - <|>foo(); - } - ", - "foo FN_DEF FileId(1) 64..80 75..78", +fn bar() { + <|>foo(); +} +"#, + "foo FN_DEF FileId(1) 65..81 76..79", "define_fn!(foo);|foo", ); } @@ -322,19 +322,19 @@ mod tests { #[test] fn goto_def_for_macro_defined_fn_no_arg() { check_goto( - " - //- /lib.rs - macro_rules! define_fn { - () => (fn foo() {}) - } + r#" +//- /lib.rs +macro_rules! define_fn { + () => (fn foo() {}) +} - define_fn!(); +define_fn!(); - fn bar() { - <|>foo(); - } - ", - "foo FN_DEF FileId(1) 51..64 51..64", +fn bar() { + <|>foo(); +} +"#, + "foo FN_DEF FileId(1) 52..65 52..65", "define_fn!();|define_fn!();", ); } @@ -804,40 +804,40 @@ mod tests { #[test] fn goto_within_macro() { check_goto( - " - //- /lib.rs - macro_rules! id { - ($($tt:tt)*) => ($($tt)*) - } + r#" +//- /lib.rs +macro_rules! id { + ($($tt:tt)*) => ($($tt)*) +} - fn foo() { - let x = 1; - id!({ - let y = <|>x; - let z = y; - }); - } - ", - "x BIND_PAT FileId(1) 69..70", +fn foo() { + let x = 1; + id!({ + let y = <|>x; + let z = y; + }); +} +"#, + "x BIND_PAT FileId(1) 70..71", "x", ); check_goto( - " - //- /lib.rs - macro_rules! id { - ($($tt:tt)*) => ($($tt)*) - } + r#" +//- /lib.rs +macro_rules! id { + ($($tt:tt)*) => ($($tt)*) +} - fn foo() { - let x = 1; - id!({ - let y = x; - let z = <|>y; - }); - } - ", - "y BIND_PAT FileId(1) 98..99", +fn foo() { + let x = 1; + id!({ + let y = x; + let z = <|>y; + }); +} +"#, + "y BIND_PAT FileId(1) 99..100", "y", ); } diff --git a/crates/ra_ide/src/hover.rs b/crates/ra_ide/src/hover.rs index d870e4cbce..a898f2e4a0 100644 --- a/crates/ra_ide/src/hover.rs +++ b/crates/ra_ide/src/hover.rs @@ -2106,51 +2106,51 @@ fn func(foo: i32) { if true { <|>foo; }; } ); assert_debug_snapshot!(actions, @r###" - [ - GoToType( - [ - HoverGotoTypeData { - mod_path: "B", - nav: NavigationTarget { - file_id: FileId( - 1, - ), - full_range: 41..54, - name: "B", - kind: STRUCT_DEF, - focus_range: Some( - 48..49, - ), - container_name: None, - description: Some( - "struct B", - ), - docs: None, - }, + [ + GoToType( + [ + HoverGotoTypeData { + mod_path: "B", + nav: NavigationTarget { + file_id: FileId( + 1, + ), + full_range: 42..55, + name: "B", + kind: STRUCT_DEF, + focus_range: Some( + 49..50, + ), + container_name: None, + description: Some( + "struct B", + ), + docs: None, }, - HoverGotoTypeData { - mod_path: "Foo", - nav: NavigationTarget { - file_id: FileId( - 1, - ), - full_range: 0..12, - name: "Foo", - kind: TRAIT_DEF, - focus_range: Some( - 6..9, - ), - container_name: None, - description: Some( - "trait Foo", - ), - docs: None, - }, + }, + HoverGotoTypeData { + mod_path: "Foo", + nav: NavigationTarget { + file_id: FileId( + 1, + ), + full_range: 0..12, + name: "Foo", + kind: TRAIT_DEF, + focus_range: Some( + 6..9, + ), + container_name: None, + description: Some( + "trait Foo", + ), + docs: None, }, - ], - ), - ] - "###); + }, + ], + ), + ] + "###); } #[test] diff --git a/crates/ra_ide/src/references.rs b/crates/ra_ide/src/references.rs index bb40d20437..4a96d65056 100644 --- a/crates/ra_ide/src/references.rs +++ b/crates/ra_ide/src/references.rs @@ -427,8 +427,8 @@ mod tests { let refs = analysis.find_all_refs(pos, None).unwrap().unwrap(); check_result( refs, - "Foo STRUCT_DEF FileId(2) 16..50 27..30 Other", - &["FileId(1) 52..55 StructLiteral", "FileId(3) 77..80 StructLiteral"], + "Foo STRUCT_DEF FileId(2) 17..51 28..31 Other", + &["FileId(1) 53..56 StructLiteral", "FileId(3) 79..82 StructLiteral"], ); } @@ -455,7 +455,7 @@ mod tests { let (analysis, pos) = analysis_and_position(code); let refs = analysis.find_all_refs(pos, None).unwrap().unwrap(); - check_result(refs, "foo SOURCE_FILE FileId(2) 0..35 Other", &["FileId(1) 13..16 Other"]); + check_result(refs, "foo SOURCE_FILE FileId(2) 0..35 Other", &["FileId(1) 14..17 Other"]); } #[test] @@ -483,7 +483,7 @@ mod tests { check_result( refs, "Foo STRUCT_DEF FileId(3) 0..41 18..21 Other", - &["FileId(2) 20..23 Other", "FileId(2) 46..49 StructLiteral"], + &["FileId(2) 20..23 Other", "FileId(2) 47..50 StructLiteral"], ); } @@ -510,7 +510,7 @@ mod tests { let refs = analysis.find_all_refs(pos, None).unwrap().unwrap(); check_result( refs, - "quux FN_DEF FileId(1) 18..34 25..29 Other", + "quux FN_DEF FileId(1) 19..35 26..30 Other", &["FileId(2) 16..20 StructLiteral", "FileId(3) 16..20 StructLiteral"], ); @@ -518,7 +518,7 @@ mod tests { analysis.find_all_refs(pos, Some(SearchScope::single_file(bar))).unwrap().unwrap(); check_result( refs, - "quux FN_DEF FileId(1) 18..34 25..29 Other", + "quux FN_DEF FileId(1) 19..35 26..30 Other", &["FileId(3) 16..20 StructLiteral"], ); } @@ -637,8 +637,8 @@ mod tests { let refs = analysis.find_all_refs(pos, None).unwrap().unwrap(); check_result( refs, - "f FN_DEF FileId(1) 25..34 28..29 Other", - &["FileId(2) 11..12 Other", "FileId(2) 27..28 StructLiteral"], + "f FN_DEF FileId(1) 26..35 29..30 Other", + &["FileId(2) 11..12 Other", "FileId(2) 28..29 StructLiteral"], ); } diff --git a/crates/ra_ide/src/runnables.rs b/crates/ra_ide/src/runnables.rs index 8105ef3732..f569a3f171 100644 --- a/crates/ra_ide/src/runnables.rs +++ b/crates/ra_ide/src/runnables.rs @@ -310,11 +310,11 @@ mod tests { file_id: FileId( 1, ), - full_range: 22..46, + full_range: 23..47, name: "test_foo", kind: FN_DEF, focus_range: Some( - 33..41, + 34..42, ), container_name: None, description: None, @@ -335,11 +335,11 @@ mod tests { file_id: FileId( 1, ), - full_range: 47..81, + full_range: 49..83, name: "test_foo", kind: FN_DEF, focus_range: Some( - 68..76, + 70..78, ), container_name: None, description: None, @@ -360,11 +360,11 @@ mod tests { file_id: FileId( 1, ), - full_range: 82..104, + full_range: 85..107, name: "bench", kind: FN_DEF, focus_range: Some( - 94..99, + 97..102, ), container_name: None, description: None, @@ -424,7 +424,7 @@ mod tests { file_id: FileId( 1, ), - full_range: 22..64, + full_range: 23..65, name: "foo", kind: FN_DEF, focus_range: None, @@ -489,7 +489,7 @@ mod tests { file_id: FileId( 1, ), - full_range: 51..105, + full_range: 52..106, name: "foo", kind: FN_DEF, focus_range: None, diff --git a/crates/rust-analyzer/tests/heavy_tests/main.rs b/crates/rust-analyzer/tests/heavy_tests/main.rs index e0de377b44..58839b14aa 100644 --- a/crates/rust-analyzer/tests/heavy_tests/main.rs +++ b/crates/rust-analyzer/tests/heavy_tests/main.rs @@ -178,14 +178,8 @@ fn main() {} pub use std::collections::HashMap; "#, "range": { - "end": { - "character": 0, - "line": 7 - }, - "start": { - "character": 0, - "line": 0 - } + "end": { "character": 0, "line": 6 }, + "start": { "character": 0, "line": 0 } } } ]), @@ -244,14 +238,8 @@ fn main() {} pub use std::collections::HashMap; "#, "range": { - "end": { - "character": 0, - "line": 10 - }, - "start": { - "character": 0, - "line": 0 - } + "end": { "character": 0, "line": 9 }, + "start": { "character": 0, "line": 0 } } } ]), diff --git a/crates/stdx/src/lib.rs b/crates/stdx/src/lib.rs index 100db9d5db..08ac6f70f3 100644 --- a/crates/stdx/src/lib.rs +++ b/crates/stdx/src/lib.rs @@ -128,3 +128,85 @@ pub fn split_delim(haystack: &str, delim: char) -> Option<(&str, &str)> { let idx = haystack.find(delim)?; Some((&haystack[..idx], &haystack[idx + delim.len_utf8()..])) } + +pub fn trim_indent(mut text: &str) -> String { + if text.starts_with('\n') { + text = &text[1..]; + } + let indent = text + .lines() + .filter(|it| !it.trim().is_empty()) + .map(|it| it.len() - it.trim_start().len()) + .min() + .unwrap_or(0); + lines_with_ends(text) + .map( + |line| { + if line.len() <= indent { + line.trim_start_matches(' ') + } else { + &line[indent..] + } + }, + ) + .collect() +} + +pub fn lines_with_ends(text: &str) -> LinesWithEnds { + LinesWithEnds { text } +} + +pub struct LinesWithEnds<'a> { + text: &'a str, +} + +impl<'a> Iterator for LinesWithEnds<'a> { + type Item = &'a str; + fn next(&mut self) -> Option<&'a str> { + if self.text.is_empty() { + return None; + } + let idx = self.text.find('\n').map_or(self.text.len(), |it| it + 1); + let (res, next) = self.text.split_at(idx); + self.text = next; + Some(res) + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_trim_indent() { + assert_eq!(trim_indent(""), ""); + assert_eq!( + trim_indent( + " + hello + world +" + ), + "hello\nworld\n" + ); + assert_eq!( + trim_indent( + " + hello + world" + ), + "hello\nworld" + ); + assert_eq!(trim_indent(" hello\n world\n"), "hello\nworld\n"); + assert_eq!( + trim_indent( + " + fn main() { + return 92; + } + " + ), + "fn main() {\n return 92;\n}\n" + ); + } +} diff --git a/crates/test_utils/src/fixture.rs b/crates/test_utils/src/fixture.rs index ba928bb9c3..8747fa4a52 100644 --- a/crates/test_utils/src/fixture.rs +++ b/crates/test_utils/src/fixture.rs @@ -2,7 +2,7 @@ //! rust-analyzer database from a single string. use rustc_hash::FxHashMap; -use stdx::split_delim; +use stdx::{lines_with_ends, split_delim, trim_indent}; #[derive(Debug, Eq, PartialEq)] pub struct Fixture { @@ -26,42 +26,30 @@ impl Fixture { /// // - other meta /// ``` pub fn parse(ra_fixture: &str) -> Vec { - let fixture = indent_first_line(ra_fixture); - let margin = fixture_margin(&fixture); - - let mut lines = fixture - .split('\n') // don't use `.lines` to not drop `\r\n` - .enumerate() - .filter_map(|(ix, line)| { - if line.len() >= margin { - assert!(line[..margin].trim().is_empty()); - let line_content = &line[margin..]; - if !line_content.starts_with("//-") { - assert!( - !line_content.contains("//-"), - r#"Metadata line {} has invalid indentation. All metadata lines need to have the same indentation. -The offending line: {:?}"#, - ix, - line - ); - } - Some(line_content) - } else { - assert!(line.trim().is_empty()); - None - } - }); + let fixture = trim_indent(ra_fixture); let mut res: Vec = Vec::new(); - for line in lines.by_ref() { + + for (ix, line) in lines_with_ends(&fixture).enumerate() { + if line.contains("//-") { + assert!( + line.starts_with("//-"), + "Metadata line {} has invalid indentation. \ + All metadata lines need to have the same indentation.\n\ + The offending line: {:?}", + ix, + line + ); + } + if line.starts_with("//-") { let meta = Fixture::parse_meta_line(line); res.push(meta) } else if let Some(entry) = res.last_mut() { entry.text.push_str(line); - entry.text.push('\n'); } } + res } @@ -118,51 +106,6 @@ The offending line: {:?}"#, } } -/// Adjusts the indentation of the first line to the minimum indentation of the rest of the lines. -/// This allows fixtures to start off in a different indentation, e.g. to align the first line with -/// the other lines visually: -/// ``` -/// let fixture = "//- /lib.rs -/// mod foo; -/// //- /foo.rs -/// fn bar() {} -/// "; -/// assert_eq!(fixture_margin(fixture), -/// " //- /lib.rs -/// mod foo; -/// //- /foo.rs -/// fn bar() {} -/// ") -/// ``` -fn indent_first_line(fixture: &str) -> String { - if fixture.is_empty() { - return String::new(); - } - let mut lines = fixture.lines(); - let first_line = lines.next().unwrap(); - if first_line.contains("//-") { - let rest = lines.collect::>().join("\n"); - let fixed_margin = fixture_margin(&rest); - let fixed_indent = fixed_margin - indent_len(first_line); - format!("\n{}{}\n{}", " ".repeat(fixed_indent), first_line, rest) - } else { - fixture.to_owned() - } -} - -fn fixture_margin(fixture: &str) -> usize { - fixture - .lines() - .filter(|it| it.trim_start().starts_with("//-")) - .map(indent_len) - .next() - .expect("empty fixture") -} - -fn indent_len(s: &str) -> usize { - s.len() - s.trim_start().len() -} - #[test] #[should_panic] fn parse_fixture_checks_further_indented_metadata() { @@ -178,25 +121,6 @@ fn parse_fixture_checks_further_indented_metadata() { ); } -#[test] -fn parse_fixture_can_handle_dedented_first_line() { - let fixture = "//- /lib.rs - mod foo; - //- /foo.rs - struct Bar; -"; - assert_eq!( - Fixture::parse(fixture), - Fixture::parse( - "//- /lib.rs -mod foo; -//- /foo.rs -struct Bar; -" - ) - ) -} - #[test] fn parse_fixture_gets_full_meta() { let parsed = Fixture::parse( @@ -208,7 +132,7 @@ fn parse_fixture_gets_full_meta() { assert_eq!(1, parsed.len()); let meta = &parsed[0]; - assert_eq!("mod m;\n\n", meta.text); + assert_eq!("mod m;\n", meta.text); assert_eq!("foo", meta.crate_name.as_ref().unwrap()); assert_eq!("/lib.rs", meta.path); diff --git a/crates/test_utils/src/lib.rs b/crates/test_utils/src/lib.rs index 3fd8505ed2..eaeeeb97b1 100644 --- a/crates/test_utils/src/lib.rs +++ b/crates/test_utils/src/lib.rs @@ -43,7 +43,7 @@ macro_rules! assert_eq_text { if left.trim() == right.trim() { eprintln!("Left:\n{:?}\n\nRight:\n{:?}\n\nWhitespace difference\n", left, right); } else { - let changeset = $crate::__Changeset::new(right, left, "\n"); + let changeset = $crate::__Changeset::new(left, right, "\n"); eprintln!("Left:\n{}\n\nRight:\n{}\n\nDiff:\n{}\n", left, right, changeset); } eprintln!($($tt)*);