ReSharper 2019.3 Help

Using Clang-Format

Starting from version 2017.2, in C++ files (and partly in JavaScript and TypeScript files) ReSharper 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 ReSharper support it?

Clang-Format is a widely-used C++ code formatter. As it provides an option to define code style options in YAML-formatted files — named .clang-format or _clang-format — 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.

If your project happens to have its code styles configured in Clang-Format files, ReSharper's code formatting engine will automatically use most of these styles for formatting C++ files, and some of them in JavaScript and TypeScript files.

Clang-Format in your solution

By default, Clang-Format properties are enabled; they will override preferences defined in ReSharper and Visual Studio options as well as EditorConfig styles. If you want ReSharper to ignore Clang-Format styles for code formatting and code syntax, clear the corresponding checkbox on the Code Editing | General Formatter Style page of ReSharper options.

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

  • On ReSharper formatting options pages for C++, JavaScript, and TypeScript, 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 File Formatting Info window, you can see and study all .clang-format and _clang-format files that affect the current file:

    ReSharper. File Formatting Info window

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 ReSharper to the .clang-format file.

  1. Select ReSharper | Options in the main menu or press Alt+R,O, then choose Code Editing | General Formatter Style on the left.

  2. In the Code style configuration files section, click Write current style to .clang-format. This will open the Export to .clang-format dialog.

  3. By default, ReSharper 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, ReSharper 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. For more detailed configuration, you can expand Show additional options and choose the following options:

    • Export settings that have default values
      By default, ReSharper only saves properties for settings that you have changed. So only these settings will apply and override other settings when the resulting .editorconfig is read by ReSharper or Rider. All other settings will take values specified in the IDE.
      If you enable this option, ReSharper will save all settings in their current state, regardless whether they were changed or not. This way you will get a stricter .editorconfig that will override all code style and formatting settings when read by ReSharper or Rider.

    • Remove existing properties with ambiguous values
      Some EditorConfig properties could correspond to multiple ReSharper-specific properties, which allow for a more precise configuration. So there could be situations when a more general property agrees with some related ReSharper-specific properties and contradicts to others, which makes this more general property ambiguous.
      As more detailed properties have higher priority, such a situation is not a problem and there is no need to remove properties with ambiguous values.
      The only case when you may want to remove properties with ambiguous values is when the project EditorConfig styles are going to be read only by ReSharper and Rider, and you want to remove redundancies from the .editorconfig file.

    • Export standard .editorconfig properties — whether to save standard .editorconfig properties These properties will also apply if the generated EditorConfig is read by other editors and IDEs.

    • Export cross-editor language-specific properties — whether to save .NET-coding-convention properties. These properties will also apply if the generated EditorConfig is read by other editors and IDEs.

    • Export JetBrains Rider/ReSharper-specific code style properties — whether to save product-specific code formatting styles, code syntax styles.

    • Export JetBrains Rider/ReSharper-specific inspection severities — whether to save severity levels for ReSharper's configurable code inspections.

    • BasedOnStyle — sets the corresponding option to define which which style is used for all options by default.

    • Hide options that coincide with the chosen base style
      Options that have the same value as defined in the base style that is selected in BasedOnStyle will not be exported.

    • Export CPP settings — whether to export Clang-Format options that affect C++ code.

    • Export CPP settings — whether to export Clang-Format options that affect JavaScript code.

  5. 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 ReSharper 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.

  6. Click Export. ReSharper 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

Option

Supported in languages

BasedOnStyle C++
AlignConsecutiveAssignments C++
AlignConsecutiveDeclarations C++
AlignAfterOpenBracket C++
AlignOperands C++
AlignTrailingComments C++
AllowShortCaseLabelsOnASingleLine C++
AlwaysBreakAfterDefinitionReturnType C++
AlwaysBreakAfterReturnType C++
AlwaysBreakTemplateDeclarations C++
BinPackArguments C++
BinPackParameters C++

BreakBeforeBraces (except for Custom and class definition in Linux)

C++
BreakBeforeTernaryOperators C++
BreakConstructorInitializersBeforeComma C++
ColumnLimit

C++, JavaScript, TypeScript

ConstructorInitializerAllOnOneLineOrOnePerLine C++
IndentCaseLabels C++
IndentWidth

C++, JavaScript, TypeScript

IndentWrappedFunctionNames C++
MaxEmptyLinesToKeep C++
NamespaceIndentation C++
PointerAlignment C++
SpaceAfterCStyleCast C++
SpaceAfterTemplateKeyword C++
SpaceBeforeParens C++
SpacesInAngles C++
SpacesInParentheses C++
SpacesInSquareBrackets C++
TabWidth

C++, JavaScript, TypeScript

UseTab

C++, JavaScript, TypeScript

This feature is supported in the following languages and technologies:

Language: C# Language: VB.NET Language: C++ Language: HTML Language: ASP.NET Language: Razor Language: JavaScript Language: TypeScript Language: CSS Language: XML Language: XAML Language: Resx Language: Build Scripts Language: Protobuf Language: JSON
Feature is not available Feature is not available Feature is available Feature is not available Feature is not available Feature is not available Feature is available Feature is available Feature is not available Feature is not available Feature is not available Feature is not available Feature is not available Feature is not available Feature is not available
Last modified: 16 April 2020