From 7d739c9558b738df648e9808a3a5a61f31275eff Mon Sep 17 00:00:00 2001 From: Fredrik Ekre Date: Thu, 20 Jun 2024 13:31:42 +0200 Subject: [PATCH] Fix a bug with single line comments inside a list --- src/runestone.jl | 7 +++---- test/runtests.jl | 5 +++++ 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/src/runestone.jl b/src/runestone.jl index e060a2d..c500131 100644 --- a/src/runestone.jl +++ b/src/runestone.jl @@ -435,6 +435,7 @@ function spaces_in_listlike(ctx::Context, node::Node) state = state_after_item(i) end elseif state === :expect_comma + trailing = i > last_item_idx if kind(kid′) === K"," || (kind(kid′) === K";" && kind(node) === K"bracescat") before_last_item = i < last_item_idx if before_last_item || expect_trailing_comma @@ -465,8 +466,8 @@ function spaces_in_listlike(ctx::Context, node::Node) !JuliaSyntax.is_whitespace, @view(kids[1:closing_leaf_idx - 1]), i + 1, ) next_kind = next_non_ws_idx === nothing ? nothing : kind(kids[next_non_ws_idx]) - # Insert a comma - if next_kind !== K"," + # Insert a comma if there isn't one coming + if trailing && next_kind !== K"," @assert expect_trailing_comma this_kid_changed = true if kids′ === kids @@ -476,8 +477,6 @@ function spaces_in_listlike(ctx::Context, node::Node) push!(kids′, comma) accept_node!(ctx, comma) state = :expect_closing - else - @assert false # Unreachable? end any_kid_changed |= this_kid_changed # Accept the newline diff --git a/test/runtests.jl b/test/runtests.jl index 0b6090e..2514ce0 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -207,6 +207,11 @@ end @test format_string("$(f)($(sp)$(a)$(sp),$(sp)$(b)$(sp))") == format_string("$(f)($(sp)$(a)$(sp),$(sp)$(b)$(sp),$(sp))") == "$(f)($(a), $(b))" + # comments on the same line + @test format_string("$(f)($(sp)$(a)$(sp), #==#$(sp)$(b)$(sp))") == + "$(f)($(a), #==# $(b))" + @test format_string("$(f)($(sp)$(a) #==#,$(sp)$(b)$(sp))") == + "$(f)($(a) #==#, $(b))" # line break in between items @test format_string("$(f)($(sp)$(a)$(sp),\n$(sp)$(b)$(sp))") == format_string("$(f)($(sp)$(a)$(sp),\n$(sp)$(b)$(sp),$(sp))") ==