Split pub fn cargo_load into pub fn load_workspace_at and pub fn load_workspace

This commit is contained in:
Vincent Esche 2021-02-16 00:26:47 +01:00
parent cc49502ab4
commit 1a44168260
6 changed files with 50 additions and 41 deletions

View File

@ -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},
};

View File

@ -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 = {

View File

@ -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());

View File

@ -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();

View File

@ -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(())
}
}

View File

@ -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<SsrRule>) -> 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<SsrRule>) -> Result<()> {
pub fn search_for_patterns(patterns: Vec<SsrPattern>, debug_snippet: Option<String>) -> 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 {