Skip to content
Draft
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
4 changes: 4 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"python.testing.pytestArgs": [],
"python.testing.pytestEnabled": true
}
23 changes: 23 additions & 0 deletions Cargo.lock

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

51 changes: 49 additions & 2 deletions common/src/table.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
use std::cell::{RefCell, RefMut};
use std::collections::HashMap;
use std::collections::{HashMap, BTreeSet};
use std::iter::zip;
use std::ops::Index;

use taplo::syntax::SyntaxKind::{ENTRY, IDENT, KEY, NEWLINE, TABLE_ARRAY_HEADER, TABLE_HEADER, VALUE};
use taplo::syntax::{SyntaxElement, SyntaxNode};
use taplo::HashSet;
use taplo::{dom::node::DomNode as _, parser::parse};

use crate::create::{make_empty_newline, make_key, make_newline, make_table_entry};
use crate::string::load_text;
Expand Down Expand Up @@ -279,7 +280,7 @@ pub fn find_key(table: &SyntaxNode, key: &str) -> Option<SyntaxNode> {
None
}

pub fn collapse_sub_tables(tables: &mut Tables, name: &str) {
pub fn collapse_sub_tables(tables: &mut Tables, name: &str, exclude: &[Vec<String>]) {
let h2p = tables.header_to_pos.clone();
let sub_name_prefix = format!("{name}.");
let sub_table_keys: Vec<&String> = h2p.keys().filter(|s| s.starts_with(sub_name_prefix.as_str())).collect();
Expand All @@ -296,6 +297,14 @@ pub fn collapse_sub_tables(tables: &mut Tables, name: &str) {
if main_positions.len() != 1 {
return;
}

// remove `name` from `exclude`s (and skip if `name` is not a prefix)
let prefix = parse_ident(name).expect("could not parse prefix");
let exclude: BTreeSet<_> = exclude.into_iter().filter_map(|id|
id.strip_prefix(prefix.as_slice())
).collect();
dbg!(&exclude);

let mut main = tables.table_set[*main_positions.first().unwrap()].borrow_mut();
for key in sub_table_keys {
let sub_positions = tables.header_to_pos[key].clone();
Expand All @@ -304,6 +313,10 @@ pub fn collapse_sub_tables(tables: &mut Tables, name: &str) {
}
let mut sub = tables.table_set[*sub_positions.first().unwrap()].borrow_mut();
let sub_name = key.strip_prefix(sub_name_prefix.as_str()).unwrap();
let sub_path = parse_ident(sub_name).unwrap();
if exclude.contains(sub_path.as_slice()) {
continue;
}
let mut header = false;
for child in sub.iter() {
let kind = child.kind();
Expand Down Expand Up @@ -340,3 +353,37 @@ pub fn collapse_sub_tables(tables: &mut Tables, name: &str) {
sub.clear();
}
}

pub fn parse_ident(ident: &str) -> Result<Vec<String>, String> {
let parsed = parse(&format!("{ident} = 1"));
if let Some(e) = parsed.errors.first() {
return Err(format!("syntax error: {e}"));
}

let root = parsed.into_dom();
let errors = root.errors();
if let Some(e) = errors.get().first() {
return Err(format!("semantic error: {e}"));
}

// We cannot use `.into_syntax()` since only the DOM transformation
// allows accessing ident `.value()`s without quotes.
let mut node = root;
let mut parts = vec![];
while let Ok(table) = node.try_into_table() {
let entries = table.entries().get();
if entries.len() != 1 {
return Err("expected exactly one entry".to_string());
}
let mut it = entries.iter();
let (key, next_node) = it.next().unwrap(); // checked if len == 1 above

parts.push(key.value().to_string());
node = next_node.clone();
}
Ok(parts)
}

fn prefixes<T>(slice: &[T]) -> impl Iterator<Item = &[T]> + DoubleEndedIterator {
(0..=slice.len()).map(|len| &slice[..len])
}
1 change: 1 addition & 0 deletions pyproject-fmt/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,4 @@ default = ["extension-module"]
[dev-dependencies]
rstest = "0.26.1" # parametrized tests
indoc = { version = "2.0.6" } # dedented test cases for literal strings
pretty_assertions = { version = "1.4.1", features = ["alloc"] }
2 changes: 1 addition & 1 deletion pyproject-fmt/rust/src/dependency_groups.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use lexical_sort::natural_lexical_cmp;
use std::cmp::Ordering;

pub fn fix(tables: &mut Tables, keep_full_version: bool) {
collapse_sub_tables(tables, "dependency-groups");
collapse_sub_tables(tables, "dependency-groups", &[]);
let table_element = tables.get("dependency-groups");
if table_element.is_none() {
return;
Expand Down
21 changes: 19 additions & 2 deletions pyproject-fmt/rust/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ use std::string::String;

use common::taplo::formatter::{format_syntax, Options};
use common::taplo::parser::parse;
use pyo3::exceptions::PyValueError;
use pyo3::prelude::{PyModule, PyModuleMethods};
use pyo3::types::PyTuple;
use pyo3::{pyclass, pyfunction, pymethods, pymodule, wrap_pyfunction, Bound, PyResult};

use crate::global::reorder_tables;
Expand All @@ -25,19 +27,21 @@ pub struct Settings {
max_supported_python: (u8, u8),
min_supported_python: (u8, u8),
generate_python_version_classifiers: bool,
do_not_collapse: Vec<Vec<String>>,
}

#[pymethods]
impl Settings {
#[new]
#[pyo3(signature = (*, column_width, indent, keep_full_version, max_supported_python, min_supported_python, generate_python_version_classifiers ))]
#[pyo3(signature = (*, column_width, indent, keep_full_version, max_supported_python, min_supported_python, generate_python_version_classifiers, do_not_collapse))]
const fn new(
column_width: usize,
indent: usize,
keep_full_version: bool,
max_supported_python: (u8, u8),
min_supported_python: (u8, u8),
generate_python_version_classifiers: bool,
do_not_collapse: Vec<Vec<String>>,
) -> Self {
Self {
column_width,
Expand All @@ -46,6 +50,7 @@ impl Settings {
max_supported_python,
min_supported_python,
generate_python_version_classifiers,
do_not_collapse,
}
}
}
Expand All @@ -64,9 +69,10 @@ pub fn format_toml(content: &str, opt: &Settings) -> String {
opt.max_supported_python,
opt.min_supported_python,
opt.generate_python_version_classifiers,
opt.do_not_collapse.as_slice(),
);
dependency_groups::fix(&mut tables, opt.keep_full_version);
ruff::fix(&mut tables);
ruff::fix(&mut tables, opt.do_not_collapse.as_slice());
reorder_tables(&root_ast, &tables);

let options = Options {
Expand Down Expand Up @@ -94,13 +100,24 @@ pub fn format_toml(content: &str, opt: &Settings) -> String {
format_syntax(root_ast, options)
}

/// Parse a nested toml identifier into a tuple of idents
///
/// >>> parse_ident('a."b.c"')
/// ('a', 'b.c')
#[pyfunction]
pub fn parse_ident<'py>(py: pyo3::Python<'py>, ident: &str) -> PyResult<Bound<'py, PyTuple>> {
let parts = common::table::parse_ident(ident).map_err(|e| PyValueError::new_err(e))?;
PyTuple::new(py, parts)
}

/// # Errors
///
/// Will return `PyErr` if an error is raised during formatting.
#[pymodule]
#[pyo3(name = "_lib")]
pub fn _lib(m: &Bound<'_, PyModule>) -> PyResult<()> {
m.add_function(wrap_pyfunction!(format_toml, m)?)?;
m.add_function(wrap_pyfunction!(parse_ident, m)?)?;
m.add_class::<Settings>()?;
Ok(())
}
3 changes: 2 additions & 1 deletion pyproject-fmt/rust/src/project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@ pub fn fix(
max_supported_python: (u8, u8),
min_supported_python: (u8, u8),
generate_python_version_classifiers: bool,
do_not_collapse: &[Vec<String>],
) {
collapse_sub_tables(tables, "project");
collapse_sub_tables(tables, "project", do_not_collapse);
let table_element = tables.get("project");
if table_element.is_none() {
return;
Expand Down
4 changes: 2 additions & 2 deletions pyproject-fmt/rust/src/ruff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ use common::table::{collapse_sub_tables, for_entries, reorder_table_keys, Tables
use lexical_sort::natural_lexical_cmp;

#[allow(clippy::too_many_lines)]
pub fn fix(tables: &mut Tables) {
collapse_sub_tables(tables, "tool.ruff");
pub fn fix(tables: &mut Tables, do_not_collapse: &[Vec<String>]) {
collapse_sub_tables(tables, "tool.ruff", do_not_collapse);
let table_element = tables.get("tool.ruff");
if table_element.is_none() {
return;
Expand Down
3 changes: 3 additions & 0 deletions pyproject-fmt/rust/src/tests/main_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@ fn test_format_toml(
max_supported_python,
min_supported_python: (3, 9),
generate_python_version_classifiers: true,
do_not_collapse: vec![],
};
let got = format_toml(start, &settings);
assert_eq!(got, expected);
Expand All @@ -216,6 +217,7 @@ fn test_issue_24(data: PathBuf) {
max_supported_python: (3, 9),
min_supported_python: (3, 9),
generate_python_version_classifiers: true,
do_not_collapse: vec![],
};
let got = format_toml(start.as_str(), &settings);
let expected = read_to_string(data.join("ruff-order.expected.toml")).unwrap();
Expand Down Expand Up @@ -246,6 +248,7 @@ fn test_column_width() {
max_supported_python: (3, 13),
min_supported_python: (3, 13),
generate_python_version_classifiers: true,
do_not_collapse: vec![],
};
let got = format_toml(start, &settings);
let expected = indoc! {r#"
Expand Down
Loading