From a943a577e477b13a8f9de6e976fa1f6cb68fbda8 Mon Sep 17 00:00:00 2001 From: tecosaur Date: Wed, 21 Feb 2024 01:21:58 +0800 Subject: [PATCH] More consistent return value for annotations, take 2 (#53333) Relands #53281 with some fixes noticed, though not the original causes of the failure. --- base/strings/annotated.jl | 27 ++++++++++++------- .../md5 | 1 - .../sha512 | 1 - .../md5 | 1 + .../sha512 | 1 + stdlib/StyledStrings.version | 2 +- 6 files changed, 20 insertions(+), 13 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 1eeaaa668d9ee7..1b1130335f0137 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 @@ -356,37 +356,44 @@ 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 -annotations(s::SubString{<:AnnotatedString}) = - annotations(s, s.offset+1:s.offset+s.ncodeunits) +function annotations(s::SubString{<:AnnotatedString}) + map(((region, annot),) -> (first(region)-s.offset:last(region)-s.offset, annot), + annotations(s.string, s.offset+1:s.offset+s.ncodeunits)) +end function annotations(s::AnnotatedString, pos::UnitRange{<:Integer}) # TODO optimise - annots = filter(label -> !isempty(intersect(pos, first(label))), - s.annotations) - last.(annots) + Tuple{UnitRange{Int64}, Pair{Symbol, Any}}[ + (max(first(pos), first(region)):min(last(pos), last(region)), annot) + for (region, annot) in s.annotations if !isempty(intersect(pos, region))] 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 fc675ee83125a1..00000000000000 --- 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 09d5a8a6192957..00000000000000 --- 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 00000000000000..2ab79799cca0ee --- /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 00000000000000..70b0ef6f5cb3a3 --- /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 8180223575f051..19a5a24514f2ff 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