diff options
author | Florian Diebold <[email protected]> | 2019-12-31 12:01:00 +0000 |
---|---|---|
committer | Florian Diebold <[email protected]> | 2020-01-11 22:33:04 +0000 |
commit | 460fa71c5528d95d34465a4db6853dc8c992b80b (patch) | |
tree | 9cf429e8242f0ea5bf525f88b3d1a48ce01430dc /crates | |
parent | 2906d188c2442d67b3d3b1bf532553e5adc9020a (diff) |
Use `self`
Diffstat (limited to 'crates')
-rw-r--r-- | crates/ra_hir_def/src/find_path.rs | 20 |
1 files changed, 20 insertions, 0 deletions
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; | |||
11 | 11 | ||
12 | const MAX_PATH_LEN: usize = 15; | 12 | const MAX_PATH_LEN: usize = 15; |
13 | 13 | ||
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. | ||
14 | pub fn find_path(db: &impl DefDatabase, item: ItemInNs, from: ModuleId) -> Option<ModPath> { | 18 | pub fn find_path(db: &impl DefDatabase, item: ItemInNs, from: ModuleId) -> Option<ModPath> { |
15 | find_path_inner(db, item, from, MAX_PATH_LEN) | 19 | find_path_inner(db, item, from, MAX_PATH_LEN) |
16 | } | 20 | } |
@@ -44,6 +48,11 @@ fn find_path_inner( | |||
44 | return Some(ModPath::from_simple_segments(PathKind::Crate, Vec::new())); | 48 | return Some(ModPath::from_simple_segments(PathKind::Crate, Vec::new())); |
45 | } | 49 | } |
46 | 50 | ||
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 | |||
47 | // - if the item is the parent module, use `super` (this is not used recursively, since `super::super` is ugly) | 56 | // - if the item is the parent module, use `super` (this is not used recursively, since `super::super` is ugly) |
48 | if let Some(parent_id) = def_map.modules[from.local_id].parent { | 57 | if let Some(parent_id) = def_map.modules[from.local_id].parent { |
49 | if item | 58 | if item |
@@ -272,6 +281,17 @@ mod tests { | |||
272 | } | 281 | } |
273 | 282 | ||
274 | #[test] | 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 | |||
294 | #[test] | ||
275 | fn crate_root() { | 295 | fn crate_root() { |
276 | let code = r#" | 296 | let code = r#" |
277 | //- /main.rs | 297 | //- /main.rs |