Skip to content

Commit cd9f863

Browse files
committed
Use CmdArgs pattern for bench & analysis stats
1 parent be49547 commit cd9f863

File tree

5 files changed

+305
-331
lines changed

5 files changed

+305
-331
lines changed

crates/rust-analyzer/src/bin/args.rs

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use std::{env, fmt::Write, path::PathBuf};
88
use anyhow::{bail, Result};
99
use pico_args::Arguments;
1010
use ra_ssr::{SsrPattern, SsrRule};
11-
use rust_analyzer::cli::{BenchWhat, Position, Verbosity};
11+
use rust_analyzer::cli::{AnalysisStatsCmd, BenchCmd, BenchWhat, Position, Verbosity};
1212
use vfs::AbsPathBuf;
1313

1414
pub(crate) struct Args {
@@ -24,23 +24,8 @@ pub(crate) enum Command {
2424
Highlight {
2525
rainbow: bool,
2626
},
27-
Stats {
28-
randomize: bool,
29-
parallel: bool,
30-
memory_usage: bool,
31-
only: Option<String>,
32-
with_deps: bool,
33-
path: PathBuf,
34-
load_output_dirs: bool,
35-
with_proc_macro: bool,
36-
},
37-
Bench {
38-
memory_usage: bool,
39-
path: PathBuf,
40-
what: BenchWhat,
41-
load_output_dirs: bool,
42-
with_proc_macro: bool,
43-
},
27+
AnalysisStats(AnalysisStatsCmd),
28+
Bench(BenchCmd),
4429
Diagnostics {
4530
path: PathBuf,
4631
load_output_dirs: bool,
@@ -199,7 +184,7 @@ ARGS:
199184
trailing.pop().unwrap().into()
200185
};
201186

202-
Command::Stats {
187+
Command::AnalysisStats(AnalysisStatsCmd {
203188
randomize,
204189
parallel,
205190
memory_usage,
@@ -208,7 +193,7 @@ ARGS:
208193
path,
209194
load_output_dirs,
210195
with_proc_macro,
211-
}
196+
})
212197
}
213198
"analysis-bench" => {
214199
if matches.contains(["-h", "--help"]) {
@@ -256,7 +241,13 @@ ARGS:
256241
let memory_usage = matches.contains("--memory-usage");
257242
let load_output_dirs = matches.contains("--load-output-dirs");
258243
let with_proc_macro = matches.contains("--with-proc-macro");
259-
Command::Bench { memory_usage, path, what, load_output_dirs, with_proc_macro }
244+
Command::Bench(BenchCmd {
245+
memory_usage,
246+
path,
247+
what,
248+
load_output_dirs,
249+
with_proc_macro,
250+
})
260251
}
261252
"diagnostics" => {
262253
if matches.contains(["-h", "--help"]) {

crates/rust-analyzer/src/bin/main.rs

Lines changed: 2 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -33,36 +33,8 @@ fn main() -> Result<()> {
3333
args::Command::Parse { no_dump } => cli::parse(no_dump)?,
3434
args::Command::Symbols => cli::symbols()?,
3535
args::Command::Highlight { rainbow } => cli::highlight(rainbow)?,
36-
args::Command::Stats {
37-
randomize,
38-
parallel,
39-
memory_usage,
40-
only,
41-
with_deps,
42-
path,
43-
load_output_dirs,
44-
with_proc_macro,
45-
} => cli::analysis_stats(
46-
args.verbosity,
47-
memory_usage,
48-
path.as_ref(),
49-
only.as_ref().map(String::as_ref),
50-
with_deps,
51-
randomize,
52-
parallel,
53-
load_output_dirs,
54-
with_proc_macro,
55-
)?,
56-
args::Command::Bench { memory_usage, path, what, load_output_dirs, with_proc_macro } => {
57-
cli::analysis_bench(
58-
args.verbosity,
59-
path.as_ref(),
60-
what,
61-
memory_usage,
62-
load_output_dirs,
63-
with_proc_macro,
64-
)?
65-
}
36+
args::Command::AnalysisStats(cmd) => cmd.run(args.verbosity)?,
37+
args::Command::Bench(cmd) => cmd.run(args.verbosity)?,
6638
args::Command::Diagnostics { path, load_output_dirs, with_proc_macro, all } => {
6739
cli::diagnostics(path.as_ref(), load_output_dirs, with_proc_macro, all)?
6840
}

crates/rust-analyzer/src/cli.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ use ra_ide::Analysis;
1414
use ra_prof::profile;
1515
use ra_syntax::{AstNode, SourceFile};
1616

17-
pub use analysis_bench::{analysis_bench, BenchWhat, Position};
18-
pub use analysis_stats::analysis_stats;
17+
pub use analysis_bench::{BenchCmd, BenchWhat, Position};
18+
pub use analysis_stats::AnalysisStatsCmd;
1919
pub use diagnostics::diagnostics;
2020
pub use load_cargo::load_cargo;
2121
pub use ssr::{apply_ssr_rules, search_for_patterns};

crates/rust-analyzer/src/cli/analysis_bench.rs

Lines changed: 60 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! Benchmark operations like highlighting or goto definition.
22
3-
use std::{env, path::Path, str::FromStr, sync::Arc, time::Instant};
3+
use std::{env, path::PathBuf, str::FromStr, sync::Arc, time::Instant};
44

55
use anyhow::{bail, format_err, Result};
66
use ra_db::{
@@ -15,6 +15,14 @@ use crate::{
1515
print_memory_usage,
1616
};
1717

18+
pub struct BenchCmd {
19+
pub path: PathBuf,
20+
pub what: BenchWhat,
21+
pub memory_usage: bool,
22+
pub load_output_dirs: bool,
23+
pub with_proc_macro: bool,
24+
}
25+
1826
pub enum BenchWhat {
1927
Highlight { path: AbsPathBuf },
2028
Complete(Position),
@@ -42,72 +50,68 @@ impl FromStr for Position {
4250
}
4351
}
4452

45-
pub fn analysis_bench(
46-
verbosity: Verbosity,
47-
path: &Path,
48-
what: BenchWhat,
49-
memory_usage: bool,
50-
load_output_dirs: bool,
51-
with_proc_macro: bool,
52-
) -> Result<()> {
53-
ra_prof::init();
54-
55-
let start = Instant::now();
56-
eprint!("loading: ");
57-
let (mut host, vfs) = load_cargo(path, load_output_dirs, with_proc_macro)?;
58-
eprintln!("{:?}\n", start.elapsed());
59-
60-
let file_id = {
61-
let path = match &what {
62-
BenchWhat::Highlight { path } => path,
63-
BenchWhat::Complete(pos) | BenchWhat::GotoDef(pos) => &pos.path,
64-
};
65-
let path = path.clone().into();
66-
vfs.file_id(&path).ok_or_else(|| format_err!("Can't find {}", path))?
67-
};
68-
69-
match &what {
70-
BenchWhat::Highlight { .. } => {
71-
let res = do_work(&mut host, file_id, |analysis| {
72-
analysis.diagnostics(file_id, true).unwrap();
73-
analysis.highlight_as_html(file_id, false).unwrap()
74-
});
75-
if verbosity.is_verbose() {
76-
println!("\n{}", res);
77-
}
78-
}
79-
BenchWhat::Complete(pos) | BenchWhat::GotoDef(pos) => {
80-
let is_completion = matches!(what, BenchWhat::Complete(..));
53+
impl BenchCmd {
54+
pub fn run(self, verbosity: Verbosity) -> Result<()> {
55+
ra_prof::init();
56+
57+
let start = Instant::now();
58+
eprint!("loading: ");
59+
let (mut host, vfs) = load_cargo(&self.path, self.load_output_dirs, self.with_proc_macro)?;
60+
eprintln!("{:?}\n", start.elapsed());
8161

82-
let offset = host
83-
.analysis()
84-
.file_line_index(file_id)?
85-
.offset(LineCol { line: pos.line - 1, col_utf16: pos.column });
86-
let file_position = FilePosition { file_id, offset };
62+
let file_id = {
63+
let path = match &self.what {
64+
BenchWhat::Highlight { path } => path,
65+
BenchWhat::Complete(pos) | BenchWhat::GotoDef(pos) => &pos.path,
66+
};
67+
let path = path.clone().into();
68+
vfs.file_id(&path).ok_or_else(|| format_err!("Can't find {}", path))?
69+
};
8770

88-
if is_completion {
89-
let options = CompletionConfig::default();
71+
match &self.what {
72+
BenchWhat::Highlight { .. } => {
9073
let res = do_work(&mut host, file_id, |analysis| {
91-
analysis.completions(&options, file_position)
74+
analysis.diagnostics(file_id, true).unwrap();
75+
analysis.highlight_as_html(file_id, false).unwrap()
9276
});
9377
if verbosity.is_verbose() {
94-
println!("\n{:#?}", res);
78+
println!("\n{}", res);
9579
}
96-
} else {
97-
let res =
98-
do_work(&mut host, file_id, |analysis| analysis.goto_definition(file_position));
99-
if verbosity.is_verbose() {
100-
println!("\n{:#?}", res);
80+
}
81+
BenchWhat::Complete(pos) | BenchWhat::GotoDef(pos) => {
82+
let is_completion = matches!(self.what, BenchWhat::Complete(..));
83+
84+
let offset = host
85+
.analysis()
86+
.file_line_index(file_id)?
87+
.offset(LineCol { line: pos.line - 1, col_utf16: pos.column });
88+
let file_position = FilePosition { file_id, offset };
89+
90+
if is_completion {
91+
let options = CompletionConfig::default();
92+
let res = do_work(&mut host, file_id, |analysis| {
93+
analysis.completions(&options, file_position)
94+
});
95+
if verbosity.is_verbose() {
96+
println!("\n{:#?}", res);
97+
}
98+
} else {
99+
let res = do_work(&mut host, file_id, |analysis| {
100+
analysis.goto_definition(file_position)
101+
});
102+
if verbosity.is_verbose() {
103+
println!("\n{:#?}", res);
104+
}
101105
}
102106
}
103107
}
104-
}
105108

106-
if memory_usage {
107-
print_memory_usage(host, vfs);
108-
}
109+
if self.memory_usage {
110+
print_memory_usage(host, vfs);
111+
}
109112

110-
Ok(())
113+
Ok(())
114+
}
111115
}
112116

113117
fn do_work<F: Fn(&Analysis) -> T, T>(host: &mut AnalysisHost, file_id: FileId, work: F) -> T {

0 commit comments

Comments
 (0)