Skip to content

Commit 0a960f7

Browse files
Merge pull request #317 from arduino/benjamindannegard/IDE-2-formatter-tutorial
[MKC-496]Benjamindannegard/ide 2.0 auto formatter tutorial
2 parents bc0f85e + 7914282 commit 0a960f7

File tree

2 files changed

+221
-0
lines changed

2 files changed

+221
-0
lines changed
Loading
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,221 @@
1+
---
2+
title: 'Customizing the Auto Formatter Feature'
3+
difficulty: easy
4+
description: 'Learn how to configure the auto formatter feature'
5+
tags:
6+
- Auto Formatter
7+
- Tools
8+
author: 'Benjamin Dannegård, Per Tillisch'
9+
---
10+
11+
Selecting **Tools > Auto Format** or pressing CTRL + T on Windows/Linux or CMD + T on MacOS when writing a sketch in the Arduino IDE 2.0 will automatically format the sketch. It is possible to change the behaviour of this command. In this tutorial we will go through how you can change the behaviour of this command.
12+
13+
You can easily download the editor from the [Arduino Software page](https://www.arduino.cc/en/software#experimental-software).
14+
15+
You can also follow the [downloading and installing the Arduino IDE 2.0](/en/Tutorial/getting-started-with-ide-v2/ide-v2-downloading-and-installing) tutorial for more detailed guide on how to install the editor.
16+
17+
## Requirements
18+
19+
- Arduino IDE 2.0 installed.
20+
21+
## Setting the Custom Configuration
22+
23+
It is possible to define your own custom configuration of the auto formatter feature in two different ways. The custom configuration of the auto formatter can be set on a global level to cover all sketches opened in the editor, or you can set the configuration to be specific to a sketch.
24+
25+
The formatter tool used by Arduino IDE 2.0 is ClangFormat. The documentation for the configuration options is [here](https://clang.llvm.org/docs/ClangFormatStyleOptions.html).
26+
27+
### Global scope
28+
If you add a `.clang-format` configuration file to either of the following locations, the Arduino IDE 2.0 will always use it instead of the Arduino default configuration.
29+
30+
If you are using Windows place the file in:
31+
```
32+
C:\Users\<username>\.arduinoIDE\
33+
```
34+
Or
35+
```
36+
C:\Users\<username>\AppData\Local\Arduino15\
37+
```
38+
39+
If you are using Linux place the file in:
40+
```
41+
~/.arduinoIDE/
42+
```
43+
Or
44+
```
45+
~/.arduino15/
46+
```
47+
48+
If you are using macOS place the file in:
49+
```
50+
~/.arduinoIDE
51+
```
52+
Or
53+
```
54+
~/Library/Arduino15/
55+
```
56+
57+
### Sketch scope
58+
If you add a `.clang-format` configuration file to the root of a sketch, the Arduino IDE will use that configuration when formatting that sketch. This file has precedence over a global formatter configuration file.
59+
60+
![.clang-format file at the root of a sketch](assets/format-file-at-root.png)
61+
62+
## Default Formatting File
63+
64+
Here you can find the default formatting file used in the Arduino IDE 2.0. If you wish to customize how your auto formatting acts in the IDE then starting with this file is a good idea.
65+
66+
***Please note that the custom configuration file completely overrides the Arduino default configuration, rather than merging with it. Any configuration option you don't set in your custom file will be set to the ClangFormat default value.***
67+
68+
```cpp
69+
---
70+
Language: Cpp
71+
# LLVM is the default style setting, used when a configuration option is not set here
72+
BasedOnStyle: LLVM
73+
AccessModifierOffset: -2
74+
AlignAfterOpenBracket: Align
75+
AlignConsecutiveAssignments: false
76+
AlignConsecutiveBitFields: false
77+
AlignConsecutiveDeclarations: false
78+
AlignConsecutiveMacros: false
79+
AlignEscapedNewlines: DontAlign
80+
AlignOperands: Align
81+
AlignTrailingComments: true
82+
AllowAllArgumentsOnNextLine: true
83+
AllowAllConstructorInitializersOnNextLine: true
84+
AllowAllParametersOfDeclarationOnNextLine: true
85+
AllowShortBlocksOnASingleLine: Always
86+
AllowShortCaseLabelsOnASingleLine: true
87+
AllowShortEnumsOnASingleLine: true
88+
AllowShortFunctionsOnASingleLine: Empty
89+
AllowShortIfStatementsOnASingleLine: Always
90+
AllowShortLambdasOnASingleLine: Empty
91+
AllowShortLoopsOnASingleLine: true
92+
AlwaysBreakAfterDefinitionReturnType: None
93+
AlwaysBreakAfterReturnType: None
94+
AlwaysBreakBeforeMultilineStrings: false
95+
AlwaysBreakTemplateDeclarations: No
96+
BinPackArguments: true
97+
BinPackParameters: true
98+
# Only used when "BreakBeforeBraces" set to "Custom"
99+
BraceWrapping:
100+
AfterCaseLabel: false
101+
AfterClass: false
102+
AfterControlStatement: Never
103+
AfterEnum: false
104+
AfterFunction: false
105+
AfterNamespace: false
106+
#AfterObjCDeclaration:
107+
AfterStruct: false
108+
AfterUnion: false
109+
AfterExternBlock: false
110+
BeforeCatch: false
111+
BeforeElse: false
112+
BeforeLambdaBody: false
113+
BeforeWhile: false
114+
IndentBraces: false
115+
SplitEmptyFunction: false
116+
SplitEmptyRecord: false
117+
SplitEmptyNamespace: false
118+
# Java-specific
119+
#BreakAfterJavaFieldAnnotations:
120+
BreakBeforeBinaryOperators: NonAssignment
121+
BreakBeforeBraces: Attach
122+
BreakBeforeTernaryOperators: true
123+
BreakConstructorInitializers: BeforeColon
124+
BreakInheritanceList: BeforeColon
125+
BreakStringLiterals: false
126+
ColumnLimit: 0
127+
# "" matches none
128+
CommentPragmas: ""
129+
CompactNamespaces: false
130+
ConstructorInitializerAllOnOneLineOrOnePerLine: true
131+
ConstructorInitializerIndentWidth: 2
132+
ContinuationIndentWidth: 2
133+
Cpp11BracedListStyle: false
134+
DeriveLineEnding: true
135+
DerivePointerAlignment: true
136+
DisableFormat: false
137+
# Docs say "Do not use this in config files". The default (LLVM 11.0.1) is "false".
138+
#ExperimentalAutoDetectBinPacking:
139+
FixNamespaceComments: false
140+
ForEachMacros: []
141+
IncludeBlocks: Preserve
142+
IncludeCategories: []
143+
# "" matches none
144+
IncludeIsMainRegex: ""
145+
IncludeIsMainSourceRegex: ""
146+
IndentCaseBlocks: true
147+
IndentCaseLabels: true
148+
IndentExternBlock: Indent
149+
IndentGotoLabels: false
150+
IndentPPDirectives: None
151+
IndentWidth: 2
152+
IndentWrappedFunctionNames: false
153+
InsertTrailingCommas: None
154+
# Java-specific
155+
#JavaImportGroups:
156+
# JavaScript-specific
157+
#JavaScriptQuotes:
158+
#JavaScriptWrapImports
159+
KeepEmptyLinesAtTheStartOfBlocks: true
160+
MacroBlockBegin: ""
161+
MacroBlockEnd: ""
162+
# Set to a large number to effectively disable
163+
MaxEmptyLinesToKeep: 100000
164+
NamespaceIndentation: None
165+
NamespaceMacros: []
166+
# Objective C-specific
167+
#ObjCBinPackProtocolList:
168+
#ObjCBlockIndentWidth:
169+
#ObjCBreakBeforeNestedBlockParam:
170+
#ObjCSpaceAfterProperty:
171+
#ObjCSpaceBeforeProtocolList
172+
PenaltyBreakAssignment: 1
173+
PenaltyBreakBeforeFirstCallParameter: 1
174+
PenaltyBreakComment: 1
175+
PenaltyBreakFirstLessLess: 1
176+
PenaltyBreakString: 1
177+
PenaltyBreakTemplateDeclaration: 1
178+
PenaltyExcessCharacter: 1
179+
PenaltyReturnTypeOnItsOwnLine: 1
180+
# Used as a fallback if alignment style can't be detected from code (DerivePointerAlignment: true)
181+
PointerAlignment: Right
182+
RawStringFormats: []
183+
ReflowComments: false
184+
SortIncludes: false
185+
SortUsingDeclarations: false
186+
SpaceAfterCStyleCast: false
187+
SpaceAfterLogicalNot: false
188+
SpaceAfterTemplateKeyword: false
189+
SpaceBeforeAssignmentOperators: true
190+
SpaceBeforeCpp11BracedList: false
191+
SpaceBeforeCtorInitializerColon: true
192+
SpaceBeforeInheritanceColon: true
193+
SpaceBeforeParens: ControlStatements
194+
SpaceBeforeRangeBasedForLoopColon: true
195+
SpaceBeforeSquareBrackets: false
196+
SpaceInEmptyBlock: false
197+
SpaceInEmptyParentheses: false
198+
SpacesBeforeTrailingComments: 2
199+
SpacesInAngles: false
200+
SpacesInCStyleCastParentheses: false
201+
SpacesInConditionalStatement: false
202+
SpacesInContainerLiterals: false
203+
SpacesInParentheses: false
204+
SpacesInSquareBrackets: false
205+
Standard: Auto
206+
StatementMacros: []
207+
TabWidth: 2
208+
TypenameMacros: []
209+
# Default to LF if line endings can't be detected from the content (DeriveLineEnding).
210+
UseCRLF: false
211+
UseTab: Never
212+
WhitespaceSensitiveMacros: []
213+
```
214+
215+
## Conclusion
216+
217+
In this tutorial we went through how to customize the behavior of the `CTRL + T` / `CMD + T` auto formatter command in the Arduino IDE 2.0. This tutorial also shows the different scopes that are available for the auto formatter configuration.
218+
219+
### More Tutorials
220+
221+
You can find more tutorials in the [Arduino IDE 2 documentation page](/software/ide-v2/).

0 commit comments

Comments
 (0)