2019-02-09 18:27:11 +01:00
|
|
|
mod analysis_stats;
|
|
|
|
|
|
2019-04-02 17:52:04 +03:00
|
|
|
use std::{fs, io::Read, path::Path};
|
2018-10-15 17:44:23 -04:00
|
|
|
|
2018-07-30 16:16:58 +03:00
|
|
|
use clap::{App, Arg, SubCommand};
|
2019-03-22 15:54:26 +03:00
|
|
|
use ra_ide_api::file_structure;
|
2019-03-30 13:25:53 +03:00
|
|
|
use ra_syntax::{SourceFile, TreeArc, AstNode};
|
2018-07-30 16:16:58 +03:00
|
|
|
use tools::collect_tests;
|
2019-02-09 18:27:11 +01:00
|
|
|
use flexi_logger::Logger;
|
2019-04-02 17:52:04 +03:00
|
|
|
use ra_prof::profile;
|
2018-07-30 16:16:58 +03:00
|
|
|
|
|
|
|
|
type Result<T> = ::std::result::Result<T, failure::Error>;
|
|
|
|
|
|
|
|
|
|
fn main() -> Result<()> {
|
2019-02-09 18:27:11 +01:00
|
|
|
Logger::with_env().start()?;
|
2018-09-16 12:54:24 +03:00
|
|
|
let matches = App::new("ra-cli")
|
2018-07-30 16:16:58 +03:00
|
|
|
.setting(clap::AppSettings::SubcommandRequiredElseHelp)
|
|
|
|
|
.subcommand(
|
|
|
|
|
SubCommand::with_name("render-test")
|
2019-02-08 14:49:43 +03:00
|
|
|
.arg(Arg::with_name("line").long("--line").required(true).takes_value(true))
|
|
|
|
|
.arg(Arg::with_name("file").long("--file").required(true).takes_value(true)),
|
2018-07-30 16:16:58 +03:00
|
|
|
)
|
2018-10-15 17:44:23 -04:00
|
|
|
.subcommand(SubCommand::with_name("parse").arg(Arg::with_name("no-dump").long("--no-dump")))
|
2018-08-05 19:06:14 +03:00
|
|
|
.subcommand(SubCommand::with_name("symbols"))
|
2019-02-09 18:27:11 +01:00
|
|
|
.subcommand(
|
2019-05-07 12:09:10 +02:00
|
|
|
SubCommand::with_name("analysis-stats")
|
|
|
|
|
.arg(Arg::with_name("verbose").short("v"))
|
2019-05-12 20:54:44 +03:00
|
|
|
.arg(Arg::with_name("only").short("o").takes_value(true))
|
|
|
|
|
.arg(Arg::with_name("path")),
|
2019-02-09 18:27:11 +01:00
|
|
|
)
|
2018-07-30 16:16:58 +03:00
|
|
|
.get_matches();
|
|
|
|
|
match matches.subcommand() {
|
2018-08-07 03:50:40 +03:00
|
|
|
("parse", Some(matches)) => {
|
2019-04-02 17:52:04 +03:00
|
|
|
let _p = profile("parsing");
|
2018-08-05 19:06:14 +03:00
|
|
|
let file = file()?;
|
2018-08-07 03:50:40 +03:00
|
|
|
if !matches.is_present("no-dump") {
|
2019-02-21 15:51:22 +03:00
|
|
|
println!("{}", file.syntax().debug_dump());
|
2018-08-07 03:50:40 +03:00
|
|
|
}
|
2018-08-08 21:14:18 +03:00
|
|
|
::std::mem::forget(file);
|
2018-08-05 19:06:14 +03:00
|
|
|
}
|
|
|
|
|
("symbols", _) => {
|
|
|
|
|
let file = file()?;
|
2018-08-14 11:20:09 +03:00
|
|
|
for s in file_structure(&file) {
|
2018-08-05 19:06:14 +03:00
|
|
|
println!("{:?}", s);
|
|
|
|
|
}
|
2018-07-31 15:40:40 +03:00
|
|
|
}
|
2018-07-30 16:16:58 +03:00
|
|
|
("render-test", Some(matches)) => {
|
|
|
|
|
let file = matches.value_of("file").unwrap();
|
|
|
|
|
let file = Path::new(file);
|
|
|
|
|
let line: usize = matches.value_of("line").unwrap().parse()?;
|
|
|
|
|
let line = line - 1;
|
|
|
|
|
let (test, tree) = render_test(file, line)?;
|
|
|
|
|
println!("{}\n{}", test, tree);
|
|
|
|
|
}
|
2019-02-09 18:27:11 +01:00
|
|
|
("analysis-stats", Some(matches)) => {
|
|
|
|
|
let verbose = matches.is_present("verbose");
|
2019-05-12 20:54:44 +03:00
|
|
|
let path = matches.value_of("path").unwrap_or("");
|
2019-05-07 12:09:10 +02:00
|
|
|
let only = matches.value_of("only");
|
2019-05-12 20:54:44 +03:00
|
|
|
analysis_stats::run(verbose, path, only)?;
|
2019-02-09 18:27:11 +01:00
|
|
|
}
|
2018-07-30 16:16:58 +03:00
|
|
|
_ => unreachable!(),
|
|
|
|
|
}
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-11 19:59:06 +03:00
|
|
|
fn file() -> Result<TreeArc<SourceFile>> {
|
2018-07-30 16:16:58 +03:00
|
|
|
let text = read_stdin()?;
|
2019-01-07 17:06:54 +03:00
|
|
|
Ok(SourceFile::parse(&text))
|
2018-07-30 16:16:58 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn read_stdin() -> Result<String> {
|
|
|
|
|
let mut buff = String::new();
|
|
|
|
|
::std::io::stdin().read_to_string(&mut buff)?;
|
|
|
|
|
Ok(buff)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn render_test(file: &Path, line: usize) -> Result<(String, String)> {
|
|
|
|
|
let text = fs::read_to_string(file)?;
|
|
|
|
|
let tests = collect_tests(&text);
|
2018-07-30 16:32:27 +03:00
|
|
|
let test = tests.into_iter().find(|(start_line, t)| {
|
|
|
|
|
*start_line <= line && line <= *start_line + t.text.lines().count()
|
2018-07-30 16:16:58 +03:00
|
|
|
});
|
|
|
|
|
let test = match test {
|
2018-12-06 21:16:37 +03:00
|
|
|
None => failure::bail!("No test found at line {} at {}", line, file.display()),
|
2018-07-30 16:32:27 +03:00
|
|
|
Some((_start_line, test)) => test,
|
2018-07-30 16:16:58 +03:00
|
|
|
};
|
2019-01-07 17:06:54 +03:00
|
|
|
let file = SourceFile::parse(&test.text);
|
2019-02-21 15:51:22 +03:00
|
|
|
let tree = file.syntax().debug_dump();
|
2018-07-30 16:16:58 +03:00
|
|
|
Ok((test.text, tree))
|
|
|
|
|
}
|