Skip to content

Commit 62432fe

Browse files
calebcartwrighttopecongiro
authored andcommitted
add new flag to list names of misformatted files (#3747)
1 parent 73847d3 commit 62432fe

File tree

6 files changed

+121
-9
lines changed

6 files changed

+121
-9
lines changed

Configurations.md

+4
Original file line numberDiff line numberDiff line change
@@ -2445,3 +2445,7 @@ Internal option
24452445
## `make_backup`
24462446

24472447
Internal option, use `--backup`
2448+
2449+
## `print_misformatted_file_names`
2450+
2451+
Internal option, use `-l` or `--files-with-diff`

src/bin/main.rs

+14
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,12 @@ fn make_opts() -> Options {
126126
`current` writes to stdout current config as if formatting the file at PATH.",
127127
"[default|minimal|current] PATH",
128128
);
129+
opts.optflag(
130+
"l",
131+
"files-with-diff",
132+
"Prints the names of mismatched files that were formatted. Prints the names of \
133+
files that would be formated when used with `--check` mode. ",
134+
);
129135

130136
if is_nightly {
131137
opts.optflag(
@@ -480,6 +486,7 @@ struct GetOptsOptions {
480486
file_lines: FileLines, // Default is all lines in all files.
481487
unstable_features: bool,
482488
error_on_unformatted: Option<bool>,
489+
print_misformatted_file_names: bool,
483490
}
484491

485492
impl GetOptsOptions {
@@ -547,6 +554,10 @@ impl GetOptsOptions {
547554
options.backup = true;
548555
}
549556

557+
if matches.opt_present("files-with-diff") {
558+
options.print_misformatted_file_names = true;
559+
}
560+
550561
if !rust_nightly {
551562
if !STABLE_EMIT_MODES.contains(&options.emit_mode) {
552563
return Err(format_err!(
@@ -610,6 +621,9 @@ impl CliOptions for GetOptsOptions {
610621
if let Some(color) = self.color {
611622
config.set().color(color);
612623
}
624+
if self.print_misformatted_file_names {
625+
config.set().print_misformatted_file_names(true);
626+
}
613627
}
614628

615629
fn config_path(&self) -> Option<&Path> {

src/config/mod.rs

+4
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,9 @@ create_config! {
152152
emit_mode: EmitMode, EmitMode::Files, false,
153153
"What emit Mode to use when none is supplied";
154154
make_backup: bool, false, false, "Backup changed files";
155+
print_misformatted_file_names: bool, false, true,
156+
"Prints the names of mismatched files that were formatted. Prints the names of \
157+
files that would be formated when used with `--check` mode. ";
155158
}
156159

157160
impl PartialConfig {
@@ -161,6 +164,7 @@ impl PartialConfig {
161164
cloned.file_lines = None;
162165
cloned.verbose = None;
163166
cloned.width_heuristics = None;
167+
cloned.print_misformatted_file_names = None;
164168

165169
::toml::to_string(&cloned).map_err(|e| format!("Could not output config: {}", e))
166170
}

src/emitter/diff.rs

+81-6
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ impl DiffEmitter {
1515
impl Emitter for DiffEmitter {
1616
fn emit_formatted_file(
1717
&mut self,
18-
_output: &mut dyn Write,
18+
output: &mut dyn Write,
1919
FormattedFile {
2020
filename,
2121
original_text,
@@ -25,11 +25,86 @@ impl Emitter for DiffEmitter {
2525
const CONTEXT_SIZE: usize = 3;
2626
let mismatch = make_diff(&original_text, formatted_text, CONTEXT_SIZE);
2727
let has_diff = !mismatch.is_empty();
28-
print_diff(
29-
mismatch,
30-
|line_num| format!("Diff in {} at line {}:", filename, line_num),
31-
&self.config,
32-
);
28+
29+
if has_diff {
30+
if self.config.print_misformatted_file_names() {
31+
writeln!(output, "{}", ensure_real_path(filename).display())?;
32+
} else {
33+
print_diff(
34+
mismatch,
35+
|line_num| format!("Diff in {} at line {}:", filename, line_num),
36+
&self.config,
37+
);
38+
}
39+
}
40+
3341
return Ok(EmitterResult { has_diff });
3442
}
3543
}
44+
45+
#[cfg(test)]
46+
mod tests {
47+
use super::*;
48+
use crate::config::Config;
49+
use crate::FileName;
50+
use std::path::PathBuf;
51+
52+
#[test]
53+
fn does_not_print_when_no_files_reformatted() {
54+
let mut writer = Vec::new();
55+
let config = Config::default();
56+
let mut emitter = DiffEmitter::new(config);
57+
let result = emitter
58+
.emit_formatted_file(
59+
&mut writer,
60+
FormattedFile {
61+
filename: &FileName::Real(PathBuf::from("src/lib.rs")),
62+
original_text: "fn empty() {}\n",
63+
formatted_text: "fn empty() {}\n",
64+
},
65+
)
66+
.unwrap();
67+
assert_eq!(result.has_diff, false);
68+
assert_eq!(writer.len(), 0);
69+
}
70+
71+
#[test]
72+
fn prints_file_names_when_config_is_enabled() {
73+
let bin_file = "src/bin.rs";
74+
let bin_original = "fn main() {\nprintln!(\"Hello, world!\");\n}";
75+
let bin_formatted = "fn main() {\n println!(\"Hello, world!\");\n}";
76+
let lib_file = "src/lib.rs";
77+
let lib_original = "fn greet() {\nprintln!(\"Greetings!\");\n}";
78+
let lib_formatted = "fn greet() {\n println!(\"Greetings!\");\n}";
79+
80+
let mut writer = Vec::new();
81+
let mut config = Config::default();
82+
config.set().print_misformatted_file_names(true);
83+
let mut emitter = DiffEmitter::new(config);
84+
let _ = emitter
85+
.emit_formatted_file(
86+
&mut writer,
87+
FormattedFile {
88+
filename: &FileName::Real(PathBuf::from(bin_file)),
89+
original_text: bin_original,
90+
formatted_text: bin_formatted,
91+
},
92+
)
93+
.unwrap();
94+
let _ = emitter
95+
.emit_formatted_file(
96+
&mut writer,
97+
FormattedFile {
98+
filename: &FileName::Real(PathBuf::from(lib_file)),
99+
original_text: lib_original,
100+
formatted_text: lib_formatted,
101+
},
102+
)
103+
.unwrap();
104+
105+
assert_eq!(
106+
String::from_utf8(writer).unwrap(),
107+
format!("{}\n{}\n", bin_file, lib_file),
108+
)
109+
}
110+
}

src/emitter/files.rs

+15-2
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,22 @@ use super::*;
22
use std::fs;
33

44
#[derive(Debug, Default)]
5-
pub(crate) struct FilesEmitter;
5+
pub(crate) struct FilesEmitter {
6+
print_misformatted_file_names: bool,
7+
}
8+
9+
impl FilesEmitter {
10+
pub(crate) fn new(print_misformatted_file_names: bool) -> Self {
11+
Self {
12+
print_misformatted_file_names,
13+
}
14+
}
15+
}
616

717
impl Emitter for FilesEmitter {
818
fn emit_formatted_file(
919
&mut self,
10-
_output: &mut dyn Write,
20+
output: &mut dyn Write,
1121
FormattedFile {
1222
filename,
1323
original_text,
@@ -18,6 +28,9 @@ impl Emitter for FilesEmitter {
1828
let filename = ensure_real_path(filename);
1929
if original_text != formatted_text {
2030
fs::write(filename, formatted_text)?;
31+
if self.print_misformatted_file_names {
32+
writeln!(output, "{}", filename.display())?;
33+
}
2134
}
2235
Ok(EmitterResult::default())
2336
}

src/lib.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -480,7 +480,9 @@ pub(crate) fn create_emitter<'a>(config: &Config) -> Box<dyn Emitter + 'a> {
480480
EmitMode::Files if config.make_backup() => {
481481
Box::new(emitter::FilesWithBackupEmitter::default())
482482
}
483-
EmitMode::Files => Box::new(emitter::FilesEmitter::default()),
483+
EmitMode::Files => Box::new(emitter::FilesEmitter::new(
484+
config.print_misformatted_file_names(),
485+
)),
484486
EmitMode::Stdout | EmitMode::Coverage => {
485487
Box::new(emitter::StdoutEmitter::new(config.verbose()))
486488
}

0 commit comments

Comments
 (0)