Skip to content

Commit

Permalink
Fix spacing around dotted operators, fixes #8.
Browse files Browse the repository at this point in the history
  • Loading branch information
fredrikekre committed Jun 13, 2024
1 parent 68675ca commit bf1e035
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 13 deletions.
5 changes: 3 additions & 2 deletions src/chisels.jl
Original file line number Diff line number Diff line change
Expand Up @@ -221,9 +221,10 @@ function is_assignment(node::Node)
# return !is_leaf(node) && JuliaSyntax.is_prec_assignment(node)
end

# Just like `JuliaSyntax.is_infix_op_call`, but also check that the node is K"call"
# Just like `JuliaSyntax.is_infix_op_call`, but also check that the node is K"call" or
# K"dotcall"
function is_infix_op_call(node::Node)
return kind(node) === K"call" && JuliaSyntax.is_infix_op_call(node)
return kind(node) in KSet"call dotcall" && JuliaSyntax.is_infix_op_call(node)
end

# Extract the operator of an infix op call node
Expand Down
43 changes: 33 additions & 10 deletions src/runestone.jl
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ end
# Insert space around `x`, where `x` can be operators, assignments, etc. with the pattern:
# `<something><space><x><space><something>`, for example the spaces around `+` and `=` in
# `a = x + y`.
function spaces_around_x(ctx::Context, node::Node, is_x::F) where F
function spaces_around_x(ctx::Context, node::Node, is_x::F, n_leaves_per_x::Int = 1) where F
# TODO: So much boilerplate here...
@assert !is_leaf(node)

Expand All @@ -165,6 +165,7 @@ function spaces_around_x(ctx::Context, node::Node, is_x::F) where F
# Toggle for whether we are currently looking for whitespace or not
looking_for_whitespace = false
looking_for_x = false
n_x_leaves_visited = 0

for (i, kid) in pairs(kids)
if kind(kid) === K"NewlineWs" ||
Expand Down Expand Up @@ -238,22 +239,43 @@ function spaces_around_x(ctx::Context, node::Node, is_x::F) where F
push!(kids′, kid)
accept_node!(ctx, kid)
looking_for_whitespace = kind(last_leaf(kid)) !== K"Whitespace"
# TODO: Duplicated with the branch below.
if looking_for_x
@assert is_x(kid)::Bool
n_x_leaves_visited += 1
if n_x_leaves_visited == n_leaves_per_x
looking_for_x = false
n_x_leaves_visited = 0
else
looking_for_whitespace = false
end
else
looking_for_x = kind(kid) !== K"Comment"
end
# Flip the switch, unless kid is a comment
looking_for_x = kind(kid) === K"Comment" ? looking_for_x : !looking_for_x
end
else # !expect_ws
if looking_for_x
@assert is_x(kid)::Bool
end
# We end up here if we look for x, or the things in between x's
@assert kind(kid) !== K"Whitespace" # This would be weird, I think?
any_changes && push!(kids′, kid)
accept_node!(ctx, kid)
looking_for_whitespace = kind(last_leaf(kid)) !== K"Whitespace"
# Flip the switch, unless kid is a comment
looking_for_x = kind(kid) === K"Comment" ? looking_for_x : !looking_for_x
if looking_for_x
# We are looking for x, check we have them all otherwise keep looking
@assert is_x(kid)::Bool
n_x_leaves_visited += 1
if n_x_leaves_visited == n_leaves_per_x
looking_for_x = false
n_x_leaves_visited = 0
else
# Multiple x's is only for dotted operators and there should be no
# whitespace in between
looking_for_whitespace = false
end
else
# This is a thing in between, but if it is a comment we still look for the
# real thing in between
looking_for_x = kind(kid) !== K"Comment"
end
end
end
# Reset stream
Expand All @@ -276,9 +298,10 @@ function spaces_around_operators(ctx::Context, node::Node)
)
return nothing
end
@assert kind(node) in KSet"call comparison <: >:"
@assert kind(node) in KSet"call dotcall comparison <: >:"
is_x = x -> is_operator_leaf(x) || is_comparison_leaf(x)
return spaces_around_x(ctx, node, is_x)
n_leaves_per_x = kind(node) === K"dotcall" ? 2 : 1
return spaces_around_x(ctx, node, is_x, n_leaves_per_x)
end

function spaces_around_assignments(ctx::Context, node::Node)
Expand Down
2 changes: 1 addition & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ end

@testset "whitespace between operators" begin
for sp in ("", " ", " ")
for op in ("+", "-", "==", "!=", "===", "!==", "<", "<=")
for op in ("+", "-", "==", "!=", "===", "!==", "<", "<=", ".+", ".==")
# a op b
@test format_string("$(sp)a$(sp)$(op)$(sp)b$(sp)") ==
"$(sp)a $(op) b$(sp)"
Expand Down

0 comments on commit bf1e035

Please sign in to comment.