Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Open
Next Next commit
First implementation of ClangFormat class
Still missing installation of clang-format binaries via build.xml
  • Loading branch information
cmaglie committed May 19, 2021
commit 4a1ef0fecee011acf99485e34ed6032ec3c86f73
121 changes: 121 additions & 0 deletions app/src/cc/arduino/packages/formatter/clangformat/ClangFormat.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
/*
* This file is part of Arduino.
*
* Arduino is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
* As a special exception, you may use this file as part of a free software
* library without restriction. Specifically, if other files instantiate
* templates or use macros or inline functions from this file, or you compile
* this file and link it with other files to produce an executable, this
* file does not by itself cause the resulting executable to be covered by
* the GNU General Public License. This exception does not however
* invalidate any other reasons why the executable file might be covered by
* the GNU General Public License.
*
* Copyright 2021 Arduino LLC (http://www.arduino.cc/)
*/

package cc.arduino.packages.formatter.clangformat;

import static processing.app.I18n.tr;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

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

import processing.app.Editor;
import processing.app.helpers.ProcessUtils;
import processing.app.tools.Tool;

public class ClangFormat implements Tool {

private Editor editor;

public ClangFormat() {
}

@Override
public void init(Editor editor) {
this.editor = editor;
}

@Override
public void run() {
String originalText = editor.getCurrentTab().getText();

try {
String formattedText = runClangFormatOn(originalText);
if (formattedText.equals(originalText)) {
editor.statusNotice(tr("No changes necessary for Auto Format."));
return;
}
editor.getCurrentTab().setText(formattedText);
editor.statusNotice(tr("Auto Format finished."));
} 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();?

}
}

@Override
public String getMenuTitle() {
return tr("Auto Format");
}

private Thread copyAndClose(InputStream input, OutputStream output) {
Thread t = new Thread(() -> {
try {
IOUtils.copy(input, output);
} catch (IOException e) {
e.printStackTrace();
}
try {
input.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
output.close();
} catch (IOException e) {
e.printStackTrace();
}
});
t.start();
return t;
}

String runClangFormatOn(String source)
throws IOException, InterruptedException {
String cmd[] = new String[] { "clang-format" };

Process process = ProcessUtils.exec(cmd);
ByteArrayOutputStream result = new ByteArrayOutputStream();
ByteArrayInputStream dataOut = new ByteArrayInputStream(source.getBytes());
Thread out = copyAndClose(process.getInputStream(), result);
Thread err = copyAndClose(process.getErrorStream(),
NullOutputStream.getInstance());
Thread in = copyAndClose(dataOut, process.getOutputStream());
/* int r = */process.waitFor();
in.join();
out.join();
err.join();
return result.toString();
}
}
19 changes: 8 additions & 11 deletions app/src/processing/app/Editor.java
Original file line number Diff line number Diff line change
Expand Up @@ -981,24 +981,21 @@ private Tool getOrCreateToolInstance(String className) {
}

private void addInternalTools(JMenu menu) {
JMenuItem item;

item = createToolMenuItem("cc.arduino.packages.formatter.AStyle");
if (item == null) {
throw new NullPointerException("Tool cc.arduino.packages.formatter.AStyle unavailable");
JMenuItem formatItem = createToolMenuItem("cc.arduino.packages.formatter.clangformat.ClangFormat");
if (formatItem == null) {
throw new NullPointerException("Tool cc.arduino.packages.formatter.clangformat.ClangFormat");
}
item.setName("menuToolsAutoFormat");
formatItem.setName("menuToolsAutoFormat");
int modifiers = Toolkit.getDefaultToolkit().getMenuShortcutKeyMask();
item.setAccelerator(KeyStroke.getKeyStroke('T', modifiers));
menu.add(item);
formatItem.setAccelerator(KeyStroke.getKeyStroke('T', modifiers));
menu.add(formatItem);

//menu.add(createToolMenuItem("processing.app.tools.CreateFont"));
//menu.add(createToolMenuItem("processing.app.tools.ColorSelector"));
// menu.add(createToolMenuItem("processing.app.tools.CreateFont"));
// menu.add(createToolMenuItem("processing.app.tools.ColorSelector"));
Comment on lines +994 to +995
Copy link

@silvanocerza silvanocerza May 21, 2021

Choose a reason for hiding this comment

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

Can't we just remove these comments?

menu.add(createToolMenuItem("processing.app.tools.Archiver"));
menu.add(createToolMenuItem("processing.app.tools.FixEncoding"));
}


private void selectSerialPort(String name) {
if(portMenu == null) {
System.out.println(tr("serialMenu is null"));
Expand Down