2018-06-09 09:25:47 -05:00
|
|
|
use std::fs;
|
|
|
|
use std::io::{self, Write};
|
2019-03-01 10:59:38 -06:00
|
|
|
use std::path::Path;
|
|
|
|
|
2019-06-12 16:59:20 -05:00
|
|
|
use crate::config::FileName;
|
|
|
|
use crate::emitter::{self, Emitter};
|
2021-12-29 20:49:39 -06:00
|
|
|
use crate::parse::session::ParseSess;
|
2019-10-19 03:15:13 -05:00
|
|
|
use crate::NewlineStyle;
|
2015-08-01 08:02:59 -05:00
|
|
|
|
2019-06-12 16:59:20 -05:00
|
|
|
#[cfg(test)]
|
|
|
|
use crate::config::Config;
|
|
|
|
#[cfg(test)]
|
|
|
|
use crate::create_emitter;
|
2018-04-20 04:08:20 -05:00
|
|
|
#[cfg(test)]
|
2019-02-04 04:30:43 -06:00
|
|
|
use crate::formatting::FileRecord;
|
2021-02-16 22:50:30 -06:00
|
|
|
|
|
|
|
use rustc_data_structures::sync::Lrc;
|
2015-08-01 08:02:59 -05:00
|
|
|
|
|
|
|
// Append a newline to the end of each file.
|
2019-05-09 13:37:51 -05:00
|
|
|
pub(crate) fn append_newline(s: &mut String) {
|
2021-07-25 22:57:19 -05:00
|
|
|
s.push('\n');
|
2015-08-01 08:02:59 -05:00
|
|
|
}
|
|
|
|
|
2018-04-20 04:08:20 -05:00
|
|
|
#[cfg(test)]
|
|
|
|
pub(crate) fn write_all_files<T>(
|
2018-08-23 16:10:46 -05:00
|
|
|
source_file: &[FileRecord],
|
2017-11-30 08:04:19 -06:00
|
|
|
out: &mut T,
|
|
|
|
config: &Config,
|
|
|
|
) -> Result<(), io::Error>
|
2017-06-11 22:58:58 -05:00
|
|
|
where
|
|
|
|
T: Write,
|
2016-01-18 23:02:21 -06:00
|
|
|
{
|
2019-08-15 21:14:53 -05:00
|
|
|
let mut emitter = create_emitter(config);
|
2019-06-12 16:59:20 -05:00
|
|
|
|
|
|
|
emitter.emit_header(out)?;
|
2018-08-23 16:10:46 -05:00
|
|
|
for &(ref filename, ref text) in source_file {
|
2019-10-19 03:15:13 -05:00
|
|
|
write_file(
|
|
|
|
None,
|
|
|
|
filename,
|
|
|
|
text,
|
|
|
|
out,
|
|
|
|
&mut *emitter,
|
|
|
|
config.newline_style(),
|
|
|
|
)?;
|
2018-04-20 04:08:20 -05:00
|
|
|
}
|
2019-06-12 16:59:20 -05:00
|
|
|
emitter.emit_footer(out)?;
|
2015-08-01 08:02:59 -05:00
|
|
|
|
2015-12-29 22:54:35 -06:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2019-05-09 13:37:51 -05:00
|
|
|
pub(crate) fn write_file<T>(
|
2024-03-03 23:31:49 -06:00
|
|
|
psess: Option<&ParseSess>,
|
2017-12-08 07:16:47 -06:00
|
|
|
filename: &FileName,
|
2019-03-01 10:59:38 -06:00
|
|
|
formatted_text: &str,
|
2017-06-11 22:58:58 -05:00
|
|
|
out: &mut T,
|
2019-08-15 21:14:53 -05:00
|
|
|
emitter: &mut dyn Emitter,
|
2019-10-19 03:15:13 -05:00
|
|
|
newline_style: NewlineStyle,
|
2019-06-12 16:59:20 -05:00
|
|
|
) -> Result<emitter::EmitterResult, io::Error>
|
2017-06-11 22:58:58 -05:00
|
|
|
where
|
|
|
|
T: Write,
|
2016-01-18 23:02:21 -06:00
|
|
|
{
|
2019-03-01 10:59:38 -06:00
|
|
|
fn ensure_real_path(filename: &FileName) -> &Path {
|
|
|
|
match *filename {
|
|
|
|
FileName::Real(ref path) => path,
|
2023-10-22 20:21:44 -05:00
|
|
|
_ => panic!("cannot format `{filename}` and emit to files"),
|
2019-03-01 10:59:38 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-19 08:34:31 -05:00
|
|
|
#[allow(non_local_definitions)]
|
2020-02-08 22:21:37 -06:00
|
|
|
impl From<&FileName> for rustc_span::FileName {
|
|
|
|
fn from(filename: &FileName) -> rustc_span::FileName {
|
2019-03-01 10:59:38 -06:00
|
|
|
match filename {
|
2020-06-11 13:22:37 -05:00
|
|
|
FileName::Real(path) => {
|
2021-05-04 07:37:28 -05:00
|
|
|
rustc_span::FileName::Real(rustc_span::RealFileName::LocalPath(path.to_owned()))
|
2020-06-11 13:22:37 -05:00
|
|
|
}
|
2020-02-08 22:21:37 -06:00
|
|
|
FileName::Stdin => rustc_span::FileName::Custom("stdin".to_owned()),
|
2019-03-01 10:59:38 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-19 03:15:13 -05:00
|
|
|
// SourceFile's in the SourceMap will always have Unix-style line endings
|
|
|
|
// See: https://github.com/rust-lang/rustfmt/issues/3850
|
|
|
|
// So if the user has explicitly overridden the rustfmt `newline_style`
|
|
|
|
// config and `filename` is FileName::Real, then we must check the file system
|
|
|
|
// to get the original file value in order to detect newline_style conflicts.
|
|
|
|
// Otherwise, parse session is around (cfg(not(test))) and newline_style has been
|
|
|
|
// left as the default value, then try getting source from the parse session
|
|
|
|
// source map instead of hitting the file system. This also supports getting
|
2019-03-01 10:59:38 -06:00
|
|
|
// original text for `FileName::Stdin`.
|
2019-10-19 03:15:13 -05:00
|
|
|
let original_text = if newline_style != NewlineStyle::Auto && *filename != FileName::Stdin {
|
2021-02-16 22:50:30 -06:00
|
|
|
Lrc::new(fs::read_to_string(ensure_real_path(filename))?)
|
2019-10-19 03:15:13 -05:00
|
|
|
} else {
|
2024-03-03 23:31:49 -06:00
|
|
|
match psess.and_then(|psess| psess.get_original_snippet(filename)) {
|
2019-10-19 03:15:13 -05:00
|
|
|
Some(ori) => ori,
|
2021-02-16 22:50:30 -06:00
|
|
|
None => Lrc::new(fs::read_to_string(ensure_real_path(filename))?),
|
2019-10-19 03:15:13 -05:00
|
|
|
}
|
2017-12-08 07:16:47 -06:00
|
|
|
};
|
|
|
|
|
2019-06-12 16:59:20 -05:00
|
|
|
let formatted_file = emitter::FormattedFile {
|
|
|
|
filename,
|
2020-03-26 21:25:34 -05:00
|
|
|
original_text: original_text.as_str(),
|
2019-06-12 16:59:20 -05:00
|
|
|
formatted_text,
|
|
|
|
};
|
2015-08-01 08:02:59 -05:00
|
|
|
|
2019-06-12 16:59:20 -05:00
|
|
|
emitter.emit_formatted_file(out, formatted_file)
|
2015-08-01 08:02:59 -05:00
|
|
|
}
|