]> git.lizzy.rs Git - rust.git/blob - src/tools/rust-analyzer/crates/rust-analyzer/src/cli/flags.rs
Rollup merge of #102044 - ChrisDenton:BCrypt-system-rand, r=thomcc
[rust.git] / src / tools / rust-analyzer / crates / rust-analyzer / src / cli / flags.rs
1 //! Grammar for the command-line arguments.
2 #![allow(unreachable_pub)]
3 use std::{path::PathBuf, str::FromStr};
4
5 use ide_ssr::{SsrPattern, SsrRule};
6
7 use crate::cli::Verbosity;
8
9 xflags::xflags! {
10     src "./src/cli/flags.rs"
11
12     /// LSP server for the Rust programming language.
13     ///
14     /// Subcommands and their flags do not provide any stability guarantees and may be removed or
15     /// changed without notice. Top-level flags that are not are marked as [Unstable] provide
16     /// backwards-compatibility and may be relied on.
17     cmd rust-analyzer {
18         /// Verbosity level, can be repeated multiple times.
19         repeated -v, --verbose
20         /// Verbosity level.
21         optional -q, --quiet
22
23         /// Log to the specified file instead of stderr.
24         optional --log-file path: PathBuf
25         /// Flush log records to the file immediately.
26         optional --no-log-buffering
27
28         /// [Unstable] Wait until a debugger is attached to (requires debug build).
29         optional --wait-dbg
30
31         default cmd lsp-server {
32             /// Print version.
33             optional --version
34             /// Print help.
35             optional -h, --help
36
37             /// Dump a LSP config JSON schema.
38             optional --print-config-schema
39         }
40
41         /// Parse stdin.
42         cmd parse {
43             /// Suppress printing.
44             optional --no-dump
45         }
46
47         /// Parse stdin and print the list of symbols.
48         cmd symbols {}
49
50         /// Highlight stdin as html.
51         cmd highlight {
52             /// Enable rainbow highlighting of identifiers.
53             optional --rainbow
54         }
55
56         /// Batch typecheck project and print summary statistics
57         cmd analysis-stats
58             /// Directory with Cargo.toml.
59             required path: PathBuf
60         {
61             optional --output format: OutputFormat
62
63             /// Randomize order in which crates, modules, and items are processed.
64             optional --randomize
65             /// Run type inference in parallel.
66             optional --parallel
67             /// Collect memory usage statistics.
68             optional --memory-usage
69             /// Print the total length of all source and macro files (whitespace is not counted).
70             optional --source-stats
71
72             /// Only analyze items matching this path.
73             optional -o, --only path: String
74             /// Also analyze all dependencies.
75             optional --with-deps
76             /// Don't load sysroot crates (`std`, `core` & friends).
77             optional --no-sysroot
78
79             /// Don't run build scripts or load `OUT_DIR` values by running `cargo check` before analysis.
80             optional --disable-build-scripts
81             /// Don't use expand proc macros.
82             optional --disable-proc-macros
83             /// Only resolve names, don't run type inference.
84             optional --skip-inference
85         }
86
87         cmd diagnostics
88             /// Directory with Cargo.toml.
89             required path: PathBuf
90         {
91             /// Don't run build scripts or load `OUT_DIR` values by running `cargo check` before analysis.
92             optional --disable-build-scripts
93             /// Don't use expand proc macros.
94             optional --disable-proc-macros
95         }
96
97         cmd ssr
98             /// A structured search replace rule (`$a.foo($b) ==> bar($a, $b)`)
99             repeated rule: SsrRule
100         {}
101
102         cmd search
103             /// A structured search replace pattern (`$a.foo($b)`)
104             repeated pattern: SsrPattern
105         {
106             /// Prints debug information for any nodes with source exactly equal to snippet.
107             optional --debug snippet: String
108         }
109
110         cmd proc-macro {}
111
112         cmd lsif
113             required path: PathBuf
114         {}
115
116         cmd scip
117             required path: PathBuf
118         {}
119     }
120 }
121
122 // generated start
123 // The following code is generated by `xflags` macro.
124 // Run `env UPDATE_XFLAGS=1 cargo build` to regenerate.
125 #[derive(Debug)]
126 pub struct RustAnalyzer {
127     pub verbose: u32,
128     pub quiet: bool,
129     pub log_file: Option<PathBuf>,
130     pub no_log_buffering: bool,
131     pub wait_dbg: bool,
132     pub subcommand: RustAnalyzerCmd,
133 }
134
135 #[derive(Debug)]
136 pub enum RustAnalyzerCmd {
137     LspServer(LspServer),
138     Parse(Parse),
139     Symbols(Symbols),
140     Highlight(Highlight),
141     AnalysisStats(AnalysisStats),
142     Diagnostics(Diagnostics),
143     Ssr(Ssr),
144     Search(Search),
145     ProcMacro(ProcMacro),
146     Lsif(Lsif),
147     Scip(Scip),
148 }
149
150 #[derive(Debug)]
151 pub struct LspServer {
152     pub version: bool,
153     pub help: bool,
154     pub print_config_schema: bool,
155 }
156
157 #[derive(Debug)]
158 pub struct Parse {
159     pub no_dump: bool,
160 }
161
162 #[derive(Debug)]
163 pub struct Symbols;
164
165 #[derive(Debug)]
166 pub struct Highlight {
167     pub rainbow: bool,
168 }
169
170 #[derive(Debug)]
171 pub struct AnalysisStats {
172     pub path: PathBuf,
173
174     pub output: Option<OutputFormat>,
175     pub randomize: bool,
176     pub parallel: bool,
177     pub memory_usage: bool,
178     pub source_stats: bool,
179     pub only: Option<String>,
180     pub with_deps: bool,
181     pub no_sysroot: bool,
182     pub disable_build_scripts: bool,
183     pub disable_proc_macros: bool,
184     pub skip_inference: bool,
185 }
186
187 #[derive(Debug)]
188 pub struct Diagnostics {
189     pub path: PathBuf,
190
191     pub disable_build_scripts: bool,
192     pub disable_proc_macros: bool,
193 }
194
195 #[derive(Debug)]
196 pub struct Ssr {
197     pub rule: Vec<SsrRule>,
198 }
199
200 #[derive(Debug)]
201 pub struct Search {
202     pub pattern: Vec<SsrPattern>,
203
204     pub debug: Option<String>,
205 }
206
207 #[derive(Debug)]
208 pub struct ProcMacro;
209
210 #[derive(Debug)]
211 pub struct Lsif {
212     pub path: PathBuf,
213 }
214
215 #[derive(Debug)]
216 pub struct Scip {
217     pub path: PathBuf,
218 }
219
220 impl RustAnalyzer {
221     pub const HELP: &'static str = Self::HELP_;
222
223     #[allow(dead_code)]
224     pub fn from_env() -> xflags::Result<Self> {
225         Self::from_env_()
226     }
227
228     #[allow(dead_code)]
229     pub fn from_vec(args: Vec<std::ffi::OsString>) -> xflags::Result<Self> {
230         Self::from_vec_(args)
231     }
232 }
233 // generated end
234
235 #[derive(Debug, PartialEq, Eq)]
236 pub enum OutputFormat {
237     Csv,
238 }
239
240 impl RustAnalyzer {
241     pub fn verbosity(&self) -> Verbosity {
242         if self.quiet {
243             return Verbosity::Quiet;
244         }
245         match self.verbose {
246             0 => Verbosity::Normal,
247             1 => Verbosity::Verbose,
248             _ => Verbosity::Spammy,
249         }
250     }
251 }
252
253 impl FromStr for OutputFormat {
254     type Err = String;
255
256     fn from_str(s: &str) -> Result<Self, Self::Err> {
257         match s {
258             "csv" => Ok(Self::Csv),
259             _ => Err(format!("unknown output format `{}`", s)),
260         }
261     }
262 }