Skip to content

Commit

Permalink
internal: ⬆️ xflags
Browse files Browse the repository at this point in the history
The main change here should be that flags are not inhereted, so

   $ rust-analyzer analysis-stats . -v -v

would do what it should do

We also no longer Don\'t
  • Loading branch information
matklad committed Oct 8, 2022
1 parent 8437e4b commit 39fa8b5
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 48 deletions.
8 changes: 4 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion crates/rust-analyzer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ itertools = "0.10.3"
scip = "0.1.1"
lsp-types = { version = "0.93.1", features = ["proposed"] }
parking_lot = "0.12.1"
xflags = "0.2.4"
xflags = "0.3.0"
oorandom = "11.1.3"
rustc-hash = "1.1.0"
serde = { version = "1.0.137", features = ["derive"] }
Expand Down
11 changes: 3 additions & 8 deletions crates/rust-analyzer/src/bin/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,15 @@ fn main() {
process::exit(code);
}

if let Err(err) = try_main() {
let flags = flags::RustAnalyzer::from_env_or_exit();
if let Err(err) = try_main(flags) {
tracing::error!("Unexpected error: {}", err);
eprintln!("{}", err);
process::exit(101);
}
}

fn try_main() -> Result<()> {
let flags = flags::RustAnalyzer::from_env()?;

fn try_main(flags: flags::RustAnalyzer) -> Result<()> {
#[cfg(debug_assertions)]
if flags.wait_dbg || env::var("RA_WAIT_DBG").is_ok() {
#[allow(unused_mut)]
Expand Down Expand Up @@ -76,10 +75,6 @@ fn try_main() -> Result<()> {
println!("rust-analyzer {}", rust_analyzer::version());
return Ok(());
}
if cmd.help {
println!("{}", flags::RustAnalyzer::HELP);
return Ok(());
}
with_extra_thread("LspServer", run_server)?;
}
flags::RustAnalyzerCmd::ProcMacro(flags::ProcMacro) => {
Expand Down
31 changes: 15 additions & 16 deletions crates/rust-analyzer/src/cli/flags.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@ xflags::xflags! {
default cmd lsp-server {
/// Print version.
optional --version
/// Print help.
optional -h, --help

/// Dump a LSP config JSON schema.
optional --print-config-schema
Expand All @@ -54,10 +52,10 @@ xflags::xflags! {
}

/// Batch typecheck project and print summary statistics
cmd analysis-stats
cmd analysis-stats {
/// Directory with Cargo.toml.
required path: PathBuf
{

optional --output format: OutputFormat

/// Randomize order in which crates, modules, and items are processed.
Expand All @@ -84,38 +82,37 @@ xflags::xflags! {
optional --skip-inference
}

cmd diagnostics
cmd diagnostics {
/// Directory with Cargo.toml.
required path: PathBuf
{

/// Don't run build scripts or load `OUT_DIR` values by running `cargo check` before analysis.
optional --disable-build-scripts
/// Don't use expand proc macros.
optional --disable-proc-macros
}

cmd ssr
cmd ssr {
/// A structured search replace rule (`$a.foo($b) ==> bar($a, $b)`)
repeated rule: SsrRule
{}
}

cmd search
cmd search {
/// A structured search replace pattern (`$a.foo($b)`)
repeated pattern: SsrPattern
{
/// Prints debug information for any nodes with source exactly equal to snippet.
optional --debug snippet: String
}

cmd proc-macro {}

cmd lsif
cmd lsif {
required path: PathBuf
{}
}

cmd scip
cmd scip {
required path: PathBuf
{}
}
}
}

Expand Down Expand Up @@ -150,7 +147,6 @@ pub enum RustAnalyzerCmd {
#[derive(Debug)]
pub struct LspServer {
pub version: bool,
pub help: bool,
pub print_config_schema: bool,
}

Expand Down Expand Up @@ -218,7 +214,10 @@ pub struct Scip {
}

impl RustAnalyzer {
pub const HELP: &'static str = Self::HELP_;
#[allow(dead_code)]
pub fn from_env_or_exit() -> Self {
Self::from_env_or_exit_()
}

#[allow(dead_code)]
pub fn from_env() -> xflags::Result<Self> {
Expand Down
2 changes: 1 addition & 1 deletion xtask/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ anyhow = "1.0.57"
flate2 = "1.0.24"
write-json = "0.1.2"
xshell = "0.2.2"
xflags = "0.2.4"
xflags = "0.3.0"
# Avoid adding more dependencies to this crate
19 changes: 6 additions & 13 deletions xtask/src/flags.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,6 @@ xflags::xflags! {

/// Run custom build command.
cmd xtask {
default cmd help {
/// Print help information.
optional -h, --help
}

/// Install rust-analyzer server or editor plugin.
cmd install {
Expand Down Expand Up @@ -42,9 +38,9 @@ xflags::xflags! {
optional --dry-run
}
/// Builds a benchmark version of rust-analyzer and puts it into `./target`.
cmd bb
cmd bb {
required suffix: String
{}
}
}
}

Expand All @@ -58,7 +54,6 @@ pub struct Xtask {

#[derive(Debug)]
pub enum XtaskCmd {
Help(Help),
Install(Install),
FuzzTests(FuzzTests),
Release(Release),
Expand All @@ -68,11 +63,6 @@ pub enum XtaskCmd {
Bb(Bb),
}

#[derive(Debug)]
pub struct Help {
pub help: bool,
}

#[derive(Debug)]
pub struct Install {
pub client: bool,
Expand Down Expand Up @@ -111,7 +101,10 @@ pub struct Bb {
}

impl Xtask {
pub const HELP: &'static str = Self::HELP_;
#[allow(dead_code)]
pub fn from_env_or_exit() -> Self {
Self::from_env_or_exit_()
}

#[allow(dead_code)]
pub fn from_env() -> xflags::Result<Self> {
Expand Down
7 changes: 2 additions & 5 deletions xtask/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,12 @@ use std::{
use xshell::{cmd, Shell};

fn main() -> anyhow::Result<()> {
let flags = flags::Xtask::from_env_or_exit();

let sh = &Shell::new()?;
sh.change_dir(project_root());

let flags = flags::Xtask::from_env()?;
match flags.subcommand {
flags::XtaskCmd::Help(_) => {
println!("{}", flags::Xtask::HELP);
Ok(())
}
flags::XtaskCmd::Install(cmd) => cmd.run(sh),
flags::XtaskCmd::FuzzTests(_) => run_fuzzer(sh),
flags::XtaskCmd::Release(cmd) => cmd.run(sh),
Expand Down

0 comments on commit 39fa8b5

Please sign in to comment.