From 0faccdb144316c6216d49e2b78c6f0e0795b2103 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bern=C3=A1t=20G=C3=A1bor?= Date: Tue, 14 May 2024 11:05:57 -0700 Subject: [PATCH] Ensure sorting is stable (#16) --- rust/src/helpers/table.rs | 28 ++++++++++++++++------------ rust/src/main.rs | 21 +++++++++++---------- 2 files changed, 27 insertions(+), 22 deletions(-) diff --git a/rust/src/helpers/table.rs b/rust/src/helpers/table.rs index dff9ed9..cca8f45 100644 --- a/rust/src/helpers/table.rs +++ b/rust/src/helpers/table.rs @@ -61,19 +61,23 @@ impl Tables { } next.push(String::new()); for (name, next_name) in zip(order.iter(), next.iter()) { - let mut entries = self.get(name).unwrap().borrow().clone(); - if entries.is_empty() { - continue; - } - entry_count += entries.len(); - let last = entries.last().unwrap(); - if name.is_empty() && last.kind() == SyntaxKind::NEWLINE && entries.len() == 1 { - continue; - } - if last.kind() == SyntaxKind::NEWLINE && get_key(name) != get_key(next_name) { - entries.splice(entries.len() - 1..entries.len(), [make_empty_newline()]); + let entries = self.get(name).unwrap().borrow(); + if !entries.is_empty() { + entry_count += entries.len(); + let last = entries.last().unwrap(); + if name.is_empty() && last.kind() == SyntaxKind::NEWLINE && entries.len() == 1 { + continue; + } + let mut add = entries.clone(); + if get_key(name) != get_key(next_name) { + if last.kind() == SyntaxKind::NEWLINE { + // replace existing newline to ensure single newline + add.pop(); + } + add.push(make_empty_newline()); + } + to_insert.extend(add); } - to_insert.extend(entries); } root_ast.splice_children(0..entry_count, to_insert); } diff --git a/rust/src/main.rs b/rust/src/main.rs index 38b845c..e00c89d 100644 --- a/rust/src/main.rs +++ b/rust/src/main.rs @@ -195,6 +195,7 @@ mod tests { "Programming Language :: Python :: 3 :: Only", "Programming Language :: Python :: 3.8", ] + [tool.coverage] a = 0 [tool.coverage.report] @@ -215,16 +216,16 @@ mod tests { #[case] keep_full_version: bool, #[case] max_supported_python: (u8, u8), ) { - let got = format_toml( - start, - &Settings { - column_width: 1, - indent, - keep_full_version, - max_supported_python, - min_supported_python: (3, 8), - }, - ); + let settings = Settings { + column_width: 1, + indent, + keep_full_version, + max_supported_python, + min_supported_python: (3, 8), + }; + let got = format_toml(start, &settings); assert_eq!(got, expected); + let second = format_toml(got.as_str(), &settings); + assert_eq!(got, second); } }