Skip to content

Commit 2e1ded8

Browse files
committed
Improve Project.toml formatting
1 parent cc3c82a commit 2e1ded8

3 files changed

Lines changed: 23 additions & 5 deletions

File tree

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name = "ITensorFormatter"
22
uuid = "b6bf39f1-c9d3-4bad-aad8-593d802f65fd"
3-
version = "0.2.22"
3+
version = "0.2.23"
44
authors = ["ITensor developers <support@itensor.org> and contributors"]
55

66
[workspace]

src/format_project_toml.jl

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,9 @@ end
2222
function format_project_toml!(path::AbstractString)
2323
@assert isfile(path) "Expected a file path, got: $path"
2424
isprojecttoml(path) || return nothing
25-
# This calls `strip_compat_trailing_zeros!(path)` internally.
2625
strip_compat_trailing_zeros!(path)
26+
# Always canonicalize ordering/formatting, even if compat did not change.
27+
sort_project_toml!(path)
2728
return nothing
2829
end
2930

@@ -64,8 +65,10 @@ function sort_project_toml!(path::AbstractString)
6465
for k in sort(collect(keys(data)))
6566
is_table(data[k]) && !(k in seen) && push!(table_keys, k)
6667
end
67-
for k in table_keys
68-
println(io)
68+
for (i, k) in pairs(table_keys)
69+
if i > 1 || !isempty(scalar_keys)
70+
println(io)
71+
end
6972
TOML.print(io, Dict(k => data[k]); sorted = true)
7073
end
7174
out = String(take!(io))
@@ -103,6 +106,5 @@ function strip_compat_trailing_zeros!(path::AbstractString)
103106
open(path, "w") do io
104107
return TOML.print(io, data)
105108
end
106-
sort_project_toml!(path)
107109
return true
108110
end

test/test_formatters.jl

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,20 @@ end
3333
@test occursin("Julia = \"1.10\"", result)
3434
@test occursin("Foo = \"1.2, 2\"", result)
3535
end
36+
37+
mktempdir() do dir
38+
path = joinpath(dir, "Project.toml")
39+
write(
40+
path,
41+
"""
42+
[deps]
43+
Zebra = "00000000-0000-0000-0000-000000000001"
44+
Alpha = "00000000-0000-0000-0000-000000000002"
45+
"""
46+
)
47+
format_project_tomls!(path)
48+
result = read(path, String)
49+
@test !startswith(result, "\n")
50+
@test findfirst("Alpha", result) < findfirst("Zebra", result)
51+
end
3652
end

0 commit comments

Comments
 (0)