Skip to content

Commit 4a1ef0f

Browse files
committed
First implementation of ClangFormat class
Still missing installation of clang-format binaries via build.xml
1 parent c4109e7 commit 4a1ef0f

File tree

2 files changed

+129
-11
lines changed

2 files changed

+129
-11
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
/*
2+
* This file is part of Arduino.
3+
*
4+
* Arduino is free software; you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License as published by
6+
* the Free Software Foundation; either version 2 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License
15+
* along with this program; if not, write to the Free Software
16+
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17+
*
18+
* As a special exception, you may use this file as part of a free software
19+
* library without restriction. Specifically, if other files instantiate
20+
* templates or use macros or inline functions from this file, or you compile
21+
* this file and link it with other files to produce an executable, this
22+
* file does not by itself cause the resulting executable to be covered by
23+
* the GNU General Public License. This exception does not however
24+
* invalidate any other reasons why the executable file might be covered by
25+
* the GNU General Public License.
26+
*
27+
* Copyright 2021 Arduino LLC (http://www.arduino.cc/)
28+
*/
29+
30+
package cc.arduino.packages.formatter.clangformat;
31+
32+
import static processing.app.I18n.tr;
33+
34+
import java.io.ByteArrayInputStream;
35+
import java.io.ByteArrayOutputStream;
36+
import java.io.IOException;
37+
import java.io.InputStream;
38+
import java.io.OutputStream;
39+
40+
import org.apache.commons.compress.utils.IOUtils;
41+
import org.apache.logging.log4j.core.util.NullOutputStream;
42+
43+
import processing.app.Editor;
44+
import processing.app.helpers.ProcessUtils;
45+
import processing.app.tools.Tool;
46+
47+
public class ClangFormat implements Tool {
48+
49+
private Editor editor;
50+
51+
public ClangFormat() {
52+
}
53+
54+
@Override
55+
public void init(Editor editor) {
56+
this.editor = editor;
57+
}
58+
59+
@Override
60+
public void run() {
61+
String originalText = editor.getCurrentTab().getText();
62+
63+
try {
64+
String formattedText = runClangFormatOn(originalText);
65+
if (formattedText.equals(originalText)) {
66+
editor.statusNotice(tr("No changes necessary for Auto Format."));
67+
return;
68+
}
69+
editor.getCurrentTab().setText(formattedText);
70+
editor.statusNotice(tr("Auto Format finished."));
71+
} catch (IOException | InterruptedException e) {
72+
editor.statusError("Auto format error: " + e.getMessage());
73+
e.printStackTrace();
74+
}
75+
}
76+
77+
@Override
78+
public String getMenuTitle() {
79+
return tr("Auto Format");
80+
}
81+
82+
private Thread copyAndClose(InputStream input, OutputStream output) {
83+
Thread t = new Thread(() -> {
84+
try {
85+
IOUtils.copy(input, output);
86+
} catch (IOException e) {
87+
e.printStackTrace();
88+
}
89+
try {
90+
input.close();
91+
} catch (IOException e) {
92+
e.printStackTrace();
93+
}
94+
try {
95+
output.close();
96+
} catch (IOException e) {
97+
e.printStackTrace();
98+
}
99+
});
100+
t.start();
101+
return t;
102+
}
103+
104+
String runClangFormatOn(String source)
105+
throws IOException, InterruptedException {
106+
String cmd[] = new String[] { "clang-format" };
107+
108+
Process process = ProcessUtils.exec(cmd);
109+
ByteArrayOutputStream result = new ByteArrayOutputStream();
110+
ByteArrayInputStream dataOut = new ByteArrayInputStream(source.getBytes());
111+
Thread out = copyAndClose(process.getInputStream(), result);
112+
Thread err = copyAndClose(process.getErrorStream(),
113+
NullOutputStream.getInstance());
114+
Thread in = copyAndClose(dataOut, process.getOutputStream());
115+
/* int r = */process.waitFor();
116+
in.join();
117+
out.join();
118+
err.join();
119+
return result.toString();
120+
}
121+
}

app/src/processing/app/Editor.java

+8-11
Original file line numberDiff line numberDiff line change
@@ -981,24 +981,21 @@ private Tool getOrCreateToolInstance(String className) {
981981
}
982982

983983
private void addInternalTools(JMenu menu) {
984-
JMenuItem item;
985-
986-
item = createToolMenuItem("cc.arduino.packages.formatter.AStyle");
987-
if (item == null) {
988-
throw new NullPointerException("Tool cc.arduino.packages.formatter.AStyle unavailable");
984+
JMenuItem formatItem = createToolMenuItem("cc.arduino.packages.formatter.clangformat.ClangFormat");
985+
if (formatItem == null) {
986+
throw new NullPointerException("Tool cc.arduino.packages.formatter.clangformat.ClangFormat");
989987
}
990-
item.setName("menuToolsAutoFormat");
988+
formatItem.setName("menuToolsAutoFormat");
991989
int modifiers = Toolkit.getDefaultToolkit().getMenuShortcutKeyMask();
992-
item.setAccelerator(KeyStroke.getKeyStroke('T', modifiers));
993-
menu.add(item);
990+
formatItem.setAccelerator(KeyStroke.getKeyStroke('T', modifiers));
991+
menu.add(formatItem);
994992

995-
//menu.add(createToolMenuItem("processing.app.tools.CreateFont"));
996-
//menu.add(createToolMenuItem("processing.app.tools.ColorSelector"));
993+
// menu.add(createToolMenuItem("processing.app.tools.CreateFont"));
994+
// menu.add(createToolMenuItem("processing.app.tools.ColorSelector"));
997995
menu.add(createToolMenuItem("processing.app.tools.Archiver"));
998996
menu.add(createToolMenuItem("processing.app.tools.FixEncoding"));
999997
}
1000998

1001-
1002999
private void selectSerialPort(String name) {
10031000
if(portMenu == null) {
10041001
System.out.println(tr("serialMenu is null"));

0 commit comments

Comments
 (0)