Skip to content

Commit

Permalink
Trailing comments on first line of multiline lists, fixes #77 (#86)
Browse files Browse the repository at this point in the history
  • Loading branch information
fredrikekre authored Oct 28, 2024
1 parent e10cd92 commit 35d6dd4
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 4 deletions.
11 changes: 11 additions & 0 deletions src/chisels.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
18 changes: 16 additions & 2 deletions src/runestone.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand Down
8 changes: 6 additions & 2 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)"
Expand Down

0 comments on commit 35d6dd4

Please sign in to comment.