Add a verbose-diff option

And don't print end of line characters by default in diffs

cc #2536
This commit is contained in:
Nick Cameron 2018-04-20 14:02:44 +12:00
parent 7a886e8fe5
commit 5194984812
6 changed files with 31 additions and 13 deletions

View file

@ -93,6 +93,7 @@ enum Operation {
struct CliOptions { struct CliOptions {
skip_children: Option<bool>, skip_children: Option<bool>,
verbose: bool, verbose: bool,
verbose_diff: bool,
write_mode: Option<WriteMode>, write_mode: Option<WriteMode>,
color: Option<Color>, color: Option<Color>,
file_lines: FileLines, // Default is all lines in all files. file_lines: FileLines, // Default is all lines in all files.
@ -104,6 +105,8 @@ impl CliOptions {
fn from_matches(matches: &Matches) -> FmtResult<CliOptions> { fn from_matches(matches: &Matches) -> FmtResult<CliOptions> {
let mut options = CliOptions::default(); let mut options = CliOptions::default();
options.verbose = matches.opt_present("verbose"); options.verbose = matches.opt_present("verbose");
options.verbose_diff = matches.opt_present("verbose-diff");
let unstable_features = matches.opt_present("unstable-features"); let unstable_features = matches.opt_present("unstable-features");
let rust_nightly = option_env!("CFG_RELEASE_CHANNEL") let rust_nightly = option_env!("CFG_RELEASE_CHANNEL")
.map(|c| c == "nightly") .map(|c| c == "nightly")
@ -150,6 +153,7 @@ impl CliOptions {
fn apply_to(self, config: &mut Config) { fn apply_to(self, config: &mut Config) {
config.set().verbose(self.verbose); config.set().verbose(self.verbose);
config.set().verbose_diff(self.verbose_diff);
config.set().file_lines(self.file_lines); config.set().file_lines(self.file_lines);
config.set().unstable_features(self.unstable_features); config.set().unstable_features(self.unstable_features);
if let Some(skip_children) = self.skip_children { if let Some(skip_children) = self.skip_children {
@ -227,6 +231,11 @@ fn make_opts() -> Options {
"Format specified line ranges. See README for more detail on the JSON format.", "Format specified line ranges. See README for more detail on the JSON format.",
"JSON", "JSON",
); );
opts.optflag(
"",
"verbose-diff",
"Emit a more verbose diff, indicating the end of lines.",
);
opts.optflag("h", "help", "Show this message"); opts.optflag("h", "help", "Show this message");
opts.optflag("", "skip-children", "Don't reformat child modules"); opts.optflag("", "skip-children", "Don't reformat child modules");
opts.optflag( opts.optflag(

View file

@ -356,7 +356,8 @@ macro_rules! create_config {
} }
pub fn is_hidden_option(name: &str) -> bool { pub fn is_hidden_option(name: &str) -> bool {
const HIDE_OPTIONS: [&str; 3] = ["verbose", "file_lines", "width_heuristics"]; const HIDE_OPTIONS: [&str; 4] =
["verbose", "verbose_diff", "file_lines", "width_heuristics"];
HIDE_OPTIONS.contains(&name) HIDE_OPTIONS.contains(&name)
} }

View file

@ -143,6 +143,7 @@ create_config! {
// Not user-facing // Not user-facing
verbose: bool, false, false, "Use verbose output"; verbose: bool, false, false, "Use verbose output";
verbose_diff: bool, false, false, "Emit verbose diffs";
file_lines: FileLines, FileLines::all(), false, file_lines: FileLines, FileLines::all(), false,
"Lines to format; this is not supported in rustfmt.toml, and can only be specified \ "Lines to format; this is not supported in rustfmt.toml, and can only be specified \
via the --file-lines option"; via the --file-lines option";

View file

@ -159,7 +159,7 @@ where
print_diff( print_diff(
mismatch, mismatch,
|line_num| format!("Diff in {} at line {}:", filename.display(), line_num), |line_num| format!("Diff in {} at line {}:", filename.display(), line_num),
config.color(), config,
); );
return Ok(has_diff); return Ok(has_diff);
} }
@ -186,7 +186,7 @@ where
print_diff( print_diff(
mismatch, mismatch,
|line_num| format!("Diff in {} at line {}:", filename.display(), line_num), |line_num| format!("Diff in {} at line {}:", filename.display(), line_num),
config.color(), config,
); );
return Ok(has_diff); return Ok(has_diff);
} }

View file

@ -8,7 +8,7 @@
// option. This file may not be copied, modified, or distributed // option. This file may not be copied, modified, or distributed
// except according to those terms. // except according to those terms.
use config::Color; use config::{Color, Config};
use diff; use diff;
use std::collections::VecDeque; use std::collections::VecDeque;
use std::io; use std::io;
@ -149,10 +149,13 @@ pub fn make_diff(expected: &str, actual: &str, context_size: usize) -> Vec<Misma
results results
} }
pub fn print_diff<F>(diff: Vec<Mismatch>, get_section_title: F, color: Color) pub fn print_diff<F>(diff: Vec<Mismatch>, get_section_title: F, config: &Config)
where where
F: Fn(u32) -> String, F: Fn(u32) -> String,
{ {
let color = config.color();
let line_terminator = if config.verbose_diff() { "" } else { "" };
let mut writer = OutputWriter::new(color); let mut writer = OutputWriter::new(color);
for mismatch in diff { for mismatch in diff {
@ -161,13 +164,17 @@ where
for line in mismatch.lines { for line in mismatch.lines {
match line { match line {
DiffLine::Context(ref str) => writer.writeln(&format!(" {}", str), None), DiffLine::Context(ref str) => {
DiffLine::Expected(ref str) => { writer.writeln(&format!(" {}{}", str, line_terminator), None)
writer.writeln(&format!("+{}", str), Some(term::color::GREEN))
}
DiffLine::Resulting(ref str) => {
writer.writeln(&format!("-{}", str), Some(term::color::RED))
} }
DiffLine::Expected(ref str) => writer.writeln(
&format!("+{}{}", str, line_terminator),
Some(term::color::GREEN),
),
DiffLine::Resulting(ref str) => writer.writeln(
&format!("-{}{}", str, line_terminator),
Some(term::color::RED),
),
} }
} }
} }

View file

@ -337,7 +337,7 @@ fn print_mismatches_default_message(result: HashMap<PathBuf, Vec<Mismatch>>) {
for (file_name, diff) in result { for (file_name, diff) in result {
let mismatch_msg_formatter = let mismatch_msg_formatter =
|line_num| format!("\nMismatch at {}:{}:", file_name.display(), line_num); |line_num| format!("\nMismatch at {}:{}:", file_name.display(), line_num);
print_diff(diff, &mismatch_msg_formatter, Color::Auto); print_diff(diff, &mismatch_msg_formatter, &Default::default());
} }
if let Some(mut t) = term::stdout() { if let Some(mut t) = term::stdout() {
@ -350,7 +350,7 @@ fn print_mismatches<T: Fn(u32) -> String>(
mismatch_msg_formatter: T, mismatch_msg_formatter: T,
) { ) {
for (_file_name, diff) in result { for (_file_name, diff) in result {
print_diff(diff, &mismatch_msg_formatter, Color::Auto); print_diff(diff, &mismatch_msg_formatter, &Default::default());
} }
if let Some(mut t) = term::stdout() { if let Some(mut t) = term::stdout() {