Skip to content

Commit d0c0392

Browse files
committed
Added default clang-format configuration and user-customizable config support
The places checked for a valid config are: - the sketch folder - the global data folder (behind preferences.txt) - the Arduino IDE app dir The default config is copied in the Arduino IDE app dir
1 parent e4af104 commit d0c0392

File tree

3 files changed

+166
-1
lines changed

3 files changed

+166
-1
lines changed

Diff for: app/src/cc/arduino/packages/formatter/clangformat/ClangFormat.java

+19-1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333

3434
import java.io.ByteArrayInputStream;
3535
import java.io.ByteArrayOutputStream;
36+
import java.io.File;
3637
import java.io.IOException;
3738
import java.io.InputStream;
3839
import java.io.OutputStream;
@@ -43,6 +44,7 @@
4344
import com.fasterxml.jackson.databind.ObjectMapper;
4445

4546
import processing.app.Base;
47+
import processing.app.BaseNoGui;
4648
import processing.app.Editor;
4749
import processing.app.EditorTab;
4850

@@ -100,11 +102,27 @@ private Thread copyAndClose(InputStream input, OutputStream output) {
100102
return t;
101103
}
102104

105+
private File findConfigFile() {
106+
// check if sketch has a config file
107+
File sketchDir = editor.getSketch().getFolder();
108+
if (new File(sketchDir, ".clang-format").isFile()) {
109+
return sketchDir;
110+
}
111+
112+
// check if a global config file exists
113+
if (BaseNoGui.getSettingsFile(".clang-format").isFile()) {
114+
return BaseNoGui.getSettingsFolder();
115+
}
116+
117+
// otherwise: no custom configs, return Arduino IDE app dir.
118+
return new File(clangExecutable).getParentFile();
119+
}
120+
103121
FormatResult runClangFormatOn(String source, int cursorOffset)
104122
throws IOException, InterruptedException, ClangException {
105123
String cmd[] = new String[] { clangExecutable, "--cursor=" + cursorOffset };
106124

107-
Process process = ProcessUtils.exec(cmd);
125+
Process process = Runtime.getRuntime().exec(cmd, null, findConfigFile());
108126
ByteArrayOutputStream clangOutput = new ByteArrayOutputStream();
109127
ByteArrayOutputStream clangError = new ByteArrayOutputStream();
110128
ByteArrayInputStream dataOut = new ByteArrayInputStream(source.getBytes());

Diff for: build/build.xml

+2
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,8 @@
345345
</antcall>
346346

347347
<delete dir="${staging_folder}/${clang_arch_name}"/>
348+
349+
<copy file="shared/clang-format-arduino" tofile="${staging_folder}/work/${staging_hardware_folder}/../.clang-format" />
348350
</target>
349351

350352
<!-- - - - - - - - - -->

Diff for: build/shared/clang-format-arduino

+145
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
# See: https://releases.llvm.org/11.0.1/tools/clang/docs/ClangFormatStyleOptions.html
2+
---
3+
Language: Cpp
4+
# LLVM is the default style setting, used when a configuration option is not set here
5+
BasedOnStyle: LLVM
6+
AccessModifierOffset: -2
7+
AlignAfterOpenBracket: Align
8+
AlignConsecutiveAssignments: false
9+
AlignConsecutiveBitFields: false
10+
AlignConsecutiveDeclarations: false
11+
AlignConsecutiveMacros: false
12+
AlignEscapedNewlines: DontAlign
13+
AlignOperands: Align
14+
AlignTrailingComments: true
15+
AllowAllArgumentsOnNextLine: true
16+
AllowAllConstructorInitializersOnNextLine: true
17+
AllowAllParametersOfDeclarationOnNextLine: true
18+
AllowShortBlocksOnASingleLine: Always
19+
AllowShortCaseLabelsOnASingleLine: true
20+
AllowShortEnumsOnASingleLine: true
21+
AllowShortFunctionsOnASingleLine: Empty
22+
AllowShortIfStatementsOnASingleLine: Always
23+
AllowShortLambdasOnASingleLine: Empty
24+
AllowShortLoopsOnASingleLine: true
25+
AlwaysBreakAfterDefinitionReturnType: None
26+
AlwaysBreakAfterReturnType: None
27+
AlwaysBreakBeforeMultilineStrings: false
28+
AlwaysBreakTemplateDeclarations: No
29+
BinPackArguments: true
30+
BinPackParameters: true
31+
# Only used when "BreakBeforeBraces" set to "Custom"
32+
BraceWrapping:
33+
AfterCaseLabel: false
34+
AfterClass: false
35+
AfterControlStatement: Never
36+
AfterEnum: false
37+
AfterFunction: false
38+
AfterNamespace: false
39+
#AfterObjCDeclaration:
40+
AfterStruct: false
41+
AfterUnion: false
42+
AfterExternBlock: false
43+
BeforeCatch: false
44+
BeforeElse: false
45+
BeforeLambdaBody: false
46+
BeforeWhile: false
47+
IndentBraces: false
48+
SplitEmptyFunction: false
49+
SplitEmptyRecord: false
50+
SplitEmptyNamespace: false
51+
# Java-specific
52+
#BreakAfterJavaFieldAnnotations:
53+
BreakBeforeBinaryOperators: NonAssignment
54+
BreakBeforeBraces: Attach
55+
BreakBeforeTernaryOperators: true
56+
BreakConstructorInitializers: BeforeColon
57+
BreakInheritanceList: BeforeColon
58+
BreakStringLiterals: false
59+
ColumnLimit: 0
60+
# "" matches none
61+
CommentPragmas: ""
62+
CompactNamespaces: false
63+
ConstructorInitializerAllOnOneLineOrOnePerLine: true
64+
ConstructorInitializerIndentWidth: 2
65+
ContinuationIndentWidth: 2
66+
Cpp11BracedListStyle: false
67+
DeriveLineEnding: true
68+
DerivePointerAlignment: true
69+
DisableFormat: false
70+
# Docs say "Do not use this in config files". The default (LLVM 11.0.1) is "false".
71+
#ExperimentalAutoDetectBinPacking:
72+
FixNamespaceComments: false
73+
ForEachMacros: []
74+
IncludeBlocks: Preserve
75+
IncludeCategories: []
76+
# "" matches none
77+
IncludeIsMainRegex: ""
78+
IncludeIsMainSourceRegex: ""
79+
IndentCaseBlocks: true
80+
IndentCaseLabels: true
81+
IndentExternBlock: Indent
82+
IndentGotoLabels: false
83+
IndentPPDirectives: None
84+
IndentWidth: 2
85+
IndentWrappedFunctionNames: false
86+
InsertTrailingCommas: None
87+
# Java-specific
88+
#JavaImportGroups:
89+
# JavaScript-specific
90+
#JavaScriptQuotes:
91+
#JavaScriptWrapImports
92+
KeepEmptyLinesAtTheStartOfBlocks: true
93+
MacroBlockBegin: ""
94+
MacroBlockEnd: ""
95+
# Set to a large number to effectively disable
96+
MaxEmptyLinesToKeep: 100000
97+
NamespaceIndentation: None
98+
NamespaceMacros: []
99+
# Objective C-specific
100+
#ObjCBinPackProtocolList:
101+
#ObjCBlockIndentWidth:
102+
#ObjCBreakBeforeNestedBlockParam:
103+
#ObjCSpaceAfterProperty:
104+
#ObjCSpaceBeforeProtocolList
105+
PenaltyBreakAssignment: 1
106+
PenaltyBreakBeforeFirstCallParameter: 1
107+
PenaltyBreakComment: 1
108+
PenaltyBreakFirstLessLess: 1
109+
PenaltyBreakString: 1
110+
PenaltyBreakTemplateDeclaration: 1
111+
PenaltyExcessCharacter: 1
112+
PenaltyReturnTypeOnItsOwnLine: 1
113+
# Used as a fallback if alignment style can't be detected from code (DerivePointerAlignment: true)
114+
PointerAlignment: Right
115+
RawStringFormats: []
116+
ReflowComments: false
117+
SortIncludes: false
118+
SortUsingDeclarations: false
119+
SpaceAfterCStyleCast: false
120+
SpaceAfterLogicalNot: false
121+
SpaceAfterTemplateKeyword: false
122+
SpaceBeforeAssignmentOperators: true
123+
SpaceBeforeCpp11BracedList: false
124+
SpaceBeforeCtorInitializerColon: true
125+
SpaceBeforeInheritanceColon: true
126+
SpaceBeforeParens: ControlStatements
127+
SpaceBeforeRangeBasedForLoopColon: true
128+
SpaceBeforeSquareBrackets: false
129+
SpaceInEmptyBlock: false
130+
SpaceInEmptyParentheses: false
131+
SpacesBeforeTrailingComments: 2
132+
SpacesInAngles: false
133+
SpacesInCStyleCastParentheses: false
134+
SpacesInConditionalStatement: false
135+
SpacesInContainerLiterals: false
136+
SpacesInParentheses: false
137+
SpacesInSquareBrackets: false
138+
Standard: Auto
139+
StatementMacros: []
140+
TabWidth: 2
141+
TypenameMacros: []
142+
# Default to LF if line endings can't be detected from the content (DeriveLineEnding).
143+
UseCRLF: false
144+
UseTab: Never
145+
WhitespaceSensitiveMacros: []

0 commit comments

Comments
 (0)