diff --git a/app/src/processing/app/SketchCode.java b/app/src/processing/app/SketchCode.java index 59ed06652..e66fe7a88 100644 --- a/app/src/processing/app/SketchCode.java +++ b/app/src/processing/app/SketchCode.java @@ -25,6 +25,7 @@ package processing.app; import java.io.*; +import java.util.Stack; import javax.swing.text.BadLocationException; import javax.swing.text.Document; @@ -64,7 +65,15 @@ public class SketchCode { * Editor.undo will be set to this object when this code is the tab * that's currently the front. */ - private UndoManager undo = new UndoManager(); + private final UndoManager undo = new UndoManager(); + + /** + * Caret positions for this tab. + * Editor.caretUndoStack and Editor.caretRedoStack will be set to these + * when this code is the tab that's currently the front. + */ + private final Stack caretUndoStack = new Stack<>(); + private final Stack caretRedoStack = new Stack<>(); /** What was on top of the undo stack when last saved. */ // private UndoableEdit lastEdit; @@ -238,6 +247,13 @@ public class SketchCode { return undo; } + public Stack getCaretRedoStack() { + return caretRedoStack; + } + + public Stack getCaretUndoStack() { + return caretUndoStack; + } // . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . diff --git a/app/src/processing/app/ui/Editor.java b/app/src/processing/app/ui/Editor.java index 832be571b..9e4ce8cb8 100644 --- a/app/src/processing/app/ui/Editor.java +++ b/app/src/processing/app/ui/Editor.java @@ -138,8 +138,11 @@ public abstract class Editor extends JFrame implements RunnerListener { /** Menu Actions updated on the opening of the edit menu. */ protected List editMenuUpdatable = new ArrayList<>(); - /** The currently selected tab's undo manager */ + /** The currently selected tab's undo manager and caret positions*/ private UndoManager undo; + // maintain caret position during undo operations + private Stack caretUndoStack = new Stack<>(); + private Stack caretRedoStack = new Stack<>(); // used internally for every edit. Groups hotkey-event text manipulations and // groups multi-character inputs into a single undos. private CompoundEdit compoundEdit; @@ -148,9 +151,6 @@ public abstract class Editor extends JFrame implements RunnerListener { private TimerTask endUndoEvent; // true if inserting text, false if removing text private boolean isInserting; - // maintain caret position during undo operations - private final Stack caretUndoStack = new Stack<>(); - private final Stack caretRedoStack = new Stack<>(); private FindReplace find; JMenu toolsMenu; @@ -1904,7 +1904,12 @@ public abstract class Editor extends JFrame implements RunnerListener { // textarea.requestFocus(); // get the caret blinking textarea.requestFocusInWindow(); // required for caret blinking + // end edits in the previous tab + endTextEditHistory(); + // update the UndoManager and caret positions to the selected tab this.undo = code.getUndo(); + caretUndoStack = code.getCaretUndoStack(); + caretRedoStack = code.getCaretRedoStack(); undoAction.updateUndoState(); redoAction.updateRedoState(); }