From 1555a1aa0d5d45e4b317db272c07ad3e8470553d Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Mon, 14 Oct 2019 15:15:47 +0300 Subject: [PATCH] remove one more dependency on source roots --- crates/ra_batch/src/lib.rs | 10 ++-------- crates/ra_cli/src/analysis_stats.rs | 29 +++++++++++++++++++++-------- crates/ra_hir/src/code_model.rs | 8 +++----- 3 files changed, 26 insertions(+), 21 deletions(-) diff --git a/crates/ra_batch/src/lib.rs b/crates/ra_batch/src/lib.rs index 602beb43941..df49eb13d12 100644 --- a/crates/ra_batch/src/lib.rs +++ b/crates/ra_batch/src/lib.rs @@ -141,14 +141,8 @@ mod tests { #[test] fn test_loading_rust_analyzer() { let path = Path::new(env!("CARGO_MANIFEST_DIR")).parent().unwrap().parent().unwrap(); - let (host, roots) = load_cargo(path).unwrap(); - let mut n_crates = 0; - for (root, _) in roots { - for _krate in Crate::source_root_crates(host.raw_database(), root) { - n_crates += 1; - } - } - + let (host, _roots) = load_cargo(path).unwrap(); + let n_crates = Crate::all(host.raw_database()).len(); // RA has quite a few crates, but the exact count doesn't matter assert!(n_crates > 20); } diff --git a/crates/ra_cli/src/analysis_stats.rs b/crates/ra_cli/src/analysis_stats.rs index a8a110bd936..fe4f57dcde6 100644 --- a/crates/ra_cli/src/analysis_stats.rs +++ b/crates/ra_cli/src/analysis_stats.rs @@ -22,16 +22,29 @@ pub fn run( let mut num_crates = 0; let mut visited_modules = HashSet::new(); let mut visit_queue = Vec::new(); - for (source_root_id, project_root) in roots { - if project_root.is_member() { - for krate in Crate::source_root_crates(db, source_root_id) { - num_crates += 1; - let module = - krate.root_module(db).expect("crate in source root without root module"); - visit_queue.push(module); - } + + let members = roots + .into_iter() + .filter_map( + |(source_root_id, project_root)| { + if project_root.is_member() { + Some(source_root_id) + } else { + None + } + }, + ) + .collect::>(); + + for krate in Crate::all(db) { + let module = krate.root_module(db).expect("crate without root module"); + let file_id = module.definition_source(db).file_id; + if members.contains(&db.file_source_root(file_id.original_file(db))) { + num_crates += 1; + visit_queue.push(module); } } + println!("Crates in this dir: {}", num_crates); let mut num_decls = 0; let mut funcs = Vec::new(); diff --git a/crates/ra_hir/src/code_model.rs b/crates/ra_hir/src/code_model.rs index 8055a07dbe2..8eb3c577d67 100644 --- a/crates/ra_hir/src/code_model.rs +++ b/crates/ra_hir/src/code_model.rs @@ -5,7 +5,7 @@ use std::sync::Arc; -use ra_db::{CrateId, Edition, FileId, SourceRootId}; +use ra_db::{CrateId, Edition, FileId}; use ra_syntax::ast::{self, NameOwner, TypeAscriptionOwner}; use crate::{ @@ -76,10 +76,8 @@ pub fn edition(self, db: &impl DefDatabase) -> Edition { crate_graph.edition(self.crate_id) } - // FIXME: should this be in source_binder? - pub fn source_root_crates(db: &impl DefDatabase, source_root: SourceRootId) -> Vec { - let crate_ids = db.source_root_crates(source_root); - crate_ids.iter().map(|&crate_id| Crate { crate_id }).collect() + pub fn all(db: &impl DefDatabase) -> Vec { + db.crate_graph().iter().map(|crate_id| Crate { crate_id }).collect() } }