Skip to content
7 changes: 7 additions & 0 deletions Source/Eliminate/CLI.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,12 @@ pub struct Cli {
/// Print a line for every binding that is inlined.
#[clap(long, short = 'v')]
pub Verbose:bool,

/// Reformat the entire file with prettyplease after inlining.
/// Default: only the inlined binding sites are rewritten; comments,
/// blank lines, and indentation are preserved verbatim.
#[clap(long, short = 'r')]
pub Reformat:bool,
}

impl Cli {
Expand All @@ -55,6 +61,7 @@ impl Cli {
InlineComments:self.InlineComments,
DryRun:self.DryRun,
Verbose:self.Verbose,
Reformat:self.Reformat,
};

let Stats = Process::Process(&self.Path, &self.Glob, &Options)?;
Expand Down
9 changes: 9 additions & 0 deletions Source/Eliminate/Definition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,14 @@ pub struct Options {

/// When `true`, emit per-binding log lines.
pub Verbose:bool,

/// When `true`, reformat the entire file with `prettyplease` after
/// inlining (the previous default behaviour).
///
/// Default `false` - only the inlined binding sites are rewritten;
/// all comments, blank lines, section banners, and the original
/// indentation style are preserved verbatim.
pub Reformat:bool,
}

impl Default for Options {
Expand All @@ -32,6 +40,7 @@ impl Default for Options {
InlineComments:false,
DryRun:false,
Verbose:false,
Reformat:false,
}
}
}
Expand Down
19 changes: 17 additions & 2 deletions Source/Eliminate/Process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@ use super::{Definition, Error, Transform};
/// elimination transform on each, and write back the result unless
/// `Options.DryRun` is set.
///
/// When `Options.Reformat` is `false` (the default) the preserve-layout path
/// is used: only the inlined binding sites are rewritten; comments, blank
/// lines, and indentation style are kept verbatim.
///
/// When `Options.Reformat` is `true` the whole file is reformatted with
/// `prettyplease` after inlining (the previous unconditional behaviour).
///
/// Returns aggregate [`Definition::Stats`] describing what was processed.
pub fn Process(Root:&Path, Pattern:&str, Options:&Definition::Options) -> Error::Result<Definition::Stats> {
let mut Stats = Definition::Stats::default();
Expand Down Expand Up @@ -69,8 +76,16 @@ fn CollectFiles(Root:&Path, GlobMatcher:&GlobSet) -> Vec<PathBuf> {
fn ProcessFile(FilePath:&Path, Options:&Definition::Options, Stats:&mut Definition::Stats) -> Error::Result<()> {
let Source = fs::read_to_string(FilePath)?;

let TransformResult = Transform::Run(&Source, Options).map_err(|E| {
if let Error::Error::Parse { Source: Src, .. } = E {
// Choose the transform path based on Options.Reformat.
// - Reformat:false (default): text-level substitution, layout preserved.
// - Reformat:true: full prettyplease reformat (previous behaviour).
let TransformResult = if Options.Reformat {
Transform::Run(&Source, Options)
} else {
Transform::RunPreserve(&Source, Options)
}
.map_err(|E| {
if let Error::Error::Parse { Source:Src, .. } = E {
Error::Error::Parse { Path:FilePath.display().to_string(), Source:Src }
} else {
E
Expand Down
Loading