rust_analyzer/cli/
flags.rs

1//! Grammar for the command-line arguments.
2#![allow(unreachable_pub)]
3use std::{path::PathBuf, str::FromStr};
4
5use ide_ssr::{SsrPattern, SsrRule};
6
7use crate::cli::Verbosity;
8
9xflags::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 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 -V, --version
34
35            /// Dump a LSP config JSON schema.
36            optional --print-config-schema
37        }
38
39        /// Parse stdin.
40        cmd parse {
41            /// Suppress printing.
42            optional --no-dump
43        }
44
45        /// Parse stdin and print the list of symbols.
46        cmd symbols {}
47
48        /// Highlight stdin as html.
49        cmd highlight {
50            /// Enable rainbow highlighting of identifiers.
51            optional --rainbow
52        }
53
54        /// Batch typecheck project and print summary statistics
55        cmd analysis-stats {
56            /// Directory with Cargo.toml or rust-project.json.
57            required path: PathBuf
58
59            optional --output format: OutputFormat
60
61            /// Randomize order in which crates, modules, and items are processed.
62            optional --randomize
63            /// Run type inference in parallel.
64            optional --parallel
65
66            /// Only analyze items matching this path.
67            optional -o, --only path: String
68            /// Also analyze all dependencies.
69            optional --with-deps
70            /// Don't load sysroot crates (`std`, `core` & friends).
71            optional --no-sysroot
72            /// Don't set #[cfg(test)].
73            optional --no-test
74
75            /// Don't run build scripts or load `OUT_DIR` values by running `cargo check` before analysis.
76            optional --disable-build-scripts
77            /// Don't expand proc macros.
78            optional --disable-proc-macros
79            /// Run the proc-macro-srv binary at the specified path.
80            optional --proc-macro-srv path: PathBuf
81            /// Skip body lowering.
82            optional --skip-lowering
83            /// Skip type inference.
84            optional --skip-inference
85            /// Skip lowering to mir
86            optional --skip-mir-stats
87            /// Skip data layout calculation
88            optional --skip-data-layout
89            /// Skip const evaluation
90            optional --skip-const-eval
91            /// Runs several IDE features after analysis, including semantics highlighting, diagnostics
92            /// and annotations. This is useful for benchmarking the memory usage on a project that has
93            /// been worked on for a bit in a longer running session.
94            optional --run-all-ide-things
95            /// Run term search on all the tail expressions (of functions, block, if statements etc.)
96            optional --run-term-search
97            /// Validate term search by running `cargo check` on every response.
98            /// Note that this also temporarily modifies the files on disk, use with caution!
99            optional --validate-term-search
100        }
101
102        /// Run unit tests of the project using mir interpreter
103        cmd run-tests {
104            /// Directory with Cargo.toml or rust-project.json.
105            required path: PathBuf
106        }
107
108        /// Run unit tests of the project using mir interpreter
109        cmd rustc-tests {
110            /// Directory with Cargo.toml.
111            required rustc_repo: PathBuf
112
113            /// Only run tests with filter as substring
114            optional --filter path: String
115        }
116
117        cmd diagnostics {
118            /// Directory with Cargo.toml or rust-project.json.
119            required path: PathBuf
120
121            /// Don't run build scripts or load `OUT_DIR` values by running `cargo check` before analysis.
122            optional --disable-build-scripts
123            /// Don't expand proc macros.
124            optional --disable-proc-macros
125            /// Run the proc-macro-srv binary at the specified path.
126            optional --proc-macro-srv path: PathBuf
127        }
128
129        /// Report unresolved references
130        cmd unresolved-references {
131            /// Directory with Cargo.toml or rust-project.json.
132            required path: PathBuf
133
134            /// Don't run build scripts or load `OUT_DIR` values by running `cargo check` before analysis.
135            optional --disable-build-scripts
136            /// Don't expand proc macros.
137            optional --disable-proc-macros
138            /// Run the proc-macro-srv binary at the specified path.
139            optional --proc-macro-srv path: PathBuf
140        }
141
142        /// Prime caches, as rust-analyzer does typically at startup in interactive sessions.
143        cmd prime-caches {
144            /// Directory with Cargo.toml or rust-project.json.
145            required path: PathBuf
146
147            /// Don't run build scripts or load `OUT_DIR` values by running `cargo check` before analysis.
148            optional --disable-build-scripts
149            /// Don't expand proc macros.
150            optional --disable-proc-macros
151            /// Run the proc-macro-srv binary at the specified path.
152            optional --proc-macro-srv path: PathBuf
153            /// The number of threads to use. Defaults to the number of physical cores.
154            optional --num-threads num_threads: usize
155        }
156
157        cmd ssr {
158            /// A structured search replace rule (`$a.foo($b) ==>> bar($a, $b)`)
159            repeated rule: SsrRule
160        }
161
162        cmd search {
163            /// A structured search replace pattern (`$a.foo($b)`)
164            repeated pattern: SsrPattern
165            /// Prints debug information for any nodes with source exactly equal to snippet.
166            optional --debug snippet: String
167        }
168
169        cmd lsif {
170            required path: PathBuf
171
172            /// Exclude code from vendored libraries from the resulting index.
173            optional --exclude-vendored-libraries
174        }
175
176        cmd scip {
177            required path: PathBuf
178
179            /// The output path where the SCIP file will be written to. Defaults to `index.scip`.
180            optional --output path: PathBuf
181
182            /// A path to an json configuration file that can be used to customize cargo behavior.
183            optional --config-path config_path: PathBuf
184
185            /// Exclude code from vendored libraries from the resulting index.
186            optional --exclude-vendored-libraries
187        }
188    }
189}
190
191// generated start
192// The following code is generated by `xflags` macro.
193// Run `env UPDATE_XFLAGS=1 cargo build` to regenerate.
194#[derive(Debug)]
195pub struct RustAnalyzer {
196    pub verbose: u32,
197    pub quiet: bool,
198    pub log_file: Option<PathBuf>,
199    pub no_log_buffering: bool,
200    pub wait_dbg: bool,
201    pub subcommand: RustAnalyzerCmd,
202}
203
204#[derive(Debug)]
205pub enum RustAnalyzerCmd {
206    LspServer(LspServer),
207    Parse(Parse),
208    Symbols(Symbols),
209    Highlight(Highlight),
210    AnalysisStats(AnalysisStats),
211    RunTests(RunTests),
212    RustcTests(RustcTests),
213    Diagnostics(Diagnostics),
214    UnresolvedReferences(UnresolvedReferences),
215    PrimeCaches(PrimeCaches),
216    Ssr(Ssr),
217    Search(Search),
218    Lsif(Lsif),
219    Scip(Scip),
220}
221
222#[derive(Debug)]
223pub struct LspServer {
224    pub version: bool,
225    pub print_config_schema: bool,
226}
227
228#[derive(Debug)]
229pub struct Parse {
230    pub no_dump: bool,
231}
232
233#[derive(Debug)]
234pub struct Symbols;
235
236#[derive(Debug)]
237pub struct Highlight {
238    pub rainbow: bool,
239}
240
241#[derive(Debug)]
242pub struct AnalysisStats {
243    pub path: PathBuf,
244
245    pub output: Option<OutputFormat>,
246    pub randomize: bool,
247    pub parallel: bool,
248    pub only: Option<String>,
249    pub with_deps: bool,
250    pub no_sysroot: bool,
251    pub no_test: bool,
252    pub disable_build_scripts: bool,
253    pub disable_proc_macros: bool,
254    pub proc_macro_srv: Option<PathBuf>,
255    pub skip_lowering: bool,
256    pub skip_inference: bool,
257    pub skip_mir_stats: bool,
258    pub skip_data_layout: bool,
259    pub skip_const_eval: bool,
260    pub run_all_ide_things: bool,
261    pub run_term_search: bool,
262    pub validate_term_search: bool,
263}
264
265#[derive(Debug)]
266pub struct RunTests {
267    pub path: PathBuf,
268}
269
270#[derive(Debug)]
271pub struct RustcTests {
272    pub rustc_repo: PathBuf,
273
274    pub filter: Option<String>,
275}
276
277#[derive(Debug)]
278pub struct Diagnostics {
279    pub path: PathBuf,
280
281    pub disable_build_scripts: bool,
282    pub disable_proc_macros: bool,
283    pub proc_macro_srv: Option<PathBuf>,
284}
285
286#[derive(Debug)]
287pub struct UnresolvedReferences {
288    pub path: PathBuf,
289
290    pub disable_build_scripts: bool,
291    pub disable_proc_macros: bool,
292    pub proc_macro_srv: Option<PathBuf>,
293}
294
295#[derive(Debug)]
296pub struct PrimeCaches {
297    pub path: PathBuf,
298
299    pub disable_build_scripts: bool,
300    pub disable_proc_macros: bool,
301    pub proc_macro_srv: Option<PathBuf>,
302    pub num_threads: Option<usize>,
303}
304
305#[derive(Debug)]
306pub struct Ssr {
307    pub rule: Vec<SsrRule>,
308}
309
310#[derive(Debug)]
311pub struct Search {
312    pub pattern: Vec<SsrPattern>,
313
314    pub debug: Option<String>,
315}
316
317#[derive(Debug)]
318pub struct Lsif {
319    pub path: PathBuf,
320
321    pub exclude_vendored_libraries: bool,
322}
323
324#[derive(Debug)]
325pub struct Scip {
326    pub path: PathBuf,
327
328    pub output: Option<PathBuf>,
329    pub config_path: Option<PathBuf>,
330    pub exclude_vendored_libraries: bool,
331}
332
333impl RustAnalyzer {
334    #[allow(dead_code)]
335    pub fn from_env_or_exit() -> Self {
336        Self::from_env_or_exit_()
337    }
338
339    #[allow(dead_code)]
340    pub fn from_env() -> xflags::Result<Self> {
341        Self::from_env_()
342    }
343
344    #[allow(dead_code)]
345    pub fn from_vec(args: Vec<std::ffi::OsString>) -> xflags::Result<Self> {
346        Self::from_vec_(args)
347    }
348}
349// generated end
350
351#[derive(Debug, PartialEq, Eq)]
352pub enum OutputFormat {
353    Csv,
354}
355
356impl RustAnalyzer {
357    pub fn verbosity(&self) -> Verbosity {
358        if self.quiet {
359            return Verbosity::Quiet;
360        }
361        match self.verbose {
362            0 => Verbosity::Normal,
363            1 => Verbosity::Verbose,
364            _ => Verbosity::Spammy,
365        }
366    }
367}
368
369impl FromStr for OutputFormat {
370    type Err = String;
371
372    fn from_str(s: &str) -> Result<Self, Self::Err> {
373        match s {
374            "csv" => Ok(Self::Csv),
375            _ => Err(format!("unknown output format `{s}`")),
376        }
377    }
378}