diff --git a/Project.toml b/Project.toml index 3e2c348..16c68a4 100644 --- a/Project.toml +++ b/Project.toml @@ -1,6 +1,6 @@ name = "ITensorFormatter" uuid = "b6bf39f1-c9d3-4bad-aad8-593d802f65fd" -version = "0.2.22" +version = "0.2.23" authors = ["ITensor developers and contributors"] [workspace] @@ -29,7 +29,6 @@ TOML = "1.0.3" YAML = "0.4.16" julia = "1.10" - [apps.itfmt] julia_flags = ["--optimize=0"] diff --git a/docs/Project.toml b/docs/Project.toml index d7aa3bb..db0f858 100644 --- a/docs/Project.toml +++ b/docs/Project.toml @@ -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" diff --git a/examples/Project.toml b/examples/Project.toml index 1466b07..897cf16 100644 --- a/examples/Project.toml +++ b/examples/Project.toml @@ -1,8 +1,8 @@ [deps] ITensorFormatter = "b6bf39f1-c9d3-4bad-aad8-593d802f65fd" -[sources] -ITensorFormatter = {path = ".."} +[sources.ITensorFormatter] +path = ".." [compat] ITensorFormatter = "0.2" diff --git a/src/format_project_toml.jl b/src/format_project_toml.jl index 9003893..906ba29 100644 --- a/src/format_project_toml.jl +++ b/src/format_project_toml.jl @@ -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 @@ -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 @@ -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) @@ -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 diff --git a/test/test_formatters.jl b/test/test_formatters.jl index 1f7a7f6..a969e32 100644 --- a/test/test_formatters.jl +++ b/test/test_formatters.jl @@ -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