Skip to content

Commit e1a76bb

Browse files
authored
More consistent return value for annotations (#53281)
This is a generally nice simplification, that tweaks the `annotations` API. It also requires the adjustment in JuliaLang/StyledStrings.jl#38 to be made, hence the bump.
1 parent 86c0191 commit e1a76bb

File tree

6 files changed

+15
-11
lines changed

6 files changed

+15
-11
lines changed

base/strings/annotated.jl

Lines changed: 12 additions & 8 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
@@ -354,12 +354,16 @@ annotate!(c::AnnotatedChar, @nospecialize(labelval::Pair{Symbol, <:Any})) =
354354
(push!(c.annotations, labelval); c)
355355

356356
"""
357-
annotations(str::AnnotatedString, [position::Union{Integer, UnitRange}])
358-
annotations(str::SubString{AnnotatedString}, [position::Union{Integer, UnitRange}])
357+
annotations(str::Union{AnnotatedString, SubString{AnnotatedString}},
358+
[position::Union{Integer, UnitRange}]) ->
359+
Vector{Tuple{UnitRange{Int}, Pair{Symbol, Any}}}
359360
360361
Get all annotations that apply to `str`. Should `position` be provided, only
361362
annotations that overlap with `position` will be returned.
362363
364+
Annotations are provided together with the regions they apply to, in the form of
365+
a vector of region–annotation tuples.
366+
363367
See also: `annotate!`.
364368
"""
365369
annotations(s::AnnotatedString) = s.annotations
@@ -369,22 +373,22 @@ annotations(s::SubString{<:AnnotatedString}) =
369373

370374
function annotations(s::AnnotatedString, pos::UnitRange{<:Integer})
371375
# TODO optimise
372-
annots = filter(label -> !isempty(intersect(pos, first(label))),
373-
s.annotations)
374-
last.(annots)
376+
filter(label -> !isempty(intersect(pos, first(label))),
377+
s.annotations)
375378
end
376379

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

379382
annotations(s::SubString{<:AnnotatedString}, pos::Integer) =
380383
annotations(s.string, s.offset + pos)
384+
381385
annotations(s::SubString{<:AnnotatedString}, pos::UnitRange{<:Integer}) =
382386
annotations(s.string, first(pos)+s.offset:last(pos)+s.offset)
383387

384388
"""
385-
annotations(chr::AnnotatedChar)
389+
annotations(chr::AnnotatedChar) -> Vector{Pair{Symbol, Any}}
386390
387-
Get all annotations of `chr`.
391+
Get all annotations of `chr`, in the form of a vector of annotation pairs.
388392
"""
389393
annotations(c::AnnotatedChar) = c.annotations
390394

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)