Skip to content

Commit

Permalink
Merge pull request #33 from jonathangreen/feature/sort-everything
Browse files Browse the repository at this point in the history
Add the ability to sort inline tables and inline arrays
  • Loading branch information
pappasam committed Dec 31, 2022
2 parents d0847b7 + 38d9320 commit 8be828d
Show file tree
Hide file tree
Showing 13 changed files with 596 additions and 87 deletions.
56 changes: 41 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,37 +33,57 @@ This project can be used as either a command line utility or a Python library. R

```console
$ toml-sort --help
usage: toml-sort [-h] [--version] [-o OUTPUT] [-a] [-i]
[--no-comments] [--no-header-comments] [--no-footer-comments]
[--no-inline-comments] [--no-block-comments]
[--spaces-before-inline-comment {1,2,3,4}] [--check] [-I]
[F ...]
usage: toml-sort [-h] [--version] [-o OUTPUT] [-i] [-I] [-a] [--no-sort-tables] [--sort-table-keys]
[--sort-inline-tables] [--sort-inline-arrays] [--no-header] [--no-comments] [--no-header-comments]
[--no-footer-comments] [--no-inline-comments] [--no-block-comments]
[--spaces-before-inline-comment {1,2,3,4}] [--spaces-indent-inline-array {2,4,6,8}]
[--trailing-comma-inline-array] [--check]
[F [F ...]]

Toml sort: a sorting utility for toml files.

positional arguments:
F filename(s) to be processed by toml-sort (default: -)

options:
optional arguments:
-h, --help show this help message and exit
--version display version information and exit
-o OUTPUT, --output OUTPUT
output filepath (default: '-')
-a, --all sort ALL keys (default: only sort non-inline 'tables
and arrays of tables')
-i, --in-place overwrite the original input file with changes
--no-comments remove all comments. Implies no header, footer,
inline, or block comments
--check silently check if an original file would be changed by the formatter

sort:
change sorting behavior

-I, --ignore-case ignore case when sorting
-a, --all sort ALL keys. This implies sort table-keys, inline-tables and inline arrays. (default: only
sort non-inline 'tables and arrays of tables')
--no-sort-tables Disables the default behavior of sorting tables and arrays of tables by their header value.
Setting this option will keep the order of tables in the toml file the same.
--sort-table-keys Sort the keys in tables and arrays of tables (excluding inline tables and arrays).
--sort-inline-tables Sort inline tables.
--sort-inline-arrays Sort inline arrays.

comments:
exclude comments from output

--no-header Deprecated. See --no-header-comments
--no-comments remove all comments. Implies no header, footer, inline, or block comments
--no-header-comments remove a document's leading comments
--no-footer-comments remove a document's trailing comments
--no-inline-comments remove a document's inline comments
--no-block-comments remove a document's block comments

formatting:
options to change output formatting

--spaces-before-inline-comment {1,2,3,4}
the number of spaces before an inline comment
(default: 1)
--check silently check if an original file would be changed by
the formatter
-I, --ignore-case ignore case when sorting
the number of spaces before an inline comment (default: 1)
--spaces-indent-inline-array {2,4,6,8}
the number of spaces to indent a multiline inline array (default: 2)
--trailing-comma-inline-array
add trailing comma to the last item in a multiline inline array

Examples:

Expand Down Expand Up @@ -99,7 +119,13 @@ no_header_comments = true
no_footer_comments = true
no_inline_comments = true
no_block_comments = true
no_sort_tables = true
sort_table_keys = true
sort_inline_tables = true
sort_inline_arrays = true
spaces_before_inline_comment = 2
spaces_indent_inline_array = 4
trailing_comma_inline_array = true
check = true
ignore_case = true
```
Expand Down
45 changes: 45 additions & 0 deletions tests/examples/inline.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
[b]
blist=[ 'c', 'a', 'b'] # Comment
alist = [
'g',



# Comment attached
'w', # Comment inline

# Orphan comment

#Multiline
# Second attached comment
'a'

]
'aalist' = { c="test", a= "another test" }#list comment
# Multilevel inline array
aaalist = [
[
{ name="Homer Simpson", town ="Springfield"}
],
[
{ name="Bart Simpson", town ="Springfield"}
],
# Weird block comment
[
# Another weird block comment
{name="Lisa Simpson", town ="Springfield", a-test = "Did this sort?"}#Should get sorted
],
]
[a]
test = [
{x = [
"foo",
"bar"
], a = [
""
]},
{y = [
"foo",
"baz"
]}
]
4 changes: 2 additions & 2 deletions tests/examples/sorted/comment-comments-preserved.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ name = "Samuel Roeca"
[[a-section.hello]]
dob = 1979-05-27T07:32:00Z # First class dates? Why not?
# test comment
ports = [ 8001, 8001, 8002 ]
ports = [8001, 8001, 8002]

# Another test comment
# make sure multiline comments work
[[a-section.hello]] # Comment here?
# multi line comment
# test comment
dob = 1920-05-27T07:32:00Z # Another date!
ports = [ 80 ]
ports = [80]

# Another comment
[b-section] # Comment there?
Expand Down
4 changes: 2 additions & 2 deletions tests/examples/sorted/comment-header-footer.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ name = "Samuel Roeca"
# start test
[[a-section.hello]]
# test comment
ports = [ 8001, 8001, 8002 ]
ports = [8001, 8001, 8002]
dob = 1979-05-27T07:32:00Z # First class dates? Why not?

# Another test comment
# make sure multiline comments work
[[a-section.hello]] # Comment here?
ports = [ 80 ]
ports = [80]
# multi line comment
# test comment
dob = 1920-05-27T07:32:00Z # Another date!
Expand Down
4 changes: 2 additions & 2 deletions tests/examples/sorted/comment-no-comments.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ name = "Samuel Roeca"

[[a-section.hello]]
dob = 1979-05-27T07:32:00Z
ports = [ 8001, 8001, 8002 ]
ports = [8001, 8001, 8002]

[[a-section.hello]]
dob = 1920-05-27T07:32:00Z
ports = [ 80 ]
ports = [80]

[b-section]
date = "2018"
Expand Down
4 changes: 2 additions & 2 deletions tests/examples/sorted/from-toml-lang.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
title = "TOML Example"

[clients]
data = [ ["gamma", "delta"], [1, 2] ] # just an update to make sure parsers support it
data = [["gamma", "delta"], [1, 2]] # just an update to make sure parsers support it
# Line breaks are OK when inside arrays
hosts = [
"alpha",
Expand All @@ -12,7 +12,7 @@ hosts = [

[database]
server = "192.168.1.1"
ports = [ 8001, 8001, 8002 ]
ports = [8001, 8001, 8002]
connection_max = 5000
enabled = true # Comment after a boolean

Expand Down
39 changes: 39 additions & 0 deletions tests/examples/sorted/inline-default.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
[a]
test = [
{x = [
"foo",
"bar"
], a = [
""
]},
{y = [
"foo",
"baz"
]}
]

[b]
blist = ['c', 'a', 'b'] # Comment
alist = [
'g',
# Comment attached
'w', # Comment inline
# Multiline
# Second attached comment
'a'
]
'aalist' = {c = "test", a = "another test"} # list comment
# Multilevel inline array
aaalist = [
[
{name = "Homer Simpson", town = "Springfield"}
],
[
{name = "Bart Simpson", town = "Springfield"}
],
# Weird block comment
[
# Another weird block comment
{name = "Lisa Simpson", town = "Springfield", a-test = "Did this sort?"} # Should get sorted
]
]
33 changes: 33 additions & 0 deletions tests/examples/sorted/inline-no-comments-no-table-sort.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
[b]
aaalist = [
[
{a-test = "Did this sort?", name = "Lisa Simpson", town = "Springfield"},
],
[
{name = "Bart Simpson", town = "Springfield"},
],
[
{name = "Homer Simpson", town = "Springfield"},
],
]
'aalist' = {a = "another test", c = "test"}
alist = [
'a',
'g',
'w',
]
blist = ['a', 'b', 'c']

[a]
test = [
{a = [
"",
], x = [
"bar",
"foo",
]},
{y = [
"baz",
"foo",
]},
]
39 changes: 39 additions & 0 deletions tests/examples/sorted/inline.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
[a]
test = [
{a = [
""
], x = [
"bar",
"foo"
]},
{y = [
"baz",
"foo"
]}
]

[b]
# Multilevel inline array
aaalist = [
# Weird block comment
[
# Another weird block comment
{a-test = "Did this sort?", name = "Lisa Simpson", town = "Springfield"} # Should get sorted
],
[
{name = "Bart Simpson", town = "Springfield"}
],
[
{name = "Homer Simpson", town = "Springfield"}
]
]
'aalist' = {a = "another test", c = "test"} # list comment
alist = [
# Multiline
# Second attached comment
'a',
'g',
# Comment attached
'w' # Comment inline
]
blist = ['a', 'b', 'c'] # Comment
33 changes: 33 additions & 0 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ def capture(
pytest.param("weird", "sorted/weird", marks=[pytest.mark.xfail]),
("pyproject-weird-order", "sorted/pyproject-weird-order"),
("comment", "sorted/comment-header-footer"),
("inline", "sorted/inline-default"),
],
)
def test_cli_defaults(
Expand Down Expand Up @@ -113,6 +114,38 @@ def test_cli_defaults(
"--all",
],
),
(
"inline",
["sorted", "inline"],
[
"--all",
"--spaces-before-inline-comment",
"2",
],
),
(
"inline",
["sorted", "inline"],
[
"--sort-table-keys",
"--sort-inline-array",
"--sort-inline-table",
"--spaces-before-inline-comment",
"2",
],
),
(
"inline",
["sorted", "inline-no-comments-no-table-sort"],
[
"--no-sort-tables",
"--sort-table-keys",
"--sort-inline-array",
"--sort-inline-table",
"--no-comments",
"--trailing-comma-inline-array",
],
),
],
)
def test_cli_args(
Expand Down

0 comments on commit 8be828d

Please sign in to comment.