diff --git a/src/JuliaSyntaxHighlighting.jl b/src/JuliaSyntaxHighlighting.jl index 48ad0df..2de62dc 100644 --- a/src/JuliaSyntaxHighlighting.jl +++ b/src/JuliaSyntaxHighlighting.jl @@ -148,13 +148,13 @@ function paren_type(k::Kind) end mutable struct ParenDepthCounter - paren::UInt - bracket::UInt - curly::UInt + paren::Int + bracket::Int + curly::Int end ParenDepthCounter() = - ParenDepthCounter(zero(UInt), zero(UInt), zero(UInt)) + ParenDepthCounter(0, 0, 0) struct GreenLineage{H} node::GreenNode{H} @@ -345,7 +345,7 @@ function _hl_annotations!(highlights::Vector{@NamedTuple{region::UnitRange{Int}, setfield!(pdepths, ptype, depthref + depthchange) else depth0 = getfield(pdepths, ptype) - setfield!(pdepths, ptype, depthref + depthchange) + setfield!(pdepths, ptype, max(0, depthref + depthchange)) depth0 end if pdepth <= 0 && UNMATCHED_DELIMITERS_ENABLED[] diff --git a/test/runtests.jl b/test/runtests.jl index 05bc070..74db56b 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -27,3 +27,12 @@ astr_sum1to8 = Base.AnnotatedString("sum(1:8)") # Check for string indexing issues @test Base.annotations(highlight(":π")) |> first |> first == 1:3 + +# Test unpaired parentheses (issue #17) +# Test consecutive unpaired closing parens and that depth counter resets properly +reset_after_unpaired = highlight("(()))) ()") +anns = Base.annotations(reset_after_unpaired) +@test anns[5].value == :julia_unpaired_parentheses # First unpaired +@test anns[6].value == :julia_unpaired_parentheses # Second unpaired +@test anns[7].value == :julia_rainbow_paren_1 # Opening after reset +@test anns[8].value == :julia_rainbow_paren_1 # Closing after reset