Skip to content

Commit 8558978

Browse files
authored
(140368780) Enable client iteration of AttributeScopes (#1093)
1 parent 0e6c827 commit 8558978

File tree

2 files changed

+24
-4
lines changed

2 files changed

+24
-4
lines changed

Sources/FoundationEssentials/AttributedString/AttributeScope.swift

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ internal func _loadDefaultAttributes() -> [String : any AttributedStringKey.Type
172172

173173
// TODO: Support AttributeScope key finding in FoundationPreview
174174
@available(macOS 12, iOS 15, tvOS 15, watchOS 8, *)
175-
internal extension AttributeScope {
175+
extension AttributeScope {
176176
private static var scopeDescription: ScopeDescription {
177177
if let cached = _loadedScopeCache.withLock({ $0[Self.self] }) {
178178
return cached
@@ -198,13 +198,17 @@ internal extension AttributeScope {
198198
return desc
199199
}
200200

201-
static func attributeKeyTypes() -> [String : any AttributedStringKey.Type] {
201+
internal static func attributeKeyTypes() -> [String : any AttributedStringKey.Type] {
202202
Self.scopeDescription.attributes
203203
}
204204

205-
static func markdownKeyTypes() -> [String : any MarkdownDecodableAttributedStringKey.Type] {
205+
internal static func markdownKeyTypes() -> [String : any MarkdownDecodableAttributedStringKey.Type] {
206206
Self.scopeDescription.markdownAttributes
207207
}
208+
209+
@available(FoundationPreview 6.2, *)
210+
public static var attributeKeys: some Sequence<any AttributedStringKey.Type> {
211+
Self.scopeDescription.attributes.values
212+
}
208213
}
209-
210214
#endif // FOUNDATION_FRAMEWORK

Tests/FoundationEssentialsTests/AttributedString/AttributedStringTests.swift

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2427,6 +2427,22 @@ E {
24272427

24282428
XCTAssertEqual(AttributedString(str[range], including: None.self), AttributedString("BC"))
24292429
}
2430+
2431+
func testScopeIterationAPI() {
2432+
struct TestScope : AttributeScope {
2433+
let testInt: AttributeScopes.TestAttributes.TestIntAttribute
2434+
let testBool: AttributeScopes.TestAttributes.TestBoolAttribute
2435+
}
2436+
2437+
let testNames = TestScope.attributeKeys.map { $0.name }.sorted()
2438+
XCTAssertEqual(testNames, [AttributeScopes.TestAttributes.TestBoolAttribute.name, AttributeScopes.TestAttributes.TestIntAttribute.name].sorted())
2439+
2440+
struct EmptyScope : AttributeScope {
2441+
2442+
}
2443+
var emptyIterator = EmptyScope.attributeKeys.makeIterator()
2444+
XCTAssertNil(emptyIterator.next())
2445+
}
24302446
#endif // FOUNDATION_FRAMEWORK
24312447

24322448
func testAssignDifferentSubstring() {

0 commit comments

Comments
 (0)