Skip to content

Commit b43306c

Browse files
committed
Rename Box to Any
1 parent 61bb469 commit b43306c

File tree

9 files changed

+43
-38
lines changed

9 files changed

+43
-38
lines changed

Sources/CodeEditTextView/TextLayoutManager/TextAttachments/TextAttachment.swift

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,24 @@
77

88
import AppKit
99

10-
public struct TextAttachmentBox: Equatable {
10+
/// Type-erasing type for ``TextAttachment`` that also contains range information about the attachment.
11+
///
12+
/// This type cannot be initialized outside of `CodeEditTextView`, but will be received when interrogating
13+
/// the ``TextAttachmentManager``.
14+
public struct AnyTextAttachment: Equatable {
1115
var range: NSRange
1216
let attachment: any TextAttachment
1317

1418
var width: CGFloat {
1519
attachment.width
1620
}
1721

18-
public static func == (_ lhs: TextAttachmentBox, _ rhs: TextAttachmentBox) -> Bool {
22+
public static func == (_ lhs: AnyTextAttachment, _ rhs: AnyTextAttachment) -> Bool {
1923
lhs.range == rhs.range && lhs.attachment === rhs.attachment
2024
}
2125
}
2226

27+
/// Represents an attachment type. Attachments take up some set width, and draw their contents in a receiver view.
2328
public protocol TextAttachment: AnyObject {
2429
var width: CGFloat { get }
2530
func draw(in context: CGContext, rect: NSRect)

Sources/CodeEditTextView/TextLayoutManager/TextAttachments/TextAttachmentManager.swift

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -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.
1515
public 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
}

Sources/CodeEditTextView/TextLayoutManager/TextLayoutManagerRenderDelegate.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public protocol TextLayoutManagerRenderDelegate: AnyObject {
1818
range: NSRange,
1919
stringRef: NSTextStorage,
2020
markedRanges: MarkedRanges?,
21-
attachments: [TextAttachmentBox]
21+
attachments: [AnyTextAttachment]
2222
)
2323

2424
func estimatedLineHeight() -> CGFloat?
@@ -35,7 +35,7 @@ public extension TextLayoutManagerRenderDelegate {
3535
range: NSRange,
3636
stringRef: NSTextStorage,
3737
markedRanges: MarkedRanges?,
38-
attachments: [TextAttachmentBox]
38+
attachments: [AnyTextAttachment]
3939
) {
4040
textLine.prepareForDisplay(
4141
displayData: displayData,

Sources/CodeEditTextView/TextLine/LineFragment.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public final class LineFragment: Identifiable, Equatable {
1414
public struct FragmentContent: Equatable {
1515
public enum Content: Equatable {
1616
case text(line: CTLine)
17-
case attachment(attachment: TextAttachmentBox)
17+
case attachment(attachment: AnyTextAttachment)
1818
}
1919

2020
let data: Content

Sources/CodeEditTextView/TextLine/TextLine.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public final class TextLine: Identifiable, Equatable {
5454
range: NSRange,
5555
stringRef: NSTextStorage,
5656
markedRanges: MarkedRanges?,
57-
attachments: [TextAttachmentBox]
57+
attachments: [AnyTextAttachment]
5858
) {
5959
let string = stringRef.attributedSubstring(from: range)
6060
self.maxWidth = displayData.maxWidth

Sources/CodeEditTextView/TextLine/Typesetter/TypesetContext.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ struct TypesetContext {
1919
/// Tracks the current position when laying out runs
2020
var currentPosition: Int = 0
2121

22-
mutating func appendAttachment(_ attachment: TextAttachmentBox) {
22+
mutating func appendAttachment(_ attachment: AnyTextAttachment) {
2323
// Check if we can append this attachment to the current line
2424
if fragmentContext.width + attachment.width > displayData.maxWidth {
2525
popCurrentData()

Sources/CodeEditTextView/TextLine/Typesetter/Typesetter.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ final public class Typesetter {
1414
let type: RunType
1515

1616
enum RunType {
17-
case attachment(TextAttachmentBox)
17+
case attachment(AnyTextAttachment)
1818
case string(CTTypesetter)
1919
}
2020
}
@@ -32,7 +32,7 @@ final public class Typesetter {
3232
documentRange: NSRange,
3333
displayData: TextLine.DisplayData,
3434
markedRanges: MarkedRanges?,
35-
attachments: [TextAttachmentBox] = []
35+
attachments: [AnyTextAttachment] = []
3636
) {
3737
makeString(string: string, markedRanges: markedRanges)
3838
lineFragments.removeAll()
@@ -69,7 +69,7 @@ final public class Typesetter {
6969
/// - documentRange: The range in the string reference.
7070
/// - attachments: Any text attachments overlapping the string reference.
7171
/// - Returns: A series of content runs making up this line.
72-
func createContentRuns(documentRange: NSRange, attachments: [TextAttachmentBox]) -> [ContentRun] {
72+
func createContentRuns(documentRange: NSRange, attachments: [AnyTextAttachment]) -> [ContentRun] {
7373
var attachments = attachments
7474
var currentPosition = 0
7575
let maxPosition = documentRange.length
@@ -118,7 +118,7 @@ final public class Typesetter {
118118
func typesetLineFragments(
119119
documentRange: NSRange,
120120
displayData: TextLine.DisplayData,
121-
attachments: [TextAttachmentBox]
121+
attachments: [AnyTextAttachment]
122122
) -> (lines: [TextLineStorage<LineFragment>.BuildItem], maxHeight: CGFloat) {
123123
let contentRuns = createContentRuns(documentRange: documentRange, attachments: attachments)
124124
var context = TypesetContext(documentRange: documentRange, displayData: displayData)

Tests/CodeEditTextViewTests/LayoutManager/OverridingLayoutManagerRenderingTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class MockRenderDelegate: TextLayoutManagerRenderDelegate {
1919
range: NSRange,
2020
stringRef: NSTextStorage,
2121
markedRanges: MarkedRanges?,
22-
attachments: [TextAttachmentBox]
22+
attachments: [AnyTextAttachment]
2323
) {
2424
prepareForDisplay?(
2525
textLine,

Tests/CodeEditTextViewTests/TypesetterTests.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ class TypesetterTests: XCTestCase {
156156
breakStrategy: .character
157157
),
158158
markedRanges: nil,
159-
attachments: [TextAttachmentBox(range: NSRange(location: 1, length: 1), attachment: attachment)]
159+
attachments: [AnyTextAttachment(range: NSRange(location: 1, length: 1), attachment: attachment)]
160160
)
161161

162162
XCTAssertEqual(typesetter.lineFragments.count, 1)
@@ -186,7 +186,7 @@ class TypesetterTests: XCTestCase {
186186
breakStrategy: .character
187187
),
188188
markedRanges: nil,
189-
attachments: [TextAttachmentBox(range: NSRange(location: 0, length: 3), attachment: attachment)]
189+
attachments: [AnyTextAttachment(range: NSRange(location: 0, length: 3), attachment: attachment)]
190190
)
191191

192192
XCTAssertEqual(typesetter.lineFragments.count, 1)

0 commit comments

Comments
 (0)