From acbe35e7493eb20590e79fd0a4e9ff3f75e66b47 Mon Sep 17 00:00:00 2001 From: TEC Date: Sun, 11 Feb 2024 19:23:56 +0800 Subject: [PATCH] More consistent return value for annotations There are two different kinds of return value that annotations may currently produce. 1. A vector of annotations (Pair{Symbol, Any}) 2. A vector of region-annotation tuples (Tuple{UnitRange{Int}, Pair{Symbol, Any}}) Currently, there is an excess of argument-return-type combinations: 1. (string) -> vector of region-annotation tuples 2. (substring, region) -> vector of annotation values 3. (string, region/index) -> vector of annotation values 4. (char) -> vector of annotation values While wanting to get the annotations of a substring, it became apparent that it behaved differently to fetching the annotations of an AnnotatedString{String}. This is undesirable. Upon further reflection, the diversity of return types when calling annotations on a string-like form is excessive, and behaviours 1-3 can be merged into a single behaviour, resulting in a simpler set of combinations: 1. (string-like, region/index?) -> vector of region-annotation tuples 2. (char) -> vector of annotation values I consider this simplification highly desirable. Since this changes the API, the usage in StyledStrings is also updated to match, and we bump the version of StyledStrings to get the new version compatible with this revised API. --- base/strings/annotated.jl | 20 +++++++++++-------- .../md5 | 1 - .../sha512 | 1 - .../md5 | 1 + .../sha512 | 1 + stdlib/StyledStrings.version | 2 +- 6 files changed, 15 insertions(+), 11 deletions(-) delete mode 100644 deps/checksums/StyledStrings-a1b2ae2434cd7d8199fa8647339422fe0e1d0324.tar.gz/md5 delete mode 100644 deps/checksums/StyledStrings-a1b2ae2434cd7d8199fa8647339422fe0e1d0324.tar.gz/sha512 create mode 100644 deps/checksums/StyledStrings-e0ca0f85412ea5cafabfeaaec4d62ca26c3959d2.tar.gz/md5 create mode 100644 deps/checksums/StyledStrings-e0ca0f85412ea5cafabfeaaec4d62ca26c3959d2.tar.gz/sha512 diff --git a/base/strings/annotated.jl b/base/strings/annotated.jl index f84a85eaf62ab..94cf38e96d101 100644 --- a/base/strings/annotated.jl +++ b/base/strings/annotated.jl @@ -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 @@ -354,12 +354,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 @@ -369,22 +373,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 diff --git a/deps/checksums/StyledStrings-a1b2ae2434cd7d8199fa8647339422fe0e1d0324.tar.gz/md5 b/deps/checksums/StyledStrings-a1b2ae2434cd7d8199fa8647339422fe0e1d0324.tar.gz/md5 deleted file mode 100644 index fc675ee83125a..0000000000000 --- a/deps/checksums/StyledStrings-a1b2ae2434cd7d8199fa8647339422fe0e1d0324.tar.gz/md5 +++ /dev/null @@ -1 +0,0 @@ -c1fd1bb7e3f9ab00afe0758a8a101374 diff --git a/deps/checksums/StyledStrings-a1b2ae2434cd7d8199fa8647339422fe0e1d0324.tar.gz/sha512 b/deps/checksums/StyledStrings-a1b2ae2434cd7d8199fa8647339422fe0e1d0324.tar.gz/sha512 deleted file mode 100644 index 09d5a8a619295..0000000000000 --- a/deps/checksums/StyledStrings-a1b2ae2434cd7d8199fa8647339422fe0e1d0324.tar.gz/sha512 +++ /dev/null @@ -1 +0,0 @@ -373302f1d32a5b5ad0efacf14fa82a16fd877c11ca589fc1f1492eb1d4bcd0f49a2a5a3f471f8577649309c8a03634e7467e31b403cfd6ab8a8e4a775d7f4e53 diff --git a/deps/checksums/StyledStrings-e0ca0f85412ea5cafabfeaaec4d62ca26c3959d2.tar.gz/md5 b/deps/checksums/StyledStrings-e0ca0f85412ea5cafabfeaaec4d62ca26c3959d2.tar.gz/md5 new file mode 100644 index 0000000000000..2ab79799cca0e --- /dev/null +++ b/deps/checksums/StyledStrings-e0ca0f85412ea5cafabfeaaec4d62ca26c3959d2.tar.gz/md5 @@ -0,0 +1 @@ +fc3a846400107c432d20da6cfdd19ccf diff --git a/deps/checksums/StyledStrings-e0ca0f85412ea5cafabfeaaec4d62ca26c3959d2.tar.gz/sha512 b/deps/checksums/StyledStrings-e0ca0f85412ea5cafabfeaaec4d62ca26c3959d2.tar.gz/sha512 new file mode 100644 index 0000000000000..70b0ef6f5cb3a --- /dev/null +++ b/deps/checksums/StyledStrings-e0ca0f85412ea5cafabfeaaec4d62ca26c3959d2.tar.gz/sha512 @@ -0,0 +1 @@ +22da8964cc4c09f7c7a3da44be14c953f520ce6d395cf0f9ccf9c17777d6d968b0a874b35c072801ef7a1f4eee40f96ea0e2fc5ed5b3a63ad0b6b776a9c14ebb diff --git a/stdlib/StyledStrings.version b/stdlib/StyledStrings.version index 8180223575f05..19a5a24514f2f 100644 --- a/stdlib/StyledStrings.version +++ b/stdlib/StyledStrings.version @@ -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