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

Commit 2a88dc1

Browse files
authored
Fix table expansion leaves behind extra content (#25)
1 parent 4c9dba0 commit 2a88dc1

File tree

6 files changed

+44
-19
lines changed

6 files changed

+44
-19
lines changed

.pre-commit-config.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ repos:
55
- id: end-of-file-fixer
66
- id: trailing-whitespace
77
- repo: https://github.com/python-jsonschema/check-jsonschema
8-
rev: 0.28.3
8+
rev: 0.28.4
99
hooks:
1010
- id: check-github-workflows
1111
args: [ "--verbose" ]
@@ -20,7 +20,7 @@ repos:
2020
- id: tox-ini-fmt
2121
args: [ "-p", "fix" ]
2222
- repo: https://github.com/tox-dev/pyproject-fmt
23-
rev: "2.1.1"
23+
rev: "2.1.2"
2424
hooks:
2525
- id: pyproject-fmt
2626
- repo: https://github.com/astral-sh/ruff-pre-commit

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.2"
3+
version = "1.1.3"
44
description = "Format pyproject.toml files"
55
repository = "https://github.com/tox-dev/pyproject-fmt"
66
readme = "README.md"

pyproject.toml

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,20 @@ target-version = "py38"
6464
line-length = 120
6565
format.preview = true
6666
format.docstring-code-line-length = 100
67-
6867
format.docstring-code-format = true
6968
lint.select = [
7069
"ALL",
7170
]
71+
lint.ignore = [
72+
"ANN101", # no type annotation for self
73+
"ANN401", # allow Any as type annotation
74+
"COM812", # Conflict with formatter
75+
"CPY", # No copyright statements
76+
"D203", # `one-blank-line-before-class` (D203) and `no-blank-line-before-class` (D211) are incompatible
77+
"D212", # `multi-line-summary-first-line` (D212) and `multi-line-summary-second-line` (D213) are incompatible
78+
"ISC001", # Conflict with formatter
79+
"S104", # Possible binding to all interface
80+
]
7281
lint.per-file-ignores."tests/**/*.py" = [
7382
"D", # don"t care about documentation in tests
7483
"FBT", # don"t care about booleans as positional arguments in tests
@@ -85,16 +94,6 @@ lint.isort = { known-first-party = [
8594
], required-imports = [
8695
"from __future__ import annotations",
8796
] }
88-
lint.ignore = [
89-
"ANN101", # no type annotation for self
90-
"ANN401", # allow Any as type annotation
91-
"COM812", # Conflict with formatter
92-
"CPY", # No copyright statements
93-
"D203", # `one-blank-line-before-class` (D203) and `no-blank-line-before-class` (D211) are incompatible
94-
"D212", # `multi-line-summary-first-line` (D212) and `multi-line-summary-second-line` (D213) are incompatible
95-
"ISC001", # Conflict with formatter
96-
"S104", # Possible binding to all interface
97-
]
9897
lint.preview = true
9998

10099
[tool.codespell]

rust/src/helpers/table.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,6 @@ impl Tables {
7777

7878
pub fn reorder(&mut self, root_ast: &SyntaxNode, order: &[&str]) {
7979
let mut to_insert = Vec::<SyntaxElement>::new();
80-
let mut entry_count: usize = 0;
8180
let order = calculate_order(&self.header_to_pos, &self.table_set, order);
8281
let mut next = order.clone();
8382
if !next.is_empty() {
@@ -88,7 +87,6 @@ impl Tables {
8887
for entries in self.get(name).unwrap() {
8988
let got = entries.borrow_mut();
9089
if !got.is_empty() {
91-
entry_count += got.len();
9290
let last = got.last().unwrap();
9391
if name.is_empty() && last.kind() == NEWLINE && got.len() == 1 {
9492
continue;
@@ -105,7 +103,7 @@ impl Tables {
105103
}
106104
}
107105
}
108-
root_ast.splice_children(0..entry_count, to_insert);
106+
root_ast.splice_children(0..root_ast.children_with_tokens().count(), to_insert);
109107
}
110108
}
111109
fn calculate_order(

rust/src/main.rs

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,11 @@ pub fn _lib(m: &Bound<'_, PyModule>) -> PyResult<()> {
100100

101101
#[cfg(test)]
102102
mod tests {
103+
use std::fs::read_to_string;
104+
use std::path::{Path, PathBuf};
105+
103106
use indoc::indoc;
104-
use rstest::rstest;
107+
use rstest::{fixture, rstest};
105108

106109
use crate::{format_toml, Settings};
107110

@@ -288,4 +291,29 @@ mod tests {
288291
let second = format_toml(got.as_str(), &settings);
289292
assert_eq!(second, got);
290293
}
294+
295+
#[fixture]
296+
fn data() -> PathBuf {
297+
Path::new(env!("CARGO_MANIFEST_DIR"))
298+
.join("rust")
299+
.join("src")
300+
.join("data")
301+
}
302+
303+
#[rstest]
304+
fn test_issue_24(data: PathBuf) {
305+
let start = read_to_string(data.join("ruff-order.start.toml")).unwrap();
306+
let settings = Settings {
307+
column_width: 1,
308+
indent: 2,
309+
keep_full_version: false,
310+
max_supported_python: (3, 8),
311+
min_supported_python: (3, 8),
312+
};
313+
let got = format_toml(start.as_str(), &settings);
314+
let expected = read_to_string(data.join("ruff-order.expected.toml")).unwrap();
315+
assert_eq!(got, expected);
316+
let second = format_toml(got.as_str(), &settings);
317+
assert_eq!(second, got);
318+
}
291319
}

0 commit comments

Comments
 (0)