Skip to content
Closed
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
121 changes: 121 additions & 0 deletions Cargo.lock

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

5 changes: 3 additions & 2 deletions common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@ taplo = { version = "0.13.2" } # formatter
pep508_rs = { version = "0.8.1" }

[dev-dependencies]
rstest = { version = "0.25.0" } # parametrized tests
indoc = { version = "2.0.6" } # dedented test cases for literal strings
rstest = { version = "0.25.0" } # parametrized tests
indoc = { version = "2.0.6" } # dedented test cases for literal strings
similar-asserts = { version = "1.7.0" } # diff-based assertions
4 changes: 2 additions & 2 deletions common/src/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ use taplo::syntax::SyntaxKind::{ARRAY, COMMA, NEWLINE, STRING, VALUE, WHITESPACE
use taplo::syntax::{SyntaxElement, SyntaxKind, SyntaxNode};

use crate::create::{make_comma, make_newline};
use crate::string::{load_text, update_content};
use crate::string::{load_text, update_content, StringUpdateMode};
use crate::util::{find_first, iter};

pub fn transform<F>(node: &SyntaxNode, transform: &F)
where
F: Fn(&str) -> String,
{
iter(node, [ARRAY, VALUE].as_ref(), &|array_entry| {
update_content(array_entry, transform);
update_content(array_entry, transform, StringUpdateMode::ConvertToString);
});
}

Expand Down
23 changes: 18 additions & 5 deletions common/src/create.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
use taplo::parser::parse;
use taplo::syntax::SyntaxElement;
use taplo::syntax::SyntaxKind::{ARRAY, COMMA, ENTRY, KEY, NEWLINE, STRING, VALUE};
use taplo::syntax::SyntaxKind::{
ARRAY, COMMA, ENTRY, KEY, MULTI_LINE_STRING, MULTI_LINE_STRING_LITERAL, NEWLINE, STRING, STRING_LITERAL, VALUE,
};

pub fn make_string_node(text: &str) -> SyntaxElement {
let expr = &format!("a = \"{}\"", text.replace('"', "\\\""));
for root in parse(expr)
use crate::string::StringType;

pub fn make_string_node(text: &str, target_type: StringType) -> SyntaxElement {
let expr = match target_type {
StringType::String => format!("a = \"{}\"", text.replace('"', "\\\"")),
StringType::Multiline => format!("a = \"\"\"{}\"\"\"", text.replace('"', "\\\"")),
StringType::Literal => format!("a = '{}'", text),
StringType::MultilineLiteral => format!("a = '''{}'''", text),
};
for root in parse(&expr)
.into_syntax()
.clone_for_update()
.first_child()
Expand All @@ -13,7 +22,11 @@ pub fn make_string_node(text: &str) -> SyntaxElement {
{
if root.kind() == VALUE {
for entries in root.as_node().unwrap().children_with_tokens() {
if entries.kind() == STRING {
if (target_type == StringType::String && entries.kind() == STRING)
|| (target_type == StringType::Multiline && entries.kind() == MULTI_LINE_STRING)
|| (target_type == StringType::Literal && entries.kind() == STRING_LITERAL)
|| (target_type == StringType::MultilineLiteral && entries.kind() == MULTI_LINE_STRING_LITERAL)
{
return entries;
}
}
Expand Down
36 changes: 33 additions & 3 deletions common/src/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,24 @@ pub fn load_text(value: &str, kind: SyntaxKind) -> String {
res
}

pub fn update_content<F>(entry: &SyntaxNode, transform: F)
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum StringUpdateMode {
/// Preserve the string type.
PreserveType,

/// Convert to a simple string (non-literal, non-multiline).
ConvertToString,
}

#[derive(Debug, Clone, Copy, PartialEq)]
pub enum StringType {
String,
Literal,
Multiline,
MultilineLiteral,
}

pub fn update_content<F>(entry: &SyntaxNode, transform: F, mode: StringUpdateMode)
where
F: Fn(&str) -> String,
{
Expand All @@ -38,9 +55,22 @@ where
let found_str_value = load_text(child.as_token().unwrap().text(), kind);
let output = transform(found_str_value.as_str());

changed = output != found_str_value || kind != STRING;
changed = output != found_str_value || (mode == StringUpdateMode::ConvertToString && kind != STRING);
if changed {
child = make_string_node(output.as_str());
let literal = [STRING_LITERAL, MULTI_LINE_STRING_LITERAL].contains(&kind);
let multiline = [MULTI_LINE_STRING, MULTI_LINE_STRING_LITERAL].contains(&kind);

let target_type = match mode {
StringUpdateMode::ConvertToString => StringType::String,
StringUpdateMode::PreserveType => match (literal, multiline) {
(false, false) => StringType::String,
(true, false) => StringType::Literal,
(false, true) => StringType::Multiline,
(true, true) => StringType::MultilineLiteral,
},
};

child = make_string_node(output.as_str(), target_type);
}
}
to_insert.push(child);
Expand Down
2 changes: 1 addition & 1 deletion common/src/tests/array_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ fn test_normalize_requirement(#[case] start: &str, #[case] expected: &str, #[cas
}
}
let res = format_syntax(root_ast, Options::default());
assert_eq!(expected, res);
similar_asserts::assert_eq!(expected: expected, actual: res);
}

#[rstest]
Expand Down
6 changes: 3 additions & 3 deletions common/src/tests/pep508_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::pep508::{format_requirement, get_canonic_requirement_name};
#[case::lowercase("A", "a")]
#[case::replace_dot_with_dash("a.b", "a-b")]
fn test_get_canonic_requirement_name(#[case] start: &str, #[case] expected: &str) {
assert_eq!(get_canonic_requirement_name(start), expected);
assert_eq!(expected, get_canonic_requirement_name(start));
}
#[rstest]
#[case::strip_version(
Expand All @@ -27,7 +27,7 @@ true
)]
fn test_format_requirement(#[case] start: &str, #[case] expected: &str, #[case] keep_full_version: bool) {
let got = format_requirement(start, keep_full_version);
assert_eq!(got, expected);
similar_asserts::assert_eq!(expected: expected, actual: got);
// formatting remains stable
assert_eq!(format_requirement(got.as_str(), keep_full_version), expected);
similar_asserts::assert_eq!(expected: expected, actual: format_requirement(got.as_str(), keep_full_version));
}
5 changes: 3 additions & 2 deletions pyproject-fmt/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,6 @@ extension-module = ["pyo3/extension-module"]
default = ["extension-module"]

[dev-dependencies]
rstest = { version = "0.25.0" } # parametrized tests
indoc = { version = "2.0.6" } # dedented test cases for literal strings
rstest = { version = "0.25.0" } # parametrized tests
indoc = { version = "2.0.6" } # dedented test cases for literal strings
similar-asserts = { version = "1.7.0" } # diff-based assertions
4 changes: 2 additions & 2 deletions pyproject-fmt/rust/src/data/ruff-order.expected.toml
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ lint.allowed-confusables = [
"∗",
"ρ",
]
lint.dummy-variable-rgx = "^_$"
lint.dummy-variable-rgx = '^_$'
lint.external = [
"ALPHA",
"Bar",
Expand Down Expand Up @@ -146,7 +146,7 @@ lint.flake8-builtins.builtins-ignorelist = [
]
lint.flake8-comprehensions.allow-dict-calls-with-keyword-arguments = true
lint.flake8-copyright.author = "Ruff"
lint.flake8-copyright.notice-rgx = "(?i)Copyright \\(C\\) \\d{4}"
lint.flake8-copyright.notice-rgx = '(?i)Copyright \\(C\\) \\d{4}'
lint.flake8-errmsg.max-string-length = 20
lint.flake8-gettext.extend-function-names = [
"ALPHA",
Expand Down
4 changes: 2 additions & 2 deletions pyproject-fmt/rust/src/dependency_groups.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use common::array::{sort, transform};
use common::pep508::{format_requirement, get_canonic_requirement_name};
use common::string::{load_text, update_content};
use common::string::{load_text, update_content, StringUpdateMode};
use common::table::{collapse_sub_tables, find_key, for_entries, reorder_table_keys, Tables};
use common::taplo::syntax::SyntaxKind::{ARRAY, ENTRY, INLINE_TABLE, STRING, VALUE};
use common::util::iter;
Expand All @@ -21,7 +21,7 @@ pub fn fix(tables: &mut Tables, keep_full_version: bool) {

// update inline table values to double-quoted string, e.g. include-group
iter(entry, [ARRAY, VALUE, INLINE_TABLE, ENTRY, VALUE].as_ref(), &|node| {
update_content(node, |s| String::from(s));
update_content(node, |s| String::from(s), StringUpdateMode::ConvertToString);
});

// sort array elements
Expand Down
Loading