Skip to content

Commit

Permalink
handle windows newlines
Browse files Browse the repository at this point in the history
  • Loading branch information
Oliver Schneider committed May 4, 2015
1 parent 0ede9ae commit 1655583
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 7 deletions.
44 changes: 37 additions & 7 deletions src/changes.rs
Expand Up @@ -18,8 +18,10 @@ use std::collections::HashMap;
use syntax::codemap::{CodeMap, Span, BytePos};
use std::fmt;
use std::fs::File;
use std::io::Write;
use std::io::{Write, stdout};
use WriteMode;
use NEWLINE_STYLE;
use NewlineStyle;

// This is basically a wrapper around a bunch of Ropes which makes it convenient
// to work with libsyntax. It is badly named.
Expand Down Expand Up @@ -148,6 +150,28 @@ impl<'a> ChangeSet<'a> {
-> Result<Option<String>, ::std::io::Error> {
let text = &self.file_map[filename];

// prints all newlines either as `\n` or as `\r\n`
fn write_system_newlines<T>(
mut writer: T,
text: &StringBuffer)
-> Result<(), ::std::io::Error>
where T: Write,
{
match NEWLINE_STYLE {
NewlineStyle::Unix => write!(writer, "{}", text),
NewlineStyle::Windows => {
for (c, _) in text.chars() {
match c {
'\n' => try!(write!(writer, "\r\n")),
'\r' => continue,
c => try!(write!(writer, "{}", c)),
}
}
Ok(())
},
}
}

match mode {
WriteMode::Overwrite => {
// Do a little dance to make writing safer - write to a temp file
Expand All @@ -157,24 +181,30 @@ impl<'a> ChangeSet<'a> {
let bk_name = filename.to_owned() + ".bk";
{
// Write text to temp file
let mut tmp_file = try!(File::create(&tmp_name));
try!(write!(tmp_file, "{}", text));
let tmp_file = try!(File::create(&tmp_name));
try!(write_system_newlines(tmp_file, text));
}

try!(::std::fs::rename(filename, bk_name));
try!(::std::fs::rename(tmp_name, filename));
}
WriteMode::NewFile(extn) => {
let filename = filename.to_owned() + "." + extn;
let mut file = try!(File::create(&filename));
try!(write!(file, "{}", text));
let file = try!(File::create(&filename));
try!(write_system_newlines(file, text));
}
WriteMode::Display => {
println!("{}:\n", filename);
println!("{}", text);
let stdout = stdout();
let stdout_lock = stdout.lock();
try!(write_system_newlines(stdout_lock, text));
}
WriteMode::Return(_) => {
return Ok(Some(text.to_string()));
// io::Write is not implemented for String, working around with Vec<u8>
let mut v = Vec::new();
try!(write_system_newlines(&mut v, text));
// won't panic, we are writing correct utf8
return Ok(Some(String::from_utf8(v).unwrap()));
}
}

Expand Down
7 changes: 7 additions & 0 deletions src/lib.rs
Expand Up @@ -59,6 +59,7 @@ const LEEWAY: usize = 5;
const MAX_WIDTH: usize = 100;
const MIN_STRING: usize = 10;
const TAB_SPACES: usize = 4;
const NEWLINE_STYLE: NewlineStyle = NewlineStyle::Unix;
const FN_BRACE_STYLE: BraceStyle = BraceStyle::SameLineWhere;
const FN_RETURN_INDENT: ReturnIndent = ReturnIndent::WithArgs;
// When we get scoped annotations, we should have rustfmt::skip.
Expand All @@ -75,6 +76,12 @@ pub enum WriteMode {
Return(&'static Fn(HashMap<String, String>)),
}

#[derive(Copy, Clone, Eq, PartialEq, Debug)]
enum NewlineStyle {
Windows, // \r\n
Unix, // \n
}

#[derive(Copy, Clone, Eq, PartialEq, Debug)]
enum BraceStyle {
AlwaysNextLine,
Expand Down

0 comments on commit 1655583

Please sign in to comment.