Skip to content

Commit 6d15d25

Browse files
authored
Merge branch 'main' into patch/workspace
2 parents 5aee7a4 + 04a5a76 commit 6d15d25

3 files changed

Lines changed: 26 additions & 6 deletions

File tree

Project.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ TOML = "1.0.3"
2929
YAML = "0.4.16"
3030
julia = "1.10"
3131

32-
3332
[apps.itfmt]
3433
julia_flags = ["--optimize=0"]
3534

src/format_project_toml.jl

Lines changed: 10 additions & 5 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

@@ -53,6 +54,9 @@ function sort_project_toml!(path::AbstractString)
5354
for k in scalar_keys
5455
TOML.print(io, Dict(k => data[k]))
5556
end
57+
sections = String[]
58+
scalar_out = strip(String(take!(io)), '\n')
59+
!isempty(scalar_out) && push!(sections, scalar_out)
5660
table_keys = String[]
5761
seen = Set{String}()
5862
for k in table_order
@@ -65,10 +69,12 @@ function sort_project_toml!(path::AbstractString)
6569
is_table(data[k]) && !(k in seen) && push!(table_keys, k)
6670
end
6771
for k in table_keys
68-
println(io)
69-
TOML.print(io, Dict(k => data[k]); sorted = true)
72+
table_io = IOBuffer()
73+
TOML.print(table_io, Dict(k => data[k]); sorted = true)
74+
table_out = strip(String(take!(table_io)), '\n')
75+
!isempty(table_out) && push!(sections, table_out)
7076
end
71-
out = String(take!(io))
77+
out = join(sections, "\n\n")
7278
endswith(out, "\n") || (out *= "\n")
7379
out == raw && return false
7480
write(path, out)
@@ -103,6 +109,5 @@ function strip_compat_trailing_zeros!(path::AbstractString)
103109
open(path, "w") do io
104110
return TOML.print(io, data)
105111
end
106-
sort_project_toml!(path)
107112
return true
108113
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)