Skip to content

Commit

Permalink
REmove more dead code
Browse files Browse the repository at this point in the history
  • Loading branch information
fredrikekre committed Nov 20, 2024
1 parent d927352 commit 6d99a59
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 116 deletions.
142 changes: 26 additions & 116 deletions src/runestone.jl
Original file line number Diff line number Diff line change
Expand Up @@ -413,11 +413,7 @@ function spaces_in_listlike(ctx::Context, node::Node)
this_kid_changed = false
first_item_in_implicit_tuple = implicit_tuple && i == opening_leaf_idx + 1
if state === :expect_item
if first_item_in_implicit_tuple && kind(kid′) === K"Whitespace" && peek(i) !== K"Comment"
# Not allowed to touch this one I think?
accept_node!(ctx, kid′)
any_kid_changed && push!(kids′, kid′)
elseif kind(kid′) === K"Whitespace" && peek(i) !== K"Comment"
if kind(kid′) === K"Whitespace" && peek(i) !== K"Comment"
@assert !first_item_in_implicit_tuple # Unreachable?
# Delete whitespace unless followed by a comment
replace_bytes!(ctx, "", span(kid′))
Expand All @@ -440,17 +436,7 @@ function spaces_in_listlike(ctx::Context, node::Node)
else
# This is an item (probably?).
@assert !JuliaSyntax.is_whitespace(first_leaf(kid′))
if kind(last_leaf(kid′)) === K"Whitespace"
# Delete the whitespace leaf
kid_ws = last_leaf(kid′)
let pos = position(ctx.fmt_io)
seek(ctx.fmt_io, pos + span(kid′) - span(kid_ws))
replace_bytes!(ctx, "", span(kid_ws))
seek(ctx.fmt_io, pos)
end
kid′ = replace_last_leaf(kid′, nullnode)
this_kid_changed = true
end
@assert !JuliaSyntax.is_whitespace(last_leaf(kid′))
if kind(kid′) === K"parameters" && require_trailing_comma &&
i == last_item_idx && !has_tag(kid′, TAG_TRAILING_COMMA)
# Tag the node to require a trailing comma
Expand All @@ -468,22 +454,15 @@ function spaces_in_listlike(ctx::Context, node::Node)
x -> !(JuliaSyntax.is_whitespace(x) || kind(x) in KSet", ;"),
verified_kids(kid′)
) == 0
# If kid is K"parameters" without items and we don't want a trailing
# comma/semicolon we need to eat any whitespace kids (e.g. comments)
# If kid is K"parameters" without items and we don't want the
# K"parameters" node for the trailing semicolon we drop the entire node
grandkids = verified_kids(kid′)
semi_idx = 1
@assert kind(grandkids[semi_idx]) === K";"
ws_idx = something(findnext(x -> kind(x) !== K"Whitespace", grandkids, semi_idx + 1), lastindex(grandkids) + 1)
@assert length(grandkids) == 1 && kind(grandkids[1]) === K";"
any_kid_changed = true
if kids′ === kids
kids′ = kids[1:(i - 1)]
end
replace_bytes!(ctx, "", mapreduce(span, +, grandkids[1:(ws_idx - 1)]; init = 0))
for j in ws_idx:length(grandkids)
grandkid = grandkids[j]
accept_node!(ctx, grandkid)
push!(kids′, grandkid)
end
replace_bytes!(ctx, "", span(kid′))
else
# Kid is now acceptable
any_kid_changed |= this_kid_changed
Expand Down Expand Up @@ -572,32 +551,14 @@ function spaces_in_listlike(ctx::Context, node::Node)
x -> !(JuliaSyntax.is_whitespace(x) || kind(x) in KSet", ;"),
verified_kids(kid′)
) == 0
# If kid is K"parameters" without items and we don't want a trailing
# comma/semicolon we need to eat any whitespace kids (e.g. comments)
# If kid is K"parameters" without items and we don't want the
# K"parameters" node for the trailing semicolon we drop the entire node
grandkids = verified_kids(kid′)
semi_idx = 1
@assert kind(grandkids[semi_idx]) === K";"
ws_idx = findnext(x -> kind(x) !== K"Whitespace", grandkids, semi_idx + 1)
if ws_idx !== nothing
any_kid_changed = true
if kids′ === kids
kids′ = kids[1:(i - 1)]
end
replace_bytes!(ctx, "", mapreduce(span, +, grandkids[1:(ws_idx - 1)]; init = 0))
for j in ws_idx:length(grandkids)
grandkid = grandkids[j]
accept_node!(ctx, grandkid)
push!(kids′, grandkid)
end
else
# Nothing in the parameter node needed, overwrite it fully
any_kid_changed = true
replace_bytes!(ctx, "", span(kid′))
if any_kid_changed
if kids′ === kids
kids′ = kids[1:(i - 1)]
end
end
@assert length(grandkids) == 1 && kind(grandkids[1]) === K";"
any_kid_changed = true
replace_bytes!(ctx, "", span(kid′))
if kids′ === kids
kids′ = kids[1:(i - 1)]
end
else
# TODO: Tag for requiring trailing comma.
Expand Down Expand Up @@ -1324,11 +1285,8 @@ function replace_with_in_filter(ctx::Context, node::Node)
@assert kind(node) === K"filter" && !is_leaf(node)
pos = position(ctx.fmt_io)
kids = verified_kids(node)
idx = findfirst(x -> kind(x) in KSet"= cartesian_iterator" && !is_leaf(x), kids)::Int
for i in 1:(idx - 1)
accept_node!(ctx, kids[i])
end
kid = kids[idx]
kid = kids[1]
@assert kind(kid) in KSet"= cartesian_iterator" && !is_leaf(kid)
if kind(kid) === K"="
kid′ = replace_with_in(ctx, kid)
else
Expand All @@ -1340,7 +1298,7 @@ function replace_with_in_filter(ctx::Context, node::Node)
return nothing
else
kids = copy(kids)
kids[idx] = kid′
kids[1] = kid′
return make_node(node, kids)
end
end
Expand Down Expand Up @@ -1455,6 +1413,7 @@ function braces_around_where_rhs(ctx::Context, node::Node)
pos = position(ctx.fmt_io)
where_idx = findfirst(x -> is_leaf(x) && kind(x) === K"where", kids)::Int
rhs_idx = findnext(!JuliaSyntax.is_whitespace, kids, where_idx + 1)::Int
@assert rhs_idx == lastindex(kids)
rhs = kids[rhs_idx]
if kind(rhs) === K"braces"
return nothing
Expand All @@ -1477,11 +1436,6 @@ function braces_around_where_rhs(ctx::Context, node::Node)
accept_node!(ctx, rhs)
replace_bytes!(ctx, "}", 0)
accept_node!(ctx, closing_brace)
# Accept any remaining kids
for i in (rhs_idx + 1):length(kids)
accept_node!(ctx, kids[i])
push!(kids′, kids[i])
end
# Reset stream and return
seek(ctx.fmt_io, pos)
return make_node(node, kids′)
Expand All @@ -1505,18 +1459,15 @@ function parens_around_op_calls_in_colon(ctx::Context, node::Node)
end
grandkids = verified_kids(kid)
first_non_ws = findfirst(!JuliaSyntax.is_whitespace, grandkids)::Int
@assert first_non_ws == firstindex(grandkids)
last_non_ws = findlast(!JuliaSyntax.is_whitespace, grandkids)::Int
# Extract whitespace grandkids to become kids
for j in 1:(first_non_ws - 1)
accept_node!(ctx, grandkids[j])
push!(kids′, grandkids[j])
end
@assert last_non_ws == lastindex(grandkids)
# Create the parens node
opening_paren = Node(JuliaSyntax.SyntaxHead(K"(", 0), 1)
replace_bytes!(ctx, "(", 0)
accept_node!(ctx, opening_paren)
parens_kids = [opening_paren]
kid′_kids = grandkids[first_non_ws:last_non_ws]
kid′_kids = copy(grandkids)
kid′ = make_node(kid, kid′_kids)
accept_node!(ctx, kid′)
push!(parens_kids, kid′)
Expand All @@ -1526,10 +1477,6 @@ function parens_around_op_calls_in_colon(ctx::Context, node::Node)
push!(parens_kids, closing_paren)
parens = Node(JuliaSyntax.SyntaxHead(K"parens", 0), parens_kids)
push!(kids′, parens)
for j in (last_non_ws + 1):length(grandkids)
accept_node!(ctx, grandkids[j])
push!(kids′, grandkids[j])
end
any_changes = true
else
accept_node!(ctx, kid)
Expand Down Expand Up @@ -2410,19 +2357,8 @@ function indent_listlike(
any_kid_changed = true
push!(kids′, kid)
accept_node!(ctx, kid)
elseif kind(last_leaf(kid)) === K"NewlineWs"
# Hidden newline without tag
grandkid = last_leaf(kid)
@assert !has_tag(grandkid, TAG_PRE_DEDENT)
grandkid = add_tag(grandkid, TAG_PRE_DEDENT)
kid = replace_last_leaf(kid, grandkid)
if kids′ === kids
kids′ = kids[1:(close_idx - 2)]
end
any_kid_changed = true
push!(kids′, kid)
accept_node!(ctx, kid)
else
@assert kind(last_leaf(kid)) !== K"NewlineWs"
# Need to insert a newline. Note that we tag the new newline directly since it
# is the responsibility of this function (otherwise there would just be an extra
# repetitive call to add it anyway).
Expand All @@ -2437,26 +2373,8 @@ function indent_listlike(
any_kid_changed = true
push!(kids′, kid)
accept_node!(ctx, kid)
elseif kind(last_leaf(kid)) === K"Whitespace"
# TODO: I believe this is only reachable because whitespace isn't trimmed from
# vcat nodes like function calls and tuples etc.
@assert kind(node) in KSet"vcat typed_vcat"
# Since the whitespace is inside the kid node we assume that a newlinews would
# have ended up inside too. We also remove all existing whitespace since it
# would otherwise have to be cleaned up by the trailing whitespace pass.
accept_node!(ctx, kid)
seek(ctx.fmt_io, position(ctx.fmt_io) - span(last_leaf(kid)))
replace_bytes!(ctx, "\n", span(last_leaf(kid)))
nlws = Node(JuliaSyntax.SyntaxHead(K"NewlineWs", JuliaSyntax.TRIVIA_FLAG), 1)
nlws = add_tag(nlws, TAG_PRE_DEDENT)
accept_node!(ctx, nlws)
kid′ = replace_last_leaf(kid, nlws)
if kids′ === kids
kids′ = kids[1:(open_idx - 1)]
end
any_kid_changed = true
push!(kids′, kid′)
else
@assert kind(last_leaf(kid)) !== K"Whitespace"
# Note that this is a trailing newline and should be put after this item
if kids′ === kids
kids′ = kids[1:(open_idx - 1)]
Expand Down Expand Up @@ -2484,11 +2402,7 @@ function indent_listlike(
any_kid_changed && push!(kids′, kid)
accept_node!(ctx, kid)
# Keep any remaining kids
for i in (close_idx + 1):length(kids)
kid = kids[i]
any_kid_changed && push!(kids′, kid)
accept_node!(ctx, kid)
end
@assert close_idx == lastindex(kids)
# Reset stream
seek(ctx.fmt_io, pos)
# Make a new node and return
Expand Down Expand Up @@ -2712,6 +2626,7 @@ function indent_assignment(ctx::Context, node::Node)
@assert kind(node) === kind(kids[eqidx])
@assert length(kids) > eqidx
rhsidx = findnext(!JuliaSyntax.is_whitespace, kids, eqidx + 1)::Int
@assert rhsidx == lastindex(kids)
r = (eqidx + 1):(rhsidx - 1)
length(r) == 0 && return nothing
rhs = kids[rhsidx]
Expand Down Expand Up @@ -2766,9 +2681,6 @@ function indent_assignment(ctx::Context, node::Node)
end
if changed
@assert kids !== kids′
for i in (rhsidx + 1):length(kids)
push!(kids′, kids[i])
end
return make_node(node, kids′)
else
return nothing
Expand Down Expand Up @@ -3421,7 +3333,7 @@ function has_return(node::Node)
# that is the job of a linter or something I guess.
return findfirst(x -> kind(x) === K"return", kids) !== nothing
else
error("unreachable")
unreachable()

Check warning on line 3336 in src/runestone.jl

View check run for this annotation

Codecov / codecov/patch

src/runestone.jl#L3336

Added line #L3336 was not covered by tests
end
end

Expand Down Expand Up @@ -3459,11 +3371,9 @@ function explicit_return_block(ctx, node)
if kind(rexpr) === K"call"
call_kids = verified_kids(rexpr)
fname_idx = findfirst(!JuliaSyntax.is_whitespace, call_kids)::Int
@assert fname_idx == firstindex(call_kids)
local fname
let p = position(ctx.fmt_io)
for i in 1:(fname_idx - 1)
accept_node!(ctx, call_kids[i])
end
fname = String(read_bytes(ctx, call_kids[fname_idx]))
seek(ctx.fmt_io, p)
end
Expand Down
4 changes: 4 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1254,6 +1254,8 @@ end
# Trailing semicolon before ws+comment
f; # comment
f;; # comment
# Trailing semicolon with whitespace on both sides
g ; # comment
"""
bodyfmt = """
# Semicolons on their own lines
Expand All @@ -1277,6 +1279,8 @@ end
# Trailing semicolon before ws+comment
f # comment
f # comment
# Trailing semicolon with whitespace on both sides
g # comment
"""
for prefix in (
"begin", "quote", "for i in I", "let", "let x = 1", "while cond",
Expand Down

0 comments on commit 6d99a59

Please sign in to comment.