aboutsummaryrefslogtreecommitdiff
path: root/crates
diff options
context:
space:
mode:
Diffstat (limited to 'crates')
-rw-r--r--crates/ra_hir_def/src/find_path.rs38
1 files changed, 25 insertions, 13 deletions
diff --git a/crates/ra_hir_def/src/find_path.rs b/crates/ra_hir_def/src/find_path.rs
index 3df2f2c09..166728153 100644
--- a/crates/ra_hir_def/src/find_path.rs
+++ b/crates/ra_hir_def/src/find_path.rs
@@ -9,17 +9,24 @@ use crate::{
9}; 9};
10use hir_expand::name::Name; 10use hir_expand::name::Name;
11 11
12const MAX_PATH_LEN: usize = 15;
13
12pub fn find_path(db: &impl DefDatabase, item: ItemInNs, from: ModuleId) -> Option<ModPath> { 14pub fn find_path(db: &impl DefDatabase, item: ItemInNs, from: ModuleId) -> Option<ModPath> {
13 find_path_inner(db, item, from, 15) 15 find_path_inner(db, item, from, MAX_PATH_LEN)
14} 16}
15 17
16fn find_path_inner(db: &impl DefDatabase, item: ItemInNs, from: ModuleId, max_len: usize) -> Option<ModPath> { 18fn find_path_inner(
17 // Base cases: 19 db: &impl DefDatabase,
18 20 item: ItemInNs,
21 from: ModuleId,
22 max_len: usize,
23) -> Option<ModPath> {
19 if max_len == 0 { 24 if max_len == 0 {
20 return None; 25 return None;
21 } 26 }
22 27
28 // Base cases:
29
23 // - if the item is already in scope, return the name under which it is 30 // - if the item is already in scope, return the name under which it is
24 let def_map = db.crate_def_map(from.krate); 31 let def_map = db.crate_def_map(from.krate);
25 let from_scope: &crate::item_scope::ItemScope = &def_map.modules[from.local_id].scope; 32 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
86 let mut best_path = None; 93 let mut best_path = None;
87 let mut best_path_len = max_len; 94 let mut best_path_len = max_len;
88 for (module_id, name) in importable_locations { 95 for (module_id, name) in importable_locations {
89 let mut path = match find_path_inner(db, ItemInNs::Types(ModuleDefId::ModuleId(module_id)), from, best_path_len - 1) 96 let mut path = match find_path_inner(
90 { 97 db,
98 ItemInNs::Types(ModuleDefId::ModuleId(module_id)),
99 from,
100 best_path_len - 1,
101 ) {
91 None => continue, 102 None => continue,
92 Some(path) => path, 103 Some(path) => path,
93 }; 104 };
@@ -101,13 +112,14 @@ fn find_path_inner(db: &impl DefDatabase, item: ItemInNs, from: ModuleId, max_le
101} 112}
102 113
103fn path_len(path: &ModPath) -> usize { 114fn path_len(path: &ModPath) -> usize {
104 path.segments.len() + match path.kind { 115 path.segments.len()
105 PathKind::Plain => 0, 116 + match path.kind {
106 PathKind::Super(i) => i as usize, 117 PathKind::Plain => 0,
107 PathKind::Crate => 1, 118 PathKind::Super(i) => i as usize,
108 PathKind::Abs => 0, 119 PathKind::Crate => 1,
109 PathKind::DollarCrate(_) => 1, 120 PathKind::Abs => 0,
110 } 121 PathKind::DollarCrate(_) => 1,
122 }
111} 123}
112 124
113fn find_importable_locations( 125fn find_importable_locations(