2018-07-30 08:16:58 -05:00
|
|
|
extern crate clap;
|
|
|
|
#[macro_use]
|
|
|
|
extern crate failure;
|
2018-09-18 16:46:10 -05:00
|
|
|
extern crate join_to_string;
|
2018-09-16 04:54:24 -05:00
|
|
|
extern crate ra_editor;
|
2018-10-15 16:44:23 -05:00
|
|
|
extern crate ra_syntax;
|
2018-07-30 08:16:58 -05:00
|
|
|
extern crate tools;
|
|
|
|
|
2018-10-15 16:44:23 -05:00
|
|
|
use std::{fs, io::Read, path::Path, time::Instant};
|
|
|
|
|
2018-07-30 08:16:58 -05:00
|
|
|
use clap::{App, Arg, SubCommand};
|
2018-09-18 16:46:10 -05:00
|
|
|
use join_to_string::join;
|
2018-10-15 16:44:23 -05:00
|
|
|
use ra_editor::{extend_selection, file_structure, syntax_tree};
|
2018-11-07 09:32:33 -06:00
|
|
|
use ra_syntax::{SourceFileNode, TextRange};
|
2018-07-30 08:16:58 -05:00
|
|
|
use tools::collect_tests;
|
|
|
|
|
|
|
|
type Result<T> = ::std::result::Result<T, failure::Error>;
|
|
|
|
|
|
|
|
fn main() -> Result<()> {
|
2018-09-16 04:54:24 -05:00
|
|
|
let matches = App::new("ra-cli")
|
2018-07-30 08:16:58 -05:00
|
|
|
.setting(clap::AppSettings::SubcommandRequiredElseHelp)
|
|
|
|
.subcommand(
|
|
|
|
SubCommand::with_name("render-test")
|
2018-07-31 07:40:40 -05: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 08:16:58 -05:00
|
|
|
)
|
2018-10-15 16:44:23 -05:00
|
|
|
.subcommand(SubCommand::with_name("parse").arg(Arg::with_name("no-dump").long("--no-dump")))
|
2018-08-05 11:06:14 -05:00
|
|
|
.subcommand(SubCommand::with_name("symbols"))
|
2018-10-15 16:44:23 -05:00
|
|
|
.subcommand(
|
|
|
|
SubCommand::with_name("extend-selection")
|
|
|
|
.arg(Arg::with_name("start"))
|
|
|
|
.arg(Arg::with_name("end")),
|
2018-09-18 16:46:10 -05:00
|
|
|
)
|
2018-07-30 08:16:58 -05:00
|
|
|
.get_matches();
|
|
|
|
match matches.subcommand() {
|
2018-08-06 19:50:40 -05:00
|
|
|
("parse", Some(matches)) => {
|
2018-08-05 11:06:14 -05:00
|
|
|
let start = Instant::now();
|
|
|
|
let file = file()?;
|
|
|
|
let elapsed = start.elapsed();
|
2018-08-06 19:50:40 -05:00
|
|
|
if !matches.is_present("no-dump") {
|
2018-08-10 09:49:45 -05:00
|
|
|
println!("{}", syntax_tree(&file));
|
2018-08-06 19:50:40 -05:00
|
|
|
}
|
2018-08-05 11:06:14 -05:00
|
|
|
eprintln!("parsing: {:?}", elapsed);
|
2018-08-08 13:14:18 -05:00
|
|
|
::std::mem::forget(file);
|
2018-08-05 11:06:14 -05:00
|
|
|
}
|
|
|
|
("symbols", _) => {
|
|
|
|
let file = file()?;
|
2018-08-14 03:20:09 -05:00
|
|
|
for s in file_structure(&file) {
|
2018-08-05 11:06:14 -05:00
|
|
|
println!("{:?}", s);
|
|
|
|
}
|
2018-07-31 07:40:40 -05:00
|
|
|
}
|
2018-07-30 08:16:58 -05: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);
|
|
|
|
}
|
2018-09-18 16:46:10 -05:00
|
|
|
("extend-selection", Some(matches)) => {
|
|
|
|
let start: u32 = matches.value_of("start").unwrap().parse()?;
|
|
|
|
let end: u32 = matches.value_of("end").unwrap().parse()?;
|
|
|
|
let file = file()?;
|
|
|
|
let sels = selections(&file, start, end);
|
|
|
|
println!("{}", sels)
|
|
|
|
}
|
2018-07-30 08:16:58 -05:00
|
|
|
_ => unreachable!(),
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2018-11-07 09:32:33 -06:00
|
|
|
fn file() -> Result<SourceFileNode> {
|
2018-07-30 08:16:58 -05:00
|
|
|
let text = read_stdin()?;
|
2018-11-07 09:32:33 -06:00
|
|
|
Ok(SourceFileNode::parse(&text))
|
2018-07-30 08:16:58 -05: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 08:32:27 -05:00
|
|
|
let test = tests.into_iter().find(|(start_line, t)| {
|
|
|
|
*start_line <= line && line <= *start_line + t.text.lines().count()
|
2018-07-30 08:16:58 -05:00
|
|
|
});
|
|
|
|
let test = match test {
|
|
|
|
None => bail!("No test found at line {} at {}", line, file.display()),
|
2018-07-30 08:32:27 -05:00
|
|
|
Some((_start_line, test)) => test,
|
2018-07-30 08:16:58 -05:00
|
|
|
};
|
2018-11-07 09:32:33 -06:00
|
|
|
let file = SourceFileNode::parse(&test.text);
|
2018-08-10 09:49:45 -05:00
|
|
|
let tree = syntax_tree(&file);
|
2018-07-30 08:16:58 -05:00
|
|
|
Ok((test.text, tree))
|
|
|
|
}
|
2018-09-18 16:46:10 -05:00
|
|
|
|
2018-11-07 09:32:33 -06:00
|
|
|
fn selections(file: &SourceFileNode, start: u32, end: u32) -> String {
|
2018-09-18 16:46:10 -05:00
|
|
|
let mut ranges = Vec::new();
|
|
|
|
let mut cur = Some(TextRange::from_to((start - 1).into(), (end - 1).into()));
|
|
|
|
while let Some(r) = cur {
|
|
|
|
ranges.push(r);
|
|
|
|
cur = extend_selection(&file, r);
|
|
|
|
}
|
2018-10-15 16:44:23 -05:00
|
|
|
let ranges = ranges
|
|
|
|
.iter()
|
2018-09-18 16:46:10 -05:00
|
|
|
.map(|r| (1 + u32::from(r.start()), 1 + u32::from(r.end())))
|
|
|
|
.map(|(s, e)| format!("({} {})", s, e));
|
|
|
|
join(ranges)
|
|
|
|
.separator(" ")
|
|
|
|
.surround_with("(", ")")
|
|
|
|
.to_string()
|
|
|
|
}
|