From 1ea2b475a99b982829e543616a7dc2694e749e70 Mon Sep 17 00:00:00 2001 From: Florian Diebold Date: Mon, 30 Dec 2019 21:33:25 +0100 Subject: handle most cases --- crates/ra_hir_def/src/find_path.rs | 70 +++++++++++++++++++++++++++++++------ crates/ra_hir_def/src/item_scope.rs | 8 +++++ 2 files changed, 67 insertions(+), 11 deletions(-) (limited to 'crates') diff --git a/crates/ra_hir_def/src/find_path.rs b/crates/ra_hir_def/src/find_path.rs index 6772330e0..afcf04280 100644 --- a/crates/ra_hir_def/src/find_path.rs +++ b/crates/ra_hir_def/src/find_path.rs @@ -8,22 +8,12 @@ use crate::{ }; use hir_expand::name::Name; -// TODO handle prelude -// TODO handle enum variants // TODO don't import from super imports? or at least deprioritize // TODO use super? // TODO use shortest path // TODO performance / memoize pub fn find_path(db: &impl DefDatabase, item: ItemInNs, from: ModuleId) -> Option { - // 1. Find all locations that the item could be imported from (i.e. that are visible) - // - this needs to consider other crates, for reexports from transitive dependencies - // - filter by visibility - // 2. For each of these, go up the module tree until we find an - // item/module/crate that is already in scope (including because it is in - // the prelude, and including aliases!) - // 3. Then select the one that gives the shortest path - // Base cases: // - if the item is already in scope, return the name under which it is @@ -46,10 +36,28 @@ pub fn find_path(db: &impl DefDatabase, item: ItemInNs, from: ModuleId) -> Optio } // - if the item is in the prelude, return the name from there - // TODO check prelude + if let Some(prelude_module) = def_map.prelude { + let prelude_def_map = db.crate_def_map(prelude_module.krate); + let prelude_scope: &crate::item_scope::ItemScope = &prelude_def_map.modules[prelude_module.local_id].scope; + if let Some((name, vis)) = prelude_scope.reverse_get(item) { + if vis.is_visible_from(db, from) { + return Some(ModPath::from_simple_segments(PathKind::Plain, vec![name.clone()])); + } + } + } // Recursive case: // - if the item is an enum variant, refer to it via the enum + if let Some(ModuleDefId::EnumVariantId(variant)) = item.as_module_def_id() { + if let Some(mut path) = find_path(db, ItemInNs::Types(variant.parent.into()), from) { + let data = db.enum_data(variant.parent); + path.segments.push(data.variants[variant.local_id].name.clone()); + return Some(path); + } + // If this doesn't work, it seems we have no way of referring to the + // enum; that's very weird, but there might still be a reexport of the + // variant somewhere + } // - otherwise, look for modules containing (reexporting) it and import it from one of those let importable_locations = find_importable_locations(db, item, from); @@ -131,6 +139,16 @@ mod tests { check_found_path(code, "S"); } + #[test] + fn enum_variant() { + let code = r#" + //- /main.rs + enum E { A } + <|> + "#; + check_found_path(code, "E::A"); + } + #[test] fn sub_module() { let code = r#" @@ -215,6 +233,19 @@ mod tests { check_found_path(code, "bar::U"); } + #[test] + fn different_crate_reexport() { + let code = r#" + //- /main.rs crate:main deps:std + <|> + //- /std.rs crate:std deps:core + pub use core::S; + //- /core.rs crate:core + pub struct S; + "#; + check_found_path(code, "std::S"); + } + #[test] fn prelude() { let code = r#" @@ -227,4 +258,21 @@ mod tests { "#; check_found_path(code, "S"); } + + #[test] + fn enum_variant_from_prelude() { + let code = r#" + //- /main.rs crate:main deps:std + <|> + //- /std.rs crate:std + pub mod prelude { + pub enum Option { Some(T), None } + pub use Option::*; + } + #[prelude_import] + pub use prelude::*; + "#; + check_found_path(code, "None"); + check_found_path(code, "Some"); + } } diff --git a/crates/ra_hir_def/src/item_scope.rs b/crates/ra_hir_def/src/item_scope.rs index f88502d78..71afdb235 100644 --- a/crates/ra_hir_def/src/item_scope.rs +++ b/crates/ra_hir_def/src/item_scope.rs @@ -204,4 +204,12 @@ impl ItemInNs { }, } } + + pub fn as_module_def_id(self) -> Option { + match self { + ItemInNs::Types(t) => Some(t), + ItemInNs::Values(v) => Some(v), + ItemInNs::Macros(_) => None, + } + } } -- cgit v1.2.3