From c1e295682ec23b7f9923cda07fc28d99c987f327 Mon Sep 17 00:00:00 2001 From: Florian Diebold Date: Sun, 10 Feb 2019 15:34:04 +0100 Subject: Implement glob imports from enums --- crates/ra_hir/src/code_model_api.rs | 4 ++-- crates/ra_hir/src/nameres.rs | 39 +++++++++++++++++++++++++++++++++---- 2 files changed, 37 insertions(+), 6 deletions(-) (limited to 'crates') diff --git a/crates/ra_hir/src/code_model_api.rs b/crates/ra_hir/src/code_model_api.rs index 19f103855..94a08aa63 100644 --- a/crates/ra_hir/src/code_model_api.rs +++ b/crates/ra_hir/src/code_model_api.rs @@ -327,7 +327,7 @@ impl Enum { db.enum_data(*self).name.clone() } - pub fn variants(&self, db: &impl HirDatabase) -> Vec { + pub fn variants(&self, db: &impl PersistentHirDatabase) -> Vec { db.enum_data(*self) .variants .iter() @@ -389,7 +389,7 @@ impl EnumVariant { self.parent } - pub fn name(&self, db: &impl HirDatabase) -> Option { + pub fn name(&self, db: &impl PersistentHirDatabase) -> Option { db.enum_data(self.parent).variants[self.id].name.clone() } diff --git a/crates/ra_hir/src/nameres.rs b/crates/ra_hir/src/nameres.rs index b7382d9c3..0b6ab1211 100644 --- a/crates/ra_hir/src/nameres.rs +++ b/crates/ra_hir/src/nameres.rs @@ -264,14 +264,45 @@ where import: &ImportData, ) -> ReachedFixedPoint { log::debug!("resolving import: {:?}", import); - if import.is_glob { - return ReachedFixedPoint::Yes; - }; let original_module = Module { krate: self.krate, module_id }; let (def, reached_fixedpoint) = self.result.resolve_path_fp(self.db, original_module, &import.path); - if reached_fixedpoint == ReachedFixedPoint::Yes { + if reached_fixedpoint != ReachedFixedPoint::Yes { + return reached_fixedpoint; + } + + if import.is_glob { + log::debug!("glob import: {:?}", import); + match def.take_types() { + Some(ModuleDef::Module(m)) => { + // TODO + } + Some(ModuleDef::Enum(e)) => { + tested_by!(glob_enum); + let variants = e.variants(self.db); + let resolutions = variants.into_iter() + .filter_map(|variant| { + let res = Resolution { + def: PerNs::both(variant.into(), e.into()), + import: Some(import_id), + }; + let name = variant.name(self.db)?; + Some((name, res)) + }) + .collect::>(); + self.update(module_id, |items| { + items.items.extend(resolutions); + }); + } + Some(d) => { + log::debug!("glob import {:?} from non-module/enum {:?}", import, d); + } + None => { + log::debug!("glob import {:?} didn't resolve as type", import); + } + } + } else { let last_segment = import.path.segments.last().unwrap(); let name = import.alias.clone().unwrap_or_else(|| last_segment.name.clone()); log::debug!("resolved import {:?} ({:?}) to {:?}", name, import, def); -- cgit v1.2.3