Skip to content

Commit 5194984

Browse files
committed
Add a verbose-diff option
And don't print end of line characters by default in diffs cc #2536
1 parent 7a886e8 commit 5194984

File tree

6 files changed

+31
-13
lines changed

6 files changed

+31
-13
lines changed

src/bin/main.rs

+9
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ enum Operation {
9393
struct CliOptions {
9494
skip_children: Option<bool>,
9595
verbose: bool,
96+
verbose_diff: bool,
9697
write_mode: Option<WriteMode>,
9798
color: Option<Color>,
9899
file_lines: FileLines, // Default is all lines in all files.
@@ -104,6 +105,8 @@ impl CliOptions {
104105
fn from_matches(matches: &Matches) -> FmtResult<CliOptions> {
105106
let mut options = CliOptions::default();
106107
options.verbose = matches.opt_present("verbose");
108+
options.verbose_diff = matches.opt_present("verbose-diff");
109+
107110
let unstable_features = matches.opt_present("unstable-features");
108111
let rust_nightly = option_env!("CFG_RELEASE_CHANNEL")
109112
.map(|c| c == "nightly")
@@ -150,6 +153,7 @@ impl CliOptions {
150153

151154
fn apply_to(self, config: &mut Config) {
152155
config.set().verbose(self.verbose);
156+
config.set().verbose_diff(self.verbose_diff);
153157
config.set().file_lines(self.file_lines);
154158
config.set().unstable_features(self.unstable_features);
155159
if let Some(skip_children) = self.skip_children {
@@ -227,6 +231,11 @@ fn make_opts() -> Options {
227231
"Format specified line ranges. See README for more detail on the JSON format.",
228232
"JSON",
229233
);
234+
opts.optflag(
235+
"",
236+
"verbose-diff",
237+
"Emit a more verbose diff, indicating the end of lines.",
238+
);
230239
opts.optflag("h", "help", "Show this message");
231240
opts.optflag("", "skip-children", "Don't reformat child modules");
232241
opts.optflag(

src/config/config_type.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,8 @@ macro_rules! create_config {
356356
}
357357

358358
pub fn is_hidden_option(name: &str) -> bool {
359-
const HIDE_OPTIONS: [&str; 3] = ["verbose", "file_lines", "width_heuristics"];
359+
const HIDE_OPTIONS: [&str; 4] =
360+
["verbose", "verbose_diff", "file_lines", "width_heuristics"];
360361
HIDE_OPTIONS.contains(&name)
361362
}
362363

src/config/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@ create_config! {
143143

144144
// Not user-facing
145145
verbose: bool, false, false, "Use verbose output";
146+
verbose_diff: bool, false, false, "Emit verbose diffs";
146147
file_lines: FileLines, FileLines::all(), false,
147148
"Lines to format; this is not supported in rustfmt.toml, and can only be specified \
148149
via the --file-lines option";

src/filemap.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ where
159159
print_diff(
160160
mismatch,
161161
|line_num| format!("Diff in {} at line {}:", filename.display(), line_num),
162-
config.color(),
162+
config,
163163
);
164164
return Ok(has_diff);
165165
}
@@ -186,7 +186,7 @@ where
186186
print_diff(
187187
mismatch,
188188
|line_num| format!("Diff in {} at line {}:", filename.display(), line_num),
189-
config.color(),
189+
config,
190190
);
191191
return Ok(has_diff);
192192
}

src/rustfmt_diff.rs

+15-8
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
use config::Color;
11+
use config::{Color, Config};
1212
use diff;
1313
use std::collections::VecDeque;
1414
use std::io;
@@ -149,10 +149,13 @@ pub fn make_diff(expected: &str, actual: &str, context_size: usize) -> Vec<Misma
149149
results
150150
}
151151

152-
pub fn print_diff<F>(diff: Vec<Mismatch>, get_section_title: F, color: Color)
152+
pub fn print_diff<F>(diff: Vec<Mismatch>, get_section_title: F, config: &Config)
153153
where
154154
F: Fn(u32) -> String,
155155
{
156+
let color = config.color();
157+
let line_terminator = if config.verbose_diff() { "⏎" } else { "" };
158+
156159
let mut writer = OutputWriter::new(color);
157160

158161
for mismatch in diff {
@@ -161,13 +164,17 @@ where
161164

162165
for line in mismatch.lines {
163166
match line {
164-
DiffLine::Context(ref str) => writer.writeln(&format!(" {}⏎", str), None),
165-
DiffLine::Expected(ref str) => {
166-
writer.writeln(&format!("+{}⏎", str), Some(term::color::GREEN))
167-
}
168-
DiffLine::Resulting(ref str) => {
169-
writer.writeln(&format!("-{}⏎", str), Some(term::color::RED))
167+
DiffLine::Context(ref str) => {
168+
writer.writeln(&format!(" {}{}", str, line_terminator), None)
170169
}
170+
DiffLine::Expected(ref str) => writer.writeln(
171+
&format!("+{}{}", str, line_terminator),
172+
Some(term::color::GREEN),
173+
),
174+
DiffLine::Resulting(ref str) => writer.writeln(
175+
&format!("-{}{}", str, line_terminator),
176+
Some(term::color::RED),
177+
),
171178
}
172179
}
173180
}

tests/lib.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,7 @@ fn print_mismatches_default_message(result: HashMap<PathBuf, Vec<Mismatch>>) {
337337
for (file_name, diff) in result {
338338
let mismatch_msg_formatter =
339339
|line_num| format!("\nMismatch at {}:{}:", file_name.display(), line_num);
340-
print_diff(diff, &mismatch_msg_formatter, Color::Auto);
340+
print_diff(diff, &mismatch_msg_formatter, &Default::default());
341341
}
342342

343343
if let Some(mut t) = term::stdout() {
@@ -350,7 +350,7 @@ fn print_mismatches<T: Fn(u32) -> String>(
350350
mismatch_msg_formatter: T,
351351
) {
352352
for (_file_name, diff) in result {
353-
print_diff(diff, &mismatch_msg_formatter, Color::Auto);
353+
print_diff(diff, &mismatch_msg_formatter, &Default::default());
354354
}
355355

356356
if let Some(mut t) = term::stdout() {

0 commit comments

Comments
 (0)