aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_cli/src/main.rs
blob: 5285f1f28e901053e6f73d346f44be328097d716 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
mod analysis_stats;

use std::{fs, io::Read, path::Path, time::Instant};

use clap::{App, Arg, SubCommand};
use join_to_string::join;
use ra_ide_api::{Analysis, FileRange};
use ra_ide_api_light::file_structure;
use ra_syntax::{SourceFile, TextRange, TreeArc, AstNode};
use tools::collect_tests;
use flexi_logger::Logger;

type Result<T> = ::std::result::Result<T, failure::Error>;

fn main() -> Result<()> {
    Logger::with_env().start()?;
    let matches = App::new("ra-cli")
        .setting(clap::AppSettings::SubcommandRequiredElseHelp)
        .subcommand(
            SubCommand::with_name("render-test")
                .arg(Arg::with_name("line").long("--line").required(true).takes_value(true))
                .arg(Arg::with_name("file").long("--file").required(true).takes_value(true)),
        )
        .subcommand(SubCommand::with_name("parse").arg(Arg::with_name("no-dump").long("--no-dump")))
        .subcommand(SubCommand::with_name("symbols"))
        .subcommand(
            SubCommand::with_name("extend-selection")
                .arg(Arg::with_name("start"))
                .arg(Arg::with_name("end")),
        )
        .subcommand(
            SubCommand::with_name("analysis-stats").arg(Arg::with_name("verbose").short("v")),
        )
        .get_matches();
    match matches.subcommand() {
        ("parse", Some(matches)) => {
            let start = Instant::now();
            let file = file()?;
            let elapsed = start.elapsed();
            if !matches.is_present("no-dump") {
                println!("{}", file.syntax().debug_dump());
            }
            eprintln!("parsing: {:?}", elapsed);
            ::std::mem::forget(file);
        }
        ("symbols", _) => {
            let file = file()?;
            for s in file_structure(&file) {
                println!("{:?}", s);
            }
        }
        ("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);
        }
        ("extend-selection", Some(matches)) => {
            let start: u32 = matches.value_of("start").unwrap().parse()?;
            let end: u32 = matches.value_of("end").unwrap().parse()?;
            let text = read_stdin()?;
            let sels = selections(text, start, end);
            println!("{}", sels)
        }
        ("analysis-stats", Some(matches)) => {
            let verbose = matches.is_present("verbose");
            analysis_stats::run(verbose)?;
        }
        _ => unreachable!(),
    }
    Ok(())
}

fn file() -> Result<TreeArc<SourceFile>> {
    let text = read_stdin()?;
    Ok(SourceFile::parse(&text))
}

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);
    let test = tests.into_iter().find(|(start_line, t)| {
        *start_line <= line && line <= *start_line + t.text.lines().count()
    });
    let test = match test {
        None => failure::bail!("No test found at line {} at {}", line, file.display()),
        Some((_start_line, test)) => test,
    };
    let file = SourceFile::parse(&test.text);
    let tree = file.syntax().debug_dump();
    Ok((test.text, tree))
}

fn selections(text: String, start: u32, end: u32) -> String {
    let (analysis, file_id) = Analysis::from_single_file(text);
    let mut ranges = Vec::new();
    let mut range = TextRange::from_to((start - 1).into(), (end - 1).into());
    loop {
        ranges.push(range);
        let next = analysis.extend_selection(FileRange { file_id, range }).unwrap();
        if range == next {
            break;
        }
        range = next;
    }
    let ranges = ranges
        .iter()
        .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()
}