]> git.lizzy.rs Git - rust.git/blob - crates/rust-analyzer/src/cli.rs
Merge branch 'Veetaha-feat/sync-branch'
[rust.git] / crates / rust-analyzer / src / cli.rs
1 //! Various batch processing tasks, intended primarily for debugging.
2
3 mod load_cargo;
4 mod analysis_stats;
5 mod analysis_bench;
6 mod diagnostics;
7 mod progress_report;
8
9 use std::io::Read;
10
11 use anyhow::Result;
12 use ra_ide::{file_structure, Analysis};
13 use ra_prof::profile;
14 use ra_syntax::{AstNode, SourceFile};
15
16 pub use analysis_bench::{analysis_bench, BenchWhat, Position};
17 pub use analysis_stats::analysis_stats;
18 pub use diagnostics::diagnostics;
19 pub use load_cargo::load_cargo;
20
21 #[derive(Clone, Copy)]
22 pub enum Verbosity {
23     Spammy,
24     Verbose,
25     Normal,
26     Quiet,
27 }
28
29 impl Verbosity {
30     pub fn is_verbose(self) -> bool {
31         match self {
32             Verbosity::Verbose | Verbosity::Spammy => true,
33             _ => false,
34         }
35     }
36     pub fn is_spammy(self) -> bool {
37         match self {
38             Verbosity::Spammy => true,
39             _ => false,
40         }
41     }
42 }
43
44 pub fn parse(no_dump: bool) -> Result<()> {
45     let _p = profile("parsing");
46     let file = file()?;
47     if !no_dump {
48         println!("{:#?}", file.syntax());
49     }
50     std::mem::forget(file);
51     Ok(())
52 }
53
54 pub fn symbols() -> Result<()> {
55     let file = file()?;
56     for s in file_structure(&file) {
57         println!("{:?}", s);
58     }
59     Ok(())
60 }
61
62 pub fn highlight(rainbow: bool) -> Result<()> {
63     let (analysis, file_id) = Analysis::from_single_file(read_stdin()?);
64     let html = analysis.highlight_as_html(file_id, rainbow).unwrap();
65     println!("{}", html);
66     Ok(())
67 }
68
69 fn file() -> Result<SourceFile> {
70     let text = read_stdin()?;
71     Ok(SourceFile::parse(&text).tree())
72 }
73
74 fn read_stdin() -> Result<String> {
75     let mut buff = String::new();
76     std::io::stdin().read_to_string(&mut buff)?;
77     Ok(buff)
78 }