Unify verbosity handling

This commit is contained in:
Aleksey Kladov 2020-02-17 18:05:45 +01:00
parent fa482a9fee
commit c818f5c65e
2 changed files with 22 additions and 22 deletions

View file

@ -13,7 +13,7 @@ use ra_db::{
}; };
use ra_ide::{Analysis, AnalysisChange, AnalysisHost, FilePosition, LineCol}; use ra_ide::{Analysis, AnalysisChange, AnalysisHost, FilePosition, LineCol};
use crate::{load_cargo::load_cargo, Result}; use crate::{load_cargo::load_cargo, Result, Verbosity};
pub(crate) struct Position { pub(crate) struct Position {
path: PathBuf, path: PathBuf,
@ -41,7 +41,7 @@ pub(crate) enum Op {
GotoDef(Position), GotoDef(Position),
} }
pub(crate) fn run(verbose: bool, path: &Path, op: Op) -> Result<()> { pub(crate) fn run(verbosity: Verbosity, path: &Path, op: Op) -> Result<()> {
ra_prof::init(); ra_prof::init();
let start = Instant::now(); let start = Instant::now();
@ -79,7 +79,7 @@ pub(crate) fn run(verbose: bool, path: &Path, op: Op) -> Result<()> {
analysis.diagnostics(file_id).unwrap(); analysis.diagnostics(file_id).unwrap();
analysis.highlight_as_html(file_id, false).unwrap() analysis.highlight_as_html(file_id, false).unwrap()
}); });
if verbose { if verbosity.is_verbose() {
println!("\n{}", res); println!("\n{}", res);
} }
} }
@ -98,13 +98,13 @@ pub(crate) fn run(verbose: bool, path: &Path, op: Op) -> Result<()> {
if is_completion { if is_completion {
let res = let res =
do_work(&mut host, file_id, |analysis| analysis.completions(file_postion)); do_work(&mut host, file_id, |analysis| analysis.completions(file_postion));
if verbose { if verbosity.is_verbose() {
println!("\n{:#?}", res); println!("\n{:#?}", res);
} }
} else { } else {
let res = let res =
do_work(&mut host, file_id, |analysis| analysis.goto_definition(file_postion)); do_work(&mut host, file_id, |analysis| analysis.goto_definition(file_postion));
if verbose { if verbosity.is_verbose() {
println!("\n{:#?}", res); println!("\n{:#?}", res);
} }
} }

View file

@ -48,8 +48,8 @@ fn main() -> Result<()> {
randomize, randomize,
)?; )?;
} }
Command::Bench { verbose, path, op } => { Command::Bench { verbosity, path, op } => {
analysis_bench::run(verbose, path.as_ref(), op)?; analysis_bench::run(verbosity, path.as_ref(), op)?;
} }
Command::HelpPrinted => (), Command::HelpPrinted => (),
} }
@ -97,7 +97,7 @@ enum Command {
path: PathBuf, path: PathBuf,
}, },
Bench { Bench {
verbose: bool, verbosity: Verbosity,
path: PathBuf, path: PathBuf,
op: analysis_bench::Op, op: analysis_bench::Op,
}, },
@ -109,6 +109,19 @@ impl Command {
let mut matches = Arguments::from_env(); let mut matches = Arguments::from_env();
let subcommand = matches.subcommand()?.unwrap_or_default(); let subcommand = matches.subcommand()?.unwrap_or_default();
let verbosity = match (
matches.contains(["-vv", "--spammy"]),
matches.contains(["-v", "--verbose"]),
matches.contains(["-q", "--quiet"]),
) {
(true, _, true) => Err("Invalid flags: -q conflicts with -vv")?,
(true, _, false) => Verbosity::Spammy,
(false, false, false) => Verbosity::Normal,
(false, false, true) => Verbosity::Quiet,
(false, true, false) => Verbosity::Verbose,
(false, true, true) => Err("Invalid flags: -q conflicts with -v")?,
};
let command = match subcommand.as_str() { let command = match subcommand.as_str() {
"parse" => { "parse" => {
if matches.contains(["-h", "--help"]) { if matches.contains(["-h", "--help"]) {
@ -193,18 +206,6 @@ ARGS:
return Ok(Command::HelpPrinted); return Ok(Command::HelpPrinted);
} }
let verbosity = match (
matches.contains(["-vv", "--spammy"]),
matches.contains(["-v", "--verbose"]),
matches.contains(["-q", "--quiet"]),
) {
(true, _, true) => Err("Invalid flags: -q conflicts with -vv")?,
(true, _, false) => Verbosity::Spammy,
(false, false, false) => Verbosity::Normal,
(false, false, true) => Verbosity::Quiet,
(false, true, false) => Verbosity::Verbose,
(false, true, true) => Err("Invalid flags: -q conflicts with -v")?,
};
let randomize = matches.contains("--randomize"); let randomize = matches.contains("--randomize");
let memory_usage = matches.contains("--memory-usage"); let memory_usage = matches.contains("--memory-usage");
let only: Option<String> = matches.opt_value_from_str(["-o", "--only"])?; let only: Option<String> = matches.opt_value_from_str(["-o", "--only"])?;
@ -242,7 +243,6 @@ ARGS:
return Ok(Command::HelpPrinted); return Ok(Command::HelpPrinted);
} }
let verbose = matches.contains(["-v", "--verbose"]);
let path: PathBuf = matches.opt_value_from_str("--path")?.unwrap_or_default(); let path: PathBuf = matches.opt_value_from_str("--path")?.unwrap_or_default();
let highlight_path: Option<String> = matches.opt_value_from_str("--highlight")?; let highlight_path: Option<String> = matches.opt_value_from_str("--highlight")?;
let complete_path: Option<String> = matches.opt_value_from_str("--complete")?; let complete_path: Option<String> = matches.opt_value_from_str("--complete")?;
@ -255,7 +255,7 @@ ARGS:
"exactly one of `--highlight`, `--complete` or `--goto-def` must be set" "exactly one of `--highlight`, `--complete` or `--goto-def` must be set"
), ),
}; };
Command::Bench { verbose, path, op } Command::Bench { verbosity, path, op }
} }
_ => { _ => {
eprintln!( eprintln!(