From 8e86d12771c0d77dce82cf80671d4cf9383057c9 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Mon, 17 Feb 2020 18:15:19 +0100 Subject: Cleanup --- crates/ra_cli/src/analysis_bench.rs | 53 +++++----------------- crates/ra_cli/src/main.rs | 88 ++++++++++++++++++++++++------------- 2 files changed, 68 insertions(+), 73 deletions(-) (limited to 'crates/ra_cli/src') diff --git a/crates/ra_cli/src/analysis_bench.rs b/crates/ra_cli/src/analysis_bench.rs index 83f95555f..3dc8dc691 100644 --- a/crates/ra_cli/src/analysis_bench.rs +++ b/crates/ra_cli/src/analysis_bench.rs @@ -1,11 +1,6 @@ //! FIXME: write short doc here -use std::{ - path::{Path, PathBuf}, - str::FromStr, - sync::Arc, - time::Instant, -}; +use std::{path::Path, sync::Arc, time::Instant}; use ra_db::{ salsa::{Database, Durability}, @@ -13,35 +8,9 @@ use ra_db::{ }; use ra_ide::{Analysis, AnalysisChange, AnalysisHost, FilePosition, LineCol}; -use crate::{load_cargo::load_cargo, Result, Verbosity}; - -pub(crate) struct Position { - path: PathBuf, - line: u32, - column: u32, -} - -impl FromStr for Position { - type Err = Box; - fn from_str(s: &str) -> Result { - let (path_line, column) = rsplit_at_char(s, ':')?; - let (path, line) = rsplit_at_char(path_line, ':')?; - Ok(Position { path: path.into(), line: line.parse()?, column: column.parse()? }) - } -} - -fn rsplit_at_char(s: &str, c: char) -> Result<(&str, &str)> { - let idx = s.rfind(':').ok_or_else(|| format!("no `{}` in {}", c, s))?; - Ok((&s[..idx], &s[idx + 1..])) -} - -pub(crate) enum Op { - Highlight { path: PathBuf }, - Complete(Position), - GotoDef(Position), -} +use crate::{load_cargo::load_cargo, BenchWhat, Result, Verbosity}; -pub(crate) fn run(verbosity: Verbosity, path: &Path, op: Op) -> Result<()> { +pub(crate) fn run(verbosity: Verbosity, path: &Path, what: BenchWhat) -> Result<()> { ra_prof::init(); let start = Instant::now(); @@ -51,9 +20,9 @@ pub(crate) fn run(verbosity: Verbosity, path: &Path, op: Op) -> Result<()> { eprintln!("{:?}\n", start.elapsed()); let file_id = { - let path = match &op { - Op::Highlight { path } => path, - Op::Complete(pos) | Op::GotoDef(pos) => &pos.path, + let path = match &what { + BenchWhat::Highlight { path } => path, + BenchWhat::Complete(pos) | BenchWhat::GotoDef(pos) => &pos.path, }; let path = std::env::current_dir()?.join(path).canonicalize()?; roots @@ -73,8 +42,8 @@ pub(crate) fn run(verbosity: Verbosity, path: &Path, op: Op) -> Result<()> { .ok_or_else(|| format!("Can't find {:?}", path))? }; - match &op { - Op::Highlight { .. } => { + match &what { + BenchWhat::Highlight { .. } => { let res = do_work(&mut host, file_id, |analysis| { analysis.diagnostics(file_id).unwrap(); analysis.highlight_as_html(file_id, false).unwrap() @@ -83,9 +52,9 @@ pub(crate) fn run(verbosity: Verbosity, path: &Path, op: Op) -> Result<()> { println!("\n{}", res); } } - Op::Complete(pos) | Op::GotoDef(pos) => { - let is_completion = match op { - Op::Complete(..) => true, + BenchWhat::Complete(pos) | BenchWhat::GotoDef(pos) => { + let is_completion = match what { + BenchWhat::Complete(..) => true, _ => false, }; diff --git a/crates/ra_cli/src/main.rs b/crates/ra_cli/src/main.rs index f4eb0bd3c..66258c860 100644 --- a/crates/ra_cli/src/main.rs +++ b/crates/ra_cli/src/main.rs @@ -5,7 +5,7 @@ mod analysis_stats; mod analysis_bench; mod progress_report; -use std::{error::Error, fmt::Write, io::Read, path::PathBuf}; +use std::{error::Error, fmt::Write, io::Read, path::PathBuf, str::FromStr}; use pico_args::Arguments; use ra_ide::{file_structure, Analysis}; @@ -51,14 +51,37 @@ fn main() -> Result<()> { randomize, )?; } - Command::Bench { verbosity, path, op } => { - analysis_bench::run(verbosity, path.as_ref(), op)?; + Command::Bench { verbosity, path, what } => { + analysis_bench::run(verbosity, path.as_ref(), what)?; } } Ok(()) } +enum Command { + Parse { + no_dump: bool, + }, + Symbols, + Highlight { + rainbow: bool, + }, + Stats { + verbosity: Verbosity, + randomize: bool, + memory_usage: bool, + only: Option, + with_deps: bool, + path: PathBuf, + }, + Bench { + verbosity: Verbosity, + path: PathBuf, + what: BenchWhat, + }, +} + #[derive(Clone, Copy)] pub enum Verbosity { Spammy, @@ -82,27 +105,30 @@ impl Verbosity { } } -enum Command { - Parse { - no_dump: bool, - }, - Symbols, - Highlight { - rainbow: bool, - }, - Stats { - verbosity: Verbosity, - randomize: bool, - memory_usage: bool, - only: Option, - with_deps: bool, - path: PathBuf, - }, - Bench { - verbosity: Verbosity, - path: PathBuf, - op: analysis_bench::Op, - }, +enum BenchWhat { + Highlight { path: PathBuf }, + Complete(Position), + GotoDef(Position), +} + +pub(crate) struct Position { + path: PathBuf, + line: u32, + column: u32, +} + +impl FromStr for Position { + type Err = Box; + fn from_str(s: &str) -> Result { + let (path_line, column) = rsplit_at_char(s, ':')?; + let (path, line) = rsplit_at_char(path_line, ':')?; + Ok(Position { path: path.into(), line: line.parse()?, column: column.parse()? }) + } +} + +fn rsplit_at_char(s: &str, c: char) -> Result<(&str, &str)> { + let idx = s.rfind(':').ok_or_else(|| format!("no `{}` in {}", c, s))?; + Ok((&s[..idx], &s[idx + 1..])) } struct HelpPrinted; @@ -248,17 +274,17 @@ ARGS: let path: PathBuf = matches.opt_value_from_str("--path")?.unwrap_or_default(); let highlight_path: Option = matches.opt_value_from_str("--highlight")?; - let complete_path: Option = matches.opt_value_from_str("--complete")?; - let goto_def_path: Option = matches.opt_value_from_str("--goto-def")?; - let op = match (highlight_path, complete_path, goto_def_path) { - (Some(path), None, None) => analysis_bench::Op::Highlight { path: path.into() }, - (None, Some(position), None) => analysis_bench::Op::Complete(position.parse()?), - (None, None, Some(position)) => analysis_bench::Op::GotoDef(position.parse()?), + let complete_path: Option = matches.opt_value_from_str("--complete")?; + let goto_def_path: Option = matches.opt_value_from_str("--goto-def")?; + let what = match (highlight_path, complete_path, goto_def_path) { + (Some(path), None, None) => BenchWhat::Highlight { path: path.into() }, + (None, Some(position), None) => BenchWhat::Complete(position), + (None, None, Some(position)) => BenchWhat::GotoDef(position), _ => panic!( "exactly one of `--highlight`, `--complete` or `--goto-def` must be set" ), }; - Command::Bench { verbosity, path, op } + Command::Bench { verbosity, path, what } } _ => { eprintln!( -- cgit v1.2.3