Skip to content

Add FoundationAttributedString availability macro #1398

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,11 @@ set(_SwiftFoundation_FutureAvailability "macOS 10000, iOS 10000, tvOS 10000, wat

# All versions to define for each availability name
list(APPEND _SwiftFoundation_versions
"5.5"
"5.7"
"5.8"
"5.9"
"6.0"
"6.0.2"
"6.1"
"6.2"
Expand All @@ -107,10 +112,12 @@ list(APPEND _SwiftFoundation_versions
# Each availability name to define
list(APPEND _SwiftFoundation_availability_names
"FoundationPreview"
"FoundationAttributedString"
"FoundationSpan")

# The aligned availability for each name (in the same order)
list(APPEND _SwiftFoundation_availability_releases
${_SwiftFoundation_BaseAvailability}
${_SwiftFoundation_BaseAvailability}
${_SwiftFoundation_macOS26Availability})

Expand Down
3 changes: 2 additions & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ import CompilerPluginSupport

let availabilityTags: [_Availability] = [
_Availability("FoundationPreview"), // Default FoundationPreview availability
_Availability("FoundationAttributedString"),
_Availability("FoundationSpan", availability: .macOS26), // Availability of Span types
]
let versionNumbers = ["6.0.2", "6.1", "6.2"]
let versionNumbers = ["5.5", "5.7", "5.8", "5.9", "6.0", "6.0.2", "6.1", "6.2"]

// Availability Macro Utilities

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
//===----------------------------------------------------------------------===//

@dynamicMemberLookup
@available(macOS 12, iOS 15, tvOS 15, watchOS 8, *)
@available(FoundationAttributedString 5.5, *)
public struct AttributeContainer : Sendable {
internal var storage : AttributedString._AttributeStorage

Expand All @@ -24,7 +24,7 @@ public struct AttributeContainer : Sendable {
}
}

@available(macOS 12, iOS 15, tvOS 15, watchOS 8, *)
@available(FoundationAttributedString 5.5, *)
extension AttributeContainer {
@preconcurrency
public subscript<T: AttributedStringKey>(_: T.Type) -> T.Value? where T.Value : Sendable {
Expand Down Expand Up @@ -87,26 +87,27 @@ extension AttributeContainer {
}
}

@available(macOS 12, iOS 15, tvOS 15, watchOS 8, *)
@available(FoundationAttributedString 5.5, *)
extension AttributeContainer: Equatable {}

@available(macOS 13, iOS 16, tvOS 16, watchOS 9, *)
@available(FoundationAttributedString 5.7, *)
extension AttributeContainer: Hashable {}

@available(macOS 12, iOS 15, tvOS 15, watchOS 8, *)
@available(FoundationAttributedString 5.5, *)
extension AttributeContainer: CustomStringConvertible {
public var description: String {
storage.description
}
}

@available(FoundationAttributedString 5.5, *)
extension AttributeContainer {
internal var _hasConstrainedAttributes: Bool {
storage.hasConstrainedAttributes
}
}

@available(FoundationPreview 6.2, *)
@available(FoundationAttributedString 6.2, *)
extension AttributeContainer {
/// Returns a copy of the attribute container with only attributes that specify the provided inheritance behavior.
/// - Parameter inheritedByAddedText: An `inheritedByAddedText` value to filter. Attributes matching this value are included in the returned container.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@
// var foregroundColor : ForegroundColor
// }
// An AttributeScope can contain other scopes as well.
@available(macOS 12, iOS 15, tvOS 15, watchOS 8, *)
@available(FoundationAttributedString 5.5, *)
public protocol AttributeScope : DecodingConfigurationProviding, EncodingConfigurationProviding {
static var decodingConfiguration: AttributeScopeCodableConfiguration { get }
static var encodingConfiguration: AttributeScopeCodableConfiguration { get }
}

@frozen
@available(macOS 12, iOS 15, tvOS 15, watchOS 8, *)
@available(FoundationAttributedString 5.5, *)
public enum AttributeScopes { }

@available(macOS, unavailable, introduced: 12.0)
Expand Down Expand Up @@ -171,7 +171,7 @@ internal func _loadDefaultAttributes() -> [String : any AttributedStringKey.Type
}

// TODO: Support AttributeScope key finding in FoundationPreview
@available(macOS 12, iOS 15, tvOS 15, watchOS 8, *)
@available(FoundationAttributedString 5.5, *)
extension AttributeScope {
private static var scopeDescription: ScopeDescription {
if let cached = _loadedScopeCache.withLock({ $0[Self.self] }) {
Expand Down Expand Up @@ -207,7 +207,7 @@ extension AttributeScope {
}

/// A list of all attribute keys contained within this scope and any sub-scopes.
@available(FoundationPreview 6.2, *)
@available(FoundationAttributedString 6.2, *)
public static var attributeKeys: some Sequence<any AttributedStringKey.Type> {
Self.scopeDescription.attributes.values
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ internal import _RopeModule
internal import _FoundationCollections
#endif

@available(macOS 12, iOS 15, tvOS 15, watchOS 8, *)
@available(FoundationAttributedString 5.5, *)
extension AttributedString {
@preconcurrency
public struct SingleAttributeTransformer<T: AttributedStringKey> : Sendable where T.Value : Sendable {
Expand All @@ -45,7 +45,7 @@ extension AttributedString {
}
}

@available(macOS 12, iOS 15, tvOS 15, watchOS 8, *)
@available(FoundationAttributedString 5.5, *)
extension AttributedString {
internal func applyRemovals<K>(
withOriginal orig: AttributedString.SingleAttributeTransformer<K>,
Expand Down Expand Up @@ -78,7 +78,7 @@ extension AttributedString {
}
}

@available(macOS 12, iOS 15, tvOS 15, watchOS 8, *)
@available(FoundationAttributedString 5.5, *)
extension AttributedString {
@preconcurrency
public func transformingAttributes<K>(
Expand Down Expand Up @@ -251,7 +251,7 @@ extension AttributedString {
}
}

@available(macOS 12, iOS 15, tvOS 15, watchOS 8, *)
@available(FoundationAttributedString 5.5, *)
extension AttributedString {
@preconcurrency
public func transformingAttributes<K>(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ internal import _RopeModule
internal import _FoundationCollections
#endif

@available(macOS 12, iOS 15, tvOS 15, watchOS 8, *)
@available(FoundationAttributedString 5.5, *)
extension AttributedString {
public struct CharacterView : Sendable {
/// The guts of the base attributed string.
Expand Down Expand Up @@ -80,13 +80,14 @@ extension AttributedString {
}
}

@available(macOS 12, iOS 15, tvOS 15, watchOS 8, *)
@available(FoundationAttributedString 5.5, *)
extension AttributedString.CharacterView {
internal var _characters: BigSubstring {
BigSubstring(_unchecked: _guts.string, in: _range)
}
}

@available(FoundationAttributedString 5.5, *)
extension Slice<AttributedString.CharacterView> {
internal var _rebased: AttributedString.CharacterView {
let bounds = Range(uncheckedBounds: (self.startIndex._value, self.endIndex._value))
Expand All @@ -100,7 +101,7 @@ extension Slice<AttributedString.CharacterView> {

// FIXME: AttributedString.CharacterView needs to publicly conform to Equatable & Hashable.

@available(macOS 12, iOS 15, tvOS 15, watchOS 8, *)
@available(FoundationAttributedString 5.5, *)
extension AttributedString.CharacterView: BidirectionalCollection {
public typealias Element = Character
public typealias Index = AttributedString.Index
Expand All @@ -123,7 +124,7 @@ extension AttributedString.CharacterView: BidirectionalCollection {
return _defaultCount
}

@available(macOS 14, iOS 17, tvOS 17, watchOS 10, *)
@available(FoundationAttributedString 5.9, *)
@usableFromInline
internal var _count: Int {
_characters.count
Expand Down Expand Up @@ -153,7 +154,7 @@ extension AttributedString.CharacterView: BidirectionalCollection {
return _defaultIndex(i, offsetBy: distance)
}

@available(macOS 14, iOS 17, tvOS 17, watchOS 10, *)
@available(FoundationAttributedString 5.9, *)
@usableFromInline
internal func _index(_ i: AttributedString.Index, offsetBy distance: Int) -> AttributedString.Index {
precondition(i >= startIndex && i <= endIndex, "AttributedString index out of bounds")
Expand All @@ -176,7 +177,7 @@ extension AttributedString.CharacterView: BidirectionalCollection {
return _defaultIndex(i, offsetBy: distance, limitedBy: limit)
}

@available(macOS 14, iOS 17, tvOS 17, watchOS 10, *)
@available(FoundationAttributedString 5.9, *)
@usableFromInline
internal func _index(
_ i: AttributedString.Index,
Expand Down Expand Up @@ -207,7 +208,7 @@ extension AttributedString.CharacterView: BidirectionalCollection {
return _defaultDistance(from: start, to: end)
}

@available(macOS 14, iOS 17, tvOS 17, watchOS 10, *)
@available(FoundationAttributedString 5.9, *)
@usableFromInline
internal func _distance(from start: AttributedString.Index, to end: AttributedString.Index) -> Int {
precondition(start >= startIndex && start <= endIndex, "AttributedString index out of bounds")
Expand Down Expand Up @@ -245,7 +246,7 @@ extension AttributedString.CharacterView: BidirectionalCollection {
}
}

@available(macOS 12, iOS 15, tvOS 15, watchOS 8, *)
@available(FoundationAttributedString 5.5, *)
extension AttributedString.CharacterView: RangeReplaceableCollection {
internal mutating func _ensureUniqueReference() {
if !isKnownUniquelyReferenced(&_guts) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ internal import _RopeModule
internal import _FoundationCollections
#endif

@available(FoundationAttributedString 5.5, *)
extension AttributedString {
internal final class Guts : @unchecked Sendable {
typealias Index = AttributedString.Index
Expand Down Expand Up @@ -54,6 +55,7 @@ extension AttributedString {
}
}

@available(FoundationAttributedString 5.5, *)
extension AttributedString.Guts {
__consuming func copy() -> AttributedString.Guts {
AttributedString.Guts(string: self.string, runs: self.runs)
Expand All @@ -73,6 +75,7 @@ extension AttributedString.Guts {
}
}

@available(FoundationAttributedString 5.5, *)
extension AttributedString.Guts {
internal static func characterwiseIsEqual(
_ left: AttributedString.Guts,
Expand Down Expand Up @@ -182,6 +185,7 @@ extension AttributedString.Guts {
}
}

@available(FoundationAttributedString 5.5, *)
extension AttributedString.Guts {
internal func description(in range: Range<BigString.Index>) -> String {
self.description(in: RangeSet(range))
Expand All @@ -199,6 +203,7 @@ extension AttributedString.Guts {
}
}

@available(FoundationAttributedString 5.5, *)
extension AttributedString.Guts {
var stringBounds: Range<BigString.Index> {
Range(uncheckedBounds: (string.startIndex, string.endIndex))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ internal import _FoundationCollections

// MARK: - Internal Index Updating

@available(FoundationAttributedString 5.5, *)
extension AttributedString.Guts {
func _prepareTrackedIndicesUpdate(mutationRange: Range<BigString.Index>) {
// Move any range endpoints inside of the mutation range to outside of the mutation range since a range should never end up splitting a mutation
Expand Down Expand Up @@ -75,7 +76,7 @@ extension AttributedString.Guts {

// MARK: - Public API

@available(FoundationPreview 6.2, *)
@available(FoundationAttributedString 6.2, *)
extension AttributedString {
// MARK: inout API

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
internal import Synchronization
#endif

@available(FoundationAttributedString 5.5, *)
extension AttributedString.Guts {
typealias Version = UInt

Expand Down Expand Up @@ -43,7 +44,7 @@ extension AttributedString.Guts {

// MARK: - Public API

@available(FoundationPreview 6.2, *)
@available(FoundationAttributedString 6.2, *)
extension AttributedString.Index {
/// Indicates whether the index is valid for use with the provided attributed string.
/// - Parameter text: An attributed string used to validate the index.
Expand All @@ -63,7 +64,7 @@ extension AttributedString.Index {
}
}

@available(FoundationPreview 6.2, *)
@available(FoundationAttributedString 6.2, *)
extension Range<AttributedString.Index> {
/// Indicates whether the range is valid for use with the provided attributed string.
/// - Parameter text: An attributed string used to validate the range.
Expand All @@ -88,7 +89,7 @@ extension Range<AttributedString.Index> {
}
}

@available(FoundationPreview 6.2, *)
@available(FoundationAttributedString 6.2, *)
extension RangeSet<AttributedString.Index> {
/// Indicates whether the range set is valid for use with the provided attributed string.
/// - Parameter text: An attributed string used to validate the range set.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
//
//===----------------------------------------------------------------------===//

@available(macOS 12, iOS 15, tvOS 15, watchOS 8, *)
@available(FoundationAttributedString 5.5, *)
extension AttributedString.Runs {
@preconcurrency
public struct AttributesSlice1<T : AttributedStringKey> : BidirectionalCollection, Sendable
Expand Down Expand Up @@ -142,7 +142,7 @@ extension AttributedString.Runs {
}
}

@available(macOS 12, iOS 15, tvOS 15, watchOS 8, *)
@available(FoundationAttributedString 5.5, *)
extension AttributedString.Runs {
@preconcurrency
public struct AttributesSlice2<
Expand Down Expand Up @@ -298,7 +298,7 @@ extension AttributedString.Runs {
}
}

@available(macOS 12, iOS 15, tvOS 15, watchOS 8, *)
@available(FoundationAttributedString 5.5, *)
extension AttributedString.Runs {
@preconcurrency
public struct AttributesSlice3<
Expand Down Expand Up @@ -466,7 +466,7 @@ extension AttributedString.Runs {
}
}

@available(macOS 12, iOS 15, tvOS 15, watchOS 8, *)
@available(FoundationAttributedString 5.5, *)
extension AttributedString.Runs {
@preconcurrency
public struct AttributesSlice4<
Expand Down Expand Up @@ -648,7 +648,7 @@ extension AttributedString.Runs {
}
}

@available(macOS 12, iOS 15, tvOS 15, watchOS 8, *)
@available(FoundationAttributedString 5.5, *)
extension AttributedString.Runs {
@preconcurrency
public struct AttributesSlice5<
Expand Down Expand Up @@ -846,7 +846,7 @@ extension AttributedString.Runs {

#if FOUNDATION_FRAMEWORK

@available(macOS 12, iOS 15, tvOS 15, watchOS 8, *)
@available(FoundationAttributedString 5.5, *)
extension AttributedString.Runs {
@_spi(AttributedString)
public struct NSAttributesSlice : BidirectionalCollection, Sendable {
Expand Down
Loading