From f9b81369e270db0bde27dc85358148712555bfa8 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 23 Oct 2020 23:41:47 +0200 Subject: Complete variants when only enun name is typed This allows the client to filter `Foo::Bar` when *either* `Foo` or `Bar` is typed. --- crates/completion/src/presentation.rs | 70 +++++++++++++++++++++++++++++++---- 1 file changed, 62 insertions(+), 8 deletions(-) (limited to 'crates/completion/src') diff --git a/crates/completion/src/presentation.rs b/crates/completion/src/presentation.rs index 2a19281cf..0a6f5a1ea 100644 --- a/crates/completion/src/presentation.rs +++ b/crates/completion/src/presentation.rs @@ -304,9 +304,14 @@ impl Completions { ) { let is_deprecated = is_deprecated(variant, ctx.db); let name = local_name.unwrap_or_else(|| variant.name(ctx.db).to_string()); - let qualified_name = match &path { - Some(it) => it.to_string(), - None => name.to_string(), + let (qualified_name, short_qualified_name) = match &path { + Some(path) => { + let full = path.to_string(); + let short = + path.segments[path.segments.len().saturating_sub(2)..].iter().join("::"); + (full, short) + } + None => (name.to_string(), name.to_string()), }; let detail_types = variant .fields(ctx.db) @@ -335,14 +340,12 @@ impl Completions { .set_deprecated(is_deprecated) .detail(detail); - if path.is_some() { - res = res.lookup_by(name); - } - if variant_kind == StructKind::Tuple { mark::hit!(inserts_parens_for_tuple_enums); let params = Params::Anonymous(variant.fields(ctx.db).len()); - res = res.add_call_parens(ctx, qualified_name, params) + res = res.add_call_parens(ctx, short_qualified_name, params) + } else if path.is_some() { + res = res.lookup_by(short_qualified_name); } res.add_to(self); @@ -606,6 +609,57 @@ fn main() { Foo::Fo<|> } ); } + #[test] + fn lookup_enums_by_two_qualifiers() { + check( + r#" +mod m { + pub enum Spam { Foo, Bar(i32) } +} +fn main() { let _: m::Spam = S<|> } +"#, + expect![[r#" + [ + CompletionItem { + label: "Spam::Bar(…)", + source_range: 75..76, + delete: 75..76, + insert: "Spam::Bar($0)", + kind: EnumVariant, + lookup: "Spam::Bar", + detail: "(i32)", + trigger_call_info: true, + }, + CompletionItem { + label: "m", + source_range: 75..76, + delete: 75..76, + insert: "m", + kind: Module, + }, + CompletionItem { + label: "m::Spam::Foo", + source_range: 75..76, + delete: 75..76, + insert: "m::Spam::Foo", + kind: EnumVariant, + lookup: "Spam::Foo", + detail: "()", + }, + CompletionItem { + label: "main()", + source_range: 75..76, + delete: 75..76, + insert: "main()$0", + kind: Function, + lookup: "main", + detail: "fn main()", + }, + ] + "#]], + ) + } + #[test] fn sets_deprecated_flag_in_completion_items() { check( -- cgit v1.2.3