Skip to content

Commit 9cf5914

Browse files
committed
internal: unify subcommand handling between ra and xtask
1 parent e8a67b6 commit 9cf5914

File tree

8 files changed

+197
-203
lines changed

8 files changed

+197
-203
lines changed

crates/rust-analyzer/src/bin/main.rs

Lines changed: 9 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,7 @@ use std::{convert::TryFrom, env, fs, path::Path, process};
88

99
use lsp_server::Connection;
1010
use project_model::ProjectManifest;
11-
use rust_analyzer::{
12-
cli::{self, flags, AnalysisStatsCmd},
13-
config::Config,
14-
from_json,
15-
lsp_ext::supports_utf8,
16-
Result,
17-
};
11+
use rust_analyzer::{cli::flags, config::Config, from_json, lsp_ext::supports_utf8, Result};
1812
use vfs::AbsPathBuf;
1913

2014
#[cfg(all(feature = "mimalloc"))]
@@ -85,29 +79,14 @@ fn try_main() -> Result<()> {
8579
}
8680
run_server()?
8781
}
88-
flags::RustAnalyzerCmd::ProcMacro(_) => proc_macro_srv::cli::run()?,
89-
flags::RustAnalyzerCmd::Parse(cmd) => cli::parse(cmd.no_dump)?,
90-
flags::RustAnalyzerCmd::Symbols(_) => cli::symbols()?,
91-
flags::RustAnalyzerCmd::Highlight(cmd) => cli::highlight(cmd.rainbow)?,
92-
flags::RustAnalyzerCmd::AnalysisStats(cmd) => AnalysisStatsCmd {
93-
randomize: cmd.randomize,
94-
parallel: cmd.parallel,
95-
memory_usage: cmd.memory_usage,
96-
only: cmd.only,
97-
with_deps: cmd.with_deps,
98-
no_sysroot: cmd.no_sysroot,
99-
path: cmd.path,
100-
enable_build_scripts: !cmd.disable_build_scripts,
101-
enable_proc_macros: !cmd.disable_proc_macros,
102-
skip_inference: cmd.skip_inference,
103-
}
104-
.run(verbosity)?,
105-
106-
flags::RustAnalyzerCmd::Diagnostics(cmd) => {
107-
cli::diagnostics(&cmd.path, !cmd.disable_build_scripts, !cmd.disable_proc_macros)?
108-
}
109-
flags::RustAnalyzerCmd::Ssr(cmd) => cli::apply_ssr_rules(cmd.rule)?,
110-
flags::RustAnalyzerCmd::Search(cmd) => cli::search_for_patterns(cmd.pattern, cmd.debug)?,
82+
flags::RustAnalyzerCmd::ProcMacro(flags::ProcMacro) => proc_macro_srv::cli::run()?,
83+
flags::RustAnalyzerCmd::Parse(cmd) => cmd.run()?,
84+
flags::RustAnalyzerCmd::Symbols(cmd) => cmd.run()?,
85+
flags::RustAnalyzerCmd::Highlight(cmd) => cmd.run()?,
86+
flags::RustAnalyzerCmd::AnalysisStats(cmd) => cmd.run(verbosity)?,
87+
flags::RustAnalyzerCmd::Diagnostics(cmd) => cmd.run()?,
88+
flags::RustAnalyzerCmd::Ssr(cmd) => cmd.run()?,
89+
flags::RustAnalyzerCmd::Search(cmd) => cmd.run()?,
11190
}
11291
Ok(())
11392
}

crates/rust-analyzer/src/cli.rs

Lines changed: 6 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,21 @@
22
33
pub mod flags;
44
pub mod load_cargo;
5+
mod parse;
6+
mod symbols;
7+
mod highlight;
58
mod analysis_stats;
69
mod diagnostics;
7-
mod progress_report;
810
mod ssr;
911

12+
mod progress_report;
13+
1014
use std::io::Read;
1115

1216
use anyhow::Result;
13-
use ide::{Analysis, AnalysisHost};
14-
use syntax::{AstNode, SourceFile};
17+
use ide::AnalysisHost;
1518
use vfs::Vfs;
1619

17-
pub use self::{
18-
analysis_stats::AnalysisStatsCmd,
19-
diagnostics::diagnostics,
20-
ssr::{apply_ssr_rules, search_for_patterns},
21-
};
22-
2320
#[derive(Clone, Copy)]
2421
pub enum Verbosity {
2522
Spammy,
@@ -37,38 +34,6 @@ impl Verbosity {
3734
}
3835
}
3936

40-
pub fn parse(no_dump: bool) -> Result<()> {
41-
let _p = profile::span("parsing");
42-
let file = file()?;
43-
if !no_dump {
44-
println!("{:#?}", file.syntax());
45-
}
46-
std::mem::forget(file);
47-
Ok(())
48-
}
49-
50-
pub fn symbols() -> Result<()> {
51-
let text = read_stdin()?;
52-
let (analysis, file_id) = Analysis::from_single_file(text);
53-
let structure = analysis.file_structure(file_id).unwrap();
54-
for s in structure {
55-
println!("{:?}", s);
56-
}
57-
Ok(())
58-
}
59-
60-
pub fn highlight(rainbow: bool) -> Result<()> {
61-
let (analysis, file_id) = Analysis::from_single_file(read_stdin()?);
62-
let html = analysis.highlight_as_html(file_id, rainbow).unwrap();
63-
println!("{}", html);
64-
Ok(())
65-
}
66-
67-
fn file() -> Result<SourceFile> {
68-
let text = read_stdin()?;
69-
Ok(SourceFile::parse(&text).tree())
70-
}
71-
7237
fn read_stdin() -> Result<String> {
7338
let mut buff = String::new();
7439
std::io::stdin().read_to_string(&mut buff)?;

crates/rust-analyzer/src/cli/analysis_stats.rs

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
44
use std::{
55
env,
6-
path::PathBuf,
76
time::{SystemTime, UNIX_EPOCH},
87
};
98

@@ -28,6 +27,7 @@ use syntax::AstNode;
2827
use vfs::{Vfs, VfsPath};
2928

3029
use crate::cli::{
30+
flags,
3131
load_cargo::{load_workspace_at, LoadCargoConfig},
3232
print_memory_usage,
3333
progress_report::ProgressReport,
@@ -43,20 +43,7 @@ impl<DB: ParallelDatabase> Clone for Snap<salsa::Snapshot<DB>> {
4343
}
4444
}
4545

46-
pub struct AnalysisStatsCmd {
47-
pub randomize: bool,
48-
pub parallel: bool,
49-
pub memory_usage: bool,
50-
pub only: Option<String>,
51-
pub with_deps: bool,
52-
pub no_sysroot: bool,
53-
pub path: PathBuf,
54-
pub enable_build_scripts: bool,
55-
pub enable_proc_macros: bool,
56-
pub skip_inference: bool,
57-
}
58-
59-
impl AnalysisStatsCmd {
46+
impl flags::AnalysisStats {
6047
pub fn run(self, verbosity: Verbosity) -> Result<()> {
6148
let mut rng = {
6249
let seed = SystemTime::now().duration_since(UNIX_EPOCH).unwrap().as_millis() as u64;
@@ -67,8 +54,8 @@ impl AnalysisStatsCmd {
6754
let mut cargo_config = CargoConfig::default();
6855
cargo_config.no_sysroot = self.no_sysroot;
6956
let load_cargo_config = LoadCargoConfig {
70-
load_out_dirs_from_check: self.enable_build_scripts,
71-
with_proc_macro: self.enable_proc_macros,
57+
load_out_dirs_from_check: !self.disable_build_scripts,
58+
with_proc_macro: !self.disable_proc_macros,
7259
prefill_caches: false,
7360
};
7461
let (host, vfs, _proc_macro) =
Lines changed: 61 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,84 +1,86 @@
1-
//! Analyze all modules in a project for diagnostics. Exits with a non-zero status
2-
//! code if any errors are found.
1+
//! Analyze all modules in a project for diagnostics. Exits with a non-zero
2+
//! status code if any errors are found.
33
4-
use std::path::Path;
5-
6-
use anyhow::anyhow;
74
use rustc_hash::FxHashSet;
85

96
use hir::{db::HirDatabase, Crate, Module};
107
use ide::{AssistResolveStrategy, DiagnosticsConfig, Severity};
118
use ide_db::base_db::SourceDatabaseExt;
129

1310
use crate::cli::{
11+
flags,
1412
load_cargo::{load_workspace_at, LoadCargoConfig},
15-
Result,
1613
};
1714

18-
fn all_modules(db: &dyn HirDatabase) -> Vec<Module> {
19-
let mut worklist: Vec<_> =
20-
Crate::all(db).into_iter().map(|krate| krate.root_module(db)).collect();
21-
let mut modules = Vec::new();
15+
impl flags::Diagnostics {
16+
pub fn run(self) -> anyhow::Result<()> {
17+
let cargo_config = Default::default();
18+
let load_cargo_config = LoadCargoConfig {
19+
load_out_dirs_from_check: !self.disable_build_scripts,
20+
with_proc_macro: !self.disable_proc_macros,
21+
prefill_caches: false,
22+
};
23+
let (host, _vfs, _proc_macro) =
24+
load_workspace_at(&self.path, &cargo_config, &load_cargo_config, &|_| {})?;
25+
let db = host.raw_database();
26+
let analysis = host.analysis();
2227

23-
while let Some(module) = worklist.pop() {
24-
modules.push(module);
25-
worklist.extend(module.children(db));
26-
}
27-
28-
modules
29-
}
28+
let mut found_error = false;
29+
let mut visited_files = FxHashSet::default();
3030

31-
pub fn diagnostics(
32-
path: &Path,
33-
load_out_dirs_from_check: bool,
34-
with_proc_macro: bool,
35-
) -> Result<()> {
36-
let cargo_config = Default::default();
37-
let load_cargo_config =
38-
LoadCargoConfig { load_out_dirs_from_check, with_proc_macro, prefill_caches: false };
39-
let (host, _vfs, _proc_macro) =
40-
load_workspace_at(path, &cargo_config, &load_cargo_config, &|_| {})?;
41-
let db = host.raw_database();
42-
let analysis = host.analysis();
31+
let work = all_modules(db).into_iter().filter(|module| {
32+
let file_id = module.definition_source(db).file_id.original_file(db);
33+
let source_root = db.file_source_root(file_id);
34+
let source_root = db.source_root(source_root);
35+
!source_root.is_library
36+
});
4337

44-
let mut found_error = false;
45-
let mut visited_files = FxHashSet::default();
38+
for module in work {
39+
let file_id = module.definition_source(db).file_id.original_file(db);
40+
if !visited_files.contains(&file_id) {
41+
let crate_name =
42+
module.krate().display_name(db).as_deref().unwrap_or("unknown").to_string();
43+
println!("processing crate: {}, module: {}", crate_name, _vfs.file_path(file_id));
44+
for diagnostic in analysis
45+
.diagnostics(
46+
&DiagnosticsConfig::default(),
47+
AssistResolveStrategy::None,
48+
file_id,
49+
)
50+
.unwrap()
51+
{
52+
if matches!(diagnostic.severity, Severity::Error) {
53+
found_error = true;
54+
}
4655

47-
let work = all_modules(db).into_iter().filter(|module| {
48-
let file_id = module.definition_source(db).file_id.original_file(db);
49-
let source_root = db.file_source_root(file_id);
50-
let source_root = db.source_root(source_root);
51-
!source_root.is_library
52-
});
53-
54-
for module in work {
55-
let file_id = module.definition_source(db).file_id.original_file(db);
56-
if !visited_files.contains(&file_id) {
57-
let crate_name =
58-
module.krate().display_name(db).as_deref().unwrap_or("unknown").to_string();
59-
println!("processing crate: {}, module: {}", crate_name, _vfs.file_path(file_id));
60-
for diagnostic in analysis
61-
.diagnostics(&DiagnosticsConfig::default(), AssistResolveStrategy::None, file_id)
62-
.unwrap()
63-
{
64-
if matches!(diagnostic.severity, Severity::Error) {
65-
found_error = true;
56+
println!("{:?}", diagnostic);
6657
}
6758

68-
println!("{:?}", diagnostic);
59+
visited_files.insert(file_id);
6960
}
61+
}
62+
63+
println!();
64+
println!("diagnostic scan complete");
7065

71-
visited_files.insert(file_id);
66+
if found_error {
67+
println!();
68+
anyhow::bail!("diagnostic error detected")
7269
}
70+
71+
Ok(())
7372
}
73+
}
7474

75-
println!();
76-
println!("diagnostic scan complete");
75+
fn all_modules(db: &dyn HirDatabase) -> Vec<Module> {
76+
let mut worklist: Vec<_> =
77+
Crate::all(db).into_iter().map(|krate| krate.root_module(db)).collect();
78+
let mut modules = Vec::new();
7779

78-
if found_error {
79-
println!();
80-
Err(anyhow!("diagnostic error detected"))
81-
} else {
82-
Ok(())
80+
while let Some(module) = worklist.pop() {
81+
modules.push(module);
82+
worklist.extend(module.children(db));
8383
}
84+
85+
modules
8486
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
//! Read Rust code on stdin, print HTML highlighted version to stdout.
2+
3+
use ide::Analysis;
4+
5+
use crate::cli::{flags, read_stdin};
6+
7+
impl flags::Highlight {
8+
pub fn run(self) -> anyhow::Result<()> {
9+
let (analysis, file_id) = Analysis::from_single_file(read_stdin()?);
10+
let html = analysis.highlight_as_html(file_id, self.rainbow).unwrap();
11+
println!("{}", html);
12+
Ok(())
13+
}
14+
}

crates/rust-analyzer/src/cli/parse.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
//! Read Rust code on stdin, print syntax tree on stdout.
2+
use syntax::{AstNode, SourceFile};
3+
4+
use crate::cli::{flags, read_stdin};
5+
6+
impl flags::Parse {
7+
pub fn run(self) -> anyhow::Result<()> {
8+
let _p = profile::span("parsing");
9+
let text = read_stdin()?;
10+
let file = SourceFile::parse(&text).tree();
11+
if !self.no_dump {
12+
println!("{:#?}", file.syntax());
13+
}
14+
std::mem::forget(file);
15+
Ok(())
16+
}
17+
}

0 commit comments

Comments
 (0)