From e1aa96f2c5b6cdbf0fb7f49b47209055b7a937f2 Mon Sep 17 00:00:00 2001 From: Kirill Bulatov Date: Mon, 9 Mar 2020 11:26:46 +0200 Subject: Less abstract CrateData api --- crates/ra_db/src/input.rs | 60 ++++++++++++++---------------- crates/ra_hir/src/code_model.rs | 13 ++++--- crates/ra_hir_def/src/find_path.rs | 2 +- crates/ra_hir_def/src/lang_item.rs | 4 +- crates/ra_hir_def/src/nameres.rs | 3 +- crates/ra_hir_def/src/nameres/collector.rs | 7 ++-- crates/ra_hir_ty/src/traits.rs | 2 +- crates/ra_ide/src/hover.rs | 4 +- crates/ra_ide/src/lib.rs | 4 +- 9 files changed, 48 insertions(+), 51 deletions(-) (limited to 'crates') diff --git a/crates/ra_db/src/input.rs b/crates/ra_db/src/input.rs index 7b9f4efe4..4069c0fed 100644 --- a/crates/ra_db/src/input.rs +++ b/crates/ra_db/src/input.rs @@ -104,13 +104,16 @@ impl CrateName { } #[derive(Debug, Clone, PartialEq, Eq)] -struct CrateData { - file_id: FileId, - edition: Edition, - declaration_name: Option, +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, cfg_options: CfgOptions, env: Env, - dependencies: Vec, + pub dependencies: Vec, } #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] @@ -135,11 +138,11 @@ impl CrateGraph { &mut self, file_id: FileId, edition: Edition, - declaration_name: Option, + display_name: Option, 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 { - 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 { - 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, + display_name: Option, 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![&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() }] ); } } diff --git a/crates/ra_hir/src/code_model.rs b/crates/ra_hir/src/code_model.rs index dca5f27b2..9685d6982 100644 --- a/crates/ra_hir/src/code_model.rs +++ b/crates/ra_hir/src/code_model.rs @@ -55,7 +55,9 @@ pub struct CrateDependency { impl Crate { pub fn dependencies(self, db: &impl DefDatabase) -> Vec { 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 { diff --git a/crates/ra_hir_def/src/find_path.rs b/crates/ra_hir_def/src/find_path.rs index 43b9b124a..217e19b01 100644 --- a/crates/ra_hir_def/src/find_path.rs +++ b/crates/ra_hir_def/src/find_path.rs @@ -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) diff --git a/crates/ra_hir_def/src/lang_item.rs b/crates/ra_hir_def/src/lang_item.rs index 5a336ea1f..79e8d8038 100644 --- a/crates/ra_hir_def/src/lang_item.rs +++ b/crates/ra_hir_def/src/lang_item.rs @@ -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())) } diff --git a/crates/ra_hir_def/src/nameres.rs b/crates/ra_hir_def/src/nameres.rs index 628c44c99..6af0f4a8e 100644 --- a/crates/ra_hir_def/src/nameres.rs +++ b/crates/ra_hir_def/src/nameres.rs @@ -179,8 +179,7 @@ impl CrateDefMap { pub(crate) fn crate_def_map_query(db: &impl DefDatabase, krate: CrateId) -> Arc { 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 = Arena::default(); let root = modules.alloc(ModuleData::default()); CrateDefMap { diff --git a/crates/ra_hir_def/src/nameres/collector.rs b/crates/ra_hir_def/src/nameres/collector.rs index 51c65a5d7..e69f89b80 100644 --- a/crates/ra_hir_def/src/nameres/collector.rs +++ b/crates/ra_hir_def/src/nameres/collector.rs @@ -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 = Arena::default(); let root = modules.alloc(ModuleData::default()); CrateDefMap { diff --git a/crates/ra_hir_ty/src/traits.rs b/crates/ra_hir_ty/src/traits.rs index 8de588790..6c653c4f5 100644 --- a/crates/ra_hir_ty/src/traits.rs +++ b/crates/ra_hir_ty/src/traits.rs @@ -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); diff --git a/crates/ra_ide/src/hover.rs b/crates/ra_ide/src/hover.rs index f87054838..8b8af35fc 100644 --- a/crates/ra_ide/src/hover.rs +++ b/crates/ra_ide/src/hover.rs @@ -121,7 +121,7 @@ fn definition_owner_name(db: &RootDatabase, def: &Definition) -> Option fn determine_mod_path(db: &RootDatabase, def: &Definition) -> Option { 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 { .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 diff --git a/crates/ra_ide/src/lib.rs b/crates/ra_ide/src/lib.rs index 56bc57d5c..903624381 100644 --- a/crates/ra_ide/src/lib.rs +++ b/crates/ra_ide/src/lib.rs @@ -421,12 +421,12 @@ impl Analysis { /// Returns the edition of the given crate. pub fn crate_edition(&self, crate_id: CrateId) -> Cancelable { - 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 { - 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. -- cgit v1.2.3