aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir/src/module/nameres.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_hir/src/module/nameres.rs')
-rw-r--r--crates/ra_hir/src/module/nameres.rs81
1 files changed, 37 insertions, 44 deletions
diff --git a/crates/ra_hir/src/module/nameres.rs b/crates/ra_hir/src/module/nameres.rs
index 5e4bb7669..9afeade9e 100644
--- a/crates/ra_hir/src/module/nameres.rs
+++ b/crates/ra_hir/src/module/nameres.rs
@@ -228,7 +228,7 @@ where
228 228
229 pub(crate) fn resolve(mut self) -> Cancelable<ItemMap> { 229 pub(crate) fn resolve(mut self) -> Cancelable<ItemMap> {
230 for (&module_id, items) in self.input.iter() { 230 for (&module_id, items) in self.input.iter() {
231 self.populate_module(module_id, items) 231 self.populate_module(module_id, items)?;
232 } 232 }
233 233
234 for &module_id in self.input.keys() { 234 for &module_id in self.input.keys() {
@@ -238,11 +238,25 @@ where
238 Ok(self.result) 238 Ok(self.result)
239 } 239 }
240 240
241 fn populate_module(&mut self, module_id: ModuleId, input: &InputModuleItems) { 241 fn populate_module(&mut self, module_id: ModuleId, input: &InputModuleItems) -> Cancelable<()> {
242 let file_id = module_id.source(&self.module_tree).file_id(); 242 let file_id = module_id.source(&self.module_tree).file_id();
243 243
244 let mut module_items = ModuleScope::default(); 244 let mut module_items = ModuleScope::default();
245 245
246 // Populate extern crates prelude
247 {
248 let root_id = module_id.crate_root(&self.module_tree);
249 let file_id = root_id.source(&self.module_tree).file_id();
250 let crate_graph = self.db.crate_graph();
251 if let Some(crate_id) = crate_graph.crate_id_for_crate_root(file_id) {
252 let krate = Crate::new(crate_id);
253 for dep in krate.dependencies(self.db) {
254 if let Some(module) = dep.krate.root_module(self.db)? {
255 self.add_module_item(&mut module_items, dep.name, module.module_id);
256 }
257 }
258 };
259 }
246 for import in input.imports.iter() { 260 for import in input.imports.iter() {
247 if let Some(name) = import.path.segments.iter().last() { 261 if let Some(name) = import.path.segments.iter().last() {
248 if let ImportKind::Named(import) = import.kind { 262 if let ImportKind::Named(import) = import.kind {
@@ -256,10 +270,9 @@ where
256 } 270 }
257 } 271 }
258 } 272 }
259 273 // Populate explicitelly declared items, except modules
260 for item in input.items.iter() { 274 for item in input.items.iter() {
261 if item.kind == MODULE { 275 if item.kind == MODULE {
262 // handle submodules separatelly
263 continue; 276 continue;
264 } 277 }
265 let def_loc = DefLoc { 278 let def_loc = DefLoc {
@@ -279,22 +292,28 @@ where
279 module_items.items.insert(item.name.clone(), resolution); 292 module_items.items.insert(item.name.clone(), resolution);
280 } 293 }
281 294
295 // Populate modules
282 for (name, module_id) in module_id.children(&self.module_tree) { 296 for (name, module_id) in module_id.children(&self.module_tree) {
283 let def_loc = DefLoc { 297 self.add_module_item(&mut module_items, name, module_id);
284 kind: DefKind::Module,
285 source_root_id: self.source_root,
286 module_id,
287 source_item_id: module_id.source(&self.module_tree).0,
288 };
289 let def_id = def_loc.id(self.db);
290 let resolution = Resolution {
291 def_id: Some(def_id),
292 import: None,
293 };
294 module_items.items.insert(name, resolution);
295 } 298 }
296 299
297 self.result.per_module.insert(module_id, module_items); 300 self.result.per_module.insert(module_id, module_items);
301 Ok(())
302 }
303
304 fn add_module_item(&self, module_items: &mut ModuleScope, name: SmolStr, module_id: ModuleId) {
305 let def_loc = DefLoc {
306 kind: DefKind::Module,
307 source_root_id: self.source_root,
308 module_id,
309 source_item_id: module_id.source(&self.module_tree).0,
310 };
311 let def_id = def_loc.id(self.db);
312 let resolution = Resolution {
313 def_id: Some(def_id),
314 import: None,
315 };
316 module_items.items.insert(name, resolution);
298 } 317 }
299 318
300 fn resolve_imports(&mut self, module_id: ModuleId) -> Cancelable<()> { 319 fn resolve_imports(&mut self, module_id: ModuleId) -> Cancelable<()> {
@@ -309,35 +328,9 @@ where
309 ImportKind::Glob => return Ok(()), 328 ImportKind::Glob => return Ok(()),
310 ImportKind::Named(ptr) => ptr, 329 ImportKind::Named(ptr) => ptr,
311 }; 330 };
312 let mut segments = import.path.segments.iter().enumerate();
313 331
314 let mut curr = match import.path.kind { 332 let mut curr = match import.path.kind {
315 PathKind::Plain => { 333 PathKind::Plain | PathKind::Self_ => module_id,
316 let root_id = module_id.crate_root(&self.module_tree);
317 let file_id = root_id.source(&self.module_tree).file_id();
318 let crate_graph = self.db.crate_graph();
319 let crate_id = match crate_graph.crate_id_for_crate_root(file_id) {
320 None => return Ok(()),
321 Some(it) => it,
322 };
323 let krate = Crate::new(crate_id);
324 let crate_name = match segments.next() {
325 None => return Ok(()),
326 Some((_, it)) => it,
327 };
328 match krate
329 .dependencies(self.db)
330 .into_iter()
331 .find(|it| &it.name == crate_name)
332 {
333 None => return Ok(()),
334 Some(dep) => match dep.krate.root_module(self.db)? {
335 None => return Ok(()),
336 Some(it) => it.module_id,
337 },
338 }
339 }
340 PathKind::Self_ => module_id,
341 PathKind::Super => { 334 PathKind::Super => {
342 match module_id.parent(&self.module_tree) { 335 match module_id.parent(&self.module_tree) {
343 Some(it) => it, 336 Some(it) => it,
@@ -348,7 +341,7 @@ where
348 PathKind::Crate => module_id.crate_root(&self.module_tree), 341 PathKind::Crate => module_id.crate_root(&self.module_tree),
349 }; 342 };
350 343
351 for (i, name) in segments { 344 for (i, name) in import.path.segments.iter().enumerate() {
352 let is_last = i == import.path.segments.len() - 1; 345 let is_last = i == import.path.segments.len() - 1;
353 346
354 let def_id = match self.result.per_module[&curr].items.get(name) { 347 let def_id = match self.result.per_module[&curr].items.get(name) {