-
-
Notifications
You must be signed in to change notification settings - Fork 7k
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
cmaglie
wants to merge
12
commits into
arduino:master
Choose a base branch
from
cmaglie:clang-format
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
4a1ef0f
First implementation of ClangFormat class
cmaglie 7ef8b06
Added clang-format download in build.xml
cmaglie f20d512
ClangFormat is no more a Tool
cmaglie d33fc4e
Fixed some trivial warnings
cmaglie 1f1dc7d
Removed libastyle
cmaglie 7e76bc5
Renamed some build.xml ant task from libastyle to liblistserials
cmaglie 66a001c
Handle cursor positioning
cmaglie c4c5558
Slightly simplify subroutine
cmaglie 321eadb
Catch clang-format error and report to user interface
cmaglie 752bcdb
Added default clang-format configuration and user-customizable config…
cmaglie 09c6626
Autoformat keeps cursor position after Undo
cmaglie 7754b7f
Fixed tests for new autoformat style
cmaglie File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next
Next commit
First implementation of ClangFormat class
Still missing installation of clang-format binaries via build.xml
- Loading branch information
commit 4a1ef0fecee011acf99485e34ed6032ec3c86f73
There are no files selected for viewing
121 changes: 121 additions & 0 deletions
121
app/src/cc/arduino/packages/formatter/clangformat/ClangFormat.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
|
||
@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(); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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")); | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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();
?