2019-02-09 18:27:11 +01:00
|
|
|
mod analysis_stats;
|
2019-06-16 19:19:38 +03:00
|
|
|
mod analysis_bench;
|
2019-02-09 18:27:11 +01:00
|
|
|
|
2019-06-15 13:24:02 +06:00
|
|
|
use std::{io::Read, error::Error};
|
2018-10-15 17:44:23 -04:00
|
|
|
|
2018-07-30 16:16:58 +03:00
|
|
|
use clap::{App, Arg, SubCommand};
|
2019-05-25 13:42:34 +03:00
|
|
|
use ra_ide_api::{file_structure, Analysis};
|
2019-03-30 13:25:53 +03:00
|
|
|
use ra_syntax::{SourceFile, TreeArc, AstNode};
|
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
|
|
|
|
2019-06-15 14:04:26 +06:00
|
|
|
type Result<T> = std::result::Result<T, Box<dyn Error + Send + Sync>>;
|
2018-07-30 16:16:58 +03:00
|
|
|
|
|
|
|
|
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)
|
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-05-25 16:23:58 +02:00
|
|
|
.subcommand(
|
|
|
|
|
SubCommand::with_name("highlight")
|
2019-05-26 11:56:31 +02:00
|
|
|
.arg(Arg::with_name("rainbow").short("r").long("rainbow")),
|
2019-05-25 16:23:58 +02:00
|
|
|
)
|
2019-02-09 18:27:11 +01:00
|
|
|
.subcommand(
|
2019-05-07 12:09:10 +02:00
|
|
|
SubCommand::with_name("analysis-stats")
|
2019-05-14 09:58:41 +03:00
|
|
|
.arg(Arg::with_name("verbose").short("v").long("verbose"))
|
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
|
|
|
)
|
2019-06-16 19:19:38 +03:00
|
|
|
.subcommand(
|
|
|
|
|
SubCommand::with_name("analysis-bench")
|
|
|
|
|
.arg(Arg::with_name("verbose").short("v").long("verbose"))
|
|
|
|
|
.arg(
|
|
|
|
|
Arg::with_name("highlight")
|
|
|
|
|
.long("highlight")
|
|
|
|
|
.takes_value(true)
|
|
|
|
|
.conflicts_with("complete")
|
|
|
|
|
.value_name("PATH")
|
|
|
|
|
.help("highlight this file"),
|
|
|
|
|
)
|
|
|
|
|
.arg(
|
|
|
|
|
Arg::with_name("complete")
|
|
|
|
|
.long("complete")
|
|
|
|
|
.takes_value(true)
|
|
|
|
|
.conflicts_with("highlight")
|
|
|
|
|
.value_name("PATH:LINE:COLUMN")
|
|
|
|
|
.help("compute completions at this location"),
|
|
|
|
|
)
|
|
|
|
|
.arg(Arg::with_name("path").value_name("PATH").help("project to analyze")),
|
|
|
|
|
)
|
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
|
|
|
}
|
2019-05-28 18:46:11 +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
|
|
|
}
|
2019-05-25 16:23:58 +02:00
|
|
|
("highlight", Some(matches)) => {
|
2019-05-25 13:42:34 +03:00
|
|
|
let (analysis, file_id) = Analysis::from_single_file(read_stdin()?);
|
2019-05-25 16:23:58 +02:00
|
|
|
let html = analysis.highlight_as_html(file_id, matches.is_present("rainbow")).unwrap();
|
2019-05-25 13:42:34 +03:00
|
|
|
println!("{}", html);
|
|
|
|
|
}
|
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-06-16 19:19:38 +03:00
|
|
|
analysis_stats::run(verbose, path.as_ref(), only)?;
|
|
|
|
|
}
|
|
|
|
|
("analysis-bench", Some(matches)) => {
|
|
|
|
|
let verbose = matches.is_present("verbose");
|
|
|
|
|
let path = matches.value_of("path").unwrap_or("");
|
|
|
|
|
let op = if let Some(path) = matches.value_of("highlight") {
|
|
|
|
|
analysis_bench::Op::Highlight { path: path.into() }
|
|
|
|
|
} else if let Some(path_line_col) = matches.value_of("complete") {
|
|
|
|
|
let (path_line, column) = rsplit_at_char(path_line_col, ':')?;
|
|
|
|
|
let (path, line) = rsplit_at_char(path_line, ':')?;
|
|
|
|
|
analysis_bench::Op::Complete {
|
|
|
|
|
path: path.into(),
|
|
|
|
|
line: line.parse()?,
|
|
|
|
|
column: column.parse()?,
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
panic!("either --highlight or --complete must be set")
|
|
|
|
|
};
|
|
|
|
|
analysis_bench::run(verbose, path.as_ref(), op)?;
|
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-05-28 18:46:11 +03:00
|
|
|
Ok(SourceFile::parse(&text).tree)
|
2018-07-30 16:16:58 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn read_stdin() -> Result<String> {
|
|
|
|
|
let mut buff = String::new();
|
2019-05-28 18:46:11 +03:00
|
|
|
std::io::stdin().read_to_string(&mut buff)?;
|
2018-07-30 16:16:58 +03:00
|
|
|
Ok(buff)
|
|
|
|
|
}
|
2019-06-16 19:19:38 +03:00
|
|
|
|
|
|
|
|
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..]))
|
|
|
|
|
}
|