Skip to content

Commit

Permalink
Format macro-generated code with prettyplease
Browse files Browse the repository at this point in the history
  • Loading branch information
smoelius committed Dec 5, 2023
1 parent fd12133 commit bccff6d
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 27 deletions.
3 changes: 1 addition & 2 deletions macro/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,10 @@ darling = "0.20"
if_chain = "1.0"
itertools = "0.12"
once_cell = "1.18"
prettyplease = "0.2"
proc-macro2 = "1.0"
quote = "1.0"
subprocess = "0.2"
syn = { version = "2.0", features = ["full", "parsing", "visit", "visit-mut"] }
toolchain_find = "0.4"

internal = { path = "../internal", package = "test-fuzz-internal", version = "=4.0.4", optional = true }

Expand Down
31 changes: 6 additions & 25 deletions macro/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,15 @@ use std::{
collections::{BTreeMap, BTreeSet},
convert::TryFrom,
env::var,
io::Write,
str::FromStr,
};
use subprocess::{Exec, Redirection};
use syn::{
parse::Parser, parse_macro_input, parse_quote, parse_str, punctuated::Punctuated, token,
Attribute, Block, Expr, FnArg, GenericArgument, GenericParam, Generics, Ident, ImplItem,
ImplItemFn, ItemFn, ItemImpl, ItemMod, LifetimeParam, PatType, Path, PathArguments,
parse::Parser, parse2, parse_macro_input, parse_quote, parse_str, punctuated::Punctuated,
token, Attribute, Block, Expr, File, FnArg, GenericArgument, GenericParam, Generics, Ident,
ImplItem, ImplItemFn, ItemFn, ItemImpl, ItemMod, LifetimeParam, PatType, Path, PathArguments,
PathSegment, Receiver, ReturnType, Signature, Stmt, Type, TypeParam, TypePath, TypeReference,
TypeSlice, Visibility, WhereClause, WherePredicate,
};
use toolchain_find::find_installed_component;

mod auto_concretize;

Expand Down Expand Up @@ -1077,25 +1074,9 @@ fn args_from_autos(autos: &[Expr]) -> Expr {

fn log(tokens: &TokenStream2) {
if log_enabled() {
find_installed_component("rustfmt").map_or_else(
|| {
println!("{tokens}");
},
|rustfmt| {
let mut popen = Exec::cmd(rustfmt)
.stdin(Redirection::Pipe)
.popen()
.expect("`popen` failed");
let mut stdin = popen
.stdin
.take()
.expect("Could not take `rustfmt`'s standard input");
write!(stdin, "{tokens}").expect("Could not write to `rustfmt`'s standard input");
drop(stdin);
let status = popen.wait().expect("`wait` failed");
assert!(status.success(), "`rustfmt` failed");
},
);
let syntax_tree: File = parse2(tokens.clone()).expect("Could not parse tokens");
let formatted = prettyplease::unparse(&syntax_tree);
print!("{formatted}");
}
}

Expand Down

0 comments on commit bccff6d

Please sign in to comment.