Skip to content

Commit 1b783fa

Browse files
author
Federico Fissore
committed
Formatter: cursor position is saved when invoking autoformat. Fixes #2293
1 parent 1708161 commit 1b783fa

File tree

5 files changed

+91
-1
lines changed

5 files changed

+91
-1
lines changed

Diff for: app/src/cc/arduino/packages/formatter/AStyle.java

+6
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import processing.app.Base;
44
import processing.app.Editor;
55
import processing.app.helpers.FileUtils;
6+
import processing.app.syntax.JEditTextArea;
67
import processing.app.tools.Tool;
78

89
import java.io.File;
@@ -54,8 +55,13 @@ public void run() {
5455
return;
5556
}
5657

58+
JEditTextArea textArea = editor.getTextArea();
59+
int line = textArea.getLineOfOffset(textArea.getCaretPosition());
60+
int lineOffset = textArea.getCaretPosition() - textArea.getLineStartOffset(line);
61+
5762
editor.setText(formattedText);
5863
editor.getSketch().setModified(true);
64+
textArea.setCaretPosition(Math.min(textArea.getLineStartOffset(line) + lineOffset, textArea.getSafeLineStopOffset(line) - 1));
5965
// mark as finished
6066
editor.statusNotice(_("Auto Format finished."));
6167
}

Diff for: app/test/processing/app/AbstractGUITest.java

+6
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import org.fest.swing.edt.FailOnThreadViolationRepaintManager;
44
import org.fest.swing.edt.GuiActionRunner;
55
import org.fest.swing.edt.GuiQuery;
6+
import org.junit.After;
67
import org.junit.Before;
78
import processing.app.helpers.ArduinoFrameFixture;
89

@@ -32,4 +33,9 @@ protected ArduinoFrameFixture executeInEDT() throws Throwable {
3233
});
3334
}
3435

36+
@After
37+
public void stopTheIDE() {
38+
window.cleanUp();
39+
}
40+
3541
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package processing.app;
2+
3+
import org.fest.swing.fixture.JMenuItemFixture;
4+
import org.junit.Test;
5+
import processing.app.helpers.JEditTextAreaFixture;
6+
7+
import static org.junit.Assert.assertEquals;
8+
9+
public class AutoformatSavesCaretPositionTest extends AbstractGUITest {
10+
11+
@Test
12+
public void shouldSaveCaretPositionAfterAutoformat() {
13+
JMenuItemFixture menuToolsAutoFormat = window.menuItem("menuToolsAutoFormat");
14+
menuToolsAutoFormat.requireEnabled();
15+
16+
JEditTextAreaFixture editor = window.jEditTextArea("editor");
17+
editor.setText("void setup() {\n" +
18+
" // put your setup code here, to run once:\n" +
19+
"\n" +
20+
"}\n" +
21+
"\n" +
22+
"void loop() {\n" +
23+
" // put your main code here, to run repeatedly:\n" +
24+
"\n" +
25+
"}");
26+
27+
editor.setCaretPosition(29); // right before the first // (double slash)
28+
29+
menuToolsAutoFormat.click();
30+
31+
String formattedText = editor.getText();
32+
assertEquals("void setup() {\n" +
33+
" // put your setup code here, to run once:\n" +
34+
"\n" +
35+
"}\n" +
36+
"\n" +
37+
"void loop() {\n" +
38+
" // put your main code here, to run repeatedly:\n" +
39+
"\n" +
40+
"}", formattedText);
41+
42+
assertEquals(29, editor.getCaretPosition());
43+
44+
}
45+
46+
}

Diff for: app/test/processing/app/helpers/JEditTextAreaComponentDriver.java

+25
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,29 @@ protected JEditTextArea executeInEDT() {
5151

5252
});
5353
}
54+
55+
public Integer getCaretPosition(final JEditTextArea target) {
56+
focusAndWaitForFocusGain(target);
57+
return GuiActionRunner.execute(new GuiQuery<Integer>() {
58+
59+
protected Integer executeInEDT() {
60+
return target.getCaretPosition();
61+
}
62+
63+
});
64+
}
65+
66+
public void setCaretPosition(final JEditTextArea target, final int caretPosition) {
67+
focusAndWaitForFocusGain(target);
68+
GuiActionRunner.execute(new GuiQuery<JEditTextArea>() {
69+
70+
protected JEditTextArea executeInEDT() {
71+
target.setCaretPosition(caretPosition);
72+
return target;
73+
}
74+
75+
});
76+
robot.waitForIdle();
77+
}
78+
5479
}

Diff for: app/test/processing/app/helpers/JEditTextAreaFixture.java

+8-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import org.fest.swing.core.Robot;
44
import org.fest.swing.fixture.ComponentFixture;
5-
65
import processing.app.syntax.JEditTextArea;
76

87
public class JEditTextAreaFixture extends ComponentFixture {
@@ -42,4 +41,12 @@ public JEditTextAreaFixture selectAll() {
4241
driver.selectAll((JEditTextArea) target);
4342
return this;
4443
}
44+
45+
public int getCaretPosition() {
46+
return driver.getCaretPosition((JEditTextArea) target);
47+
}
48+
49+
public void setCaretPosition(int caretPosition) {
50+
driver.setCaretPosition((JEditTextArea) target, caretPosition);
51+
}
4552
}

0 commit comments

Comments
 (0)