Better filtering of qualified enum variants in completion

This commit is contained in:
Aleksey Kladov 2020-04-29 11:26:21 +02:00
parent 6046804cec
commit 041aea2263
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, // Variants with trivial paths are already added by the existing completion logic,
// so we should avoid adding these twice // so we should avoid adding these twice
if path.segments.len() > 1 { 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, delete: 248..250,
insert: "Foo::Bar", insert: "Foo::Bar",
kind: EnumVariant, kind: EnumVariant,
lookup: "Bar",
detail: "()", detail: "()",
}, },
CompletionItem { CompletionItem {
@ -1181,6 +1182,7 @@ mod tests {
delete: 248..250, delete: 248..250,
insert: "Foo::Baz", insert: "Foo::Baz",
kind: EnumVariant, kind: EnumVariant,
lookup: "Baz",
detail: "()", detail: "()",
}, },
CompletionItem { CompletionItem {
@ -1189,6 +1191,7 @@ mod tests {
delete: 248..250, delete: 248..250,
insert: "Foo::Quux", insert: "Foo::Quux",
kind: EnumVariant, kind: EnumVariant,
lookup: "Quux",
detail: "()", detail: "()",
}, },
] ]
@ -1231,6 +1234,7 @@ mod tests {
delete: 219..221, delete: 219..221,
insert: "Foo::Bar", insert: "Foo::Bar",
kind: EnumVariant, kind: EnumVariant,
lookup: "Bar",
detail: "()", detail: "()",
}, },
CompletionItem { CompletionItem {
@ -1239,6 +1243,7 @@ mod tests {
delete: 219..221, delete: 219..221,
insert: "Foo::Baz", insert: "Foo::Baz",
kind: EnumVariant, kind: EnumVariant,
lookup: "Baz",
detail: "()", detail: "()",
}, },
CompletionItem { CompletionItem {
@ -1247,6 +1252,7 @@ mod tests {
delete: 219..221, delete: 219..221,
insert: "Foo::Quux", insert: "Foo::Quux",
kind: EnumVariant, kind: EnumVariant,
lookup: "Quux",
detail: "()", detail: "()",
}, },
] ]
@ -1285,6 +1291,7 @@ mod tests {
delete: 185..186, delete: 185..186,
insert: "Foo::Bar", insert: "Foo::Bar",
kind: EnumVariant, kind: EnumVariant,
lookup: "Bar",
detail: "()", detail: "()",
}, },
CompletionItem { CompletionItem {
@ -1293,6 +1300,7 @@ mod tests {
delete: 185..186, delete: 185..186,
insert: "Foo::Baz", insert: "Foo::Baz",
kind: EnumVariant, kind: EnumVariant,
lookup: "Baz",
detail: "()", detail: "()",
}, },
CompletionItem { CompletionItem {
@ -1301,6 +1309,7 @@ mod tests {
delete: 185..186, delete: 185..186,
insert: "Foo::Quux", insert: "Foo::Quux",
kind: EnumVariant, kind: EnumVariant,
lookup: "Quux",
detail: "()", detail: "()",
}, },
CompletionItem { CompletionItem {
@ -1353,6 +1362,7 @@ mod tests {
delete: 98..99, delete: 98..99,
insert: "m::E::V", insert: "m::E::V",
kind: EnumVariant, kind: EnumVariant,
lookup: "V",
detail: "()", detail: "()",
}, },
] ]

View file

@ -1,6 +1,6 @@
//! This modules takes care of rendering various definitions as completion items. //! 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 ra_syntax::ast::NameOwner;
use stdx::SepBy; use stdx::SepBy;
use test_utils::tested_by; use test_utils::tested_by;
@ -246,14 +246,37 @@ impl Completions {
.add_to(self); .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( pub(crate) fn add_enum_variant(
&mut self, &mut self,
ctx: &CompletionContext, ctx: &CompletionContext,
variant: hir::EnumVariant, variant: hir::EnumVariant,
local_name: Option<String>, 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 is_deprecated = is_deprecated(variant, ctx.db);
let name = local_name.unwrap_or_else(|| variant.name(ctx.db).to_string()); 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 let detail_types = variant
.fields(ctx.db) .fields(ctx.db)
.into_iter() .into_iter()
@ -271,16 +294,23 @@ impl Completions {
.surround_with("{ ", " }") .surround_with("{ ", " }")
.to_string(), .to_string(),
}; };
let mut res = let mut res = CompletionItem::new(
CompletionItem::new(CompletionKind::Reference, ctx.source_range(), name.clone()) CompletionKind::Reference,
.kind(CompletionItemKind::EnumVariant) ctx.source_range(),
.set_documentation(variant.docs(ctx.db)) qualified_name.clone(),
.set_deprecated(is_deprecated) )
.detail(detail); .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 { if variant_kind == StructKind::Tuple {
let params = Params::Anonymous(variant.fields(ctx.db).len()); 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); res.add_to(self);