From 7e16140b780d292da18fa2dfa14de514a1cac672 Mon Sep 17 00:00:00 2001 From: Adam Leventhal Date: Sun, 3 Oct 2021 21:14:01 -0700 Subject: [PATCH] improved whitespace handling (#6) --- Cargo.lock | 1 + Cargo.toml | 1 + src/main.rs | 8 ++++---- 3 files changed, 6 insertions(+), 4 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 326af4c..6067fef 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -175,6 +175,7 @@ dependencies = [ "openapiv3", "proc-macro2", "quote", + "regex", "rustfmt-wrapper", "schemars", "serde", diff --git a/Cargo.toml b/Cargo.toml index 0821af4..2f53f79 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,6 +13,7 @@ indexmap = "1.7.0" openapiv3 = "1.0.0-beta.2" proc-macro2 = "1.0.29" quote = "1.0.9" +regex = "1.5.4" rustfmt-wrapper = { git = "https://github.com/oxidecomputer/typify" } schemars = "0.8.5" serde = { version = "1", features = [ "derive" ] } diff --git a/src/main.rs b/src/main.rs index e7197ac..35b09ad 100644 --- a/src/main.rs +++ b/src/main.rs @@ -824,10 +824,10 @@ fn generate(api: &OpenAPI, ts: &mut TypeSpace) -> Result { #(#methods)* } }; - let file = rustfmt_wrapper::rustfmt(file) - .unwrap() - // Format any indented item. - .replace(" }\n", " }\n\n"); + let regex = regex::Regex::new(r#"(})(\n\s*[^} ])"#).unwrap(); + let file = rustfmt_wrapper::rustfmt(file).unwrap(); + + let file = regex.replace_all(&file, "$1\n$2").to_string(); Ok(file) }