cancel
Showing results for 
Search instead for 
Did you mean: 

Code formatting style

CSharpino
Associate III

Since I upgraded to version 3.6.4 of STM32CubeIDE for Visual Studio Code, the style used for automatic code formatting has changed. For example, curly braces are now inlined, not wrapped as they were before.
How can I revert to the old style?

 
1 ACCEPTED SOLUTION

Accepted Solutions

Good day @CSharpino I am assuming the attached clang-format.txt file was renamed to clang-format.txt so that it could be attached to the comment. Please note that if you used the file as is, clangd would default to the LLVM style, which formats braces on the same line. Also note that I used your clang-format file and encountered an error. I believe it was AlignFunctionDeclarations. This is not supported by the starm-clangd executable and clangd is very unforgiving to non-recognised keys. This will result in it defaulting to LLVM style, which may be the case for you.

If you have generated a clang-format file using a later version using a style, it will use every key available in its arsenal. My advice to you would be to make your own .clang-format file and copy out options from here Clang-Format Style Options — Clang 22.0.0git documentation based on what you want. Each option will have a version associated with it and if you ensure you only use options that are version 18 and less, you will be ok. This is time consuming, but it allows complete customisation of how you want things styled.

I have attached a clang-format file that I generated based on Microsoft style using clang-format version 18.1.3 and this correctly formats my files when I tested it. Please download this, rename to .clang-format and copy it to the root of your project. You can always tweak the options as you see fit.

 

Anyway, one thing that I do that prevents headaches like the one you encounter with conflicting extensions is to create profiles and only install extensions that are required so my best advice is to create a new profile, name it STM32 and only install the stm32cubeide extension pack.

View solution in original post

18 REPLIES 18
Adam Hamilton
Senior

Have you tried using clang-format?

I would imagine if you don't have a .clang-format file then your formatting might just take on a default LLVM style which uses inline braces.

If you have clang format installed, you can generate an appropriate clang-format file using the following command in the root of your project.

clang-format --style=Microsoft --dump-config > .clang-format

You can then customise this file. There are of course other styles you can use, see this link ClangFormat — Clang 22.0.0git documentation

This should allow the clangd extension that is installed by STM32CubeIDE extension to format the code when you trigger the formatting command

t_j_l
Associate

Yes, I have also encountered this situation and felt extremely uncomfortable.

vincent_grenet
ST Employee

STM32CubeIDE for Visual Studio Code does not force any formatting features.

We promote clangd to support code indexing, enabling code navigation and enhancing the VSCode editor experience. Clangd is a utility toolbox capable of static code checking and code formatting. However, we do not provide any settings that enforce its usage but it may be used.

Please check your local VSCode setup via the command palette:

vincent_grenet_0-1762414924954.png

Try "Format Document With..." while a .c file is active:

vincent_grenet_1-1762415000992.png

You control which formatter to use.

vincent_grenet_2-1762415064032.png
PS: Without any third-party formatting extensions (like Prettier or others from the marketplace), VSCode defaults to the only available formatter.
 
 

I’m taking this opportunity to ask you about formatting features. We are considering promoting some optional formatting tools. Please don’t worry; we don’t want to force anyone but simply offer an additional optional service.

The rationale would be to provide formatting capabilities aligned with the style we apply internally to our drivers, middleware, and example code bases delivered as part of the STM32Cube ecosystem. This would improve the user experience without reformatting everything in case local setups that most end users may not focus on, which often causes surprising source control diffs. What do you think? Would this be valuable?

To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.
CSharpino
Associate III

I installed LLVM and used the clang-format command to generate the .clang-format file in the root of my application, but nothing changed. The brackets still move inline.
I also tried uninstalling the STM extensions, and the formatting works correctly again. The incorrect style starts to appear when I install the STM extensions and disable the C/C++ extension's intellisense, as required by the STM extension.

The format file I'm using is the one attached.

 

@CSharpino to "help" you in the short term, please be aware that the clangd extension promoted as part of the STM32CubeIDE for Visual Studio Code extension pack is optional. You can easily disable or remove it. For more details, see: https://community.st.com/t5/stm32cubeide-for-visual-studio/quot-stm32cube-for-visual-studio-code-quot-extension-pack/td-p/836206

I’m convinced you can still get everything working locally while keeping it enabled, but the choice is yours.

To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.

If STM32CubeIDE doesn't enforce any formatting features, why am I faced with files formatted differently after updating this extension?
And why does the formatting work correctly again when I remove all STM32 extensions?
And why does it not work correctly again when I reinstall STM32CubeIDE extensions?

Have you found a solution to this problem?
I managed to get back the formatting I used before by simply removing the STM clangd extension and re-enabling the intellisense and formatting features of the Microsoft C/C++ extension.

@CSharpino as shared Thanks my very first answer VSCode takes itself formatter contribution if any.
If you start from none VSCode has nothing to take.
Then you add STM32CubeIDE for Visual Studio Code which is serving a formatter engine solution (without forcing it) so VSCode is able to take one .. this one.
Uninstalling then VSCode is back to initial state ie. nothing to take. 
Here is rational according to my understanding.

To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.

But I wasn't starting from a situation where I didn't have a default formatter; my formatter (like that of many others) is the one provided by the Microsoft C/C++ extension, as shown in the image.

CSharpino_0-1762425654998.png

This formatter, however, is disabled by the STM extension's request to disable the C/C++ extension's intellisense because it conflicts with yours, essentially forcing your extension to become the default.
The solution I found was to delete the STM32 clang extension and re-enable C/C++ intellisense.
However, these forced changes should be avoided or at least documented to avoid wasting days of work, given that the C/C++ extension has 90 million downloads, so it's quite likely present in people's VSCode.