-
-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Bernát Gábor <[email protected]>
- Loading branch information
1 parent
a6bf4ad
commit 0f1a966
Showing
7 changed files
with
128 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
from __future__ import annotations | ||
|
||
from dataclasses import dataclass, field | ||
from typing import Sequence | ||
|
||
from tomlkit.items import Array, Comment, Item, Key, String, Trivia, Whitespace | ||
|
||
|
||
def order_keys(body: list[tuple[Key | None, Item]], order: Sequence[str]) -> None: | ||
entries = {i.key: (i, v) for (i, v) in body if isinstance(i, Key)} | ||
body.clear() | ||
for key in order: | ||
if key in entries: | ||
body.append(entries[key]) | ||
# body.append((None, Whitespace("\n"))) | ||
del entries[key] | ||
body.extend(entries.values()) # append the rest | ||
body.append((None, Whitespace("\n"))) # add trailing newline to separate | ||
|
||
|
||
@dataclass | ||
class _ArrayEntries: | ||
text: String | ||
comments: list[Comment] = field(default_factory=list) | ||
|
||
|
||
def multiline_sorted_array(array: Array, indent: int) -> None: | ||
body = array._value | ||
|
||
entries: list[_ArrayEntries] = [] | ||
start: list[Comment] = [] | ||
for entry in body: | ||
if isinstance(entry, String): | ||
entries.append(_ArrayEntries(entry)) | ||
elif isinstance(entry, Comment): | ||
(entries[-1].comments if len(entries) else start).append(entry) | ||
|
||
body.clear() | ||
indent_text = " " * indent | ||
for entry in start: | ||
body.append(Whitespace(f"\n{indent_text}")) | ||
entry.indent(0) | ||
body.append(entry) | ||
for entry in sorted(entries, key=lambda e: str(e.text)): | ||
body.append(Whitespace(f"\n{indent_text}")) | ||
body.append(entry.text) | ||
body.append(Whitespace(",")) | ||
if entry.comments: | ||
com = " ".join(i.trivia.comment[1:].strip() for i in entry.comments) | ||
body.append(Comment(Trivia(comment=f" # {com}", trail=""))) | ||
body.append(Whitespace("\n")) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
from pathlib import Path | ||
from textwrap import dedent | ||
|
||
from pyproject_fmt.cli import PyProjectFmtNamespace | ||
from pyproject_fmt.formatter import format_pyproject | ||
|
||
|
||
def test_build_backend(tmp_path: Path) -> None: | ||
txt = """ | ||
[build-system] | ||
backend-path = ['A', 'B'] | ||
requires = [ | ||
# start | ||
# two | ||
"A", | ||
"B", # c # d follow-up comment | ||
"C", # magic | ||
"D", # a | ||
"E", # b | ||
"F", # comment ok | ||
] | ||
build-backend = "hatchling.build" | ||
""" | ||
toml = tmp_path / "a.toml" | ||
toml.write_text(dedent(txt)) | ||
result = format_pyproject(PyProjectFmtNamespace(pyproject_toml=toml)) | ||
expected = """ | ||
[build-system] | ||
build-backend = "hatchling.build" | ||
requires = [ | ||
# start | ||
# two | ||
"A", | ||
"B", # c # d follow-up comment | ||
"C", # magic | ||
"D", # a | ||
"E", # b | ||
"F", # comment ok | ||
] | ||
backend-path = [ | ||
'A', | ||
'B', | ||
] | ||
""" | ||
|
||
assert result == dedent(expected) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters