Skip to content

Commit 1e9a73e

Browse files
represent selection restoration setting as optional
1 parent adf02f1 commit 1e9a73e

1 file changed

Lines changed: 9 additions & 12 deletions

File tree

Sources/DeclarativeTextKit/Buffer/Undoable.swift

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -139,16 +139,15 @@ public final class Undoable<Base>: Buffer where Base: Buffer {
139139
else { preconditionFailure("Undoable buffer used without UndoManager") }
140140

141141
let oldContent = try base.content(in: deletedRange)
142-
let oldSelection = base.selectedRange
143-
let oldSelectionRestoration = self.isRestoringSelection
142+
let oldSelection = self.isRestoringSelection ? base.selectedRange : nil
144143

145144
try base.delete(in: deletedRange)
146145

147146
undoManager.beginUndoGrouping()
148147
undoManager.registerUndo(withTarget: self) { undoableBuffer in
149148
try? undoableBuffer.insert(oldContent, at: deletedRange.location)
150-
if oldSelectionRestoration {
151-
undoableBuffer.withSelectionRestoration(oldSelectionRestoration) {
149+
if let oldSelection {
150+
undoableBuffer.withSelectionRestoration(true) {
152151
undoableBuffer.select(oldSelection)
153152
}
154153
}
@@ -161,17 +160,16 @@ public final class Undoable<Base>: Buffer where Base: Buffer {
161160
else { preconditionFailure("Undoable buffer used without UndoManager") }
162161

163162
let oldContent = try base.content(in: replacementRange)
164-
let oldSelection = base.selectedRange
165-
let oldSelectionRestoration = self.isRestoringSelection
163+
let oldSelection = self.isRestoringSelection ? base.selectedRange : nil
166164

167165
try base.replace(range: replacementRange, with: content)
168166

169167
let newRange = Buffer.Range(location: replacementRange.location, length: length(of: content))
170168
undoManager.beginUndoGrouping()
171169
undoManager.registerUndo(withTarget: self) { undoableBuffer in
172170
try? undoableBuffer.replace(range: newRange, with: oldContent)
173-
if oldSelectionRestoration {
174-
undoableBuffer.withSelectionRestoration(oldSelectionRestoration) {
171+
if let oldSelection {
172+
undoableBuffer.withSelectionRestoration(true) {
175173
undoableBuffer.select(oldSelection)
176174
}
177175
}
@@ -183,17 +181,16 @@ public final class Undoable<Base>: Buffer where Base: Buffer {
183181
guard let undoManager
184182
else { preconditionFailure("Undoable buffer used without UndoManager") }
185183

186-
let oldSelection = base.selectedRange
187-
let oldSelectionRestoration = self.isRestoringSelection
184+
let oldSelection = self.isRestoringSelection ? base.selectedRange : nil
188185

189186
try base.insert(content, at: location)
190187

191188
let newRange = Buffer.Range(location: location, length: length(of: content))
192189
undoManager.beginUndoGrouping()
193190
undoManager.registerUndo(withTarget: self) { undoableBuffer in
194191
try? undoableBuffer.delete(in: newRange)
195-
if oldSelectionRestoration {
196-
undoableBuffer.withSelectionRestoration(oldSelectionRestoration) {
192+
if let oldSelection {
193+
undoableBuffer.withSelectionRestoration(true) {
197194
undoableBuffer.select(oldSelection)
198195
}
199196
}

0 commit comments

Comments
 (0)