Skip to content

Commit 682f0fb

Browse files
author
Federico Fissore
committed
Fixed failing PdeKeywords test
Autoformat was not saving caret position any more. Fixed
1 parent 11d2190 commit 682f0fb

File tree

2 files changed

+42
-16
lines changed

2 files changed

+42
-16
lines changed

app/src/cc/arduino/packages/formatter/AStyle.java

Lines changed: 38 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -31,19 +31,18 @@
3131

3232
package cc.arduino.packages.formatter;
3333

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-
4134
import processing.app.Base;
4235
import processing.app.Editor;
4336
import processing.app.helpers.FileUtils;
4437
import processing.app.syntax.SketchTextArea;
4538
import processing.app.tools.Tool;
4639

40+
import javax.swing.text.BadLocationException;
41+
import java.io.File;
42+
import java.io.IOException;
43+
44+
import static processing.app.I18n._;
45+
4746
public class AStyle implements Tool {
4847

4948
private static final String FORMATTER_CONF = "formatter.conf";
@@ -89,18 +88,45 @@ public void run() {
8988
}
9089

9190
SketchTextArea textArea = editor.getTextArea();
91+
92+
int line = getLineOfOffset(textArea);
93+
int lineOffset = getLineOffset(textArea, line);
94+
9295
editor.setText(formattedText);
9396
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) {
95107
try {
96-
int line = textArea.getLineOfOffset(textArea.getCaretPosition());
97-
int lineOffset = textArea.getCaretPosition() - textArea.getLineStartOffset(line);
98108
textArea.setCaretPosition(Math.min(textArea.getLineStartOffset(line) + lineOffset, textArea.getLineEndOffset(line) - 1));
99109
} catch (BadLocationException e) {
100110
e.printStackTrace();
101111
}
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;
104130
}
105131

106132
@Override

app/test/processing/app/syntax/PdeKeywordsTest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,15 @@ public void testKeywordsTxtParsing() throws Exception {
1616
pdeKeywords.reload();
1717

1818
assertEquals("Constants", pdeKeywords.getReference("HIGH"));
19-
assertEquals("IDENTIFIER", pdeKeywords.getTokenTypeAsString("HIGH"));
20-
assertEquals(TokenTypes.IDENTIFIER, pdeKeywords.getTokenType("HIGH".toCharArray(), 0, 3));
19+
assertEquals("RESERVED_WORD_2", pdeKeywords.getTokenTypeAsString("HIGH"));
20+
assertEquals(TokenTypes.RESERVED_WORD_2, pdeKeywords.getTokenType("HIGH".toCharArray(), 0, 3));
2121

2222
assertEquals("IncrementCompound", pdeKeywords.getReference("+="));
2323
assertNull(pdeKeywords.getTokenTypeAsString("+="));
2424

2525
assertNull(pdeKeywords.getReference("Mouse"));
26-
assertEquals("VARIABLE", pdeKeywords.getTokenTypeAsString("Mouse"));
27-
assertEquals(TokenTypes.VARIABLE, pdeKeywords.getTokenType("Mouse".toCharArray(), 0, 4));
26+
assertEquals("DATA_TYPE", pdeKeywords.getTokenTypeAsString("Mouse"));
27+
assertEquals(TokenTypes.DATA_TYPE, pdeKeywords.getTokenType("Mouse".toCharArray(), 0, 4));
2828
}
2929

3030
}

0 commit comments

Comments
 (0)