Curly brackets are enforced - any clear fix?

Hi, I noticed that the current version (2.xx) enforces a specific (K&R) style curly bracket, and does not seem to allow any freedom anymore. ...at least without resorting to tricks.

Allman style is visually clearer, for me at least, so it would be awesome if someone could say how to fix that.

At the moment this is the only thing that works:

void function()//
{
  asm("nop");
  //important stuff
}

Hi @jussim. The Arduino IDE 2.x "Auto Format" feature is configurable. You can learn how to do that from this tutorial:

You will be interested in the BreakBeforeBraces configuration option:

https://clang.llvm.org/docs/ClangFormatStyleOptions.html#breakbeforebraces

Clang setup; where is .clang-format - #10 by sterretje has a zip that uses the allman.

Further follow information provided by @ptillisch here and in the linked topic.

Change:
"BreakBeforeBraces: Attach"
to:
"BreakBeforeBraces: Allman"

@johnwasser , one problem is that by default the file with the formatting settings does not exists :wink:

The tutorial does link to the file containing the official Arduino code style. So you have the option of downloading that file and making any specific adjustments you like instead of starting from a blank page.

But sterretje provided a link to a modified version of that file that already has the BreakBeforeBraces: Allman modification (and maybe some other things too?).

Can't remember; I don't think that there was another change. But OP can compare the original from your download link with the one I provided :wink:

1 Like

Thanks for bringing up this issue and for @sterretje sharing his format file.

I first tried the .clang-format in my sketch folder and tested the Allman and Attach options.
Then I deleted it and moved it to my ~/arduinoIDE folder, I install the nightly via zip, and it took those settings system wide. Linux/Kubuntu 20.04.5

Along the same lines, is there a way to PREVENT auto-format from changing a particular block of lines?
eg to leave this "as is" (without the line break)

if (someCondition) {// do stuff if the condition is true} else {// do stuff if the condition is false}

rather than

if (someCondition) {
// do stuff if the condition is true
} 
else {
// do stuff if the condition is false
}

the various options I looked at in the link
https://clang.llvm.org/docs/ClangFormatStyleOptions.html#breakbeforebraces

make the code spread complex functions across multiple pages with not much text - so it looks like this, wastes paper and makes the code hard to follow.

Yes. It is documented here:

https://clang.llvm.org/docs/ClangFormatStyleOptions.html#disabling-formatting-on-a-piece-of-code

int formatted_code;
// clang-format off
    void    unformatted_code  ;
// clang-format on
void formatted_code_again;
2 Likes

Thank you @ptillisch and @sterretje !! This is wonderful news, the solution (and ready-modified .clang-format) seem to work quite well.

Funny how such a small thing such as curly brackets and their placement can influence the ability to think and write code, bringing it to a stop if not "correct".

Thank you again.

You are welcome. I'm glad if I was able to be of assistance.

Regards,
Per

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.