ra_ap_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.
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            /// Print the total length of all source and macro files (whitespace is not counted).
66            optional --source-stats
67
68            /// Only analyze items matching this path.
69            optional -o, --only path: String
70            /// Also analyze all dependencies.
71            optional --with-deps
72            /// Don't load sysroot crates (`std`, `core` & friends).
73            optional --no-sysroot
74            /// Don't set #[cfg(test)].
75            optional --no-test
76
77            /// Don't run build scripts or load `OUT_DIR` values by running `cargo check` before analysis.
78            optional --disable-build-scripts
79            /// Don't use expand proc macros.
80            optional --disable-proc-macros
81            /// Run the proc-macro-srv binary at the specified path.
82            optional --proc-macro-srv path: PathBuf
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.
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.
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 use 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
131        /// Report unresolved references
132        cmd unresolved-references {
133            /// Directory with Cargo.toml.
134            required path: PathBuf
135
136            /// Don't run build scripts or load `OUT_DIR` values by running `cargo check` before analysis.
137            optional --disable-build-scripts
138            /// Don't use expand proc macros.
139            optional --disable-proc-macros
140            /// Run the proc-macro-srv binary at the specified path.
141            optional --proc-macro-srv path: PathBuf
142        }
143
144        cmd ssr {
145            /// A structured search replace rule (`$a.foo($b) ==>> bar($a, $b)`)
146            repeated rule: SsrRule
147        }
148
149        cmd search {
150            /// A structured search replace pattern (`$a.foo($b)`)
151            repeated pattern: SsrPattern
152            /// Prints debug information for any nodes with source exactly equal to snippet.
153            optional --debug snippet: String
154        }
155
156        cmd lsif {
157            required path: PathBuf
158
159            /// Exclude code from vendored libraries from the resulting index.
160            optional --exclude-vendored-libraries
161        }
162
163        cmd scip {
164            required path: PathBuf
165
166            /// The output path where the SCIP file will be written to. Defaults to `index.scip`.
167            optional --output path: PathBuf
168
169            /// A path to an json configuration file that can be used to customize cargo behavior.
170            optional --config-path config_path: PathBuf
171
172            /// Exclude code from vendored libraries from the resulting index.
173            optional --exclude-vendored-libraries
174        }
175    }
176}
177
178// generated start
179// The following code is generated by `xflags` macro.
180// Run `env UPDATE_XFLAGS=1 cargo build` to regenerate.
181#[derive(Debug)]
182pub struct RustAnalyzer {
183    pub verbose: u32,
184    pub quiet: bool,
185    pub log_file: Option<PathBuf>,
186    pub no_log_buffering: bool,
187    pub wait_dbg: bool,
188    pub subcommand: RustAnalyzerCmd,
189}
190
191#[derive(Debug)]
192pub enum RustAnalyzerCmd {
193    LspServer(LspServer),
194    Parse(Parse),
195    Symbols(Symbols),
196    Highlight(Highlight),
197    AnalysisStats(AnalysisStats),
198    RunTests(RunTests),
199    RustcTests(RustcTests),
200    Diagnostics(Diagnostics),
201    UnresolvedReferences(UnresolvedReferences),
202    Ssr(Ssr),
203    Search(Search),
204    Lsif(Lsif),
205    Scip(Scip),
206}
207
208#[derive(Debug)]
209pub struct LspServer {
210    pub version: bool,
211    pub print_config_schema: bool,
212}
213
214#[derive(Debug)]
215pub struct Parse {
216    pub no_dump: bool,
217}
218
219#[derive(Debug)]
220pub struct Symbols;
221
222#[derive(Debug)]
223pub struct Highlight {
224    pub rainbow: bool,
225}
226
227#[derive(Debug)]
228pub struct AnalysisStats {
229    pub path: PathBuf,
230
231    pub output: Option<OutputFormat>,
232    pub randomize: bool,
233    pub parallel: bool,
234    pub source_stats: bool,
235    pub only: Option<String>,
236    pub with_deps: bool,
237    pub no_sysroot: bool,
238    pub no_test: bool,
239    pub disable_build_scripts: bool,
240    pub disable_proc_macros: bool,
241    pub proc_macro_srv: Option<PathBuf>,
242    pub skip_lowering: bool,
243    pub skip_inference: bool,
244    pub skip_mir_stats: bool,
245    pub skip_data_layout: bool,
246    pub skip_const_eval: bool,
247    pub run_all_ide_things: bool,
248    pub run_term_search: bool,
249    pub validate_term_search: bool,
250}
251
252#[derive(Debug)]
253pub struct RunTests {
254    pub path: PathBuf,
255}
256
257#[derive(Debug)]
258pub struct RustcTests {
259    pub rustc_repo: PathBuf,
260
261    pub filter: Option<String>,
262}
263
264#[derive(Debug)]
265pub struct Diagnostics {
266    pub path: PathBuf,
267
268    pub disable_build_scripts: bool,
269    pub disable_proc_macros: bool,
270    pub proc_macro_srv: Option<PathBuf>,
271}
272
273#[derive(Debug)]
274pub struct UnresolvedReferences {
275    pub path: PathBuf,
276
277    pub disable_build_scripts: bool,
278    pub disable_proc_macros: bool,
279    pub proc_macro_srv: Option<PathBuf>,
280}
281
282#[derive(Debug)]
283pub struct Ssr {
284    pub rule: Vec<SsrRule>,
285}
286
287#[derive(Debug)]
288pub struct Search {
289    pub pattern: Vec<SsrPattern>,
290
291    pub debug: Option<String>,
292}
293
294#[derive(Debug)]
295pub struct Lsif {
296    pub path: PathBuf,
297
298    pub exclude_vendored_libraries: bool,
299}
300
301#[derive(Debug)]
302pub struct Scip {
303    pub path: PathBuf,
304
305    pub output: Option<PathBuf>,
306    pub config_path: Option<PathBuf>,
307    pub exclude_vendored_libraries: bool,
308}
309
310impl RustAnalyzer {
311    #[allow(dead_code)]
312    pub fn from_env_or_exit() -> Self {
313        Self::from_env_or_exit_()
314    }
315
316    #[allow(dead_code)]
317    pub fn from_env() -> xflags::Result<Self> {
318        Self::from_env_()
319    }
320
321    #[allow(dead_code)]
322    pub fn from_vec(args: Vec<std::ffi::OsString>) -> xflags::Result<Self> {
323        Self::from_vec_(args)
324    }
325}
326// generated end
327
328#[derive(Debug, PartialEq, Eq)]
329pub enum OutputFormat {
330    Csv,
331}
332
333impl RustAnalyzer {
334    pub fn verbosity(&self) -> Verbosity {
335        if self.quiet {
336            return Verbosity::Quiet;
337        }
338        match self.verbose {
339            0 => Verbosity::Normal,
340            1 => Verbosity::Verbose,
341            _ => Verbosity::Spammy,
342        }
343    }
344}
345
346impl FromStr for OutputFormat {
347    type Err = String;
348
349    fn from_str(s: &str) -> Result<Self, Self::Err> {
350        match s {
351            "csv" => Ok(Self::Csv),
352            _ => Err(format!("unknown output format `{s}`")),
353        }
354    }
355}