aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--crates/ra_hir/src/resolve.rs38
-rw-r--r--crates/ra_hir/src/source_binder.rs19
2 files changed, 40 insertions, 17 deletions
diff --git a/crates/ra_hir/src/resolve.rs b/crates/ra_hir/src/resolve.rs
index a029513b5..7ff0c43ae 100644
--- a/crates/ra_hir/src/resolve.rs
+++ b/crates/ra_hir/src/resolve.rs
@@ -19,7 +19,7 @@ use crate::{
19 code_model::Crate, 19 code_model::Crate,
20 db::HirDatabase, 20 db::HirDatabase,
21 expr::{ExprScopes, PatId, ScopeId}, 21 expr::{ExprScopes, PatId, ScopeId},
22 DefWithBody, GenericDef, GenericParam, Local, MacroDef, PerNs, ScopeDef, 22 DefWithBody, GenericDef, MacroDef, PerNs,
23}; 23};
24 24
25#[derive(Debug, Clone, Default)] 25#[derive(Debug, Clone, Default)]
@@ -377,6 +377,13 @@ impl Resolver {
377 _ => None, 377 _ => None,
378 }) 378 })
379 } 379 }
380
381 pub(crate) fn body_owner(&self) -> Option<DefWithBodyId> {
382 self.scopes.iter().find_map(|scope| match scope {
383 Scope::ExprScope(it) => Some(it.owner),
384 _ => None,
385 })
386 }
380} 387}
381 388
382impl Resolver { 389impl Resolver {
@@ -420,6 +427,14 @@ impl Resolver {
420 } 427 }
421} 428}
422 429
430pub(crate) enum ScopeDef {
431 PerNs(PerNs),
432 ImplSelfType(ImplId),
433 AdtSelfType(AdtId),
434 GenericParam(u32),
435 Local(PatId),
436}
437
423impl Scope { 438impl Scope {
424 fn process_names(&self, db: &impl DefDatabase2, f: &mut dyn FnMut(Name, ScopeDef)) { 439 fn process_names(&self, db: &impl DefDatabase2, f: &mut dyn FnMut(Name, ScopeDef)) {
425 match self { 440 match self {
@@ -432,30 +447,24 @@ impl Scope {
432 // }), 447 // }),
433 // ); 448 // );
434 m.crate_def_map[m.module_id].scope.entries().for_each(|(name, res)| { 449 m.crate_def_map[m.module_id].scope.entries().for_each(|(name, res)| {
435 f(name.clone(), res.def.into()); 450 f(name.clone(), ScopeDef::PerNs(res.def));
436 }); 451 });
437 m.crate_def_map[m.module_id].scope.legacy_macros().for_each(|(name, macro_)| { 452 m.crate_def_map[m.module_id].scope.legacy_macros().for_each(|(name, macro_)| {
438 f(name.clone(), ScopeDef::MacroDef(macro_.into())); 453 f(name.clone(), ScopeDef::PerNs(PerNs::macros(macro_)));
439 }); 454 });
440 m.crate_def_map.extern_prelude().iter().for_each(|(name, &def)| { 455 m.crate_def_map.extern_prelude().iter().for_each(|(name, &def)| {
441 f(name.clone(), ScopeDef::ModuleDef(def.into())); 456 f(name.clone(), ScopeDef::PerNs(PerNs::types(def.into())));
442 }); 457 });
443 if let Some(prelude) = m.crate_def_map.prelude() { 458 if let Some(prelude) = m.crate_def_map.prelude() {
444 let prelude_def_map = db.crate_def_map(prelude.krate); 459 let prelude_def_map = db.crate_def_map(prelude.krate);
445 prelude_def_map[prelude.module_id].scope.entries().for_each(|(name, res)| { 460 prelude_def_map[prelude.module_id].scope.entries().for_each(|(name, res)| {
446 f(name.clone(), res.def.into()); 461 f(name.clone(), ScopeDef::PerNs(res.def));
447 }); 462 });
448 } 463 }
449 } 464 }
450 Scope::GenericParams { params, def } => { 465 Scope::GenericParams { params, .. } => {
451 for param in params.params.iter() { 466 for param in params.params.iter() {
452 f( 467 f(param.name.clone(), ScopeDef::GenericParam(param.idx))
453 param.name.clone(),
454 ScopeDef::GenericParam(GenericParam {
455 parent: (*def).into(),
456 idx: param.idx,
457 }),
458 )
459 } 468 }
460 } 469 }
461 Scope::ImplBlockScope(i) => { 470 Scope::ImplBlockScope(i) => {
@@ -466,8 +475,7 @@ impl Scope {
466 } 475 }
467 Scope::ExprScope(scope) => { 476 Scope::ExprScope(scope) => {
468 scope.expr_scopes.entries(scope.scope_id).iter().for_each(|e| { 477 scope.expr_scopes.entries(scope.scope_id).iter().for_each(|e| {
469 let local = Local { parent: scope.owner.into(), pat_id: e.pat() }; 478 f(e.name().clone(), ScopeDef::Local(e.pat()));
470 f(e.name().clone(), ScopeDef::Local(local));
471 }); 479 });
472 } 480 }
473 } 481 }
diff --git a/crates/ra_hir/src/source_binder.rs b/crates/ra_hir/src/source_binder.rs
index 467e46d8c..f13d43740 100644
--- a/crates/ra_hir/src/source_binder.rs
+++ b/crates/ra_hir/src/source_binder.rs
@@ -23,7 +23,7 @@ use crate::{
23 db::HirDatabase, 23 db::HirDatabase,
24 expr::{BodySourceMap, ExprScopes, ScopeId}, 24 expr::{BodySourceMap, ExprScopes, ScopeId},
25 ids::LocationCtx, 25 ids::LocationCtx,
26 resolve::{resolver_for_scope, HasResolver, TypeNs, ValueNs}, 26 resolve::{self, resolver_for_scope, HasResolver, TypeNs, ValueNs},
27 ty::method_resolution::{self, implements_trait}, 27 ty::method_resolution::{self, implements_trait},
28 Adt, AssocItem, Const, DefWithBody, Either, Enum, EnumVariant, FromSource, Function, 28 Adt, AssocItem, Const, DefWithBody, Either, Enum, EnumVariant, FromSource, Function,
29 GenericParam, HasBody, HirFileId, Local, MacroDef, Module, Name, Path, Resolver, ScopeDef, 29 GenericParam, HasBody, HirFileId, Local, MacroDef, Module, Name, Path, Resolver, ScopeDef,
@@ -310,7 +310,22 @@ impl SourceAnalyzer {
310 } 310 }
311 311
312 pub fn process_all_names(&self, db: &impl HirDatabase, f: &mut dyn FnMut(Name, ScopeDef)) { 312 pub fn process_all_names(&self, db: &impl HirDatabase, f: &mut dyn FnMut(Name, ScopeDef)) {
313 self.resolver.process_all_names(db, f) 313 self.resolver.process_all_names(db, &mut |name, def| {
314 let def = match def {
315 resolve::ScopeDef::PerNs(it) => it.into(),
316 resolve::ScopeDef::ImplSelfType(it) => ScopeDef::ImplSelfType(it.into()),
317 resolve::ScopeDef::AdtSelfType(it) => ScopeDef::AdtSelfType(it.into()),
318 resolve::ScopeDef::GenericParam(idx) => {
319 let parent = self.resolver.generic_def().unwrap().into();
320 ScopeDef::GenericParam(GenericParam { parent, idx })
321 }
322 resolve::ScopeDef::Local(pat_id) => {
323 let parent = self.resolver.body_owner().unwrap().into();
324 ScopeDef::Local(Local { parent, pat_id })
325 }
326 };
327 f(name, def)
328 })
314 } 329 }
315 330
316 // FIXME: we only use this in `inline_local_variable` assist, ideally, we 331 // FIXME: we only use this in `inline_local_variable` assist, ideally, we