Browse Source

Fix whitespace around :: when LHS is missing

`::` can be used without a LHS in e.g. function definitions like
`f(::Int) = ...`.
pull/19/head
Fredrik Ekre 2 years ago
parent
commit
7c48019613
No known key found for this signature in database
GPG Key ID: DE82E6D5E364C0A2
  1. 7
      src/chisels.jl
  2. 6
      src/runestone.jl
  3. 2
      test/runtests.jl

7
src/chisels.jl

@ -82,3 +82,10 @@ end
function is_operator_leaf(node::JuliaSyntax.GreenNode) function is_operator_leaf(node::JuliaSyntax.GreenNode)
return is_leaf(node) && JuliaSyntax.is_operator(node) return is_leaf(node) && JuliaSyntax.is_operator(node)
end 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
src/runestone.jl

@ -308,6 +308,12 @@ function no_spaces_around_x(ctx::Context, node::JuliaSyntax.GreenNode, is_x::F)
looking_for_x = false 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) for (i, child) in pairs(children)
span_sum += JuliaSyntax.span(child) 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"

2
test/runtests.jl

@ -171,6 +171,8 @@ end
@test format_string("a::T") == "a::T" @test format_string("a::T") == "a::T"
@test format_string("a::T::S") == "a::T::S" @test format_string("a::T::S") == "a::T::S"
@test format_string("a :: T") == "a::T" @test format_string("a :: T") == "a::T"
@test format_string("f(::T)::T = 1") == "f(::T)::T = 1"
@test format_string("f(:: T) :: T = 1") == "f(::T)::T = 1"
# K"<:" and K">:" # K"<:" and K">:"
@test format_string("a<:T") == "a <: T" @test format_string("a<:T") == "a <: T"
@test format_string("a>:T") == "a >: T" @test format_string("a>:T") == "a >: T"

Loading…
Cancel
Save