diff options
author | Vincent Esche <[email protected]> | 2021-02-08 10:30:16 +0000 |
---|---|---|
committer | Vincent Esche <[email protected]> | 2021-02-08 10:30:16 +0000 |
commit | 8f461ffe8f65d6338929b5d5dc40b3e0a42577f0 (patch) | |
tree | d2238e7bb4f0c1d86eeb790bed957f7d222c8332 | |
parent | 6d9c13c7105f0d5287ccb76439214ec536cd1ea4 (diff) |
Consolidate `fn load_cargo(…)` parameters into `struct LoadCargoConfig { … }`
-rw-r--r-- | crates/rust-analyzer/src/cli/analysis_bench.rs | 19 | ||||
-rw-r--r-- | crates/rust-analyzer/src/cli/analysis_stats.rs | 18 | ||||
-rw-r--r-- | crates/rust-analyzer/src/cli/diagnostics.rs | 18 | ||||
-rw-r--r-- | crates/rust-analyzer/src/cli/load_cargo.rs | 27 | ||||
-rw-r--r-- | crates/rust-analyzer/src/cli/ssr.rs | 19 |
5 files changed, 70 insertions, 31 deletions
diff --git a/crates/rust-analyzer/src/cli/analysis_bench.rs b/crates/rust-analyzer/src/cli/analysis_bench.rs index f4068937e..6735b6388 100644 --- a/crates/rust-analyzer/src/cli/analysis_bench.rs +++ b/crates/rust-analyzer/src/cli/analysis_bench.rs | |||
@@ -16,7 +16,10 @@ use ide_db::{ | |||
16 | }; | 16 | }; |
17 | use vfs::AbsPathBuf; | 17 | use vfs::AbsPathBuf; |
18 | 18 | ||
19 | use crate::cli::{load_cargo::load_cargo, print_memory_usage, Verbosity}; | 19 | use crate::cli::{ |
20 | load_cargo::{load_cargo, LoadCargoConfig}, | ||
21 | print_memory_usage, Verbosity, | ||
22 | }; | ||
20 | 23 | ||
21 | pub struct BenchCmd { | 24 | pub struct BenchCmd { |
22 | pub path: PathBuf, | 25 | pub path: PathBuf, |
@@ -59,12 +62,14 @@ impl BenchCmd { | |||
59 | 62 | ||
60 | let start = Instant::now(); | 63 | let start = Instant::now(); |
61 | eprint!("loading: "); | 64 | eprint!("loading: "); |
62 | let (mut host, vfs) = load_cargo( | 65 | |
63 | &self.path, | 66 | let load_cargo_config = LoadCargoConfig { |
64 | &Default::default(), | 67 | cargo_config: Default::default(), |
65 | self.load_output_dirs, | 68 | load_out_dirs_from_check: self.load_output_dirs, |
66 | self.with_proc_macro, | 69 | with_proc_macro: self.with_proc_macro, |
67 | )?; | 70 | }; |
71 | |||
72 | let (mut host, vfs) = load_cargo(&self.path, &load_cargo_config)?; | ||
68 | eprintln!("{:?}\n", start.elapsed()); | 73 | eprintln!("{:?}\n", start.elapsed()); |
69 | 74 | ||
70 | let file_id = { | 75 | let file_id = { |
diff --git a/crates/rust-analyzer/src/cli/analysis_stats.rs b/crates/rust-analyzer/src/cli/analysis_stats.rs index f0852d125..3417af687 100644 --- a/crates/rust-analyzer/src/cli/analysis_stats.rs +++ b/crates/rust-analyzer/src/cli/analysis_stats.rs | |||
@@ -25,8 +25,10 @@ use stdx::format_to; | |||
25 | use syntax::AstNode; | 25 | use syntax::AstNode; |
26 | 26 | ||
27 | use crate::cli::{ | 27 | use crate::cli::{ |
28 | load_cargo::load_cargo, print_memory_usage, progress_report::ProgressReport, report_metric, | 28 | load_cargo::{load_cargo, LoadCargoConfig}, |
29 | Result, Verbosity, | 29 | print_memory_usage, |
30 | progress_report::ProgressReport, | ||
31 | report_metric, Result, Verbosity, | ||
30 | }; | 32 | }; |
31 | use profile::StopWatch; | 33 | use profile::StopWatch; |
32 | 34 | ||
@@ -57,12 +59,12 @@ impl AnalysisStatsCmd { | |||
57 | }; | 59 | }; |
58 | 60 | ||
59 | let mut db_load_sw = self.stop_watch(); | 61 | let mut db_load_sw = self.stop_watch(); |
60 | let (host, vfs) = load_cargo( | 62 | let load_cargo_config = LoadCargoConfig { |
61 | &self.path, | 63 | cargo_config: Default::default(), |
62 | &Default::default(), | 64 | load_out_dirs_from_check: self.load_output_dirs, |
63 | self.load_output_dirs, | 65 | with_proc_macro: self.with_proc_macro, |
64 | self.with_proc_macro, | 66 | }; |
65 | )?; | 67 | let (host, vfs) = load_cargo(&self.path, &load_cargo_config)?; |
66 | let db = host.raw_database(); | 68 | let db = host.raw_database(); |
67 | eprintln!("{:<20} {}", "Database loaded:", db_load_sw.elapsed()); | 69 | eprintln!("{:<20} {}", "Database loaded:", db_load_sw.elapsed()); |
68 | 70 | ||
diff --git a/crates/rust-analyzer/src/cli/diagnostics.rs b/crates/rust-analyzer/src/cli/diagnostics.rs index 6646e417c..c60374c24 100644 --- a/crates/rust-analyzer/src/cli/diagnostics.rs +++ b/crates/rust-analyzer/src/cli/diagnostics.rs | |||
@@ -10,7 +10,10 @@ use hir::{db::HirDatabase, Crate, Module}; | |||
10 | use ide::{DiagnosticsConfig, Severity}; | 10 | use ide::{DiagnosticsConfig, Severity}; |
11 | use ide_db::base_db::SourceDatabaseExt; | 11 | use ide_db::base_db::SourceDatabaseExt; |
12 | 12 | ||
13 | use crate::cli::{load_cargo::load_cargo, Result}; | 13 | use crate::cli::{ |
14 | load_cargo::{load_cargo, LoadCargoConfig}, | ||
15 | Result, | ||
16 | }; | ||
14 | 17 | ||
15 | fn all_modules(db: &dyn HirDatabase) -> Vec<Module> { | 18 | fn all_modules(db: &dyn HirDatabase) -> Vec<Module> { |
16 | let mut worklist: Vec<_> = | 19 | let mut worklist: Vec<_> = |
@@ -25,8 +28,17 @@ fn all_modules(db: &dyn HirDatabase) -> Vec<Module> { | |||
25 | modules | 28 | modules |
26 | } | 29 | } |
27 | 30 | ||
28 | pub fn diagnostics(path: &Path, load_output_dirs: bool, with_proc_macro: bool) -> Result<()> { | 31 | pub fn diagnostics( |
29 | let (host, _vfs) = load_cargo(path, &Default::default(), load_output_dirs, with_proc_macro)?; | 32 | path: &Path, |
33 | load_out_dirs_from_check: bool, | ||
34 | with_proc_macro: bool, | ||
35 | ) -> Result<()> { | ||
36 | let load_cargo_config = LoadCargoConfig { | ||
37 | cargo_config: Default::default(), | ||
38 | load_out_dirs_from_check, | ||
39 | with_proc_macro, | ||
40 | }; | ||
41 | let (host, _vfs) = load_cargo(path, &load_cargo_config)?; | ||
30 | let db = host.raw_database(); | 42 | let db = host.raw_database(); |
31 | let analysis = host.analysis(); | 43 | let analysis = host.analysis(); |
32 | 44 | ||
diff --git a/crates/rust-analyzer/src/cli/load_cargo.rs b/crates/rust-analyzer/src/cli/load_cargo.rs index 4669c9bc3..cc63c6cc2 100644 --- a/crates/rust-analyzer/src/cli/load_cargo.rs +++ b/crates/rust-analyzer/src/cli/load_cargo.rs | |||
@@ -13,15 +13,16 @@ use vfs::{loader::Handle, AbsPath, AbsPathBuf}; | |||
13 | 13 | ||
14 | use crate::reload::{ProjectFolders, SourceRootConfig}; | 14 | use crate::reload::{ProjectFolders, SourceRootConfig}; |
15 | 15 | ||
16 | pub fn load_cargo( | 16 | pub struct LoadCargoConfig { |
17 | root: &Path, | 17 | pub cargo_config: CargoConfig, |
18 | config: &CargoConfig, | 18 | pub load_out_dirs_from_check: bool, |
19 | load_out_dirs_from_check: bool, | 19 | pub with_proc_macro: bool, |
20 | with_proc_macro: bool, | 20 | } |
21 | ) -> Result<(AnalysisHost, vfs::Vfs)> { | 21 | |
22 | pub fn load_cargo(root: &Path, config: &LoadCargoConfig) -> Result<(AnalysisHost, vfs::Vfs)> { | ||
22 | let root = AbsPathBuf::assert(std::env::current_dir()?.join(root)); | 23 | let root = AbsPathBuf::assert(std::env::current_dir()?.join(root)); |
23 | let root = ProjectManifest::discover_single(&root)?; | 24 | let root = ProjectManifest::discover_single(&root)?; |
24 | let ws = ProjectWorkspace::load(root, config, &|_| {})?; | 25 | let ws = ProjectWorkspace::load(root, &config.cargo_config, &|_| {})?; |
25 | 26 | ||
26 | let (sender, receiver) = unbounded(); | 27 | let (sender, receiver) = unbounded(); |
27 | let mut vfs = vfs::Vfs::default(); | 28 | let mut vfs = vfs::Vfs::default(); |
@@ -31,14 +32,14 @@ pub fn load_cargo( | |||
31 | Box::new(loader) | 32 | Box::new(loader) |
32 | }; | 33 | }; |
33 | 34 | ||
34 | let proc_macro_client = if with_proc_macro { | 35 | let proc_macro_client = if config.with_proc_macro { |
35 | let path = std::env::current_exe()?; | 36 | let path = std::env::current_exe()?; |
36 | Some(ProcMacroClient::extern_process(path, &["proc-macro"]).unwrap()) | 37 | Some(ProcMacroClient::extern_process(path, &["proc-macro"]).unwrap()) |
37 | } else { | 38 | } else { |
38 | None | 39 | None |
39 | }; | 40 | }; |
40 | 41 | ||
41 | let build_data = if load_out_dirs_from_check { | 42 | let build_data = if config.load_out_dirs_from_check { |
42 | let mut collector = BuildDataCollector::default(); | 43 | let mut collector = BuildDataCollector::default(); |
43 | ws.collect_build_data_configs(&mut collector); | 44 | ws.collect_build_data_configs(&mut collector); |
44 | Some(collector.collect(&|_| {})?) | 45 | Some(collector.collect(&|_| {})?) |
@@ -117,7 +118,13 @@ mod tests { | |||
117 | #[test] | 118 | #[test] |
118 | fn test_loading_rust_analyzer() { | 119 | fn test_loading_rust_analyzer() { |
119 | let path = Path::new(env!("CARGO_MANIFEST_DIR")).parent().unwrap().parent().unwrap(); | 120 | let path = Path::new(env!("CARGO_MANIFEST_DIR")).parent().unwrap().parent().unwrap(); |
120 | let (host, _vfs) = load_cargo(path, &Default::default(), false, false).unwrap(); | 121 | let load_cargo_config = LoadCargoConfig { |
122 | cargo_config: Default::default(), | ||
123 | load_out_dirs_from_check: false, | ||
124 | with_proc_macro: false, | ||
125 | }; | ||
126 | |||
127 | let (host, _vfs) = load_cargo(path, &load_cargo_config).unwrap(); | ||
121 | let n_crates = Crate::all(host.raw_database()).len(); | 128 | let n_crates = Crate::all(host.raw_database()).len(); |
122 | // RA has quite a few crates, but the exact count doesn't matter | 129 | // RA has quite a few crates, but the exact count doesn't matter |
123 | assert!(n_crates > 20); | 130 | assert!(n_crates > 20); |
diff --git a/crates/rust-analyzer/src/cli/ssr.rs b/crates/rust-analyzer/src/cli/ssr.rs index 8ab43e0f7..8729ff0d9 100644 --- a/crates/rust-analyzer/src/cli/ssr.rs +++ b/crates/rust-analyzer/src/cli/ssr.rs | |||
@@ -1,11 +1,19 @@ | |||
1 | //! Applies structured search replace rules from the command line. | 1 | //! Applies structured search replace rules from the command line. |
2 | 2 | ||
3 | use crate::cli::{load_cargo::load_cargo, Result}; | 3 | use crate::cli::{ |
4 | load_cargo::{load_cargo, LoadCargoConfig}, | ||
5 | Result, | ||
6 | }; | ||
4 | use ssr::{MatchFinder, SsrPattern, SsrRule}; | 7 | use ssr::{MatchFinder, SsrPattern, SsrRule}; |
5 | 8 | ||
6 | pub fn apply_ssr_rules(rules: Vec<SsrRule>) -> Result<()> { | 9 | pub fn apply_ssr_rules(rules: Vec<SsrRule>) -> Result<()> { |
7 | use ide_db::base_db::SourceDatabaseExt; | 10 | use ide_db::base_db::SourceDatabaseExt; |
8 | let (host, vfs) = load_cargo(&std::env::current_dir()?, &Default::default(), true, true)?; | 11 | let load_cargo_config = LoadCargoConfig { |
12 | cargo_config: Default::default(), | ||
13 | load_out_dirs_from_check: true, | ||
14 | with_proc_macro: true, | ||
15 | }; | ||
16 | let (host, vfs) = load_cargo(&std::env::current_dir()?, &load_cargo_config)?; | ||
9 | let db = host.raw_database(); | 17 | let db = host.raw_database(); |
10 | let mut match_finder = MatchFinder::at_first_file(db)?; | 18 | let mut match_finder = MatchFinder::at_first_file(db)?; |
11 | for rule in rules { | 19 | for rule in rules { |
@@ -28,7 +36,12 @@ pub fn apply_ssr_rules(rules: Vec<SsrRule>) -> Result<()> { | |||
28 | pub fn search_for_patterns(patterns: Vec<SsrPattern>, debug_snippet: Option<String>) -> Result<()> { | 36 | pub fn search_for_patterns(patterns: Vec<SsrPattern>, debug_snippet: Option<String>) -> Result<()> { |
29 | use ide_db::base_db::SourceDatabaseExt; | 37 | use ide_db::base_db::SourceDatabaseExt; |
30 | use ide_db::symbol_index::SymbolsDatabase; | 38 | use ide_db::symbol_index::SymbolsDatabase; |
31 | let (host, _vfs) = load_cargo(&std::env::current_dir()?, &Default::default(), true, true)?; | 39 | let load_cargo_config = LoadCargoConfig { |
40 | cargo_config: Default::default(), | ||
41 | load_out_dirs_from_check: true, | ||
42 | with_proc_macro: true, | ||
43 | }; | ||
44 | let (host, _vfs) = load_cargo(&std::env::current_dir()?, &load_cargo_config)?; | ||
32 | let db = host.raw_database(); | 45 | let db = host.raw_database(); |
33 | let mut match_finder = MatchFinder::at_first_file(db)?; | 46 | let mut match_finder = MatchFinder::at_first_file(db)?; |
34 | for pattern in patterns { | 47 | for pattern in patterns { |