From cb64ab22ecd24823b175db309a695420eab86746 Mon Sep 17 00:00:00 2001 From: Jan Likar <likar.jan@gmail.com> Date: Fri, 13 Nov 2015 01:13:25 +0100 Subject: [PATCH 1/2] Enable rustfmt to format a list of files Fix #580 by allowing rustfmt to accept a list of files. This also enables usage of shell wildcard expansion, although notably this does not work with cmd.exe on Windows. For example: 'rustfmt *.rs' will format all rust files in the current working directory. - Change usage text to show rustfmt will accept a list of files - Change "Using rustfmt config file: {}" message to "Using rustfmt config file {} for {}" - Change Operation::Format(PathBuf, WriteMode) to Operation::Format(Vec<PathBuf>, WriteMode) - Loop through Vec<PathBuf>, load config and call 'run' for each path --- src/bin/rustfmt.rs | 36 +++++++++++++++++++++++------------- 1 file changed, 23 insertions(+), 13 deletions(-) diff --git a/src/bin/rustfmt.rs b/src/bin/rustfmt.rs index de906729b31..d9f4d46ec30 100644 --- a/src/bin/rustfmt.rs +++ b/src/bin/rustfmt.rs @@ -29,8 +29,8 @@ use getopts::Options; /// Rustfmt operations. enum Operation { - /// Format a file and its child modules. - Format(PathBuf, WriteMode), + /// Format files and its child modules. + Format(Vec<PathBuf>, WriteMode), /// Print the help message. Help, /// Print detailed configuration help. @@ -114,16 +114,20 @@ fn execute() -> i32 { run_from_stdin(input, write_mode, &config); 0 } - Operation::Format(file, write_mode) => { - let config = match lookup_and_read_project_file(&file) { - Ok((path, toml)) => { - println!("Using rustfmt config file: {}", path.display()); - Config::from_toml(&toml) - } - Err(_) => Default::default(), - }; + Operation::Format(files, write_mode) => { + for file in files { + let config = match lookup_and_read_project_file(&file) { + Ok((path, toml)) => { + println!("Using rustfmt config file {} for {}", + path.display(), + file.display()); + Config::from_toml(&toml) + } + Err(_) => Default::default(), + }; - run(&file, write_mode, &config); + run(&file, write_mode, &config); + } 0 } } @@ -144,7 +148,7 @@ fn main() { } fn print_usage(opts: &Options, reason: &str) { - let reason = format!("{}\nusage: {} [options] <file>", + let reason = format!("{}\nusage: {} [options] <file>...", reason, env::current_exe().unwrap().display()); println!("{}", opts.usage(&reason)); @@ -189,5 +193,11 @@ fn determine_operation<I>(opts: &Options, args: I) -> Operation None => WriteMode::Replace, }; - Operation::Format(PathBuf::from(&matches.free[0]), write_mode) + let mut files = Vec::with_capacity(matches.free.len()); + + for arg in matches.free { + files.push(PathBuf::from(arg)); + } + + Operation::Format(files, write_mode) } From 4443c4b7ccc0f53b94bf2d47c1907047ba42d0bf Mon Sep 17 00:00:00 2001 From: Jan Likar <likar.jan@gmail.com> Date: Fri, 13 Nov 2015 02:08:57 +0100 Subject: [PATCH 2/2] Correct some non-idiomatic code --- src/bin/rustfmt.rs | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/src/bin/rustfmt.rs b/src/bin/rustfmt.rs index d9f4d46ec30..b90cdb3d3ca 100644 --- a/src/bin/rustfmt.rs +++ b/src/bin/rustfmt.rs @@ -29,7 +29,7 @@ use getopts::Options; /// Rustfmt operations. enum Operation { - /// Format files and its child modules. + /// Format files and their child modules. Format(Vec<PathBuf>, WriteMode), /// Print the help message. Help, @@ -193,11 +193,7 @@ fn determine_operation<I>(opts: &Options, args: I) -> Operation None => WriteMode::Replace, }; - let mut files = Vec::with_capacity(matches.free.len()); - - for arg in matches.free { - files.push(PathBuf::from(arg)); - } + let files: Vec<_> = matches.free.iter().map(|a| PathBuf::from(a)).collect(); Operation::Format(files, write_mode) }