Skip to content

Commit

Permalink
Fix whitespace around :: when LHS is missing
Browse files Browse the repository at this point in the history
`::` can be used without a LHS in e.g. function definitions like
`f(::Int) = ...`.
  • Loading branch information
fredrikekre committed May 27, 2024
1 parent 5ddee14 commit 04694a2
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/chisels.jl
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,10 @@ end
function is_operator_leaf(node::JuliaSyntax.GreenNode)
return is_leaf(node) && JuliaSyntax.is_operator(node)
end

function first_non_whitespace_child(node::JuliaSyntax.GreenNode)
@assert !is_leaf(node)
children = JuliaSyntax.children(node)::AbstractVector
idx = findfirst(!JuliaSyntax.is_whitespace, children)::Int
return children[idx]
end
6 changes: 6 additions & 0 deletions src/runestone.jl
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,12 @@ function no_spaces_around_x(ctx::Context, node::JuliaSyntax.GreenNode, is_x::F)

looking_for_x = false

# K"::" is a special case here since it can be used without an LHS in e.g. function
# definitions like `f(::Int) = ...`.
if JuliaSyntax.kind(node) === K"::"
looking_for_x = is_x(first_non_whitespace_child(node))::Bool
end

for (i, child) in pairs(children)
span_sum += JuliaSyntax.span(child)
if (i == 1 || i == length(children)) && JuliaSyntax.kind(child) === K"Whitespace"
Expand Down
2 changes: 2 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,8 @@ end
@test format_string("a::T") == "a::T"
@test format_string("a::T::S") == "a::T::S"
@test format_string("a :: T") == "a::T"
@test format_string("f(::T)::T = 1") == "f(::T) = 1"
@test format_string("f(:: T) :: T = 1") == "f(::T)::T = 1"
# K"<:" and K">:"
@test format_string("a<:T") == "a <: T"
@test format_string("a>:T") == "a >: T"
Expand Down

0 comments on commit 04694a2

Please sign in to comment.