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

177 lines
6 KiB
Rust
Raw Normal View History

//! FIXME: write short doc here
mod analysis_stats;
mod analysis_bench;
2019-09-10 10:31:40 +00:00
mod help;
mod progress_report;
2019-09-10 10:31:40 +00:00
use std::{error::Error, fmt::Write, io::Read};
2019-09-10 10:31:40 +00:00
use pico_args::Arguments;
2019-11-27 18:32:33 +00:00
use ra_ide::{file_structure, Analysis};
2019-04-02 14:52:04 +00:00
use ra_prof::profile;
2019-07-19 10:16:30 +00:00
use ra_syntax::{AstNode, SourceFile};
2018-07-30 13:16:58 +00:00
2019-06-15 08:04:26 +00:00
type Result<T> = std::result::Result<T, Box<dyn Error + Send + Sync>>;
2018-07-30 13:16:58 +00:00
2019-09-12 08:45:33 +00:00
#[derive(Clone, Copy)]
pub enum Verbosity {
Spammy,
2019-09-12 08:45:33 +00:00
Verbose,
Normal,
Quiet,
}
impl Verbosity {
2020-01-13 16:27:06 +00:00
fn is_verbose(self) -> bool {
2019-09-12 08:45:33 +00:00
match self {
Verbosity::Verbose | Verbosity::Spammy => true,
_ => false,
}
}
fn is_spammy(self) -> bool {
match self {
Verbosity::Spammy => true,
2019-09-12 08:45:33 +00:00
_ => false,
}
}
}
2018-07-30 13:16:58 +00:00
fn main() -> Result<()> {
2019-11-30 00:20:48 +00:00
env_logger::try_init()?;
2019-09-10 10:31:40 +00:00
2019-09-10 15:17:11 +00:00
let subcommand = match std::env::args_os().nth(1) {
None => {
eprintln!("{}", help::GLOBAL_HELP);
return Ok(());
}
Some(s) => s,
};
let mut matches = Arguments::from_vec(std::env::args_os().skip(2).collect());
2019-09-10 10:31:40 +00:00
match &*subcommand.to_string_lossy() {
"parse" => {
if matches.contains(["-h", "--help"]) {
2019-09-10 15:17:11 +00:00
eprintln!("{}", help::PARSE_HELP);
2019-09-10 10:31:40 +00:00
return Ok(());
2019-09-10 15:17:11 +00:00
}
let no_dump = matches.contains("--no-dump");
matches.finish().or_else(handle_extra_flags)?;
2019-09-10 10:31:40 +00:00
2019-09-10 15:17:11 +00:00
let _p = profile("parsing");
let file = file()?;
if !no_dump {
println!("{:#?}", file.syntax());
2018-08-07 00:50:40 +00:00
}
2019-09-10 15:17:11 +00:00
std::mem::forget(file);
2018-08-05 16:06:14 +00:00
}
2019-09-10 10:31:40 +00:00
"symbols" => {
if matches.contains(["-h", "--help"]) {
2019-09-10 15:17:11 +00:00
eprintln!("{}", help::SYMBOLS_HELP);
2019-09-10 10:31:40 +00:00
return Ok(());
2019-09-10 15:17:11 +00:00
}
matches.finish().or_else(handle_extra_flags)?;
let file = file()?;
for s in file_structure(&file) {
println!("{:?}", s);
2018-08-05 16:06:14 +00:00
}
2018-07-31 12:40:40 +00:00
}
2019-09-10 10:31:40 +00:00
"highlight" => {
if matches.contains(["-h", "--help"]) {
2019-09-10 15:17:11 +00:00
eprintln!("{}", help::HIGHLIGHT_HELP);
2019-09-10 10:31:40 +00:00
return Ok(());
}
2019-09-10 15:17:11 +00:00
let rainbow_opt = matches.contains(["-r", "--rainbow"]);
matches.finish().or_else(handle_extra_flags)?;
let (analysis, file_id) = Analysis::from_single_file(read_stdin()?);
let html = analysis.highlight_as_html(file_id, rainbow_opt).unwrap();
println!("{}", html);
2019-05-25 10:42:34 +00:00
}
2019-09-10 10:31:40 +00:00
"analysis-stats" => {
if matches.contains(["-h", "--help"]) {
2019-09-10 15:17:11 +00:00
eprintln!("{}", help::ANALYSIS_STATS_HELP);
2019-09-10 10:31:40 +00:00
return Ok(());
}
2019-09-12 08:45:33 +00:00
let verbosity = match (
matches.contains(["-vv", "--spammy"]),
2019-09-12 08:45:33 +00:00
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")?,
2019-09-12 08:45:33 +00:00
};
let randomize = matches.contains("--randomize");
2019-09-10 15:17:11 +00:00
let memory_usage = matches.contains("--memory-usage");
2019-10-02 14:58:15 +00:00
let only: Option<String> = 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 {
eprintln!("{}", help::ANALYSIS_STATS_HELP);
Err("Invalid flags")?;
}
trailing.pop().unwrap()
};
2019-09-10 15:17:11 +00:00
analysis_stats::run(
2019-09-12 08:45:33 +00:00
verbosity,
2019-09-10 15:17:11 +00:00
memory_usage,
path.as_ref(),
only.as_ref().map(String::as_ref),
with_deps,
randomize,
2019-09-10 15:17:11 +00:00
)?;
}
2019-09-10 10:31:40 +00:00
"analysis-bench" => {
if matches.contains(["-h", "--help"]) {
2019-09-10 15:17:11 +00:00
eprintln!("{}", help::ANALYSIS_BENCH_HELP);
2019-09-10 10:31:40 +00:00
return Ok(());
}
2019-09-10 15:17:11 +00:00
let verbose = matches.contains(["-v", "--verbose"]);
2019-10-02 14:58:15 +00:00
let path: String = matches.opt_value_from_str("--path")?.unwrap_or_default();
2020-02-16 17:20:22 +00:00
let highlight_path: Option<String> = matches.opt_value_from_str("--highlight")?;
2020-02-16 17:17:35 +00:00
let complete_path: Option<String> = matches.opt_value_from_str("--complete")?;
2020-02-16 17:24:06 +00:00
let goto_def_path: Option<String> = 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"
),
2019-09-10 15:17:11 +00:00
};
matches.finish().or_else(handle_extra_flags)?;
analysis_bench::run(verbose, path.as_ref(), op)?;
}
2019-09-10 15:17:11 +00:00
_ => eprintln!("{}", help::GLOBAL_HELP),
2018-07-30 13:16:58 +00:00
}
Ok(())
}
2019-09-10 10:31:40 +00:00
fn handle_extra_flags(e: pico_args::Error) -> Result<()> {
if let pico_args::Error::UnusedArgsLeft(flags) = e {
let mut invalid_flags = String::new();
for flag in flags {
2019-09-10 15:17:11 +00:00
write!(&mut invalid_flags, "{}, ", flag)?;
2019-09-10 10:31:40 +00:00
}
let (invalid_flags, _) = invalid_flags.split_at(invalid_flags.len() - 2);
Err(format!("Invalid flags: {}", invalid_flags).into())
} else {
Err(e.to_string().into())
}
}
2019-07-19 10:16:30 +00:00
fn file() -> Result<SourceFile> {
2018-07-30 13:16:58 +00:00
let text = read_stdin()?;
2019-07-19 10:16:30 +00:00
Ok(SourceFile::parse(&text).tree())
2018-07-30 13:16:58 +00:00
}
fn read_stdin() -> Result<String> {
let mut buff = String::new();
2019-05-28 15:46:11 +00:00
std::io::stdin().read_to_string(&mut buff)?;
2018-07-30 13:16:58 +00:00
Ok(buff)
}