diff --git a/src/runestone.jl b/src/runestone.jl index 0b25915..5edcf50 100644 --- a/src/runestone.jl +++ b/src/runestone.jl @@ -178,38 +178,9 @@ function spaces_around_x(ctx::Context, node::Node, is_x::F, n_leaves_per_x::Int replace_bytes!(ctx, " ", span(kid)) accept_node!(ctx, ws) looking_for_whitespace = false - elseif !is_leaf(kid) && kind(first_leaf(kid)) === K"Whitespace" - # Whitespace found at the beginning of next kid. - kid_ws = first_leaf(kid) - looking_for_whitespace = kind(last_leaf(kid)) !== K"Whitespace" - @assert !is_x(kid)::Bool - looking_for_x = true - if span(kid_ws) == 1 - # Accept the node - accept_node!(ctx, kid) - any_changes && push!(kids′, kid) - else - # Replace the whitespace node of the kid - kid′ = replace_first_leaf(kid, ws) - @assert span(kid′) == span(kid) - span(kid_ws) + 1 - replace_bytes!(ctx, " ", span(kid_ws)) - accept_node!(ctx, kid′) - any_changes = true - if kids′ === kids - kids′ = kids[1:(i - 1)] - end - push!(kids′, kid′) - end - elseif !is_leaf(kid) && kind(first_leaf(kid)) === K"NewlineWs" - # NewlineWs have to be accepted as is - # @info " ... kids first leaf is NewlineWs I'll take it" - accept_node!(ctx, kid) - any_changes && push!(kids′, kid) - looking_for_whitespace = kind(last_leaf(kid)) !== K"Whitespace" - @assert !is_x(kid)::Bool - looking_for_x = true else - # @info " ... no whitespace, inserting" kind(kid) + @assert !(first_leaf(kid) in KSet"Whitespace NewlineWs") + @assert !(first_leaf(kid) in KSet"Comment Whitespace NewlineWs") # Not a whitespace node, insert one any_changes = true if kids′ === kids @@ -468,14 +439,7 @@ function spaces_in_listlike(ctx::Context, node::Node) state = :expect_space # To ensure space after the comment else # This is an item (probably?). - # Make sure it doesn't have leading or trailing whitespace. - if kind(first_leaf(kid′)) === K"Whitespace" && kind(second_leaf(kid′)) !== K"Comment" && !first_item_in_implicit_tuple - # Delete the whitespace leaf - kid_ws = first_leaf(kid′) - replace_bytes!(ctx, "", span(kid_ws)) - kid′ = replace_first_leaf(kid′, nullnode) - this_kid_changed = true - end + @assert !JuliaSyntax.is_whitespace(first_leaf(kid′)) if kind(last_leaf(kid′)) === K"Whitespace" # Delete the whitespace leaf kid_ws = last_leaf(kid′) @@ -589,16 +553,7 @@ function spaces_in_listlike(ctx::Context, node::Node) elseif kind(kid′) === K"parameters" # Note that some of these are not valid Julia syntax still parse @assert kind(node) in KSet"call dotcall macrocall curly tuple vect ref braces" - if kind(first_leaf(kid′)) === K"Whitespace" - # Delete the whitespace leaf - kid_ws = first_leaf(kid′) - replace_bytes!(ctx, "", span(kid_ws)) - kid′ = replace_first_leaf(kid′, nullnode) - this_kid_changed = true - # if kids′ === kids - # kids′ = kids[1:i - 1] - # end - end + @assert !JuliaSyntax.is_whitespace(first_leaf(kid′)) if require_trailing_comma && !has_tag(kid′, TAG_TRAILING_COMMA) # Tag the parameters node to require a trailing comma kid′ = add_tag(kid′, TAG_TRAILING_COMMA) @@ -686,52 +641,21 @@ function spaces_in_listlike(ctx::Context, node::Node) accept_node!(ctx, kid′) any_kid_changed && push!(kids′, kid′) state = :expect_item - elseif kind(kid′) === K"Comment" - # Comments are accepted, state stays the same - # TODO: Make sure there is a space before the comment? Maybe that's not the - # responsibility of this function though. - accept_node!(ctx, kid′) - any_kid_changed && push!(kids′, kid′) else - # Probably a list item, look for leading whitespace, or insert. + # Probably a list item, insert a space before it @assert !(kind(kid′) in KSet", ;") - if kind(first_leaf(kid′)) === K"NewlineWs" || - kind(first_leaf(kid′)) === K"Comment" || - (kind(first_leaf(kid′)) === K"Whitespace" && kind(second_leaf(kid′)) === K"Comment") - # Newline, comment, or whitespace followed by comment - accept_node!(ctx, kid′) - any_kid_changed && push!(kids′, kid′) - state = state_after_item(i, last_item_idx, require_trailing_comma) - elseif kind(first_leaf(kid′)) === K"Whitespace" - ws_node = first_leaf(kid′) - if span(ws_node) == 1 - accept_node!(ctx, kid′) - any_kid_changed && push!(kids′, kid′) - else - kid′ = replace_first_leaf(kid′, ws) - this_kid_changed = true - if kids′ === kids - kids′ = kids[1:(i - 1)] - end - replace_bytes!(ctx, " ", span(ws_node)) - accept_node!(ctx, kid′) - push!(kids′, kid′) - end - state = state_after_item(i, last_item_idx, require_trailing_comma) - else - # Insert a standalone space kid and then accept the current node - this_kid_changed = true - if kids′ === kids - kids′ = kids[1:(i - 1)] - end - replace_bytes!(ctx, " ", 0) - push!(kids′, ws) - accept_node!(ctx, ws) - push!(kids′, kid′) - accept_node!(ctx, kid′) - # Here we inserted a space and consumed the next item, moving on to comma - state = state_after_item(i, last_item_idx, require_trailing_comma) + @assert !JuliaSyntax.is_whitespace(first_leaf(kid′)) + this_kid_changed = true + if kids′ === kids + kids′ = kids[1:(i - 1)] end + replace_bytes!(ctx, " ", 0) + push!(kids′, ws) + accept_node!(ctx, ws) + push!(kids′, kid′) + accept_node!(ctx, kid′) + # Here we inserted a space and consumed the next item, moving on to comma + state = state_after_item(i, last_item_idx, require_trailing_comma) end else @assert state === :expect_closing @@ -867,11 +791,10 @@ function no_spaces_around_x(ctx::Context, node::Node, is_x::F) where {F} end for (i, kid) in pairs(kids) - if (i == 1 || i == length(kids)) && kind(kid) === K"Whitespace" - # Leave any leading and trailing whitespace - accept_node!(ctx, kid) - any_changes && push!(kids′, kid) - elseif kind(kid) === K"Whitespace" + if kind(kid) === K"Whitespace" + # Leading and trailing whitespace should not be dropped but normalization should + # make sure these never exist. + @assert 1 < i < length(kids) # Ignore it but need to copy kids and re-write bytes any_changes = true if kids′ === kids @@ -882,23 +805,6 @@ function no_spaces_around_x(ctx::Context, node::Node, is_x::F) where {F} @assert !JuliaSyntax.is_whitespace(kid) # Filtered out above if looking_for_x @assert is_x(kid)::Bool - else - if i > first_x_idx - # Remove leading whitespace - ws_kid = first_leaf(kid) - if kind(ws_kid) === K"Whitespace" - kid = replace_first_leaf(kid, nullnode) - replace_bytes!(ctx, "", span(ws_kid)) - any_changes = true - end - end - if i < last_x_idx - # Remove trailing whitespace - ws_kid = last_leaf(kid) - if kind(ws_kid) === K"Whitespace" - unreachable() - end - end end if any_changes if kids === kids′ @@ -1318,7 +1224,7 @@ function spaces_around_keywords(ctx::Context, node::Node) end elseif state === :looking_for_space if (kind(kid) === K"Whitespace" && span(kid) == 1) || - kind(kid) === K"NewlineWs" || kind(first_leaf(kid)) === K"NewlineWs" + kind(kid) === K"NewlineWs" if kind(kid) === K"NewlineWs" # Is a newline instead of a space accepted for any other case? @assert kind(node) in KSet"where local global const" @@ -1334,24 +1240,6 @@ function spaces_around_keywords(ctx::Context, node::Node) replace_bytes!(ctx, " ", span(kid)) push!(kids′, ws) accept_node!(ctx, ws) - elseif space_after && kind(first_leaf(kid)) === K"Whitespace" - kid_ws = first_leaf(kid) - if span(kid_ws) == 1 - accept_node!(ctx, kid) - any_changes && push!(kids′, kid) - else - kid′ = replace_first_leaf(kid, ws) - @assert span(kid′) == span(kid) - span(kid_ws) + 1 - replace_bytes!(ctx, " ", span(kid_ws)) - accept_node!(ctx, kid′) - any_changes = true - if kids′ === kids - kids′ = kids[1:(i - 1)] - end - push!(kids′, kid′) - end - elseif !space_after && kind(last_leaf(kid)) === K"Whitespace" - unreachable() else # Reachable in e.g. `T where{T}`, `if(`, ... insert space @assert kind(node) in KSet"where if elseif while do function return local global module baremodule" @@ -2057,12 +1945,6 @@ function indent_block( if kind(kids′[insert_idx]) === K"Whitespace" kids′, ws = popatview!(kids′, insert_idx) wsspn = span(ws) - elseif !is_leaf(kids′[insert_idx]) && - kind(first_leaf(kids′[insert_idx])) === K"Whitespace" - ws = first_leaf(kids′[insert_idx]) - kids′[insert_idx] = replace_first_leaf(kids′[insert_idx], nullnode) - any_kid_changed = true - wsspn = span(ws) end # If we end up in this code path we are most likely splitting a single line block # into multiples lines. This means that we haven't yet updated the indent level for @@ -2430,17 +2312,6 @@ function indent_listlike( any_kid_changed = true push!(kids′, kid) accept_node!(ctx, kid) - elseif kind(first_leaf(kid)) === K"Whitespace" - grandkid = first_leaf(kid) - grandkid = Node(JuliaSyntax.SyntaxHead(K"NewlineWs", JuliaSyntax.TRIVIA_FLAG), span(grandkid) + 1) - replace_bytes!(ctx, "\n", 0) - kid = replace_first_leaf(kid, grandkid) - if kids′ === kids - kids′ = kids[1:(open_idx - 1)] - end - any_kid_changed = true - push!(kids′, kid) - accept_node!(ctx, kid) elseif kind(kid) === K"parameters" # For parameters we want the newline after the semi-colon grandkids = verified_kids(kid) @@ -2490,6 +2361,7 @@ function indent_listlike( accept_node!(ctx, kid) end else + @assert kind(first_leaf(kid)) !== K"Whitespace" nlws = Node(JuliaSyntax.SyntaxHead(K"NewlineWs", JuliaSyntax.TRIVIA_FLAG), 1) replace_bytes!(ctx, "\n", 0) if kids′ === kids @@ -3102,12 +2974,6 @@ function indent_local_global(ctx::Context, node::Node) if kind(kids[i]) === K"NewlineWs" && !has_tag(kids[i], TAG_LINE_CONT) kids[i] = add_tag(kids[i], TAG_LINE_CONT) changed = true - elseif i == nonws && kind(first_leaf(kids[i])) === K"NewlineWs" && - !has_tag(first_leaf(kids[i]), TAG_LINE_CONT) - kid′ = replace_first_leaf(kids[i], add_tag(first_leaf(kids[i]), TAG_LINE_CONT)) - @assert kid′ !== nothing - kids[i] = kid′ - changed = true end end return changed ? make_node(node, kids) : nothing @@ -3515,16 +3381,10 @@ function return_node(ctx::Context, ret::Node) push!(kids, ws) push!(kids, ret) else - @assert kind(first_leaf(ret)) !== K"NewlineWs" - has_ws = kind(first_leaf(ret)) === K"Whitespace" - if has_ws - replace_bytes!(ctx, "return", 0) - push!(kids, ret) - else - replace_bytes!(ctx, "return ", 0) - push!(kids, ws) - push!(kids, ret) - end + @assert !(kind(first_leaf(ret)) in KSet"NewlineWs Whitespace") + replace_bytes!(ctx, "return ", 0) + push!(kids, ws) + push!(kids, ret) end return Node(JuliaSyntax.SyntaxHead(K"return", 0), kids) end @@ -3632,11 +3492,8 @@ function explicit_return_block(ctx, node) seek(ctx.fmt_io, position(ctx.fmt_io) - spn) rexpr_idx -= 1 @assert kind(first_leaf(rexpr)) !== K"Whitespace" - elseif kind(first_leaf(rexpr)) === K"Whitespace" - # The first child in the return node is whitespace - spn = span(first_leaf(rexpr)) - rexpr = replace_first_leaf(rexpr, nullnode) end + @assert kind(first_leaf(rexpr)) !== K"Whitespace" nl = "\n" * " "^(4 * ctx.indent_level) nlnode = Node(JuliaSyntax.SyntaxHead(K"NewlineWs", JuliaSyntax.TRIVIA_FLAG), sizeof(nl)) insert!(kids′, rexpr_idx, nlnode)