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

Fix table reordering unstable for empty sub-tables #19

Merged
merged 1 commit into from
May 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "pyproject-fmt-rust"
version = "1.1.0"
version = "1.1.1"
description = "Format pyproject.toml files"
repository = "https://github.com/tox-dev/pyproject-fmt"
readme = "README.md"
Expand Down
10 changes: 7 additions & 3 deletions rust/src/helpers/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,7 @@ impl Tables {
pub fn reorder(&mut self, root_ast: &SyntaxNode, order: &[&str]) {
let mut to_insert = Vec::<SyntaxElement>::new();
let mut entry_count: usize = 0;

let order = calculate_order(&self.header_to_pos, order);
let order = calculate_order(&self.header_to_pos, &self.table_set, order);
let mut next = order.clone();
if !next.is_empty() {
next.remove(0);
Expand Down Expand Up @@ -104,7 +103,11 @@ impl Tables {
}
}

fn calculate_order(header_to_pos: &HashMap<String, Vec<usize>>, ordering: &[&str]) -> Vec<String> {
fn calculate_order(
header_to_pos: &HashMap<String, Vec<usize>>,
table_set: &[RefCell<Vec<SyntaxElement>>],
ordering: &[&str],
) -> Vec<String> {
let max_ordering = ordering.len() * 2;
let key_to_pos = ordering
.iter()
Expand All @@ -115,6 +118,7 @@ fn calculate_order(header_to_pos: &HashMap<String, Vec<usize>>, ordering: &[&str
let mut header_pos: Vec<(String, usize)> = header_to_pos
.clone()
.into_iter()
.filter(|(_k, v)| v.iter().any(|p| !table_set.get(*p).unwrap().borrow().is_empty()))
.map(|(k, v)| (k, *v.iter().min().unwrap()))
.collect();

Expand Down
36 changes: 33 additions & 3 deletions rust/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ mod tests {
(3, 8)
)]
#[case::subsubtable(
indoc ! {r#"
indoc ! {r"
[project]
[tool.coverage.report]
a = 2
Expand All @@ -188,7 +188,7 @@ mod tests {
a = 1
[tool.coverage.run]
a = 3
"#},
"},
indoc ! {r#"
[project]
classifiers = [
Expand Down Expand Up @@ -239,6 +239,36 @@ mod tests {
true,
(3, 8)
)]
#[case::unstable_issue_18(
indoc ! {r#"
[project]
requires-python = "==3.12"
classifiers = [
"Programming Language :: Python :: 3 :: Only",
"Programming Language :: Python :: 3.12",
]
[project.urls]
Source = "https://github.com/VWS-Python/vws-python-mock"

[tool.setuptools]
zip-safe = false
"#},
indoc ! {r#"
[project]
requires-python = "==3.12"
classifiers = [
"Programming Language :: Python :: 3 :: Only",
"Programming Language :: Python :: 3.12",
]
urls.Source = "https://github.com/VWS-Python/vws-python-mock"

[tool.setuptools]
zip-safe = false
"#},
2,
true,
(3, 8)
)]
fn test_format_toml(
#[case] start: &str,
#[case] expected: &str,
Expand All @@ -256,6 +286,6 @@ mod tests {
let got = format_toml(start, &settings);
assert_eq!(got, expected);
let second = format_toml(got.as_str(), &settings);
assert_eq!(got, second);
assert_eq!(second, got);
}
}