8000 Use clang-format for auto-format (replaces libastyle) by cmaglie · Pull Request #11543 · arduino/Arduino · GitHub
[go: up one dir, main page]

Skip to content

Use clang-format for auto-format (replaces libastyle) #11543

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
Prev Previous commit
Next Next commit
Catch clang-format error and report to user interface
  • Loading branch information
cmaglie committed May 21, 2021
commit 321eadbf0a85d2f385b0eb132c9639a50dcb8c4f
22 changes: 16 additions & 6 deletions app/src/cc/arduino/packages/formatter/clangformat/ClangFormat.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,13 @@
import java.io.OutputStream;

import org.apache.commons.compress.utils.IOUtils;
import org.apache.logging.log4j.core.util.NullOutputStream;

import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;

import processing.app.Base;
import processing.app.Editor;
import processing.app.EditorTab;
import processing.app.helpers.ProcessUtils;

public class ClangFormat implements Runnable {

Expand Down Expand Up @@ -75,6 +73,8 @@ public void run() {
} catch (IOException | InterruptedException e) {
editor.statusError("Auto format error: " + e.getMessage());
e.printStackTrace();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Care to removee e.printStackTrace();?

} catch (ClangException e) {
editor.statusError("Auto format error: " + e.getMessage());
}
}

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

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

Process process = ProcessUtils.exec(cmd);
ByteArrayOutputStream clangOutput = new ByteArrayOutputStream();
ByteArrayOutputStream clangError = new ByteArrayOutputStream();
ByteArrayInputStream dataOut = new ByteArrayInputStream(source.getBytes());

Thread in = copyAndClose(dataOut, process.getOutputStream());
Thread err = copyAndClose(process.getErrorStream(),
NullOutputStream.getInstance());
Thread err = copyAndClose(process.getErrorStream(), clangError);
Thread out = copyAndClose(process.getInputStream(), clangOutput);

/* int r = */process.waitFor();
int r = process.waitFor();
in.join();
out.join();
err.join();

if (r != 0) {
throw new ClangException(clangError.toString());
}

// clang-format will output first a JSON object with:
// - the resulting cursor position and
// - a flag teling if the conversion was successful
Expand All @@ -141,6 +145,12 @@ FormatResult runClangFormatOn(String source, int cursorOffset)
}
}

class ClangException extends Exception {
public ClangException(String string) {
super(string);
}
}

class FormatResult {
public String FormattedText;
public int Cursor;
Expand Down
0