This commit is contained in:
Aleksey Kladov 2020-08-26 13:06:43 +02:00
parent bb50614d88
commit ed6304131a

View File

@ -5,7 +5,7 @@
use std::{env, fmt::Write, path::PathBuf}; use std::{env, fmt::Write, path::PathBuf};
use anyhow::{bail, Result}; use anyhow::{bail, format_err, Result};
use pico_args::Arguments; use pico_args::Arguments;
use rust_analyzer::cli::{AnalysisStatsCmd, BenchCmd, BenchWhat, Position, Verbosity}; use rust_analyzer::cli::{AnalysisStatsCmd, BenchCmd, BenchWhat, Position, Verbosity};
use ssr::{SsrPattern, SsrRule}; use ssr::{SsrPattern, SsrRule};
@ -96,8 +96,6 @@ diagnostics <PATH>
ssr [RULE...] ssr [RULE...]
<RULE> A structured search replace rule (`$a.foo($b) ==> bar($a, $b)`) <RULE> A structured search replace rule (`$a.foo($b) ==> bar($a, $b)`)
--debug <snippet> Prints debug information for any nodes with source exactly
equal to <snippet>
search [PATTERN..] search [PATTERN..]
<PATTERN> A structured search replace pattern (`$a.foo($b)`) <PATTERN> A structured search replace pattern (`$a.foo($b)`)
@ -145,116 +143,81 @@ impl Args {
} }
}; };
let command = match subcommand.as_str() { let command = match subcommand.as_str() {
"parse" => { "parse" => Command::Parse { no_dump: matches.contains("--no-dump") },
let no_dump = matches.contains("--no-dump"); "symbols" => Command::Symbols,
matches.finish().or_else(handle_extra_flags)?; "highlight" => Command::Highlight { rainbow: matches.contains("--rainbow") },
Command::Parse { no_dump } "analysis-stats" => Command::AnalysisStats(AnalysisStatsCmd {
} randomize: matches.contains("--randomize"),
"symbols" => { parallel: matches.contains("--parallel"),
matches.finish().or_else(handle_extra_flags)?; memory_usage: matches.contains("--memory-usage"),
Command::Symbols only: matches.opt_value_from_str(["-o", "--only"])?,
} with_deps: matches.contains("--with-deps"),
"highlight" => { load_output_dirs: matches.contains("--load-output-dirs"),
let rainbow = matches.contains("--rainbow"); with_proc_macro: matches.contains("--with-proc-macro"),
matches.finish().or_else(handle_extra_flags)?; path: matches
Command::Highlight { rainbow } .free_from_str()?
} .ok_or_else(|| format_err!("expected positional argument"))?,
"analysis-stats" => { }),
let randomize = matches.contains("--randomize"); "analysis-bench" => Command::Bench(BenchCmd {
let parallel = matches.contains("--parallel"); what: {
let memory_usage = matches.contains("--memory-usage"); let highlight_path: Option<String> =
let only: Option<String> = matches.opt_value_from_str(["-o", "--only"])?; matches.opt_value_from_str("--highlight")?;
let with_deps: bool = matches.contains("--with-deps"); let complete_path: Option<Position> =
let load_output_dirs = matches.contains("--load-output-dirs"); matches.opt_value_from_str("--complete")?;
let with_proc_macro = matches.contains("--with-proc-macro"); let goto_def_path: Option<Position> =
let path = { matches.opt_value_from_str("--goto-def")?;
let mut trailing = matches.free()?; match (highlight_path, complete_path, goto_def_path) {
if trailing.len() != 1 { (Some(path), None, None) => {
bail!("Invalid flags"); let path = env::current_dir().unwrap().join(path);
} BenchWhat::Highlight { path: AbsPathBuf::assert(path) }
trailing.pop().unwrap().into() }
}; (None, Some(position), None) => BenchWhat::Complete(position),
(None, None, Some(position)) => BenchWhat::GotoDef(position),
Command::AnalysisStats(AnalysisStatsCmd { _ => panic!(
randomize, "exactly one of `--highlight`, `--complete` or `--goto-def` must be set"
parallel, ),
memory_usage, }
only, },
with_deps, memory_usage: matches.contains("--memory-usage"),
path, load_output_dirs: matches.contains("--load-output-dirs"),
load_output_dirs, with_proc_macro: matches.contains("--with-proc-macro"),
with_proc_macro, path: matches
}) .free_from_str()?
} .ok_or_else(|| format_err!("expected positional argument"))?,
"analysis-bench" => { }),
let highlight_path: Option<String> = matches.opt_value_from_str("--highlight")?; "diagnostics" => Command::Diagnostics {
let complete_path: Option<Position> = matches.opt_value_from_str("--complete")?; load_output_dirs: matches.contains("--load-output-dirs"),
let goto_def_path: Option<Position> = matches.opt_value_from_str("--goto-def")?; with_proc_macro: matches.contains("--with-proc-macro"),
let what = match (highlight_path, complete_path, goto_def_path) { path: matches
(Some(path), None, None) => { .free_from_str()?
let path = env::current_dir().unwrap().join(path); .ok_or_else(|| format_err!("expected positional argument"))?,
BenchWhat::Highlight { path: AbsPathBuf::assert(path) } },
}
(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"
),
};
let memory_usage = matches.contains("--memory-usage");
let load_output_dirs = matches.contains("--load-output-dirs");
let with_proc_macro = matches.contains("--with-proc-macro");
let path = {
let mut trailing = matches.free()?;
if trailing.len() != 1 {
bail!("Invalid flags");
}
trailing.pop().unwrap().into()
};
Command::Bench(BenchCmd {
memory_usage,
path,
what,
load_output_dirs,
with_proc_macro,
})
}
"diagnostics" => {
let load_output_dirs = matches.contains("--load-output-dirs");
let with_proc_macro = matches.contains("--with-proc-macro");
let path = {
let mut trailing = matches.free()?;
if trailing.len() != 1 {
bail!("Invalid flags");
}
trailing.pop().unwrap().into()
};
Command::Diagnostics { path, load_output_dirs, with_proc_macro }
}
"proc-macro" => Command::ProcMacro, "proc-macro" => Command::ProcMacro,
"ssr" => { "ssr" => Command::Ssr {
let mut rules = Vec::new(); rules: {
while let Some(rule) = matches.free_from_str()? { let mut acc = Vec::new();
rules.push(rule); while let Some(rule) = matches.free_from_str()? {
} acc.push(rule);
Command::Ssr { rules } }
} acc
"search" => { },
let debug_snippet = matches.opt_value_from_str("--debug")?; },
let mut patterns = Vec::new(); "search" => Command::StructuredSearch {
while let Some(rule) = matches.free_from_str()? { debug_snippet: matches.opt_value_from_str("--debug")?,
patterns.push(rule); patterns: {
} let mut acc = Vec::new();
Command::StructuredSearch { patterns, debug_snippet } while let Some(rule) = matches.free_from_str()? {
} acc.push(rule);
}
acc
},
},
_ => { _ => {
eprintln!("{}", HELP); eprintln!("{}", HELP);
return Ok(Args { verbosity, log_file: None, command: Command::Help }); return Ok(Args { verbosity, log_file: None, command: Command::Help });
} }
}; };
matches.finish().or_else(handle_extra_flags)?;
Ok(Args { verbosity, log_file, command }) Ok(Args { verbosity, log_file, command })
} }
} }