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<()> {
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(())
}
"Spam",
);
}
+
+ #[test]
+ fn completes_enum_variant() {
+ check_reference_completion(
+ "
+ //- /lib.rs
+ enum E { Foo, Bar(i32) }
+ fn foo() { let _ = E::<|> }
+ ",
+ "Foo;Bar",
+ );
+ }
}
pub fn name(&self, db: &impl HirDatabase) -> Cancelable<Option<SmolStr>> {
Ok(db.enum_data(self.def_id)?.name.clone())
}
+
+ pub fn variants(&self, db: &impl HirDatabase) -> Cancelable<Vec<(SmolStr, Arc<VariantData>)>> {
+ Ok(db.enum_data(self.def_id)?.variants.clone())
+ }
}
#[derive(Debug, Clone, PartialEq, Eq)]
CompletionItemKind::Function => Function,
CompletionItemKind::Struct => Struct,
CompletionItemKind::Enum => Enum,
+ CompletionItemKind::EnumVariant => EnumMember,
CompletionItemKind::Binding => Variable,
CompletionItemKind::Field => Field,
}