Skip to content

Commit

Permalink
Document the semantics of annotation ordering
Browse files Browse the repository at this point in the history
It's important to specify the way that annotations relate to the
characters of the underlying string and each other.
  • Loading branch information
tecosaur committed Apr 29, 2024
1 parent 91069e5 commit f869c2f
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions base/strings/annotated.jl
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,17 @@ and a value (`Any`), paired together as a `Pair{Symbol, <:Any}`.
Labels do not need to be unique, the same region can hold multiple annotations
with the same label.
Code written for `AnnotatedString`s in general should conserve the following
properties:
- Which characters an annotation is applied to
- The order in which annotations are applied to each character
Additional semantics may be introduced by specific uses of `AnnotatedString`s.
A corollary of these rules is that adjacent, consecutively placed, annotations
with identical labels and values are equivalent to a single annotation spanning
the combined range.
See also [`AnnotatedChar`](@ref), [`annotatedstring`](@ref),
[`annotations`](@ref), and [`annotate!`](@ref).
Expand Down Expand Up @@ -301,6 +312,9 @@ end
Annotate a `range` of `str` (or the entire string) with a labeled value (`label` => `value`).
To remove existing `label` annotations, use a value of `nothing`.
The order in which annotations are applied to `str` is semantically meaningful,
as described in [`AnnotatedString`](@ref).
"""
annotate!(s::AnnotatedString, range::UnitRange{Int}, @nospecialize(labelval::Pair{Symbol, <:Any})) =
(_annotate!(s.annotations, range, labelval); s)
Expand Down Expand Up @@ -333,6 +347,9 @@ 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.
In accordance with the semantics documented in [`AnnotatedString`](@ref), the
order of annotations returned matches the order in which they were applied.
See also: `annotate!`.
"""
annotations(s::AnnotatedString) = s.annotations
Expand Down

0 comments on commit f869c2f

Please sign in to comment.