Skip to content

Improve end lines handling #2779

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 11, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
137 changes: 36 additions & 101 deletions src/filemap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,12 @@

// TODO: add tests

use std::fs::{self, File};
use std::io::{self, BufWriter, Read, Write};
use std::path::Path;
use std::fs;
use std::io::{self, Write};

use checkstyle::output_checkstyle_file;
use config::{Config, EmitMode, FileName, NewlineStyle, Verbosity};
use rustfmt_diff::{make_diff, output_modified, print_diff, Mismatch};
use config::{Config, EmitMode, FileName, Verbosity};
use rustfmt_diff::{make_diff, output_modified, print_diff};

#[cfg(test)]
use FileRecord;
Expand Down Expand Up @@ -48,72 +47,15 @@ where
Ok(())
}

// Prints all newlines either as `\n` or as `\r\n`.
pub fn write_system_newlines<T>(writer: T, text: &str, config: &Config) -> Result<(), io::Error>
where
T: Write,
{
// Buffer output, since we're writing a since char at a time.
let mut writer = BufWriter::new(writer);

let style = if config.newline_style() == NewlineStyle::Native {
if cfg!(windows) {
NewlineStyle::Windows
} else {
NewlineStyle::Unix
}
} else {
config.newline_style()
};

match style {
NewlineStyle::Unix => write!(writer, "{}", text),
NewlineStyle::Windows => {
for c in text.chars() {
match c {
'\n' => write!(writer, "\r\n")?,
'\r' => continue,
c => write!(writer, "{}", c)?,
}
}
Ok(())
}
NewlineStyle::Native => unreachable!(),
}
}

pub fn write_file<T>(
text: &str,
formatted_text: &str,
filename: &FileName,
out: &mut T,
config: &Config,
) -> Result<bool, io::Error>
where
T: Write,
{
fn source_and_formatted_text(
text: &str,
filename: &Path,
config: &Config,
) -> Result<(String, String), io::Error> {
let mut f = File::open(filename)?;
let mut ori_text = String::new();
f.read_to_string(&mut ori_text)?;
let mut v = Vec::new();
write_system_newlines(&mut v, text, config)?;
let fmt_text = String::from_utf8(v).unwrap();
Ok((ori_text, fmt_text))
}

fn create_diff(
filename: &Path,
text: &str,
config: &Config,
) -> Result<Vec<Mismatch>, io::Error> {
let (ori, fmt) = source_and_formatted_text(text, filename, config)?;
Ok(make_diff(&ori, &fmt, 3))
}

let filename_to_path = || match *filename {
FileName::Real(ref path) => path,
_ => panic!("cannot format `{}` and emit to files", filename),
Expand All @@ -122,65 +64,58 @@ where
match config.emit_mode() {
EmitMode::Files if config.make_backup() => {
let filename = filename_to_path();
if let Ok((ori, fmt)) = source_and_formatted_text(text, filename, config) {
if fmt != ori {
// Do a little dance to make writing safer - write to a temp file
// rename the original to a .bk, then rename the temp file to the
// original.
let tmp_name = filename.with_extension("tmp");
let bk_name = filename.with_extension("bk");
{
// Write text to temp file
let tmp_file = File::create(&tmp_name)?;
write_system_newlines(tmp_file, text, config)?;
}

fs::rename(filename, bk_name)?;
fs::rename(tmp_name, filename)?;
}
let ori = fs::read_to_string(filename)?;
if ori != formatted_text {
// Do a little dance to make writing safer - write to a temp file
// rename the original to a .bk, then rename the temp file to the
// original.
let tmp_name = filename.with_extension("tmp");
let bk_name = filename.with_extension("bk");

fs::write(&tmp_name, formatted_text)?;
fs::rename(filename, bk_name)?;
fs::rename(tmp_name, filename)?;
}
}
EmitMode::Files => {
// Write text directly over original file if there is a diff.
let filename = filename_to_path();
let (source, formatted) = source_and_formatted_text(text, filename, config)?;
if source != formatted {
let file = File::create(filename)?;
write_system_newlines(file, text, config)?;
let ori = fs::read_to_string(filename)?;
if ori != formatted_text {
fs::write(filename, formatted_text)?;
}
}
EmitMode::Stdout | EmitMode::Coverage => {
if config.verbose() != Verbosity::Quiet {
println!("{}:\n", filename);
}
write_system_newlines(out, text, config)?;
write!(out, "{}", formatted_text)?;
}
EmitMode::ModifiedLines => {
let filename = filename_to_path();
if let Ok((ori, fmt)) = source_and_formatted_text(text, filename, config) {
let mismatch = make_diff(&ori, &fmt, 0);
let has_diff = !mismatch.is_empty();
output_modified(out, mismatch);
return Ok(has_diff);
}
let ori = fs::read_to_string(filename)?;
let mismatch = make_diff(&ori, formatted_text, 0);
let has_diff = !mismatch.is_empty();
output_modified(out, mismatch);
return Ok(has_diff);
}
EmitMode::Checkstyle => {
let filename = filename_to_path();
let diff = create_diff(filename, text, config)?;
let ori = fs::read_to_string(filename)?;
let diff = make_diff(&ori, formatted_text, 3);
output_checkstyle_file(out, filename, diff)?;
}
EmitMode::Diff => {
let filename = filename_to_path();
if let Ok((ori, fmt)) = source_and_formatted_text(text, filename, config) {
let mismatch = make_diff(&ori, &fmt, 3);
let has_diff = !mismatch.is_empty();
print_diff(
mismatch,
|line_num| format!("Diff in {} at line {}:", filename.display(), line_num),
config,
);
return Ok(has_diff);
}
let ori = fs::read_to_string(filename)?;
let mismatch = make_diff(&ori, formatted_text, 3);
let has_diff = !mismatch.is_empty();
print_diff(
mismatch,
|line_num| format!("Diff in {} at line {}:", filename.display(), line_num),
config,
);
return Ok(has_diff);
}
}

Expand Down
44 changes: 39 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ use visitor::{FmtVisitor, SnippetProvider};
pub use checkstyle::{footer as checkstyle_footer, header as checkstyle_header};
pub use config::summary::Summary;
pub use config::{
load_config, CliOptions, Color, Config, EmitMode, FileLines, FileName, Range, Verbosity,
load_config, CliOptions, Color, Config, EmitMode, FileLines, FileName, NewlineStyle, Range,
Verbosity,
};

#[macro_use]
Expand Down Expand Up @@ -132,6 +133,9 @@ pub enum ErrorKind {
/// An io error during reading or writing.
#[fail(display = "io error: {}", _0)]
IoError(io::Error),
/// Parse error occured when parsing the Input.
#[fail(display = "parse error")]
ParseError,
/// The user mandated a version and the current version of Rustfmt does not
/// satisfy that requirement.
#[fail(display = "Version mismatch")]
Expand Down Expand Up @@ -172,9 +176,10 @@ impl FormattingError {
}
fn msg_prefix(&self) -> &str {
match self.kind {
ErrorKind::LineOverflow(..) | ErrorKind::TrailingWhitespace | ErrorKind::IoError(_) => {
"internal error:"
}
ErrorKind::LineOverflow(..)
| ErrorKind::TrailingWhitespace
| ErrorKind::IoError(_)
| ErrorKind::ParseError => "internal error:",
ErrorKind::LicenseCheck | ErrorKind::BadAttr | ErrorKind::VersionMismatch => "error:",
ErrorKind::BadIssue(_) | ErrorKind::DeprecatedAttr => "warning:",
}
Expand Down Expand Up @@ -844,7 +849,7 @@ fn format_input_inner<T: Write>(
ParseError::Recovered => {}
}
summary.add_parsing_error();
return Ok((summary, FileMap::new(), FormatReport::new()));
return Err((ErrorKind::ParseError, summary));
}
};

Expand Down Expand Up @@ -873,6 +878,7 @@ fn format_input_inner<T: Write>(
filemap::append_newline(file);

format_lines(file, file_name, skipped_range, config, report);
replace_with_system_newlines(file, config);

if let Some(ref mut out) = out {
return filemap::write_file(file, file_name, out, config);
Expand Down Expand Up @@ -925,6 +931,34 @@ fn format_input_inner<T: Write>(
}
}

pub fn replace_with_system_newlines(text: &mut String, config: &Config) -> () {
let style = if config.newline_style() == NewlineStyle::Native {
if cfg!(windows) {
NewlineStyle::Windows
} else {
NewlineStyle::Unix
}
} else {
config.newline_style()
};

match style {
NewlineStyle::Unix => return,
NewlineStyle::Windows => {
let mut transformed = String::with_capacity(text.capacity());
for c in text.chars() {
match c {
'\n' => transformed.push_str("\r\n"),
'\r' => continue,
c => transformed.push(c),
}
}
*text = transformed;
}
NewlineStyle::Native => unreachable!(),
}
}

/// A single span of changed lines, with 0 or more removed lines
/// and a vector of 0 or more inserted lines.
#[derive(Debug, PartialEq, Eq)]
Expand Down
12 changes: 3 additions & 9 deletions src/test/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ use std::str::Chars;

use config::summary::Summary;
use config::{Color, Config, ReportTactic};
use filemap::write_system_newlines;
use rustfmt_diff::*;
use *;

Expand Down Expand Up @@ -401,14 +400,9 @@ fn idempotent_check(
}

let mut write_result = HashMap::new();
for &(ref filename, ref text) in &file_map {
let mut v = Vec::new();
// Won't panic, as we're not doing any IO.
write_system_newlines(&mut v, text, &config).unwrap();
// Won't panic, we are writing correct utf8.
let one_result = String::from_utf8(v).unwrap();
if let FileName::Real(ref filename) = *filename {
write_result.insert(filename.to_owned(), one_result);
for (filename, text) in file_map {
if let FileName::Real(ref filename) = filename {
write_result.insert(filename.to_owned(), text);
}
}

Expand Down