Skip to content

Commit 460fa71

Browse files
committed
Use self
1 parent 2906d18 commit 460fa71

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

crates/ra_hir_def/src/find_path.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ use hir_expand::name::Name;
1111

1212
const MAX_PATH_LEN: usize = 15;
1313

14+
// FIXME: handle local items
15+
16+
/// Find a path that can be used to refer to a certain item. This can depend on
17+
/// *from where* you're referring to the item, hence the `from` parameter.
1418
pub fn find_path(db: &impl DefDatabase, item: ItemInNs, from: ModuleId) -> Option<ModPath> {
1519
find_path_inner(db, item, from, MAX_PATH_LEN)
1620
}
@@ -44,6 +48,11 @@ fn find_path_inner(
4448
return Some(ModPath::from_simple_segments(PathKind::Crate, Vec::new()));
4549
}
4650

51+
// - if the item is the module we're in, use `self`
52+
if item == ItemInNs::Types(from.into()) {
53+
return Some(ModPath::from_simple_segments(PathKind::Super(0), Vec::new()));
54+
}
55+
4756
// - if the item is the parent module, use `super` (this is not used recursively, since `super::super` is ugly)
4857
if let Some(parent_id) = def_map.modules[from.local_id].parent {
4958
if item
@@ -271,6 +280,17 @@ mod tests {
271280
check_found_path(code, "super::S");
272281
}
273282

283+
#[test]
284+
fn self_module() {
285+
let code = r#"
286+
//- /main.rs
287+
mod foo;
288+
//- /foo.rs
289+
<|>
290+
"#;
291+
check_found_path(code, "self");
292+
}
293+
274294
#[test]
275295
fn crate_root() {
276296
let code = r#"

0 commit comments

Comments
 (0)