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