1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
|
//! FIXME: write short doc here
mod load_cargo;
mod analysis_stats;
mod analysis_bench;
mod progress_report;
use std::{error::Error, fmt::Write, io::Read, path::PathBuf};
use pico_args::Arguments;
use ra_ide::{file_structure, Analysis};
use ra_prof::profile;
use ra_syntax::{AstNode, SourceFile};
type Result<T, E = Box<dyn Error + Send + Sync>> = std::result::Result<T, E>;
fn main() -> Result<()> {
env_logger::try_init()?;
let command = match Command::from_args()? {
Ok(it) => it,
Err(HelpPrinted) => return Ok(()),
};
match command {
Command::Parse { no_dump } => {
let _p = profile("parsing");
let file = file()?;
if !no_dump {
println!("{:#?}", file.syntax());
}
std::mem::forget(file);
}
Command::Symbols => {
let file = file()?;
for s in file_structure(&file) {
println!("{:?}", s);
}
}
Command::Highlight { rainbow } => {
let (analysis, file_id) = Analysis::from_single_file(read_stdin()?);
let html = analysis.highlight_as_html(file_id, rainbow).unwrap();
println!("{}", html);
}
Command::Stats { verbosity, randomize, memory_usage, only, with_deps, path } => {
analysis_stats::run(
verbosity,
memory_usage,
path.as_ref(),
only.as_ref().map(String::as_ref),
with_deps,
randomize,
)?;
}
Command::Bench { verbosity, path, op } => {
analysis_bench::run(verbosity, path.as_ref(), op)?;
}
}
Ok(())
}
#[derive(Clone, Copy)]
pub enum Verbosity {
Spammy,
Verbose,
Normal,
Quiet,
}
impl Verbosity {
fn is_verbose(self) -> bool {
match self {
Verbosity::Verbose | Verbosity::Spammy => true,
_ => false,
}
}
fn is_spammy(self) -> bool {
match self {
Verbosity::Spammy => true,
_ => false,
}
}
}
enum Command {
Parse {
no_dump: bool,
},
Symbols,
Highlight {
rainbow: bool,
},
Stats {
verbosity: Verbosity,
randomize: bool,
memory_usage: bool,
only: Option<String>,
with_deps: bool,
path: PathBuf,
},
Bench {
verbosity: Verbosity,
path: PathBuf,
op: analysis_bench::Op,
},
}
struct HelpPrinted;
impl Command {
fn from_args() -> Result<Result<Command, HelpPrinted>> {
let mut matches = Arguments::from_env();
let subcommand = matches.subcommand()?.unwrap_or_default();
let verbosity = match (
matches.contains(["-vv", "--spammy"]),
matches.contains(["-v", "--verbose"]),
matches.contains(["-q", "--quiet"]),
) {
(true, _, true) => Err("Invalid flags: -q conflicts with -vv")?,
(true, _, false) => Verbosity::Spammy,
(false, false, false) => Verbosity::Normal,
(false, false, true) => Verbosity::Quiet,
(false, true, false) => Verbosity::Verbose,
(false, true, true) => Err("Invalid flags: -q conflicts with -v")?,
};
let command = match subcommand.as_str() {
"parse" => {
if matches.contains(["-h", "--help"]) {
eprintln!(
"\
ra-cli-parse
USAGE:
ra_cli parse [FLAGS]
FLAGS:
-h, --help Prints help inforamtion
--no-dump"
);
return Ok(Err(HelpPrinted));
}
let no_dump = matches.contains("--no-dump");
matches.finish().or_else(handle_extra_flags)?;
Command::Parse { no_dump }
}
"symbols" => {
if matches.contains(["-h", "--help"]) {
eprintln!(
"\
ra-cli-symbols
USAGE:
ra_cli highlight [FLAGS]
FLAGS:
-h, --help Prints help inforamtion"
);
return Ok(Err(HelpPrinted));
}
matches.finish().or_else(handle_extra_flags)?;
Command::Symbols
}
"highlight" => {
if matches.contains(["-h", "--help"]) {
eprintln!(
"\
ra-cli-highlight
USAGE:
ra_cli highlight [FLAGS]
FLAGS:
-h, --help Prints help information
-r, --rainbow"
);
return Ok(Err(HelpPrinted));
}
let rainbow = matches.contains(["-r", "--rainbow"]);
matches.finish().or_else(handle_extra_flags)?;
Command::Highlight { rainbow }
}
"analysis-stats" => {
if matches.contains(["-h", "--help"]) {
eprintln!(
"\
ra-cli-analysis-stats
USAGE:
ra_cli analysis-stats [FLAGS] [OPTIONS] [PATH]
FLAGS:
-h, --help Prints help information
--memory-usage
-v, --verbose
-q, --quiet
OPTIONS:
-o <ONLY>
ARGS:
<PATH>"
);
return Ok(Err(HelpPrinted));
}
let randomize = matches.contains("--randomize");
let memory_usage = matches.contains("--memory-usage");
let only: Option<String> = matches.opt_value_from_str(["-o", "--only"])?;
let with_deps: bool = matches.contains("--with-deps");
let path = {
let mut trailing = matches.free()?;
if trailing.len() != 1 {
Err("Invalid flags")?;
}
trailing.pop().unwrap().into()
};
Command::Stats { verbosity, randomize, memory_usage, only, with_deps, path }
}
"analysis-bench" => {
if matches.contains(["-h", "--help"]) {
eprintln!(
"\
ra_cli-analysis-bench
USAGE:
ra_cli analysis-bench [FLAGS] [OPTIONS] [PATH]
FLAGS:
-h, --help Prints help information
-v, --verbose
OPTIONS:
--complete <PATH:LINE:COLUMN> Compute completions at this location
--highlight <PATH> Hightlight this file
ARGS:
<PATH> Project to analyse"
);
return Ok(Err(HelpPrinted));
}
let path: PathBuf = matches.opt_value_from_str("--path")?.unwrap_or_default();
let highlight_path: Option<String> = matches.opt_value_from_str("--highlight")?;
let complete_path: Option<String> = matches.opt_value_from_str("--complete")?;
let goto_def_path: Option<String> = matches.opt_value_from_str("--goto-def")?;
let op = match (highlight_path, complete_path, goto_def_path) {
(Some(path), None, None) => analysis_bench::Op::Highlight { path: path.into() },
(None, Some(position), None) => analysis_bench::Op::Complete(position.parse()?),
(None, None, Some(position)) => analysis_bench::Op::GotoDef(position.parse()?),
_ => panic!(
"exactly one of `--highlight`, `--complete` or `--goto-def` must be set"
),
};
Command::Bench { verbosity, path, op }
}
_ => {
eprintln!(
"\
ra-cli
USAGE:
ra_cli <SUBCOMMAND>
FLAGS:
-h, --help Prints help information
SUBCOMMANDS:
analysis-bench
analysis-stats
highlight
parse
symbols"
);
return Ok(Err(HelpPrinted));
}
};
Ok(Ok(command))
}
}
fn handle_extra_flags(e: pico_args::Error) -> Result<()> {
if let pico_args::Error::UnusedArgsLeft(flags) = e {
let mut invalid_flags = String::new();
for flag in flags {
write!(&mut invalid_flags, "{}, ", flag)?;
}
let (invalid_flags, _) = invalid_flags.split_at(invalid_flags.len() - 2);
Err(format!("Invalid flags: {}", invalid_flags).into())
} else {
Err(e.to_string().into())
}
}
fn file() -> Result<SourceFile> {
let text = read_stdin()?;
Ok(SourceFile::parse(&text).tree())
}
fn read_stdin() -> Result<String> {
let mut buff = String::new();
std::io::stdin().read_to_string(&mut buff)?;
Ok(buff)
}
|