Skip to content

Commit 321eadb

Browse files
committed
Catch clang-format error and report to user interface
1 parent c4c5558 commit 321eadb

File tree

1 file changed

+16
-6
lines changed

1 file changed

+16
-6
lines changed

Diff for: app/src/cc/arduino/packages/formatter/clangformat/ClangFormat.java

+16-6
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,13 @@
3838
import java.io.OutputStream;
3939

4040
import org.apache.commons.compress.utils.IOUtils;
41-
import org.apache.logging.log4j.core.util.NullOutputStream;
4241

4342
import com.fasterxml.jackson.databind.DeserializationFeature;
4443
import com.fasterxml.jackson.databind.ObjectMapper;
4544

4645
import processing.app.Base;
4746
import processing.app.Editor;
4847
import processing.app.EditorTab;
49-
import processing.app.helpers.ProcessUtils;
5048

5149
public class ClangFormat implements Runnable {
5250

@@ -75,6 +73,8 @@ public void run() {
7573
} catch (IOException | InterruptedException e) {
7674
editor.statusError("Auto format error: " + e.getMessage());
7775
e.printStackTrace();
76+
} catch (ClangException e) {
77+
editor.statusError("Auto format error: " + e.getMessage());
7878
}
7979
}
8080

@@ -101,23 +101,27 @@ private Thread copyAndClose(InputStream input, OutputStream output) {
101101
}
102102

103103
FormatResult runClangFormatOn(String source, int cursorOffset)
104-
throws IOException, InterruptedException {
104+
throws IOException, InterruptedException, ClangException {
105105
String cmd[] = new String[] { clangExecutable, "--cursor=" + cursorOffset };
106106

107107
Process process = ProcessUtils.exec(cmd);
108108
ByteArrayOutputStream clangOutput = new ByteArrayOutputStream();
109+
ByteArrayOutputStream clangError = new ByteArrayOutputStream();
109110
ByteArrayInputStream dataOut = new ByteArrayInputStream(source.getBytes());
110111

111112
Thread in = copyAndClose(dataOut, process.getOutputStream());
112-
Thread err = copyAndClose(process.getErrorStream(),
113-
NullOutputStream.getInstance());
113+
Thread err = copyAndClose(process.getErrorStream(), clangError);
114114
Thread out = copyAndClose(process.getInputStream(), clangOutput);
115115

116-
/* int r = */process.waitFor();
116+
int r = process.waitFor();
117117
in.join();
118118
out.join();
119119
err.join();
120120

121+
if (r != 0) {
122+
throw new ClangException(clangError.toString());
123+
}
124+
121125
// clang-format will output first a JSON object with:
122126
// - the resulting cursor position and
123127
// - a flag teling if the conversion was successful
@@ -141,6 +145,12 @@ FormatResult runClangFormatOn(String source, int cursorOffset)
141145
}
142146
}
143147

148+
class ClangException extends Exception {
149+
public ClangException(String string) {
150+
super(string);
151+
}
152+
}
153+
144154
class FormatResult {
145155
public String FormattedText;
146156
public int Cursor;

0 commit comments

Comments
 (0)