38
38
import java .io .OutputStream ;
39
39
40
40
import org .apache .commons .compress .utils .IOUtils ;
41
- import org .apache .logging .log4j .core .util .NullOutputStream ;
42
41
43
42
import com .fasterxml .jackson .databind .DeserializationFeature ;
44
43
import com .fasterxml .jackson .databind .ObjectMapper ;
45
44
46
45
import processing .app .Base ;
47
46
import processing .app .Editor ;
48
47
import processing .app .EditorTab ;
49
- import processing .app .helpers .ProcessUtils ;
50
48
51
49
public class ClangFormat implements Runnable {
52
50
@@ -75,6 +73,8 @@ public void run() {
75
73
} catch (IOException | InterruptedException e ) {
76
74
editor .statusError ("Auto format error: " + e .getMessage ());
77
75
e .printStackTrace ();
76
+ } catch (ClangException e ) {
77
+ editor .statusError ("Auto format error: " + e .getMessage ());
78
78
}
79
79
}
80
80
@@ -101,23 +101,27 @@ private Thread copyAndClose(InputStream input, OutputStream output) {
101
101
}
102
102
103
103
FormatResult runClangFormatOn (String source , int cursorOffset )
104
- throws IOException , InterruptedException {
104
+ throws IOException , InterruptedException , ClangException {
105
105
String cmd [] = new String [] { clangExecutable , "--cursor=" + cursorOffset };
106
106
107
107
Process process = ProcessUtils .exec (cmd );
108
108
ByteArrayOutputStream clangOutput = new ByteArrayOutputStream ();
109
+ ByteArrayOutputStream clangError = new ByteArrayOutputStream ();
109
110
ByteArrayInputStream dataOut = new ByteArrayInputStream (source .getBytes ());
110
111
111
112
Thread in = copyAndClose (dataOut , process .getOutputStream ());
112
- Thread err = copyAndClose (process .getErrorStream (),
113
- NullOutputStream .getInstance ());
113
+ Thread err = copyAndClose (process .getErrorStream (), clangError );
114
114
Thread out = copyAndClose (process .getInputStream (), clangOutput );
115
115
116
- /* int r = */ process .waitFor ();
116
+ int r = process .waitFor ();
117
117
in .join ();
118
118
out .join ();
119
119
err .join ();
120
120
121
+ if (r != 0 ) {
122
+ throw new ClangException (clangError .toString ());
123
+ }
124
+
121
125
// clang-format will output first a JSON object with:
122
126
// - the resulting cursor position and
123
127
// - a flag teling if the conversion was successful
@@ -141,6 +145,12 @@ FormatResult runClangFormatOn(String source, int cursorOffset)
141
145
}
142
146
}
143
147
148
+ class ClangException extends Exception {
149
+ public ClangException (String string ) {
150
+ super (string );
151
+ }
152
+ }
153
+
144
154
class FormatResult {
145
155
public String FormattedText ;
146
156
public int Cursor ;
0 commit comments