mirror of
https://github.com/rust-lang/rust.git
synced 2025-05-14 02:49:40 +00:00
completion for enum variants
This commit is contained in:
parent
1a860dba38
commit
11122e29b7
@ -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",
|
||||
);
|
||||
}
|
||||
}
|
||||
|
@ -29,6 +29,7 @@ pub enum CompletionItemKind {
|
||||
Function,
|
||||
Struct,
|
||||
Enum,
|
||||
EnumVariant,
|
||||
Binding,
|
||||
Field,
|
||||
}
|
||||
|
@ -73,6 +73,10 @@ impl Enum {
|
||||
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)]
|
||||
|
@ -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,
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user