]> git.lizzy.rs Git - rust.git/blob - src/source_file.rs
Fixed comment dropped between & and type issue (#4482)
[rust.git] / src / source_file.rs
1 use std::fs;
2 use std::io::{self, Write};
3 use std::path::Path;
4
5 use crate::config::FileName;
6 use crate::emitter::{self, Emitter};
7 use crate::syntux::session::ParseSess;
8 use crate::NewlineStyle;
9
10 #[cfg(test)]
11 use crate::config::Config;
12 #[cfg(test)]
13 use crate::create_emitter;
14 #[cfg(test)]
15 use crate::formatting::FileRecord;
16 use std::rc::Rc;
17
18 // Append a newline to the end of each file.
19 pub(crate) fn append_newline(s: &mut String) {
20     s.push_str("\n");
21 }
22
23 #[cfg(test)]
24 pub(crate) fn write_all_files<T>(
25     source_file: &[FileRecord],
26     out: &mut T,
27     config: &Config,
28 ) -> Result<(), io::Error>
29 where
30     T: Write,
31 {
32     let mut emitter = create_emitter(config);
33
34     emitter.emit_header(out)?;
35     for &(ref filename, ref text) in source_file {
36         write_file(
37             None,
38             filename,
39             text,
40             out,
41             &mut *emitter,
42             config.newline_style(),
43         )?;
44     }
45     emitter.emit_footer(out)?;
46
47     Ok(())
48 }
49
50 pub(crate) fn write_file<T>(
51     parse_sess: Option<&ParseSess>,
52     filename: &FileName,
53     formatted_text: &str,
54     out: &mut T,
55     emitter: &mut dyn Emitter,
56     newline_style: NewlineStyle,
57 ) -> Result<emitter::EmitterResult, io::Error>
58 where
59     T: Write,
60 {
61     fn ensure_real_path(filename: &FileName) -> &Path {
62         match *filename {
63             FileName::Real(ref path) => path,
64             _ => panic!("cannot format `{}` and emit to files", filename),
65         }
66     }
67
68     impl From<&FileName> for rustc_span::FileName {
69         fn from(filename: &FileName) -> rustc_span::FileName {
70             match filename {
71                 FileName::Real(path) => {
72                     rustc_span::FileName::Real(rustc_span::RealFileName::Named(path.to_owned()))
73                 }
74                 FileName::Stdin => rustc_span::FileName::Custom("stdin".to_owned()),
75             }
76         }
77     }
78
79     // SourceFile's in the SourceMap will always have Unix-style line endings
80     // See: https://github.com/rust-lang/rustfmt/issues/3850
81     // So if the user has explicitly overridden the rustfmt `newline_style`
82     // config and `filename` is FileName::Real, then we must check the file system
83     // to get the original file value in order to detect newline_style conflicts.
84     // Otherwise, parse session is around (cfg(not(test))) and newline_style has been
85     // left as the default value, then try getting source from the parse session
86     // source map instead of hitting the file system. This also supports getting
87     // original text for `FileName::Stdin`.
88     let original_text = if newline_style != NewlineStyle::Auto && *filename != FileName::Stdin {
89         Rc::new(fs::read_to_string(ensure_real_path(filename))?)
90     } else {
91         match parse_sess.and_then(|sess| sess.get_original_snippet(filename)) {
92             Some(ori) => ori,
93             None => Rc::new(fs::read_to_string(ensure_real_path(filename))?),
94         }
95     };
96
97     let formatted_file = emitter::FormattedFile {
98         filename,
99         original_text: original_text.as_str(),
100         formatted_text,
101     };
102
103     emitter.emit_formatted_file(out, formatted_file)
104 }