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