Skip to content

Commit c3953aa

Browse files
author
Federico Fissore
committed
Autoformat was generating two undo action, the first one being an empty editor. Tested and fixed
1 parent 73972fe commit c3953aa

File tree

2 files changed

+58
-0
lines changed

2 files changed

+58
-0
lines changed

app/src/cc/arduino/packages/formatter/AStyle.java

+2
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,10 @@ public void run() {
9292
int line = getLineOfOffset(textArea);
9393
int lineOffset = getLineOffset(textArea, line);
9494

95+
editor.getTextArea().getUndoManager().beginInternalAtomicEdit();
9596
editor.setText(formattedText);
9697
editor.getSketch().setModified(true);
98+
editor.getTextArea().getUndoManager().endInternalAtomicEdit();
9799

98100
if (line != -1 && lineOffset != -1) {
99101
setCaretPosition(textArea, line, lineOffset);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package processing.app;
2+
3+
import org.fest.swing.fixture.JMenuItemFixture;
4+
import org.junit.Test;
5+
import processing.app.helpers.RSyntaxTextAreaFixture;
6+
7+
import static org.junit.Assert.assertEquals;
8+
import static org.junit.Assert.assertNotEquals;
9+
10+
public class AutoformatProducesOneUndoActionTest extends AbstractGUITest {
11+
12+
public static final String SOURCE_BEFORE = "void setup() {\n" +
13+
" // put your setup code here, to run once:\n" +
14+
"\n" +
15+
"}\n" +
16+
"\n" +
17+
"void loop() {\n" +
18+
" // put your main code here, to run repeatedly:\n" +
19+
"\n" +
20+
"}";
21+
public static final String SOURCE_AFTER = "void setup() {\n" +
22+
" // put your setup code here, to run once:\n" +
23+
"\n" +
24+
"}\n" +
25+
"\n" +
26+
"void loop() {\n" +
27+
" // put your main code here, to run repeatedly:\n" +
28+
"\n" +
29+
"}";
30+
31+
@Test
32+
public void shouldSaveCaretPositionAfterAutoformat() {
33+
JMenuItemFixture menuEditUndo = window.menuItem("menuEditUndo");
34+
menuEditUndo.requireDisabled();
35+
36+
JMenuItemFixture menuToolsAutoFormat = window.menuItem("menuToolsAutoFormat");
37+
menuToolsAutoFormat.requireEnabled();
38+
39+
RSyntaxTextAreaFixture editor = window.RSyntaxTextArea("editor");
40+
editor.setText(SOURCE_BEFORE);
41+
42+
editor.setCaretPosition(29); // right before the first // (double slash)
43+
44+
menuToolsAutoFormat.click();
45+
46+
String formattedText = editor.getText();
47+
assertEquals(SOURCE_AFTER, formattedText);
48+
49+
assertEquals(29, editor.getCaretPosition());
50+
51+
menuEditUndo.requireEnabled();
52+
menuEditUndo.click();
53+
assertEquals(SOURCE_BEFORE, editor.getText());
54+
}
55+
56+
}

0 commit comments

Comments
 (0)