Prettyplease default, rustfmt opt-in via config

This commit is contained in:
David Tolnay 2022-08-13 11:48:46 -07:00
parent 5902eff89c
commit 9e6d9270e7
No known key found for this signature in database
GPG key ID: F9BA143B95FF6D82
5 changed files with 28 additions and 37 deletions

View file

@ -19,8 +19,6 @@ jobs:
components: rustfmt
- run: cargo run -- expand --manifest-path tests/Cargo.toml > expand.rs
- run: diff tests/lib.expand.rs expand.rs
- run: cargo run --features prettyplease -- expand --manifest-path tests/Cargo.toml > expand.rs
- run: diff tests/lib.expand.rs expand.rs
build:
name: Rust ${{matrix.rust}}
@ -28,17 +26,15 @@ jobs:
strategy:
fail-fast: false
matrix:
rust: [nightly, beta, stable, 1.58.0]
rust: [nightly, beta, stable, 1.60.0]
steps:
- uses: actions/checkout@v3
- uses: dtolnay/rust-toolchain@master
with:
toolchain: ${{matrix.rust}}
- run: cargo check
- run: cargo check --features prettyplease
- run: cargo update
- run: cargo check
- run: cargo check --features prettyplease
- run: cargo test
clippy:

View file

@ -12,12 +12,16 @@ license = "MIT OR Apache-2.0"
readme = "README.md"
repository = "https://github.com/dtolnay/cargo-expand"
[features]
default = ["prettyplease"]
prettyplease = []
[dependencies]
atty = "0.2"
bat = { version = "0.21", default-features = false, features = ["paging", "regex-fancy"] }
cargo-subcommand-metadata = "0.1"
clap = { version = "3.2.5", default-features = false, features = ["deprecated", "derive", "std", "suggestions"] }
prettyplease = { version = "0.1.13", optional = true, features = ["verbatim"] }
prettyplease = { version = "0.1.13", features = ["verbatim"] }
proc-macro2 = "1.0"
quote = { version = "1.0", default-features = false }
serde = { version = "1.0", features = ["derive"] }

View file

@ -4,12 +4,10 @@ use std::path::PathBuf;
fn main() {
let mut version = env!("CARGO_PKG_VERSION").to_owned();
if cfg!(feature = "prettyplease") {
if let Ok(prettyplease_version) = env::var("DEP_PRETTYPLEASE01_VERSION") {
// TODO: Make this appear only if `--version --verbose` is used.
version.push_str(" + prettyplease ");
version.push_str(&prettyplease_version);
}
if let Ok(prettyplease_version) = env::var("DEP_PRETTYPLEASE01_VERSION") {
// TODO: Make this appear only if `--version --verbose` is used.
version.push_str(" + prettyplease ");
version.push_str(&prettyplease_version);
}
let out_dir = PathBuf::from(env::var("OUT_DIR").unwrap());

View file

@ -16,6 +16,11 @@ pub struct Config {
pub color: Option<String>,
#[serde(default)]
pub pager: bool,
/// Format using rustfmt instead of prettyplease. This is significantly
/// slower, and less reliable on macro-generated code, but produces more
/// aesthetic formatting when it works.
#[serde(default)]
pub rustfmt: bool,
}
pub fn deserialize() -> Config {

View file

@ -33,13 +33,11 @@ use std::env;
use std::ffi::OsString;
use std::fs;
use std::io::{self, BufRead, Write};
#[cfg(feature = "prettyplease")]
use std::panic::{self, PanicInfo, UnwindSafe};
use std::path::{Path, PathBuf};
use std::process::{self, Command, Stdio};
use std::ptr;
use std::str::FromStr;
#[cfg(feature = "prettyplease")]
use std::thread::Result as ThreadResult;
use termcolor::{Color::Green, ColorChoice, ColorSpec, StandardStream, WriteColor};
@ -139,7 +137,6 @@ fn cargo_expand() -> Result<i32> {
return Ok(0);
}
let mut rustfmt = None;
if let Some(item) = &args.item {
if args.ugly {
let _ = writeln!(
@ -149,20 +146,18 @@ fn cargo_expand() -> Result<i32> {
);
return Ok(1);
}
if !cfg!(feature = "prettyplease") {
rustfmt = which_rustfmt();
if rustfmt.is_none() {
let _ = writeln!(
io::stderr(),
"ERROR: cannot expand single item ({}) without rustfmt.",
item,
);
let _ = writeln!(
io::stderr(),
"Install rustfmt by running `rustup component add rustfmt --toolchain nightly`.",
);
return Ok(1);
}
}
let mut rustfmt = None;
if config.rustfmt {
rustfmt = which_rustfmt();
if rustfmt.is_none() {
let _ = io::stderr().write_all(
b"ERROR: cargo-expand configuration sets rustfmt=true, but \
rustfmt is not found. Install rustfmt by running `rustup \
component add rustfmt --toolchain nightly`.\n",
);
return Ok(1);
}
}
@ -221,13 +216,7 @@ fn cargo_expand() -> Result<i32> {
return Ok(1);
}
}
#[cfg(feature = "prettyplease")]
// This is behind a feature because it's probably not mature enough
// to use in panic=abort mode yet. I'll remove the feature and do
// this by default when prettyplease is further along, or when
// cfg(panic = "unwind") is stabilized, whichever comes first.
// Tracking issue: https://github.com/rust-lang/rust/issues/77443
{
if !config.rustfmt {
if let Ok(formatted) = ignore_panic(|| prettyplease::unparse(&syntax_tree)) {
stage = Stage::Formatted(formatted);
}
@ -506,7 +495,6 @@ fn ignore_cargo_err(line: &str) -> bool {
false
}
#[cfg(feature = "prettyplease")]
fn ignore_panic<F, T>(f: F) -> ThreadResult<T>
where
F: UnwindSafe + FnOnce() -> T,