Skip to content

Commit 1ac4ca3

Browse files
committed
More consistent return value for annotations, take 2 (JuliaLang#53333)
Relands JuliaLang#53281 with some fixes noticed, though not the original causes of the failure.
1 parent a12d0cd commit 1ac4ca3

File tree

6 files changed

+20
-13
lines changed

6 files changed

+20
-13
lines changed

base/strings/annotated.jl

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ lastindex(s::AnnotatedString) = lastindex(s.string)
153153
function getindex(s::AnnotatedString, i::Integer)
154154
@boundscheck checkbounds(s, i)
155155
@inbounds if isvalid(s, i)
156-
AnnotatedChar(s.string[i], annotations(s, i))
156+
AnnotatedChar(s.string[i], map(last, annotations(s, i)))
157157
else
158158
string_index_err(s, i)
159159
end
@@ -356,37 +356,44 @@ annotate!(c::AnnotatedChar, @nospecialize(labelval::Pair{Symbol, <:Any})) =
356356
(push!(c.annotations, labelval); c)
357357

358358
"""
359-
annotations(str::AnnotatedString, [position::Union{Integer, UnitRange}])
360-
annotations(str::SubString{AnnotatedString}, [position::Union{Integer, UnitRange}])
359+
annotations(str::Union{AnnotatedString, SubString{AnnotatedString}},
360+
[position::Union{Integer, UnitRange}]) ->
361+
Vector{Tuple{UnitRange{Int}, Pair{Symbol, Any}}}
361362
362363
Get all annotations that apply to `str`. Should `position` be provided, only
363364
annotations that overlap with `position` will be returned.
364365
366+
Annotations are provided together with the regions they apply to, in the form of
367+
a vector of region–annotation tuples.
368+
365369
See also: `annotate!`.
366370
"""
367371
annotations(s::AnnotatedString) = s.annotations
368372

369-
annotations(s::SubString{<:AnnotatedString}) =
370-
annotations(s, s.offset+1:s.offset+s.ncodeunits)
373+
function annotations(s::SubString{<:AnnotatedString})
374+
map(((region, annot),) -> (first(region)-s.offset:last(region)-s.offset, annot),
375+
annotations(s.string, s.offset+1:s.offset+s.ncodeunits))
376+
end
371377

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

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

381387
annotations(s::SubString{<:AnnotatedString}, pos::Integer) =
382388
annotations(s.string, s.offset + pos)
389+
383390
annotations(s::SubString{<:AnnotatedString}, pos::UnitRange{<:Integer}) =
384391
annotations(s.string, first(pos)+s.offset:last(pos)+s.offset)
385392

386393
"""
387-
annotations(chr::AnnotatedChar)
394+
annotations(chr::AnnotatedChar) -> Vector{Pair{Symbol, Any}}
388395
389-
Get all annotations of `chr`.
396+
Get all annotations of `chr`, in the form of a vector of annotation pairs.
390397
"""
391398
annotations(c::AnnotatedChar) = c.annotations
392399

deps/checksums/StyledStrings-a1b2ae2434cd7d8199fa8647339422fe0e1d0324.tar.gz/md5

Lines changed: 0 additions & 1 deletion
This file was deleted.

deps/checksums/StyledStrings-a1b2ae2434cd7d8199fa8647339422fe0e1d0324.tar.gz/sha512

Lines changed: 0 additions & 1 deletion
This file was deleted.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
fc3a846400107c432d20da6cfdd19ccf
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
22da8964cc4c09f7c7a3da44be14c953f520ce6d395cf0f9ccf9c17777d6d968b0a874b35c072801ef7a1f4eee40f96ea0e2fc5ed5b3a63ad0b6b776a9c14ebb

stdlib/StyledStrings.version

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
STYLEDSTRINGS_BRANCH = main
2-
STYLEDSTRINGS_SHA1 = a1b2ae2434cd7d8199fa8647339422fe0e1d0324
2+
STYLEDSTRINGS_SHA1 = e0ca0f85412ea5cafabfeaaec4d62ca26c3959d2
33
STYLEDSTRINGS_GIT_URL := https://github.com/JuliaLang/StyledStrings.jl.git
44
STYLEDSTRINGS_TAR_URL = https://api.github.com/repos/JuliaLang/StyledStrings.jl/tarball/$1

0 commit comments

Comments
 (0)