diff --git a/crates/rust-analyzer/src/cli.rs b/crates/rust-analyzer/src/cli.rs index 6879a462dc6..ed732eb3879 100644 --- a/crates/rust-analyzer/src/cli.rs +++ b/crates/rust-analyzer/src/cli.rs @@ -18,7 +18,7 @@ analysis_bench::{BenchCmd, BenchWhat, Position}, analysis_stats::AnalysisStatsCmd, diagnostics::diagnostics, - load_cargo::load_cargo, + load_cargo::{load_workspace, load_workspace_at, LoadCargoConfig}, ssr::{apply_ssr_rules, search_for_patterns}, }; diff --git a/crates/rust-analyzer/src/cli/analysis_bench.rs b/crates/rust-analyzer/src/cli/analysis_bench.rs index 6735b6388cd..d26b4cf66d3 100644 --- a/crates/rust-analyzer/src/cli/analysis_bench.rs +++ b/crates/rust-analyzer/src/cli/analysis_bench.rs @@ -17,7 +17,7 @@ use vfs::AbsPathBuf; use crate::cli::{ - load_cargo::{load_cargo, LoadCargoConfig}, + load_cargo::{load_workspace_at, LoadCargoConfig}, print_memory_usage, Verbosity, }; @@ -63,13 +63,13 @@ pub fn run(self, verbosity: Verbosity) -> Result<()> { let start = Instant::now(); eprint!("loading: "); + let cargo_config = Default::default(); let load_cargo_config = LoadCargoConfig { - cargo_config: Default::default(), load_out_dirs_from_check: self.load_output_dirs, with_proc_macro: self.with_proc_macro, }; - - let (mut host, vfs) = load_cargo(&self.path, &load_cargo_config)?; + let (mut host, vfs) = + load_workspace_at(&self.path, &cargo_config, &load_cargo_config, &|_| {})?; eprintln!("{:?}\n", start.elapsed()); let file_id = { diff --git a/crates/rust-analyzer/src/cli/analysis_stats.rs b/crates/rust-analyzer/src/cli/analysis_stats.rs index 3417af687d5..87dcd406df1 100644 --- a/crates/rust-analyzer/src/cli/analysis_stats.rs +++ b/crates/rust-analyzer/src/cli/analysis_stats.rs @@ -25,7 +25,7 @@ use syntax::AstNode; use crate::cli::{ - load_cargo::{load_cargo, LoadCargoConfig}, + load_cargo::{load_workspace_at, LoadCargoConfig}, print_memory_usage, progress_report::ProgressReport, report_metric, Result, Verbosity, @@ -59,12 +59,13 @@ pub fn run(self, verbosity: Verbosity) -> Result<()> { }; let mut db_load_sw = self.stop_watch(); + let cargo_config = Default::default(); let load_cargo_config = LoadCargoConfig { - cargo_config: Default::default(), load_out_dirs_from_check: self.load_output_dirs, with_proc_macro: self.with_proc_macro, }; - let (host, vfs) = load_cargo(&self.path, &load_cargo_config)?; + let (host, vfs) = + load_workspace_at(&self.path, &cargo_config, &load_cargo_config, &|_| {})?; let db = host.raw_database(); eprintln!("{:<20} {}", "Database loaded:", db_load_sw.elapsed()); diff --git a/crates/rust-analyzer/src/cli/diagnostics.rs b/crates/rust-analyzer/src/cli/diagnostics.rs index c60374c24f1..876f6c44f3e 100644 --- a/crates/rust-analyzer/src/cli/diagnostics.rs +++ b/crates/rust-analyzer/src/cli/diagnostics.rs @@ -11,7 +11,7 @@ use ide_db::base_db::SourceDatabaseExt; use crate::cli::{ - load_cargo::{load_cargo, LoadCargoConfig}, + load_cargo::{load_workspace_at, LoadCargoConfig}, Result, }; @@ -33,12 +33,9 @@ pub fn diagnostics( load_out_dirs_from_check: bool, with_proc_macro: bool, ) -> Result<()> { - let load_cargo_config = LoadCargoConfig { - cargo_config: Default::default(), - load_out_dirs_from_check, - with_proc_macro, - }; - let (host, _vfs) = load_cargo(path, &load_cargo_config)?; + let cargo_config = Default::default(); + let load_cargo_config = LoadCargoConfig { load_out_dirs_from_check, with_proc_macro }; + let (host, _vfs) = load_workspace_at(path, &cargo_config, &load_cargo_config, &|_| {})?; let db = host.raw_database(); let analysis = host.analysis(); diff --git a/crates/rust-analyzer/src/cli/load_cargo.rs b/crates/rust-analyzer/src/cli/load_cargo.rs index cf0cd81deef..23442afacce 100644 --- a/crates/rust-analyzer/src/cli/load_cargo.rs +++ b/crates/rust-analyzer/src/cli/load_cargo.rs @@ -14,16 +14,28 @@ use crate::reload::{ProjectFolders, SourceRootConfig}; pub struct LoadCargoConfig { - pub cargo_config: CargoConfig, pub load_out_dirs_from_check: bool, pub with_proc_macro: bool, } -pub fn load_cargo(root: &Path, config: &LoadCargoConfig) -> Result<(AnalysisHost, vfs::Vfs)> { +pub fn load_workspace_at( + root: &Path, + cargo_config: &CargoConfig, + load_config: &LoadCargoConfig, + progress: &dyn Fn(String), +) -> Result<(AnalysisHost, vfs::Vfs)> { let root = AbsPathBuf::assert(std::env::current_dir()?.join(root)); let root = ProjectManifest::discover_single(&root)?; - let ws = ProjectWorkspace::load(root, &config.cargo_config, &|_| {})?; + let workspace = ProjectWorkspace::load(root, cargo_config, progress)?; + load_workspace(workspace, load_config, progress) +} + +pub fn load_workspace( + ws: ProjectWorkspace, + config: &LoadCargoConfig, + progress: &dyn Fn(String), +) -> Result<(AnalysisHost, vfs::Vfs)> { let (sender, receiver) = unbounded(); let mut vfs = vfs::Vfs::default(); let mut loader = { @@ -42,7 +54,7 @@ pub fn load_cargo(root: &Path, config: &LoadCargoConfig) -> Result<(AnalysisHost let build_data = if config.load_out_dirs_from_check { let mut collector = BuildDataCollector::default(); ws.collect_build_data_configs(&mut collector); - Some(collector.collect(&|_| {})?) + Some(collector.collect(progress)?) } else { None }; @@ -66,11 +78,12 @@ pub fn load_cargo(root: &Path, config: &LoadCargoConfig) -> Result<(AnalysisHost }); log::debug!("crate graph: {:?}", crate_graph); - let host = load(crate_graph, project_folders.source_root_config, &mut vfs, &receiver); + let host = + load_crate_graph(crate_graph, project_folders.source_root_config, &mut vfs, &receiver); Ok((host, vfs)) } -fn load( +fn load_crate_graph( crate_graph: CrateGraph, source_root_config: SourceRootConfig, vfs: &mut vfs::Vfs, @@ -120,17 +133,17 @@ mod tests { use hir::Crate; #[test] - fn test_loading_rust_analyzer() { + fn test_loading_rust_analyzer() -> Result<()> { let path = Path::new(env!("CARGO_MANIFEST_DIR")).parent().unwrap().parent().unwrap(); - let load_cargo_config = LoadCargoConfig { - cargo_config: Default::default(), - load_out_dirs_from_check: false, - with_proc_macro: false, - }; + let cargo_config = Default::default(); + let load_cargo_config = + LoadCargoConfig { load_out_dirs_from_check: false, with_proc_macro: false }; + let (host, _vfs) = load_workspace_at(path, &cargo_config, &load_cargo_config, &|_| {})?; - let (host, _vfs) = load_cargo(path, &load_cargo_config).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); + + Ok(()) } } diff --git a/crates/rust-analyzer/src/cli/ssr.rs b/crates/rust-analyzer/src/cli/ssr.rs index 8729ff0d9f6..71c61ed58a4 100644 --- a/crates/rust-analyzer/src/cli/ssr.rs +++ b/crates/rust-analyzer/src/cli/ssr.rs @@ -1,19 +1,18 @@ //! Applies structured search replace rules from the command line. use crate::cli::{ - load_cargo::{load_cargo, LoadCargoConfig}, + load_cargo::{load_workspace_at, LoadCargoConfig}, Result, }; use ssr::{MatchFinder, SsrPattern, SsrRule}; pub fn apply_ssr_rules(rules: Vec) -> Result<()> { use ide_db::base_db::SourceDatabaseExt; - let load_cargo_config = LoadCargoConfig { - cargo_config: Default::default(), - load_out_dirs_from_check: true, - with_proc_macro: true, - }; - let (host, vfs) = load_cargo(&std::env::current_dir()?, &load_cargo_config)?; + let cargo_config = Default::default(); + let load_cargo_config = + LoadCargoConfig { load_out_dirs_from_check: true, with_proc_macro: true }; + let (host, vfs) = + load_workspace_at(&std::env::current_dir()?, &cargo_config, &load_cargo_config, &|_| {})?; let db = host.raw_database(); let mut match_finder = MatchFinder::at_first_file(db)?; for rule in rules { @@ -36,12 +35,11 @@ pub fn apply_ssr_rules(rules: Vec) -> Result<()> { pub fn search_for_patterns(patterns: Vec, debug_snippet: Option) -> Result<()> { use ide_db::base_db::SourceDatabaseExt; use ide_db::symbol_index::SymbolsDatabase; - let load_cargo_config = LoadCargoConfig { - cargo_config: Default::default(), - load_out_dirs_from_check: true, - with_proc_macro: true, - }; - let (host, _vfs) = load_cargo(&std::env::current_dir()?, &load_cargo_config)?; + let cargo_config = Default::default(); + let load_cargo_config = + LoadCargoConfig { load_out_dirs_from_check: true, with_proc_macro: true }; + let (host, _vfs) = + load_workspace_at(&std::env::current_dir()?, &cargo_config, &load_cargo_config, &|_| {})?; let db = host.raw_database(); let mut match_finder = MatchFinder::at_first_file(db)?; for pattern in patterns {