aboutsummaryrefslogtreecommitdiff
path: root/crates/rust-analyzer/src/bin/args.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/rust-analyzer/src/bin/args.rs')
-rw-r--r--crates/rust-analyzer/src/bin/args.rs274
1 files changed, 0 insertions, 274 deletions
diff --git a/crates/rust-analyzer/src/bin/args.rs b/crates/rust-analyzer/src/bin/args.rs
deleted file mode 100644
index 164d94a30..000000000
--- a/crates/rust-analyzer/src/bin/args.rs
+++ /dev/null
@@ -1,274 +0,0 @@
1//! Command like parsing for rust-analyzer.
2//!
3//! If run started args, we run the LSP server loop. With a subcommand, we do a
4//! one-time batch processing.
5
6use std::{env, path::PathBuf};
7
8use anyhow::{bail, format_err, Result};
9use ide_ssr::{SsrPattern, SsrRule};
10use pico_args::Arguments;
11use rust_analyzer::cli::{AnalysisStatsCmd, BenchCmd, BenchWhat, Position, Verbosity};
12use vfs::AbsPathBuf;
13
14pub(crate) struct Args {
15 pub(crate) verbosity: Verbosity,
16 pub(crate) log_file: Option<PathBuf>,
17 pub(crate) no_buffering: bool,
18 pub(crate) command: Command,
19 #[allow(unused)]
20 pub(crate) wait_dbg: bool,
21}
22
23pub(crate) enum Command {
24 Parse { no_dump: bool },
25 Symbols,
26 Highlight { rainbow: bool },
27 AnalysisStats(AnalysisStatsCmd),
28 Bench(BenchCmd),
29 Diagnostics { path: PathBuf, load_output_dirs: bool, with_proc_macro: bool },
30 Ssr { rules: Vec<SsrRule> },
31 StructuredSearch { debug_snippet: Option<String>, patterns: Vec<SsrPattern> },
32 ProcMacro,
33 RunServer,
34 PrintConfigSchema,
35 Version,
36 Help,
37}
38
39const HELP: &str = "\
40rust-analyzer
41
42USAGE:
43 rust-analyzer [FLAGS] [COMMAND] [COMMAND_OPTIONS]
44
45FLAGS:
46 --version Print version
47 -h, --help Print this help
48
49 -v, --verbose
50 -vv, --spammy
51 -q, --quiet Set verbosity
52
53 --print-config-schema
54 Dump a LSP config JSON schema
55 --log-file <PATH> Log to the specified file instead of stderr
56 --no-log-buffering
57 Flush log records to the file immediately
58
59 --wait-dbg Wait until a debugger is attached to.
60 The flag is valid for debug builds only
61
62ENVIRONMENTAL VARIABLES:
63 RA_LOG Set log filter in env_logger format
64 RA_PROFILE Enable hierarchical profiler
65 RA_WAIT_DBG If set acts like a --wait-dbg flag
66
67COMMANDS:
68
69not specified Launch LSP server
70
71parse < main.rs Parse tree
72 --no-dump Suppress printing
73
74symbols < main.rs Parse input an print the list of symbols
75
76highlight < main.rs Highlight input as html
77 --rainbow Enable rainbow highlighting of identifiers
78
79analysis-stats <PATH> Batch typecheck project and print summary statistics
80 <PATH> Directory with Cargo.toml
81 --randomize Randomize order in which crates, modules, and items are processed
82 --parallel Run type inference in parallel
83 --memory-usage Collect memory usage statistics
84 -o, --only <PATH> Only analyze items matching this path
85 --with-deps Also analyze all dependencies
86 --load-output-dirs
87 Load OUT_DIR values by running `cargo check` before analysis
88 --with-proc-macro Use proc-macro-srv for proc-macro expanding
89
90analysis-bench <PATH> Benchmark specific analysis operation
91 <PATH> Directory with Cargo.toml
92 --highlight <PATH>
93 Compute syntax highlighting for this file
94 --complete <PATH:LINE:COLUMN>
95 Compute completions at this location
96 --goto-def <PATH:LINE:COLUMN>
97 Compute goto definition at this location
98 --memory-usage Collect memory usage statistics
99 --load-output-dirs
100 Load OUT_DIR values by running `cargo check` before analysis
101 --with-proc-macro Use proc-macro-srv for proc-macro expanding
102
103diagnostics <PATH>
104 <PATH> Directory with Cargo.toml
105 --load-output-dirs
106 Load OUT_DIR values by running `cargo check` before analysis
107 --with-proc-macro Use proc-macro-srv for proc-macro expanding
108
109ssr [RULE...]
110 <RULE> A structured search replace rule (`$a.foo($b) ==> bar($a, $b)`)
111
112search [PATTERN..]
113 <PATTERN> A structured search replace pattern (`$a.foo($b)`)
114 --debug <snippet> Prints debug information for any nodes with source exactly
115 equal to <snippet>
116";
117
118impl Args {
119 pub(crate) fn parse() -> Result<Args> {
120 let mut matches = Arguments::from_env();
121
122 if matches.contains("--version") {
123 finish_args(matches)?;
124 return Ok(Args {
125 verbosity: Verbosity::Normal,
126 log_file: None,
127 command: Command::Version,
128 no_buffering: false,
129 wait_dbg: false,
130 });
131 }
132
133 let verbosity = match (
134 matches.contains(["-vv", "--spammy"]),
135 matches.contains(["-v", "--verbose"]),
136 matches.contains(["-q", "--quiet"]),
137 ) {
138 (true, _, true) => bail!("Invalid flags: -q conflicts with -vv"),
139 (true, _, false) => Verbosity::Spammy,
140 (false, false, false) => Verbosity::Normal,
141 (false, false, true) => Verbosity::Quiet,
142 (false, true, false) => Verbosity::Verbose,
143 (false, true, true) => bail!("Invalid flags: -q conflicts with -v"),
144 };
145 let log_file = matches.opt_value_from_str("--log-file")?;
146 let no_buffering = matches.contains("--no-log-buffering");
147 let wait_dbg = matches.contains("--wait-dbg");
148
149 if matches.contains(["-h", "--help"]) {
150 eprintln!("{}", HELP);
151 return Ok(Args {
152 verbosity,
153 log_file: None,
154 command: Command::Help,
155 no_buffering,
156 wait_dbg,
157 });
158 }
159
160 if matches.contains("--print-config-schema") {
161 return Ok(Args {
162 verbosity,
163 log_file,
164 command: Command::PrintConfigSchema,
165 no_buffering,
166 wait_dbg,
167 });
168 }
169
170 let subcommand = match matches.subcommand()? {
171 Some(it) => it,
172 None => {
173 finish_args(matches)?;
174 return Ok(Args {
175 verbosity,
176 log_file,
177 command: Command::RunServer,
178 no_buffering,
179 wait_dbg,
180 });
181 }
182 };
183 let command = match subcommand.as_str() {
184 "parse" => Command::Parse { no_dump: matches.contains("--no-dump") },
185 "symbols" => Command::Symbols,
186 "highlight" => Command::Highlight { rainbow: matches.contains("--rainbow") },
187 "analysis-stats" => Command::AnalysisStats(AnalysisStatsCmd {
188 randomize: matches.contains("--randomize"),
189 parallel: matches.contains("--parallel"),
190 memory_usage: matches.contains("--memory-usage"),
191 only: matches.opt_value_from_str(["-o", "--only"])?,
192 with_deps: matches.contains("--with-deps"),
193 load_output_dirs: matches.contains("--load-output-dirs"),
194 with_proc_macro: matches.contains("--with-proc-macro"),
195 path: matches
196 .opt_free_from_str()?
197 .ok_or_else(|| format_err!("expected positional argument"))?,
198 }),
199 "analysis-bench" => Command::Bench(BenchCmd {
200 what: {
201 let highlight_path: Option<String> =
202 matches.opt_value_from_str("--highlight")?;
203 let complete_path: Option<Position> =
204 matches.opt_value_from_str("--complete")?;
205 let goto_def_path: Option<Position> =
206 matches.opt_value_from_str("--goto-def")?;
207 match (highlight_path, complete_path, goto_def_path) {
208 (Some(path), None, None) => {
209 let path = env::current_dir().unwrap().join(path);
210 BenchWhat::Highlight { path: AbsPathBuf::assert(path) }
211 }
212 (None, Some(position), None) => BenchWhat::Complete(position),
213 (None, None, Some(position)) => BenchWhat::GotoDef(position),
214 _ => panic!(
215 "exactly one of `--highlight`, `--complete` or `--goto-def` must be set"
216 ),
217 }
218 },
219 memory_usage: matches.contains("--memory-usage"),
220 load_output_dirs: matches.contains("--load-output-dirs"),
221 with_proc_macro: matches.contains("--with-proc-macro"),
222 path: matches
223 .opt_free_from_str()?
224 .ok_or_else(|| format_err!("expected positional argument"))?,
225 }),
226 "diagnostics" => Command::Diagnostics {
227 load_output_dirs: matches.contains("--load-output-dirs"),
228 with_proc_macro: matches.contains("--with-proc-macro"),
229 path: matches
230 .opt_free_from_str()?
231 .ok_or_else(|| format_err!("expected positional argument"))?,
232 },
233 "proc-macro" => Command::ProcMacro,
234 "ssr" => Command::Ssr {
235 rules: {
236 let mut acc = Vec::new();
237 while let Some(rule) = matches.opt_free_from_str()? {
238 acc.push(rule);
239 }
240 acc
241 },
242 },
243 "search" => Command::StructuredSearch {
244 debug_snippet: matches.opt_value_from_str("--debug")?,
245 patterns: {
246 let mut acc = Vec::new();
247 while let Some(rule) = matches.opt_free_from_str()? {
248 acc.push(rule);
249 }
250 acc
251 },
252 },
253 _ => {
254 eprintln!("{}", HELP);
255 return Ok(Args {
256 verbosity,
257 log_file: None,
258 command: Command::Help,
259 no_buffering,
260 wait_dbg,
261 });
262 }
263 };
264 finish_args(matches)?;
265 Ok(Args { verbosity, log_file, command, no_buffering, wait_dbg })
266 }
267}
268
269fn finish_args(args: Arguments) -> Result<()> {
270 if !args.finish().is_empty() {
271 bail!("Unused arguments.");
272 }
273 Ok(())
274}