Skip to content

Commit ffdcc3e

Browse files
authored
Still trying to find the balance with redo/undo with external vs internal editors (#503)
1 parent 5af6000 commit ffdcc3e

File tree

1 file changed

+11
-6
lines changed

1 file changed

+11
-6
lines changed

cq_editor/widgets/editor.py

+11-6
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@ class Editor(CodeEditor, ComponentMixin):
5151

5252
EXTENSIONS = "py"
5353

54+
# Tracks whether or not the document was saved from the Spyder editor vs an external editor
55+
was_modified_by_self = False
56+
5457
def __init__(self, parent=None):
5558

5659
self._watched_file = None
@@ -198,6 +201,8 @@ def save(self):
198201
# Let the editor and the rest of the app know that the file is no longer dirty
199202
self.reset_modified()
200203

204+
self.was_modified_by_self = True
205+
201206
else:
202207
self.save_as()
203208

@@ -269,9 +274,6 @@ def _file_changed(self):
269274
vertical_scroll_pos = self.verticalScrollBar().value()
270275
horizontal_scroll_pos = self.horizontalScrollBar().value()
271276

272-
# Save undo stack before reloading text
273-
undo_stack = self.document().isUndoAvailable()
274-
275277
# Block signals to avoid reset issues
276278
self.blockSignals(True)
277279

@@ -290,9 +292,12 @@ def _file_changed(self):
290292
# Stop blocking signals
291293
self.blockSignals(False)
292294

293-
# Restore undo stack availability
294-
if undo_stack:
295-
self.document().setModified(True)
295+
self.document().setModified(True)
296+
297+
# Undo has to be backed up one step to compensate for the text insertion
298+
if self.was_modified_by_self:
299+
self.document().undo()
300+
self.was_modified_by_self = False
296301

297302
# Restore the cursor position and selection
298303
cursor.setPosition(anchor_position)

0 commit comments

Comments
 (0)