Getting started with Clang-Format Detector

Horatiu Prica Horatiu October 30, 2020

Are you trying to get started with Clang-Format? Not sure what Format Options to use or what is the best format style for you and your projects? Are you seeking to improve your existing Clang-Format style? Clang Power Tools has you covered with its new Clang-Format Detector.

  1. What is Clang-Format Detector
  2. Launch the Clang-Format Detector
  3. Select your code files for detection
  4. Detecting your Clang-Format style
  5. Polishing the detection
  6. Video Tutorial
  7. Wrap up

What is Clang-Format Detector

In a previous article, we talked about the Clang-Format Editor, which lets you write code, and then through trial and error, find a format style. From this concept, we started to build a new tool that makes the whole experience a lot faster and smoother by automatically detecting your selected code files' closest matching Clang-Format style.

So let’s begin by looking at how you can start using the Clang-Format Detector.

Launch the Clang-Format Detector

To access the Clang-Format Detector go to Settings > Format tab > Detect Format Style.

Now you are prompted to select the files which you want to use in the detection process.

File Select

Select your code files for detection

Press Browse and select the files you want to use in the detection process.

Large File Highlight

💡 Larger files increase the detection time and are marked in the list.

For this example, we’ll remove MemoryUtil.cpp so that the detection time will be shorter.

Once you have decided what files to keep for detection, press Detect Format Style to begin the operation.

Detecting your Clang-Format style

After pressing the Detect Format Style button, the detection process begins. It will take some time, depending on the number of files and their size.

Detecting Style

When the detection operation finishes, you are prompted with the Detection Results.

Detection Results

As you can see from the results, the Base Style detected is Google, and the Format Options that most closely match the selected files' style are next.

After inspecting the Detection Results summary, press Continue to see their effects on your select files with the help of the built-in diff.

Polishing the detection

In the Clang-Format Generator window, you can see the effects of Clang-Format on your code by using the diff. You can also do any required tweaking and tinkering to the Style Options.

Diff Format

💡 Bolded names mark options that are modified from their base style value and will be included in the .clang-format file.

  1. Diff view
    • ▪ File - select the file to view
    • ▪ Format Preview - see the effects of the Clang-Format Style Options on the selected file
  2. Style Options
    • ▪ Option - contains name and value
    • ▪ Reset / Reset All - reset the option / options to their detected values
    • ▪ Description - option description
  3. Command Buttons
    • ▪ Preview Style - updates the diff view by applying the Clang-Format Style Options
    • ▪ Generate Clang-Format - create the .clang-format file on your local machine

Once you are satisfied with your style and how it formats your code, it is time to press Generate Clang-Format to create the .clang-format file. Now you are ready to start formatting your code.

Video tutorial

Wrap up

Now that you had a quick look at how Clang-Format Detector works and how it can help you find the best formatting style for your project, it is time to start experimenting and find the best fit for you..

For any suggestions or bugs to report, let us know on our GitHub.