implement tidy bless for alphabetical blocks

This commit is contained in:
Waffle Lapkin 2025-08-16 14:26:34 +02:00
parent cdc977d6ec
commit 4e9c504cbf
No known key found for this signature in database
3 changed files with 31 additions and 13 deletions

View file

@ -5615,6 +5615,7 @@ dependencies = [
"semver",
"serde",
"similar",
"tempfile",
"termcolor",
"toml 0.7.8",
"walkdir",

View file

@ -18,6 +18,7 @@ rustc-hash = "2.0.0"
fluent-syntax = "0.12"
similar = "2.5.0"
toml = "0.7.8"
tempfile = "3.15.0"
[features]
build-metrics = ["dep:serde"]

View file

@ -214,20 +214,36 @@ fn check_lines<'a>(path: &Path, content: &'a str, tidy_ctx: &TidyCtx, check: &mu
// oh nyooo :(
if sorted != section {
let base_line_number = content[..offset + start_nl_end].lines().count();
let line_offset = sorted
.lines()
.zip(section.lines())
.enumerate()
.find(|(_, (a, b))| a != b)
.unwrap()
.0;
let line_number = base_line_number + line_offset;
if !tidy_ctx.is_bless_enabled() {
let base_line_number = content[..offset + start_nl_end].lines().count();
let line_offset = sorted
.lines()
.zip(section.lines())
.enumerate()
.find(|(_, (a, b))| a != b)
.unwrap()
.0;
let line_number = base_line_number + line_offset;
check.error(format!(
"{path}:{line_number}: line not in alphabetical order (tip: use --bless to sort this list)",
path = path.display(),
));
check.error(format!(
"{path}:{line_number}: line not in alphabetical order (tip: use --bless to sort this list)",
path = path.display(),
));
} else {
// Use atomic rename as to not corrupt the file upon crashes/ctrl+c
let mut tempfile =
tempfile::Builder::new().tempfile_in(path.parent().unwrap()).unwrap();
fs::copy(path, tempfile.path()).unwrap();
tempfile
.as_file_mut()
.seek(std::io::SeekFrom::Start((offset + start_nl_end) as u64))
.unwrap();
tempfile.as_file_mut().write_all(sorted.as_bytes()).unwrap();
tempfile.persist(path).unwrap();
}
}
// Start the next search after the end section