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
17 use rustc_data_structures::sync::Lrc;
18
19 // Append a newline to the end of each file.
append_newline(s: &mut String)20 pub(crate) fn append_newline(s: &mut String) {
21 s.push('\n');
22 }
23
24 #[cfg(test)]
write_all_files<T>( source_file: &[FileRecord], out: &mut T, config: &Config, ) -> Result<(), io::Error> where T: Write,25 pub(crate) fn write_all_files<T>(
26 source_file: &[FileRecord],
27 out: &mut T,
28 config: &Config,
29 ) -> Result<(), io::Error>
30 where
31 T: Write,
32 {
33 let mut emitter = create_emitter(config);
34
35 emitter.emit_header(out)?;
36 for &(ref filename, ref text) in source_file {
37 write_file(
38 None,
39 filename,
40 text,
41 out,
42 &mut *emitter,
43 config.newline_style(),
44 )?;
45 }
46 emitter.emit_footer(out)?;
47
48 Ok(())
49 }
50
write_file<T>( parse_sess: Option<&ParseSess>, filename: &FileName, formatted_text: &str, out: &mut T, emitter: &mut dyn Emitter, newline_style: NewlineStyle, ) -> Result<emitter::EmitterResult, io::Error> where T: Write,51 pub(crate) fn write_file<T>(
52 parse_sess: Option<&ParseSess>,
53 filename: &FileName,
54 formatted_text: &str,
55 out: &mut T,
56 emitter: &mut dyn Emitter,
57 newline_style: NewlineStyle,
58 ) -> Result<emitter::EmitterResult, io::Error>
59 where
60 T: Write,
61 {
62 fn ensure_real_path(filename: &FileName) -> &Path {
63 match *filename {
64 FileName::Real(ref path) => path,
65 _ => panic!("cannot format `{}` and emit to files", filename),
66 }
67 }
68
69 impl From<&FileName> for rustc_span::FileName {
70 fn from(filename: &FileName) -> rustc_span::FileName {
71 match filename {
72 FileName::Real(path) => {
73 rustc_span::FileName::Real(rustc_span::RealFileName::LocalPath(path.to_owned()))
74 }
75 FileName::Stdin => rustc_span::FileName::Custom("stdin".to_owned()),
76 }
77 }
78 }
79
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))?)
91 } else {
92 match parse_sess.and_then(|sess| sess.get_original_snippet(filename)) {
93 Some(ori) => ori,
94 None => Lrc::new(fs::read_to_string(ensure_real_path(filename))?),
95 }
96 };
97
98 let formatted_file = emitter::FormattedFile {
99 filename,
100 original_text: original_text.as_str(),
101 formatted_text,
102 };
103
104 emitter.emit_formatted_file(out, formatted_file)
105 }
106