Skip to content

Commit

Permalink
Reapply "More consistent return value for annotations" (JuliaLang#53309)
Browse files Browse the repository at this point in the history
This reverts commit 81c3474.
  • Loading branch information
tecosaur committed Feb 14, 2024
1 parent 09a27b3 commit 6faf589
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 11 deletions.
20 changes: 12 additions & 8 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,12 +356,16 @@ 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
Expand All @@ -371,22 +375,22 @@ annotations(s::SubString{<:AnnotatedString}) =

function annotations(s::AnnotatedString, pos::UnitRange{<:Integer})
# TODO optimise
annots = filter(label -> !isempty(intersect(pos, first(label))),
s.annotations)
last.(annots)
filter(label -> !isempty(intersect(pos, first(label))),
s.annotations)
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 6faf589

Please sign in to comment.