Skip to content
This repository was archived by the owner on Oct 17, 2024. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
25 changes: 24 additions & 1 deletion Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,4 @@ nursery = "warn"
[dev-dependencies]
rstest = { version = "0.19.0" } # parametrized tests
indoc = { version = "2.0.5" } # dedented test cases for literal strings
pretty_assertions = "1.4.0"
1 change: 1 addition & 0 deletions rust/src/build_system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ pub fn fix(tables: &mut Tables, keep_full_version: bool) {
#[cfg(test)]
mod tests {
use indoc::indoc;
use pretty_assertions::assert_eq;
use rstest::rstest;
use taplo::formatter::{format_syntax, Options};
use taplo::parser::parse;
Expand Down
13 changes: 13 additions & 0 deletions rust/src/data/column-width.expected.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[build-system]
build-backend = "backend"
requires = [ "c>=1.5", "d==2" ]

[project]
name = "beta"
classifiers = [
"Programming Language :: Python :: 3 :: Only",
"Programming Language :: Python :: 3.12",
]
dependencies = [
"e>=1.5",
]
9 changes: 9 additions & 0 deletions rust/src/data/column-width.start.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[build-system]
build-backend = "backend"
requires = ["c>=1.5", "d == 2" ]

[project]
name = "beta"
dependencies = [
"e>=1.5",
]
1 change: 1 addition & 0 deletions rust/src/global.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ pub fn reorder_tables(root_ast: &SyntaxNode<Lang>, tables: &mut Tables) {
#[cfg(test)]
mod tests {
use indoc::indoc;
use pretty_assertions::assert_eq;
use rstest::rstest;
use taplo::formatter::{format_syntax, Options};
use taplo::parser::parse;
Expand Down
19 changes: 15 additions & 4 deletions rust/src/helpers/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ use lexical_sort::{natural_lexical_cmp, StringSort};
use taplo::syntax::SyntaxKind::{ARRAY, COMMA, NEWLINE, STRING, VALUE, WHITESPACE};
use taplo::syntax::{SyntaxElement, SyntaxKind, SyntaxNode};

use crate::helpers::create::{make_comma, make_newline};
use crate::helpers::create::make_comma;
use crate::helpers::string::{load_text, update_content};

use super::create::make_newline;

pub fn transform<F>(node: &SyntaxNode, transform: &F)
where
F: Fn(&str) -> String,
Expand Down Expand Up @@ -37,6 +39,10 @@ where
.filter(|x| *x == COMMA || *x == VALUE)
.last()
== Some(COMMA);
let multiline = array_node
.children_with_tokens()
.find(|e| e.kind() == NEWLINE)
.is_some();
let mut value_set = Vec::<Vec<SyntaxElement>>::new();
let entry_set = RefCell::new(Vec::<SyntaxElement>::new());
let mut key_to_pos = HashMap::<String, usize>::new();
Expand Down Expand Up @@ -67,7 +73,9 @@ where
match &entry.kind() {
SyntaxKind::BRACKET_START => {
entries.push(entry);
entries.push(make_newline());
if multiline {
entries.push(make_newline());
}
previous_is_bracket_open = true;
}
SyntaxKind::BRACKET_END => {
Expand All @@ -80,7 +88,9 @@ where
}
VALUE => {
if has_value {
entry_set.borrow_mut().push(make_newline());
if multiline {
entry_set.borrow_mut().push(make_newline());
}
add_to_value_set(entry_value.clone());
}
has_value = true;
Expand Down Expand Up @@ -117,7 +127,7 @@ where

let mut order: Vec<String> = key_to_pos.clone().into_keys().collect();
order.string_sort_unstable(natural_lexical_cmp);
let end = entries.split_off(2);
let end = entries.split_off(if multiline { 2 } else { 1 });
for key in order {
entries.extend(value_set[key_to_pos[&key]].clone());
}
Expand All @@ -140,6 +150,7 @@ where
#[cfg(test)]
mod tests {
use indoc::indoc;
use pretty_assertions::assert_eq;
use rstest::rstest;
use taplo::formatter::{format_syntax, Options};
use taplo::parser::parse;
Expand Down
1 change: 1 addition & 0 deletions rust/src/helpers/pep508.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ pub fn get_canonic_requirement_name(value: &str) -> String {

#[cfg(test)]
mod tests {
use pretty_assertions::assert_eq;
use rstest::rstest;

use crate::helpers::pep508::{format_requirement, get_canonic_requirement_name};
Expand Down
40 changes: 30 additions & 10 deletions rust/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,16 +63,16 @@ pub fn format_toml(content: &str, opt: &Settings) -> String {
reorder_tables(&root_ast, &mut tables);

let options = Options {
align_entries: false, // do not align by =
align_comments: true, // align inline comments
align_single_comments: true, // align comments after entries
array_trailing_comma: true, // ensure arrays finish with trailing comma
array_auto_expand: true, // arrays go to multi line for easier diffs
array_auto_collapse: false, // do not collapse for easier diffs
compact_arrays: false, // do not compact for easier diffs
compact_inline_tables: false, // do not compact for easier diffs
compact_entries: false, // do not compact for easier diffs
column_width: opt.column_width, // always expand arrays per https://github.com/tamasfe/taplo/issues/390
align_entries: false, // do not align by =
align_comments: true, // align inline comments
align_single_comments: true, // align comments after entries
array_trailing_comma: true, // ensure arrays finish with trailing comma
array_auto_expand: true, // arrays go to multi line when too long
array_auto_collapse: false, // do not collapse for easier diffs
compact_arrays: false, // leave whitespace
compact_inline_tables: false, // leave whitespace
compact_entries: false, // leave whitespace
column_width: opt.column_width,
indent_tables: false,
indent_entries: false,
inline_table_expand: true,
Expand Down Expand Up @@ -104,6 +104,7 @@ mod tests {
use std::path::{Path, PathBuf};

use indoc::indoc;
use pretty_assertions::assert_eq;
use rstest::{fixture, rstest};

use crate::{format_toml, Settings};
Expand Down Expand Up @@ -316,4 +317,23 @@ mod tests {
let second = format_toml(got.as_str(), &settings);
assert_eq!(second, got);
}

/// Test that the column width is respected,
/// and that arrays are neither exploded nor collapsed without reason
#[rstest]
fn test_column_width(data: PathBuf) {
let start = read_to_string(data.join("column-width.start.toml")).unwrap();
let settings = Settings {
column_width: 80,
indent: 4,
keep_full_version: false,
max_supported_python: (3, 12),
min_supported_python: (3, 12),
};
let got = format_toml(start.as_str(), &settings);
let expected = read_to_string(data.join("column-width.expected.toml")).unwrap();
assert_eq!(got, expected);
let second = format_toml(got.as_str(), &settings);
assert_eq!(second, got);
}
}
1 change: 1 addition & 0 deletions rust/src/project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,7 @@ mod tests {
use taplo::formatter::{format_syntax, Options};
use taplo::parser::parse;
use taplo::syntax::SyntaxElement;
use pretty_assertions::assert_eq;

use crate::helpers::table::Tables;
use crate::project::fix;
Expand Down
1 change: 1 addition & 0 deletions rust/src/ruff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ mod tests {
use taplo::formatter::{format_syntax, Options};
use taplo::parser::parse;
use taplo::syntax::SyntaxElement;
use pretty_assertions::assert_eq;

use crate::helpers::table::Tables;
use crate::ruff::fix;
Expand Down
16 changes: 4 additions & 12 deletions tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,11 @@
def test_format_toml() -> None:
txt = """
[project]
keywords = [
"A",
]
keywords = [ "A" ]
classifiers = [
"Programming Language :: Python :: 3 :: Only",
]
dynamic = [
"B",
]
dynamic = [ "B" ]
dependencies = [
"requests>=2.0",
]
Expand All @@ -33,17 +29,13 @@ def test_format_toml() -> None:

expected = """\
[project]
keywords = [
"A",
]
keywords = [ "A" ]
classifiers = [
"Programming Language :: Python :: 3 :: Only",
"Programming Language :: Python :: 3.7",
"Programming Language :: Python :: 3.8",
]
dynamic = [
"B",
]
dynamic = [ "B" ]
dependencies = [
"requests>=2.0",
]
Expand Down