From 2e1ded844280f7e491cbd1af1783204157b87862 Mon Sep 17 00:00:00 2001 From: mtfishman Date: Thu, 26 Feb 2026 22:04:52 -0500 Subject: [PATCH 1/2] Improve Project.toml formatting --- Project.toml | 2 +- src/format_project_toml.jl | 10 ++++++---- test/test_formatters.jl | 16 ++++++++++++++++ 3 files changed, 23 insertions(+), 5 deletions(-) diff --git a/Project.toml b/Project.toml index 3e2c348..fcae58f 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] diff --git a/src/format_project_toml.jl b/src/format_project_toml.jl index 9003893..fbe125f 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 @@ -64,8 +65,10 @@ function sort_project_toml!(path::AbstractString) for k in sort(collect(keys(data))) is_table(data[k]) && !(k in seen) && push!(table_keys, k) end - for k in table_keys - println(io) + for (i, k) in pairs(table_keys) + if i > 1 || !isempty(scalar_keys) + println(io) + end TOML.print(io, Dict(k => data[k]); sorted = true) end out = String(take!(io)) @@ -103,6 +106,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 From 4169725ca1b18c48f1780472532b17ca7b02aef0 Mon Sep 17 00:00:00 2001 From: mtfishman Date: Thu, 26 Feb 2026 22:40:34 -0500 Subject: [PATCH 2/2] Fix blank line formatting in Project.toml --- Project.toml | 1 - docs/Project.toml | 8 ++++---- examples/Project.toml | 4 ++-- src/format_project_toml.jl | 15 +++++++++------ 4 files changed, 15 insertions(+), 13 deletions(-) diff --git a/Project.toml b/Project.toml index fcae58f..16c68a4 100644 --- a/Project.toml +++ b/Project.toml @@ -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 fbe125f..906ba29 100644 --- a/src/format_project_toml.jl +++ b/src/format_project_toml.jl @@ -54,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,13 +68,13 @@ function sort_project_toml!(path::AbstractString) for k in sort(collect(keys(data))) is_table(data[k]) && !(k in seen) && push!(table_keys, k) end - for (i, k) in pairs(table_keys) - if i > 1 || !isempty(scalar_keys) - println(io) - end - TOML.print(io, Dict(k => data[k]); sorted = true) + for k in table_keys + 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)