Skip to content
This repository has been archived by the owner on Oct 17, 2024. It is now read-only.

Commit

Permalink
Ensure sorting is stable (#16)
Browse files Browse the repository at this point in the history
  • Loading branch information
gaborbernat authored May 14, 2024
1 parent 6892f56 commit 0faccdb
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 22 deletions.
28 changes: 16 additions & 12 deletions rust/src/helpers/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
21 changes: 11 additions & 10 deletions rust/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@ mod tests {
"Programming Language :: Python :: 3 :: Only",
"Programming Language :: Python :: 3.8",
]
[tool.coverage]
a = 0
[tool.coverage.report]
Expand All @@ -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);
}
}

0 comments on commit 0faccdb

Please sign in to comment.