Repository: fredrikekre/Runic.jl (julia)

Issue:
Failed to format input file.
I ran into an issue formatting a file in https://github.com/QuantumKitHub/MPSKit.jl/pull/303/
I managed to make the following minimal example, with stacktrace:

```
ERROR: failed to format input from runic_file.jl: Runic.AssertionError: re-parsing the formatted output failed. This is unexpected, please file an issue with a reproducible example at https://github.com/fredrikekre/Runic.jl/issues/new.
Stacktrace:
 [1] format_tree!(ctx::Runic.Context)
   @ Runic ~/.julia/packages/Runic/iGXsf/src/Runic.jl:618
 [2] main(argv::Vector{String})
   @ Runic ~/.julia/packages/Runic/iGXsf/src/main.jl:415
 [3] top-level scope
   @ none:1
 [4] eval
   @ ./boot.jl:430 [inlined]
 [5] exec_options(opts::Base.JLOptions)
   @ Base ./client.jl:296
```

```julia
function kitaev_model()
    return Iterators.flatten((((i, i + 1) => TB + SC for i in 1:(L - 1)),
                              (i,) => CP for i in 1:L))
end
```

Note that the following does work:

```julia
function kitaev_model()
    return Iterators.flatten((((i, i + 1) => TB + SC for i in 1:(L - 1)), (i,) => CP for i in 1:L))
end
```

Pull request description:
This fix another edgecase where adding a trailing comma after the last item (a generator) in a list results in invalid code. Fixes #151.

Patch:
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 5131a3f..0e4df16 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -5,6 +5,11 @@ All notable changes to this project will be documented in this file.
 The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
 and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
 
+## [v1.4.4] - 2025-07-24
+### Fixed
+ - Fix another edgecase where adding a trailing comma after the last item in a list results
+   in invalid code. ([#151], [#152]).
+
 ## [v1.4.3] - 2025-02-28
 ### Fixed
  - Fix formatting of floating point literals that use `−` (Unicode U+2212) instead of the
diff --git a/src/runestone.jl b/src/runestone.jl
index 5c84dd2..9373e1f 100644
--- a/src/runestone.jl
+++ b/src/runestone.jl
@@ -340,6 +340,9 @@ function spaces_in_listlike(ctx::Context, node::Node)
     allow_trailing_comma = multiline
     if kind(node) in KSet"call dotcall macrocall"
         require_trailing_comma = false
+    elseif n_items > 0 && kind(kids[last_item_idx::Int]) === K"generator"
+        # https://github.com/fredrikekre/Runic.jl/issues/151
+        require_trailing_comma = false
     elseif implicit_tuple
         # Trailing commas in implicit tuples in the LHS of an assignment, e.g. `x, = 1, 2`,
         # is required for single item tuples and allowed for multiple items (to allow e.g.


Test patch:
diff --git a/test/runtests.jl b/test/runtests.jl
index 3b4ccd7..6ae26e8 100644
--- a/test/runtests.jl
+++ b/test/runtests.jl
@@ -447,6 +447,8 @@ end
     # Edge case with comment and no items
     @test format_string("[# a\n]") == "[# a\n]"
     @test format_string("[ # a\n]") == "[ # a\n]"
+    # https://github.com/fredrikekre/Runic.jl/issues/151
+    @test format_string("(\n    a, b for b in B\n)") == "(\n    a, b for b in B\n)"
 end
 
 @testset "whitespace in let" begin