Skip to content

Commit 54db9bb

Browse files
committed
Sync sketch formatter configuration from source
The Arduino IDE's "Auto Format" feature is configured to produce the standard Arduino sketch formatting style, as established by the Arduino IDE 1.x formatter. The configuration is consumed by several other projects which require the configuration in a YAML file. In order to provide all the consumers with a single canonical source and to locate the infrastructure and activity related to the maintenance of the file in a more appropriate repository, it is now hosted in a permanent location in the `arduino/tooling-project-assets` repository. The following changes have been made to the source configuration: - Move documentation comments to a dedicated file in the upstream repository - Make additional non-functional changes to the configuration format to facilitate maintenance - Update to use the configuration API of ClangFormat 14.0.0 This last item did result in some functional changes to the configuration which will result in minor differences in the formatter output. These are actually reversions of unwanted differences from the Arduino IDE 1.x formatter output, which were unavoidable when using the 11.0.1 version of ClangFormat in use at the time of the configuration's creation. These changes will provide greater consistency during the migration from Arduino IDE 1.x to 2.x. The default output of the Arduino IDE 1.x formatter will continue to be considered the "gold standard" until Arduino IDE 2.x graduates from "pre-release" status. The Arduino IDE 2.x formatter configuration is fully customizable according to the preferences of each user. Those already using custom configurations will not be affected in any way (though they are encouraged to sync their configuration files from the source to bring them into compliance with the configuration API of the ClangFormat version currently in use by Arduino IDE 2.x). See the documentation and commit history for the source file for details on the configuration changes: https://github.com/arduino/tooling-project-assets/tree/main/other/clang-format-configuration
1 parent 676eb2f commit 54db9bb

File tree

1 file changed

+83
-44
lines changed

1 file changed

+83
-44
lines changed

arduino-ide-extension/src/node/clang-formatter.ts

+83-44
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,6 @@ function toClangOptions(
122122
return { UseTab: 'Never', TabWidth: 2 };
123123
}
124124

125-
// See: https://releases.llvm.org/11.0.1/tools/clang/docs/ClangFormatStyleOptions.html
126125
export function style({ TabWidth, UseTab }: ClangFormatOptions): string {
127126
let styleArgument = JSON.stringify(styleJson({ TabWidth, UseTab })).replace(
128127
/[\\"]/g,
@@ -147,16 +146,15 @@ function styleJson({
147146
TabWidth,
148147
UseTab,
149148
}: ClangFormatOptions): Record<string, unknown> {
149+
// Source: https://github.com/arduino/tooling-project-assets/tree/main/other/clang-format-configuration
150150
return {
151-
Language: 'Cpp',
152-
// # LLVM is the default style setting, used when a configuration option is not set here
153-
BasedOnStyle: 'LLVM',
154151
AccessModifierOffset: -2,
155152
AlignAfterOpenBracket: 'Align',
156-
AlignConsecutiveAssignments: false,
157-
AlignConsecutiveBitFields: false,
158-
AlignConsecutiveDeclarations: false,
159-
AlignConsecutiveMacros: false,
153+
AlignArrayOfStructures: 'None',
154+
AlignConsecutiveAssignments: 'None',
155+
AlignConsecutiveBitFields: 'None',
156+
AlignConsecutiveDeclarations: 'None',
157+
AlignConsecutiveMacros: 'None',
160158
AlignEscapedNewlines: 'DontAlign',
161159
AlignOperands: 'Align',
162160
AlignTrailingComments: true,
@@ -167,24 +165,26 @@ function styleJson({
167165
AllowShortCaseLabelsOnASingleLine: true,
168166
AllowShortEnumsOnASingleLine: true,
169167
AllowShortFunctionsOnASingleLine: 'Empty',
170-
AllowShortIfStatementsOnASingleLine: 'Always',
168+
AllowShortIfStatementsOnASingleLine: 'AllIfsAndElse',
171169
AllowShortLambdasOnASingleLine: 'Empty',
172170
AllowShortLoopsOnASingleLine: true,
173171
AlwaysBreakAfterDefinitionReturnType: 'None',
174172
AlwaysBreakAfterReturnType: 'None',
175173
AlwaysBreakBeforeMultilineStrings: false,
176174
AlwaysBreakTemplateDeclarations: 'No',
175+
AttributeMacros: ['__capability'],
176+
BasedOnStyle: 'LLVM',
177177
BinPackArguments: true,
178178
BinPackParameters: true,
179-
// # Only used when "BreakBeforeBraces" set to "Custom"
179+
BitFieldColonSpacing: 'Both',
180180
BraceWrapping: {
181181
AfterCaseLabel: false,
182182
AfterClass: false,
183183
AfterControlStatement: 'Never',
184184
AfterEnum: false,
185185
AfterFunction: false,
186186
AfterNamespace: false,
187-
// #AfterObjCDeclaration:
187+
AfterObjCDeclaration: false,
188188
AfterStruct: false,
189189
AfterUnion: false,
190190
AfterExternBlock: false,
@@ -193,104 +193,143 @@ function styleJson({
193193
BeforeLambdaBody: false,
194194
BeforeWhile: false,
195195
IndentBraces: false,
196-
SplitEmptyFunction: false,
197-
SplitEmptyRecord: false,
198-
SplitEmptyNamespace: false,
196+
SplitEmptyFunction: true,
197+
SplitEmptyRecord: true,
198+
SplitEmptyNamespace: true,
199199
},
200-
// # Java-specific
201-
// #BreakAfterJavaFieldAnnotations:
200+
BreakAfterJavaFieldAnnotations: false,
202201
BreakBeforeBinaryOperators: 'NonAssignment',
203202
BreakBeforeBraces: 'Attach',
203+
BreakBeforeConceptDeclarations: false,
204+
BreakBeforeInheritanceComma: false,
204205
BreakBeforeTernaryOperators: true,
205206
BreakConstructorInitializers: 'BeforeColon',
207+
BreakConstructorInitializersBeforeComma: false,
206208
BreakInheritanceList: 'BeforeColon',
207209
BreakStringLiterals: false,
208210
ColumnLimit: 0,
209-
// # "" matches none
210211
CommentPragmas: '',
211212
CompactNamespaces: false,
212-
ConstructorInitializerAllOnOneLineOrOnePerLine: true,
213+
ConstructorInitializerAllOnOneLineOrOnePerLine: false,
213214
ConstructorInitializerIndentWidth: 2,
214215
ContinuationIndentWidth: 2,
215216
Cpp11BracedListStyle: false,
216217
DeriveLineEnding: true,
217218
DerivePointerAlignment: true,
218219
DisableFormat: false,
219-
// # Docs say "Do not use this in config files". The default (LLVM 11.0.1) is "false".
220-
// #ExperimentalAutoDetectBinPacking:
220+
EmptyLineAfterAccessModifier: 'Leave',
221+
EmptyLineBeforeAccessModifier: 'Leave',
222+
ExperimentalAutoDetectBinPacking: false,
221223
FixNamespaceComments: false,
222-
ForEachMacros: [],
224+
ForEachMacros: ['foreach', 'Q_FOREACH', 'BOOST_FOREACH'],
225+
IfMacros: ['KJ_IF_MAYBE'],
223226
IncludeBlocks: 'Preserve',
224-
IncludeCategories: [],
225-
// # "" matches none
227+
IncludeCategories: [
228+
{
229+
Regex: '^"(llvm|llvm-c|clang|clang-c)/',
230+
Priority: 2,
231+
SortPriority: 0,
232+
CaseSensitive: false,
233+
},
234+
{
235+
Regex: '^(<|"(gtest|gmock|isl|json)/)',
236+
Priority: 3,
237+
SortPriority: 0,
238+
CaseSensitive: false,
239+
},
240+
{ Regex: '.*', Priority: 1, SortPriority: 0, CaseSensitive: false },
241+
],
226242
IncludeIsMainRegex: '',
227243
IncludeIsMainSourceRegex: '',
244+
IndentAccessModifiers: false,
228245
IndentCaseBlocks: true,
229246
IndentCaseLabels: true,
230247
IndentExternBlock: 'Indent',
231248
IndentGotoLabels: false,
232249
IndentPPDirectives: 'None',
250+
IndentRequires: true,
233251
IndentWidth: 2,
234252
IndentWrappedFunctionNames: false,
235253
InsertTrailingCommas: 'None',
236-
// # Java-specific
237-
// #JavaImportGroups:
238-
// # JavaScript-specific
239-
// #JavaScriptQuotes:
240-
// #JavaScriptWrapImports
254+
JavaScriptQuotes: 'Leave',
255+
JavaScriptWrapImports: true,
241256
KeepEmptyLinesAtTheStartOfBlocks: true,
257+
LambdaBodyIndentation: 'Signature',
258+
Language: 'Cpp',
242259
MacroBlockBegin: '',
243260
MacroBlockEnd: '',
244-
// # Set to a large number to effectively disable
245261
MaxEmptyLinesToKeep: 100000,
246262
NamespaceIndentation: 'None',
247-
NamespaceMacros: [],
248-
// # Objective C-specific
249-
// #ObjCBinPackProtocolList:
250-
// #ObjCBlockIndentWidth:
251-
// #ObjCBreakBeforeNestedBlockParam:
252-
// #ObjCSpaceAfterProperty:
253-
// #ObjCSpaceBeforeProtocolList
263+
ObjCBinPackProtocolList: 'Auto',
264+
ObjCBlockIndentWidth: 2,
265+
ObjCBreakBeforeNestedBlockParam: true,
266+
ObjCSpaceAfterProperty: false,
267+
ObjCSpaceBeforeProtocolList: true,
268+
PPIndentWidth: -1,
269+
PackConstructorInitializers: 'BinPack',
254270
PenaltyBreakAssignment: 1,
255271
PenaltyBreakBeforeFirstCallParameter: 1,
256272
PenaltyBreakComment: 1,
257273
PenaltyBreakFirstLessLess: 1,
274+
PenaltyBreakOpenParenthesis: 1,
258275
PenaltyBreakString: 1,
259276
PenaltyBreakTemplateDeclaration: 1,
260277
PenaltyExcessCharacter: 1,
278+
PenaltyIndentedWhitespace: 1,
261279
PenaltyReturnTypeOnItsOwnLine: 1,
262-
// # Used as a fallback if alignment style can't be detected from code (DerivePointerAlignment: true)
263280
PointerAlignment: 'Right',
264-
RawStringFormats: [],
281+
QualifierAlignment: 'Leave',
282+
ReferenceAlignment: 'Pointer',
265283
ReflowComments: false,
266-
SortIncludes: false,
284+
RemoveBracesLLVM: false,
285+
SeparateDefinitionBlocks: 'Leave',
286+
ShortNamespaceLines: 0,
287+
SortIncludes: 'Never',
288+
SortJavaStaticImport: 'Before',
267289
SortUsingDeclarations: false,
268290
SpaceAfterCStyleCast: false,
269291
SpaceAfterLogicalNot: false,
270292
SpaceAfterTemplateKeyword: false,
293+
SpaceAroundPointerQualifiers: 'Default',
271294
SpaceBeforeAssignmentOperators: true,
295+
SpaceBeforeCaseColon: false,
272296
SpaceBeforeCpp11BracedList: false,
273297
SpaceBeforeCtorInitializerColon: true,
274298
SpaceBeforeInheritanceColon: true,
275299
SpaceBeforeParens: 'ControlStatements',
300+
SpaceBeforeParensOptions: {
301+
AfterControlStatements: true,
302+
AfterForeachMacros: true,
303+
AfterFunctionDefinitionName: false,
304+
AfterFunctionDeclarationName: false,
305+
AfterIfMacros: true,
306+
AfterOverloadedOperator: false,
307+
BeforeNonEmptyParentheses: false,
308+
},
276309
SpaceBeforeRangeBasedForLoopColon: true,
277310
SpaceBeforeSquareBrackets: false,
278311
SpaceInEmptyBlock: false,
279312
SpaceInEmptyParentheses: false,
280313
SpacesBeforeTrailingComments: 2,
281-
SpacesInAngles: false,
314+
SpacesInAngles: 'Leave',
282315
SpacesInCStyleCastParentheses: false,
283316
SpacesInConditionalStatement: false,
284317
SpacesInContainerLiterals: false,
318+
SpacesInLineCommentPrefix: { Minimum: 0, Maximum: -1 },
285319
SpacesInParentheses: false,
286320
SpacesInSquareBrackets: false,
287321
Standard: 'Auto',
288-
StatementMacros: [],
322+
StatementAttributeLikeMacros: ['Q_EMIT'],
323+
StatementMacros: ['Q_UNUSED', 'QT_REQUIRE_VERSION'],
289324
TabWidth,
290-
TypenameMacros: [],
291-
// # Default to LF if line endings can't be detected from the content (DeriveLineEnding).
292325
UseCRLF: false,
293326
UseTab,
294-
WhitespaceSensitiveMacros: [],
327+
WhitespaceSensitiveMacros: [
328+
'STRINGIZE',
329+
'PP_STRINGIZE',
330+
'BOOST_PP_STRINGIZE',
331+
'NS_SWIFT_NAME',
332+
'CF_SWIFT_NAME',
333+
],
295334
};
296335
}

0 commit comments

Comments
 (0)