5013: More principled indentation trimming in fixtures r=matklad a=matklad



bors r+
🤖

Co-authored-by: Aleksey Kladov <aleksey.kladov@gmail.com>
This commit is contained in:
bors[bot] 2020-06-23 21:34:12 +00:00 committed by GitHub
commit e2e373e071
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
17 changed files with 543 additions and 560 deletions

View File

@ -986,7 +986,6 @@ fn main() {
",
r"
pub(crate) fn bar() {
${0:todo!()}
}",

View File

@ -497,7 +497,8 @@ macro_rules! foo {
//- /main.rs crate:main deps:crate_with_macro
fn main() {
foo<|>
}",
}
",
r"use crate_with_macro::foo;
fn main() {
@ -853,7 +854,8 @@ fn dep_import() {
//- /main.rs crate:main deps:dep
fn main() {
Struct<|>
}",
}
",
r"use dep::Struct;
fn main() {
@ -879,10 +881,12 @@ pub fn panic_fmt() {}
//- /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 {}
",
);
@ -895,7 +899,6 @@ fn macro_generated() {
auto_import,
r"
//- /lib.rs crate:dep
macro_rules! mac {
() => {
pub struct Cheese;
@ -905,10 +908,10 @@ macro_rules! mac {
mac!();
//- /main.rs crate:main deps:dep
fn main() {
Cheese<|>;
}",
}
",
r"use dep::Cheese;
fn main() {
@ -925,15 +928,14 @@ fn casing() {
auto_import,
r"
//- /lib.rs crate:dep
pub struct FMT;
pub struct fmt;
//- /main.rs crate:main deps:dep
fn main() {
FMT<|>;
}",
}
",
r"use dep::FMT;
fn main() {

View File

@ -263,7 +263,6 @@ fn main() { foo::Foo<|> }
struct Foo;
",
r"$0pub(crate) struct Foo;
",
);
}
@ -279,14 +278,14 @@ fn fix_visibility_of_struct_field() {
);
check_assist(
fix_visibility,
r"//- /lib.rs
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,7 +295,8 @@ pub struct Foo { bar: () }
);
check_assist_not_applicable(
fix_visibility,
r"//- /lib.rs
r"
//- /lib.rs
mod foo;
fn main() { foo::Foo { <|>bar: () }; }
//- /foo.rs
@ -316,14 +316,14 @@ fn fix_visibility_of_enum_variant_field() {
);
check_assist(
fix_visibility,
r"//- /lib.rs
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,7 +333,8 @@ pub enum Foo { Bar { bar: () } }
);
check_assist_not_applicable(
fix_visibility,
r"//- /lib.rs
r"
//- /lib.rs
mod foo;
fn main() { foo::Foo { <|>bar: () }; }
//- /foo.rs
@ -355,14 +356,14 @@ fn fix_visibility_of_union_field() {
);
check_assist(
fix_visibility,
r"//- /lib.rs
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,7 +373,8 @@ pub union Foo { bar: () }
);
check_assist_not_applicable(
fix_visibility,
r"//- /lib.rs
r"
//- /lib.rs
mod foo;
fn main() { foo::Foo { <|>bar: () }; }
//- /foo.rs
@ -470,7 +472,6 @@ pub fn baz() {}
r"$0pub(crate) mod bar {
pub fn baz() {}
}
",
);
@ -492,10 +493,8 @@ fn fix_visibility_of_inline_module_in_other_file() {
//- /foo.rs
mod bar;
//- /foo/bar.rs
pub fn baz() {}
}
",
r"$0pub(crate) mod bar;
",
@ -506,14 +505,16 @@ pub fn baz() {}
fn fix_visibility_of_module_declaration_in_other_file() {
check_assist(
fix_visibility,
r"//- /main.rs
r"
//- /main.rs
mod foo;
fn main() { foo::bar<|>>::baz(); }
//- /foo.rs
mod bar {
pub fn baz() {}
}",
}
",
r"$0pub(crate) mod bar {
pub fn baz() {}
}
@ -525,10 +526,12 @@ pub fn baz() {}
fn adds_pub_when_target_is_in_another_crate() {
check_assist(
fix_visibility,
r"//- /main.rs crate:a deps:foo
r"
//- /main.rs crate:a deps:foo
foo::Bar<|>
//- /lib.rs crate:foo
struct Bar;",
struct Bar;
",
r"$0pub struct Bar;
",
)

View File

@ -110,7 +110,7 @@ fn with_single_file(db: &mut dyn SourceDatabaseExt, ra_fixture: &str) -> FileId
let source_root = SourceRoot::new_local(file_set);
let crate_graph = if let Some(meta) = ra_fixture.lines().find(|it| it.contains("//-")) {
let entry = Fixture::parse_single(meta.trim());
let entry = Fixture::parse_meta_line(meta.trim());
let meta = match ParsedMeta::from(&entry) {
ParsedMeta::File(it) => it,
};

View File

@ -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);
@ -239,7 +239,7 @@ fn caller2() {
"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]",
],
&[],
);
@ -268,7 +268,7 @@ fn test_caller() {
"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]",
],
&[],
);
@ -290,7 +290,7 @@ fn caller() {
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]"],
&[],
);
}
@ -327,9 +327,9 @@ fn call<|>er() {
//- /foo/mod.rs
pub fn callee() {}
"#,
"caller FN_DEF FileId(1) 26..55 29..35",
"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]"],
);
}
@ -350,9 +350,9 @@ 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]"],
);
}
}

View File

@ -42,8 +42,8 @@ fn foo() {}
[
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 @@ fn foo() {}
[
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 @@ fn main() {
[
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 @@ fn main() {
},
CompletionItem {
label: "main()",
source_range: 163..163,
delete: 163..163,
source_range: 164..164,
delete: 164..164,
insert: "main()$0",
kind: Function,
lookup: "main",

View File

@ -541,8 +541,8 @@ fn foo() { let _ = S::<|> }
[
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 @@ fn foo() { let _ = S::<|> }
[
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 @@ fn foo() { let _ = S::<|> }
[
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 @@ fn foo() { let _ = S::<|> }
[
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 @@ fn foo() { let _ = S::<|> }
[
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 @@ fn foo() { let _ = S::<|> }
[
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 @@ fn foo() { let _ = U::<|> }
[
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 @@ fn foo() { let _ = Trait::<|> }
[
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 @@ fn foo() { let _ = S::<|> }
[
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 @@ fn foo() { let _ = <S as Trait>::<|> }
[
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 @@ fn foo<T: Sub>() {
[
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 @@ fn foo<T: Sub>() {
},
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 @@ fn foo<T: Sub>() {
},
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 @@ fn foo<T: Sub>() {
},
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 @@ fn subfunc() {
[
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 @@ fn subfunc() {
},
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 @@ fn subfunc() {
},
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 @@ fn subfunc() {
},
CompletionItem {
label: "submethod()",
source_range: 365..365,
delete: 365..365,
source_range: 367..367,
delete: 367..367,
insert: "submethod()$0",
kind: Method,
lookup: "submethod",

View File

@ -781,46 +781,46 @@ fn main() {
[
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 @@ fn foo() {
[
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 @@ fn main() {
[
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 @@ fn main() {
[
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",

View File

@ -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 @@ fn check_apply(before: &str, after: &str, func: DiagnosticChecker) {
/// * 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 @@ fn check_apply_diagnostic_fix_from_position(fixture: &str, after: &str) {
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 @@ fn div(x: i32, y: i32) -> Result<i32, ()> {
}
x / y<|>
}
//- /core/lib.rs
pub mod result {
pub enum Result<T, E> { Ok(T), Err(E) }
@ -431,7 +417,6 @@ fn div<T>(x: T) -> Result<T, i32> {
}
<|>x
}
//- /core/lib.rs
pub mod result {
pub enum Result<T, E> { Ok(T), Err(E) }
@ -464,7 +449,6 @@ fn div(x: i32, y: i32) -> MyResult<i32> {
}
x <|>/ y
}
//- /core/lib.rs
pub mod result {
pub enum Result<T, E> { Ok(T), Err(E) }
@ -474,6 +458,7 @@ pub enum Result<T, E> { Ok(T), Err(E) }
use core::result::Result::{self, Ok, Err};
type MyResult<T> = Result<T, ()>;
fn div(x: i32, y: i32) -> MyResult<i32> {
if y == 0 {
return Err(());

View File

@ -192,27 +192,27 @@ enum E { X(Foo<|>) }
#[test]
fn goto_def_for_module_declaration() {
check_goto(
"
r#"
//- /lib.rs
mod <|>foo;
//- /foo.rs
// empty
",
"foo SOURCE_FILE FileId(2) 0..10",
"// empty\n\n",
"#,
"foo SOURCE_FILE FileId(2) 0..9",
"// empty\n",
);
check_goto(
"
r#"
//- /lib.rs
mod <|>foo;
//- /foo/mod.rs
// empty
",
"foo SOURCE_FILE FileId(2) 0..10",
"// empty\n\n",
"#,
"foo SOURCE_FILE FileId(2) 0..9",
"// empty\n",
);
}
@ -254,14 +254,14 @@ macro_rules! foo { () => { () } }
#[test]
fn goto_def_for_use_alias() {
check_goto(
"
r#"
//- /lib.rs
use foo as bar<|>;
//- /foo/lib.rs
#[macro_export]
macro_rules! foo { () => { () } }",
macro_rules! foo { () => { () } }
"#,
"SOURCE_FILE FileId(2) 0..50",
"#[macro_export]\nmacro_rules! foo { () => { () } }\n",
);
@ -302,7 +302,7 @@ macro_rules! foo { () => { () } }
#[test]
fn goto_def_for_macro_defined_fn_with_arg() {
check_goto(
"
r#"
//- /lib.rs
macro_rules! define_fn {
($name:ident) => (fn $name() {})
@ -313,8 +313,8 @@ macro_rules! define_fn {
fn bar() {
<|>foo();
}
",
"foo FN_DEF FileId(1) 64..80 75..78",
"#,
"foo FN_DEF FileId(1) 65..81 76..79",
"define_fn!(foo);|foo",
);
}
@ -322,7 +322,7 @@ fn bar() {
#[test]
fn goto_def_for_macro_defined_fn_no_arg() {
check_goto(
"
r#"
//- /lib.rs
macro_rules! define_fn {
() => (fn foo() {})
@ -333,8 +333,8 @@ macro_rules! define_fn {
fn bar() {
<|>foo();
}
",
"foo FN_DEF FileId(1) 51..64 51..64",
"#,
"foo FN_DEF FileId(1) 52..65 52..65",
"define_fn!();|define_fn!();",
);
}
@ -804,7 +804,7 @@ struct Foo<T: Clone> {
#[test]
fn goto_within_macro() {
check_goto(
"
r#"
//- /lib.rs
macro_rules! id {
($($tt:tt)*) => ($($tt)*)
@ -817,13 +817,13 @@ fn foo() {
let z = y;
});
}
",
"x BIND_PAT FileId(1) 69..70",
"#,
"x BIND_PAT FileId(1) 70..71",
"x",
);
check_goto(
"
r#"
//- /lib.rs
macro_rules! id {
($($tt:tt)*) => ($($tt)*)
@ -836,8 +836,8 @@ fn foo() {
let z = <|>y;
});
}
",
"y BIND_PAT FileId(1) 98..99",
"#,
"y BIND_PAT FileId(1) 99..100",
"y",
);
}

View File

@ -2115,11 +2115,11 @@ fn main() {
file_id: FileId(
1,
),
full_range: 41..54,
full_range: 42..55,
name: "B",
kind: STRUCT_DEF,
focus_range: Some(
48..49,
49..50,
),
container_name: None,
description: Some(

View File

@ -427,8 +427,8 @@ fn f() {
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 @@ pub struct Foo {
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 @@ pub(super) struct Foo<|> {
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 @@ pub fn quux<|>() {}
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 @@ pub fn quux<|>() {}
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 @@ fn g() {
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"],
);
}

View File

@ -310,11 +310,11 @@ fn bench() {}
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 @@ fn bench() {}
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 @@ fn bench() {}
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 @@ fn foo() {}
file_id: FileId(
1,
),
full_range: 22..64,
full_range: 23..65,
name: "foo",
kind: FN_DEF,
focus_range: None,
@ -489,7 +489,7 @@ fn foo() {}
file_id: FileId(
1,
),
full_range: 51..105,
full_range: 52..106,
name: "foo",
kind: FN_DEF,
focus_range: None,

View File

@ -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 }
}
}
]),

View File

@ -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"
);
}
}

View File

@ -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,47 +26,35 @@ impl Fixture {
/// // - other meta
/// ```
pub fn parse(ra_fixture: &str) -> Vec<Fixture> {
let fixture = indent_first_line(ra_fixture);
let margin = fixture_margin(&fixture);
let fixture = trim_indent(ra_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("//-") {
let mut res: Vec<Fixture> = Vec::new();
for (ix, line) in lines_with_ends(&fixture).enumerate() {
if line.contains("//-") {
assert!(
!line_content.contains("//-"),
r#"Metadata line {} has invalid indentation. All metadata lines need to have the same indentation.
The offending line: {:?}"#,
line.starts_with("//-"),
"Metadata line {} has invalid indentation. \
All metadata lines need to have the same indentation.\n\
The offending line: {:?}",
ix,
line
);
}
Some(line_content)
} else {
assert!(line.trim().is_empty());
None
}
});
let mut res: Vec<Fixture> = Vec::new();
for line in lines.by_ref() {
if line.starts_with("//-") {
let meta = Fixture::parse_single(line);
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
}
//- /lib.rs crate:foo deps:bar,baz cfg:foo=a,bar=b env:OUTDIR=path/to,OTHER=foo
pub fn parse_single(meta: &str) -> Fixture {
pub fn parse_meta_line(meta: &str) -> Fixture {
assert!(meta.starts_with("//-"));
let meta = meta["//-".len()..].trim();
let components = meta.split_ascii_whitespace().collect::<Vec<_>>();
@ -118,51 +106,6 @@ pub fn parse_single(meta: &str) -> Fixture {
}
}
/// 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::<Vec<_>>().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 @@ pub fn baz() {}
);
}
#[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);

View File

@ -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)*);