Skip to content

Commit 0bcf878

Browse files
committed
Merge pull request #4310 from joelmoniz/bugfixes-undo
Fix a few undo-related bugs
2 parents 2ff61db + b8910b1 commit 0bcf878

3 files changed

Lines changed: 70 additions & 20 deletions

File tree

app/src/processing/app/syntax/JEditTextArea.java

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1436,10 +1436,24 @@ public final String getSelectedText()
14361436
* @param selectedText The replacement text for the selection
14371437
*/
14381438
public void setSelectedText(String selectedText) {
1439+
setSelectedText(selectedText, false);
1440+
}
1441+
1442+
1443+
/**
1444+
* Replaces the selection with the specified text.
1445+
* @param selectedText The replacement text for the selection
1446+
* @param recordCompoundEdit Whether the replacement should be
1447+
* recorded as a compound edit
1448+
*/
1449+
public void setSelectedText(String selectedText, boolean recordCompoundEdit) {
14391450
if (!editable) {
14401451
throw new InternalError("Text component read only");
14411452
}
1442-
document.beginCompoundEdit();
1453+
1454+
if (recordCompoundEdit) {
1455+
document.beginCompoundEdit();
1456+
}
14431457

14441458
try {
14451459
if (rectSelect) {
@@ -1496,7 +1510,10 @@ public void setSelectedText(String selectedText) {
14961510

14971511
} finally {
14981512
// No matter what happens... stops us from leaving document in a bad state
1499-
document.endCompoundEdit();
1513+
// (provided this has to be recorded as a compound edit, of course...)
1514+
if (recordCompoundEdit) {
1515+
document.endCompoundEdit();
1516+
}
15001517
}
15011518
setCaretPosition(selectionEnd);
15021519
}
@@ -1568,7 +1585,10 @@ public void overwriteSetSelectedText(String str)
15681585
// Don't overstrike if there is a selection
15691586
if(!overwrite || selectionStart != selectionEnd)
15701587
{
1571-
setSelectedText(str);
1588+
// record the whole operation as a compound edit if
1589+
// selected text is being replaced
1590+
boolean isSelectAndReplaceOp = (selectionStart != selectionEnd);
1591+
setSelectedText(str, isSelectAndReplaceOp);
15721592
return;
15731593
}
15741594

@@ -1578,12 +1598,10 @@ public void overwriteSetSelectedText(String str)
15781598
int caretLineEnd = getLineStopOffset(getCaretLine());
15791599
if(caretLineEnd - caret <= str.length())
15801600
{
1581-
setSelectedText(str);
1601+
setSelectedText(str, false);
15821602
return;
15831603
}
15841604

1585-
document.beginCompoundEdit();
1586-
15871605
try
15881606
{
15891607
document.remove(caret,str.length());
@@ -1593,10 +1611,6 @@ public void overwriteSetSelectedText(String str)
15931611
{
15941612
bl.printStackTrace();
15951613
}
1596-
finally
1597-
{
1598-
document.endCompoundEdit();
1599-
}
16001614
}
16011615

16021616
/**

app/src/processing/app/ui/Editor.java

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,7 @@ public BasicSplitPaneDivider createDefaultDivider() {
297297
String lastText = textarea.getText();
298298
public void caretUpdate(CaretEvent e) {
299299
String newText = textarea.getText();
300-
if (lastText.equals(newText) && isDirectEdit()) {
300+
if (lastText.equals(newText) && isDirectEdit() && !textarea.isOverwriteEnabled()) {
301301
endTextEditHistory();
302302
}
303303
lastText = newText;
@@ -1581,6 +1581,11 @@ public void setSelectedText(String what) {
15811581
}
15821582

15831583

1584+
public void setSelectedText(String what, boolean ever) {
1585+
textarea.setSelectedText(what, ever);
1586+
}
1587+
1588+
15841589
public void setSelection(int start, int stop) {
15851590
// make sure that a tool isn't asking for a bad location
15861591
start = PApplet.constrain(start, 0, textarea.getDocumentLength());
@@ -1720,7 +1725,20 @@ public void setCode(SketchCode code) {
17201725
SyntaxDocument document = (SyntaxDocument) code.getDocument();
17211726

17221727
if (document == null) { // this document not yet inited
1723-
document = new SyntaxDocument();
1728+
document = new SyntaxDocument() {
1729+
@Override
1730+
public void beginCompoundEdit() {
1731+
if (compoundEdit == null)
1732+
startCompoundEdit();
1733+
super.beginCompoundEdit();
1734+
}
1735+
1736+
@Override
1737+
public void endCompoundEdit() {
1738+
stopCompoundEdit();
1739+
super.endCompoundEdit();
1740+
}
1741+
};
17241742
code.setDocument(document);
17251743

17261744
// turn on syntax highlighting
@@ -1739,17 +1757,20 @@ public void setCode(SketchCode code) {
17391757
document.addDocumentListener(new DocumentListener() {
17401758

17411759
public void removeUpdate(DocumentEvent e) {
1742-
if (isInserting && isDirectEdit()) {
1760+
if (isInserting && isDirectEdit() && !textarea.isOverwriteEnabled()) {
17431761
endTextEditHistory();
17441762
}
17451763
isInserting = false;
17461764
}
17471765

17481766
public void insertUpdate(DocumentEvent e) {
1749-
if (!isInserting && isDirectEdit()) {
1767+
if (!isInserting && !textarea.isOverwriteEnabled() && isDirectEdit()) {
17501768
endTextEditHistory();
17511769
}
1752-
isInserting = true;
1770+
1771+
if (!textarea.isOverwriteEnabled()) {
1772+
isInserting = true;
1773+
}
17531774
}
17541775

17551776
public void changedUpdate(DocumentEvent e) {

app/src/processing/app/ui/FindReplace.java

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -417,15 +417,27 @@ protected void setFound(boolean found) {
417417
replaceAndFindButton.setEnabled(found);
418418
}
419419

420+
/**
421+
* Replace the current selection with whatever's in the
422+
* replacement text field.
423+
* @param isCompoundEdit True if the action is to be marked as a copmound edit
424+
*/
425+
public void replace(boolean isCompoundEdit) {
426+
editor.setSelectedText(replaceField.getText(), isCompoundEdit);
427+
428+
editor.getSketch().setModified(true); // This necessary- calling replace()
429+
// doesn't seem to mark a sketch as modified
430+
431+
setFound(false);
432+
}
433+
420434

421435
/**
422436
* Replace the current selection with whatever's in the
423-
* replacement text field.
437+
* replacement text field, marking the action as a compound edit.
424438
*/
425439
public void replace() {
426-
editor.setSelectedText(replaceField.getText());
427-
editor.getSketch().setModified(true); // TODO is this necessary?
428-
setFound(false);
440+
replace(true);
429441
}
430442

431443

@@ -451,6 +463,8 @@ public void replaceAll() {
451463
int startTab = -1;
452464
int startIndex = -1;
453465
int counter = 10000; // prevent infinite loop
466+
467+
editor.startCompoundEdit();
454468
while (--counter > 0) {
455469
if (find(false, false)) {
456470
int caret = editor.getSelectionStart();
@@ -467,11 +481,12 @@ public void replaceAll() {
467481
startTab = editor.getSketch().getCurrentCodeIndex();
468482
startIndex = editor.getSelectionStart();
469483
}
470-
replace();
484+
replace(false);
471485
} else {
472486
break;
473487
}
474488
}
489+
editor.stopCompoundEdit();
475490
if (!foundAtLeastOne) {
476491
Toolkit.beep();
477492
}

0 commit comments

Comments
 (0)