]> git.lizzy.rs Git - rust.git/commitdiff
add new flag to list names of misformatted files (#3747)
authorCaleb Cartwright <calebcartwright@users.noreply.github.com>
Mon, 19 Aug 2019 02:04:40 +0000 (21:04 -0500)
committerSeiichi Uchida <seuchida@gmail.com>
Mon, 19 Aug 2019 02:04:40 +0000 (11:04 +0900)
Configurations.md
src/bin/main.rs
src/config/mod.rs
src/emitter/diff.rs
src/emitter/files.rs
src/lib.rs

index 9291da05d303c3cc401dd4326663f858895bf6c7..e1885aba836db8d5f19d1e93d647ed107197d842 100644 (file)
@@ -2445,3 +2445,7 @@ Internal option
 ## `make_backup`
 
 Internal option, use `--backup`
+
+## `print_misformatted_file_names`
+
+Internal option, use `-l` or `--files-with-diff`
index 9d3be60ac8f053ffce949d5217b14b6e37546c23..bb12169329a1e079a1ad601ecfca9f9af674639b 100644 (file)
@@ -126,6 +126,12 @@ fn make_opts() -> Options {
          `current` writes to stdout current config as if formatting the file at PATH.",
         "[default|minimal|current] PATH",
     );
+    opts.optflag(
+        "l",
+        "files-with-diff",
+        "Prints the names of mismatched files that were formatted. Prints the names of \
+         files that would be formated when used with `--check` mode. ",
+    );
 
     if is_nightly {
         opts.optflag(
@@ -480,6 +486,7 @@ struct GetOptsOptions {
     file_lines: FileLines, // Default is all lines in all files.
     unstable_features: bool,
     error_on_unformatted: Option<bool>,
+    print_misformatted_file_names: bool,
 }
 
 impl GetOptsOptions {
@@ -547,6 +554,10 @@ pub fn from_matches(matches: &Matches) -> Result<GetOptsOptions, FailureError> {
             options.backup = true;
         }
 
+        if matches.opt_present("files-with-diff") {
+            options.print_misformatted_file_names = true;
+        }
+
         if !rust_nightly {
             if !STABLE_EMIT_MODES.contains(&options.emit_mode) {
                 return Err(format_err!(
@@ -610,6 +621,9 @@ fn apply_to(self, config: &mut Config) {
         if let Some(color) = self.color {
             config.set().color(color);
         }
+        if self.print_misformatted_file_names {
+            config.set().print_misformatted_file_names(true);
+        }
     }
 
     fn config_path(&self) -> Option<&Path> {
index fa3a0b28e1473b73dd0d066eb624f0d34e81fa21..a4930b7c575151305748b5d41c635bcb2e1f455f 100644 (file)
     emit_mode: EmitMode, EmitMode::Files, false,
         "What emit Mode to use when none is supplied";
     make_backup: bool, false, false, "Backup changed files";
+    print_misformatted_file_names: bool, false, true,
+        "Prints the names of mismatched files that were formatted. Prints the names of \
+         files that would be formated when used with `--check` mode. ";
 }
 
 impl PartialConfig {
@@ -161,6 +164,7 @@ pub fn to_toml(&self) -> Result<String, String> {
         cloned.file_lines = None;
         cloned.verbose = None;
         cloned.width_heuristics = None;
+        cloned.print_misformatted_file_names = None;
 
         ::toml::to_string(&cloned).map_err(|e| format!("Could not output config: {}", e))
     }
index 08a42b4679db9824b0baeb8fb453b37319e163d8..462a80946ee71e0b61d75c2f7b97c53921afa20c 100644 (file)
@@ -15,7 +15,7 @@ pub(crate) fn new(config: Config) -> Self {
 impl Emitter for DiffEmitter {
     fn emit_formatted_file(
         &mut self,
-        _output: &mut dyn Write,
+        output: &mut dyn Write,
         FormattedFile {
             filename,
             original_text,
@@ -25,11 +25,86 @@ fn emit_formatted_file(
         const CONTEXT_SIZE: usize = 3;
         let mismatch = make_diff(&original_text, formatted_text, CONTEXT_SIZE);
         let has_diff = !mismatch.is_empty();
-        print_diff(
-            mismatch,
-            |line_num| format!("Diff in {} at line {}:", filename, line_num),
-            &self.config,
-        );
+
+        if has_diff {
+            if self.config.print_misformatted_file_names() {
+                writeln!(output, "{}", ensure_real_path(filename).display())?;
+            } else {
+                print_diff(
+                    mismatch,
+                    |line_num| format!("Diff in {} at line {}:", filename, line_num),
+                    &self.config,
+                );
+            }
+        }
+
         return Ok(EmitterResult { has_diff });
     }
 }
+
+#[cfg(test)]
+mod tests {
+    use super::*;
+    use crate::config::Config;
+    use crate::FileName;
+    use std::path::PathBuf;
+
+    #[test]
+    fn does_not_print_when_no_files_reformatted() {
+        let mut writer = Vec::new();
+        let config = Config::default();
+        let mut emitter = DiffEmitter::new(config);
+        let result = emitter
+            .emit_formatted_file(
+                &mut writer,
+                FormattedFile {
+                    filename: &FileName::Real(PathBuf::from("src/lib.rs")),
+                    original_text: "fn empty() {}\n",
+                    formatted_text: "fn empty() {}\n",
+                },
+            )
+            .unwrap();
+        assert_eq!(result.has_diff, false);
+        assert_eq!(writer.len(), 0);
+    }
+
+    #[test]
+    fn prints_file_names_when_config_is_enabled() {
+        let bin_file = "src/bin.rs";
+        let bin_original = "fn main() {\nprintln!(\"Hello, world!\");\n}";
+        let bin_formatted = "fn main() {\n    println!(\"Hello, world!\");\n}";
+        let lib_file = "src/lib.rs";
+        let lib_original = "fn greet() {\nprintln!(\"Greetings!\");\n}";
+        let lib_formatted = "fn greet() {\n    println!(\"Greetings!\");\n}";
+
+        let mut writer = Vec::new();
+        let mut config = Config::default();
+        config.set().print_misformatted_file_names(true);
+        let mut emitter = DiffEmitter::new(config);
+        let _ = emitter
+            .emit_formatted_file(
+                &mut writer,
+                FormattedFile {
+                    filename: &FileName::Real(PathBuf::from(bin_file)),
+                    original_text: bin_original,
+                    formatted_text: bin_formatted,
+                },
+            )
+            .unwrap();
+        let _ = emitter
+            .emit_formatted_file(
+                &mut writer,
+                FormattedFile {
+                    filename: &FileName::Real(PathBuf::from(lib_file)),
+                    original_text: lib_original,
+                    formatted_text: lib_formatted,
+                },
+            )
+            .unwrap();
+
+        assert_eq!(
+            String::from_utf8(writer).unwrap(),
+            format!("{}\n{}\n", bin_file, lib_file),
+        )
+    }
+}
index 4538a8f4c491ce0e10a078f5427fdd314d454865..6360b73ee615e084b0a3c64ee46a70e0102c6789 100644 (file)
@@ -2,12 +2,22 @@
 use std::fs;
 
 #[derive(Debug, Default)]
-pub(crate) struct FilesEmitter;
+pub(crate) struct FilesEmitter {
+    print_misformatted_file_names: bool,
+}
+
+impl FilesEmitter {
+    pub(crate) fn new(print_misformatted_file_names: bool) -> Self {
+        Self {
+            print_misformatted_file_names,
+        }
+    }
+}
 
 impl Emitter for FilesEmitter {
     fn emit_formatted_file(
         &mut self,
-        _output: &mut dyn Write,
+        output: &mut dyn Write,
         FormattedFile {
             filename,
             original_text,
@@ -18,6 +28,9 @@ fn emit_formatted_file(
         let filename = ensure_real_path(filename);
         if original_text != formatted_text {
             fs::write(filename, formatted_text)?;
+            if self.print_misformatted_file_names {
+                writeln!(output, "{}", filename.display())?;
+            }
         }
         Ok(EmitterResult::default())
     }
index 2e43b9d8c7e97af5910b4b7a728bcb95bf5995ad..31df6b1c72cd433930f840ff883c3ea942f7046f 100644 (file)
@@ -480,7 +480,9 @@ pub(crate) fn create_emitter<'a>(config: &Config) -> Box<dyn Emitter + 'a> {
         EmitMode::Files if config.make_backup() => {
             Box::new(emitter::FilesWithBackupEmitter::default())
         }
-        EmitMode::Files => Box::new(emitter::FilesEmitter::default()),
+        EmitMode::Files => Box::new(emitter::FilesEmitter::new(
+            config.print_misformatted_file_names(),
+        )),
         EmitMode::Stdout | EmitMode::Coverage => {
             Box::new(emitter::StdoutEmitter::new(config.verbose()))
         }