2 use std::io::{self, Write};
5 use crate::config::FileName;
6 use crate::emitter::{self, Emitter};
7 use crate::syntux::session::ParseSess;
8 use crate::NewlineStyle;
11 use crate::config::Config;
13 use crate::create_emitter;
15 use crate::formatting::FileRecord;
17 use rustc_data_structures::sync::Lrc;
19 // Append a newline to the end of each file.
20 pub(crate) fn append_newline(s: &mut String) {
25 pub(crate) fn write_all_files<T>(
26 source_file: &[FileRecord],
29 ) -> Result<(), io::Error>
33 let mut emitter = create_emitter(config);
35 emitter.emit_header(out)?;
36 for &(ref filename, ref text) in source_file {
43 config.newline_style(),
46 emitter.emit_footer(out)?;
51 pub(crate) fn write_file<T>(
52 parse_sess: Option<&ParseSess>,
56 emitter: &mut dyn Emitter,
57 newline_style: NewlineStyle,
58 ) -> Result<emitter::EmitterResult, io::Error>
62 fn ensure_real_path(filename: &FileName) -> &Path {
64 FileName::Real(ref path) => path,
65 _ => panic!("cannot format `{}` and emit to files", filename),
69 impl From<&FileName> for rustc_span::FileName {
70 fn from(filename: &FileName) -> rustc_span::FileName {
72 FileName::Real(path) => {
73 rustc_span::FileName::Real(rustc_span::RealFileName::LocalPath(path.to_owned()))
75 FileName::Stdin => rustc_span::FileName::Custom("stdin".to_owned()),
80 // SourceFile's in the SourceMap will always have Unix-style line endings
81 // See: https://github.com/rust-lang/rustfmt/issues/3850
82 // So if the user has explicitly overridden the rustfmt `newline_style`
83 // config and `filename` is FileName::Real, then we must check the file system
84 // to get the original file value in order to detect newline_style conflicts.
85 // Otherwise, parse session is around (cfg(not(test))) and newline_style has been
86 // left as the default value, then try getting source from the parse session
87 // source map instead of hitting the file system. This also supports getting
88 // original text for `FileName::Stdin`.
89 let original_text = if newline_style != NewlineStyle::Auto && *filename != FileName::Stdin {
90 Lrc::new(fs::read_to_string(ensure_real_path(filename))?)
92 match parse_sess.and_then(|sess| sess.get_original_snippet(filename)) {
94 None => Lrc::new(fs::read_to_string(ensure_real_path(filename))?),
98 let formatted_file = emitter::FormattedFile {
100 original_text: original_text.as_str(),
104 emitter.emit_formatted_file(out, formatted_file)