]> git.lizzy.rs Git - rust.git/blobdiff - src/git-rustfmt/main.rs
rewrite_string: retain blank lines that are trailing
[rust.git] / src / git-rustfmt / main.rs
index 41b16969dc236145696ff04f52acba2efb616886..4ad004d3a8b16aa7640e84f08779301f5eb9399a 100644 (file)
 extern crate rustfmt_nightly as rustfmt;
 
 use std::env;
+use std::io::stdout;
 use std::path::{Path, PathBuf};
 use std::process::Command;
 use std::str::FromStr;
 
 use getopts::{Matches, Options};
 
-use rustfmt::{format_and_emit_report, load_config, Input};
+use rustfmt::{load_config, CliOptions, Input, Session};
 
 fn prune_files(files: Vec<&str>) -> Vec<&str> {
     let prefixes: Vec<_> = files
@@ -68,18 +69,35 @@ fn get_files(input: &str) -> Vec<&str> {
 }
 
 fn fmt_files(files: &[&str]) -> i32 {
-    let (config, _) = load_config(Some(Path::new(".")), None).expect("couldn't load config");
+    let (config, _) =
+        load_config::<NullOptions>(Some(Path::new(".")), None).expect("couldn't load config");
 
     let mut exit_code = 0;
+    let mut out = stdout();
+    let mut session = Session::new(config, Some(&mut out));
     for file in files {
-        let summary = format_and_emit_report(Input::File(PathBuf::from(file)), &config).unwrap();
-        if !summary.has_no_errors() {
+        let report = session.format(Input::File(PathBuf::from(file))).unwrap();
+        if report.has_warnings() {
+            eprintln!("{}", report);
+        }
+        if !session.has_no_errors() {
             exit_code = 1;
         }
     }
     exit_code
 }
 
+struct NullOptions;
+
+impl CliOptions for NullOptions {
+    fn apply_to(self, _: &mut rustfmt::Config) {
+        unreachable!();
+    }
+    fn config_path(&self) -> Option<&Path> {
+        unreachable!();
+    }
+}
+
 fn uncommitted_files() -> Vec<String> {
     let mut cmd = Command::new("git");
     cmd.arg("ls-files");