rust/src/source_file.rs

103 lines
3.0 KiB
Rust
Raw Normal View History

use std::fs;
use std::io::{self, Write};
use std::path::Path;
use crate::config::FileName;
use crate::emitter::{self, Emitter};
2020-03-26 21:25:34 -05:00
use crate::syntux::session::ParseSess;
use crate::NewlineStyle;
#[cfg(test)]
use crate::config::Config;
#[cfg(test)]
use crate::create_emitter;
#[cfg(test)]
2019-02-04 04:30:43 -06:00
use crate::formatting::FileRecord;
2020-03-26 21:25:34 -05:00
use std::rc::Rc;
// 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) {
s.push_str("\n");
}
#[cfg(test)]
pub(crate) fn write_all_files<T>(
source_file: &[FileRecord],
2017-11-30 08:04:19 -06:00
out: &mut T,
config: &Config,
) -> Result<(), io::Error>
where
T: Write,
{
2019-08-15 21:14:53 -05:00
let mut emitter = create_emitter(config);
emitter.emit_header(out)?;
for &(ref filename, ref text) in source_file {
write_file(
None,
filename,
text,
out,
&mut *emitter,
config.newline_style(),
)?;
}
emitter.emit_footer(out)?;
Ok(())
}
2019-05-09 13:37:51 -05:00
pub(crate) fn write_file<T>(
2020-03-26 21:25:34 -05:00
parse_sess: Option<&ParseSess>,
filename: &FileName,
formatted_text: &str,
out: &mut T,
2019-08-15 21:14:53 -05:00
emitter: &mut dyn Emitter,
newline_style: NewlineStyle,
) -> Result<emitter::EmitterResult, io::Error>
where
T: Write,
{
fn ensure_real_path(filename: &FileName) -> &Path {
match *filename {
FileName::Real(ref path) => path,
_ => panic!("cannot format `{}` and emit to files", filename),
}
}
2020-02-08 22:21:37 -06:00
impl From<&FileName> for rustc_span::FileName {
fn from(filename: &FileName) -> rustc_span::FileName {
match filename {
2020-02-08 22:21:37 -06:00
FileName::Real(path) => rustc_span::FileName::Real(path.to_owned()),
FileName::Stdin => rustc_span::FileName::Custom("stdin".to_owned()),
}
}
}
// 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
// original text for `FileName::Stdin`.
let original_text = if newline_style != NewlineStyle::Auto && *filename != FileName::Stdin {
2020-03-26 21:25:34 -05:00
Rc::new(fs::read_to_string(ensure_real_path(filename))?)
} else {
2020-03-26 21:25:34 -05:00
match parse_sess.and_then(|sess| sess.get_original_snippet(filename)) {
Some(ori) => ori,
2020-03-26 21:25:34 -05:00
None => Rc::new(fs::read_to_string(ensure_real_path(filename))?),
}
};
let formatted_file = emitter::FormattedFile {
filename,
2020-03-26 21:25:34 -05:00
original_text: original_text.as_str(),
formatted_text,
};
emitter.emit_formatted_file(out, formatted_file)
}