Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
16 changes: 16 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ rustfmt-config_proc_macro = { version = "0.3", path = "config_proc_macro" }
semver = "1.0.21"

[dev-dependencies]
indoc = "2.0.8"

@jieyouxu jieyouxu Oct 10, 2026 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ytmimi a quick vibecheck on the extra dev-dependency: do you think the changes here pull its weight? I more-or-less neutral with a lean towards "seems nicer for these cases".

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes—I think the places where indoc helps read a bit nicer, and I have more tests planned that would benefit similarly. I wrote a small dedent! macro as an alternative in that upcoming change (respecting the one LLM change in flight request to post this and the followups that sit on top of it), but that leaves rustfmt maintainers maintaining code that duplicates an established, stable solution.

My calculus is that indoc is a small crate—about 17 KB and 405 lines of code—with broad adoption. The readability benefit seems worth the dependency, especially as a dev-dependency.

Personally, I’d also be comfortable with it as a regular dependency, though I understand why a foundational tool like rustfmt would be more conservative. It feels like functionality that could reasonably belong in the standard library.

So I see little downside to using it for tests, and a relatively small tradeoff even for production code.

@jieyouxu jieyouxu Oct 10, 2026 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm yeah. Since this is dev-deps and test-only, I think it's fine introducing it.

Re. the calculus, indoc also has the other variants for UX that I don't think we want to be maintaining ourselves.

tempfile = "3.23.0"
insta = { version = "1.48.0", features = ["filters"] }

Expand Down
6 changes: 5 additions & 1 deletion src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -550,6 +550,7 @@ fn config_path(options: &dyn CliOptions) -> Result<Option<PathBuf>, Error> {
#[cfg(test)]
mod test {
use super::*;
use indoc::formatdoc;
use std::str;

use crate::config::macro_names::{MacroName, MacroSelectors};
Expand Down Expand Up @@ -726,7 +727,10 @@ mod test {
let toml = used_options.to_toml().unwrap();
assert_eq!(
toml,
format!("merge_derives = {merge_derives}\nskip_children = {skip_children}\n",)
formatdoc! {"
merge_derives = {merge_derives}
skip_children = {skip_children}
"}
);
}

Expand Down
33 changes: 25 additions & 8 deletions src/emitter/checkstyle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ where
#[cfg(test)]
mod tests {
use super::*;
use indoc::indoc;
use std::path::PathBuf;

#[test]
Expand All @@ -89,11 +90,27 @@ mod tests {
#[test]
fn emits_single_xml_tree_containing_all_files() {
let bin_file = "src/bin.rs";
let bin_original = ["fn main() {", "println!(\"Hello, world!\");", "}"];
let bin_formatted = ["fn main() {", " println!(\"Hello, world!\");", "}"];
let bin_original = indoc! {r#"
fn main() {
println!("Hello, world!");
}
"#};
let bin_formatted = indoc! {r#"
fn main() {
println!("Hello, world!");
}
"#};
let lib_file = "src/lib.rs";
let lib_original = ["fn greet() {", "println!(\"Greetings!\");", "}"];
let lib_formatted = ["fn greet() {", " println!(\"Greetings!\");", "}"];
let lib_original = indoc! {r#"
fn greet() {
println!("Greetings!");
}
"#};
let lib_formatted = indoc! {r#"
fn greet() {
println!("Greetings!");
}
"#};
let mut writer = Vec::new();
let mut emitter = CheckstyleEmitter::default();
let _ = emitter.emit_header(&mut writer);
Expand All @@ -102,8 +119,8 @@ mod tests {
&mut writer,
FormattedFile {
filename: &FileName::Real(PathBuf::from(bin_file)),
original_text: &bin_original.join("\n"),
formatted_text: &bin_formatted.join("\n"),
original_text: bin_original,
formatted_text: bin_formatted,
},
)
.unwrap();
Expand All @@ -112,8 +129,8 @@ mod tests {
&mut writer,
FormattedFile {
filename: &FileName::Real(PathBuf::from(lib_file)),
original_text: &lib_original.join("\n"),
formatted_text: &lib_formatted.join("\n"),
original_text: lib_original,
formatted_text: lib_formatted,
},
)
.unwrap();
Expand Down
30 changes: 25 additions & 5 deletions src/emitter/diff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ impl Emitter for DiffEmitter {
#[cfg(test)]
mod tests {
use super::*;
use indoc::{formatdoc, indoc};
use std::path::PathBuf;

#[test]
Expand All @@ -75,11 +76,27 @@ mod tests {
#[test]
fn prints_file_names_when_config_is_enabled() {
let bin_file = "src/bin.rs";
let bin_original = "fn main() {\nprintln!(\"Hello, world!\");\n}";
let bin_formatted = "fn main() {\n println!(\"Hello, world!\");\n}";
let bin_original = indoc! {r#"
fn main() {
println!("Hello, world!");
}
"#};
let bin_formatted = indoc! {r#"
fn main() {
println!("Hello, world!");
}
"#};
let lib_file = "src/lib.rs";
let lib_original = "fn greet() {\nprintln!(\"Greetings!\");\n}";
let lib_formatted = "fn greet() {\n println!(\"Greetings!\");\n}";
let lib_original = indoc! {r#"
fn greet() {
println!("Greetings!");
}
"#};
let lib_formatted = indoc! {r#"
fn greet() {
println!("Greetings!");
}
"#};

let mut writer = Vec::new();
let mut config = Config::default();
Expand Down Expand Up @@ -108,7 +125,10 @@ mod tests {

assert_eq!(
String::from_utf8(writer).unwrap(),
format!("{bin_file}\n{lib_file}\n"),
formatdoc! {"
{bin_file}
{lib_file}
"},
)
}

Expand Down
105 changes: 65 additions & 40 deletions src/emitter/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ impl JsonEmitter {
#[cfg(test)]
mod tests {
use super::*;
use indoc::indoc;
use std::path::PathBuf;

#[test]
Expand All @@ -122,7 +123,12 @@ mod tests {
expected_begin_line: 79,
expected_end_line: 82,
original: String::from("fn Foo<T>() where T: Bar {\n"),
expected: String::from("fn Foo<T>()\nwhere\n T: Bar,\n{\n"),
expected: String::from(indoc! {"
fn Foo<T>()
where
T: Bar,
{
"}),
}],
};
let mismatch = Mismatch {
Expand Down Expand Up @@ -213,32 +219,32 @@ mod tests {
#[test]
fn emits_array_with_files_with_diffs() {
let file_name = "src/bin.rs";
let original = [
"fn main() {",
"println!(\"Hello, world!\");",
"}",
"",
"#[cfg(test)]",
"mod tests {",
"#[test]",
"fn it_works() {",
" assert_eq!(2 + 2, 4);",
"}",
"}",
];
let formatted = [
"fn main() {",
" println!(\"Hello, world!\");",
"}",
"",
"#[cfg(test)]",
"mod tests {",
" #[test]",
" fn it_works() {",
" assert_eq!(2 + 2, 4);",
" }",
"}",
];
let original = indoc! {r#"
fn main() {
println!("Hello, world!");
}

#[cfg(test)]
mod tests {
#[test]
fn it_works() {
assert_eq!(2 + 2, 4);
}
}
"#};
let formatted = indoc! {r#"
fn main() {
println!("Hello, world!");
}

#[cfg(test)]
mod tests {
#[test]
fn it_works() {
assert_eq!(2 + 2, 4);
}
}
"#};
let mut writer = Vec::new();
let mut emitter = JsonEmitter::default();
let _ = emitter.emit_header(&mut writer);
Expand All @@ -247,8 +253,8 @@ mod tests {
&mut writer,
FormattedFile {
filename: &FileName::Real(PathBuf::from(file_name)),
original_text: &original.join("\n"),
formatted_text: &formatted.join("\n"),
original_text: original,
formatted_text: formatted,
},
)
.unwrap();
Expand All @@ -269,9 +275,12 @@ mod tests {
original_end_line: 10,
expected_begin_line: 7,
expected_end_line: 10,
original: String::from(
"#[test]\nfn it_works() {\n assert_eq!(2 + 2, 4);\n}\n",
),
original: String::from(indoc! {"
#[test]
fn it_works() {
assert_eq!(2 + 2, 4);
}
"}),
expected: String::from(
" #[test]\n fn it_works() {\n assert_eq!(2 + 2, 4);\n }\n",
),
Expand All @@ -286,11 +295,27 @@ mod tests {
#[test]
fn emits_valid_json_with_multiple_files() {
let bin_file = "src/bin.rs";
let bin_original = ["fn main() {", "println!(\"Hello, world!\");", "}"];
let bin_formatted = ["fn main() {", " println!(\"Hello, world!\");", "}"];
let bin_original = indoc! {r#"
fn main() {
println!("Hello, world!");
}
"#};
let bin_formatted = indoc! {r#"
fn main() {
println!("Hello, world!");
}
"#};
let lib_file = "src/lib.rs";
let lib_original = ["fn greet() {", "println!(\"Greetings!\");", "}"];
let lib_formatted = ["fn greet() {", " println!(\"Greetings!\");", "}"];
let lib_original = indoc! {r#"
fn greet() {
println!("Greetings!");
}
"#};
let lib_formatted = indoc! {r#"
fn greet() {
println!("Greetings!");
}
"#};
let mut writer = Vec::new();
let mut emitter = JsonEmitter::default();
let _ = emitter.emit_header(&mut writer);
Expand All @@ -299,8 +324,8 @@ mod tests {
&mut writer,
FormattedFile {
filename: &FileName::Real(PathBuf::from(bin_file)),
original_text: &bin_original.join("\n"),
formatted_text: &bin_formatted.join("\n"),
original_text: bin_original,
formatted_text: bin_formatted,
},
)
.unwrap();
Expand All @@ -309,8 +334,8 @@ mod tests {
&mut writer,
FormattedFile {
filename: &FileName::Real(PathBuf::from(lib_file)),
original_text: &lib_original.join("\n"),
formatted_text: &lib_formatted.join("\n"),
original_text: lib_original,
formatted_text: lib_formatted,
},
)
.unwrap();
Expand Down
9 changes: 8 additions & 1 deletion src/rustfmt_diff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,7 @@ mod test {
use super::DiffLine::*;
use super::{Mismatch, make_diff};
use super::{ModifiedChunk, ModifiedLines};
use indoc::indoc;

#[test]
fn diff_simple() {
Expand Down Expand Up @@ -370,7 +371,13 @@ mod test {
fn modified_lines_from_str() {
use std::str::FromStr;

let src = "1 6 2\nfn some() {}\nfn main() {}\n25 3 1\n struct Test {}";
let src = indoc! {"
1 6 2
fn some() {}
fn main() {}
25 3 1
struct Test {}
"};
let lines = ModifiedLines::from_str(src).unwrap();
assert_eq!(
lines,
Expand Down
Loading
Loading