From 7c48019613af41d010903c0a9b2779103b341a56 Mon Sep 17 00:00:00 2001 From: Fredrik Ekre Date: Mon, 27 May 2024 11:35:31 +0200 Subject: [PATCH] Fix whitespace around :: when LHS is missing `::` can be used without a LHS in e.g. function definitions like `f(::Int) = ...`. --- src/chisels.jl | 7 +++++++ src/runestone.jl | 6 ++++++ test/runtests.jl | 2 ++ 3 files changed, 15 insertions(+) diff --git a/src/chisels.jl b/src/chisels.jl index 1407214..a35d9ff 100644 --- a/src/chisels.jl +++ b/src/chisels.jl @@ -82,3 +82,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 diff --git a/src/runestone.jl b/src/runestone.jl index cf5a15a..592f407 100644 --- a/src/runestone.jl +++ b/src/runestone.jl @@ -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" diff --git a/test/runtests.jl b/test/runtests.jl index bf254a5..714017f 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -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)::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"