Skip to content

Commit

Permalink
Misc UndoManager Fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
thecoolwinter committed Feb 24, 2024
1 parent 6653c21 commit dd7590b
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 10 deletions.
10 changes: 0 additions & 10 deletions Sources/CodeEditTextView/TextView/TextView+ReplaceCharacters.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,6 @@ extension TextView {
layoutManager.beginTransaction()
textStorage.beginEditing()

var shouldEndGrouping = false
if !(_undoManager?.isGrouping ?? false) {
_undoManager?.beginGrouping()
shouldEndGrouping = true
}

// Can't insert an empty string into an empty range. One must be not empty
for range in ranges.sorted(by: { $0.location > $1.location }) where
(!range.isEmpty || !string.isEmpty) &&
Expand All @@ -46,10 +40,6 @@ extension TextView {
delegate?.textView(self, didReplaceContentsIn: range, with: string)
}

if shouldEndGrouping {
_undoManager?.endGrouping()
}

layoutManager.endTransaction()
textStorage.endEditing()
selectionManager.notifyAfterEdit()
Expand Down
7 changes: 7 additions & 0 deletions Sources/CodeEditTextView/Utils/CEUndoManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ public class CEUndoManager {
public class DelegatedUndoManager: UndoManager {
weak var parent: CEUndoManager?

public override var isUndoing: Bool { parent?.isUndoing ?? false }
public override var isRedoing: Bool { parent?.isRedoing ?? false }
public override var canUndo: Bool { parent?.canUndo ?? false }
public override var canRedo: Bool { parent?.canRedo ?? false }

Expand Down Expand Up @@ -97,9 +99,11 @@ public class CEUndoManager {
}
isUndoing = true
NotificationCenter.default.post(name: .NSUndoManagerWillUndoChange, object: self.manager)
textView.textStorage.beginEditing()
for mutation in item.mutations.reversed() {
textView.replaceCharacters(in: mutation.inverse.range, with: mutation.inverse.string)
}
textView.textStorage.endEditing()
NotificationCenter.default.post(name: .NSUndoManagerDidUndoChange, object: self.manager)
redoStack.append(item)
isUndoing = false
Expand All @@ -112,9 +116,11 @@ public class CEUndoManager {
}
isRedoing = true
NotificationCenter.default.post(name: .NSUndoManagerWillRedoChange, object: self.manager)
textView.textStorage.beginEditing()
for mutation in item.mutations {
textView.replaceCharacters(in: mutation.mutation.range, with: mutation.mutation.string)
}
textView.textStorage.endEditing()
NotificationCenter.default.post(name: .NSUndoManagerDidRedoChange, object: self.manager)
undoStack.append(item)
isRedoing = false
Expand Down Expand Up @@ -188,6 +194,7 @@ public class CEUndoManager {
/// - lastMutation: The last mutation applied to the document.
/// - Returns: Whether or not the given mutations can be grouped.
private func shouldContinueGroup(_ mutation: Mutation, lastMutation: Mutation) -> Bool {
return false
// If last mutation was delete & new is insert or vice versa, split group
if (mutation.mutation.range.length > 0 && lastMutation.mutation.range.length == 0)
|| (mutation.mutation.range.length == 0 && lastMutation.mutation.range.length > 0) {
Expand Down

0 comments on commit dd7590b

Please sign in to comment.