diff --git a/src/chisels.jl b/src/chisels.jl index 0ca817c..ee2ac95 100644 --- a/src/chisels.jl +++ b/src/chisels.jl @@ -382,6 +382,17 @@ function second_leaf(node::Node) return nth_leaf(node, 2) end +# TODO: This should probably be merged with kmatch or something... +function peek_leafs(node::Node, leaf_kinds::Tuple) + for (i, leaf_kind) in pairs(leaf_kinds) + ith = nth_leaf(node, i) + if ith === nothing || kind(ith) !== leaf_kind + return false + end + end + return true +end + # Return number of non-whitespace kids, basically the length the equivalent # (expr::Expr).args function meta_nargs(node::Node) diff --git a/src/runestone.jl b/src/runestone.jl index 8dda47f..c48e0dc 100644 --- a/src/runestone.jl +++ b/src/runestone.jl @@ -2366,11 +2366,25 @@ function indent_listlike( # Next we expect the leading newline @assert multiline kid = kids[open_idx + 1] + idx_after_leading_nl = open_idx + 2 if kind(kid) === K"NewlineWs" || - kind(first_leaf(kid)) === K"NewlineWs" + kind(first_leaf(kid)) === K"NewlineWs" || + peek_leafs(kid, KSet"Comment NewlineWs") || peek_leafs(kid, KSet"Whitespace Comment NewlineWs") # Newline or newlinde hidden in first item any_kid_changed && push!(kids′, kid) accept_node!(ctx, kid) + elseif kmatch(kids, KSet"Comment NewlineWs", open_idx + 1) + for i in (open_idx + 1):(open_idx + 2) + accept_node!(ctx, kids[i]) + any_kid_changed && push!(kids′, kids[i]) + end + idx_after_leading_nl = idx_after_leading_nl + 1 + elseif kmatch(kids, KSet"Whitespace Comment NewlineWs", open_idx + 1) + for i in (open_idx + 1):(open_idx + 3) + accept_node!(ctx, kids[i]) + any_kid_changed && push!(kids′, kids[i]) + end + idx_after_leading_nl = idx_after_leading_nl + 2 else # Need to insert a newline if kind(kid) === K"Whitespace" @@ -2458,7 +2472,7 @@ function indent_listlike( end end # Bring all kids between the opening and closing token to the new list - for i in (open_idx + 2):(close_idx - 2) + for i in idx_after_leading_nl:(close_idx - 2) kid = kids[i] any_kid_changed && push!(kids′, kid) accept_node!(ctx, kid) diff --git a/test/runtests.jl b/test/runtests.jl index 041b689..76e56df 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -260,9 +260,13 @@ end "$(o)\n $(a),\n $(b),\n$(c)" # trailing comments @test format_string("$(o)$(sp)# x\n$(sp)$(a)$(sp),$(sp)# a\n$(sp)$(b)$(sp)# b\n$(c)") == - "$(o)\n # x\n $(a),$(csp)# a\n $(b)$(tr)$(csp)# b\n$(c)" + "$(o)$(sp)# x\n $(a),$(csp)# a\n $(b)$(tr)$(csp)# b\n$(c)" @test format_string("$(o)$(sp)# x\n$(sp)$(a)$(sp),$(sp)# a\n$(sp)$(b),$(sp)# b\n$(c)") == - "$(o)\n # x\n $(a),$(csp)# a\n $(b),$(csp)# b\n$(c)" + "$(o)$(sp)# x\n $(a),$(csp)# a\n $(b),$(csp)# b\n$(c)" + @test format_string("$(o)$(sp)#= x =#$(sp)$(a)$(sp),$(sp)# a\n$(sp)$(b),$(sp)# b\n$(c)") == + "$(o)\n #= x =# $(a),$(csp)# a\n $(b),$(csp)# b\n$(c)" + @test format_string("$(o)$(sp)#= x =#\n$(a)$(sp),$(sp)# a\n$(sp)$(b),$(sp)# b\n$(c)") == + "$(o)$(sp)#= x =#\n $(a),$(csp)# a\n $(b),$(csp)# b\n$(c)" # comments on separate lines between items @test format_string("$(o)\n# a\n$(a)$(sp),\n# b\n$(b)\n$(c)") == "$(o)\n # a\n $(a),\n # b\n $(b)$(tr)\n$(c)"