diff --git a/app/src/processing/app/syntax/JEditTextArea.java b/app/src/processing/app/syntax/JEditTextArea.java index 5fb7392db5..6d936b2e5d 100644 --- a/app/src/processing/app/syntax/JEditTextArea.java +++ b/app/src/processing/app/syntax/JEditTextArea.java @@ -1434,10 +1434,24 @@ public final String getSelectedText() * @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 +1508,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 +1583,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; } @@ -1576,12 +1596,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()); @@ -1591,10 +1609,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 47081b99e1..3343b5b21f 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; @@ -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()); @@ -1711,7 +1716,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 @@ -1730,17 +1748,20 @@ public void setCode(SketchCode code) { 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) { 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(); }