Skip to content

Implement list item delimiter attribute #1272

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

Merged
merged 1 commit into from
May 5, 2025
Merged
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 @@ -57,6 +57,8 @@ extension AttributeScopes {
public let presentationIntent: PresentationIntentAttribute
@available(macOS 13, iOS 16, tvOS 16, watchOS 9, *)
public let markdownSourcePosition: MarkdownSourcePositionAttribute
@available(FoundationPreview 6.2, *)
public let listItemDelimiter: ListItemDelimiterAttribute

@available(macOS 13, iOS 16, tvOS 16, watchOS 9, *)
public let localizedStringArgumentAttributes: LocalizedStringArgumentAttributes
Expand Down Expand Up @@ -440,6 +442,44 @@ extension AttributeScopes.FoundationAttributes {
public typealias Value = AttributedString.MarkdownSourcePosition
}

@frozen
@available(FoundationPreview 6.2, *)
public enum ListItemDelimiterAttribute : CodableAttributedStringKey, ObjectiveCConvertibleAttributedStringKey {
public typealias Value = Character
public typealias ObjectiveCValue = NSString

public static let name = NSAttributedString.Key.listItemDelimiter.rawValue

public static func objectiveCValue(for value: Character) throws -> NSString {
String(value) as NSString
}

public static func value(for object: NSString) throws -> Character {
let stringValue = object as String
guard stringValue.count == 1 else {
throw CocoaError(.coderInvalidValue)
}
return stringValue[stringValue.startIndex]
}

public static func encode(_ value: Character, to encoder: any Encoder) throws {
var container = encoder.singleValueContainer()
try container.encode(String(value))
}

public static func decode(from decoder: any Decoder) throws -> Character {
let container = try decoder.singleValueContainer()
let text = try container.decode(String.self)
guard text.count == 1 else {
throw DecodingError.dataCorrupted(DecodingError.Context(
codingPath: container.codingPath,
debugDescription: "List item delimeter encoded value must contain only one character / grapheme cluster"
))
}
return text[text.startIndex]
}
}

#endif // FOUNDATION_FRAMEWORK

@frozen
Expand Down