Skip to content
Merged
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
3 changes: 1 addition & 2 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "ITensorFormatter"
uuid = "b6bf39f1-c9d3-4bad-aad8-593d802f65fd"
version = "0.2.22"
version = "0.2.23"
authors = ["ITensor developers <support@itensor.org> and contributors"]

[workspace]
Expand Down Expand Up @@ -29,7 +29,6 @@ TOML = "1.0.3"
YAML = "0.4.16"
julia = "1.10"


[apps.itfmt]
julia_flags = ["--optimize=0"]

Expand Down
8 changes: 4 additions & 4 deletions docs/Project.toml
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
[deps]
Documenter = "e30172f5-a6a5-5a46-863b-614d45cd2de4"
Literate = "98b081ad-f1c9-55d3-8b20-4c87d4299306"
ITensorFormatter = "b6bf39f1-c9d3-4bad-aad8-593d802f65fd"
Literate = "98b081ad-f1c9-55d3-8b20-4c87d4299306"

[sources]
ITensorFormatter = {path = ".."}
[sources.ITensorFormatter]
path = ".."

[compat]
Documenter = "1"
Literate = "2"
ITensorFormatter = "0.2"
Literate = "2"
4 changes: 2 additions & 2 deletions examples/Project.toml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
[deps]
ITensorFormatter = "b6bf39f1-c9d3-4bad-aad8-593d802f65fd"

[sources]
ITensorFormatter = {path = ".."}
[sources.ITensorFormatter]
path = ".."

[compat]
ITensorFormatter = "0.2"
15 changes: 10 additions & 5 deletions src/format_project_toml.jl
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@ end
function format_project_toml!(path::AbstractString)
@assert isfile(path) "Expected a file path, got: $path"
isprojecttoml(path) || return nothing
# This calls `strip_compat_trailing_zeros!(path)` internally.
strip_compat_trailing_zeros!(path)
# Always canonicalize ordering/formatting, even if compat did not change.
sort_project_toml!(path)
return nothing
end

Expand Down Expand Up @@ -53,6 +54,9 @@ function sort_project_toml!(path::AbstractString)
for k in scalar_keys
TOML.print(io, Dict(k => data[k]))
end
sections = String[]
scalar_out = strip(String(take!(io)), '\n')
!isempty(scalar_out) && push!(sections, scalar_out)
table_keys = String[]
seen = Set{String}()
for k in table_order
Expand All @@ -65,10 +69,12 @@ function sort_project_toml!(path::AbstractString)
is_table(data[k]) && !(k in seen) && push!(table_keys, k)
end
for k in table_keys
println(io)
TOML.print(io, Dict(k => data[k]); sorted = true)
table_io = IOBuffer()
TOML.print(table_io, Dict(k => data[k]); sorted = true)
table_out = strip(String(take!(table_io)), '\n')
!isempty(table_out) && push!(sections, table_out)
end
out = String(take!(io))
out = join(sections, "\n\n")
endswith(out, "\n") || (out *= "\n")
out == raw && return false
write(path, out)
Expand Down Expand Up @@ -103,6 +109,5 @@ function strip_compat_trailing_zeros!(path::AbstractString)
open(path, "w") do io
return TOML.print(io, data)
end
sort_project_toml!(path)
return true
end
16 changes: 16 additions & 0 deletions test/test_formatters.jl
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,20 @@ end
@test occursin("Julia = \"1.10\"", result)
@test occursin("Foo = \"1.2, 2\"", result)
end

mktempdir() do dir
path = joinpath(dir, "Project.toml")
write(
path,
"""
[deps]
Zebra = "00000000-0000-0000-0000-000000000001"
Alpha = "00000000-0000-0000-0000-000000000002"
"""
)
format_project_tomls!(path)
result = read(path, String)
@test !startswith(result, "\n")
@test findfirst("Alpha", result) < findfirst("Zebra", result)
end
end
Loading