Skip to content

Commit 7bacce8

Browse files
committed
Update Logic Slightly
1 parent 25b7072 commit 7bacce8

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

Sources/CodeEditTextView/Utils/CEUndoManager.swift

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -122,10 +122,14 @@ public class CEUndoManager: UndoManager {
122122
/// should be one continuous range. This merges those ranges into a set of disjoint ranges before updating the
123123
/// selection manager.
124124
private func updateSelectionsForMutations(mutations: [TextMutation]) {
125-
if mutations.reduce(0, { $0 + $1.range.length }) == 0, let last = mutations.last {
126-
// If the mutations are only deleting text (no replacement), we just place the cursor at the last range,
127-
// since all the ranges are the same but the other method will return no ranges (empty range).
128-
textView?.selectionManager.setSelectedRange(last.range)
125+
if mutations.reduce(0, { $0 + $1.range.length }) == 0 {
126+
if let minimumMutation = mutations.min(by: { $0.range.location < $1.range.location }) {
127+
// If the mutations are only deleting text (no replacement), we just place the cursor at the last range,
128+
// since all the ranges are the same but the other method will return no ranges (empty range).
129+
textView?.selectionManager.setSelectedRange(
130+
NSRange(location: minimumMutation.range.location, length: 0)
131+
)
132+
}
129133
} else {
130134
let mergedRanges = mutations.reduce(into: IndexSet(), { set, mutation in
131135
set.insert(range: mutation.range)

0 commit comments

Comments
 (0)