File tree 5 files changed +91
-1
lines changed
src/cc/arduino/packages/formatter
5 files changed +91
-1
lines changed Original file line number Diff line number Diff line change 3
3
import processing .app .Base ;
4
4
import processing .app .Editor ;
5
5
import processing .app .helpers .FileUtils ;
6
+ import processing .app .syntax .JEditTextArea ;
6
7
import processing .app .tools .Tool ;
7
8
8
9
import java .io .File ;
@@ -54,8 +55,13 @@ public void run() {
54
55
return ;
55
56
}
56
57
58
+ JEditTextArea textArea = editor .getTextArea ();
59
+ int line = textArea .getLineOfOffset (textArea .getCaretPosition ());
60
+ int lineOffset = textArea .getCaretPosition () - textArea .getLineStartOffset (line );
61
+
57
62
editor .setText (formattedText );
58
63
editor .getSketch ().setModified (true );
64
+ textArea .setCaretPosition (Math .min (textArea .getLineStartOffset (line ) + lineOffset , textArea .getSafeLineStopOffset (line ) - 1 ));
59
65
// mark as finished
60
66
editor .statusNotice (_ ("Auto Format finished." ));
61
67
}
Original file line number Diff line number Diff line change 3
3
import org .fest .swing .edt .FailOnThreadViolationRepaintManager ;
4
4
import org .fest .swing .edt .GuiActionRunner ;
5
5
import org .fest .swing .edt .GuiQuery ;
6
+ import org .junit .After ;
6
7
import org .junit .Before ;
7
8
import processing .app .helpers .ArduinoFrameFixture ;
8
9
@@ -32,4 +33,9 @@ protected ArduinoFrameFixture executeInEDT() throws Throwable {
32
33
});
33
34
}
34
35
36
+ @ After
37
+ public void stopTheIDE () {
38
+ window .cleanUp ();
39
+ }
40
+
35
41
}
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() {
51
51
52
52
});
53
53
}
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
+
54
79
}
Original file line number Diff line number Diff line change 2
2
3
3
import org .fest .swing .core .Robot ;
4
4
import org .fest .swing .fixture .ComponentFixture ;
5
-
6
5
import processing .app .syntax .JEditTextArea ;
7
6
8
7
public class JEditTextAreaFixture extends ComponentFixture {
@@ -42,4 +41,12 @@ public JEditTextAreaFixture selectAll() {
42
41
driver .selectAll ((JEditTextArea ) target );
43
42
return this ;
44
43
}
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
+ }
45
52
}
You can’t perform that action at this time.
0 commit comments