Skip to content

Commit a456c0b

Browse files
authored
(151882766) Fixup documentation for new AttributedString APIs (#1316)
1 parent 1eed9aa commit a456c0b

7 files changed

+75
-18
lines changed

Sources/FoundationEssentials/AttributedString/AttributeContainer.swift

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,9 @@ extension AttributeContainer {
108108

109109
@available(FoundationPreview 6.2, *)
110110
extension AttributeContainer {
111-
/// Returns an attribute container storing only the attributes in `self` with the `inheritedByAddedText` property set to `true`
111+
/// Returns a copy of the attribute container with only attributes that specify the provided inheritance behavior.
112+
/// - Parameter inheritedByAddedText: An `inheritedByAddedText` value to filter. Attributes matching this value are included in the returned container.
113+
/// - Returns: A copy of the attribute container with only attributes whose `inheritedByAddedText` property matches the provided value.
112114
public func filter(inheritedByAddedText: Bool) -> AttributeContainer {
113115
var storage = self.storage
114116
for (key, value) in storage.contents {
@@ -120,9 +122,9 @@ extension AttributeContainer {
120122
return AttributeContainer(storage)
121123
}
122124

123-
/// Returns an attribute container storing only the attributes in `self` with a matching run boundary property
124-
///
125-
/// Note: if `nil` is provided then only attributes not bound to any particular boundary will be returned
125+
/// Returns a copy of the attribute container with only attributes that have the provided run boundaries.
126+
/// - Parameter runBoundaries: The required `runBoundaries` value of the filtered attributes. If `nil` is provided, only attributes not bound to any specific boundary will be returned.
127+
/// - Returns: A copy of the attribute container with only attributes whose `runBoundaries` property matches the provided value.
126128
public func filter(runBoundaries: AttributedString.AttributeRunBoundaries?) -> AttributeContainer {
127129
var storage = self.storage
128130
for (key, value) in storage.contents {

Sources/FoundationEssentials/AttributedString/AttributeScope.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,7 @@ extension AttributeScope {
206206
Self.scopeDescription.markdownAttributes
207207
}
208208

209+
/// A list of all attribute keys contained within this scope and any sub-scopes.
209210
@available(FoundationPreview 6.2, *)
210211
public static var attributeKeys: some Sequence<any AttributedStringKey.Type> {
211212
Self.scopeDescription.attributes.values

Sources/FoundationEssentials/AttributedString/AttributedString+IndexTracking.swift

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -79,21 +79,25 @@ extension AttributedString.Guts {
7979
extension AttributedString {
8080
// MARK: inout API
8181

82-
/// Tracks the location of the provided range throughout the mutation closure, updating the provided range to one that represents the same effective locations after the mutation. If updating the provided range is not possible (tracking failed) then this function will fatal error. Use the Optional-returning variants to provide custom fallback behavior.
82+
/// Tracks the location of the provided range throughout the mutation closure, updating the provided range to one that represents the same effective locations after the mutation.
83+
///
84+
/// If updating the provided range is not possible (tracking failed) then this function will fatal error. Use the `Optional`-returning variants to provide custom fallback behavior.
8385
/// - Parameters:
84-
/// - range: a range to track throughout the `body` closure
85-
/// - body: a mutating operation, or set of operations, to perform on the value of `self`. The value of `self` is provided to the closure as an `inout AttributedString` that the closure should mutate directly. Do not capture the value of `self` in the provided closure - the closure should mutate the provided `inout` copy.
86+
/// - range: A range to track throughout the `body` closure.
87+
/// - body: A mutating operation, or set of operations, to perform on the value of `self`. The value of `self` is provided to the closure as an `inout AttributedString` that the closure should mutate directly. Do not capture the value of `self` in the provided closure - the closure should mutate the provided `inout` copy.
8688
public mutating func transform<E>(updating range: inout Range<Index>, body: (inout AttributedString) throws(E) -> Void) throws(E) -> Void {
8789
guard let result = try self.transform(updating: range, body: body) else {
8890
fatalError("The provided mutation body did not allow for maintaining index tracking. Ensure that your mutation body mutates the provided AttributedString instead of replacing it with a different AttributedString or use the non-inout version of transform(updating:body:) which returns an Optional value to provide fallback behavior.")
8991
}
9092
range = result
9193
}
9294

93-
/// Tracks the location of the provided ranges throughout the mutation closure, updating them to new ranges that represent the same effective locations after the mutation. If updating the provided ranges is not possible (tracking failed) then this function will fatal error. Use the Optional-returning variants to provide custom fallback behavior.
95+
/// Tracks the location of the provided ranges throughout the mutation closure, updating them to new ranges that represent the same effective locations after the mutation.
96+
///
97+
/// If updating the provided ranges is not possible (tracking failed) then this function will fatal error. Use the `Optional`-returning variants to provide custom fallback behavior.
9498
/// - Parameters:
95-
/// - ranges: a list of ranges to track throughout the `body` closure. The updated array (after the function is called) is guaranteed to be the same size as the provided array. Updated ranges are located at the same indices as their respective original ranges in the input `ranges` array.
96-
/// - body: a mutating operation, or set of operations, to perform on the value of `self`. The value of `self` is provided to the closure as an `inout AttributedString` that the closure should mutate directly. Do not capture the value of `self` in the provided closure - the closure should mutate the provided `inout` copy.
99+
/// - ranges: A list of ranges to track throughout the `body` closure. The updated array (after the function is called) is guaranteed to be the same size as the provided array. Updated ranges are located at the same indices as their respective original ranges in the input `ranges` array.
100+
/// - body: A mutating operation, or set of operations, to perform on the value of `self`. The value of `self` is provided to the closure as an `inout AttributedString` that the closure should mutate directly. Do not capture the value of `self` in the provided closure - the closure should mutate the provided `inout` copy.
97101
public mutating func transform<E>(updating ranges: inout [Range<Index>], body: (inout AttributedString) throws(E) -> Void) throws(E) -> Void {
98102
guard let result = try self.transform(updating: ranges, body: body) else {
99103
fatalError("The provided mutation body did not allow for maintaining index tracking. Ensure that your mutation body mutates the provided AttributedString instead of replacing it with a different AttributedString or use the non-inout version of transform(updating:body:) which returns an Optional value to provide fallback behavior.")
@@ -103,20 +107,20 @@ extension AttributedString {
103107

104108
// MARK: Optional-returning API
105109

106-
/// Tracks the location of the provided range throughout the mutation closure, returning a new, updated range that represents the same effective locations after the mutation
110+
/// Tracks the location of the provided range throughout the mutation closure, returning a new, updated range that represents the same effective locations after the mutation.
107111
/// - Parameters:
108-
/// - range: a range to track throughout the `mutation` block
109-
/// - mutation: a mutating operation, or set of operations, to perform on this `AttributedString`
110-
/// - Returns: the updated `Range` that is valid after the mutation has been performed, or `nil` if the mutation performed does not allow for tracking to succeed (such as replacing the provided inout variable with an entirely different AttributedString)
112+
/// - range: A range to track throughout the `body` block.
113+
/// - body: A mutating operation, or set of operations, to perform on this `AttributedString`.
114+
/// - Returns: the updated `Range` that is valid after the mutation has been performed, or `nil` if the mutation performed does not allow for tracking to succeed (such as replacing the provided inout variable with an entirely different `AttributedString`).
111115
public mutating func transform<E>(updating range: Range<Index>, body: (inout AttributedString) throws(E) -> Void) throws(E) -> Range<Index>? {
112116
try self.transform(updating: [range], body: body)?.first
113117
}
114118

115119
/// Tracks the location of the provided ranges throughout the mutation closure, returning a new, updated range that represents the same effective locations after the mutation
116120
/// - Parameters:
117-
/// - index: an index to track throughout the `mutation` block
118-
/// - mutation: a mutating operation, or set of operations, to perform on this `AttributedString`
119-
/// - Returns: the updated `Range`s that is valid after the mutation has been performed, or `nil` if the mutation performed does not allow for tracking to succeed (such as replacing the provided inout variable with an entirely different AttributedString). When the return value is non-nil, the returned array is guaranteed to be the same size as the provided array with updated ranges at the same Array indices as their respective original ranges in the input array.
121+
/// - ranges: Ranges to track throughout the `body` block.
122+
/// - body: A mutating operation, or set of operations, to perform on this `AttributedString`.
123+
/// - Returns: the updated `Range`s that are valid after the mutation has been performed or `nil` if the mutation performed does not allow for tracking to succeed (such as replacing the provided inout variable with an entirely different `AttributedString`). When the return value is non-`nil`, the returned array is guaranteed to be the same size as the provided array with updated ranges at the same indices as their respective original ranges in the input array.
120124
public mutating func transform<E>(updating ranges: [Range<Index>], body: (inout AttributedString) throws(E) -> Void) throws(E) -> [Range<Index>]? {
121125
precondition(!ranges.isEmpty, "Cannot update an empty array of ranges")
122126

Sources/FoundationEssentials/AttributedString/AttributedString+IndexValidity.swift

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,18 @@ extension AttributedString.Guts {
4545

4646
@available(FoundationPreview 6.2, *)
4747
extension AttributedString.Index {
48+
/// Indicates whether the index is valid for use with the provided attributed string.
49+
/// - Parameter text: An attributed string used to validate the index.
50+
/// - Returns: `true` when the index is valid for use with the provided attributed string; otherwise, false. An index is valid if it is both within the bounds of the attributed string and was produced from the provided string without any intermediate mutations.
4851
public func isValid(within text: some AttributedStringProtocol) -> Bool {
4952
self._version == text.__guts.version &&
5053
self >= text.startIndex &&
5154
self < text.endIndex
5255
}
5356

57+
/// Indicates whether the index is valid for use with the provided discontiguous attributed string.
58+
/// - Parameter text: A discontiguous attributed string used to validate the index.
59+
/// - Returns: `true` when the index is valid for use with the provided discontiguous attributed string; otherwise, false. An index is valid if it is both within the bounds of the discontigous attributed string and was produced from the provided string without any intermediate mutations.
5460
public func isValid(within text: DiscontiguousAttributedSubstring) -> Bool {
5561
self._version == text._guts.version &&
5662
text._indices.contains(self._value)
@@ -59,6 +65,9 @@ extension AttributedString.Index {
5965

6066
@available(FoundationPreview 6.2, *)
6167
extension Range<AttributedString.Index> {
68+
/// Indicates whether the range is valid for use with the provided attributed string.
69+
/// - Parameter text: An attributed string used to validate the range.
70+
/// - Returns: `true` when the range is valid for use with the provided attributed string; otherwise, false. A range is valid if its lower and upper bounds are each either valid in the attributed string or equivalent to the string's `endIndex`.
6271
public func isValid(within text: some AttributedStringProtocol) -> Bool {
6372
// Note: By nature of Range's lowerBound <= upperBound requirement, this is also sufficient to determine that lowerBound <= endIndex && upperBound >= startIndex
6473
self.lowerBound._version == text.__guts.version &&
@@ -67,6 +76,9 @@ extension Range<AttributedString.Index> {
6776
self.upperBound <= text.endIndex
6877
}
6978

79+
/// Indicates whether the range is valid for use with the provided discontiguous attributed string.
80+
/// - Parameter text: A discontiguous attributed string used to validate the range.
81+
/// - Returns: `true` when the range is valid for use with the provided discontiguous attributed string; otherwise, false. A range is valid if its lower and upper bounds are each either valid in the discontiguous attributed string or equivalent to the string's `endIndex`.
7082
public func isValid(within text: DiscontiguousAttributedSubstring) -> Bool {
7183
let endIndex = text._indices.ranges.last?.upperBound
7284
return self.lowerBound._version == text._guts.version &&
@@ -78,12 +90,18 @@ extension Range<AttributedString.Index> {
7890

7991
@available(FoundationPreview 6.2, *)
8092
extension RangeSet<AttributedString.Index> {
93+
/// Indicates whether the range set is valid for use with the provided attributed string.
94+
/// - Parameter text: An attributed string used to validate the range set.
95+
/// - Returns: `true` when the range set is valid for use with the provided attributed string; otherwise, false. A range set is valid if each of its ranges are valid in the attributed string.
8196
public func isValid(within text: some AttributedStringProtocol) -> Bool {
8297
self.ranges.allSatisfy {
8398
$0.isValid(within: text)
8499
}
85100
}
86101

102+
/// Indicates whether the range set is valid for use with the provided discontiguous attributed string.
103+
/// - Parameter text: A discontigious attributed string used to validate the range set.
104+
/// - Returns: `true` when the range set is valid for use with the provided discontiguous attributed string; otherwise, false. A range set is valid if each of its ranges are valid in the discontiguous attributed string.
87105
public func isValid(within text: DiscontiguousAttributedSubstring) -> Bool {
88106
self.ranges.allSatisfy {
89107
$0.isValid(within: text)

Sources/FoundationEssentials/AttributedString/AttributedString+UTF16View.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ internal import _FoundationCollections
2020

2121
@available(FoundationPreview 6.2, *)
2222
extension AttributedString {
23+
/// A view of an attributed string’s contents as a collection of UTF-16 code units.
2324
public struct UTF16View: Sendable {
2425
internal var _guts: Guts
2526
internal var _range: Range<BigString.Index>
@@ -35,13 +36,15 @@ extension AttributedString {
3536
}
3637
}
3738

39+
/// A view of the attributed string’s contents as a collection of UTF-16 code units.
3840
public var utf16: UTF16View {
3941
UTF16View(_guts)
4042
}
4143
}
4244

4345
@available(FoundationPreview 6.2, *)
4446
extension AttributedSubstring {
47+
/// A view of the attributed substring's contents as a collection of UTF-16 code units.
4548
public var utf16: AttributedString.UTF16View {
4649
AttributedString.UTF16View(_guts, in: _range)
4750
}

Sources/FoundationEssentials/AttributedString/AttributedString+UTF8View.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ internal import _FoundationCollections
2020

2121
@available(FoundationPreview 6.2, *)
2222
extension AttributedString {
23+
/// A view of an attributed string’s contents as a collection of UTF-8 code units.
2324
public struct UTF8View: Sendable {
2425
internal var _guts: Guts
2526
internal var _range: Range<BigString.Index>
@@ -34,14 +35,16 @@ extension AttributedString {
3435
_range = range
3536
}
3637
}
37-
38+
39+
/// A view of the attributed string’s contents as a collection of UTF-8 code units.
3840
public var utf8: UTF8View {
3941
UTF8View(_guts)
4042
}
4143
}
4244

4345
@available(FoundationPreview 6.2, *)
4446
extension AttributedSubstring {
47+
/// A view of the attributed substring's contents as a collection of UTF-8 code units.
4548
public var utf8: AttributedString.UTF8View {
4649
AttributedString.UTF8View(_guts, in: _range)
4750
}

0 commit comments

Comments
 (0)