@@ -51,6 +51,9 @@ class Editor(CodeEditor, ComponentMixin):
51
51
52
52
EXTENSIONS = "py"
53
53
54
+ # Tracks whether or not the document was saved from the Spyder editor vs an external editor
55
+ was_modified_by_self = False
56
+
54
57
def __init__ (self , parent = None ):
55
58
56
59
self ._watched_file = None
@@ -198,6 +201,8 @@ def save(self):
198
201
# Let the editor and the rest of the app know that the file is no longer dirty
199
202
self .reset_modified ()
200
203
204
+ self .was_modified_by_self = True
205
+
201
206
else :
202
207
self .save_as ()
203
208
@@ -269,9 +274,6 @@ def _file_changed(self):
269
274
vertical_scroll_pos = self .verticalScrollBar ().value ()
270
275
horizontal_scroll_pos = self .horizontalScrollBar ().value ()
271
276
272
- # Save undo stack before reloading text
273
- undo_stack = self .document ().isUndoAvailable ()
274
-
275
277
# Block signals to avoid reset issues
276
278
self .blockSignals (True )
277
279
@@ -290,9 +292,12 @@ def _file_changed(self):
290
292
# Stop blocking signals
291
293
self .blockSignals (False )
292
294
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
296
301
297
302
# Restore the cursor position and selection
298
303
cursor .setPosition (anchor_position )
0 commit comments