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 optional --num-threads num_threads: usize
184 }
185
186 cmd scip {
187 required path: PathBuf
188
189 optional --output path: PathBuf
191
192 optional --config-path config_path: PathBuf
194
195 optional --exclude-vendored-libraries
197
198 optional --num-threads num_threads: usize
200 }
201 }
202}
203
204#[derive(Debug)]
208pub struct RustAnalyzer {
209 pub verbose: u32,
210 pub quiet: bool,
211 pub log_file: Option<PathBuf>,
212 pub no_log_buffering: bool,
213 pub wait_dbg: bool,
214 pub subcommand: RustAnalyzerCmd,
215}
216
217#[derive(Debug)]
218pub enum RustAnalyzerCmd {
219 LspServer(LspServer),
220 Parse(Parse),
221 Symbols(Symbols),
222 Highlight(Highlight),
223 AnalysisStats(AnalysisStats),
224 RunTests(RunTests),
225 RustcTests(RustcTests),
226 Diagnostics(Diagnostics),
227 UnresolvedReferences(UnresolvedReferences),
228 PrimeCaches(PrimeCaches),
229 Ssr(Ssr),
230 Search(Search),
231 Lsif(Lsif),
232 Scip(Scip),
233}
234
235#[derive(Debug)]
236pub struct LspServer {
237 pub version: bool,
238 pub print_config_schema: bool,
239}
240
241#[derive(Debug)]
242pub struct Parse {
243 pub no_dump: bool,
244 pub json: bool,
245}
246
247#[derive(Debug)]
248pub struct Symbols;
249
250#[derive(Debug)]
251pub struct Highlight {
252 pub rainbow: bool,
253}
254
255#[derive(Debug)]
256pub struct AnalysisStats {
257 pub path: PathBuf,
258
259 pub output: Option<OutputFormat>,
260 pub randomize: bool,
261 pub parallel: bool,
262 pub only: Option<String>,
263 pub with_deps: bool,
264 pub no_sysroot: bool,
265 pub no_test: bool,
266 pub disable_build_scripts: bool,
267 pub disable_proc_macros: bool,
268 pub proc_macro_srv: Option<PathBuf>,
269 pub skip_lang_items: bool,
270 pub skip_lowering: bool,
271 pub skip_inference: bool,
272 pub skip_mir_stats: bool,
273 pub skip_data_layout: bool,
274 pub skip_const_eval: bool,
275 pub run_all_ide_things: bool,
276 pub run_term_search: bool,
277 pub validate_term_search: bool,
278}
279
280#[derive(Debug)]
281pub struct RunTests {
282 pub path: PathBuf,
283}
284
285#[derive(Debug)]
286pub struct RustcTests {
287 pub rustc_repo: PathBuf,
288
289 pub filter: Option<String>,
290}
291
292#[derive(Debug)]
293pub struct Diagnostics {
294 pub path: PathBuf,
295
296 pub disable_build_scripts: bool,
297 pub disable_proc_macros: bool,
298 pub proc_macro_srv: Option<PathBuf>,
299 pub severity: Option<Severity>,
300}
301
302#[derive(Debug)]
303pub struct UnresolvedReferences {
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}
310
311#[derive(Debug)]
312pub struct PrimeCaches {
313 pub path: PathBuf,
314
315 pub disable_build_scripts: bool,
316 pub disable_proc_macros: bool,
317 pub proc_macro_srv: Option<PathBuf>,
318 pub num_threads: Option<usize>,
319}
320
321#[derive(Debug)]
322pub struct Ssr {
323 pub rule: Vec<SsrRule>,
324}
325
326#[derive(Debug)]
327pub struct Search {
328 pub pattern: Vec<SsrPattern>,
329
330 pub debug: Option<String>,
331}
332
333#[derive(Debug)]
334pub struct Lsif {
335 pub path: PathBuf,
336
337 pub exclude_vendored_libraries: bool,
338 pub num_threads: Option<usize>,
339}
340
341#[derive(Debug)]
342pub struct Scip {
343 pub path: PathBuf,
344
345 pub output: Option<PathBuf>,
346 pub config_path: Option<PathBuf>,
347 pub exclude_vendored_libraries: bool,
348 pub num_threads: Option<usize>,
349}
350
351impl RustAnalyzer {
352 #[allow(dead_code)]
353 pub fn from_env_or_exit() -> Self {
354 Self::from_env_or_exit_()
355 }
356
357 #[allow(dead_code)]
358 pub fn from_env() -> xflags::Result<Self> {
359 Self::from_env_()
360 }
361
362 #[allow(dead_code)]
363 pub fn from_vec(args: Vec<std::ffi::OsString>) -> xflags::Result<Self> {
364 Self::from_vec_(args)
365 }
366}
367#[derive(Debug, PartialEq, Eq)]
370pub enum OutputFormat {
371 Csv,
372}
373
374impl RustAnalyzer {
375 pub fn verbosity(&self) -> Verbosity {
376 if self.quiet {
377 return Verbosity::Quiet;
378 }
379 match self.verbose {
380 0 => Verbosity::Normal,
381 1 => Verbosity::Verbose,
382 _ => Verbosity::Spammy,
383 }
384 }
385}
386
387impl FromStr for OutputFormat {
388 type Err = String;
389
390 fn from_str(s: &str) -> Result<Self, Self::Err> {
391 match s {
392 "csv" => Ok(Self::Csv),
393 _ => Err(format!("unknown output format `{s}`")),
394 }
395 }
396}
397
398#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
399pub enum Severity {
400 Weak,
401 Warning,
402 Error,
403}
404
405impl FromStr for Severity {
406 type Err = String;
407
408 fn from_str(s: &str) -> Result<Self, Self::Err> {
409 match &*s.to_ascii_lowercase() {
410 "weak" => Ok(Self::Weak),
411 "warning" => Ok(Self::Warning),
412 "error" => Ok(Self::Error),
413 _ => Err(format!("unknown severity `{s}`")),
414 }
415 }
416}