From 11122e29b7ec5bc2e08822deaa6fdf9a1cc8ffca Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 28 Dec 2018 21:06:08 +0300 Subject: completion for enum variants --- crates/ra_analysis/src/completion/complete_path.rs | 39 +++++++++++++++++----- .../ra_analysis/src/completion/completion_item.rs | 1 + crates/ra_hir/src/adt.rs | 4 +++ crates/ra_lsp_server/src/conv.rs | 1 + 4 files changed, 36 insertions(+), 9 deletions(-) (limited to 'crates') diff --git a/crates/ra_analysis/src/completion/complete_path.rs b/crates/ra_analysis/src/completion/complete_path.rs index aaa2c7cee..c73a083a4 100644 --- a/crates/ra_analysis/src/completion/complete_path.rs +++ b/crates/ra_analysis/src/completion/complete_path.rs @@ -1,6 +1,6 @@ use crate::{ Cancelable, - completion::{CompletionItem, Completions, CompletionKind, CompletionContext}, + completion::{CompletionItem, CompletionItemKind, Completions, CompletionKind, CompletionContext}, }; pub(super) fn complete_path(acc: &mut Completions, ctx: &CompletionContext) -> Cancelable<()> { @@ -12,16 +12,25 @@ pub(super) fn complete_path(acc: &mut Completions, ctx: &CompletionContext) -> C Some(it) => it, None => return Ok(()), }; - let target_module = match def_id.resolve(ctx.db)? { - hir::Def::Module(it) => it, + match def_id.resolve(ctx.db)? { + hir::Def::Module(module) => { + let module_scope = module.scope(ctx.db)?; + module_scope.entries().for_each(|(name, res)| { + CompletionItem::new(CompletionKind::Reference, name.to_string()) + .from_resolution(ctx.db, res) + .add_to(acc) + }); + } + hir::Def::Enum(e) => e + .variants(ctx.db)? + .into_iter() + .for_each(|(name, _variant)| { + CompletionItem::new(CompletionKind::Reference, name.to_string()) + .kind(CompletionItemKind::EnumVariant) + .add_to(acc) + }), _ => return Ok(()), }; - let module_scope = target_module.scope(ctx.db)?; - module_scope.entries().for_each(|(name, res)| { - CompletionItem::new(CompletionKind::Reference, name.to_string()) - .from_resolution(ctx.db, res) - .add_to(acc) - }); Ok(()) } @@ -92,4 +101,16 @@ mod tests { "Spam", ); } + + #[test] + fn completes_enum_variant() { + check_reference_completion( + " + //- /lib.rs + enum E { Foo, Bar(i32) } + fn foo() { let _ = E::<|> } + ", + "Foo;Bar", + ); + } } diff --git a/crates/ra_analysis/src/completion/completion_item.rs b/crates/ra_analysis/src/completion/completion_item.rs index c9f9f495d..1d294c553 100644 --- a/crates/ra_analysis/src/completion/completion_item.rs +++ b/crates/ra_analysis/src/completion/completion_item.rs @@ -29,6 +29,7 @@ pub enum CompletionItemKind { Function, Struct, Enum, + EnumVariant, Binding, Field, } diff --git a/crates/ra_hir/src/adt.rs b/crates/ra_hir/src/adt.rs index 65c461148..6cdaa1888 100644 --- a/crates/ra_hir/src/adt.rs +++ b/crates/ra_hir/src/adt.rs @@ -73,6 +73,10 @@ impl Enum { pub fn name(&self, db: &impl HirDatabase) -> Cancelable> { Ok(db.enum_data(self.def_id)?.name.clone()) } + + pub fn variants(&self, db: &impl HirDatabase) -> Cancelable)>> { + Ok(db.enum_data(self.def_id)?.variants.clone()) + } } #[derive(Debug, Clone, PartialEq, Eq)] diff --git a/crates/ra_lsp_server/src/conv.rs b/crates/ra_lsp_server/src/conv.rs index 3d56ccd97..0d6e62727 100644 --- a/crates/ra_lsp_server/src/conv.rs +++ b/crates/ra_lsp_server/src/conv.rs @@ -57,6 +57,7 @@ impl Conv for CompletionItemKind { CompletionItemKind::Function => Function, CompletionItemKind::Struct => Struct, CompletionItemKind::Enum => Enum, + CompletionItemKind::EnumVariant => EnumMember, CompletionItemKind::Binding => Variable, CompletionItemKind::Field => Field, } -- cgit v1.2.3