From 1b783fa48c1a4d16b3e25a1d7c73d89ea815e0ca Mon Sep 17 00:00:00 2001 From: Federico Fissore Date: Tue, 30 Sep 2014 17:17:13 +0200 Subject: [PATCH] Formatter: cursor position is saved when invoking autoformat. Fixes #2293 --- .../cc/arduino/packages/formatter/AStyle.java | 6 +++ app/test/processing/app/AbstractGUITest.java | 6 +++ .../app/AutoformatSavesCaretPositionTest.java | 46 +++++++++++++++++++ .../helpers/JEditTextAreaComponentDriver.java | 25 ++++++++++ .../app/helpers/JEditTextAreaFixture.java | 9 +++- 5 files changed, 91 insertions(+), 1 deletion(-) create mode 100644 app/test/processing/app/AutoformatSavesCaretPositionTest.java diff --git a/app/src/cc/arduino/packages/formatter/AStyle.java b/app/src/cc/arduino/packages/formatter/AStyle.java index 54d51dfde..c92d1c5cd 100644 --- a/app/src/cc/arduino/packages/formatter/AStyle.java +++ b/app/src/cc/arduino/packages/formatter/AStyle.java @@ -3,6 +3,7 @@ package cc.arduino.packages.formatter; import processing.app.Base; import processing.app.Editor; import processing.app.helpers.FileUtils; +import processing.app.syntax.JEditTextArea; import processing.app.tools.Tool; import java.io.File; @@ -54,8 +55,13 @@ public class AStyle implements Tool { return; } + JEditTextArea textArea = editor.getTextArea(); + int line = textArea.getLineOfOffset(textArea.getCaretPosition()); + int lineOffset = textArea.getCaretPosition() - textArea.getLineStartOffset(line); + editor.setText(formattedText); editor.getSketch().setModified(true); + textArea.setCaretPosition(Math.min(textArea.getLineStartOffset(line) + lineOffset, textArea.getSafeLineStopOffset(line) - 1)); // mark as finished editor.statusNotice(_("Auto Format finished.")); } diff --git a/app/test/processing/app/AbstractGUITest.java b/app/test/processing/app/AbstractGUITest.java index fc9fd5794..b43ec010c 100644 --- a/app/test/processing/app/AbstractGUITest.java +++ b/app/test/processing/app/AbstractGUITest.java @@ -3,6 +3,7 @@ package processing.app; import org.fest.swing.edt.FailOnThreadViolationRepaintManager; import org.fest.swing.edt.GuiActionRunner; import org.fest.swing.edt.GuiQuery; +import org.junit.After; import org.junit.Before; import processing.app.helpers.ArduinoFrameFixture; @@ -32,4 +33,9 @@ public abstract class AbstractGUITest { }); } + @After + public void stopTheIDE() { + window.cleanUp(); + } + } diff --git a/app/test/processing/app/AutoformatSavesCaretPositionTest.java b/app/test/processing/app/AutoformatSavesCaretPositionTest.java new file mode 100644 index 000000000..8ffa03190 --- /dev/null +++ b/app/test/processing/app/AutoformatSavesCaretPositionTest.java @@ -0,0 +1,46 @@ +package processing.app; + +import org.fest.swing.fixture.JMenuItemFixture; +import org.junit.Test; +import processing.app.helpers.JEditTextAreaFixture; + +import static org.junit.Assert.assertEquals; + +public class AutoformatSavesCaretPositionTest extends AbstractGUITest { + + @Test + public void shouldSaveCaretPositionAfterAutoformat() { + JMenuItemFixture menuToolsAutoFormat = window.menuItem("menuToolsAutoFormat"); + menuToolsAutoFormat.requireEnabled(); + + JEditTextAreaFixture editor = window.jEditTextArea("editor"); + editor.setText("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" + + "}"); + + editor.setCaretPosition(29); // right before the first // (double slash) + + menuToolsAutoFormat.click(); + + String formattedText = editor.getText(); + assertEquals("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" + + "}", formattedText); + + assertEquals(29, editor.getCaretPosition()); + + } + +} diff --git a/app/test/processing/app/helpers/JEditTextAreaComponentDriver.java b/app/test/processing/app/helpers/JEditTextAreaComponentDriver.java index e3c357269..f292c3998 100644 --- a/app/test/processing/app/helpers/JEditTextAreaComponentDriver.java +++ b/app/test/processing/app/helpers/JEditTextAreaComponentDriver.java @@ -51,4 +51,29 @@ public class JEditTextAreaComponentDriver extends JComponentDriver { }); } + + public Integer getCaretPosition(final JEditTextArea target) { + focusAndWaitForFocusGain(target); + return GuiActionRunner.execute(new GuiQuery() { + + protected Integer executeInEDT() { + return target.getCaretPosition(); + } + + }); + } + + public void setCaretPosition(final JEditTextArea target, final int caretPosition) { + focusAndWaitForFocusGain(target); + GuiActionRunner.execute(new GuiQuery() { + + protected JEditTextArea executeInEDT() { + target.setCaretPosition(caretPosition); + return target; + } + + }); + robot.waitForIdle(); + } + } diff --git a/app/test/processing/app/helpers/JEditTextAreaFixture.java b/app/test/processing/app/helpers/JEditTextAreaFixture.java index 5c94ca8c8..19560d88b 100644 --- a/app/test/processing/app/helpers/JEditTextAreaFixture.java +++ b/app/test/processing/app/helpers/JEditTextAreaFixture.java @@ -2,7 +2,6 @@ package processing.app.helpers; import org.fest.swing.core.Robot; import org.fest.swing.fixture.ComponentFixture; - import processing.app.syntax.JEditTextArea; public class JEditTextAreaFixture extends ComponentFixture { @@ -42,4 +41,12 @@ public class JEditTextAreaFixture extends ComponentFixture { driver.selectAll((JEditTextArea) target); return this; } + + public int getCaretPosition() { + return driver.getCaretPosition((JEditTextArea) target); + } + + public void setCaretPosition(int caretPosition) { + driver.setCaretPosition((JEditTextArea) target, caretPosition); + } }