diff --git a/Cargo.lock b/Cargo.lock index dc9279f..3b53491 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -520,7 +520,7 @@ dependencies = [ [[package]] name = "pyproject-fmt-rust" -version = "1.0.7" +version = "1.1.1" dependencies = [ "indoc", "lexical-sort", diff --git a/Cargo.toml b/Cargo.toml index d57134f..49e9d39 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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" diff --git a/rust/src/helpers/table.rs b/rust/src/helpers/table.rs index a61217d..a43a28e 100644 --- a/rust/src/helpers/table.rs +++ b/rust/src/helpers/table.rs @@ -72,8 +72,7 @@ impl Tables { pub fn reorder(&mut self, root_ast: &SyntaxNode, order: &[&str]) { let mut to_insert = Vec::::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); @@ -104,7 +103,11 @@ impl Tables { } } -fn calculate_order(header_to_pos: &HashMap>, ordering: &[&str]) -> Vec { +fn calculate_order( + header_to_pos: &HashMap>, + table_set: &[RefCell>], + ordering: &[&str], +) -> Vec { let max_ordering = ordering.len() * 2; let key_to_pos = ordering .iter() @@ -115,6 +118,7 @@ fn calculate_order(header_to_pos: &HashMap>, 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(); diff --git a/rust/src/main.rs b/rust/src/main.rs index c82eaa8..c3e3ed7 100644 --- a/rust/src/main.rs +++ b/rust/src/main.rs @@ -178,7 +178,7 @@ mod tests { (3, 8) )] #[case::subsubtable( - indoc ! {r#" + indoc ! {r" [project] [tool.coverage.report] a = 2 @@ -188,7 +188,7 @@ mod tests { a = 1 [tool.coverage.run] a = 3 - "#}, + "}, indoc ! {r#" [project] classifiers = [ @@ -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, @@ -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); } }