[Solved] Modify vscode code formatter (how it tidy code) with {}

Hi there!

I have setup vscode and platformio (in replacement of atom) and it works great.

There is only one little thing I find a bit annoying.

I use ctrl+shift+i to tidy my code (on ubuntu), shortcut are different on other platform (as far as I found on a forum, I didn’t test on win/mac):

  • On Windows Shift + Alt + F
  • On Mac Shift + Option + F
  • On Ubuntu Ctrl + Shift + I

The point is that opening “{” is always send to next line.
for example:

if (x == true) {
   x = x + 1;
} else {
   x = 2
}

will become

if (x == true)
{
   x = x + 1;
}
else
{
   x = 2
}

This is not extremely critical, but when you have a lot of loops, if, else etc… the code length is significantly increased and I like my code clean & compact. (I agree it’s purely a matter of taste…)

Is there a way to modify this ??

Code formatting is part of the C++ language plugin, not the PlatformIO plugin. Please refer to this repository: GitHub - microsoft/vscode-cpptools: Official repository for the Microsoft C/C++ extension for VS Code.

1 Like

What Max said. Specificially, I think you’re after the C_Cpp.clang_format_style option in the VSCode preferences. There, you could do something like the default fallback Visual Studio style of { BasedOnStyle: LLVM, UseTab: Never, IndentWidth: 4, TabWidth: 4, BreakBeforeBraces: Allman, AllowShortIfStatementsOnASingleLine: false, IndentCaseLabels: false, ColumnLimit: 0, AccessModifierOffset: -4 } but obviously configured how you please.

1 Like

Thanks for explanations and proposal ! :grinning:
I should be able to handle what I want based on this (and/or it provide several keywords to help any further search on this topic).
I’m away from vscode today but I will try tomorrow.
Best regards,

1 Like

Hi there,
I did not succeed using your suggestion (as proposed) @pfeerick, maybe I forget something…it’s high probability…this ways of settings preference, with json, is not very noobs friendly :slight_smile:
But based on the proposal I was able to do some more accurate search and I ended on this post:

Someone here suggested to simply use “webkit” as C_Cpp.clang_format_fallbackStyle

So, I get in vscode in files/preferences/settings, here I wrote ‘clang’ in the search field on top, C_Cpp.clang_format_fallbackStyle is the 1st search result.
I wrote here “Webkit” instead of default value “Visual Studio”, press enter, and that’s it.

Now the opening { are where I wanted them to be !
I hope this ‘webkit’ settings has no issue with arduino code :thinking:

Anyway, for now, problem is solved for me. I write this message just to share if some other are interested.

Thanks for your help !

1 Like

Yeah, it’s not the most user friendly of settings - would be nice if there was a GUI for that, but that’s an issue for Microsoft and VSCode to deal with.

Anyway, glad you figured it out… I was actually looking at the fallbackStyle parameter first since it was one of earlier results, but thought that other parameter would take priority… good to know for when I want to tell the formatter to format stuff how I want it to be formatted. It shouldn’t cause any issues with Arduino (C++) code - it’s only style / whitespace, not changes to the actual content.