Formatting

From RAD Studio
Jump to: navigation, search

Go Up to C++ Options


Tools > Options > Language > C++ > Formatting

C++ formatting options

The code formatter is used for changing the layout of your code: these are stylistic choices such as where you have newlines, where braces are located, how much and what is indented, and so forth.

Usually, this is done to match a particular coding standard, either a common one, your own personal, or a company one. C++Builder implements this through the clang-format tool.

Format code in the editor for selected code, a whole unit, or via a command-line tool. In-IDE is useful to format the code you are working on right now. The command-line tool is useful for automatic formatting when committing code, formatting your code in bulk, or similar operations.

In pages under C++, you can set options to be used in C++ projects for C/C++ source code files with the following extensions: *.cpp, *.cxx, *.cc, *.c, *.hpp, *.hxx, *.hh, *.h.

Formatting behavior

Item Description

Automatic formatting

Format your code manually or automatically when you save or format on newline. Choose from:

  • Not automatic - formats code only when you manually invoke it by pressing Ctrl+D. The formatter will format the selected code in the editor or the entire file if nothing is selected.
  • Format on save - formats code upon saving the file.
  • Format on new line - formats code each time you press Enter to add a new line.

Format on new-line line limit

If you choose to automatically format on a new line, the formatter will not be invoked and formatting will not be applied once if the code is longer than this number of lines. This is because clang-format can take a noticeable amount of time to format longer units. The default is set to 1000.

Formatting style

Click the drop-down menu to show the list of predefined formatting styles: LLVM, Google, Chromium, Mozilla, WebKit, and Embarcadero. The default is Embarcadero.

Custom formatting

You can create clang-format presets that show in this setting just like the default Embarcadero, Mozilla, etc settings, or override format settings on a per-project basis.

Per-project overrides

The clang-format convention is for projects to specify their formatting style in a .clang-format formatting configuration file in the source folder (note the filename begins with a dot). If the IDE finds a .clang-format configuration file associated with your project source, it uses that formatting when formatting code.

When no configuration file is found, the IDE uses the style defined by this setting as a fallback. This means that the IDE’s settings are used unless overridden on a per-project basis and that the IDE will respect per-project overrides using the convention of providing a .clang-format file. This is common in open-source C++ libraries.

New IDE-wide configuration

You can create new configuration files storing formatter options and make them available to the IDE as a fallback for all projects. To do so, the file must:

  • Match the Formatter_*.clang-format file mask.
  • Be located in the RAD Studio working directory: C:\Users\<username>\AppData\Roaming\Embarcadero\BDS\22.0.

This causes the name of the formatter to appear as an option in the ‘Formatting style’ setting.

Edit or create files based on pre-existing configurations. For more information on how to write a configuration file, see Clang Format Style Options.

Note: New files display in the Formatting style drop-down menu next time the IDE is started.

Show Confirmation dialog

When selected, the Confirmation prompting message appears when you run any formatting command.

Preventing formatting

Prevent formatting on specific sections by surrounding code you do not want formatted with comments as follows:

// clang-format off
Code here will not be formatted
// clang-format on

Turning formatting off via a comment pauses formatting until the next 'on' comment is found. You can use comments in single line format (//) or multi-line (/**/) format.

Clang-format Command Line

You can invoke the formatter on the command line. It is called clang-format.exe. Clang-format provides no option to choose a formatting configuration file, and always looks for a local .clang-format file, so to use the Embarcadero formatting style you will need to copy Embarcadero.clang-format to your source folder, renaming it to .clang-format.

See Also