Skip to content

Commit

Permalink
Privatise the annotations API, for StyledStrings
Browse files Browse the repository at this point in the history
This is a counterpart to a modification to the StyledStrings stdlib that
together shift the annotated API from being part of Base's API to part
of the StyledStrings API.

This is done to open the door to some of these types/function
potentially being implemented in StyledStrings directly, without
breaking any public API.
  • Loading branch information
tecosaur committed Aug 11, 2024
1 parent 7e809b0 commit 05c9f08
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 68 deletions.
7 changes: 0 additions & 7 deletions base/public.jl
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,8 @@ public
ImmutableDict,
OneTo,
LogRange,
AnnotatedString,
AnnotatedChar,
UUID,

# Annotated strings
annotatedstring,
annotate!,
annotations,

# Semaphores
Semaphore,
acquire,
Expand Down
14 changes: 6 additions & 8 deletions base/strings/annotated.jl
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,10 @@ See also [`AnnotatedChar`](@ref), [`annotatedstring`](@ref),
[`annotations`](@ref), and [`annotate!`](@ref).
!!! warning
While the constructors are part of the Base public API, the fields
of `AnnotatedString` are not. This is to allow for potential future
While the constructors are part of the `StyledStrings` public API, the
fields of `AnnotatedString` are not. This is to allow for potential future
changes in the implementation of this type. Instead use the
[`annotations`](@ref), and [`annotate!`](@ref) getter/setter
functions.
[`annotations`](@ref), and [`annotate!`](@ref) getter/setter functions.
# Constructors
Expand Down Expand Up @@ -82,11 +81,10 @@ See also: [`AnnotatedString`](@ref), [`annotatedstring`](@ref), `annotations`,
and `annotate!`.
!!! warning
While the constructors are part of the Base public API, the fields
of `AnnotatedChar` are not. This it to allow for potential future
While the constructors are part of the `StyledStrings` public API, the
fields of `AnnotatedChar` are not. This it to allow for potential future
changes in the implementation of this type. Instead use the
[`annotations`](@ref), and [`annotate!`](@ref) getter/setter
functions.
[`annotations`](@ref), and [`annotate!`](@ref) getter/setter functions.
# Constructors
Expand Down
5 changes: 0 additions & 5 deletions doc/src/base/strings.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,6 @@ Core.String(::AbstractString)
Base.SubString
Base.LazyString
Base.@lazy_str
Base.AnnotatedString
Base.AnnotatedChar
Base.annotatedstring
Base.annotations
Base.annotate!
Base.transcode
Base.unsafe_string
Base.ncodeunits(::AbstractString)
Expand Down
48 changes: 0 additions & 48 deletions doc/src/manual/strings.md
Original file line number Diff line number Diff line change
Expand Up @@ -1203,51 +1203,3 @@ Notice that the first two backslashes appear verbatim in the output, since they
precede a quote character.
However, the next backslash character escapes the backslash that follows it, and the
last backslash escapes a quote, since these backslashes appear before a quote.


## [Annotated Strings](@id man-annotated-strings)

It is sometimes useful to be able to hold metadata relating to regions of a
string. A [`AnnotatedString`](@ref Base.AnnotatedString) wraps another string and
allows for regions of it to be annotated with labelled values (`:label => value`).
All generic string operations are applied to the underlying string. However,
when possible, styling information is preserved. This means you can manipulate a
[`AnnotatedString`](@ref Base.AnnotatedString) —taking substrings, padding them,
concatenating them with other strings— and the metadata annotations will "come
along for the ride".

This string type is fundamental to the [StyledStrings stdlib](@ref
stdlib-styledstrings), which uses `:face`-labelled annotations to hold styling
information.

When concatenating a [`AnnotatedString`](@ref Base.AnnotatedString), take care to use
[`annotatedstring`](@ref Base.annotatedstring) instead of [`string`](@ref) if you want
to keep the string annotations.

```jldoctest
julia> str = Base.AnnotatedString("hello there",
[(1:5, :word => :greeting), (7:11, :label => 1)])
"hello there"
julia> length(str)
11
julia> lpad(str, 14)
" hello there"
julia> typeof(lpad(str, 7))
Base.AnnotatedString{String}
julia> str2 = Base.AnnotatedString(" julia", [(2:6, :face => :magenta)])
" julia"
julia> Base.annotatedstring(str, str2)
"hello there julia"
julia> str * str2 == Base.annotatedstring(str, str2) # *-concatenation still works
true
```

The annotations of a [`AnnotatedString`](@ref Base.AnnotatedString) can be accessed
and modified via the [`annotations`](@ref Base.annotations) and
[`annotate!`](@ref Base.annotate!) functions.

0 comments on commit 05c9f08

Please sign in to comment.