Skip to content
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

AttributedString Scope Enumeration #1093

Merged
merged 1 commit into from
Jan 25, 2025
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ internal func _loadDefaultAttributes() -> [String : any AttributedStringKey.Type

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

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

static func markdownKeyTypes() -> [String : any MarkdownDecodableAttributedStringKey.Type] {
internal static func markdownKeyTypes() -> [String : any MarkdownDecodableAttributedStringKey.Type] {
Self.scopeDescription.markdownAttributes
}

@available(FoundationPreview 6.2, *)
public static var attributeKeys: some Sequence<any AttributedStringKey.Type> {
Self.scopeDescription.attributes.values
}
}

#endif // FOUNDATION_FRAMEWORK
Original file line number Diff line number Diff line change
Expand Up @@ -2427,6 +2427,22 @@ E {

XCTAssertEqual(AttributedString(str[range], including: None.self), AttributedString("BC"))
}

func testScopeIterationAPI() {
struct TestScope : AttributeScope {
let testInt: AttributeScopes.TestAttributes.TestIntAttribute
let testBool: AttributeScopes.TestAttributes.TestBoolAttribute
}

let testNames = TestScope.attributeKeys.map { $0.name }.sorted()
XCTAssertEqual(testNames, [AttributeScopes.TestAttributes.TestBoolAttribute.name, AttributeScopes.TestAttributes.TestIntAttribute.name].sorted())

struct EmptyScope : AttributeScope {

}
var emptyIterator = EmptyScope.attributeKeys.makeIterator()
XCTAssertNil(emptyIterator.next())
}
#endif // FOUNDATION_FRAMEWORK

func testAssignDifferentSubstring() {
Expand Down