]> git.lizzy.rs Git - rust.git/blob - crates/rust-analyzer/src/cli.rs
Merge #9772
[rust.git] / crates / rust-analyzer / src / cli.rs
1 //! Various batch processing tasks, intended primarily for debugging.
2
3 pub mod load_cargo;
4 mod analysis_stats;
5 mod diagnostics;
6 mod progress_report;
7 mod ssr;
8
9 use std::io::Read;
10
11 use anyhow::Result;
12 use ide::{Analysis, AnalysisHost};
13 use syntax::{AstNode, SourceFile};
14 use vfs::Vfs;
15
16 pub use self::{
17     analysis_stats::AnalysisStatsCmd,
18     diagnostics::diagnostics,
19     ssr::{apply_ssr_rules, search_for_patterns},
20 };
21
22 #[derive(Clone, Copy)]
23 pub enum Verbosity {
24     Spammy,
25     Verbose,
26     Normal,
27     Quiet,
28 }
29
30 impl Verbosity {
31     pub fn is_verbose(self) -> bool {
32         matches!(self, Verbosity::Verbose | Verbosity::Spammy)
33     }
34     pub fn is_spammy(self) -> bool {
35         matches!(self, Verbosity::Spammy)
36     }
37 }
38
39 pub fn parse(no_dump: bool) -> Result<()> {
40     let _p = profile::span("parsing");
41     let file = file()?;
42     if !no_dump {
43         println!("{:#?}", file.syntax());
44     }
45     std::mem::forget(file);
46     Ok(())
47 }
48
49 pub fn symbols() -> Result<()> {
50     let text = read_stdin()?;
51     let (analysis, file_id) = Analysis::from_single_file(text);
52     let structure = analysis.file_structure(file_id).unwrap();
53     for s in structure {
54         println!("{:?}", s);
55     }
56     Ok(())
57 }
58
59 pub fn highlight(rainbow: bool) -> Result<()> {
60     let (analysis, file_id) = Analysis::from_single_file(read_stdin()?);
61     let html = analysis.highlight_as_html(file_id, rainbow).unwrap();
62     println!("{}", html);
63     Ok(())
64 }
65
66 fn file() -> Result<SourceFile> {
67     let text = read_stdin()?;
68     Ok(SourceFile::parse(&text).tree())
69 }
70
71 fn read_stdin() -> Result<String> {
72     let mut buff = String::new();
73     std::io::stdin().read_to_string(&mut buff)?;
74     Ok(buff)
75 }
76
77 fn report_metric(metric: &str, value: u64, unit: &str) {
78     if std::env::var("RA_METRICS").is_err() {
79         return;
80     }
81     println!("METRIC:{}:{}:{}", metric, value, unit)
82 }
83
84 fn print_memory_usage(mut host: AnalysisHost, vfs: Vfs) {
85     let mut mem = host.per_query_memory_usage();
86
87     let before = profile::memory_usage();
88     drop(vfs);
89     let vfs = before.allocated - profile::memory_usage().allocated;
90     mem.push(("VFS".into(), vfs));
91
92     let before = profile::memory_usage();
93     drop(host);
94     mem.push(("Unaccounted".into(), before.allocated - profile::memory_usage().allocated));
95
96     mem.push(("Remaining".into(), profile::memory_usage().allocated));
97
98     for (name, bytes) in mem {
99         // NOTE: Not a debug print, so avoid going through the `eprintln` defined above.
100         eprintln!("{:>8} {}", bytes, name);
101     }
102 }