4203: Better filtering of qualified enum variants in completion r=matklad a=matklad



bors r+
🤖

Co-authored-by: Aleksey Kladov <aleksey.kladov@gmail.com>
This commit is contained in:
bors[bot] 2020-04-29 09:31:22 +00:00 committed by GitHub
commit b1408271a3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 49 additions and 9 deletions

View File

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

View File

@ -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<String>,
) {
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<String>,
path: Option<ModPath>,
) {
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);