From 460fa71c5528d95d34465a4db6853dc8c992b80b Mon Sep 17 00:00:00 2001 From: Florian Diebold Date: Tue, 31 Dec 2019 13:01:00 +0100 Subject: Use `self` --- crates/ra_hir_def/src/find_path.rs | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) (limited to 'crates') diff --git a/crates/ra_hir_def/src/find_path.rs b/crates/ra_hir_def/src/find_path.rs index 166728153..69cdfc943 100644 --- a/crates/ra_hir_def/src/find_path.rs +++ b/crates/ra_hir_def/src/find_path.rs @@ -11,6 +11,10 @@ use hir_expand::name::Name; const MAX_PATH_LEN: usize = 15; +// FIXME: handle local items + +/// Find a path that can be used to refer to a certain item. This can depend on +/// *from where* you're referring to the item, hence the `from` parameter. pub fn find_path(db: &impl DefDatabase, item: ItemInNs, from: ModuleId) -> Option { find_path_inner(db, item, from, MAX_PATH_LEN) } @@ -44,6 +48,11 @@ fn find_path_inner( return Some(ModPath::from_simple_segments(PathKind::Crate, Vec::new())); } + // - if the item is the module we're in, use `self` + if item == ItemInNs::Types(from.into()) { + return Some(ModPath::from_simple_segments(PathKind::Super(0), Vec::new())); + } + // - if the item is the parent module, use `super` (this is not used recursively, since `super::super` is ugly) if let Some(parent_id) = def_map.modules[from.local_id].parent { if item @@ -271,6 +280,17 @@ mod tests { check_found_path(code, "super::S"); } + #[test] + fn self_module() { + let code = r#" + //- /main.rs + mod foo; + //- /foo.rs + <|> + "#; + check_found_path(code, "self"); + } + #[test] fn crate_root() { let code = r#" -- cgit v1.2.3