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

Commit 0faccdb

Browse files
authored
Ensure sorting is stable (#16)
1 parent 6892f56 commit 0faccdb

File tree

2 files changed

+27
-22
lines changed

2 files changed

+27
-22
lines changed

rust/src/helpers/table.rs

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -61,19 +61,23 @@ impl Tables {
6161
}
6262
next.push(String::new());
6363
for (name, next_name) in zip(order.iter(), next.iter()) {
64-
let mut entries = self.get(name).unwrap().borrow().clone();
65-
if entries.is_empty() {
66-
continue;
67-
}
68-
entry_count += entries.len();
69-
let last = entries.last().unwrap();
70-
if name.is_empty() && last.kind() == SyntaxKind::NEWLINE && entries.len() == 1 {
71-
continue;
72-
}
73-
if last.kind() == SyntaxKind::NEWLINE && get_key(name) != get_key(next_name) {
74-
entries.splice(entries.len() - 1..entries.len(), [make_empty_newline()]);
64+
let entries = self.get(name).unwrap().borrow();
65+
if !entries.is_empty() {
66+
entry_count += entries.len();
67+
let last = entries.last().unwrap();
68+
if name.is_empty() && last.kind() == SyntaxKind::NEWLINE && entries.len() == 1 {
69+
continue;
70+
}
71+
let mut add = entries.clone();
72+
if get_key(name) != get_key(next_name) {
73+
if last.kind() == SyntaxKind::NEWLINE {
74+
// replace existing newline to ensure single newline
75+
add.pop();
76+
}
77+
add.push(make_empty_newline());
78+
}
79+
to_insert.extend(add);
7580
}
76-
to_insert.extend(entries);
7781
}
7882
root_ast.splice_children(0..entry_count, to_insert);
7983
}

rust/src/main.rs

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,7 @@ mod tests {
195195
"Programming Language :: Python :: 3 :: Only",
196196
"Programming Language :: Python :: 3.8",
197197
]
198+
198199
[tool.coverage]
199200
a = 0
200201
[tool.coverage.report]
@@ -215,16 +216,16 @@ mod tests {
215216
#[case] keep_full_version: bool,
216217
#[case] max_supported_python: (u8, u8),
217218
) {
218-
let got = format_toml(
219-
start,
220-
&Settings {
221-
column_width: 1,
222-
indent,
223-
keep_full_version,
224-
max_supported_python,
225-
min_supported_python: (3, 8),
226-
},
227-
);
219+
let settings = Settings {
220+
column_width: 1,
221+
indent,
222+
keep_full_version,
223+
max_supported_python,
224+
min_supported_python: (3, 8),
225+
};
226+
let got = format_toml(start, &settings);
228227
assert_eq!(got, expected);
228+
let second = format_toml(got.as_str(), &settings);
229+
assert_eq!(got, second);
229230
}
230231
}

0 commit comments

Comments
 (0)