forked from arduino/Arduino
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathAStyle.java
80 lines (63 loc) · 2.21 KB
/
AStyle.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
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;
public class AStyle implements Tool {
private static String FORMATTER_CONF = "formatter.conf";
private final AStyleInterface aStyleInterface;
private final String formatterConfiguration;
private Editor editor;
public AStyle() {
this.aStyleInterface = new AStyleInterface();
File customFormatterConf = Base.getSettingsFile(FORMATTER_CONF);
File defaultFormatterConf = new File(Base.getContentFile("lib"), FORMATTER_CONF);
File formatterConf;
if (customFormatterConf.exists()) {
formatterConf = customFormatterConf;
} else {
formatterConf = defaultFormatterConf;
}
String formatterConfiguration = "";
try {
formatterConfiguration = FileUtils.readFileToString(formatterConf);
} catch (IOException e) {
// noop
}
this.formatterConfiguration = formatterConfiguration;
}
@Override
public void init(Editor editor) {
this.editor = editor;
}
@Override
public void run() {
String originalText = editor.getText();
String formattedText = aStyleInterface.AStyleMain(originalText, formatterConfiguration);
if (formattedText.equals(originalText)) {
editor.statusNotice(_("No changes necessary for Auto Format."));
return;
}
SketchTextArea textArea = editor.getTextArea();
editor.setText(formattedText);
editor.getSketch().setModified(true);
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."));
}
@Override
public String getMenuTitle() {
return _("Auto Format");
}
}