From 6d294f8146bad32fb5a21b395cb43910232d7654 Mon Sep 17 00:00:00 2001 From: jdf Date: Fri, 26 Mar 2010 03:25:33 +0000 Subject: [PATCH] Sample code for running an external beautifier --- app/src/processing/app/tools/Uncrustify.java | 58 ++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 app/src/processing/app/tools/Uncrustify.java diff --git a/app/src/processing/app/tools/Uncrustify.java b/app/src/processing/app/tools/Uncrustify.java new file mode 100644 index 000000000..89689bdee --- /dev/null +++ b/app/src/processing/app/tools/Uncrustify.java @@ -0,0 +1,58 @@ +package processing.app.tools; + +import java.io.File; +import java.io.FileWriter; +import java.io.IOException; +import processing.app.Base; +import processing.app.Editor; +import processing.util.exec.ProcessHelper; +import processing.util.exec.ProcessResult; + +public class Uncrustify implements Tool { + + private Editor editor; + + public String getMenuTitle() { + return "Uncrustify (experimental)"; + } + + public void init(Editor editor) { + this.editor = editor; + } + + public void run() { + try { + final File lib = Base.getContentFile("lib"); + final File uncrustify = new File(lib, "uncrustify"); + final File config = new File(lib, "uncrustify-pde.cfg"); + final File tmp = File.createTempFile("uncrustify", ".pde"); + try { + final FileWriter out = new FileWriter(tmp); + try { + out.write(editor.getText()); + } finally { + out.close(); + } + final ProcessResult result = new ProcessHelper(uncrustify + .getAbsolutePath(), "-c", config.getAbsolutePath(), "-l", "JAVA", + "-f", tmp + .getAbsolutePath()) + .execute(); + if (!result.succeeded()) { + Base.showMessage("Could not Uncrustify", result.getStderr()); + return; + } + editor.setText(result.getStdout()); + } catch (InterruptedException e) { + Base.showWarning("Could not Uncrustify", "Unexpected exception", e); + } finally { + if (!tmp.delete()) { + tmp.deleteOnExit(); + } + } + } catch (IOException e) { + Base.showWarning("Could not Uncrustify", "Unexpected exception", e); + } + } + +}