Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 24 additions & 10 deletions app/src/processing/app/syntax/JEditTextArea.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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;
}

Expand All @@ -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());
Expand All @@ -1591,10 +1609,6 @@ public void overwriteSetSelectedText(String str)
{
bl.printStackTrace();
}
finally
{
document.endCompoundEdit();
}
}

/**
Expand Down
31 changes: 26 additions & 5 deletions app/src/processing/app/ui/Editor.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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
Expand All @@ -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) {
Expand Down
25 changes: 20 additions & 5 deletions app/src/processing/app/ui/FindReplace.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}


Expand All @@ -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();
Expand All @@ -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();
}
Expand Down