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

Commit 55f2813

Browse files
Fix arrays being expanded without reason (#43)
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent bc7200c commit 55f2813

File tree

4 files changed

+130
-50
lines changed

4 files changed

+130
-50
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.

rust/src/helpers/array.rs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ where
3737
.filter(|x| *x == COMMA || *x == VALUE)
3838
.last()
3939
== Some(COMMA);
40+
let multiline = array_node
41+
.children_with_tokens()
42+
.find(|e| e.kind() == NEWLINE)
43+
.is_some();
4044
let mut value_set = Vec::<Vec<SyntaxElement>>::new();
4145
let entry_set = RefCell::new(Vec::<SyntaxElement>::new());
4246
let mut key_to_pos = HashMap::<String, usize>::new();
@@ -67,7 +71,9 @@ where
6771
match &entry.kind() {
6872
SyntaxKind::BRACKET_START => {
6973
entries.push(entry);
70-
entries.push(make_newline());
74+
if multiline {
75+
entries.push(make_newline());
76+
}
7177
previous_is_bracket_open = true;
7278
}
7379
SyntaxKind::BRACKET_END => {
@@ -80,7 +86,9 @@ where
8086
}
8187
VALUE => {
8288
if has_value {
83-
entry_set.borrow_mut().push(make_newline());
89+
if multiline {
90+
entry_set.borrow_mut().push(make_newline());
91+
}
8492
add_to_value_set(entry_value.clone());
8593
}
8694
has_value = true;
@@ -117,7 +125,7 @@ where
117125

118126
let mut order: Vec<String> = key_to_pos.clone().into_keys().collect();
119127
order.string_sort_unstable(natural_lexical_cmp);
120-
let end = entries.split_off(2);
128+
let end = entries.split_off(if multiline { 2 } else { 1 });
121129
for key in order {
122130
entries.extend(value_set[key_to_pos[&key]].clone());
123131
}

rust/src/main.rs

Lines changed: 53 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -63,16 +63,16 @@ pub fn format_toml(content: &str, opt: &Settings) -> String {
6363
reorder_tables(&root_ast, &mut tables);
6464

6565
let options = Options {
66-
align_entries: false, // do not align by =
67-
align_comments: true, // align inline comments
68-
align_single_comments: true, // align comments after entries
69-
array_trailing_comma: true, // ensure arrays finish with trailing comma
70-
array_auto_expand: true, // arrays go to multi line for easier diffs
71-
array_auto_collapse: false, // do not collapse for easier diffs
72-
compact_arrays: false, // do not compact for easier diffs
73-
compact_inline_tables: false, // do not compact for easier diffs
74-
compact_entries: false, // do not compact for easier diffs
75-
column_width: opt.column_width, // always expand arrays per https://github.com/tamasfe/taplo/issues/390
66+
align_entries: false, // do not align by =
67+
align_comments: true, // align inline comments
68+
align_single_comments: true, // align comments after entries
69+
array_trailing_comma: true, // ensure arrays finish with trailing comma
70+
array_auto_expand: true, // arrays go to multi line when too long
71+
array_auto_collapse: false, // do not collapse for easier diffs
72+
compact_arrays: false, // leave whitespace
73+
compact_inline_tables: false, // leave whitespace
74+
compact_entries: false, // leave whitespace
75+
column_width: opt.column_width,
7676
indent_tables: false,
7777
indent_entries: false,
7878
inline_table_expand: true,
@@ -316,4 +316,47 @@ mod tests {
316316
let second = format_toml(got.as_str(), &settings);
317317
assert_eq!(second, got);
318318
}
319+
320+
/// Test that the column width is respected,
321+
/// and that arrays are neither exploded nor collapsed without reason
322+
#[rstest]
323+
fn test_column_width() {
324+
let start = indoc! {r#"
325+
[build-system]
326+
build-backend = "backend"
327+
requires = ["c>=1.5", "d == 2" ]
328+
329+
[project]
330+
name = "beta"
331+
dependencies = [
332+
"e>=1.5",
333+
]
334+
"#};
335+
let settings = Settings {
336+
column_width: 80,
337+
indent: 4,
338+
keep_full_version: false,
339+
max_supported_python: (3, 12),
340+
min_supported_python: (3, 12),
341+
};
342+
let got = format_toml(start, &settings);
343+
let expected = indoc! {r#"
344+
[build-system]
345+
build-backend = "backend"
346+
requires = [ "c>=1.5", "d==2" ]
347+
348+
[project]
349+
name = "beta"
350+
classifiers = [
351+
"Programming Language :: Python :: 3 :: Only",
352+
"Programming Language :: Python :: 3.12",
353+
]
354+
dependencies = [
355+
"e>=1.5",
356+
]
357+
"#};
358+
assert_eq!(got, expected);
359+
let second = format_toml(got.as_str(), &settings);
360+
assert_eq!(second, got);
361+
}
319362
}

tests/test_main.py

Lines changed: 65 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -2,50 +2,79 @@
22

33
from textwrap import dedent
44

5-
from pyproject_fmt_rust import Settings, format_toml
5+
import pytest
66

7+
from pyproject_fmt_rust import Settings, format_toml
78

8-
def test_format_toml() -> None:
9-
txt = """
10-
[project]
11-
keywords = [
12-
"A",
13-
]
14-
classifiers = [
15-
"Programming Language :: Python :: 3 :: Only",
16-
]
17-
dynamic = [
18-
"B",
19-
]
20-
dependencies = [
21-
"requests>=2.0",
22-
]
23-
"""
249

10+
@pytest.mark.parametrize(
11+
("start", "expected"),
12+
[
13+
pytest.param(
14+
"""
15+
[project]
16+
keywords = [
17+
"A",
18+
]
19+
classifiers = [
20+
"Programming Language :: Python :: 3 :: Only",
21+
]
22+
dynamic = [
23+
"B",
24+
]
25+
dependencies = [
26+
"requests>=2.0",
27+
]
28+
""",
29+
"""\
30+
[project]
31+
keywords = [
32+
"A",
33+
]
34+
classifiers = [
35+
"Programming Language :: Python :: 3 :: Only",
36+
"Programming Language :: Python :: 3.7",
37+
"Programming Language :: Python :: 3.8",
38+
]
39+
dynamic = [
40+
"B",
41+
]
42+
dependencies = [
43+
"requests>=2.0",
44+
]
45+
""",
46+
id="expanded",
47+
),
48+
pytest.param(
49+
"""
50+
[project]
51+
keywords = ["A"]
52+
classifiers = ["Programming Language :: Python :: 3 :: Only"]
53+
dynamic = ["B"]
54+
dependencies = ["requests>=2.0"]
55+
""",
56+
"""\
57+
[project]
58+
keywords = [ "A" ]
59+
classifiers = [
60+
"Programming Language :: Python :: 3 :: Only",
61+
"Programming Language :: Python :: 3.7",
62+
"Programming Language :: Python :: 3.8",
63+
]
64+
dynamic = [ "B" ]
65+
dependencies = [ "requests>=2.0" ]
66+
""",
67+
id="collapsed",
68+
),
69+
],
70+
)
71+
def test_format_toml(start: str, expected: str) -> None:
2572
settings = Settings(
2673
column_width=120,
2774
indent=4,
2875
keep_full_version=True,
2976
min_supported_python=(3, 7),
3077
max_supported_python=(3, 8),
3178
)
32-
res = format_toml(dedent(txt), settings)
33-
34-
expected = """\
35-
[project]
36-
keywords = [
37-
"A",
38-
]
39-
classifiers = [
40-
"Programming Language :: Python :: 3 :: Only",
41-
"Programming Language :: Python :: 3.7",
42-
"Programming Language :: Python :: 3.8",
43-
]
44-
dynamic = [
45-
"B",
46-
]
47-
dependencies = [
48-
"requests>=2.0",
49-
]
50-
"""
79+
res = format_toml(dedent(start), settings)
5180
assert res == dedent(expected)

0 commit comments

Comments
 (0)