mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-28 21:05:02 +00:00
Cleanup Crate API
This commit is contained in:
parent
ffcdd25cc8
commit
08d3166c8b
5 changed files with 42 additions and 32 deletions
|
@ -25,7 +25,7 @@ use hir_expand::{
|
||||||
MacroDefId,
|
MacroDefId,
|
||||||
};
|
};
|
||||||
use hir_ty::expr::ExprValidator;
|
use hir_ty::expr::ExprValidator;
|
||||||
use ra_db::{CrateId, Edition};
|
use ra_db::{CrateId, Edition, FileId};
|
||||||
use ra_syntax::ast;
|
use ra_syntax::ast;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
|
@ -40,7 +40,7 @@ use crate::{
|
||||||
/// root module.
|
/// root module.
|
||||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
||||||
pub struct Crate {
|
pub struct Crate {
|
||||||
pub(crate) crate_id: CrateId,
|
pub(crate) id: CrateId,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
|
@ -50,33 +50,47 @@ pub struct CrateDependency {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Crate {
|
impl Crate {
|
||||||
pub fn crate_id(self) -> CrateId {
|
|
||||||
self.crate_id
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn dependencies(self, db: &impl DefDatabase) -> Vec<CrateDependency> {
|
pub fn dependencies(self, db: &impl DefDatabase) -> Vec<CrateDependency> {
|
||||||
db.crate_graph()
|
db.crate_graph()
|
||||||
.dependencies(self.crate_id)
|
.dependencies(self.id)
|
||||||
.map(|dep| {
|
.map(|dep| {
|
||||||
let krate = Crate { crate_id: dep.crate_id() };
|
let krate = Crate { id: dep.crate_id() };
|
||||||
let name = dep.as_name();
|
let name = dep.as_name();
|
||||||
CrateDependency { krate, name }
|
CrateDependency { krate, name }
|
||||||
})
|
})
|
||||||
.collect()
|
.collect()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// FIXME: add `transitive_reverse_dependencies`.
|
||||||
|
pub fn reverse_dependencies(self, db: &impl DefDatabase) -> Vec<Crate> {
|
||||||
|
let crate_graph = db.crate_graph();
|
||||||
|
crate_graph
|
||||||
|
.iter()
|
||||||
|
.filter(|&krate| crate_graph.dependencies(krate).any(|it| it.crate_id == self.id))
|
||||||
|
.map(|id| Crate { id })
|
||||||
|
.collect()
|
||||||
|
}
|
||||||
|
|
||||||
pub fn root_module(self, db: &impl DefDatabase) -> Option<Module> {
|
pub fn root_module(self, db: &impl DefDatabase) -> Option<Module> {
|
||||||
let module_id = db.crate_def_map(self.crate_id).root;
|
let module_id = db.crate_def_map(self.id).root;
|
||||||
Some(Module::new(self, module_id))
|
Some(Module::new(self, module_id))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn root_file(self, db: &impl DefDatabase) -> FileId {
|
||||||
|
db.crate_graph().crate_root(self.id)
|
||||||
|
}
|
||||||
|
|
||||||
pub fn edition(self, db: &impl DefDatabase) -> Edition {
|
pub fn edition(self, db: &impl DefDatabase) -> Edition {
|
||||||
let crate_graph = db.crate_graph();
|
let crate_graph = db.crate_graph();
|
||||||
crate_graph.edition(self.crate_id)
|
crate_graph.edition(self.id)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn all(db: &impl DefDatabase) -> Vec<Crate> {
|
pub fn all(db: &impl DefDatabase) -> Vec<Crate> {
|
||||||
db.crate_graph().iter().map(|crate_id| Crate { crate_id }).collect()
|
db.crate_graph().iter().map(|id| Crate { id }).collect()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn crate_id(self) -> CrateId {
|
||||||
|
self.id
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -115,7 +129,7 @@ pub use hir_def::attr::Attrs;
|
||||||
|
|
||||||
impl Module {
|
impl Module {
|
||||||
pub(crate) fn new(krate: Crate, crate_module_id: LocalModuleId) -> Module {
|
pub(crate) fn new(krate: Crate, crate_module_id: LocalModuleId) -> Module {
|
||||||
Module { id: ModuleId { krate: krate.crate_id, local_id: crate_module_id } }
|
Module { id: ModuleId { krate: krate.id, local_id: crate_module_id } }
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Name of this module.
|
/// Name of this module.
|
||||||
|
@ -133,7 +147,7 @@ impl Module {
|
||||||
|
|
||||||
/// Returns the crate this module is part of.
|
/// Returns the crate this module is part of.
|
||||||
pub fn krate(self) -> Crate {
|
pub fn krate(self) -> Crate {
|
||||||
Crate { crate_id: self.id.krate }
|
Crate { id: self.id.krate }
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Topmost parent of this module. Every module has a `crate_root`, but some
|
/// Topmost parent of this module. Every module has a `crate_root`, but some
|
||||||
|
@ -878,11 +892,11 @@ pub struct ImplBlock {
|
||||||
|
|
||||||
impl ImplBlock {
|
impl ImplBlock {
|
||||||
pub fn all_in_crate(db: &impl HirDatabase, krate: Crate) -> Vec<ImplBlock> {
|
pub fn all_in_crate(db: &impl HirDatabase, krate: Crate) -> Vec<ImplBlock> {
|
||||||
let impls = db.impls_in_crate(krate.crate_id);
|
let impls = db.impls_in_crate(krate.id);
|
||||||
impls.all_impls().map(Self::from).collect()
|
impls.all_impls().map(Self::from).collect()
|
||||||
}
|
}
|
||||||
pub fn for_trait(db: &impl HirDatabase, krate: Crate, trait_: Trait) -> Vec<ImplBlock> {
|
pub fn for_trait(db: &impl HirDatabase, krate: Crate, trait_: Trait) -> Vec<ImplBlock> {
|
||||||
let impls = db.impls_in_crate(krate.crate_id);
|
let impls = db.impls_in_crate(krate.id);
|
||||||
impls.lookup_impl_blocks_for_trait(trait_.id).map(Self::from).collect()
|
impls.lookup_impl_blocks_for_trait(trait_.id).map(Self::from).collect()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -915,7 +929,7 @@ impl ImplBlock {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn krate(&self, db: &impl DefDatabase) -> Crate {
|
pub fn krate(&self, db: &impl DefDatabase) -> Crate {
|
||||||
Crate { crate_id: self.module(db).id.krate }
|
Crate { id: self.module(db).id.krate }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1053,7 +1067,7 @@ impl Type {
|
||||||
krate: Crate,
|
krate: Crate,
|
||||||
mut callback: impl FnMut(AssocItem) -> Option<T>,
|
mut callback: impl FnMut(AssocItem) -> Option<T>,
|
||||||
) -> Option<T> {
|
) -> Option<T> {
|
||||||
for krate in self.ty.value.def_crates(db, krate.crate_id)? {
|
for krate in self.ty.value.def_crates(db, krate.id)? {
|
||||||
let impls = db.impls_in_crate(krate);
|
let impls = db.impls_in_crate(krate);
|
||||||
|
|
||||||
for impl_block in impls.lookup_impl_blocks(&self.ty.value) {
|
for impl_block in impls.lookup_impl_blocks(&self.ty.value) {
|
||||||
|
|
|
@ -57,9 +57,9 @@ pub trait HirDebugDatabase {
|
||||||
impl<DB: HirDebugHelper> HirDebugDatabase for DB {
|
impl<DB: HirDebugHelper> HirDebugDatabase for DB {
|
||||||
fn debug_crate(&self, krate: Crate, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
|
fn debug_crate(&self, krate: Crate, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
let mut builder = fmt.debug_tuple("Crate");
|
let mut builder = fmt.debug_tuple("Crate");
|
||||||
match self.crate_name(krate.crate_id) {
|
match self.crate_name(krate.id) {
|
||||||
Some(name) => builder.field(&name),
|
Some(name) => builder.field(&name),
|
||||||
None => builder.field(&krate.crate_id),
|
None => builder.field(&krate.id),
|
||||||
}
|
}
|
||||||
.finish()
|
.finish()
|
||||||
}
|
}
|
||||||
|
|
|
@ -14,8 +14,8 @@ use crate::{
|
||||||
};
|
};
|
||||||
|
|
||||||
impl From<ra_db::CrateId> for Crate {
|
impl From<ra_db::CrateId> for Crate {
|
||||||
fn from(crate_id: ra_db::CrateId) -> Self {
|
fn from(id: ra_db::CrateId) -> Self {
|
||||||
Crate { crate_id }
|
Crate { id }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -95,7 +95,7 @@ impl FromSource for MacroDef {
|
||||||
|
|
||||||
let module_src = ModuleSource::from_child_node(db, src.as_ref().map(|it| it.syntax()));
|
let module_src = ModuleSource::from_child_node(db, src.as_ref().map(|it| it.syntax()));
|
||||||
let module = Module::from_definition(db, InFile::new(src.file_id, module_src))?;
|
let module = Module::from_definition(db, InFile::new(src.file_id, module_src))?;
|
||||||
let krate = Some(module.krate().crate_id());
|
let krate = Some(module.krate().id);
|
||||||
|
|
||||||
let ast_id = Some(AstId::new(src.file_id, db.ast_id_map(src.file_id).ast_id(&src.value)));
|
let ast_id = Some(AstId::new(src.file_id, db.ast_id_map(src.file_id).ast_id(&src.value)));
|
||||||
|
|
||||||
|
|
|
@ -5,7 +5,7 @@
|
||||||
use std::mem;
|
use std::mem;
|
||||||
|
|
||||||
use hir::{DefWithBody, HasSource, ModuleSource};
|
use hir::{DefWithBody, HasSource, ModuleSource};
|
||||||
use ra_db::{FileId, SourceDatabase, SourceDatabaseExt};
|
use ra_db::{FileId, SourceDatabaseExt};
|
||||||
use ra_prof::profile;
|
use ra_prof::profile;
|
||||||
use ra_syntax::{AstNode, TextRange};
|
use ra_syntax::{AstNode, TextRange};
|
||||||
use rustc_hash::FxHashMap;
|
use rustc_hash::FxHashMap;
|
||||||
|
@ -120,16 +120,12 @@ impl NameDefinition {
|
||||||
}
|
}
|
||||||
if vis.as_str() == "pub" {
|
if vis.as_str() == "pub" {
|
||||||
let krate = self.container.krate();
|
let krate = self.container.krate();
|
||||||
let crate_graph = db.crate_graph();
|
for rev_dep in krate.reverse_dependencies(db) {
|
||||||
for crate_id in crate_graph.iter() {
|
let root_file = rev_dep.root_file(db);
|
||||||
let mut crate_deps = crate_graph.dependencies(crate_id);
|
|
||||||
if crate_deps.any(|dep| dep.crate_id() == krate.crate_id()) {
|
|
||||||
let root_file = crate_graph.crate_root(crate_id);
|
|
||||||
let source_root_id = db.file_source_root(root_file);
|
let source_root_id = db.file_source_root(root_file);
|
||||||
let source_root = db.source_root(source_root_id);
|
let source_root = db.source_root(source_root_id);
|
||||||
res.extend(source_root.walk().map(|id| (id, None)));
|
res.extend(source_root.walk().map(|id| (id, None)));
|
||||||
}
|
}
|
||||||
}
|
|
||||||
return SearchScope::new(res);
|
return SearchScope::new(res);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue