|
|
|
@ -18,7 +18,7 @@ function trim_trailing_whitespace(ctx::Context, node::JuliaSyntax.GreenNode) |
|
|
|
return nothing |
|
|
|
return nothing |
|
|
|
end |
|
|
|
end |
|
|
|
# Write new bytes and reset the stream |
|
|
|
# Write new bytes and reset the stream |
|
|
|
nb = write_and_reset(ctx, str′) |
|
|
|
nb = replace_bytes!(ctx, str′, JuliaSyntax.span(node)) |
|
|
|
@assert nb != JuliaSyntax.span(node) |
|
|
|
@assert nb != JuliaSyntax.span(node) |
|
|
|
# Create new node and return it |
|
|
|
# Create new node and return it |
|
|
|
node′ = JuliaSyntax.GreenNode(JuliaSyntax.head(node), nb, ()) |
|
|
|
node′ = JuliaSyntax.GreenNode(JuliaSyntax.head(node), nb, ()) |
|
|
|
@ -43,7 +43,7 @@ function format_hex_literals(ctx::Context, node::JuliaSyntax.GreenNode) |
|
|
|
while length(bytes) < target_spans[i] |
|
|
|
while length(bytes) < target_spans[i] |
|
|
|
insert!(bytes, 3, '0') |
|
|
|
insert!(bytes, 3, '0') |
|
|
|
end |
|
|
|
end |
|
|
|
nb = write_and_reset(ctx, bytes) |
|
|
|
nb = replace_bytes!(ctx, bytes, span) |
|
|
|
@assert nb == length(bytes) == target_spans[i] |
|
|
|
@assert nb == length(bytes) == target_spans[i] |
|
|
|
# Create new node and return it |
|
|
|
# Create new node and return it |
|
|
|
node′ = JuliaSyntax.GreenNode(JuliaSyntax.head(node), nb, ()) |
|
|
|
node′ = JuliaSyntax.GreenNode(JuliaSyntax.head(node), nb, ()) |
|
|
|
@ -81,7 +81,7 @@ function format_oct_literals(ctx::Context, node::JuliaSyntax.GreenNode) |
|
|
|
while length(bytes) < target_span |
|
|
|
while length(bytes) < target_span |
|
|
|
insert!(bytes, 3, '0') |
|
|
|
insert!(bytes, 3, '0') |
|
|
|
end |
|
|
|
end |
|
|
|
nb = write_and_reset(ctx, bytes) |
|
|
|
nb = replace_bytes!(ctx, bytes, span) |
|
|
|
@assert nb == length(bytes) == target_span |
|
|
|
@assert nb == length(bytes) == target_span |
|
|
|
# Create new node and return it |
|
|
|
# Create new node and return it |
|
|
|
node′ = JuliaSyntax.GreenNode(JuliaSyntax.head(node), nb, ()) |
|
|
|
node′ = JuliaSyntax.GreenNode(JuliaSyntax.head(node), nb, ()) |
|
|
|
@ -138,7 +138,7 @@ function format_float_literals(ctx::Context, node::JuliaSyntax.GreenNode) |
|
|
|
write(io, exp_part) |
|
|
|
write(io, exp_part) |
|
|
|
end |
|
|
|
end |
|
|
|
bytes = take!(io) |
|
|
|
bytes = take!(io) |
|
|
|
nb = write_and_reset(ctx, bytes) |
|
|
|
nb = replace_bytes!(ctx, bytes, JuliaSyntax.span(node)) |
|
|
|
@assert nb == length(bytes) |
|
|
|
@assert nb == length(bytes) |
|
|
|
# Create new node and return it |
|
|
|
# Create new node and return it |
|
|
|
node′ = JuliaSyntax.GreenNode(JuliaSyntax.head(node), nb, ()) |
|
|
|
node′ = JuliaSyntax.GreenNode(JuliaSyntax.head(node), nb, ()) |
|
|
|
@ -155,8 +155,6 @@ function spaces_around_x(ctx::Context, node::JuliaSyntax.GreenNode, is_x::F) whe |
|
|
|
children = verified_children(node) |
|
|
|
children = verified_children(node) |
|
|
|
children′ = children |
|
|
|
children′ = children |
|
|
|
any_changes = false |
|
|
|
any_changes = false |
|
|
|
original_bytes = node_bytes(ctx, node) |
|
|
|
|
|
|
|
span_sum = 0 |
|
|
|
|
|
|
|
pos = position(ctx.fmt_io) |
|
|
|
pos = position(ctx.fmt_io) |
|
|
|
ws = JuliaSyntax.GreenNode( |
|
|
|
ws = JuliaSyntax.GreenNode( |
|
|
|
JuliaSyntax.SyntaxHead(K"Whitespace", JuliaSyntax.TRIVIA_FLAG), 1, (), |
|
|
|
JuliaSyntax.SyntaxHead(K"Whitespace", JuliaSyntax.TRIVIA_FLAG), 1, (), |
|
|
|
@ -167,7 +165,6 @@ function spaces_around_x(ctx::Context, node::JuliaSyntax.GreenNode, is_x::F) whe |
|
|
|
looking_for_x = false |
|
|
|
looking_for_x = false |
|
|
|
|
|
|
|
|
|
|
|
for (i, child) in pairs(children) |
|
|
|
for (i, child) in pairs(children) |
|
|
|
span_sum += JuliaSyntax.span(child) |
|
|
|
|
|
|
|
if JuliaSyntax.kind(child) === K"NewlineWs" || |
|
|
|
if JuliaSyntax.kind(child) === K"NewlineWs" || |
|
|
|
(i == 1 && JuliaSyntax.kind(child) === K"Whitespace") |
|
|
|
(i == 1 && JuliaSyntax.kind(child) === K"Whitespace") |
|
|
|
# NewlineWs are accepted as is by this pass. |
|
|
|
# NewlineWs are accepted as is by this pass. |
|
|
|
@ -190,11 +187,8 @@ function spaces_around_x(ctx::Context, node::JuliaSyntax.GreenNode, is_x::F) whe |
|
|
|
children′ = children[1:i - 1] |
|
|
|
children′ = children[1:i - 1] |
|
|
|
end |
|
|
|
end |
|
|
|
push!(children′, ws) |
|
|
|
push!(children′, ws) |
|
|
|
write_and_reset(ctx, " ") |
|
|
|
replace_bytes!(ctx, " ", JuliaSyntax.span(child)) |
|
|
|
accept_node!(ctx, ws) |
|
|
|
accept_node!(ctx, ws) |
|
|
|
# Re-write bytes for remaining children |
|
|
|
|
|
|
|
remaining_bytes = @view original_bytes[(span_sum + 1):end] |
|
|
|
|
|
|
|
write_and_reset(ctx, remaining_bytes) |
|
|
|
|
|
|
|
looking_for_whitespace = false |
|
|
|
looking_for_whitespace = false |
|
|
|
elseif JuliaSyntax.haschildren(child) && |
|
|
|
elseif JuliaSyntax.haschildren(child) && |
|
|
|
JuliaSyntax.kind(first_leaf(child)) === K"Whitespace" |
|
|
|
JuliaSyntax.kind(first_leaf(child)) === K"Whitespace" |
|
|
|
@ -213,9 +207,7 @@ function spaces_around_x(ctx::Context, node::JuliaSyntax.GreenNode, is_x::F) whe |
|
|
|
@assert JuliaSyntax.span(child′) == JuliaSyntax.span(child) - JuliaSyntax.span(child_ws) + 1 |
|
|
|
@assert JuliaSyntax.span(child′) == JuliaSyntax.span(child) - JuliaSyntax.span(child_ws) + 1 |
|
|
|
bytes_to_skip = JuliaSyntax.span(child) - JuliaSyntax.span(child′) |
|
|
|
bytes_to_skip = JuliaSyntax.span(child) - JuliaSyntax.span(child′) |
|
|
|
@assert bytes_to_skip > 0 |
|
|
|
@assert bytes_to_skip > 0 |
|
|
|
remaining_bytes_inclusive = |
|
|
|
replace_bytes!(ctx, "", bytes_to_skip) |
|
|
|
@view original_bytes[(span_sum + 1 + bytes_to_skip - JuliaSyntax.span(child)):end] |
|
|
|
|
|
|
|
write_and_reset(ctx, remaining_bytes_inclusive) |
|
|
|
|
|
|
|
accept_node!(ctx, child′) |
|
|
|
accept_node!(ctx, child′) |
|
|
|
any_changes = true |
|
|
|
any_changes = true |
|
|
|
if children′ === children |
|
|
|
if children′ === children |
|
|
|
@ -226,25 +218,24 @@ function spaces_around_x(ctx::Context, node::JuliaSyntax.GreenNode, is_x::F) whe |
|
|
|
elseif JuliaSyntax.haschildren(child) && |
|
|
|
elseif JuliaSyntax.haschildren(child) && |
|
|
|
JuliaSyntax.kind(first_leaf(child)) === K"NewlineWs" |
|
|
|
JuliaSyntax.kind(first_leaf(child)) === K"NewlineWs" |
|
|
|
# NewlineWs have to be accepted as is |
|
|
|
# NewlineWs have to be accepted as is |
|
|
|
|
|
|
|
# @info " ... childs first leaf is NewlineWs I'll take it" |
|
|
|
accept_node!(ctx, child) |
|
|
|
accept_node!(ctx, child) |
|
|
|
any_changes && push!(children′, child) |
|
|
|
any_changes && push!(children′, child) |
|
|
|
looking_for_whitespace = JuliaSyntax.kind(last_leaf(child)) !== K"Whitespace" |
|
|
|
looking_for_whitespace = JuliaSyntax.kind(last_leaf(child)) !== K"Whitespace" |
|
|
|
@assert !is_x(child)::Bool |
|
|
|
@assert !is_x(child)::Bool |
|
|
|
looking_for_x = true |
|
|
|
looking_for_x = true |
|
|
|
else |
|
|
|
else |
|
|
|
|
|
|
|
# @info " ... no whitespace, inserting" JuliaSyntax.kind(child) |
|
|
|
# Not a whitespace node, insert one |
|
|
|
# Not a whitespace node, insert one |
|
|
|
any_changes = true |
|
|
|
any_changes = true |
|
|
|
if children′ === children |
|
|
|
if children′ === children |
|
|
|
children′ = children[1:i - 1] |
|
|
|
children′ = children[1:i - 1] |
|
|
|
end |
|
|
|
end |
|
|
|
push!(children′, ws) |
|
|
|
push!(children′, ws) |
|
|
|
write_and_reset(ctx, " ") |
|
|
|
replace_bytes!(ctx, " ", 0) |
|
|
|
accept_node!(ctx, ws) |
|
|
|
accept_node!(ctx, ws) |
|
|
|
# Write and accept the node |
|
|
|
# Write and accept the node |
|
|
|
push!(children′, child) |
|
|
|
push!(children′, child) |
|
|
|
remaining_bytes_inclusive = |
|
|
|
|
|
|
|
@view original_bytes[(span_sum + 1 - JuliaSyntax.span(child)):end] |
|
|
|
|
|
|
|
write_and_reset(ctx, remaining_bytes_inclusive) |
|
|
|
|
|
|
|
accept_node!(ctx, child) |
|
|
|
accept_node!(ctx, child) |
|
|
|
looking_for_whitespace = JuliaSyntax.kind(last_leaf(child)) !== K"Whitespace" |
|
|
|
looking_for_whitespace = JuliaSyntax.kind(last_leaf(child)) !== K"Whitespace" |
|
|
|
if looking_for_x |
|
|
|
if looking_for_x |
|
|
|
@ -313,8 +304,6 @@ function no_spaces_around_x(ctx::Context, node::JuliaSyntax.GreenNode, is_x::F) |
|
|
|
children = verified_children(node) |
|
|
|
children = verified_children(node) |
|
|
|
children′ = children |
|
|
|
children′ = children |
|
|
|
any_changes = false |
|
|
|
any_changes = false |
|
|
|
original_bytes = node_bytes(ctx, node) |
|
|
|
|
|
|
|
span_sum = 0 |
|
|
|
|
|
|
|
pos = position(ctx.fmt_io) |
|
|
|
pos = position(ctx.fmt_io) |
|
|
|
|
|
|
|
|
|
|
|
looking_for_x = false |
|
|
|
looking_for_x = false |
|
|
|
@ -326,7 +315,6 @@ function no_spaces_around_x(ctx::Context, node::JuliaSyntax.GreenNode, is_x::F) |
|
|
|
end |
|
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
for (i, child) in pairs(children) |
|
|
|
for (i, child) in pairs(children) |
|
|
|
span_sum += JuliaSyntax.span(child) |
|
|
|
|
|
|
|
if (i == 1 || i == length(children)) && JuliaSyntax.kind(child) === K"Whitespace" |
|
|
|
if (i == 1 || i == length(children)) && JuliaSyntax.kind(child) === K"Whitespace" |
|
|
|
accept_node!(ctx, child) |
|
|
|
accept_node!(ctx, child) |
|
|
|
any_changes && push!(children′, child) |
|
|
|
any_changes && push!(children′, child) |
|
|
|
@ -336,8 +324,7 @@ function no_spaces_around_x(ctx::Context, node::JuliaSyntax.GreenNode, is_x::F) |
|
|
|
if children′ === children |
|
|
|
if children′ === children |
|
|
|
children′ = children[1:i - 1] |
|
|
|
children′ = children[1:i - 1] |
|
|
|
end |
|
|
|
end |
|
|
|
remaining_bytes = @view original_bytes[(span_sum + 1):end] |
|
|
|
replace_bytes!(ctx, "", JuliaSyntax.span(child)) |
|
|
|
write_and_reset(ctx, remaining_bytes) |
|
|
|
|
|
|
|
else |
|
|
|
else |
|
|
|
@assert JuliaSyntax.kind(child) !== K"Whitespace" |
|
|
|
@assert JuliaSyntax.kind(child) !== K"Whitespace" |
|
|
|
if looking_for_x |
|
|
|
if looking_for_x |
|
|
|
@ -394,24 +381,18 @@ function replace_with_in(ctx::Context, node::JuliaSyntax.GreenNode) |
|
|
|
@assert JuliaSyntax.kind(in_node) in KSet"∈ =" |
|
|
|
@assert JuliaSyntax.kind(in_node) in KSet"∈ =" |
|
|
|
@assert JuliaSyntax.is_trivia(in_node) |
|
|
|
@assert JuliaSyntax.is_trivia(in_node) |
|
|
|
@assert is_leaf(in_node) |
|
|
|
@assert is_leaf(in_node) |
|
|
|
bytes = node_bytes(ctx, node) # TODO: Need something better... |
|
|
|
|
|
|
|
# Accept nodes to advance the stream |
|
|
|
# Accept nodes to advance the stream |
|
|
|
span_sum = 0 |
|
|
|
|
|
|
|
for i in 1:(in_index - 1) |
|
|
|
for i in 1:(in_index - 1) |
|
|
|
span_sum += JuliaSyntax.span(children[i]) |
|
|
|
|
|
|
|
accept_node!(ctx, children[i]) |
|
|
|
accept_node!(ctx, children[i]) |
|
|
|
end |
|
|
|
end |
|
|
|
span_sum += JuliaSyntax.span(children[in_index]) |
|
|
|
|
|
|
|
# Construct the replacement |
|
|
|
# Construct the replacement |
|
|
|
nb = write_and_reset(ctx, "in") |
|
|
|
nb = replace_bytes!(ctx, "in", JuliaSyntax.span(in_node)) |
|
|
|
in_node′ = JuliaSyntax.GreenNode( |
|
|
|
in_node′ = JuliaSyntax.GreenNode( |
|
|
|
JuliaSyntax.SyntaxHead(K"in", JuliaSyntax.TRIVIA_FLAG), nb, (), |
|
|
|
JuliaSyntax.SyntaxHead(K"in", JuliaSyntax.TRIVIA_FLAG), nb, (), |
|
|
|
) |
|
|
|
) |
|
|
|
accept_node!(ctx, in_node′) |
|
|
|
accept_node!(ctx, in_node′) |
|
|
|
children′ = copy(children) |
|
|
|
children′ = copy(children) |
|
|
|
children′[in_index] = in_node′ |
|
|
|
children′[in_index] = in_node′ |
|
|
|
# Write the backed up bytes |
|
|
|
|
|
|
|
write_and_reset(ctx, @view(bytes[(span_sum + 1):end])) |
|
|
|
|
|
|
|
# Accept remaining eq_children |
|
|
|
# Accept remaining eq_children |
|
|
|
for i in (in_index + 1):length(children′) |
|
|
|
for i in (in_index + 1):length(children′) |
|
|
|
accept_node!(ctx, children′[i]) |
|
|
|
accept_node!(ctx, children′[i]) |
|
|
|
@ -424,10 +405,7 @@ function replace_with_in_cartesian(ctx::Context, node::JuliaSyntax.GreenNode) |
|
|
|
@assert JuliaSyntax.kind(node) === K"cartesian_iterator" && !is_leaf(node) |
|
|
|
@assert JuliaSyntax.kind(node) === K"cartesian_iterator" && !is_leaf(node) |
|
|
|
children = verified_children(node) |
|
|
|
children = verified_children(node) |
|
|
|
children′ = children |
|
|
|
children′ = children |
|
|
|
bytes = node_bytes(ctx, node) |
|
|
|
|
|
|
|
span_sum = 0 |
|
|
|
|
|
|
|
for (i, child) in pairs(children) |
|
|
|
for (i, child) in pairs(children) |
|
|
|
span_sum += JuliaSyntax.span(child) |
|
|
|
|
|
|
|
if JuliaSyntax.kind(child) === K"=" |
|
|
|
if JuliaSyntax.kind(child) === K"=" |
|
|
|
child′ = replace_with_in(ctx, child) |
|
|
|
child′ = replace_with_in(ctx, child) |
|
|
|
if child′ !== nothing |
|
|
|
if child′ !== nothing |
|
|
|
@ -435,8 +413,6 @@ function replace_with_in_cartesian(ctx::Context, node::JuliaSyntax.GreenNode) |
|
|
|
children′ = copy(children) |
|
|
|
children′ = copy(children) |
|
|
|
end |
|
|
|
end |
|
|
|
children′[i] = child′ |
|
|
|
children′[i] = child′ |
|
|
|
# Need to re-write the bytes |
|
|
|
|
|
|
|
write_and_reset(ctx, @view(bytes[(span_sum + 1):end])) |
|
|
|
|
|
|
|
else |
|
|
|
else |
|
|
|
children′[i] = child |
|
|
|
children′[i] = child |
|
|
|
accept_node!(ctx, child) |
|
|
|
accept_node!(ctx, child) |
|
|
|
@ -463,8 +439,7 @@ function for_loop_use_in(ctx::Context, node::JuliaSyntax.GreenNode) |
|
|
|
) |
|
|
|
) |
|
|
|
return nothing |
|
|
|
return nothing |
|
|
|
end |
|
|
|
end |
|
|
|
pos = position(ctx.fmt_io) # In case a reset is needed later |
|
|
|
pos = position(ctx.fmt_io) |
|
|
|
bytes = node_bytes(ctx, node) |
|
|
|
|
|
|
|
children = verified_children(node) |
|
|
|
children = verified_children(node) |
|
|
|
for_index = findfirst(c -> JuliaSyntax.kind(c) === K"for" && is_leaf(c), children)::Int |
|
|
|
for_index = findfirst(c -> JuliaSyntax.kind(c) === K"for" && is_leaf(c), children)::Int |
|
|
|
for_node = children[for_index] |
|
|
|
for_node = children[for_index] |
|
|
|
@ -484,7 +459,6 @@ function for_loop_use_in(ctx::Context, node::JuliaSyntax.GreenNode) |
|
|
|
for_spec_node′ = replace_with_in_cartesian(ctx, for_spec_node) |
|
|
|
for_spec_node′ = replace_with_in_cartesian(ctx, for_spec_node) |
|
|
|
end |
|
|
|
end |
|
|
|
if for_spec_node′ === nothing |
|
|
|
if for_spec_node′ === nothing |
|
|
|
# TODO: reset here? Because we werent' supposed to write "for" above |
|
|
|
|
|
|
|
seek(ctx.fmt_io, pos) |
|
|
|
seek(ctx.fmt_io, pos) |
|
|
|
return nothing |
|
|
|
return nothing |
|
|
|
end |
|
|
|
end |
|
|
|
@ -492,9 +466,6 @@ function for_loop_use_in(ctx::Context, node::JuliaSyntax.GreenNode) |
|
|
|
# Insert the new for spec node |
|
|
|
# Insert the new for spec node |
|
|
|
children′ = copy(children) |
|
|
|
children′ = copy(children) |
|
|
|
children′[for_spec_index] = for_spec_node′ |
|
|
|
children′[for_spec_index] = for_spec_node′ |
|
|
|
# Write the backup bytes |
|
|
|
|
|
|
|
span_sum = JuliaSyntax.span(for_node) + JuliaSyntax.span(for_spec_node) |
|
|
|
|
|
|
|
write_and_reset(ctx, @view(bytes[(span_sum + 1):end])) |
|
|
|
|
|
|
|
# At this point the eq node is done, just accept any remaining nodes |
|
|
|
# At this point the eq node is done, just accept any remaining nodes |
|
|
|
# TODO: Don't need to do this... |
|
|
|
# TODO: Don't need to do this... |
|
|
|
for i in (for_spec_index + 1):length(children′) |
|
|
|
for i in (for_spec_index + 1):length(children′) |
|
|
|
|