File tree Expand file tree Collapse file tree 5 files changed +91
-1
lines changed
src/cc/arduino/packages/formatter Expand file tree Collapse file tree 5 files changed +91
-1
lines changed Original file line number Diff line number Diff line change 33import processing .app .Base ;
44import processing .app .Editor ;
55import processing .app .helpers .FileUtils ;
6+ import processing .app .syntax .JEditTextArea ;
67import processing .app .tools .Tool ;
78
89import 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 }
Original file line number Diff line number Diff line change 33import org .fest .swing .edt .FailOnThreadViolationRepaintManager ;
44import org .fest .swing .edt .GuiActionRunner ;
55import org .fest .swing .edt .GuiQuery ;
6+ import org .junit .After ;
67import org .junit .Before ;
78import 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 number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff 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}
Original file line number Diff line number Diff line change 22
33import org .fest .swing .core .Robot ;
44import org .fest .swing .fixture .ComponentFixture ;
5-
65import processing .app .syntax .JEditTextArea ;
76
87public 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}
You can’t perform that action at this time.
0 commit comments