import Foundation import OrgCore /// A range of a file to read rather than edit: its style runs as segments, with the brackets /// and targets of described links hidden (`org-link-descriptive`) and folded drawers shown as /// their first line and `…`, as the editor shows them. public enum ReadingText { public struct Segment: Sendable, Equatable { public var text: String public var roles: Set /// The link this text belongs to. public var link: OrgLink? /// Where the text starts in the file. public var offset: Int } /// `folded` are drawers' and blocks' hidden ranges (`Wrapper.hidden`). public static func segments(_ tree: OrgTree, in range: Range, folded: [Range] = []) -> [Segment] { let text = tree.text as NSString let runs = Presentation.styles(tree, in: range) // A fold hides from the end of the opening line through the closing line, so the // marker sits on the opening line. let folds = folded.compactMap { r -> Range? in guard r.lowerBound > range.lowerBound, r.lowerBound <= range.upperBound else { return nil } let end = r.upperBound > r.lowerBound && r.upperBound <= text.length && text.character(at: r.upperBound - 1) == 10 ? r.upperBound - 1 : r.upperBound return (r.lowerBound - 1)..