mirror of
https://github.com/arduino/Arduino.git
synced 2025-02-27 21:54:30 +01:00
Fixed failing PdeKeywords test
Autoformat was not saving caret position any more. Fixed
This commit is contained in:
parent
9ebe916fac
commit
c0a1ffa27a
@ -31,19 +31,18 @@
|
|||||||
|
|
||||||
package cc.arduino.packages.formatter;
|
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.Base;
|
||||||
import processing.app.Editor;
|
import processing.app.Editor;
|
||||||
import processing.app.helpers.FileUtils;
|
import processing.app.helpers.FileUtils;
|
||||||
import processing.app.syntax.SketchTextArea;
|
import processing.app.syntax.SketchTextArea;
|
||||||
import processing.app.tools.Tool;
|
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 {
|
public class AStyle implements Tool {
|
||||||
|
|
||||||
private static final String FORMATTER_CONF = "formatter.conf";
|
private static final String FORMATTER_CONF = "formatter.conf";
|
||||||
@ -89,18 +88,45 @@ public class AStyle implements Tool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
SketchTextArea textArea = editor.getTextArea();
|
SketchTextArea textArea = editor.getTextArea();
|
||||||
|
|
||||||
|
int line = getLineOfOffset(textArea);
|
||||||
|
int lineOffset = getLineOffset(textArea, line);
|
||||||
|
|
||||||
editor.setText(formattedText);
|
editor.setText(formattedText);
|
||||||
editor.getSketch().setModified(true);
|
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 {
|
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));
|
textArea.setCaretPosition(Math.min(textArea.getLineStartOffset(line) + lineOffset, textArea.getLineEndOffset(line) - 1));
|
||||||
} catch (BadLocationException e) {
|
} catch (BadLocationException e) {
|
||||||
e.printStackTrace();
|
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
|
@Override
|
||||||
|
@ -16,15 +16,15 @@ public class PdeKeywordsTest extends AbstractWithPreferencesTest {
|
|||||||
pdeKeywords.reload();
|
pdeKeywords.reload();
|
||||||
|
|
||||||
assertEquals("Constants", pdeKeywords.getReference("HIGH"));
|
assertEquals("Constants", pdeKeywords.getReference("HIGH"));
|
||||||
assertEquals("IDENTIFIER", pdeKeywords.getTokenTypeAsString("HIGH"));
|
assertEquals("RESERVED_WORD_2", pdeKeywords.getTokenTypeAsString("HIGH"));
|
||||||
assertEquals(TokenTypes.IDENTIFIER, pdeKeywords.getTokenType("HIGH".toCharArray(), 0, 3));
|
assertEquals(TokenTypes.RESERVED_WORD_2, pdeKeywords.getTokenType("HIGH".toCharArray(), 0, 3));
|
||||||
|
|
||||||
assertEquals("IncrementCompound", pdeKeywords.getReference("+="));
|
assertEquals("IncrementCompound", pdeKeywords.getReference("+="));
|
||||||
assertNull(pdeKeywords.getTokenTypeAsString("+="));
|
assertNull(pdeKeywords.getTokenTypeAsString("+="));
|
||||||
|
|
||||||
assertNull(pdeKeywords.getReference("Mouse"));
|
assertNull(pdeKeywords.getReference("Mouse"));
|
||||||
assertEquals("VARIABLE", pdeKeywords.getTokenTypeAsString("Mouse"));
|
assertEquals("DATA_TYPE", pdeKeywords.getTokenTypeAsString("Mouse"));
|
||||||
assertEquals(TokenTypes.VARIABLE, pdeKeywords.getTokenType("Mouse".toCharArray(), 0, 4));
|
assertEquals(TokenTypes.DATA_TYPE, pdeKeywords.getTokenType("Mouse".toCharArray(), 0, 4));
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user