mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-30 22:01:37 +00:00
Use self
This commit is contained in:
parent
2906d188c2
commit
460fa71c55
1 changed files with 20 additions and 0 deletions
|
@ -11,6 +11,10 @@ use hir_expand::name::Name;
|
||||||
|
|
||||||
const MAX_PATH_LEN: usize = 15;
|
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<ModPath> {
|
pub fn find_path(db: &impl DefDatabase, item: ItemInNs, from: ModuleId) -> Option<ModPath> {
|
||||||
find_path_inner(db, item, from, MAX_PATH_LEN)
|
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()));
|
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 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 let Some(parent_id) = def_map.modules[from.local_id].parent {
|
||||||
if item
|
if item
|
||||||
|
@ -271,6 +280,17 @@ mod tests {
|
||||||
check_found_path(code, "super::S");
|
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]
|
#[test]
|
||||||
fn crate_root() {
|
fn crate_root() {
|
||||||
let code = r#"
|
let code = r#"
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue