Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parallel formatting to increase speed #6095

Open
wants to merge 23 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Fixup most tests
  • Loading branch information
MarcusGrass committed Feb 26, 2024
commit adef47a140f62abc0760f455b2d4973bf80182b6
18 changes: 9 additions & 9 deletions src/comment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1921,19 +1921,19 @@ mod test {
let comment = rewrite_comment(" //test",
true,
Shape::legacy(100, Indent::new(0, 100)),
&wrap_normalize_config).unwrap();
&wrap_normalize_config, &Printer::no_color()).unwrap();
assert_eq!("/* test */", comment);

let comment = rewrite_comment("// comment on a",
false,
Shape::legacy(10, Indent::empty()),
&wrap_normalize_config).unwrap();
&wrap_normalize_config, &Printer::no_color()).unwrap();
assert_eq!("// comment\n// on a", comment);

let comment = rewrite_comment("// A multi line comment\n // between args.",
false,
Shape::legacy(60, Indent::new(0, 12)),
&wrap_normalize_config).unwrap();
&wrap_normalize_config, &Printer::no_color()).unwrap();
assert_eq!("// A multi line comment\n // between args.", comment);

let input = "// comment";
Expand All @@ -1942,13 +1942,13 @@ mod test {
let comment = rewrite_comment(input,
true,
Shape::legacy(9, Indent::new(0, 69)),
&wrap_normalize_config).unwrap();
&wrap_normalize_config, &Printer::no_color()).unwrap();
assert_eq!(expected, comment);

let comment = rewrite_comment("/* trimmed */",
true,
Shape::legacy(100, Indent::new(0, 100)),
&wrap_normalize_config).unwrap();
&wrap_normalize_config, &Printer::no_color()).unwrap();
assert_eq!("/* trimmed */", comment);

// Check that different comment style are properly recognised.
Expand All @@ -1959,7 +1959,7 @@ mod test {
*/"#,
false,
Shape::legacy(100, Indent::new(0, 0)),
&wrap_normalize_config).unwrap();
&wrap_normalize_config, &Printer::no_color()).unwrap();
assert_eq!("/// test1\n/// test2\n// test3", comment);

// Check that the blank line marks the end of a commented paragraph.
Expand All @@ -1968,7 +1968,7 @@ mod test {
// test2"#,
false,
Shape::legacy(100, Indent::new(0, 0)),
&wrap_normalize_config).unwrap();
&wrap_normalize_config, &Printer::no_color()).unwrap();
assert_eq!("// test1\n\n// test2", comment);

// Check that the blank line marks the end of a custom-commented paragraph.
Expand All @@ -1977,7 +1977,7 @@ mod test {
//@ test2"#,
false,
Shape::legacy(100, Indent::new(0, 0)),
&wrap_normalize_config).unwrap();
&wrap_normalize_config, &Printer::no_color()).unwrap();
assert_eq!("//@ test1\n\n//@ test2", comment);

// Check that bare lines are just indented but otherwise left unchanged.
Expand All @@ -1989,7 +1989,7 @@ mod test {
*/"#,
false,
Shape::legacy(100, Indent::new(0, 0)),
&wrap_config).unwrap();
&wrap_config, &Printer::no_color()).unwrap();
assert_eq!("// test1\n/*\n a bare line!\n\n another bare line!\n*/", comment);
}

Expand Down
2 changes: 2 additions & 0 deletions src/emitter/checkstyle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ mod tests {
let _ = emitter
.emit_formatted_file(
&mut writer,
&Printer::no_color(),
FormattedFile {
filename: &FileName::Real(PathBuf::from(bin_file)),
original_text: &bin_original.join("\n"),
Expand All @@ -111,6 +112,7 @@ mod tests {
let _ = emitter
.emit_formatted_file(
&mut writer,
&Printer::no_color(),
FormattedFile {
filename: &FileName::Real(PathBuf::from(lib_file)),
original_text: &lib_original.join("\n"),
Expand Down
4 changes: 4 additions & 0 deletions src/emitter/diff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ mod tests {
let result = emitter
.emit_formatted_file(
&mut writer,
&Printer::no_color(),
FormattedFile {
filename: &FileName::Real(PathBuf::from("src/lib.rs")),
original_text: "fn empty() {}\n",
Expand Down Expand Up @@ -90,6 +91,7 @@ mod tests {
let _ = emitter
.emit_formatted_file(
&mut writer,
&Printer::no_color(),
FormattedFile {
filename: &FileName::Real(PathBuf::from(bin_file)),
original_text: bin_original,
Expand All @@ -100,6 +102,7 @@ mod tests {
let _ = emitter
.emit_formatted_file(
&mut writer,
&Printer::no_color(),
FormattedFile {
filename: &FileName::Real(PathBuf::from(lib_file)),
original_text: lib_original,
Expand All @@ -122,6 +125,7 @@ mod tests {
let _ = emitter
.emit_formatted_file(
&mut writer,
&Printer::no_color(),
FormattedFile {
filename: &FileName::Real(PathBuf::from("src/lib.rs")),
original_text: "fn empty() {}\n",
Expand Down
4 changes: 4 additions & 0 deletions src/emitter/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@ mod tests {
let result = emitter
.emit_formatted_file(
&mut writer,
&Printer::no_color(),
FormattedFile {
filename: &FileName::Real(PathBuf::from("src/lib.rs")),
original_text: "fn empty() {}\n",
Expand Down Expand Up @@ -245,6 +246,7 @@ mod tests {
let result = emitter
.emit_formatted_file(
&mut writer,
&Printer::no_color(),
FormattedFile {
filename: &FileName::Real(PathBuf::from(file_name)),
original_text: &original.join("\n"),
Expand Down Expand Up @@ -297,6 +299,7 @@ mod tests {
let _ = emitter
.emit_formatted_file(
&mut writer,
&Printer::no_color(),
FormattedFile {
filename: &FileName::Real(PathBuf::from(bin_file)),
original_text: &bin_original.join("\n"),
Expand All @@ -307,6 +310,7 @@ mod tests {
let _ = emitter
.emit_formatted_file(
&mut writer,
&Printer::no_color(),
FormattedFile {
filename: &FileName::Real(PathBuf::from(lib_file)),
original_text: &lib_original.join("\n"),
Expand Down
15 changes: 7 additions & 8 deletions src/emitter/stdout.rs
Original file line number Diff line number Diff line change
@@ -1,33 +1,32 @@
use crate::buf_println;
use super::*;
use crate::config::Verbosity;

#[derive(Debug)]
pub(crate) struct StdoutEmitter {
pub(crate) struct IntoOutputEmitter {
verbosity: Verbosity,
}

impl StdoutEmitter {
impl IntoOutputEmitter {
pub(crate) fn new(verbosity: Verbosity) -> Self {
Self { verbosity }
}
}

impl Emitter for StdoutEmitter {
impl Emitter for IntoOutputEmitter {
fn emit_formatted_file(
&mut self,
_output: &mut dyn Write,
printer: &Printer,
output: &mut dyn Write,
_printer: &Printer,
FormattedFile {
filename,
formatted_text,
..
}: FormattedFile<'_>,
) -> Result<EmitterResult, io::Error> {
if self.verbosity != Verbosity::Quiet {
buf_println!(printer, "{filename}:\n");
writeln!(output, "{filename}:\n")?;
}
buf_println!(printer, "{formatted_text}");
write!(output, "{formatted_text}")?;
Ok(EmitterResult::default())
}
}
16 changes: 9 additions & 7 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -527,12 +527,14 @@ pub(crate) fn create_emitter<'a>(config: &Config) -> Box<dyn Emitter + 'a> {
config.print_misformatted_file_names(),
)),
EmitMode::Stdout | EmitMode::Coverage => {
Box::new(emitter::StdoutEmitter::new(config.verbose()))
Box::new(emitter::IntoOutputEmitter::new(config.verbose()))
}
EmitMode::Json => Box::new(emitter::JsonEmitter::default()),
EmitMode::ModifiedLines => Box::new(emitter::ModifiedLinesEmitter::default()),
EmitMode::Checkstyle => Box::new(emitter::CheckstyleEmitter::default()),
EmitMode::Diff => Box::new(emitter::DiffEmitter::new(config.clone())),
EmitMode::Diff => {
Box::new(emitter::DiffEmitter::new(config.clone()))
},
}
}

Expand Down Expand Up @@ -586,15 +588,15 @@ mod unit_tests {
// `format_snippet()` and `format_code_block()` should not panic
// even when we cannot parse the given snippet.
let snippet = "let";
assert!(format_snippet(snippet, &Config::default(), false).is_none());
assert!(format_code_block(snippet, &Config::default(), false).is_none());
assert!(format_snippet(snippet, &Config::default(), false, &Printer::no_color()).is_none());
assert!(format_code_block(snippet, &Config::default(), false, &Printer::no_color()).is_none());
}

fn test_format_inner<F>(formatter: F, input: &str, expected: &str) -> bool
where
F: Fn(&str, &Config, bool) -> Option<FormattedSnippet>,
F: Fn(&str, &Config, bool, &Printer) -> Option<FormattedSnippet>,
{
let output = formatter(input, &Config::default(), false);
let output = formatter(input, &Config::default(), false, &Printer::no_color());
output.is_some() && output.unwrap().snippet == expected
}

Expand All @@ -616,7 +618,7 @@ mod unit_tests {
fn test_format_code_block_fail() {
#[rustfmt::skip]
let code_block = "this_line_is_100_characters_long_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx(x, y, z);";
assert!(format_code_block(code_block, &Config::default(), false).is_none());
assert!(format_code_block(code_block, &Config::default(), false, &Printer::no_color()).is_none());
}

#[test]
Expand Down
4 changes: 4 additions & 0 deletions src/print.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ impl Printer {
}
}

pub fn no_color() -> Self {
Self::new(Color::Never)
}

#[inline]
pub fn push_msg(&self, msg: PrintMessage) {
self.inner.lock().unwrap().messages.push(msg);
Expand Down
1 change: 1 addition & 0 deletions src/source_file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ where
out,
&mut *emitter,
config.newline_style(),
&Printer::no_color()
)?;
}
emitter.emit_footer(out)?;
Expand Down
4 changes: 3 additions & 1 deletion src/test/configuration_snippet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use super::{print_mismatches, write_message, DIFF_CONTEXT_SIZE};
use crate::config::{Config, EmitMode, Verbosity};
use crate::rustfmt_diff::{make_diff, Mismatch};
use crate::{Input, Session};
use crate::print::Printer;

const CONFIGURATIONS_FILE_NAME: &str = "Configurations.md";

Expand Down Expand Up @@ -197,7 +198,8 @@ impl ConfigCodeBlock {
let mut buf: Vec<u8> = vec![];

{
let mut session = Session::new(config, Some(&mut buf));
let printer = Printer::no_color();
let mut session = Session::new(config, Some(&mut buf), &printer);
session.format(input).unwrap();
if self.has_parsing_errors(&session) {
return false;
Expand Down
Loading