mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-28 12:54:58 +00:00
Store DB in SourceBinder
This commit is contained in:
parent
ccfe53376a
commit
a71bb70f0a
3 changed files with 33 additions and 32 deletions
|
@ -48,6 +48,7 @@ pub use crate::{
|
||||||
from_source::FromSource,
|
from_source::FromSource,
|
||||||
has_source::HasSource,
|
has_source::HasSource,
|
||||||
source_analyzer::{PathResolution, ScopeEntryWithSyntax, SourceAnalyzer},
|
source_analyzer::{PathResolution, ScopeEntryWithSyntax, SourceAnalyzer},
|
||||||
|
source_binder::SourceBinder,
|
||||||
};
|
};
|
||||||
|
|
||||||
pub use hir_def::{
|
pub use hir_def::{
|
||||||
|
|
|
@ -105,7 +105,7 @@ impl SourceAnalyzer {
|
||||||
node: InFile<&SyntaxNode>,
|
node: InFile<&SyntaxNode>,
|
||||||
offset: Option<TextUnit>,
|
offset: Option<TextUnit>,
|
||||||
) -> SourceAnalyzer {
|
) -> 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(
|
pub(crate) fn new_for_body(
|
||||||
|
|
|
@ -18,48 +18,52 @@ use rustc_hash::FxHashMap;
|
||||||
|
|
||||||
use crate::{db::HirDatabase, ModuleSource, SourceAnalyzer};
|
use crate::{db::HirDatabase, ModuleSource, SourceAnalyzer};
|
||||||
|
|
||||||
#[derive(Default)]
|
pub struct SourceBinder<'a, DB> {
|
||||||
pub struct SourceBinder {
|
pub db: &'a DB,
|
||||||
child_by_source_cache: FxHashMap<ChildContainer, DynMap>,
|
child_by_source_cache: FxHashMap<ChildContainer, DynMap>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl SourceBinder {
|
impl<DB: HirDatabase> SourceBinder<'_, DB> {
|
||||||
|
pub fn new(db: &DB) -> SourceBinder<DB> {
|
||||||
|
SourceBinder { db, child_by_source_cache: FxHashMap::default() }
|
||||||
|
}
|
||||||
|
|
||||||
pub fn analyze(
|
pub fn analyze(
|
||||||
&mut self,
|
&mut self,
|
||||||
db: &impl HirDatabase,
|
|
||||||
src: InFile<&SyntaxNode>,
|
src: InFile<&SyntaxNode>,
|
||||||
offset: Option<TextUnit>,
|
offset: Option<TextUnit>,
|
||||||
) -> SourceAnalyzer {
|
) -> SourceAnalyzer {
|
||||||
let _p = profile("SourceBinder::analyzer");
|
let _p = profile("SourceBinder::analyzer");
|
||||||
let container = match self.find_container(db, src) {
|
let container = match self.find_container(src) {
|
||||||
Some(it) => it,
|
Some(it) => it,
|
||||||
None => return SourceAnalyzer::new_for_resolver(Resolver::default(), src),
|
None => return SourceAnalyzer::new_for_resolver(Resolver::default(), src),
|
||||||
};
|
};
|
||||||
|
|
||||||
let resolver = match container {
|
let resolver = match container {
|
||||||
ChildContainer::DefWithBodyId(def) => {
|
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::TraitId(it) => it.resolver(self.db),
|
||||||
ChildContainer::ImplId(it) => it.resolver(db),
|
ChildContainer::ImplId(it) => it.resolver(self.db),
|
||||||
ChildContainer::ModuleId(it) => it.resolver(db),
|
ChildContainer::ModuleId(it) => it.resolver(self.db),
|
||||||
ChildContainer::EnumId(it) => it.resolver(db),
|
ChildContainer::EnumId(it) => it.resolver(self.db),
|
||||||
ChildContainer::VariantId(it) => it.resolver(db),
|
ChildContainer::VariantId(it) => it.resolver(self.db),
|
||||||
};
|
};
|
||||||
SourceAnalyzer::new_for_resolver(resolver, src)
|
SourceAnalyzer::new_for_resolver(resolver, src)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn to_def<D, ID>(&mut self, db: &impl HirDatabase, src: InFile<ID::Ast>) -> Option<D>
|
pub fn to_def<D, ID>(&mut self, src: InFile<ID::Ast>) -> Option<D>
|
||||||
where
|
where
|
||||||
D: From<ID>,
|
D: From<ID>,
|
||||||
ID: ToId,
|
ID: ToId,
|
||||||
{
|
{
|
||||||
let id: ID = self.to_id(db, src)?;
|
let id: ID = self.to_id(src)?;
|
||||||
Some(id.into())
|
Some(id.into())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn to_id<D: ToId>(&mut self, db: &impl HirDatabase, src: InFile<D::Ast>) -> Option<D> {
|
fn to_id<D: ToId>(&mut self, src: InFile<D::Ast>) -> Option<D> {
|
||||||
let container = self.find_container(db, src.as_ref().map(|it| it.syntax()))?;
|
let container = self.find_container(src.as_ref().map(|it| it.syntax()))?;
|
||||||
|
let db = self.db;
|
||||||
let dyn_map =
|
let dyn_map =
|
||||||
&*self.child_by_source_cache.entry(container).or_insert_with(|| match container {
|
&*self.child_by_source_cache.entry(container).or_insert_with(|| match container {
|
||||||
ChildContainer::DefWithBodyId(it) => it.child_by_source(db),
|
ChildContainer::DefWithBodyId(it) => it.child_by_source(db),
|
||||||
|
@ -72,44 +76,40 @@ impl SourceBinder {
|
||||||
dyn_map[D::KEY].get(&src).copied()
|
dyn_map[D::KEY].get(&src).copied()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn find_container(
|
fn find_container(&mut self, src: InFile<&SyntaxNode>) -> Option<ChildContainer> {
|
||||||
&mut self,
|
for container in src.cloned().ancestors_with_macros(self.db).skip(1) {
|
||||||
db: &impl HirDatabase,
|
|
||||||
src: InFile<&SyntaxNode>,
|
|
||||||
) -> Option<ChildContainer> {
|
|
||||||
for container in src.cloned().ancestors_with_macros(db).skip(1) {
|
|
||||||
let res: ChildContainer = match_ast! {
|
let res: ChildContainer = match_ast! {
|
||||||
match (container.value) {
|
match (container.value) {
|
||||||
ast::TraitDef(it) => {
|
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()
|
def.into()
|
||||||
},
|
},
|
||||||
ast::ImplBlock(it) => {
|
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()
|
def.into()
|
||||||
},
|
},
|
||||||
ast::FnDef(it) => {
|
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()
|
DefWithBodyId::from(def).into()
|
||||||
},
|
},
|
||||||
ast::StaticDef(it) => {
|
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()
|
DefWithBodyId::from(def).into()
|
||||||
},
|
},
|
||||||
ast::ConstDef(it) => {
|
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()
|
DefWithBodyId::from(def).into()
|
||||||
},
|
},
|
||||||
ast::EnumDef(it) => {
|
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()
|
def.into()
|
||||||
},
|
},
|
||||||
ast::StructDef(it) => {
|
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()
|
VariantId::from(def).into()
|
||||||
},
|
},
|
||||||
ast::UnionDef(it) => {
|
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()
|
VariantId::from(def).into()
|
||||||
},
|
},
|
||||||
// FIXME: handle out-of-line modules here
|
// FIXME: handle out-of-line modules here
|
||||||
|
@ -119,8 +119,8 @@ impl SourceBinder {
|
||||||
return Some(res);
|
return Some(res);
|
||||||
}
|
}
|
||||||
|
|
||||||
let module_source = ModuleSource::from_child_node(db, src);
|
let module_source = ModuleSource::from_child_node(self.db, src);
|
||||||
let c = crate::Module::from_definition(db, src.with_value(module_source))?;
|
let c = crate::Module::from_definition(self.db, src.with_value(module_source))?;
|
||||||
Some(c.id.into())
|
Some(c.id.into())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue