]> git.lizzy.rs Git - rust.git/blobdiff - src/changes.rs
Merge pull request #128 from marcusklaas/subexpr
[rust.git] / src / changes.rs
index 97133ab67fbdd688fb6d2e22d1e2b4a651bf2800..4cce45e5eb3af31a4a0f4a2198f108244fc200da 100644 (file)
@@ -21,6 +21,7 @@
 use std::io::{Write, stdout};
 use WriteMode;
 use NewlineStyle;
+use config::Config;
 use utils::round_up_to_power_of_two;
 
 // This is basically a wrapper around a bunch of Ropes which makes it convenient
@@ -34,11 +35,9 @@ pub struct ChangeSet<'a> {
 impl<'a> ChangeSet<'a> {
     // Create a new ChangeSet for a given libsyntax CodeMap.
     pub fn from_codemap(codemap: &'a CodeMap) -> ChangeSet<'a> {
-        let mut result = ChangeSet {
-            file_map: HashMap::new(),
-            codemap: codemap,
-            file_spans: Vec::with_capacity(codemap.files.borrow().len()),
-        };
+        let mut result = ChangeSet { file_map: HashMap::new(),
+                                     codemap: codemap,
+                                     file_spans: Vec::with_capacity(codemap.files.borrow().len()), };
 
         for f in codemap.files.borrow().iter() {
             // Use the length of the file as a heuristic for how much space we
@@ -100,6 +99,14 @@ pub fn push_str_span(&mut self, span: Span, text: &str) {
         self.push_str(&file_name, text)
     }
 
+    // Fetch the output buffer for the given file name.
+    // Panics on unknown files.
+    pub fn get(&mut self, file_name: &str) -> &StringBuffer {
+        self.file_map.get(file_name).unwrap()
+    }
+
+    // Fetch a mutable reference to the output buffer for the given file name.
+    // Panics on unknown files.
     pub fn get_mut(&mut self, file_name: &str) -> &mut StringBuffer {
         self.file_map.get_mut(file_name).unwrap()
     }
@@ -115,11 +122,7 @@ pub fn cur_offset_span(&mut self, span: Span) -> usize {
 
     // Return an iterator over the entire changed text.
     pub fn text<'c>(&'c self) -> FileIterator<'c, 'a> {
-        FileIterator {
-            change_set: self,
-            keys: self.file_map.keys().collect(),
-            cur_key: 0,
-        }
+        FileIterator { change_set: self, keys: self.file_map.keys().collect(), cur_key: 0 }
     }
 
     // Append a newline to the end of each file.
@@ -130,11 +133,12 @@ pub fn append_newlines(&mut self) {
     }
 
     pub fn write_all_files(&self,
-                           mode: WriteMode)
+                           mode: WriteMode,
+                           config: &Config)
                            -> Result<(HashMap<String, String>), ::std::io::Error> {
         let mut result = HashMap::new();
         for filename in self.file_map.keys() {
-            let one_result = try!(self.write_file(filename, mode));
+            let one_result = try!(self.write_file(filename, mode, config));
             if let Some(r) = one_result {
                 result.insert(filename.clone(), r);
             }
@@ -145,18 +149,19 @@ pub fn write_all_files(&self,
 
     pub fn write_file(&self,
                       filename: &str,
-                      mode: WriteMode)
+                      mode: WriteMode,
+                      config: &Config)
                       -> 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,
+        fn write_system_newlines<T>(mut writer: T,
+                                    text: &StringBuffer,
+                                    config: &Config)
+                                    -> Result<(), ::std::io::Error>
+            where T: Write
         {
-            match config!(newline_style) {
+            match config.newline_style {
                 NewlineStyle::Unix => write!(writer, "{}", text),
                 NewlineStyle::Windows => {
                     for (c, _) in text.chars() {
@@ -181,7 +186,7 @@ fn write_system_newlines<T>(
                 {
                     // Write text to temp file
                     let tmp_file = try!(File::create(&tmp_name));
-                    try!(write_system_newlines(tmp_file, text));
+                    try!(write_system_newlines(tmp_file, text, config));
                 }
 
                 try!(::std::fs::rename(filename, bk_name));
@@ -190,18 +195,18 @@ fn write_system_newlines<T>(
             WriteMode::NewFile(extn) => {
                 let filename = filename.to_owned() + "." + extn;
                 let file = try!(File::create(&filename));
-                try!(write_system_newlines(file, text));
+                try!(write_system_newlines(file, text, config));
             }
             WriteMode::Display => {
                 println!("{}:\n", filename);
                 let stdout = stdout();
                 let stdout_lock = stdout.lock();
-                try!(write_system_newlines(stdout_lock, text));
+                try!(write_system_newlines(stdout_lock, text, config));
             }
             WriteMode::Return(_) => {
                 // io::Write is not implemented for String, working around with Vec<u8>
                 let mut v = Vec::new();
-                try!(write_system_newlines(&mut v, text));
+                try!(write_system_newlines(&mut v, text, config));
                 // won't panic, we are writing correct utf8
                 return Ok(Some(String::from_utf8(v).unwrap()));
             }
@@ -209,6 +214,10 @@ fn write_system_newlines<T>(
 
         Ok(None)
     }
+
+    pub fn is_changed(&self, filename: &str) -> bool {
+        self.file_map.get(filename).expect("Unknown filename").len != 0
+    }
 }
 
 // Iterates over each file in the ChangSet. Yields the filename and the changed