@@ -13,16 +13,16 @@ import Foundation
1313/// If two attachments are overlapping, the one placed further along in the document will be
1414/// ignored when laying out attachments.
1515public final class TextAttachmentManager {
16- private var orderedAttachments : [ TextAttachmentBox ] = [ ]
16+ private var orderedAttachments : [ AnyTextAttachment ] = [ ]
1717 weak var layoutManager : TextLayoutManager ?
1818
19- /// Adds a new attachment box , keeping `orderedAttachments` sorted by range.location.
19+ /// Adds a new attachment, keeping `orderedAttachments` sorted by range.location.
2020 /// If two attachments overlap, the layout phase will later ignore the one with the higher start.
2121 /// - Complexity: `O(n log(n))` due to array insertion. Could be improved with a binary tree.
2222 public func add( _ attachment: any TextAttachment , for range: NSRange ) {
23- let box = TextAttachmentBox ( range: range, attachment: attachment)
23+ let attachment = AnyTextAttachment ( range: range, attachment: attachment)
2424 let insertIndex = findInsertionIndex ( for: range. location)
25- orderedAttachments. insert ( box , at: insertIndex)
25+ orderedAttachments. insert ( attachment , at: insertIndex)
2626 layoutManager? . lineStorage. linesInRange ( range) . dropFirst ( ) . forEach {
2727 if $0. height != 0 {
2828 layoutManager? . lineStorage. update ( atOffset: $0. range. location, delta: 0 , deltaHeight: - $0. height)
@@ -46,20 +46,20 @@ public final class TextAttachmentManager {
4646 /// Finds attachments starting in the given line range, and returns them as an array.
4747 /// Returned attachment's ranges will be relative to the _document_, not the line.
4848 /// - Complexity: `O(n log(n))`, ideally `O(log(n))`
49- public func get( startingIn range: NSRange ) -> [ TextAttachmentBox ] {
50- var results : [ TextAttachmentBox ] = [ ]
49+ public func get( startingIn range: NSRange ) -> [ AnyTextAttachment ] {
50+ var results : [ AnyTextAttachment ] = [ ]
5151 var idx = findInsertionIndex ( for: range. location)
5252 while idx < orderedAttachments. count {
53- let box = orderedAttachments [ idx]
54- let loc = box . range. location
53+ let attachment = orderedAttachments [ idx]
54+ let loc = attachment . range. location
5555 if loc >= range. upperBound {
5656 break
5757 }
5858 if range. contains ( loc) {
59- if let lastResult = results. last, !lastResult. range. contains ( box . range. location) {
60- results. append ( box )
59+ if let lastResult = results. last, !lastResult. range. contains ( attachment . range. location) {
60+ results. append ( attachment )
6161 } else if results. isEmpty {
62- results. append ( box )
62+ results. append ( attachment )
6363 }
6464 }
6565 idx += 1
@@ -70,25 +70,25 @@ public final class TextAttachmentManager {
7070 /// Returns all attachments whose ranges overlap the given query range.
7171 ///
7272 /// - Parameter query: The `NSRange` to test for overlap.
73- /// - Returns: An array of `TextAttachmentBox ` instances whose ranges intersect `query`.
74- public func get( overlapping query: NSRange ) -> [ TextAttachmentBox ] {
73+ /// - Returns: An array of `AnyTextAttachment ` instances whose ranges intersect `query`.
74+ public func get( overlapping query: NSRange ) -> [ AnyTextAttachment ] {
7575 // Find the first attachment whose end is beyond the start of the query.
7676 guard let startIdx = firstIndex ( where: { $0. range. upperBound > query. location } ) else {
7777 return [ ]
7878 }
7979
80- var results : [ TextAttachmentBox ] = [ ]
80+ var results : [ AnyTextAttachment ] = [ ]
8181 var idx = startIdx
8282
8383 // Collect every subsequent attachment that truly overlaps the query.
8484 while idx < orderedAttachments. count {
85- let box = orderedAttachments [ idx]
86- if box . range. location >= query. upperBound {
85+ let attachment = orderedAttachments [ idx]
86+ if attachment . range. location >= query. upperBound {
8787 break
8888 }
89- if NSIntersectionRange ( box . range, query) . length > 0 ,
90- results. last? . range != box . range {
91- results. append ( box )
89+ if NSIntersectionRange ( attachment . range, query) . length > 0 ,
90+ results. last? . range != attachment . range {
91+ results. append ( attachment )
9292 }
9393 idx += 1
9494 }
@@ -97,10 +97,10 @@ public final class TextAttachmentManager {
9797 }
9898
9999 package func textUpdated( atOffset: Int , delta: Int ) {
100- for (idx, box ) in orderedAttachments. enumerated ( ) . reversed ( ) {
101- if box . range. contains ( atOffset) {
100+ for (idx, attachment ) in orderedAttachments. enumerated ( ) . reversed ( ) {
101+ if attachment . range. contains ( atOffset) {
102102 orderedAttachments. remove ( at: idx)
103- } else if box . range. location > atOffset {
103+ } else if attachment . range. location > atOffset {
104104 orderedAttachments [ idx] . range. location += delta
105105 }
106106 }
@@ -115,7 +115,7 @@ private extension TextAttachmentManager {
115115 /// If it returns `orderedAttachments.count`, no element satisfied
116116 /// the predicate, but that’s still a valid insertion point.
117117 func lowerBoundIndex(
118- where predicate: ( TextAttachmentBox ) -> Bool
118+ where predicate: ( AnyTextAttachment ) -> Bool
119119 ) -> Int {
120120 var low = 0
121121 var high = orderedAttachments. count
@@ -144,7 +144,7 @@ private extension TextAttachmentManager {
144144 /// - Parameter predicate: the query predicate.
145145 /// - Returns: the first matching index, or `nil` if none of the
146146 /// attachments satisfy the predicate.
147- func firstIndex( where predicate: ( TextAttachmentBox ) -> Bool ) -> Int ? {
147+ func firstIndex( where predicate: ( AnyTextAttachment ) -> Bool ) -> Int ? {
148148 let idx = lowerBoundIndex { predicate ( $0) }
149149 return idx < orderedAttachments. count ? idx : nil
150150 }
0 commit comments