]> git.lizzy.rs Git - rust.git/blobdiff - src/source_file.rs
Merge commit 'c4416f20dcaec5d93077f72470e83e150fb923b1' into sync-rustfmt
[rust.git] / src / source_file.rs
index b6764ee7464337d5c99802c884b25d71f76b8929..56d4ab4003832f57cda0b4774731766d22fbdf6f 100644 (file)
@@ -2,10 +2,9 @@
 use std::io::{self, Write};
 use std::path::Path;
 
-use syntax::source_map::SourceMap;
-
 use crate::config::FileName;
 use crate::emitter::{self, Emitter};
+use crate::parse::session::ParseSess;
 use crate::NewlineStyle;
 
 #[cfg(test)]
 #[cfg(test)]
 use crate::formatting::FileRecord;
 
+use rustc_data_structures::sync::Lrc;
+
 // Append a newline to the end of each file.
 pub(crate) fn append_newline(s: &mut String) {
-    s.push_str("\n");
+    s.push('\n');
 }
 
 #[cfg(test)]
@@ -48,7 +49,7 @@ pub(crate) fn write_all_files<T>(
 }
 
 pub(crate) fn write_file<T>(
-    source_map: Option<&SourceMap>,
+    parse_sess: Option<&ParseSess>,
     filename: &FileName,
     formatted_text: &str,
     out: &mut T,
@@ -65,11 +66,13 @@ fn ensure_real_path(filename: &FileName) -> &Path {
         }
     }
 
-    impl From<&FileName> for syntax_pos::FileName {
-        fn from(filename: &FileName) -> syntax_pos::FileName {
+    impl From<&FileName> for rustc_span::FileName {
+        fn from(filename: &FileName) -> rustc_span::FileName {
             match filename {
-                FileName::Real(path) => syntax_pos::FileName::Real(path.to_owned()),
-                FileName::Stdin => syntax_pos::FileName::Custom("stdin".to_owned()),
+                FileName::Real(path) => {
+                    rustc_span::FileName::Real(rustc_span::RealFileName::LocalPath(path.to_owned()))
+                }
+                FileName::Stdin => rustc_span::FileName::Custom("stdin".to_owned()),
             }
         }
     }
@@ -84,20 +87,17 @@ fn from(filename: &FileName) -> syntax_pos::FileName {
     // source map instead of hitting the file system. This also supports getting
     // original text for `FileName::Stdin`.
     let original_text = if newline_style != NewlineStyle::Auto && *filename != FileName::Stdin {
-        fs::read_to_string(ensure_real_path(filename))?
+        Lrc::new(fs::read_to_string(ensure_real_path(filename))?)
     } else {
-        match source_map
-            .and_then(|x| x.get_source_file(&filename.into()))
-            .and_then(|x| x.src.as_ref().map(ToString::to_string))
-        {
+        match parse_sess.and_then(|sess| sess.get_original_snippet(filename)) {
             Some(ori) => ori,
-            None => fs::read_to_string(ensure_real_path(filename))?,
+            None => Lrc::new(fs::read_to_string(ensure_real_path(filename))?),
         }
     };
 
     let formatted_file = emitter::FormattedFile {
         filename,
-        original_text: &original_text,
+        original_text: original_text.as_str(),
         formatted_text,
     };