5199: Cleanup record completion tests r=matklad a=matklad



bors r+
🤖

Co-authored-by: Aleksey Kladov <aleksey.kladov@gmail.com>
This commit is contained in:
bors[bot] 2020-07-03 09:49:12 +00:00 committed by GitHub
commit 81bb3d9c1a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 243 additions and 390 deletions

View file

@ -18,389 +18,209 @@ pub(super) fn complete_record(acc: &mut Completions, ctx: &CompletionContext) ->
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
mod record_pat_tests { use expect::{expect, Expect};
use insta::assert_debug_snapshot;
use crate::completion::{test_utils::do_completion, CompletionItem, CompletionKind}; use crate::completion::{test_utils::completion_list, CompletionKind};
fn complete(code: &str) -> Vec<CompletionItem> { fn check(ra_fixture: &str, expect: Expect) {
do_completion(code, CompletionKind::Reference) let actual = completion_list(ra_fixture, CompletionKind::Reference);
} expect.assert_eq(&actual);
#[test]
fn test_record_pattern_field() {
let completions = complete(
r"
struct S { foo: u32 }
fn process(f: S) {
match f {
S { f<|>: 92 } => (),
}
}
",
);
assert_debug_snapshot!(completions, @r###"
[
CompletionItem {
label: "foo",
source_range: 68..69,
delete: 68..69,
insert: "foo",
kind: Field,
detail: "u32",
},
]
"###);
}
#[test]
fn test_record_pattern_enum_variant() {
let completions = complete(
r"
enum E {
S { foo: u32, bar: () }
}
fn process(e: E) {
match e {
E::S { <|> } => (),
}
}
",
);
assert_debug_snapshot!(completions, @r###"
[
CompletionItem {
label: "bar",
source_range: 88..88,
delete: 88..88,
insert: "bar",
kind: Field,
detail: "()",
},
CompletionItem {
label: "foo",
source_range: 88..88,
delete: 88..88,
insert: "foo",
kind: Field,
detail: "u32",
},
]
"###);
}
#[test]
fn test_record_pattern_field_in_simple_macro() {
let completions = complete(
r"
macro_rules! m { ($e:expr) => { $e } }
struct S { foo: u32 }
fn process(f: S) {
m!(match f {
S { f<|>: 92 } => (),
})
}
",
);
assert_debug_snapshot!(completions, @r###"
[
CompletionItem {
label: "foo",
source_range: 110..111,
delete: 110..111,
insert: "foo",
kind: Field,
detail: "u32",
},
]
"###);
}
#[test]
fn only_missing_fields_are_completed_in_destruct_pats() {
let completions = complete(
r"
struct S {
foo1: u32,
foo2: u32,
bar: u32,
baz: u32,
}
fn main() {
let s = S {
foo1: 1,
foo2: 2,
bar: 3,
baz: 4,
};
if let S { foo1, foo2: a, <|> } = s {}
}
",
);
assert_debug_snapshot!(completions, @r###"
[
CompletionItem {
label: "bar",
source_range: 203..203,
delete: 203..203,
insert: "bar",
kind: Field,
detail: "u32",
},
CompletionItem {
label: "baz",
source_range: 203..203,
delete: 203..203,
insert: "baz",
kind: Field,
detail: "u32",
},
]
"###);
}
} }
mod record_lit_tests { #[test]
use insta::assert_debug_snapshot; fn test_record_pattern_field() {
check(
r#"
struct S { foo: u32 }
use crate::completion::{test_utils::do_completion, CompletionItem, CompletionKind}; fn process(f: S) {
match f {
fn complete(code: &str) -> Vec<CompletionItem> { S { f<|>: 92 } => (),
do_completion(code, CompletionKind::Reference) }
} }
"#,
#[test] expect![[r#"
fn test_record_literal_deprecated_field() { fd foo u32
let completions = complete( "#]],
r" );
struct A { }
#[deprecated]
the_field: u32, #[test]
} fn test_record_pattern_enum_variant() {
fn foo() { check(
A { the<|> } r#"
} enum E { S { foo: u32, bar: () } }
",
); fn process(e: E) {
assert_debug_snapshot!(completions, @r###" match e {
[ E::S { <|> } => (),
CompletionItem { }
label: "the_field", }
source_range: 69..72, "#,
delete: 69..72, expect![[r#"
insert: "the_field", fd bar ()
kind: Field, fd foo u32
detail: "u32", "#]],
deprecated: true, );
}, }
]
"###); #[test]
} fn test_record_pattern_field_in_simple_macro() {
check(
#[test] r"
fn test_record_literal_field() { macro_rules! m { ($e:expr) => { $e } }
let completions = complete( struct S { foo: u32 }
r"
struct A { the_field: u32 } fn process(f: S) {
fn foo() { m!(match f {
A { the<|> } S { f<|>: 92 } => (),
} })
", }
); ",
assert_debug_snapshot!(completions, @r###" expect![[r#"
[ fd foo u32
CompletionItem { "#]],
label: "the_field", );
source_range: 46..49, }
delete: 46..49,
insert: "the_field", #[test]
kind: Field, fn only_missing_fields_are_completed_in_destruct_pats() {
detail: "u32", check(
}, r#"
] struct S {
"###); foo1: u32, foo2: u32,
} bar: u32, baz: u32,
}
#[test]
fn test_record_literal_enum_variant() { fn main() {
let completions = complete( let s = S {
r" foo1: 1, foo2: 2,
enum E { bar: 3, baz: 4,
A { a: u32 } };
} if let S { foo1, foo2: a, <|> } = s {}
fn foo() { }
let _ = E::A { <|> } "#,
} expect![[r#"
", fd bar u32
); fd baz u32
assert_debug_snapshot!(completions, @r###" "#]],
[ );
CompletionItem { }
label: "a",
source_range: 58..58, #[test]
delete: 58..58, fn test_record_literal_field() {
insert: "a", check(
kind: Field, r#"
detail: "u32", struct A { the_field: u32 }
}, fn foo() {
] A { the<|> }
"###); }
} "#,
expect![[r#"
#[test] fd the_field u32
fn test_record_literal_two_structs() { "#]],
let completions = complete( );
r" }
struct A { a: u32 }
struct B { b: u32 } #[test]
fn test_record_literal_enum_variant() {
fn foo() { check(
let _: A = B { <|> } r#"
} enum E { A { a: u32 } }
", fn foo() {
); let _ = E::A { <|> }
assert_debug_snapshot!(completions, @r###" }
[ "#,
CompletionItem { expect![[r#"
label: "b", fd a u32
source_range: 70..70, "#]],
delete: 70..70, );
insert: "b", }
kind: Field,
detail: "u32", #[test]
}, fn test_record_literal_two_structs() {
] check(
"###); r#"
} struct A { a: u32 }
struct B { b: u32 }
#[test]
fn test_record_literal_generic_struct() { fn foo() {
let completions = complete( let _: A = B { <|> }
r" }
struct A<T> { a: T } "#,
expect![[r#"
fn foo() { fd b u32
let _: A<u32> = A { <|> } "#]],
} );
", }
);
assert_debug_snapshot!(completions, @r###" #[test]
[ fn test_record_literal_generic_struct() {
CompletionItem { check(
label: "a", r#"
source_range: 56..56, struct A<T> { a: T }
delete: 56..56,
insert: "a", fn foo() {
kind: Field, let _: A<u32> = A { <|> }
detail: "u32", }
}, "#,
] expect![[r#"
"###); fd a u32
} "#]],
);
#[test] }
fn test_record_literal_field_in_simple_macro() {
let completions = complete( #[test]
r" fn test_record_literal_field_in_simple_macro() {
macro_rules! m { ($e:expr) => { $e } } check(
struct A { the_field: u32 } r#"
fn foo() { macro_rules! m { ($e:expr) => { $e } }
m!(A { the<|> }) struct A { the_field: u32 }
} fn foo() {
", m!(A { the<|> })
); }
assert_debug_snapshot!(completions, @r###" "#,
[ expect![[r#"
CompletionItem { fd the_field u32
label: "the_field", "#]],
source_range: 88..91, );
delete: 88..91, }
insert: "the_field",
kind: Field, #[test]
detail: "u32", fn only_missing_fields_are_completed() {
}, check(
] r#"
"###); struct S {
} foo1: u32, foo2: u32,
bar: u32, baz: u32,
#[test] }
fn only_missing_fields_are_completed() {
let completions = complete( fn main() {
r" let foo1 = 1;
struct S { let s = S { foo1, foo2: 5, <|> }
foo1: u32, }
foo2: u32, "#,
bar: u32, expect![[r#"
baz: u32, fd bar u32
} fd baz u32
"#]],
fn main() { );
let foo1 = 1; }
let s = S {
foo1, #[test]
foo2: 5, fn completes_functional_update() {
<|> check(
} r#"
} struct S { foo1: u32, foo2: u32 }
",
); fn main() {
assert_debug_snapshot!(completions, @r###" let foo1 = 1;
[ let s = S { foo1, <|> .. loop {} }
CompletionItem { }
label: "bar", "#,
source_range: 157..157, expect![[r#"
delete: 157..157, fd foo2 u32
insert: "bar", "#]],
kind: Field, );
detail: "u32",
},
CompletionItem {
label: "baz",
source_range: 157..157,
delete: 157..157,
insert: "baz",
kind: Field,
detail: "u32",
},
]
"###);
}
#[test]
fn completes_functional_update() {
let completions = complete(
r"
struct S {
foo1: u32,
foo2: u32,
}
fn main() {
let foo1 = 1;
let s = S {
foo1,
<|>
.. loop {}
}
}
",
);
assert_debug_snapshot!(completions, @r###"
[
CompletionItem {
label: "foo2",
source_range: 112..112,
delete: 112..112,
insert: "foo2",
kind: Field,
detail: "u32",
},
]
"###);
}
} }
} }

View file

@ -129,24 +129,24 @@ impl CompletionItemKind {
#[cfg(test)] #[cfg(test)]
pub(crate) fn tag(&self) -> &'static str { pub(crate) fn tag(&self) -> &'static str {
match self { match self {
CompletionItemKind::Snippet => "sn", CompletionItemKind::Attribute => "at",
CompletionItemKind::Keyword => "kw", CompletionItemKind::Binding => "bn",
CompletionItemKind::Module => "md",
CompletionItemKind::Function => "fn",
CompletionItemKind::BuiltinType => "bt", CompletionItemKind::BuiltinType => "bt",
CompletionItemKind::Struct => "st", CompletionItemKind::Const => "ct",
CompletionItemKind::Enum => "en", CompletionItemKind::Enum => "en",
CompletionItemKind::EnumVariant => "ev", CompletionItemKind::EnumVariant => "ev",
CompletionItemKind::Binding => "bn",
CompletionItemKind::Field => "fd", CompletionItemKind::Field => "fd",
CompletionItemKind::Function => "fn",
CompletionItemKind::Keyword => "kw",
CompletionItemKind::Macro => "ma",
CompletionItemKind::Method => "me",
CompletionItemKind::Module => "md",
CompletionItemKind::Snippet => "sn",
CompletionItemKind::Static => "sc", CompletionItemKind::Static => "sc",
CompletionItemKind::Const => "ct", CompletionItemKind::Struct => "st",
CompletionItemKind::Trait => "tt", CompletionItemKind::Trait => "tt",
CompletionItemKind::TypeAlias => "ta", CompletionItemKind::TypeAlias => "ta",
CompletionItemKind::Method => "me",
CompletionItemKind::TypeParam => "tp", CompletionItemKind::TypeParam => "tp",
CompletionItemKind::Macro => "ma",
CompletionItemKind::Attribute => "at",
} }
} }
} }

View file

@ -606,6 +606,31 @@ mod tests {
] ]
"### "###
); );
assert_debug_snapshot!(do_reference_completion(
r#"
struct A {
#[deprecated]
the_field: u32,
}
fn foo() {
A { the<|> }
}
"#,
),
@r###"
[
CompletionItem {
label: "the_field",
source_range: 69..72,
delete: 69..72,
insert: "the_field",
kind: Field,
detail: "u32",
deprecated: true,
},
]
"###);
} }
#[test] #[test]

View file

@ -2,6 +2,7 @@
use hir::Semantics; use hir::Semantics;
use ra_syntax::{AstNode, NodeOrToken, SyntaxElement}; use ra_syntax::{AstNode, NodeOrToken, SyntaxElement};
use stdx::format_to;
use crate::{ use crate::{
completion::{completion_item::CompletionKind, CompletionConfig}, completion::{completion_item::CompletionKind, CompletionConfig},
@ -42,7 +43,14 @@ pub(crate) fn completion_list_with_options(
kind_completions.sort_by_key(|c| c.label().to_owned()); kind_completions.sort_by_key(|c| c.label().to_owned());
kind_completions kind_completions
.into_iter() .into_iter()
.map(|it| format!("{} {}\n", it.kind().unwrap().tag(), it.label())) .map(|it| {
let mut buf = format!("{} {}", it.kind().unwrap().tag(), it.label());
if let Some(detail) = it.detail() {
format_to!(buf, " {}", detail);
}
format_to!(buf, "\n");
buf
})
.collect() .collect()
} }