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