Formatting Source Code

From RAD Studio
Jump to: navigation, search

Go Up to How To Edit Code in the Code Editor


While editing Delphi or C++ code in the Code Editor, you can apply the Format Source context menu command to format the source code. Under the Formatter group in the Options dialog box (Tools > Options), you can set formatting options for Indentation, Spaces, Line Breaks, and Capitalization. Use the Delphi and C++ groups to specify separate sets of formatting options for Delphi or C++ source code files.

Notice that the source code formatter takes into account only the specified formatting options. It totally overwrites all your manual formatting implemented in the Code Editor.

To enable the formatter

  1. Click Tools > Options.
  2. In the tree in the left pane of the Options dialog box, expand the Formatter node.
  3. Select the Profiles and Status node -- the Formatter Profiles and Status page opens.
  4. Select Enable Formatter.
  5. Click OK. The source code formatting commands become enabled. That is, the Format Source and Format Project Sources commands appear in the corresponding menus (and context menus) and become available.
In the Profiles and Status page you can also manage sets of formatter options (using configuration files storing the formatter options) and enable or disable the Confirmation prompting message before the formatting operations.

To customize the formatter options

  1. Click Tools > Options.
  2. In the tree in the left pane of the Options dialog box, expand the Formatter node. The C++ and Delphi subtrees open.
    • The C++ subtree contains the Indentation, Spaces, Line breaks, and Align groups of options. The C++ options are used in C++ projects to format C/C++ source code files with the following extensions: *.cpp, *.cxx, *.cc, *.c, *.hpp, *.hxx, *.hh, *.h.
    • Delphi contains the Indentation, Spaces, Line breaks, Capitalization, and Align groups of options. The Delphi options are used in Delphi projects to format Delphi source code files with the following extensions: *.pas, *.dpr, *.dpk, *.dpkw, *.inc.
  3. Select a group of options; the right pane displays options in the selected group.
  4. Select any option; a detailed description of this option appears in the lower part or the right pane.
  5. To change the option value (or view all possible values of the option), click the right column of the option row and then click the emergent drop-down arrow. The list of all possible values opens.
  6. Select the desired value.
  7. Click OK. The specified set of formatter options will be used by the source code formatter.

To format the source code file

  1. Open your Delphi or C++ source code file in the Code Editor.
  2. Set the desired formatting options (if needed).
  3. Right-click in the Code Editor and select the Format Source command from the context menu (or click the Edit > Format Source menu command.)
  4. If the Confirmation prompt opens, click Yes.
  5. The Code Editor pane refreshes and shows the source code formatted according to the specified formatter options.

To format the source code block

  1. Open your Delphi or C++ source code file in the Code Editor.
  2. Set the desired formatting options (if needed).
  3. Select a block of code.
  4. Right-click in the selected block and select the Format Source command from the context menu (or click the Edit > Format Source menu command.)
  5. If the Confirmation prompt opens, click Yes.
  6. The Code Editor pane refreshes and shows the selected block formatted according to the specified formatter options. Only the formatting of the selected block will be updated.

To format the source code files in a project

  1. In the Project Manager, select a project to be active.
  2. Run the Format Project Source command:
    • Right-click in the Project Manager and select the Format Project Source context menu command or
    • Select the Project > Format Project Source menu command.
  3. The Format Project Sources confirmation prompt opens; click OK in it.

The formatter will format the sources of all source code files in this project and update files with the modified source codes.

To use formatter profiles

  1. Click Tools > Options.
  2. In the tree in the left pane of the Options dialog box, expand the Formatter node.
  3. Select the Profiles and Status node; the Formatter Profiles and Status page opens.
  4. In the Select profile group, click in Profiles to show the list of accessible profiles and select a profile. The selected profile name appears in Profiles.
Profiles are the special kind of files storing the formatter options. To be shown in the Profiles list, a file must:
  • Match the Formatter_*.config file mask.
  • Be located in the RAD Studio working directory: C:\Users\<username>\AppData\Roaming\Embarcadero\BDS\17.0.
The standard set of profiles includes Formatter_Compact.config, Formatter_Default.config, and Formatter_Wide.config.
Sets of formatter options stored in profiles can be treated as formatter-profiles.
  1. Click Apply. The set of formatter options stored in the selected profile becomes the active formatter options.
  2. That is, we can say that we load the formatter profile from the selected profile.

To save the customized set of formatter options in a file

  1. Click Tools > Options.
  2. In the tree in the left pane of the Options dialog box, expand the Formatter node. Customize the formatter options as it is described in the To customize the formatter options section.
  3. Select the Profiles and Status node; the Formatter Profiles and Status page opens. Click Save As. The Save Formatter Profile dialog box opens.
    • By default, this dialog box suggests to save a file in the RAD Studio working directory.
    • By default, the Save as type filter uses the Profiles (Formatter_*.config) file mask.
  4. In File name, specify the name of the file in which to save the current set of formatter options.
  5. Click Save.
Your customized set of formatter options will be saved in the specified file.

To load the set of formatter options from a file

  1. Click Tools > Options.
  2. In the tree in the left pane of the Options dialog box, expand the Formatter node.
  3. Select the Profiles and Status node; the Formatter Profiles and Status page opens. Click Load. The Load Formatter Profile dialog box opens.
    • By default, this dialog box suggests to load a file from the RAD Studio working directory.
    • By default, the Files of type filter uses the Profiles (Formatter_*.config) file mask.
The files with this mask located in the RAD Studio working directory are the so-called profiles. Therefore, by default, the list of files in the main pane shows only existing profiles. To show other files storing formatter options, in the Files of type filter, select Config files (*.config).
  1. In File name, specify the name of the file from which to load the stored set of formatter options.
  2. Click Open.
  3. The set of formatter options stored in the specified file loads and becomes the current set of formatter options.

See Also