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 {
skip_children: Option<bool>,
verbose: bool,
verbose_diff: bool,
write_mode: Option<WriteMode>,
color: Option<Color>,
file_lines: FileLines, // Default is all lines in all files.
@ -104,6 +105,8 @@ impl CliOptions {
fn from_matches(matches: &Matches) -> FmtResult<CliOptions> {
let mut options = CliOptions::default();
options.verbose = matches.opt_present("verbose");
options.verbose_diff = matches.opt_present("verbose-diff");
let unstable_features = matches.opt_present("unstable-features");
let rust_nightly = option_env!("CFG_RELEASE_CHANNEL")
.map(|c| c == "nightly")
@ -150,6 +153,7 @@ fn from_matches(matches: &Matches) -> FmtResult<CliOptions> {
fn apply_to(self, config: &mut Config) {
config.set().verbose(self.verbose);
config.set().verbose_diff(self.verbose_diff);
config.set().file_lines(self.file_lines);
config.set().unstable_features(self.unstable_features);
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.",
"JSON",
);
opts.optflag(
"",
"verbose-diff",
"Emit a more verbose diff, indicating the end of lines.",
);
opts.optflag("h", "help", "Show this message");
opts.optflag("", "skip-children", "Don't reformat child modules");
opts.optflag(

View File

@ -356,7 +356,8 @@ fn resolve_project_file(dir: &Path) -> Result<Option<PathBuf>, Error> {
}
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)
}

View File

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

View File

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

View File

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