From a71bb70f0a933ca5e78ca02a205fd4cb94ece48e Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Tue, 14 Jan 2020 16:55:35 +0100 Subject: Store DB in SourceBinder --- crates/ra_hir/src/lib.rs | 1 + crates/ra_hir/src/source_analyzer.rs | 2 +- crates/ra_hir/src/source_binder.rs | 62 ++++++++++++++++++------------------ 3 files changed, 33 insertions(+), 32 deletions(-) diff --git a/crates/ra_hir/src/lib.rs b/crates/ra_hir/src/lib.rs index a953eabc7..a2350573c 100644 --- a/crates/ra_hir/src/lib.rs +++ b/crates/ra_hir/src/lib.rs @@ -48,6 +48,7 @@ pub use crate::{ from_source::FromSource, has_source::HasSource, source_analyzer::{PathResolution, ScopeEntryWithSyntax, SourceAnalyzer}, + source_binder::SourceBinder, }; pub use hir_def::{ diff --git a/crates/ra_hir/src/source_analyzer.rs b/crates/ra_hir/src/source_analyzer.rs index 90bc93999..186dd2411 100644 --- a/crates/ra_hir/src/source_analyzer.rs +++ b/crates/ra_hir/src/source_analyzer.rs @@ -105,7 +105,7 @@ impl SourceAnalyzer { node: InFile<&SyntaxNode>, offset: Option, ) -> SourceAnalyzer { - crate::source_binder::SourceBinder::default().analyze(db, node, offset) + crate::source_binder::SourceBinder::new(db).analyze(node, offset) } pub(crate) fn new_for_body( diff --git a/crates/ra_hir/src/source_binder.rs b/crates/ra_hir/src/source_binder.rs index cec3f8c2c..8f002d2ee 100644 --- a/crates/ra_hir/src/source_binder.rs +++ b/crates/ra_hir/src/source_binder.rs @@ -18,48 +18,52 @@ use rustc_hash::FxHashMap; use crate::{db::HirDatabase, ModuleSource, SourceAnalyzer}; -#[derive(Default)] -pub struct SourceBinder { +pub struct SourceBinder<'a, DB> { + pub db: &'a DB, child_by_source_cache: FxHashMap, } -impl SourceBinder { +impl SourceBinder<'_, DB> { + pub fn new(db: &DB) -> SourceBinder { + SourceBinder { db, child_by_source_cache: FxHashMap::default() } + } + pub fn analyze( &mut self, - db: &impl HirDatabase, src: InFile<&SyntaxNode>, offset: Option, ) -> SourceAnalyzer { let _p = profile("SourceBinder::analyzer"); - let container = match self.find_container(db, src) { + let container = match self.find_container(src) { Some(it) => it, None => return SourceAnalyzer::new_for_resolver(Resolver::default(), src), }; let resolver = match container { ChildContainer::DefWithBodyId(def) => { - return SourceAnalyzer::new_for_body(db, def, src, offset) + return SourceAnalyzer::new_for_body(self.db, def, src, offset) } - ChildContainer::TraitId(it) => it.resolver(db), - ChildContainer::ImplId(it) => it.resolver(db), - ChildContainer::ModuleId(it) => it.resolver(db), - ChildContainer::EnumId(it) => it.resolver(db), - ChildContainer::VariantId(it) => it.resolver(db), + ChildContainer::TraitId(it) => it.resolver(self.db), + ChildContainer::ImplId(it) => it.resolver(self.db), + ChildContainer::ModuleId(it) => it.resolver(self.db), + ChildContainer::EnumId(it) => it.resolver(self.db), + ChildContainer::VariantId(it) => it.resolver(self.db), }; SourceAnalyzer::new_for_resolver(resolver, src) } - pub fn to_def(&mut self, db: &impl HirDatabase, src: InFile) -> Option + pub fn to_def(&mut self, src: InFile) -> Option where D: From, ID: ToId, { - let id: ID = self.to_id(db, src)?; + let id: ID = self.to_id(src)?; Some(id.into()) } - fn to_id(&mut self, db: &impl HirDatabase, src: InFile) -> Option { - let container = self.find_container(db, src.as_ref().map(|it| it.syntax()))?; + fn to_id(&mut self, src: InFile) -> Option { + let container = self.find_container(src.as_ref().map(|it| it.syntax()))?; + let db = self.db; let dyn_map = &*self.child_by_source_cache.entry(container).or_insert_with(|| match container { ChildContainer::DefWithBodyId(it) => it.child_by_source(db), @@ -72,44 +76,40 @@ impl SourceBinder { dyn_map[D::KEY].get(&src).copied() } - fn find_container( - &mut self, - db: &impl HirDatabase, - src: InFile<&SyntaxNode>, - ) -> Option { - for container in src.cloned().ancestors_with_macros(db).skip(1) { + fn find_container(&mut self, src: InFile<&SyntaxNode>) -> Option { + for container in src.cloned().ancestors_with_macros(self.db).skip(1) { let res: ChildContainer = match_ast! { match (container.value) { ast::TraitDef(it) => { - let def: TraitId = self.to_id(db, container.with_value(it))?; + let def: TraitId = self.to_id(container.with_value(it))?; def.into() }, ast::ImplBlock(it) => { - let def: ImplId = self.to_id(db, container.with_value(it))?; + let def: ImplId = self.to_id(container.with_value(it))?; def.into() }, ast::FnDef(it) => { - let def: FunctionId = self.to_id(db, container.with_value(it))?; + let def: FunctionId = self.to_id(container.with_value(it))?; DefWithBodyId::from(def).into() }, ast::StaticDef(it) => { - let def: StaticId = self.to_id(db, container.with_value(it))?; + let def: StaticId = self.to_id(container.with_value(it))?; DefWithBodyId::from(def).into() }, ast::ConstDef(it) => { - let def: ConstId = self.to_id(db, container.with_value(it))?; + let def: ConstId = self.to_id(container.with_value(it))?; DefWithBodyId::from(def).into() }, ast::EnumDef(it) => { - let def: EnumId = self.to_id(db, container.with_value(it))?; + let def: EnumId = self.to_id(container.with_value(it))?; def.into() }, ast::StructDef(it) => { - let def: StructId = self.to_id(db, container.with_value(it))?; + let def: StructId = self.to_id(container.with_value(it))?; VariantId::from(def).into() }, ast::UnionDef(it) => { - let def: UnionId = self.to_id(db, container.with_value(it))?; + let def: UnionId = self.to_id(container.with_value(it))?; VariantId::from(def).into() }, // FIXME: handle out-of-line modules here @@ -119,8 +119,8 @@ impl SourceBinder { return Some(res); } - let module_source = ModuleSource::from_child_node(db, src); - let c = crate::Module::from_definition(db, src.with_value(module_source))?; + let module_source = ModuleSource::from_child_node(self.db, src); + let c = crate::Module::from_definition(self.db, src.with_value(module_source))?; Some(c.id.into()) } } -- cgit v1.2.3