1#![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 cmd rust-analyzer {
18 repeated -v, --verbose
20 optional -q, --quiet
22
23 optional --log-file path: PathBuf
25 optional --no-log-buffering
27
28 optional --wait-dbg
30
31 default cmd lsp-server {
32 optional -V, --version
34
35 optional --print-config-schema
37 }
38
39 cmd parse {
41 optional --no-dump
43 optional --json
45 }
46
47 cmd symbols {}
49
50 cmd highlight {
52 optional --rainbow
54 }
55
56 cmd analysis-stats {
58 required path: PathBuf
60
61 optional --output format: OutputFormat
62
63 optional --randomize
65 optional --parallel
67
68 optional -o, --only path: String
70 optional --with-deps
72 optional --no-sysroot
74 optional --no-test
76
77 optional --disable-build-scripts
79 optional --disable-proc-macros
81 optional --proc-macro-srv path: PathBuf
83 optional --skip-lang-items
85 optional --skip-lowering
87 optional --skip-inference
89 optional --skip-mir-stats
91 optional --skip-data-layout
93 optional --skip-const-eval
95 optional --run-all-ide-things
99 optional --run-term-search
101 optional --validate-term-search
104 }
105
106 cmd run-tests {
108 required path: PathBuf
110 }
111
112 cmd rustc-tests {
114 required rustc_repo: PathBuf
116
117 optional --filter path: String
119 }
120
121 cmd diagnostics {
122 required path: PathBuf
124
125 optional --disable-build-scripts
127 optional --disable-proc-macros
129 optional --proc-macro-srv path: PathBuf
131
132 optional --severity severity: Severity
134 }
135
136 cmd unresolved-references {
138 required path: PathBuf
140
141 optional --disable-build-scripts
143 optional --disable-proc-macros
145 optional --proc-macro-srv path: PathBuf
147 }
148
149 cmd prime-caches {
151 required path: PathBuf
153
154 optional --disable-build-scripts
156 optional --disable-proc-macros
158 optional --proc-macro-srv path: PathBuf
160 optional --num-threads num_threads: usize
162 }
163
164 cmd ssr {
165 repeated rule: SsrRule
167 }
168
169 cmd search {
170 repeated pattern: SsrPattern
172 optional --debug snippet: String
174 }
175
176 cmd lsif {
177 required path: PathBuf
178
179 optional --exclude-vendored-libraries
181 }
182
183 cmd scip {
184 required path: PathBuf
185
186 optional --output path: PathBuf
188
189 optional --config-path config_path: PathBuf
191
192 optional --exclude-vendored-libraries
194 }
195 }
196}
197
198#[derive(Debug)]
202pub struct RustAnalyzer {
203 pub verbose: u32,
204 pub quiet: bool,
205 pub log_file: Option<PathBuf>,
206 pub no_log_buffering: bool,
207 pub wait_dbg: bool,
208 pub subcommand: RustAnalyzerCmd,
209}
210
211#[derive(Debug)]
212pub enum RustAnalyzerCmd {
213 LspServer(LspServer),
214 Parse(Parse),
215 Symbols(Symbols),
216 Highlight(Highlight),
217 AnalysisStats(AnalysisStats),
218 RunTests(RunTests),
219 RustcTests(RustcTests),
220 Diagnostics(Diagnostics),
221 UnresolvedReferences(UnresolvedReferences),
222 PrimeCaches(PrimeCaches),
223 Ssr(Ssr),
224 Search(Search),
225 Lsif(Lsif),
226 Scip(Scip),
227}
228
229#[derive(Debug)]
230pub struct LspServer {
231 pub version: bool,
232 pub print_config_schema: bool,
233}
234
235#[derive(Debug)]
236pub struct Parse {
237 pub no_dump: bool,
238 pub json: bool,
239}
240
241#[derive(Debug)]
242pub struct Symbols;
243
244#[derive(Debug)]
245pub struct Highlight {
246 pub rainbow: bool,
247}
248
249#[derive(Debug)]
250pub struct AnalysisStats {
251 pub path: PathBuf,
252
253 pub output: Option<OutputFormat>,
254 pub randomize: bool,
255 pub parallel: bool,
256 pub only: Option<String>,
257 pub with_deps: bool,
258 pub no_sysroot: bool,
259 pub no_test: bool,
260 pub disable_build_scripts: bool,
261 pub disable_proc_macros: bool,
262 pub proc_macro_srv: Option<PathBuf>,
263 pub skip_lang_items: bool,
264 pub skip_lowering: bool,
265 pub skip_inference: bool,
266 pub skip_mir_stats: bool,
267 pub skip_data_layout: bool,
268 pub skip_const_eval: bool,
269 pub run_all_ide_things: bool,
270 pub run_term_search: bool,
271 pub validate_term_search: bool,
272}
273
274#[derive(Debug)]
275pub struct RunTests {
276 pub path: PathBuf,
277}
278
279#[derive(Debug)]
280pub struct RustcTests {
281 pub rustc_repo: PathBuf,
282
283 pub filter: Option<String>,
284}
285
286#[derive(Debug)]
287pub struct Diagnostics {
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 pub severity: Option<Severity>,
294}
295
296#[derive(Debug)]
297pub struct UnresolvedReferences {
298 pub path: PathBuf,
299
300 pub disable_build_scripts: bool,
301 pub disable_proc_macros: bool,
302 pub proc_macro_srv: Option<PathBuf>,
303}
304
305#[derive(Debug)]
306pub struct PrimeCaches {
307 pub path: PathBuf,
308
309 pub disable_build_scripts: bool,
310 pub disable_proc_macros: bool,
311 pub proc_macro_srv: Option<PathBuf>,
312 pub num_threads: Option<usize>,
313}
314
315#[derive(Debug)]
316pub struct Ssr {
317 pub rule: Vec<SsrRule>,
318}
319
320#[derive(Debug)]
321pub struct Search {
322 pub pattern: Vec<SsrPattern>,
323
324 pub debug: Option<String>,
325}
326
327#[derive(Debug)]
328pub struct Lsif {
329 pub path: PathBuf,
330
331 pub exclude_vendored_libraries: bool,
332}
333
334#[derive(Debug)]
335pub struct Scip {
336 pub path: PathBuf,
337
338 pub output: Option<PathBuf>,
339 pub config_path: Option<PathBuf>,
340 pub exclude_vendored_libraries: bool,
341}
342
343impl RustAnalyzer {
344 #[allow(dead_code)]
345 pub fn from_env_or_exit() -> Self {
346 Self::from_env_or_exit_()
347 }
348
349 #[allow(dead_code)]
350 pub fn from_env() -> xflags::Result<Self> {
351 Self::from_env_()
352 }
353
354 #[allow(dead_code)]
355 pub fn from_vec(args: Vec<std::ffi::OsString>) -> xflags::Result<Self> {
356 Self::from_vec_(args)
357 }
358}
359#[derive(Debug, PartialEq, Eq)]
362pub enum OutputFormat {
363 Csv,
364}
365
366impl RustAnalyzer {
367 pub fn verbosity(&self) -> Verbosity {
368 if self.quiet {
369 return Verbosity::Quiet;
370 }
371 match self.verbose {
372 0 => Verbosity::Normal,
373 1 => Verbosity::Verbose,
374 _ => Verbosity::Spammy,
375 }
376 }
377}
378
379impl FromStr for OutputFormat {
380 type Err = String;
381
382 fn from_str(s: &str) -> Result<Self, Self::Err> {
383 match s {
384 "csv" => Ok(Self::Csv),
385 _ => Err(format!("unknown output format `{s}`")),
386 }
387 }
388}
389
390#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
391pub enum Severity {
392 Weak,
393 Warning,
394 Error,
395}
396
397impl FromStr for Severity {
398 type Err = String;
399
400 fn from_str(s: &str) -> Result<Self, Self::Err> {
401 match &*s.to_ascii_lowercase() {
402 "weak" => Ok(Self::Weak),
403 "warning" => Ok(Self::Warning),
404 "error" => Ok(Self::Error),
405 _ => Err(format!("unknown severity `{s}`")),
406 }
407 }
408}