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

Commit 67d67bf

Browse files
authored
Fix table reordering unstable for empty sub-tables (#19)
1 parent 1bc07d8 commit 67d67bf

File tree

4 files changed

+42
-8
lines changed

4 files changed

+42
-8
lines changed

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "pyproject-fmt-rust"
3-
version = "1.1.0"
3+
version = "1.1.1"
44
description = "Format pyproject.toml files"
55
repository = "https://github.com/tox-dev/pyproject-fmt"
66
readme = "README.md"

rust/src/helpers/table.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,7 @@ impl Tables {
7272
pub fn reorder(&mut self, root_ast: &SyntaxNode, order: &[&str]) {
7373
let mut to_insert = Vec::<SyntaxElement>::new();
7474
let mut entry_count: usize = 0;
75-
76-
let order = calculate_order(&self.header_to_pos, order);
75+
let order = calculate_order(&self.header_to_pos, &self.table_set, order);
7776
let mut next = order.clone();
7877
if !next.is_empty() {
7978
next.remove(0);
@@ -104,7 +103,11 @@ impl Tables {
104103
}
105104
}
106105

107-
fn calculate_order(header_to_pos: &HashMap<String, Vec<usize>>, ordering: &[&str]) -> Vec<String> {
106+
fn calculate_order(
107+
header_to_pos: &HashMap<String, Vec<usize>>,
108+
table_set: &[RefCell<Vec<SyntaxElement>>],
109+
ordering: &[&str],
110+
) -> Vec<String> {
108111
let max_ordering = ordering.len() * 2;
109112
let key_to_pos = ordering
110113
.iter()
@@ -115,6 +118,7 @@ fn calculate_order(header_to_pos: &HashMap<String, Vec<usize>>, ordering: &[&str
115118
let mut header_pos: Vec<(String, usize)> = header_to_pos
116119
.clone()
117120
.into_iter()
121+
.filter(|(_k, v)| v.iter().any(|p| !table_set.get(*p).unwrap().borrow().is_empty()))
118122
.map(|(k, v)| (k, *v.iter().min().unwrap()))
119123
.collect();
120124

rust/src/main.rs

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ mod tests {
178178
(3, 8)
179179
)]
180180
#[case::subsubtable(
181-
indoc ! {r#"
181+
indoc ! {r"
182182
[project]
183183
[tool.coverage.report]
184184
a = 2
@@ -188,7 +188,7 @@ mod tests {
188188
a = 1
189189
[tool.coverage.run]
190190
a = 3
191-
"#},
191+
"},
192192
indoc ! {r#"
193193
[project]
194194
classifiers = [
@@ -239,6 +239,36 @@ mod tests {
239239
true,
240240
(3, 8)
241241
)]
242+
#[case::unstable_issue_18(
243+
indoc ! {r#"
244+
[project]
245+
requires-python = "==3.12"
246+
classifiers = [
247+
"Programming Language :: Python :: 3 :: Only",
248+
"Programming Language :: Python :: 3.12",
249+
]
250+
[project.urls]
251+
Source = "https://github.com/VWS-Python/vws-python-mock"
252+
253+
[tool.setuptools]
254+
zip-safe = false
255+
"#},
256+
indoc ! {r#"
257+
[project]
258+
requires-python = "==3.12"
259+
classifiers = [
260+
"Programming Language :: Python :: 3 :: Only",
261+
"Programming Language :: Python :: 3.12",
262+
]
263+
urls.Source = "https://github.com/VWS-Python/vws-python-mock"
264+
265+
[tool.setuptools]
266+
zip-safe = false
267+
"#},
268+
2,
269+
true,
270+
(3, 8)
271+
)]
242272
fn test_format_toml(
243273
#[case] start: &str,
244274
#[case] expected: &str,
@@ -256,6 +286,6 @@ mod tests {
256286
let got = format_toml(start, &settings);
257287
assert_eq!(got, expected);
258288
let second = format_toml(got.as_str(), &settings);
259-
assert_eq!(got, second);
289+
assert_eq!(second, got);
260290
}
261291
}

0 commit comments

Comments
 (0)