Skip to content

Commit 56bdebb

Browse files
author
Federico Fissore
committed
Introducing Artistic Style: new IDE autoformatter
1 parent cb7b62f commit 56bdebb

File tree

7 files changed

+192
-5
lines changed

7 files changed

+192
-5
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,17 @@ hardware/arduino/bootloaders/caterina_LUFA/Caterina.elf
1414
hardware/arduino/bootloaders/caterina_LUFA/Caterina.eep
1515
hardware/arduino/bootloaders/caterina_LUFA/.dep/
1616
build/windows/work/
17+
build/windows/jre.zip
18+
build/windows/libastylej*
1719
build/windows/arduino-*.zip
1820
build/windows/dist/gcc-*.tar.gz
1921
build/macosx/arduino-*.zip
2022
build/macosx/dist/gcc-*.tar.gz
23+
build/macosx/libastylej*
2124
build/linux/work/
2225
build/linux/dist/*.tar.gz
2326
build/linux/*.tgz
27+
build/linux/libastylej*
2428
test-bin
2529
*.iml
2630
.idea
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package cc.arduino.packages.formatter;
2+
3+
import processing.app.Base;
4+
import processing.app.Editor;
5+
import processing.app.helpers.FileUtils;
6+
import processing.app.tools.Tool;
7+
8+
import java.io.File;
9+
import java.io.IOException;
10+
11+
import static processing.app.I18n._;
12+
13+
public class AStyle implements Tool {
14+
15+
private static String FORMATTER_CONF = "formatter.conf";
16+
17+
private final AStyleInterface aStyleInterface;
18+
private final String formatterConfiguration;
19+
private Editor editor;
20+
21+
public AStyle() {
22+
this.aStyleInterface = new AStyleInterface();
23+
File customFormatterConf = Base.getSettingsFile(FORMATTER_CONF);
24+
File defaultFormatterConf = new File(Base.getContentFile("lib"), FORMATTER_CONF);
25+
26+
File formatterConf;
27+
if (customFormatterConf.exists()) {
28+
formatterConf = customFormatterConf;
29+
} else {
30+
formatterConf = defaultFormatterConf;
31+
}
32+
String formatterConfiguration = "";
33+
34+
try {
35+
formatterConfiguration = FileUtils.readFileToString(formatterConf);
36+
} catch (IOException e) {
37+
// noop
38+
}
39+
this.formatterConfiguration = formatterConfiguration;
40+
}
41+
42+
@Override
43+
public void init(Editor editor) {
44+
this.editor = editor;
45+
}
46+
47+
@Override
48+
public void run() {
49+
String originalText = editor.getText();
50+
String formattedText = aStyleInterface.AStyleMain(originalText, formatterConfiguration);
51+
52+
if (formattedText.equals(originalText)) {
53+
editor.statusNotice(_("No changes necessary for Auto Format."));
54+
return;
55+
}
56+
57+
editor.setText(formattedText);
58+
editor.getSketch().setModified(true);
59+
// mark as finished
60+
editor.statusNotice(_("Auto Format finished."));
61+
}
62+
63+
@Override
64+
public String getMenuTitle() {
65+
return _("Auto Format");
66+
}
67+
68+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package cc.arduino.packages.formatter;
2+
3+
import processing.app.Base;
4+
5+
import java.io.File;
6+
7+
public class AStyleInterface {
8+
9+
static {
10+
File astyleLib = new File(Base.getContentFile("lib"), System.mapLibraryName("astylej"));
11+
String astylePath = astyleLib.getAbsolutePath();
12+
try {
13+
System.load(astylePath);
14+
} catch (UnsatisfiedLinkError e) {
15+
e.printStackTrace();
16+
System.out.println(e.getMessage());
17+
System.out.println("Cannot load native library " + astylePath);
18+
System.out.println("The program has terminated!");
19+
System.exit(1);
20+
}
21+
}
22+
23+
/**
24+
* Calls the AStyleMain function in Artistic Style.
25+
*
26+
* @param textIn A string containing the source code to be formatted.
27+
* @param options A string of options to Artistic Style.
28+
* @return A String containing the formatted source from Artistic Style.
29+
*/
30+
public native String AStyleMain(String textIn, String options);
31+
32+
/**
33+
* Calls the AStyleGetVersion function in Artistic Style.
34+
*
35+
* @return A String containing the version number of Artistic Style.
36+
*/
37+
public native String AStyleGetVersion();
38+
39+
/**
40+
* Error handler for messages from Artistic Style.
41+
* This method is called only if there are errors when AStyleMain is called.
42+
* This is for debugging and there should be no errors when the calling
43+
* parameters are correct.
44+
* Changing the method name requires changing Artistic Style.
45+
* Signature: (ILjava/lang/String;)V.
46+
*
47+
* @param errorNumber The error number from Artistic Style.
48+
* @param errorMessage The error message from Artistic Style.
49+
*/
50+
private void ErrorHandler(int errorNumber, String errorMessage) {
51+
System.out.println("AStyle error " + String.valueOf(errorNumber) + " - " + errorMessage);
52+
}
53+
54+
}

app/src/processing/app/Editor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -907,7 +907,7 @@ public void actionPerformed(ActionEvent e) {
907907
protected JMenu addInternalTools(JMenu menu) {
908908
JMenuItem item;
909909

910-
item = createToolMenuItem("processing.app.tools.AutoFormat");
910+
item = createToolMenuItem("cc.arduino.packages.formatter.AStyle");
911911
int modifiers = Toolkit.getDefaultToolkit().getMenuShortcutKeyMask();
912912
item.setAccelerator(KeyStroke.getKeyStroke('T', modifiers));
913913
menu.add(item);

app/src/processing/app/helpers/FileUtils.java

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
package processing.app.helpers;
22

3-
import java.io.File;
4-
import java.io.FileInputStream;
5-
import java.io.FileOutputStream;
6-
import java.io.IOException;
3+
import java.io.*;
74
import java.util.Arrays;
85
import java.util.List;
96
import java.util.Random;
@@ -170,4 +167,25 @@ public static String getLinuxPathFrom(File file) {
170167
public static boolean isSCCSOrHiddenFile(File file) {
171168
return file.isHidden() || file.getName().charAt(0) == '.' || (file.isDirectory() && SOURCE_CONTROL_FOLDERS.contains(file.getName()));
172169
}
170+
171+
public static String readFileToString(File file) throws IOException {
172+
BufferedReader reader = null;
173+
try {
174+
reader = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
175+
StringBuilder sb = new StringBuilder();
176+
String line;
177+
while ((line = reader.readLine()) != null) {
178+
sb.append(line).append("\n");
179+
}
180+
return sb.toString();
181+
} finally {
182+
if (reader != null) {
183+
try {
184+
reader.close();
185+
} catch (IOException e) {
186+
// noop
187+
}
188+
}
189+
}
190+
}
173191
}

build/build.xml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,11 @@
259259
<fileset dir="macosx/work/Arduino.app/Contents/Resources/Java/hardware/tools" includes="**/man/**/*"/>
260260
<fileset dir="macosx/work/Arduino.app/Contents/Resources/Java/hardware/tools" includes="**/man"/>
261261
</delete>
262+
263+
<get src="http://downloads.arduino.cc/libastylej-2.03.zip" dest="macosx" usetimestamp="true" />
264+
<unzip src="macosx/libastylej-2.03.zip" dest="macosx" overwrite="true"/>
265+
<copy file="macosx/libastylej/libastylej.jnilib" todir="macosx/work/Arduino.app/Contents/Resources/Java/lib/" />
266+
<chmod perm="755" file="macosx/work/Arduino.app/Contents/Resources/Java/lib/libastylej.jnilib" />
262267
</target>
263268

264269
<target name="macosx-run" depends="macosx-build" description="Run Mac OS X version">
@@ -470,6 +475,11 @@
470475

471476
<copy todir="linux/work" file="linux/dist/arduino" />
472477
<chmod perm="755" file="linux/work/arduino" />
478+
479+
<get src="http://downloads.arduino.cc/libastylej-2.03.zip" dest="linux" usetimestamp="true" />
480+
<unzip src="linux/libastylej-2.03.zip" dest="linux" overwrite="true"/>
481+
<copy file="linux/libastylej/libastylej${arch-bits}.so" tofile="linux/work/lib/libastylej.so" />
482+
<chmod perm="755" file="linux/work/lib/libastylej.so" />
473483
</target>
474484

475485
<target name="linux32-build" depends="linux-build" description="Build linux (32-bit) version">
@@ -485,6 +495,7 @@
485495
<arg value="-xjf"/>
486496
<arg value="../../avr_tools_linux32.tar.bz2"/>
487497
</exec>
498+
488499
</target>
489500

490501
<target name="linux64-build" depends="linux-build" description="Build linux (64-bit) version">
@@ -659,6 +670,10 @@
659670
<fileset file="windows/eeprom.h" />
660671
</copy>
661672

673+
<get src="http://downloads.arduino.cc/libastylej-2.03.zip" dest="windows" usetimestamp="true" />
674+
<unzip src="windows/libastylej-2.03.zip" dest="windows" overwrite="true"/>
675+
<copy file="windows/libastylej/AStylej.dll" todir="windows/work/lib" />
676+
662677
<!-- Copy bossac.exe tool -->
663678
<copy todir="windows/work/hardware/tools">
664679
<fileset file="windows/bossac.exe" />

build/shared/lib/formatter.conf

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# This configuration file contains a selection of the available options provided by the formatting tool "Artistic Style"
2+
# http://astyle.sourceforge.net/astyle.html
3+
#
4+
# If you wish to change them, don't edit this file.
5+
# Instead, copy it in the same folder of file "preferences.txt" and modify the copy. This way, you won't lose your custom formatter settings when upgrading the IDE
6+
# If you don't know where file preferences.txt is stored, open the IDE, File -> Preferences and you'll find a link
7+
8+
# 2 spaces indentation
9+
indent=spaces=2
10+
11+
# also indent macros
12+
indent-preprocessor
13+
14+
# indent classes, switches (and cases), comments starting at column 1
15+
indent-classes
16+
indent-switches
17+
indent-cases
18+
indent-col1-comments
19+
20+
# put a space around operators
21+
pad-oper
22+
23+
# put a space after if/for/while
24+
pad-header
25+
26+
# if you like one-liners, keep them
27+
keep-one-line-statements
28+

0 commit comments

Comments
 (0)