This commit is contained in:
Florian Diebold 2019-12-30 23:08:40 +01:00
parent 38cd9f0c94
commit 2906d188c2

View file

@ -9,17 +9,24 @@ use crate::{
}; };
use hir_expand::name::Name; use hir_expand::name::Name;
const MAX_PATH_LEN: usize = 15;
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, 15) find_path_inner(db, item, from, MAX_PATH_LEN)
} }
fn find_path_inner(db: &impl DefDatabase, item: ItemInNs, from: ModuleId, max_len: usize) -> Option<ModPath> { fn find_path_inner(
// Base cases: db: &impl DefDatabase,
item: ItemInNs,
from: ModuleId,
max_len: usize,
) -> Option<ModPath> {
if max_len == 0 { if max_len == 0 {
return None; return None;
} }
// Base cases:
// - if the item is already in scope, return the name under which it is // - if the item is already in scope, return the name under which it is
let def_map = db.crate_def_map(from.krate); let def_map = db.crate_def_map(from.krate);
let from_scope: &crate::item_scope::ItemScope = &def_map.modules[from.local_id].scope; let from_scope: &crate::item_scope::ItemScope = &def_map.modules[from.local_id].scope;
@ -86,8 +93,12 @@ fn find_path_inner(db: &impl DefDatabase, item: ItemInNs, from: ModuleId, max_le
let mut best_path = None; let mut best_path = None;
let mut best_path_len = max_len; let mut best_path_len = max_len;
for (module_id, name) in importable_locations { for (module_id, name) in importable_locations {
let mut path = match find_path_inner(db, ItemInNs::Types(ModuleDefId::ModuleId(module_id)), from, best_path_len - 1) let mut path = match find_path_inner(
{ db,
ItemInNs::Types(ModuleDefId::ModuleId(module_id)),
from,
best_path_len - 1,
) {
None => continue, None => continue,
Some(path) => path, Some(path) => path,
}; };
@ -101,13 +112,14 @@ fn find_path_inner(db: &impl DefDatabase, item: ItemInNs, from: ModuleId, max_le
} }
fn path_len(path: &ModPath) -> usize { fn path_len(path: &ModPath) -> usize {
path.segments.len() + match path.kind { path.segments.len()
PathKind::Plain => 0, + match path.kind {
PathKind::Super(i) => i as usize, PathKind::Plain => 0,
PathKind::Crate => 1, PathKind::Super(i) => i as usize,
PathKind::Abs => 0, PathKind::Crate => 1,
PathKind::DollarCrate(_) => 1, PathKind::Abs => 0,
} PathKind::DollarCrate(_) => 1,
}
} }
fn find_importable_locations( fn find_importable_locations(