|
31 | 31 |
|
32 | 32 | package cc.arduino.packages.formatter; |
33 | 33 |
|
34 | | -import static processing.app.I18n._; |
35 | | - |
36 | | -import java.io.File; |
37 | | -import java.io.IOException; |
38 | | - |
39 | | -import javax.swing.text.BadLocationException; |
40 | | - |
41 | 34 | import processing.app.Base; |
42 | 35 | import processing.app.Editor; |
43 | 36 | import processing.app.helpers.FileUtils; |
44 | 37 | import processing.app.syntax.SketchTextArea; |
45 | 38 | import processing.app.tools.Tool; |
46 | 39 |
|
| 40 | +import javax.swing.text.BadLocationException; |
| 41 | +import java.io.File; |
| 42 | +import java.io.IOException; |
| 43 | + |
| 44 | +import static processing.app.I18n._; |
| 45 | + |
47 | 46 | public class AStyle implements Tool { |
48 | 47 |
|
49 | 48 | private static final String FORMATTER_CONF = "formatter.conf"; |
@@ -89,18 +88,45 @@ public void run() { |
89 | 88 | } |
90 | 89 |
|
91 | 90 | SketchTextArea textArea = editor.getTextArea(); |
| 91 | + |
| 92 | + int line = getLineOfOffset(textArea); |
| 93 | + int lineOffset = getLineOffset(textArea, line); |
| 94 | + |
92 | 95 | editor.setText(formattedText); |
93 | 96 | editor.getSketch().setModified(true); |
94 | | - |
| 97 | + |
| 98 | + if (line != -1 && lineOffset != -1) { |
| 99 | + setCaretPosition(textArea, line, lineOffset); |
| 100 | + } |
| 101 | + |
| 102 | + // mark as finished |
| 103 | + editor.statusNotice(_("Auto Format finished.")); |
| 104 | + } |
| 105 | + |
| 106 | + private void setCaretPosition(SketchTextArea textArea, int line, int lineOffset) { |
95 | 107 | try { |
96 | | - int line = textArea.getLineOfOffset(textArea.getCaretPosition()); |
97 | | - int lineOffset = textArea.getCaretPosition() - textArea.getLineStartOffset(line); |
98 | 108 | textArea.setCaretPosition(Math.min(textArea.getLineStartOffset(line) + lineOffset, textArea.getLineEndOffset(line) - 1)); |
99 | 109 | } catch (BadLocationException e) { |
100 | 110 | e.printStackTrace(); |
101 | 111 | } |
102 | | - // mark as finished |
103 | | - editor.statusNotice(_("Auto Format finished.")); |
| 112 | + } |
| 113 | + |
| 114 | + private int getLineOffset(SketchTextArea textArea, int line) { |
| 115 | + try { |
| 116 | + return textArea.getCaretPosition() - textArea.getLineStartOffset(line); |
| 117 | + } catch (BadLocationException e) { |
| 118 | + e.printStackTrace(); |
| 119 | + } |
| 120 | + return -1; |
| 121 | + } |
| 122 | + |
| 123 | + private int getLineOfOffset(SketchTextArea textArea) { |
| 124 | + try { |
| 125 | + return textArea.getLineOfOffset(textArea.getCaretPosition()); |
| 126 | + } catch (BadLocationException e) { |
| 127 | + e.printStackTrace(); |
| 128 | + } |
| 129 | + return -1; |
104 | 130 | } |
105 | 131 |
|
106 | 132 | @Override |
|
0 commit comments