Skip to content

Commit

Permalink
More consistent return value for annotations, take 2 (JuliaLang#53333)
Browse files Browse the repository at this point in the history
Relands JuliaLang#53281 with some fixes noticed, though not the original causes
of the failure.
  • Loading branch information
tecosaur authored and mkitti committed Mar 7, 2024
1 parent 1912fdd commit a943a57
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 13 deletions.
27 changes: 17 additions & 10 deletions base/strings/annotated.jl
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ lastindex(s::AnnotatedString) = lastindex(s.string)
function getindex(s::AnnotatedString, i::Integer)
@boundscheck checkbounds(s, i)
@inbounds if isvalid(s, i)
AnnotatedChar(s.string[i], annotations(s, i))
AnnotatedChar(s.string[i], map(last, annotations(s, i)))
else
string_index_err(s, i)
end
Expand Down Expand Up @@ -356,37 +356,44 @@ annotate!(c::AnnotatedChar, @nospecialize(labelval::Pair{Symbol, <:Any})) =
(push!(c.annotations, labelval); c)

"""
annotations(str::AnnotatedString, [position::Union{Integer, UnitRange}])
annotations(str::SubString{AnnotatedString}, [position::Union{Integer, UnitRange}])
annotations(str::Union{AnnotatedString, SubString{AnnotatedString}},
[position::Union{Integer, UnitRange}]) ->
Vector{Tuple{UnitRange{Int}, Pair{Symbol, Any}}}
Get all annotations that apply to `str`. Should `position` be provided, only
annotations that overlap with `position` will be returned.
Annotations are provided together with the regions they apply to, in the form of
a vector of region–annotation tuples.
See also: `annotate!`.
"""
annotations(s::AnnotatedString) = s.annotations

annotations(s::SubString{<:AnnotatedString}) =
annotations(s, s.offset+1:s.offset+s.ncodeunits)
function annotations(s::SubString{<:AnnotatedString})
map(((region, annot),) -> (first(region)-s.offset:last(region)-s.offset, annot),
annotations(s.string, s.offset+1:s.offset+s.ncodeunits))
end

function annotations(s::AnnotatedString, pos::UnitRange{<:Integer})
# TODO optimise
annots = filter(label -> !isempty(intersect(pos, first(label))),
s.annotations)
last.(annots)
Tuple{UnitRange{Int64}, Pair{Symbol, Any}}[
(max(first(pos), first(region)):min(last(pos), last(region)), annot)
for (region, annot) in s.annotations if !isempty(intersect(pos, region))]
end

annotations(s::AnnotatedString, pos::Integer) = annotations(s, pos:pos)

annotations(s::SubString{<:AnnotatedString}, pos::Integer) =
annotations(s.string, s.offset + pos)

annotations(s::SubString{<:AnnotatedString}, pos::UnitRange{<:Integer}) =
annotations(s.string, first(pos)+s.offset:last(pos)+s.offset)

"""
annotations(chr::AnnotatedChar)
annotations(chr::AnnotatedChar) -> Vector{Pair{Symbol, Any}}
Get all annotations of `chr`.
Get all annotations of `chr`, in the form of a vector of annotation pairs.
"""
annotations(c::AnnotatedChar) = c.annotations

Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
fc3a846400107c432d20da6cfdd19ccf
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
22da8964cc4c09f7c7a3da44be14c953f520ce6d395cf0f9ccf9c17777d6d968b0a874b35c072801ef7a1f4eee40f96ea0e2fc5ed5b3a63ad0b6b776a9c14ebb
2 changes: 1 addition & 1 deletion stdlib/StyledStrings.version
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
STYLEDSTRINGS_BRANCH = main
STYLEDSTRINGS_SHA1 = a1b2ae2434cd7d8199fa8647339422fe0e1d0324
STYLEDSTRINGS_SHA1 = e0ca0f85412ea5cafabfeaaec4d62ca26c3959d2
STYLEDSTRINGS_GIT_URL := https://github.com/JuliaLang/StyledStrings.jl.git
STYLEDSTRINGS_TAR_URL = https://api.github.com/repos/JuliaLang/StyledStrings.jl/tarball/$1

0 comments on commit a943a57

Please sign in to comment.