From 424ff1b8df8173db8061bfcf58e8d6483a22711e Mon Sep 17 00:00:00 2001 From: Joel Moniz Date: Wed, 17 Feb 2016 01:31:08 -0500 Subject: [PATCH 1/3] Select and replace undoes in one step --- .../processing/app/syntax/JEditTextArea.java | 27 ++++++++++++++++--- app/src/processing/app/ui/Editor.java | 15 ++++++++++- 2 files changed, 38 insertions(+), 4 deletions(-) diff --git a/app/src/processing/app/syntax/JEditTextArea.java b/app/src/processing/app/syntax/JEditTextArea.java index 5fb7392db5..9cf2b3119c 100644 --- a/app/src/processing/app/syntax/JEditTextArea.java +++ b/app/src/processing/app/syntax/JEditTextArea.java @@ -1429,15 +1429,30 @@ public final String getSelectedText() } } + /** * Replaces the selection with the specified text. * @param selectedText The replacement text for the selection */ public void setSelectedText(String selectedText) { + setSelectedText(selectedText, false); + } + + + /** + * Replaces the selection with the specified text. + * @param selectedText The replacement text for the selection + * @param recordCompoundEdit Whether the replacement should be + * recorded as a compound edit + */ + public void setSelectedText(String selectedText, boolean recordCompoundEdit) { if (!editable) { throw new InternalError("Text component read only"); } - document.beginCompoundEdit(); + + if (recordCompoundEdit) { + document.beginCompoundEdit(); + } try { if (rectSelect) { @@ -1494,7 +1509,10 @@ public void setSelectedText(String selectedText) { } finally { // No matter what happens... stops us from leaving document in a bad state - document.endCompoundEdit(); + // (provided this has to be recorded as a compound edit, of course...) + if (recordCompoundEdit) { + document.endCompoundEdit(); + } } setCaretPosition(selectionEnd); } @@ -1566,7 +1584,10 @@ public void overwriteSetSelectedText(String str) // Don't overstrike if there is a selection if(!overwrite || selectionStart != selectionEnd) { - setSelectedText(str); + // record the whole operation as a compound edit if + // selected text is being replaced + boolean isSelectAndReplaceOp = (selectionStart != selectionEnd); + setSelectedText(str, isSelectAndReplaceOp); return; } diff --git a/app/src/processing/app/ui/Editor.java b/app/src/processing/app/ui/Editor.java index 47081b99e1..b6e55f707d 100644 --- a/app/src/processing/app/ui/Editor.java +++ b/app/src/processing/app/ui/Editor.java @@ -1711,7 +1711,20 @@ public void setCode(SketchCode code) { SyntaxDocument document = (SyntaxDocument) code.getDocument(); if (document == null) { // this document not yet inited - document = new SyntaxDocument(); + document = new SyntaxDocument() { + @Override + public void beginCompoundEdit() { + if (compoundEdit == null) + startCompoundEdit(); + super.beginCompoundEdit(); + } + + @Override + public void endCompoundEdit() { + stopCompoundEdit(); + super.endCompoundEdit(); + } + }; code.setDocument(document); // turn on syntax highlighting From 54863021217417048449bffdbb7dde6a94b07c28 Mon Sep 17 00:00:00 2001 From: Joel Moniz Date: Wed, 17 Feb 2016 01:34:57 -0500 Subject: [PATCH 2/3] Insert text now undoes smoothly --- app/src/processing/app/syntax/JEditTextArea.java | 8 +------- app/src/processing/app/ui/Editor.java | 11 +++++++---- 2 files changed, 8 insertions(+), 11 deletions(-) diff --git a/app/src/processing/app/syntax/JEditTextArea.java b/app/src/processing/app/syntax/JEditTextArea.java index 9cf2b3119c..f100c285dd 100644 --- a/app/src/processing/app/syntax/JEditTextArea.java +++ b/app/src/processing/app/syntax/JEditTextArea.java @@ -1597,12 +1597,10 @@ public void overwriteSetSelectedText(String str) int caretLineEnd = getLineStopOffset(getCaretLine()); if(caretLineEnd - caret <= str.length()) { - setSelectedText(str); + setSelectedText(str, false); return; } - document.beginCompoundEdit(); - try { document.remove(caret,str.length()); @@ -1612,10 +1610,6 @@ public void overwriteSetSelectedText(String str) { bl.printStackTrace(); } - finally - { - document.endCompoundEdit(); - } } /** diff --git a/app/src/processing/app/ui/Editor.java b/app/src/processing/app/ui/Editor.java index b6e55f707d..29857018d6 100644 --- a/app/src/processing/app/ui/Editor.java +++ b/app/src/processing/app/ui/Editor.java @@ -297,7 +297,7 @@ public BasicSplitPaneDivider createDefaultDivider() { String lastText = textarea.getText(); public void caretUpdate(CaretEvent e) { String newText = textarea.getText(); - if (lastText.equals(newText) && isDirectEdit()) { + if (lastText.equals(newText) && isDirectEdit() && !textarea.isOverwriteEnabled()) { endTextEditHistory(); } lastText = newText; @@ -1743,17 +1743,20 @@ public void endCompoundEdit() { document.addDocumentListener(new DocumentListener() { public void removeUpdate(DocumentEvent e) { - if (isInserting && isDirectEdit()) { + if (isInserting && isDirectEdit() && !textarea.isOverwriteEnabled()) { endTextEditHistory(); } isInserting = false; } public void insertUpdate(DocumentEvent e) { - if (!isInserting && isDirectEdit()) { + if (!isInserting && !textarea.isOverwriteEnabled() && isDirectEdit()) { endTextEditHistory(); } - isInserting = true; + + if (!textarea.isOverwriteEnabled()) { + isInserting = true; + } } public void changedUpdate(DocumentEvent e) { From b8910b1a8cdfd36322e6e7baa845a66222614074 Mon Sep 17 00:00:00 2001 From: Joel Moniz Date: Wed, 17 Feb 2016 01:36:50 -0500 Subject: [PATCH 3/3] Replace All now undoes in one shot --- .../processing/app/syntax/JEditTextArea.java | 1 - app/src/processing/app/ui/Editor.java | 5 ++++ app/src/processing/app/ui/FindReplace.java | 25 +++++++++++++++---- 3 files changed, 25 insertions(+), 6 deletions(-) diff --git a/app/src/processing/app/syntax/JEditTextArea.java b/app/src/processing/app/syntax/JEditTextArea.java index f100c285dd..6d936b2e5d 100644 --- a/app/src/processing/app/syntax/JEditTextArea.java +++ b/app/src/processing/app/syntax/JEditTextArea.java @@ -1429,7 +1429,6 @@ public final String getSelectedText() } } - /** * Replaces the selection with the specified text. * @param selectedText The replacement text for the selection diff --git a/app/src/processing/app/ui/Editor.java b/app/src/processing/app/ui/Editor.java index 29857018d6..3343b5b21f 100644 --- a/app/src/processing/app/ui/Editor.java +++ b/app/src/processing/app/ui/Editor.java @@ -1572,6 +1572,11 @@ public void setSelectedText(String what) { } + public void setSelectedText(String what, boolean ever) { + textarea.setSelectedText(what, ever); + } + + public void setSelection(int start, int stop) { // make sure that a tool isn't asking for a bad location start = PApplet.constrain(start, 0, textarea.getDocumentLength()); diff --git a/app/src/processing/app/ui/FindReplace.java b/app/src/processing/app/ui/FindReplace.java index cf4ae60477..b9e9409cac 100644 --- a/app/src/processing/app/ui/FindReplace.java +++ b/app/src/processing/app/ui/FindReplace.java @@ -417,15 +417,27 @@ protected void setFound(boolean found) { replaceAndFindButton.setEnabled(found); } +/** + * Replace the current selection with whatever's in the + * replacement text field. + * @param isCompoundEdit True if the action is to be marked as a copmound edit + */ + public void replace(boolean isCompoundEdit) { + editor.setSelectedText(replaceField.getText(), isCompoundEdit); + + editor.getSketch().setModified(true); // This necessary- calling replace() + // doesn't seem to mark a sketch as modified + + setFound(false); + } + /** * Replace the current selection with whatever's in the - * replacement text field. + * replacement text field, marking the action as a compound edit. */ public void replace() { - editor.setSelectedText(replaceField.getText()); - editor.getSketch().setModified(true); // TODO is this necessary? - setFound(false); + replace(true); } @@ -451,6 +463,8 @@ public void replaceAll() { int startTab = -1; int startIndex = -1; int counter = 10000; // prevent infinite loop + + editor.startCompoundEdit(); while (--counter > 0) { if (find(false, false)) { int caret = editor.getSelectionStart(); @@ -467,11 +481,12 @@ public void replaceAll() { startTab = editor.getSketch().getCurrentCodeIndex(); startIndex = editor.getSelectionStart(); } - replace(); + replace(false); } else { break; } } + editor.stopCompoundEdit(); if (!foundAtLeastOne) { Toolkit.beep(); }