JetBrains Rider 2023.3 Help

Using Clang-Format

In C++ files JetBrains Rider supports formatting styles defined in Clang-Format. By default, Clang-Format configuration files get automatically used as a source of formatting style settings.

What is Clang-Format and how does JetBrains Rider support it?

Clang-Format is a widely-used C++ code formatter. It provides an option to define code style options in YAML-formatted files — named .clang-format or _clang-format — and these files often become a part of your project where you keep all code style rules.

To see the full list of Clang-Format style options and examples of configuration files, check out the Clang-Format documentation.

There are two ways to make use of Clang-format in JetBrains Rider:

  • If your project happens to have its code styles configured in Clang-Format files, JetBrains Rider's code formatting engine can use most of these styles for formatting C++ files in addition to its original styles. Note that settings from .clang-format files will override settings from *.DotSettings if there are any conflicts.

  • If you prefer the default LLVM-style formatting and want to use clang-format as code formatter, you can choose Clang-format under C++ Formatting Engine on the Editor | Code Style | C++ | General page of JetBrains Rider settings Ctrl+Alt+S, and optionally specify a custom Clang-format executable. In this case, the .clang-format configuration files will be read automatically.

Clang-Format in your solution

By default, JetBrains Rider takes into account Clang-Format properties even if you opt for ReSharper C++ formatter engine, and they will override preferences defined in JetBrains Rider settings as well as EditorConfig styles. If you want JetBrains Rider to ignore Clang-Format styles for code formatting and code syntax , clear the corresponding checkbox on the Editor | Code Style | C++ | General page of JetBrains Rider settings .

When Clang-Format support is enabled and there are .clang-format or _clang-format files that affect the current file, JetBrains Rider will help you understand which Clang-Format styles are applied and where these settings come from:

  • On JetBrains Rider formatting options pages for C++ , you will see a yellow warning if at least one preference on the page is overridden by Clang-Format styles for the current file, each overridden preference will also be highlighted with yellow. For example:

    Code formatting options overridden by Clang-Format styles
  • In the Code Style Configuration dialog, you can see and study all .clang-format and _clang-format files that affect the current file:

    JetBrains Rider. Code Style Configuration dialog

Export code style settings to Clang-Format

If you are going to share code style settings via Clang-Format, you may want to export the styles already configured in JetBrains Rider to the .clang-format file.

  1. Press Ctrl+Alt+S or choose File | Settings (Windows and Linux) or JetBrains Rider | Preferences (macOS) from the menu , then choose Editor | Code Style | C++ | General on the left.

  2. Click Write current style to .clang-format. This will open the Export to .clang-format dialog.

  3. By default, JetBrains Rider writes the settings to a new .clang-format file in the root directory of the current solution. If one or more .clang-format files exist, JetBrains Rider will use the closest file in the directory hierarchy to save the settings. If necessary, you can change the destination file using the path selector at the top of the dialog.

  4. If there is an existing .clang-format file in the specified location, there could be conflicts between values of properties that exist there and the ones that JetBrains Rider is about to save. Such properties will be shown in red and if you proceed with saving, the conflicts will be resolved in favor of the newly generated ones.

  5. Click Export. JetBrains Rider will create or update the .clang-format file in the specified location.

You can also save formatting styles to Clang-Format after contextual configuration of formatting rules.

Supported Clang-Format Options

BasedOnStyle

AlignConsecutiveAssignments

AlignConsecutiveDeclarations

AlignAfterOpenBracket

AlignOperands

AlignTrailingComments

AllowShortCaseLabelsOnASingleLine

AlwaysBreakAfterDefinitionReturnType

AlwaysBreakAfterReturnType

AlwaysBreakTemplateDeclarations

BinPackArguments

BinPackParameters

BreakBeforeBraces (except for Custom and class definition in Linux)

BreakBeforeTernaryOperators

BreakConstructorInitializersBeforeComma

ColumnLimit

ConstructorInitializerAllOnOneLineOrOnePerLine

IncludeBlocks

IncludeCategories

IncludeIsMainRegex

IndentCaseLabels

IndentWidth

IndentWrappedFunctionNames

MaxEmptyLinesToKeep

NamespaceIndentation

PointerAlignment

SortIncludes

SpaceAfterCStyleCast

SpaceAfterTemplateKeyword

SpaceBeforeParens

SpacesInAngles

SpacesInParentheses

SpacesInSquareBrackets

TabWidth

UseTab

Last modified: 21 March 2024