From 041aea2263aed03431f5e8d110c0423911e2496b Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Wed, 29 Apr 2020 11:26:21 +0200 Subject: Better filtering of qualified enum variants in completion --- .../src/completion/complete_unqualified_path.rs | 12 +++++- crates/ra_ide/src/completion/presentation.rs | 46 ++++++++++++++++++---- 2 files changed, 49 insertions(+), 9 deletions(-) (limited to 'crates/ra_ide/src') diff --git a/crates/ra_ide/src/completion/complete_unqualified_path.rs b/crates/ra_ide/src/completion/complete_unqualified_path.rs index f559f2b97..a6a5568de 100644 --- a/crates/ra_ide/src/completion/complete_unqualified_path.rs +++ b/crates/ra_ide/src/completion/complete_unqualified_path.rs @@ -53,7 +53,7 @@ fn complete_enum_variants(acc: &mut Completions, ctx: &CompletionContext, ty: &T // Variants with trivial paths are already added by the existing completion logic, // so we should avoid adding these twice if path.segments.len() > 1 { - acc.add_enum_variant(ctx, variant, Some(path.to_string())); + acc.add_qualified_enum_variant(ctx, variant, path); } } } @@ -1173,6 +1173,7 @@ mod tests { delete: 248..250, insert: "Foo::Bar", kind: EnumVariant, + lookup: "Bar", detail: "()", }, CompletionItem { @@ -1181,6 +1182,7 @@ mod tests { delete: 248..250, insert: "Foo::Baz", kind: EnumVariant, + lookup: "Baz", detail: "()", }, CompletionItem { @@ -1189,6 +1191,7 @@ mod tests { delete: 248..250, insert: "Foo::Quux", kind: EnumVariant, + lookup: "Quux", detail: "()", }, ] @@ -1231,6 +1234,7 @@ mod tests { delete: 219..221, insert: "Foo::Bar", kind: EnumVariant, + lookup: "Bar", detail: "()", }, CompletionItem { @@ -1239,6 +1243,7 @@ mod tests { delete: 219..221, insert: "Foo::Baz", kind: EnumVariant, + lookup: "Baz", detail: "()", }, CompletionItem { @@ -1247,6 +1252,7 @@ mod tests { delete: 219..221, insert: "Foo::Quux", kind: EnumVariant, + lookup: "Quux", detail: "()", }, ] @@ -1285,6 +1291,7 @@ mod tests { delete: 185..186, insert: "Foo::Bar", kind: EnumVariant, + lookup: "Bar", detail: "()", }, CompletionItem { @@ -1293,6 +1300,7 @@ mod tests { delete: 185..186, insert: "Foo::Baz", kind: EnumVariant, + lookup: "Baz", detail: "()", }, CompletionItem { @@ -1301,6 +1309,7 @@ mod tests { delete: 185..186, insert: "Foo::Quux", kind: EnumVariant, + lookup: "Quux", detail: "()", }, CompletionItem { @@ -1353,6 +1362,7 @@ mod tests { delete: 98..99, insert: "m::E::V", kind: EnumVariant, + lookup: "V", detail: "()", }, ] diff --git a/crates/ra_ide/src/completion/presentation.rs b/crates/ra_ide/src/completion/presentation.rs index 77d354376..2edb130cf 100644 --- a/crates/ra_ide/src/completion/presentation.rs +++ b/crates/ra_ide/src/completion/presentation.rs @@ -1,6 +1,6 @@ //! This modules takes care of rendering various definitions as completion items. -use hir::{Docs, HasAttrs, HasSource, HirDisplay, ScopeDef, StructKind, Type}; +use hir::{Docs, HasAttrs, HasSource, HirDisplay, ModPath, ScopeDef, StructKind, Type}; use ra_syntax::ast::NameOwner; use stdx::SepBy; use test_utils::tested_by; @@ -246,14 +246,37 @@ impl Completions { .add_to(self); } + pub(crate) fn add_qualified_enum_variant( + &mut self, + ctx: &CompletionContext, + variant: hir::EnumVariant, + path: ModPath, + ) { + self.add_enum_variant_impl(ctx, variant, None, Some(path)) + } + pub(crate) fn add_enum_variant( &mut self, ctx: &CompletionContext, variant: hir::EnumVariant, local_name: Option, + ) { + self.add_enum_variant_impl(ctx, variant, local_name, None) + } + + fn add_enum_variant_impl( + &mut self, + ctx: &CompletionContext, + variant: hir::EnumVariant, + local_name: Option, + path: Option, ) { 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 detail_types = variant .fields(ctx.db) .into_iter() @@ -271,16 +294,23 @@ impl Completions { .surround_with("{ ", " }") .to_string(), }; - let mut res = - CompletionItem::new(CompletionKind::Reference, ctx.source_range(), name.clone()) - .kind(CompletionItemKind::EnumVariant) - .set_documentation(variant.docs(ctx.db)) - .set_deprecated(is_deprecated) - .detail(detail); + let mut res = CompletionItem::new( + CompletionKind::Reference, + ctx.source_range(), + qualified_name.clone(), + ) + .kind(CompletionItemKind::EnumVariant) + .set_documentation(variant.docs(ctx.db)) + .set_deprecated(is_deprecated) + .detail(detail); + + if path.is_some() { + res = res.lookup_by(name); + } if variant_kind == StructKind::Tuple { let params = Params::Anonymous(variant.fields(ctx.db).len()); - res = res.add_call_parens(ctx, name, params) + res = res.add_call_parens(ctx, qualified_name, params) } res.add_to(self); -- cgit v1.2.3