1
0
mirror of https://github.com/arduino/Arduino.git synced 2024-11-29 10:24:12 +01:00

Fixed failing PdeKeywords test

Autoformat was not saving caret position any more. Fixed
This commit is contained in:
Federico Fissore 2015-04-27 18:17:53 +02:00
parent 9ebe916fac
commit c0a1ffa27a
2 changed files with 42 additions and 16 deletions

View File

@ -31,19 +31,18 @@
package cc.arduino.packages.formatter;
import static processing.app.I18n._;
import java.io.File;
import java.io.IOException;
import javax.swing.text.BadLocationException;
import processing.app.Base;
import processing.app.Editor;
import processing.app.helpers.FileUtils;
import processing.app.syntax.SketchTextArea;
import processing.app.tools.Tool;
import javax.swing.text.BadLocationException;
import java.io.File;
import java.io.IOException;
import static processing.app.I18n._;
public class AStyle implements Tool {
private static final String FORMATTER_CONF = "formatter.conf";
@ -89,18 +88,45 @@ public class AStyle implements Tool {
}
SketchTextArea textArea = editor.getTextArea();
int line = getLineOfOffset(textArea);
int lineOffset = getLineOffset(textArea, line);
editor.setText(formattedText);
editor.getSketch().setModified(true);
if (line != -1 && lineOffset != -1) {
setCaretPosition(textArea, line, lineOffset);
}
// mark as finished
editor.statusNotice(_("Auto Format finished."));
}
private void setCaretPosition(SketchTextArea textArea, int line, int lineOffset) {
try {
int line = textArea.getLineOfOffset(textArea.getCaretPosition());
int lineOffset = textArea.getCaretPosition() - textArea.getLineStartOffset(line);
textArea.setCaretPosition(Math.min(textArea.getLineStartOffset(line) + lineOffset, textArea.getLineEndOffset(line) - 1));
} catch (BadLocationException e) {
e.printStackTrace();
}
// mark as finished
editor.statusNotice(_("Auto Format finished."));
}
private int getLineOffset(SketchTextArea textArea, int line) {
try {
return textArea.getCaretPosition() - textArea.getLineStartOffset(line);
} catch (BadLocationException e) {
e.printStackTrace();
}
return -1;
}
private int getLineOfOffset(SketchTextArea textArea) {
try {
return textArea.getLineOfOffset(textArea.getCaretPosition());
} catch (BadLocationException e) {
e.printStackTrace();
}
return -1;
}
@Override

View File

@ -16,15 +16,15 @@ public class PdeKeywordsTest extends AbstractWithPreferencesTest {
pdeKeywords.reload();
assertEquals("Constants", pdeKeywords.getReference("HIGH"));
assertEquals("IDENTIFIER", pdeKeywords.getTokenTypeAsString("HIGH"));
assertEquals(TokenTypes.IDENTIFIER, pdeKeywords.getTokenType("HIGH".toCharArray(), 0, 3));
assertEquals("RESERVED_WORD_2", pdeKeywords.getTokenTypeAsString("HIGH"));
assertEquals(TokenTypes.RESERVED_WORD_2, pdeKeywords.getTokenType("HIGH".toCharArray(), 0, 3));
assertEquals("IncrementCompound", pdeKeywords.getReference("+="));
assertNull(pdeKeywords.getTokenTypeAsString("+="));
assertNull(pdeKeywords.getReference("Mouse"));
assertEquals("VARIABLE", pdeKeywords.getTokenTypeAsString("Mouse"));
assertEquals(TokenTypes.VARIABLE, pdeKeywords.getTokenType("Mouse".toCharArray(), 0, 4));
assertEquals("DATA_TYPE", pdeKeywords.getTokenTypeAsString("Mouse"));
assertEquals(TokenTypes.DATA_TYPE, pdeKeywords.getTokenType("Mouse".toCharArray(), 0, 4));
}
}