- Joined
- Jun 17, 2024
- Messages
- 16
- Solutions
- 1
- Reaction score
- 23
- Points
- 3
Tutorial: Source Formatting with Clang Power Tools
The goal of this tutorial is to ensure that all developers on a project use the same code style rules. Here’s how to configure Clang Power Tools in Visual Studio to standardize code formatting.Step 1: Installing the Clang Power Tools Extension
- Open Visual Studio.
- Go to Extensions -> Manage Extensions -> Online.
- Search for Clang Power Tools and download the extension.
- Restart Visual Studio and accept any modifications.
- You should now see new action buttons at the top of Visual Studio.
Step 2: Configuring the Style File
- Click on the Settings button at the top of Visual Studio, then go to Format -> Clang Format Edit Open.
- Choose Open Configurator.
- At the bottom left, select the style you wanted. I prefer the LLVM style with some custom rules.
- Paste your code into the Input area to see the formatted version in the Output area.
- Adjust the formatting rules on the left to achieve your desired style.
- Important: Ensure that "SortIncludes" is set to FALSE to avoid build compilation errors.
- When satisfied with your style, click the Export .clang-format button.
- Save the file as .clang-format in your source folder.
- Important: If you name it different than just ".clang-format" it will not work !
I'm sharing the content of the .clang-format file, it's not perfect as everyone has their own preferences but it can give you a preset.
# Format Style Options - Created with Clang Power Tools
---
BasedOnStyle: LLVM
BreakBeforeBraces: Linux
SortIncludes: false
...
Step 3: Activating the Formatting
- Go back to Visual Studio in the Clang Power Tools Settings in the Format section.
- Choose Style -> file.
- Enable Format on save.
- Restart Visual Studio.
- Test the formatting on a file by pressing Ctrl+S.
- Verify that the result matches your expectations. If not, go back to the clang file configuration and adjust the rules.
Step 4: Formatting All Your Projects
- To format all the files in your project, right-click on it -> Clang Power Tools -> Format.
- Repeat this for each project.
Your project is now fully formatted, and any new code will be automatically formatted on save.
Don’t forget to share your .clang-format file with your collaborators to ensure consistent code style.
Hope you enjoyed this tutorial !