Skip to content

Commit 1ea2b47

Browse files
committed
handle most cases
1 parent b62292e commit 1ea2b47

File tree

2 files changed

+67
-11
lines changed

2 files changed

+67
-11
lines changed

crates/ra_hir_def/src/find_path.rs

Lines changed: 59 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,12 @@ use crate::{
88
};
99
use hir_expand::name::Name;
1010

11-
// TODO handle prelude
12-
// TODO handle enum variants
1311
// TODO don't import from super imports? or at least deprioritize
1412
// TODO use super?
1513
// TODO use shortest path
1614
// TODO performance / memoize
1715

1816
pub fn find_path(db: &impl DefDatabase, item: ItemInNs, from: ModuleId) -> Option<ModPath> {
19-
// 1. Find all locations that the item could be imported from (i.e. that are visible)
20-
// - this needs to consider other crates, for reexports from transitive dependencies
21-
// - filter by visibility
22-
// 2. For each of these, go up the module tree until we find an
23-
// item/module/crate that is already in scope (including because it is in
24-
// the prelude, and including aliases!)
25-
// 3. Then select the one that gives the shortest path
26-
2717
// Base cases:
2818

2919
// - 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
4636
}
4737

4838
// - if the item is in the prelude, return the name from there
49-
// TODO check prelude
39+
if let Some(prelude_module) = def_map.prelude {
40+
let prelude_def_map = db.crate_def_map(prelude_module.krate);
41+
let prelude_scope: &crate::item_scope::ItemScope = &prelude_def_map.modules[prelude_module.local_id].scope;
42+
if let Some((name, vis)) = prelude_scope.reverse_get(item) {
43+
if vis.is_visible_from(db, from) {
44+
return Some(ModPath::from_simple_segments(PathKind::Plain, vec![name.clone()]));
45+
}
46+
}
47+
}
5048

5149
// Recursive case:
5250
// - if the item is an enum variant, refer to it via the enum
51+
if let Some(ModuleDefId::EnumVariantId(variant)) = item.as_module_def_id() {
52+
if let Some(mut path) = find_path(db, ItemInNs::Types(variant.parent.into()), from) {
53+
let data = db.enum_data(variant.parent);
54+
path.segments.push(data.variants[variant.local_id].name.clone());
55+
return Some(path);
56+
}
57+
// If this doesn't work, it seems we have no way of referring to the
58+
// enum; that's very weird, but there might still be a reexport of the
59+
// variant somewhere
60+
}
5361

5462
// - otherwise, look for modules containing (reexporting) it and import it from one of those
5563
let importable_locations = find_importable_locations(db, item, from);
@@ -131,6 +139,16 @@ mod tests {
131139
check_found_path(code, "S");
132140
}
133141

142+
#[test]
143+
fn enum_variant() {
144+
let code = r#"
145+
//- /main.rs
146+
enum E { A }
147+
<|>
148+
"#;
149+
check_found_path(code, "E::A");
150+
}
151+
134152
#[test]
135153
fn sub_module() {
136154
let code = r#"
@@ -215,6 +233,19 @@ mod tests {
215233
check_found_path(code, "bar::U");
216234
}
217235

236+
#[test]
237+
fn different_crate_reexport() {
238+
let code = r#"
239+
//- /main.rs crate:main deps:std
240+
<|>
241+
//- /std.rs crate:std deps:core
242+
pub use core::S;
243+
//- /core.rs crate:core
244+
pub struct S;
245+
"#;
246+
check_found_path(code, "std::S");
247+
}
248+
218249
#[test]
219250
fn prelude() {
220251
let code = r#"
@@ -227,4 +258,21 @@ mod tests {
227258
"#;
228259
check_found_path(code, "S");
229260
}
261+
262+
#[test]
263+
fn enum_variant_from_prelude() {
264+
let code = r#"
265+
//- /main.rs crate:main deps:std
266+
<|>
267+
//- /std.rs crate:std
268+
pub mod prelude {
269+
pub enum Option<T> { Some(T), None }
270+
pub use Option::*;
271+
}
272+
#[prelude_import]
273+
pub use prelude::*;
274+
"#;
275+
check_found_path(code, "None");
276+
check_found_path(code, "Some");
277+
}
230278
}

crates/ra_hir_def/src/item_scope.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,4 +204,12 @@ impl ItemInNs {
204204
},
205205
}
206206
}
207+
208+
pub fn as_module_def_id(self) -> Option<ModuleDefId> {
209+
match self {
210+
ItemInNs::Types(t) => Some(t),
211+
ItemInNs::Values(v) => Some(v),
212+
ItemInNs::Macros(_) => None,
213+
}
214+
}
207215
}

0 commit comments

Comments
 (0)