From f8aecd0f9c098de54e50844ab918efcb3438d335 Mon Sep 17 00:00:00 2001 From: Federico Fissore Date: Mon, 27 Apr 2015 18:47:47 +0200 Subject: [PATCH] Autoformat was generating two undo action, the first one being an empty editor. Tested and fixed --- .../cc/arduino/packages/formatter/AStyle.java | 2 + .../AutoformatProducesOneUndoActionTest.java | 56 +++++++++++++++++++ 2 files changed, 58 insertions(+) create mode 100644 app/test/processing/app/AutoformatProducesOneUndoActionTest.java diff --git a/app/src/cc/arduino/packages/formatter/AStyle.java b/app/src/cc/arduino/packages/formatter/AStyle.java index 7533e761f..9c61ff2db 100644 --- a/app/src/cc/arduino/packages/formatter/AStyle.java +++ b/app/src/cc/arduino/packages/formatter/AStyle.java @@ -92,8 +92,10 @@ public class AStyle implements Tool { int line = getLineOfOffset(textArea); int lineOffset = getLineOffset(textArea, line); + editor.getTextArea().getUndoManager().beginInternalAtomicEdit(); editor.setText(formattedText); editor.getSketch().setModified(true); + editor.getTextArea().getUndoManager().endInternalAtomicEdit(); if (line != -1 && lineOffset != -1) { setCaretPosition(textArea, line, lineOffset); diff --git a/app/test/processing/app/AutoformatProducesOneUndoActionTest.java b/app/test/processing/app/AutoformatProducesOneUndoActionTest.java new file mode 100644 index 000000000..9db88512b --- /dev/null +++ b/app/test/processing/app/AutoformatProducesOneUndoActionTest.java @@ -0,0 +1,56 @@ +package processing.app; + +import org.fest.swing.fixture.JMenuItemFixture; +import org.junit.Test; +import processing.app.helpers.RSyntaxTextAreaFixture; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotEquals; + +public class AutoformatProducesOneUndoActionTest extends AbstractGUITest { + + public static final String SOURCE_BEFORE = "void setup() {\n" + + " // put your setup code here, to run once:\n" + + "\n" + + "}\n" + + "\n" + + "void loop() {\n" + + " // put your main code here, to run repeatedly:\n" + + "\n" + + "}"; + public static final String SOURCE_AFTER = "void setup() {\n" + + " // put your setup code here, to run once:\n" + + "\n" + + "}\n" + + "\n" + + "void loop() {\n" + + " // put your main code here, to run repeatedly:\n" + + "\n" + + "}"; + + @Test + public void shouldSaveCaretPositionAfterAutoformat() { + JMenuItemFixture menuEditUndo = window.menuItem("menuEditUndo"); + menuEditUndo.requireDisabled(); + + JMenuItemFixture menuToolsAutoFormat = window.menuItem("menuToolsAutoFormat"); + menuToolsAutoFormat.requireEnabled(); + + RSyntaxTextAreaFixture editor = window.RSyntaxTextArea("editor"); + editor.setText(SOURCE_BEFORE); + + editor.setCaretPosition(29); // right before the first // (double slash) + + menuToolsAutoFormat.click(); + + String formattedText = editor.getText(); + assertEquals(SOURCE_AFTER, formattedText); + + assertEquals(29, editor.getCaretPosition()); + + menuEditUndo.requireEnabled(); + menuEditUndo.click(); + assertEquals(SOURCE_BEFORE, editor.getText()); + } + +}