Files
rust/crates/ra_cli/src/main.rs

116 lines
4.3 KiB
Rust
Raw Normal View History

mod analysis_stats;
mod analysis_bench;
2019-06-15 13:24:02 +06:00
use std::{io::Read, error::Error};
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};
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<()> {
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("parse").arg(Arg::with_name("no-dump").long("--no-dump")))
2018-08-05 19:06:14 +03:00
.subcommand(SubCommand::with_name("symbols"))
.subcommand(
SubCommand::with_name("highlight")
2019-05-26 11:56:31 +02:00
.arg(Arg::with_name("rainbow").short("r").long("rainbow")),
)
.subcommand(
SubCommand::with_name("analysis-stats")
.arg(Arg::with_name("verbose").short("v").long("verbose"))
.arg(Arg::with_name("only").short("o").takes_value(true))
.arg(Arg::with_name("path")),
)
.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
}
("highlight", Some(matches)) => {
2019-05-25 13:42:34 +03:00
let (analysis, file_id) = Analysis::from_single_file(read_stdin()?);
let html = analysis.highlight_as_html(file_id, matches.is_present("rainbow")).unwrap();
2019-05-25 13:42:34 +03:00
println!("{}", html);
}
("analysis-stats", Some(matches)) => {
let verbose = matches.is_present("verbose");
let path = matches.value_of("path").unwrap_or("");
let only = matches.value_of("only");
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)?;
}
2018-07-30 16:16:58 +03:00
_ => unreachable!(),
}
Ok(())
}
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)
}
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..]))
}