]> 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 41e4ee2931d484dede30a56e5daff0adb6091292..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::{config, run, Input};
+use rustfmt::{load_config, CliOptions, Input, Session};
 
 fn prune_files(files: Vec<&str>) -> Vec<&str> {
     let prefixes: Vec<_> = files
@@ -32,16 +33,7 @@ fn prune_files(files: Vec<&str>) -> Vec<&str> {
 
     let mut pruned_prefixes = vec![];
     for p1 in prefixes {
-        let mut include = true;
-        if !p1.starts_with("src/bin/") {
-            for p2 in &pruned_prefixes {
-                if p1.starts_with(p2) {
-                    include = false;
-                    break;
-                }
-            }
-        }
-        if include {
+        if p1.starts_with("src/bin/") || pruned_prefixes.iter().all(|p2| !p1.starts_with(p2)) {
             pruned_prefixes.push(p1);
         }
     }
@@ -50,17 +42,10 @@ fn prune_files(files: Vec<&str>) -> Vec<&str> {
     files
         .into_iter()
         .filter(|f| {
-            let mut include = true;
             if f.ends_with("mod.rs") || f.ends_with("lib.rs") || f.starts_with("src/bin/") {
                 return true;
             }
-            for pp in &pruned_prefixes {
-                if f.starts_with(pp) {
-                    include = false;
-                    break;
-                }
-            }
-            include
+            pruned_prefixes.iter().all(|pp| !f.starts_with(pp))
         })
         .collect()
 }
@@ -84,19 +69,35 @@ fn get_files(input: &str) -> Vec<&str> {
 }
 
 fn fmt_files(files: &[&str]) -> i32 {
-    let (config, _) = config::Config::from_resolved_toml_path(Path::new("."))
-        .unwrap_or_else(|_| (config::Config::default(), None));
+    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 = run(Input::File(PathBuf::from(file)), &config);
-        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");
@@ -187,7 +188,8 @@ fn main() {
     env_logger::init();
 
     let opts = make_opts();
-    let matches = opts.parse(env::args().skip(1))
+    let matches = opts
+        .parse(env::args().skip(1))
         .expect("Couldn't parse command line");
     let config = Config::from_args(&matches, &opts);