Skip to content

Commit 2163fae

Browse files
committed
Rename Similar Method Names For Clarity
1 parent ef4e68f commit 2163fae

File tree

4 files changed

+22
-13
lines changed

4 files changed

+22
-13
lines changed

Sources/CodeEditTextView/TextLayoutManager/TextAttachments/TextAttachmentManager.swift

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,22 +31,26 @@ public final class TextAttachmentManager {
3131
layoutManager?.setNeedsLayout()
3232
}
3333

34-
public func remove(atOffset offset: Int) {
34+
/// Removes an attachment and invalidates layout for the removed range.
35+
/// - Parameter offset: The offset the attachment begins at.
36+
/// - Returns: The removed attachment, if it exists.
37+
@discardableResult
38+
public func remove(atOffset offset: Int) -> AnyTextAttachment? {
3539
let index = findInsertionIndex(for: offset)
3640

3741
guard index < orderedAttachments.count && orderedAttachments[index].range.location == offset else {
38-
assertionFailure("No attachment found at offset \(offset)")
39-
return
42+
return nil
4043
}
4144

4245
let attachment = orderedAttachments.remove(at: index)
4346
layoutManager?.invalidateLayoutForRange(attachment.range)
47+
return attachment
4448
}
4549

4650
/// Finds attachments starting in the given line range, and returns them as an array.
4751
/// Returned attachment's ranges will be relative to the _document_, not the line.
4852
/// - Complexity: `O(n log(n))`, ideally `O(log(n))`
49-
public func get(startingIn range: NSRange) -> [AnyTextAttachment] {
53+
public func getAttachmentsStartingIn(_ range: NSRange) -> [AnyTextAttachment] {
5054
var results: [AnyTextAttachment] = []
5155
var idx = findInsertionIndex(for: range.location)
5256
while idx < orderedAttachments.count {
@@ -69,11 +73,11 @@ public final class TextAttachmentManager {
6973

7074
/// Returns all attachments whose ranges overlap the given query range.
7175
///
72-
/// - Parameter query: The `NSRange` to test for overlap.
76+
/// - Parameter range: The `NSRange` to test for overlap.
7377
/// - Returns: An array of `AnyTextAttachment` instances whose ranges intersect `query`.
74-
public func get(overlapping query: NSRange) -> [AnyTextAttachment] {
78+
public func getAttachmentsOverlapping(_ range: NSRange) -> [AnyTextAttachment] {
7579
// Find the first attachment whose end is beyond the start of the query.
76-
guard let startIdx = firstIndex(where: { $0.range.upperBound > query.location }) else {
80+
guard let startIdx = firstIndex(where: { $0.range.upperBound > range.location }) else {
7781
return []
7882
}
7983

@@ -83,10 +87,10 @@ public final class TextAttachmentManager {
8387
// Collect every subsequent attachment that truly overlaps the query.
8488
while idx < orderedAttachments.count {
8589
let attachment = orderedAttachments[idx]
86-
if attachment.range.location >= query.upperBound {
90+
if attachment.range.location >= range.upperBound {
8791
break
8892
}
89-
if NSIntersectionRange(attachment.range, query).length > 0,
93+
if attachment.range.intersection(range)?.length ?? 0 > 0,
9094
results.last?.range != attachment.range {
9195
results.append(attachment)
9296
}
@@ -96,6 +100,11 @@ public final class TextAttachmentManager {
96100
return results
97101
}
98102

103+
/// Updates the text attachments to stay in the same relative spot after the edit, and removes any attachments that
104+
/// were in the updated range.
105+
/// - Parameters:
106+
/// - atOffset: The offset text was updated at.
107+
/// - delta: The change delta, positive is an insertion.
99108
package func textUpdated(atOffset: Int, delta: Int) {
100109
for (idx, attachment) in orderedAttachments.enumerated().reversed() {
101110
if attachment.range.contains(atOffset) {

Sources/CodeEditTextView/TextLayoutManager/TextLayoutManager+Iterator.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ public extension TextLayoutManager {
173173
return originalPosition
174174
}
175175

176-
let attachments = attachments.get(overlapping: originalPosition.position.range)
176+
let attachments = attachments.getAttachmentsOverlapping(originalPosition.position.range)
177177
guard let firstAttachment = attachments.first, let lastAttachment = attachments.last else {
178178
// No change, either no attachments or attachment doesn't span multiple lines.
179179
return originalPosition

Sources/CodeEditTextView/TextLayoutManager/TextLayoutManager+Layout.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -193,15 +193,15 @@ extension TextLayoutManager {
193193
range: position.range,
194194
stringRef: textStorage,
195195
markedRanges: markedTextManager.markedRanges(in: position.range),
196-
attachments: attachments.get(startingIn: position.range)
196+
attachments: attachments.getAttachmentsStartingIn(position.range)
197197
)
198198
} else {
199199
line.prepareForDisplay(
200200
displayData: lineDisplayData,
201201
range: position.range,
202202
stringRef: textStorage,
203203
markedRanges: markedTextManager.markedRanges(in: position.range),
204-
attachments: attachments.get(startingIn: position.range)
204+
attachments: attachments.getAttachmentsStartingIn(position.range)
205205
)
206206
}
207207

Sources/CodeEditTextView/TextSelectionManager/SelectionManipulation/TextSelectionManager+SelectionManipulation.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public extension TextSelectionManager {
5959
}
6060

6161
// Extend ranges to include attachments.
62-
if let attachments = layoutManager?.attachments.get(overlapping: range) {
62+
if let attachments = layoutManager?.attachments.getAttachmentsOverlapping(range) {
6363
attachments.forEach { textAttachment in
6464
range.formUnion(textAttachment.range)
6565
}

0 commit comments

Comments
 (0)