From 90e61ac75dcf4cdae41de4df41557f6ae5186a9a Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Mon, 17 Feb 2020 17:41:53 +0100 Subject: Sort deps --- crates/ra_lsp_server/Cargo.toml | 29 +++++++++++++++-------------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/crates/ra_lsp_server/Cargo.toml b/crates/ra_lsp_server/Cargo.toml index fdf81ed87..0066929c0 100644 --- a/crates/ra_lsp_server/Cargo.toml +++ b/crates/ra_lsp_server/Cargo.toml @@ -8,27 +8,28 @@ authors = ["rust-analyzer developers"] doctest = false [dependencies] -threadpool = "1.7.1" -relative-path = "1.0.0" -serde_json = "1.0.34" -serde = { version = "1.0.83", features = ["derive"] } crossbeam-channel = "0.4" +either = "1.5" +env_logger = { version = "0.7.1", default-features = false } +jod-thread = "0.1.0" log = "0.4.3" lsp-types = { version = "0.70.0", features = ["proposed"] } -rustc-hash = "1.0" parking_lot = "0.10.0" -jod-thread = "0.1.0" -ra_vfs = "0.5.0" -ra_syntax = { path = "../ra_syntax" } -ra_text_edit = { path = "../ra_text_edit" } -ra_ide = { path = "../ra_ide" } +relative-path = "1.0.0" +rustc-hash = "1.0" +serde = { version = "1.0.83", features = ["derive"] } +serde_json = "1.0.34" +threadpool = "1.7.1" + lsp-server = "0.3.0" -ra_project_model = { path = "../ra_project_model" } +ra_cargo_watch = { path = "../ra_cargo_watch" } +ra_ide = { path = "../ra_ide" } ra_prof = { path = "../ra_prof" } +ra_project_model = { path = "../ra_project_model" } +ra_syntax = { path = "../ra_syntax" } +ra_text_edit = { path = "../ra_text_edit" } +ra_vfs = "0.5.0" ra_vfs_glob = { path = "../ra_vfs_glob" } -env_logger = { version = "0.7.1", default-features = false } -ra_cargo_watch = { path = "../ra_cargo_watch" } -either = "1.5" [target.'cfg(windows)'.dependencies] winapi = "0.3" -- cgit v1.2.3 From 017f0e4e53dfb1fb1b8a1844cf99b253dd9dbd25 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Mon, 17 Feb 2020 18:02:10 +0100 Subject: Refactor arg parsing --- crates/ra_cli/src/main.rs | 284 +++++++++++++++++++++++++++------------------- 1 file changed, 169 insertions(+), 115 deletions(-) diff --git a/crates/ra_cli/src/main.rs b/crates/ra_cli/src/main.rs index 4a428faff..7479fbd80 100644 --- a/crates/ra_cli/src/main.rs +++ b/crates/ra_cli/src/main.rs @@ -14,6 +14,15 @@ use ra_syntax::{AstNode, SourceFile}; type Result = std::result::Result>; +fn main() -> Result<()> { + env_logger::try_init()?; + + let command = Command::from_args()?; + command.run()?; + + Ok(()) +} + #[derive(Clone, Copy)] pub enum Verbosity { Spammy, @@ -37,17 +46,40 @@ impl Verbosity { } } -fn main() -> Result<()> { - env_logger::try_init()?; +enum Command { + Parse { + no_dump: bool, + }, + Symbols, + Highlight { + rainbow: bool, + }, + Stats { + verbosity: Verbosity, + randomize: bool, + memory_usage: bool, + only: Option, + with_deps: bool, + path: String, + }, + Bench { + verbose: bool, + path: String, + op: analysis_bench::Op, + }, + HelpPrinted, +} - let mut matches = Arguments::from_env(); - let subcommand = matches.subcommand()?.unwrap_or_default(); +impl Command { + fn from_args() -> Result { + let mut matches = Arguments::from_env(); + let subcommand = matches.subcommand()?.unwrap_or_default(); - match subcommand.as_str() { - "parse" => { - if matches.contains(["-h", "--help"]) { - eprintln!( - "\ + let command = match subcommand.as_str() { + "parse" => { + if matches.contains(["-h", "--help"]) { + eprintln!( + "\ ra-cli-parse USAGE: @@ -56,24 +88,18 @@ USAGE: FLAGS: -h, --help Prints help inforamtion --no-dump" - ); - return Ok(()); - } - - let no_dump = matches.contains("--no-dump"); - matches.finish().or_else(handle_extra_flags)?; + ); + return Ok(Command::HelpPrinted); + } - let _p = profile("parsing"); - let file = file()?; - if !no_dump { - println!("{:#?}", file.syntax()); + let no_dump = matches.contains("--no-dump"); + matches.finish().or_else(handle_extra_flags)?; + Command::Parse { no_dump } } - std::mem::forget(file); - } - "symbols" => { - if matches.contains(["-h", "--help"]) { - eprintln!( - "\ + "symbols" => { + if matches.contains(["-h", "--help"]) { + eprintln!( + "\ ra-cli-symbols USAGE: @@ -81,21 +107,18 @@ USAGE: FLAGS: -h, --help Prints help inforamtion" - ); - return Ok(()); - } + ); + return Ok(Command::HelpPrinted); + } - matches.finish().or_else(handle_extra_flags)?; + matches.finish().or_else(handle_extra_flags)?; - let file = file()?; - for s in file_structure(&file) { - println!("{:?}", s); + Command::Symbols } - } - "highlight" => { - if matches.contains(["-h", "--help"]) { - eprintln!( - "\ + "highlight" => { + if matches.contains(["-h", "--help"]) { + eprintln!( + "\ ra-cli-highlight USAGE: @@ -104,21 +127,18 @@ USAGE: FLAGS: -h, --help Prints help information -r, --rainbow" - ); - return Ok(()); - } - - let rainbow_opt = matches.contains(["-r", "--rainbow"]); - matches.finish().or_else(handle_extra_flags)?; + ); + return Ok(Command::HelpPrinted); + } - let (analysis, file_id) = Analysis::from_single_file(read_stdin()?); - let html = analysis.highlight_as_html(file_id, rainbow_opt).unwrap(); - println!("{}", html); - } - "analysis-stats" => { - if matches.contains(["-h", "--help"]) { - eprintln!( - "\ + let rainbow = matches.contains(["-r", "--rainbow"]); + matches.finish().or_else(handle_extra_flags)?; + Command::Highlight { rainbow } + } + "analysis-stats" => { + if matches.contains(["-h", "--help"]) { + eprintln!( + "\ ra-cli-analysis-stats USAGE: @@ -135,47 +155,40 @@ OPTIONS: ARGS: " - ); - return Ok(()); - } - - 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 memory_usage = matches.contains("--memory-usage"); - let only: Option = matches.opt_value_from_str(["-o", "--only"])?; - let with_deps: bool = matches.contains("--with-deps"); - let path = { - let mut trailing = matches.free()?; - if trailing.len() != 1 { - Err("Invalid flags")?; + ); + return Ok(Command::HelpPrinted); } - trailing.pop().unwrap() - }; - - analysis_stats::run( - verbosity, - memory_usage, - path.as_ref(), - only.as_ref().map(String::as_ref), - with_deps, - randomize, - )?; - } - "analysis-bench" => { - if matches.contains(["-h", "--help"]) { - eprintln!( - "\ + + 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 memory_usage = matches.contains("--memory-usage"); + let only: Option = matches.opt_value_from_str(["-o", "--only"])?; + let with_deps: bool = matches.contains("--with-deps"); + let path = { + let mut trailing = matches.free()?; + if trailing.len() != 1 { + Err("Invalid flags")?; + } + trailing.pop().unwrap() + }; + + Command::Stats { verbosity, randomize, memory_usage, only, with_deps, path } + } + "analysis-bench" => { + if matches.contains(["-h", "--help"]) { + eprintln!( + "\ ra_cli-analysis-bench USAGE: @@ -191,29 +204,28 @@ OPTIONS: ARGS: Project to analyse" - ); - return Ok(()); - } + ); + return Ok(Command::HelpPrinted); + } - let verbose = matches.contains(["-v", "--verbose"]); - let path: String = 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()?), - _ => panic!( - "exactly one of `--highlight`, `--complete` or `--goto-def` must be set" - ), - }; - matches.finish().or_else(handle_extra_flags)?; - - analysis_bench::run(verbose, path.as_ref(), op)?; - } - _ => eprintln!( - "\ + let verbose = matches.contains(["-v", "--verbose"]); + let path: String = 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()?), + _ => panic!( + "exactly one of `--highlight`, `--complete` or `--goto-def` must be set" + ), + }; + Command::Bench { verbose, path, op } + } + _ => { + eprintln!( + "\ ra-cli USAGE: @@ -228,9 +240,51 @@ SUBCOMMANDS: highlight parse symbols" - ), + ); + return Ok(Command::HelpPrinted); + } + }; + Ok(command) + } + + fn run(self) -> Result<()> { + match self { + Command::Parse { no_dump } => { + let _p = profile("parsing"); + let file = file()?; + if !no_dump { + println!("{:#?}", file.syntax()); + } + std::mem::forget(file); + } + Command::Symbols => { + let file = file()?; + for s in file_structure(&file) { + println!("{:?}", s); + } + } + Command::Highlight { rainbow } => { + let (analysis, file_id) = Analysis::from_single_file(read_stdin()?); + let html = analysis.highlight_as_html(file_id, rainbow).unwrap(); + println!("{}", html); + } + Command::Stats { verbosity, randomize, memory_usage, only, with_deps, path } => { + analysis_stats::run( + verbosity, + memory_usage, + path.as_ref(), + only.as_ref().map(String::as_ref), + with_deps, + randomize, + )?; + } + Command::Bench { verbose, path, op } => { + analysis_bench::run(verbose, path.as_ref(), op)?; + } + Command::HelpPrinted => (), + } + Ok(()) } - Ok(()) } fn handle_extra_flags(e: pico_args::Error) -> Result<()> { -- cgit v1.2.3 From a51b2603f993dd02275d275d37bbd0a0f24dbe33 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Mon, 17 Feb 2020 18:02:59 +0100 Subject: Stronger Types --- crates/ra_cli/src/main.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/crates/ra_cli/src/main.rs b/crates/ra_cli/src/main.rs index 7479fbd80..42f0b3e17 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}; +use std::{error::Error, fmt::Write, io::Read, path::PathBuf}; use pico_args::Arguments; use ra_ide::{file_structure, Analysis}; @@ -60,11 +60,11 @@ enum Command { memory_usage: bool, only: Option, with_deps: bool, - path: String, + path: PathBuf, }, Bench { verbose: bool, - path: String, + path: PathBuf, op: analysis_bench::Op, }, HelpPrinted, @@ -180,7 +180,7 @@ ARGS: if trailing.len() != 1 { Err("Invalid flags")?; } - trailing.pop().unwrap() + trailing.pop().unwrap().into() }; Command::Stats { verbosity, randomize, memory_usage, only, with_deps, path } @@ -209,7 +209,7 @@ ARGS: } let verbose = matches.contains(["-v", "--verbose"]); - let path: String = 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 = 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")?; -- cgit v1.2.3 From fa482a9fee6596f9b3021b188a2120873d5bb0d3 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Mon, 17 Feb 2020 18:03:48 +0100 Subject: Move interesting stuff to main --- crates/ra_cli/src/main.rs | 75 ++++++++++++++++++++++------------------------- 1 file changed, 35 insertions(+), 40 deletions(-) diff --git a/crates/ra_cli/src/main.rs b/crates/ra_cli/src/main.rs index 42f0b3e17..f952d0f6c 100644 --- a/crates/ra_cli/src/main.rs +++ b/crates/ra_cli/src/main.rs @@ -18,7 +18,41 @@ fn main() -> Result<()> { env_logger::try_init()?; let command = Command::from_args()?; - command.run()?; + match command { + Command::Parse { no_dump } => { + let _p = profile("parsing"); + let file = file()?; + if !no_dump { + println!("{:#?}", file.syntax()); + } + std::mem::forget(file); + } + Command::Symbols => { + let file = file()?; + for s in file_structure(&file) { + println!("{:?}", s); + } + } + Command::Highlight { rainbow } => { + let (analysis, file_id) = Analysis::from_single_file(read_stdin()?); + let html = analysis.highlight_as_html(file_id, rainbow).unwrap(); + println!("{}", html); + } + Command::Stats { verbosity, randomize, memory_usage, only, with_deps, path } => { + analysis_stats::run( + verbosity, + memory_usage, + path.as_ref(), + only.as_ref().map(String::as_ref), + with_deps, + randomize, + )?; + } + Command::Bench { verbose, path, op } => { + analysis_bench::run(verbose, path.as_ref(), op)?; + } + Command::HelpPrinted => (), + } Ok(()) } @@ -246,45 +280,6 @@ SUBCOMMANDS: }; Ok(command) } - - fn run(self) -> Result<()> { - match self { - Command::Parse { no_dump } => { - let _p = profile("parsing"); - let file = file()?; - if !no_dump { - println!("{:#?}", file.syntax()); - } - std::mem::forget(file); - } - Command::Symbols => { - let file = file()?; - for s in file_structure(&file) { - println!("{:?}", s); - } - } - Command::Highlight { rainbow } => { - let (analysis, file_id) = Analysis::from_single_file(read_stdin()?); - let html = analysis.highlight_as_html(file_id, rainbow).unwrap(); - println!("{}", html); - } - Command::Stats { verbosity, randomize, memory_usage, only, with_deps, path } => { - analysis_stats::run( - verbosity, - memory_usage, - path.as_ref(), - only.as_ref().map(String::as_ref), - with_deps, - randomize, - )?; - } - Command::Bench { verbose, path, op } => { - analysis_bench::run(verbose, path.as_ref(), op)?; - } - Command::HelpPrinted => (), - } - Ok(()) - } } fn handle_extra_flags(e: pico_args::Error) -> Result<()> { -- cgit v1.2.3 From c818f5c65e67c93a55cbf7b1b6ded983d8f392b8 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Mon, 17 Feb 2020 18:05:45 +0100 Subject: Unify verbosity handling --- crates/ra_cli/src/analysis_bench.rs | 10 +++++----- crates/ra_cli/src/main.rs | 34 +++++++++++++++++----------------- 2 files changed, 22 insertions(+), 22 deletions(-) diff --git a/crates/ra_cli/src/analysis_bench.rs b/crates/ra_cli/src/analysis_bench.rs index 3f10ed400..83f95555f 100644 --- a/crates/ra_cli/src/analysis_bench.rs +++ b/crates/ra_cli/src/analysis_bench.rs @@ -13,7 +13,7 @@ use ra_db::{ }; 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 { path: PathBuf, @@ -41,7 +41,7 @@ pub(crate) enum Op { 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(); 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.highlight_as_html(file_id, false).unwrap() }); - if verbose { + if verbosity.is_verbose() { println!("\n{}", res); } } @@ -98,13 +98,13 @@ pub(crate) fn run(verbose: bool, path: &Path, op: Op) -> Result<()> { if is_completion { let res = do_work(&mut host, file_id, |analysis| analysis.completions(file_postion)); - if verbose { + if verbosity.is_verbose() { println!("\n{:#?}", res); } } else { let res = do_work(&mut host, file_id, |analysis| analysis.goto_definition(file_postion)); - if verbose { + if verbosity.is_verbose() { println!("\n{:#?}", res); } } diff --git a/crates/ra_cli/src/main.rs b/crates/ra_cli/src/main.rs index f952d0f6c..58fea2cd4 100644 --- a/crates/ra_cli/src/main.rs +++ b/crates/ra_cli/src/main.rs @@ -48,8 +48,8 @@ fn main() -> Result<()> { randomize, )?; } - Command::Bench { verbose, path, op } => { - analysis_bench::run(verbose, path.as_ref(), op)?; + Command::Bench { verbosity, path, op } => { + analysis_bench::run(verbosity, path.as_ref(), op)?; } Command::HelpPrinted => (), } @@ -97,7 +97,7 @@ enum Command { path: PathBuf, }, Bench { - verbose: bool, + verbosity: Verbosity, path: PathBuf, op: analysis_bench::Op, }, @@ -109,6 +109,19 @@ impl Command { let mut matches = Arguments::from_env(); 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() { "parse" => { if matches.contains(["-h", "--help"]) { @@ -193,18 +206,6 @@ ARGS: 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 memory_usage = matches.contains("--memory-usage"); let only: Option = matches.opt_value_from_str(["-o", "--only"])?; @@ -242,7 +243,6 @@ ARGS: return Ok(Command::HelpPrinted); } - let verbose = matches.contains(["-v", "--verbose"]); 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")?; @@ -255,7 +255,7 @@ ARGS: "exactly one of `--highlight`, `--complete` or `--goto-def` must be set" ), }; - Command::Bench { verbose, path, op } + Command::Bench { verbosity, path, op } } _ => { eprintln!( -- cgit v1.2.3 From 3ef916061b208ce6f746562bc732b4a437639505 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Mon, 17 Feb 2020 18:09:07 +0100 Subject: More precise types --- crates/ra_cli/src/main.rs | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/crates/ra_cli/src/main.rs b/crates/ra_cli/src/main.rs index 58fea2cd4..5302f6ed2 100644 --- a/crates/ra_cli/src/main.rs +++ b/crates/ra_cli/src/main.rs @@ -12,12 +12,15 @@ use ra_ide::{file_structure, Analysis}; use ra_prof::profile; use ra_syntax::{AstNode, SourceFile}; -type Result = std::result::Result>; +type Result> = std::result::Result; fn main() -> Result<()> { env_logger::try_init()?; - let command = Command::from_args()?; + let command = match Command::from_args()? { + Ok(it) => it, + Err(HelpPrinted) => return Ok(()), + }; match command { Command::Parse { no_dump } => { let _p = profile("parsing"); @@ -51,7 +54,6 @@ fn main() -> Result<()> { Command::Bench { verbosity, path, op } => { analysis_bench::run(verbosity, path.as_ref(), op)?; } - Command::HelpPrinted => (), } Ok(()) @@ -101,11 +103,12 @@ enum Command { path: PathBuf, op: analysis_bench::Op, }, - HelpPrinted, } +struct HelpPrinted; + impl Command { - fn from_args() -> Result { + fn from_args() -> Result> { let mut matches = Arguments::from_env(); let subcommand = matches.subcommand()?.unwrap_or_default(); @@ -136,7 +139,7 @@ FLAGS: -h, --help Prints help inforamtion --no-dump" ); - return Ok(Command::HelpPrinted); + return Ok(Err(HelpPrinted)); } let no_dump = matches.contains("--no-dump"); @@ -155,7 +158,7 @@ USAGE: FLAGS: -h, --help Prints help inforamtion" ); - return Ok(Command::HelpPrinted); + return Ok(Err(HelpPrinted)); } matches.finish().or_else(handle_extra_flags)?; @@ -175,7 +178,7 @@ FLAGS: -h, --help Prints help information -r, --rainbow" ); - return Ok(Command::HelpPrinted); + return Ok(Err(HelpPrinted)); } let rainbow = matches.contains(["-r", "--rainbow"]); @@ -203,7 +206,7 @@ OPTIONS: ARGS: " ); - return Ok(Command::HelpPrinted); + return Ok(Err(HelpPrinted)); } let randomize = matches.contains("--randomize"); @@ -240,7 +243,7 @@ OPTIONS: ARGS: Project to analyse" ); - return Ok(Command::HelpPrinted); + return Ok(Err(HelpPrinted)); } let path: PathBuf = matches.opt_value_from_str("--path")?.unwrap_or_default(); @@ -275,10 +278,10 @@ SUBCOMMANDS: parse symbols" ); - return Ok(Command::HelpPrinted); + return Ok(Err(HelpPrinted)); } }; - Ok(command) + Ok(Ok(command)) } } -- cgit v1.2.3 From 06e37b273ea6f50d4d8cceccd85e613a5840b4c0 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Mon, 17 Feb 2020 18:10:15 +0100 Subject: Better name --- crates/ra_cli/src/main.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/ra_cli/src/main.rs b/crates/ra_cli/src/main.rs index 5302f6ed2..f4eb0bd3c 100644 --- a/crates/ra_cli/src/main.rs +++ b/crates/ra_cli/src/main.rs @@ -17,7 +17,7 @@ type Result> = std::result::Result; fn main() -> Result<()> { env_logger::try_init()?; - let command = match Command::from_args()? { + let command = match Command::from_env_args()? { Ok(it) => it, Err(HelpPrinted) => return Ok(()), }; @@ -108,7 +108,7 @@ enum Command { struct HelpPrinted; impl Command { - fn from_args() -> Result> { + fn from_env_args() -> Result> { let mut matches = Arguments::from_env(); let subcommand = matches.subcommand()?.unwrap_or_default(); -- cgit v1.2.3 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(-) 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 From 2d1b3da5fb69d932c65884a361ec10d81e8a51d8 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Mon, 17 Feb 2020 18:19:25 +0100 Subject: Use anyhow --- Cargo.lock | 1 + crates/ra_cli/Cargo.toml | 1 + crates/ra_cli/src/analysis_bench.rs | 3 ++- crates/ra_cli/src/load_cargo.rs | 7 +++---- crates/ra_cli/src/main.rs | 18 +++++++++--------- 5 files changed, 16 insertions(+), 14 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 4ae611a6b..82f910c1b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -945,6 +945,7 @@ dependencies = [ name = "ra_cli" version = "0.1.0" dependencies = [ + "anyhow", "crossbeam-channel", "env_logger", "itertools", diff --git a/crates/ra_cli/Cargo.toml b/crates/ra_cli/Cargo.toml index 03494a809..ce88a76b1 100644 --- a/crates/ra_cli/Cargo.toml +++ b/crates/ra_cli/Cargo.toml @@ -13,6 +13,7 @@ log = "0.4.5" pico-args = "0.3.0" rand = { version = "0.7.0", features = ["small_rng"] } rustc-hash = "1.0" +anyhow = "1.0" hir = { path = "../ra_hir", package = "ra_hir" } hir_def = { path = "../ra_hir_def", package = "ra_hir_def" } diff --git a/crates/ra_cli/src/analysis_bench.rs b/crates/ra_cli/src/analysis_bench.rs index 3dc8dc691..91fc55fe2 100644 --- a/crates/ra_cli/src/analysis_bench.rs +++ b/crates/ra_cli/src/analysis_bench.rs @@ -2,6 +2,7 @@ use std::{path::Path, sync::Arc, time::Instant}; +use anyhow::format_err; use ra_db::{ salsa::{Database, Durability}, FileId, SourceDatabaseExt, @@ -39,7 +40,7 @@ pub(crate) fn run(verbosity: Verbosity, path: &Path, what: BenchWhat) -> Result< } None }) - .ok_or_else(|| format!("Can't find {:?}", path))? + .ok_or_else(|| format_err!("Can't find {}", path.display()))? }; match &what { diff --git a/crates/ra_cli/src/load_cargo.rs b/crates/ra_cli/src/load_cargo.rs index 2d6433f18..b9a4e6aba 100644 --- a/crates/ra_cli/src/load_cargo.rs +++ b/crates/ra_cli/src/load_cargo.rs @@ -1,8 +1,6 @@ //! FIXME: write short doc here -use std::{collections::HashSet, error::Error, path::Path}; - -use rustc_hash::FxHashMap; +use std::{collections::HashSet, path::Path}; use crossbeam_channel::{unbounded, Receiver}; use ra_db::{CrateGraph, FileId, SourceRootId}; @@ -10,8 +8,9 @@ use ra_ide::{AnalysisChange, AnalysisHost, FeatureFlags}; use ra_project_model::{get_rustc_cfg_options, PackageRoot, ProjectWorkspace}; use ra_vfs::{RootEntry, Vfs, VfsChange, VfsTask, Watch}; use ra_vfs_glob::RustPackageFilterBuilder; +use rustc_hash::FxHashMap; -type Result = std::result::Result>; +use anyhow::Result; fn vfs_file_to_id(f: ra_vfs::VfsFile) -> FileId { FileId(f.0) diff --git a/crates/ra_cli/src/main.rs b/crates/ra_cli/src/main.rs index 66258c860..4cf062f47 100644 --- a/crates/ra_cli/src/main.rs +++ b/crates/ra_cli/src/main.rs @@ -5,14 +5,14 @@ mod analysis_stats; mod analysis_bench; mod progress_report; -use std::{error::Error, fmt::Write, io::Read, path::PathBuf, str::FromStr}; +use std::{fmt::Write, io::Read, path::PathBuf, str::FromStr}; use pico_args::Arguments; use ra_ide::{file_structure, Analysis}; use ra_prof::profile; use ra_syntax::{AstNode, SourceFile}; -type Result> = std::result::Result; +use anyhow::{bail, format_err, Result}; fn main() -> Result<()> { env_logger::try_init()?; @@ -118,7 +118,7 @@ pub(crate) struct Position { } impl FromStr for Position { - type Err = Box; + type Err = anyhow::Error; fn from_str(s: &str) -> Result { let (path_line, column) = rsplit_at_char(s, ':')?; let (path, line) = rsplit_at_char(path_line, ':')?; @@ -127,7 +127,7 @@ impl FromStr for Position { } fn rsplit_at_char(s: &str, c: char) -> Result<(&str, &str)> { - let idx = s.rfind(':').ok_or_else(|| format!("no `{}` in {}", c, s))?; + let idx = s.rfind(c).ok_or_else(|| format_err!("no `{}` in {}", c, s))?; Ok((&s[..idx], &s[idx + 1..])) } @@ -143,12 +143,12 @@ impl Command { matches.contains(["-v", "--verbose"]), matches.contains(["-q", "--quiet"]), ) { - (true, _, true) => Err("Invalid flags: -q conflicts with -vv")?, + (true, _, true) => bail!("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")?, + (false, true, true) => bail!("Invalid flags: -q conflicts with -v"), }; let command = match subcommand.as_str() { @@ -242,7 +242,7 @@ ARGS: let path = { let mut trailing = matches.free()?; if trailing.len() != 1 { - Err("Invalid flags")?; + bail!("Invalid flags"); } trailing.pop().unwrap().into() }; @@ -318,9 +318,9 @@ fn handle_extra_flags(e: pico_args::Error) -> Result<()> { write!(&mut invalid_flags, "{}, ", flag)?; } let (invalid_flags, _) = invalid_flags.split_at(invalid_flags.len() - 2); - Err(format!("Invalid flags: {}", invalid_flags).into()) + bail!("Invalid flags: {}", invalid_flags); } else { - Err(e.to_string().into()) + bail!(e); } } -- cgit v1.2.3