Less abstract CrateData api
This commit is contained in:
parent
5cffef56e2
commit
e1aa96f2c5
@ -104,13 +104,16 @@ impl CrateName {
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
struct CrateData {
|
||||
file_id: FileId,
|
||||
edition: Edition,
|
||||
declaration_name: Option<String>,
|
||||
pub struct CrateData {
|
||||
pub root_file_id: FileId,
|
||||
pub edition: Edition,
|
||||
/// The name to display to the end user.
|
||||
/// This actual crate name can be different in a particular dependent crate
|
||||
/// or may even be missing for some cases, such as a dummy crate for the code snippet.
|
||||
pub display_name: Option<String>,
|
||||
cfg_options: CfgOptions,
|
||||
env: Env,
|
||||
dependencies: Vec<Dependency>,
|
||||
pub dependencies: Vec<Dependency>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
||||
@ -135,11 +138,11 @@ impl CrateGraph {
|
||||
&mut self,
|
||||
file_id: FileId,
|
||||
edition: Edition,
|
||||
declaration_name: Option<String>,
|
||||
display_name: Option<String>,
|
||||
cfg_options: CfgOptions,
|
||||
env: Env,
|
||||
) -> CrateId {
|
||||
let data = CrateData::new(file_id, edition, declaration_name, cfg_options, env);
|
||||
let data = CrateData::new(file_id, edition, display_name, cfg_options, env);
|
||||
let crate_id = CrateId(self.arena.len() as u32);
|
||||
let prev = self.arena.insert(crate_id, data);
|
||||
assert!(prev.is_none());
|
||||
@ -171,33 +174,17 @@ impl CrateGraph {
|
||||
self.arena.keys().copied()
|
||||
}
|
||||
|
||||
pub fn crate_root(&self, crate_id: CrateId) -> FileId {
|
||||
self.arena[&crate_id].file_id
|
||||
}
|
||||
|
||||
pub fn edition(&self, crate_id: CrateId) -> Edition {
|
||||
self.arena[&crate_id].edition
|
||||
}
|
||||
|
||||
/// Returns a name of a crate, declared in the root project.
|
||||
/// May be missing for some cases, such as when the crate definition was created for a code snippet.
|
||||
///
|
||||
/// This should not be considered as a normal crate name, since the actual name can be different in
|
||||
/// a particular dependent crate, where it is specified.
|
||||
pub fn declaration_name(&self, crate_id: &CrateId) -> Option<&String> {
|
||||
self.arena[crate_id].declaration_name.as_ref()
|
||||
pub fn crate_data(&self, crate_id: &CrateId) -> &CrateData {
|
||||
&self.arena[crate_id]
|
||||
}
|
||||
|
||||
// FIXME: this only finds one crate with the given root; we could have multiple
|
||||
pub fn crate_id_for_crate_root(&self, file_id: FileId) -> Option<CrateId> {
|
||||
let (&crate_id, _) = self.arena.iter().find(|(_crate_id, data)| data.file_id == file_id)?;
|
||||
let (&crate_id, _) =
|
||||
self.arena.iter().find(|(_crate_id, data)| data.root_file_id == file_id)?;
|
||||
Some(crate_id)
|
||||
}
|
||||
|
||||
pub fn dependencies(&self, crate_id: CrateId) -> impl Iterator<Item = &Dependency> {
|
||||
self.arena[&crate_id].dependencies.iter()
|
||||
}
|
||||
|
||||
/// Extends this crate graph by adding a complete disjoint second crate
|
||||
/// graph.
|
||||
///
|
||||
@ -220,7 +207,7 @@ impl CrateGraph {
|
||||
return false;
|
||||
}
|
||||
|
||||
for dep in self.dependencies(from) {
|
||||
for dep in &self.crate_data(&from).dependencies {
|
||||
let crate_id = dep.crate_id();
|
||||
if crate_id == target {
|
||||
return true;
|
||||
@ -242,13 +229,20 @@ impl CrateId {
|
||||
|
||||
impl CrateData {
|
||||
fn new(
|
||||
file_id: FileId,
|
||||
root_file_id: FileId,
|
||||
edition: Edition,
|
||||
declaration_name: Option<String>,
|
||||
display_name: Option<String>,
|
||||
cfg_options: CfgOptions,
|
||||
env: Env,
|
||||
) -> CrateData {
|
||||
CrateData { file_id, edition, declaration_name, dependencies: Vec::new(), cfg_options, env }
|
||||
CrateData {
|
||||
root_file_id,
|
||||
edition,
|
||||
display_name,
|
||||
dependencies: Vec::new(),
|
||||
cfg_options,
|
||||
env,
|
||||
}
|
||||
}
|
||||
|
||||
fn add_dep(&mut self, name: SmolStr, crate_id: CrateId) {
|
||||
@ -382,8 +376,8 @@ mod tests {
|
||||
.add_dep(crate1, CrateName::normalize_dashes("crate-name-with-dashes"), crate2)
|
||||
.is_ok());
|
||||
assert_eq!(
|
||||
graph.dependencies(crate1).collect::<Vec<_>>(),
|
||||
vec![&Dependency { crate_id: crate2, name: "crate_name_with_dashes".into() }]
|
||||
graph.crate_data(&crate1).dependencies,
|
||||
vec![Dependency { crate_id: crate2, name: "crate_name_with_dashes".into() }]
|
||||
);
|
||||
}
|
||||
}
|
||||
|
@ -55,7 +55,9 @@ pub struct CrateDependency {
|
||||
impl Crate {
|
||||
pub fn dependencies(self, db: &impl DefDatabase) -> Vec<CrateDependency> {
|
||||
db.crate_graph()
|
||||
.dependencies(self.id)
|
||||
.crate_data(&self.id)
|
||||
.dependencies
|
||||
.iter()
|
||||
.map(|dep| {
|
||||
let krate = Crate { id: dep.crate_id() };
|
||||
let name = dep.as_name();
|
||||
@ -69,7 +71,9 @@ impl Crate {
|
||||
let crate_graph = db.crate_graph();
|
||||
crate_graph
|
||||
.iter()
|
||||
.filter(|&krate| crate_graph.dependencies(krate).any(|it| it.crate_id == self.id))
|
||||
.filter(|&krate| {
|
||||
crate_graph.crate_data(&krate).dependencies.iter().any(|it| it.crate_id == self.id)
|
||||
})
|
||||
.map(|id| Crate { id })
|
||||
.collect()
|
||||
}
|
||||
@ -80,12 +84,11 @@ impl Crate {
|
||||
}
|
||||
|
||||
pub fn root_file(self, db: &impl DefDatabase) -> FileId {
|
||||
db.crate_graph().crate_root(self.id)
|
||||
db.crate_graph().crate_data(&self.id).root_file_id
|
||||
}
|
||||
|
||||
pub fn edition(self, db: &impl DefDatabase) -> Edition {
|
||||
let crate_graph = db.crate_graph();
|
||||
crate_graph.edition(self.id)
|
||||
db.crate_graph().crate_data(&self.id).edition
|
||||
}
|
||||
|
||||
pub fn all(db: &impl DefDatabase) -> Vec<Crate> {
|
||||
|
@ -176,7 +176,7 @@ fn find_importable_locations(
|
||||
// directly (only through reexports in direct dependencies).
|
||||
for krate in Some(from.krate)
|
||||
.into_iter()
|
||||
.chain(crate_graph.dependencies(from.krate).map(|dep| dep.crate_id))
|
||||
.chain(crate_graph.crate_data(&from.krate).dependencies.iter().map(|dep| dep.crate_id))
|
||||
{
|
||||
result.extend(
|
||||
importable_locations_in_crate(db, item, krate)
|
||||
|
@ -117,7 +117,9 @@ impl LangItems {
|
||||
return Some(*target);
|
||||
}
|
||||
db.crate_graph()
|
||||
.dependencies(start_crate)
|
||||
.crate_data(&start_crate)
|
||||
.dependencies
|
||||
.iter()
|
||||
.find_map(|dep| db.lang_item(dep.crate_id, item.clone()))
|
||||
}
|
||||
|
||||
|
@ -179,8 +179,7 @@ impl CrateDefMap {
|
||||
pub(crate) fn crate_def_map_query(db: &impl DefDatabase, krate: CrateId) -> Arc<CrateDefMap> {
|
||||
let _p = profile("crate_def_map_query");
|
||||
let def_map = {
|
||||
let crate_graph = db.crate_graph();
|
||||
let edition = crate_graph.edition(krate);
|
||||
let edition = db.crate_graph().crate_data(&krate).edition;
|
||||
let mut modules: Arena<LocalModuleId, ModuleData> = Arena::default();
|
||||
let root = modules.alloc(ModuleData::default());
|
||||
CrateDefMap {
|
||||
|
@ -34,7 +34,7 @@ pub(super) fn collect_defs(db: &impl DefDatabase, mut def_map: CrateDefMap) -> C
|
||||
let crate_graph = db.crate_graph();
|
||||
|
||||
// populate external prelude
|
||||
for dep in crate_graph.dependencies(def_map.krate) {
|
||||
for dep in &crate_graph.crate_data(&def_map.krate).dependencies {
|
||||
let dep_def_map = db.crate_def_map(dep.crate_id);
|
||||
log::debug!("crate dep {:?} -> {:?}", dep.name, dep.crate_id);
|
||||
def_map.extern_prelude.insert(
|
||||
@ -128,8 +128,7 @@ where
|
||||
DB: DefDatabase,
|
||||
{
|
||||
fn collect(&mut self) {
|
||||
let crate_graph = self.db.crate_graph();
|
||||
let file_id = crate_graph.crate_root(self.def_map.krate);
|
||||
let file_id = self.db.crate_graph().crate_data(&self.def_map.krate).root_file_id;
|
||||
let raw_items = self.db.raw_items(file_id.into());
|
||||
let module_id = self.def_map.root;
|
||||
self.def_map.modules[module_id].origin = ModuleOrigin::CrateRoot { definition: file_id };
|
||||
@ -955,7 +954,7 @@ mod tests {
|
||||
let krate = db.test_crate();
|
||||
|
||||
let def_map = {
|
||||
let edition = db.crate_graph().edition(krate);
|
||||
let edition = db.crate_graph().crate_data(&krate).edition;
|
||||
let mut modules: Arena<LocalModuleId, ModuleData> = Arena::default();
|
||||
let root = modules.alloc(ModuleData::default());
|
||||
CrateDefMap {
|
||||
|
@ -47,7 +47,7 @@ pub(crate) fn impls_for_trait_query(
|
||||
// will only ever get called for a few crates near the root of the tree (the
|
||||
// ones the user is editing), so this may actually be a waste of memory. I'm
|
||||
// doing it like this mainly for simplicity for now.
|
||||
for dep in db.crate_graph().dependencies(krate) {
|
||||
for dep in &db.crate_graph().crate_data(&krate).dependencies {
|
||||
impls.extend(db.impls_for_trait(dep.crate_id, trait_).iter());
|
||||
}
|
||||
let crate_impl_defs = db.impls_in_crate(krate);
|
||||
|
@ -121,7 +121,7 @@ fn definition_owner_name(db: &RootDatabase, def: &Definition) -> Option<String>
|
||||
|
||||
fn determine_mod_path(db: &RootDatabase, def: &Definition) -> Option<String> {
|
||||
let mod_path = def.module(db).map(|module| {
|
||||
once(db.crate_graph().declaration_name(&module.krate().into()).cloned())
|
||||
once(db.crate_graph().crate_data(&module.krate().into()).display_name.clone())
|
||||
.chain(
|
||||
module
|
||||
.path_to_root(db)
|
||||
@ -130,7 +130,7 @@ fn determine_mod_path(db: &RootDatabase, def: &Definition) -> Option<String> {
|
||||
.map(|it| it.name(db).map(|name| name.to_string())),
|
||||
)
|
||||
.chain(once(definition_owner_name(db, def)))
|
||||
.filter_map(std::convert::identity)
|
||||
.flatten()
|
||||
.join("::")
|
||||
});
|
||||
mod_path
|
||||
|
@ -421,12 +421,12 @@ impl Analysis {
|
||||
|
||||
/// Returns the edition of the given crate.
|
||||
pub fn crate_edition(&self, crate_id: CrateId) -> Cancelable<Edition> {
|
||||
self.with_db(|db| db.crate_graph().edition(crate_id))
|
||||
self.with_db(|db| db.crate_graph().crate_data(&crate_id).edition)
|
||||
}
|
||||
|
||||
/// Returns the root file of the given crate.
|
||||
pub fn crate_root(&self, crate_id: CrateId) -> Cancelable<FileId> {
|
||||
self.with_db(|db| db.crate_graph().crate_root(crate_id))
|
||||
self.with_db(|db| db.crate_graph().crate_data(&crate_id).root_file_id)
|
||||
}
|
||||
|
||||
/// Returns the set of possible targets to run for the current file.
|
||||
|
Loading…
x
Reference in New Issue
Block a user