Browse Source

Trailing comments on first line of multiline lists, fixes #77 (#86)

pull/87/head
Fredrik Ekre 1 year ago committed by GitHub
parent
commit
35d6dd44ca
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 11
      src/chisels.jl
  2. 18
      src/runestone.jl
  3. 8
      test/runtests.jl

11
src/chisels.jl

@ -382,6 +382,17 @@ function second_leaf(node::Node) @@ -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)

18
src/runestone.jl

@ -2366,11 +2366,25 @@ function indent_listlike( @@ -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( @@ -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)

8
test/runtests.jl

@ -260,9 +260,13 @@ end @@ -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)"

Loading…
Cancel
Save