mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-29 05:15:04 +00:00
completion for enum variants
This commit is contained in:
parent
1a860dba38
commit
11122e29b7
4 changed files with 36 additions and 9 deletions
|
@ -1,6 +1,6 @@
|
||||||
use crate::{
|
use crate::{
|
||||||
Cancelable,
|
Cancelable,
|
||||||
completion::{CompletionItem, Completions, CompletionKind, CompletionContext},
|
completion::{CompletionItem, CompletionItemKind, Completions, CompletionKind, CompletionContext},
|
||||||
};
|
};
|
||||||
|
|
||||||
pub(super) fn complete_path(acc: &mut Completions, ctx: &CompletionContext) -> Cancelable<()> {
|
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,
|
Some(it) => it,
|
||||||
None => return Ok(()),
|
None => return Ok(()),
|
||||||
};
|
};
|
||||||
let target_module = match def_id.resolve(ctx.db)? {
|
match def_id.resolve(ctx.db)? {
|
||||||
hir::Def::Module(it) => it,
|
hir::Def::Module(module) => {
|
||||||
_ => return Ok(()),
|
let module_scope = module.scope(ctx.db)?;
|
||||||
};
|
|
||||||
let module_scope = target_module.scope(ctx.db)?;
|
|
||||||
module_scope.entries().for_each(|(name, res)| {
|
module_scope.entries().for_each(|(name, res)| {
|
||||||
CompletionItem::new(CompletionKind::Reference, name.to_string())
|
CompletionItem::new(CompletionKind::Reference, name.to_string())
|
||||||
.from_resolution(ctx.db, res)
|
.from_resolution(ctx.db, res)
|
||||||
.add_to(acc)
|
.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(()),
|
||||||
|
};
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -92,4 +101,16 @@ mod tests {
|
||||||
"Spam",
|
"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,
|
Function,
|
||||||
Struct,
|
Struct,
|
||||||
Enum,
|
Enum,
|
||||||
|
EnumVariant,
|
||||||
Binding,
|
Binding,
|
||||||
Field,
|
Field,
|
||||||
}
|
}
|
||||||
|
|
|
@ -73,6 +73,10 @@ impl Enum {
|
||||||
pub fn name(&self, db: &impl HirDatabase) -> Cancelable<Option<SmolStr>> {
|
pub fn name(&self, db: &impl HirDatabase) -> Cancelable<Option<SmolStr>> {
|
||||||
Ok(db.enum_data(self.def_id)?.name.clone())
|
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)]
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||||
|
|
|
@ -57,6 +57,7 @@ impl Conv for CompletionItemKind {
|
||||||
CompletionItemKind::Function => Function,
|
CompletionItemKind::Function => Function,
|
||||||
CompletionItemKind::Struct => Struct,
|
CompletionItemKind::Struct => Struct,
|
||||||
CompletionItemKind::Enum => Enum,
|
CompletionItemKind::Enum => Enum,
|
||||||
|
CompletionItemKind::EnumVariant => EnumMember,
|
||||||
CompletionItemKind::Binding => Variable,
|
CompletionItemKind::Binding => Variable,
|
||||||
CompletionItemKind::Field => Field,
|
CompletionItemKind::Field => Field,
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue