This is annoying and I'd like to change it to :
I can't find anything in preferences to do this.
This is annoying and I'd like to change it to :
I can't find anything in preferences to do this.
I doubt that you can change it. It's the definition of the enum so I think that you need to change (reformat) the definition.
As far as I know it can be disabled but no idea how.
Hi @dougp. Since you didn't provide much information, I'll have to guess that you are referring to Auto Format?
Arduino IDE simply passes your code to a standard external C++ code formatter tool (for Arduino IDE 2.x, this tool is the industry standard ClangFormat). So you can't configure the behavior of the formatter tool via the Arduino IDE preferences. You must instead use the formatter tool's native configuration interface.
You can learn how to do that from this tutorial:
https://docs.arduino.cc/software/ide-v2/tutorials/ide-v2-customize-auto-formatter/
It looks like the relevant ClangFormat configuration option should be AllowShortEnumsOnASingleLine
:
However, that option is already set to true
in the default auto format configuration:
I see there is a bug report about this in the issue tracker for ClangFormat:
The workaround for the bug would be to change the value of the ColumnLimit
option from 0 to some arbitrary large number. For example:
ColumnLimit: 999
You might be tempted to add only that line to your custom .clang-format
file, but if you do that you will find that there are many random changes to the behavior of the IDE's auto format feature. The reason for this is that your custom file overrides Arduino IDE's default formatter configuration completely, meaning that the ClangFormat default setting (which is often different from the Arduino IDE defaults) will be used for any option you don't define in your file. So you should copy the entire default .clang-format
file from the tutorial I linked above, then use that as a base for your modifications.
And I thought it was the hover functionality
So @ptillisch gave an option; alternative is to disable the formatter for a piece of code.
int formatted_code;
// clang-format off
void unformatted_code ;
// clang-format on
void formatted_code_again;
See https://clang.llvm.org/docs/ClangFormatStyleOptions.html#disabling-formatting-on-a-piece-of-code
Thank you. For that reason I prefer @sterretje's option.
More typing but easier to control, IMO.
I don't follow your logic. The problem you referenced is overcome by spending 30 seconds to copy and paste the default configuration into your file. You only have to do that work once.
The solution provided by @sterretje is perfectly valid, but it simply doesn't make sense to consider it superior to the one I provided for the specific reason you gave.
Maybe I didn't explain the process clearly in my previous post. I'll provide more detailed instructions:
Click the following link to open Arduino's default ClangFormat configuration file in your web browser:
https://raw.githubusercontent.com/arduino/tooling-project-assets/refs/heads/main/other/clang-format-configuration/.clang-format
Press the Ctrl+A keyboard shortcut (Command+A for macOS users).
This will select all the text in the page.
Press the Ctrl+C keyboard shortcut (Command+C for macOS users).
This will copy the selected text to the clipboard.
Open a new text file in any text editor.
Press the Ctrl+V keyboard shortcut (Command+V for macOS users).
This will paste the copied text into the editor.
Change line 62 from this:
ColumnLimit: 0
to this
ColumnLimit: 999
Save the file to the following path:
/home/<username>/.arduinoIDE/.clang-format
(where <username>
is your Linux username).arduinoIDE
folder may be hidden by default in your file manager and terminal./Users/<username>/.arduinoIDE/.clang-format
(where <username>
is your macOS username).arduinoIDE
folder is hidden by default. You can make it visible by pressing theC:\Users\<username>\.arduinoIDE\.clang-format
(where <username>
is your Windows username) Make sure to name the file with the exact filename .clang-format
, not something like .clang-format.txt
.
No, I just got confused and shied away from the
phrase.
I have followed the instructions in post #6 but see no effect. I presume a restart of the IDE is required?
They work as expected for me. Please provide the following information and I'll investigate further:
No, it is not necessary.
Starting code:
enum { ramSingle, zerkAlpha, indexRodAlpha, pusherVertical, shellPlate, caseSingle, rockerSingle, x10C }; //
After ctrl + t
enum { ramSingle,
zerkAlpha,
indexRodAlpha,
pusherVertical,
shellPlate,
caseSingle,
rockerSingle,
x10C }; //
clang changed:
BreakInheritanceList: BeforeColon
BreakStringLiterals: false
ColumnLimit: 999
CommentPragmas: ''
CompactNamespaces: false
edit -
I have since done a complete reboot (windows update needed) and the behavior is unchanged. the 'column limit 999' setting did survive the reboot.
On my system, after following the instructions in post #6, auto format doesn't result in any changes to your starting code.
Before:
enum { ramSingle, zerkAlpha, indexRodAlpha, pusherVertical, shellPlate, caseSingle, rockerSingle, x10C }; //
void setup() {}
void loop() {}
After:
enum { ramSingle, zerkAlpha, indexRodAlpha, pusherVertical, shellPlate, caseSingle, rockerSingle, x10C }; //
void setup() {}
void loop() {}
(no change)
Please double check that the file is present at this path on your computer:
C:\Users\<username>\.arduinoIDE\.clang-format
(where <username>
is your Windows username)
Please double check that the filename is exactly .clang-format
.
Windows File Explorer has an annoying behavior where, with the default settings (the "Hide extensions for known file types" option enabled), it hides file extensions (so if the file is named .clang-format.txt
, it will appear as .clang-format
in File Explorer). If you have this configuration, the only indication of the file extension is the message shown in the "Type" column when you have the folder in the "Details" view. It must show "CLANG-FORMAT file" in that column:
Not something like "Text Source File" (which would indicate the file is actually named .clang-format.txt
):
I renamed the original file so I'd have a fallback if needed
clang-format from explorer:
properties:
The file highlighted is the modified version (columns 999).
How to make it be CLANG_FORMAT file?
OK, great. Now we have identified the problem!
.clang-format.txt
..clang-format.txt
will now become an editable field..txt
from the end of the .clang-format.txt
filename..clang-format
Now try formatting your "Starting code" again in Arduino IDE. Hopefully this time it will leave the code as is.
Success! See post #12
Thanks, @ptillisch and @sterretje !
note: changed 'solution' to this post from
Thread bookmarked.
You are welcome. I'm glad auto format is working according to your preferences now.
Hopefully the ClangFormat/LLVM developers will be able to fix that bug at some point in the future.
Since you seem to be fairly opinionated regarding code formatting, you might end up finding that the knowledge gained here regarding how to adjust the behavior of the formatter, and to disable it selectively in cases where it isn't possible to make it work according to your preferences (or when it isn't worth the effort to figure out how to configure it), will be useful for other applications in the future.
Regards, Per
I've subscribed to the thread(?) so I'll know when/if that happens.