Formatting Source Code
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 > Language > Formatter), you can set formatting options for Indentation, Spaces, Line Breaks, and Align.
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.
Contents
- 1 To enable the formatter
- 2 To customize the formatter options
- 3 To format the source code file
- 4 To format the source code block
- 5 To format the source code files in a project
- 6 To use formatter profiles
- 7 To save the customized set of formatter options in a file
- 8 To load the set of formatter options from a file
- 9 See Also
To enable the formatter
- Click Tools > Options > Language > Formatter.
- In the tree in the left pane of the Options dialog box, expand the Formatter node.
- Select the Profiles and Status node -- the Formatter Profiles and Status page opens.
- Select Enable Formatter.
- 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
- Click Tools > Options > Language > Formatter.
- In the tree in the left pane of the Options dialog box, expand the Formatter node. The Delphi subtrees open.
- 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
. For more information see Formatting. - Delphi contains the Indentation, Spaces, Line breaks, Capitalization, and Alignment 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
.
- The C++ options are used in C++ projects to format C/C++ source code files with the following extensions:
- Select a group of options; the right pane displays options in the selected group.
- Select any option; a detailed description of this option appears in the lower part of the right pane.
- 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.
- Select the desired value.
- Click OK. The specified set of formatter options will be used by the source code formatter.
To format the source code file
- Open your Delphi or C++ source code file in the Code Editor.
- Set the desired formatting options (if needed).
- Right-click in the Code Editor and select the Format Source command from the context menu (or click the Edit > Format Source menu command.)
- If the Confirmation prompt opens, click Yes.
- The Code Editor pane refreshes and shows the source code formatted according to the specified formatter options.
To format the source code block
- Open your Delphi or C++ source code file in the Code Editor.
- Set the desired formatting options (if needed).
- Select a block of code.
- Right-click in the selected block and select the Format Source command from the context menu (or click the Edit > Format Source menu command.)
- If the Confirmation prompt opens, click Yes.
- 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
- In the Project Manager, select a project to be active.
- 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.
- 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
- Click Tools > Options > Language > Formatter.
- In the tree in the left pane of the Options dialog box, expand the Formatter node.
- Select the Profiles and Status node; the Formatter Profiles and Status page opens.
- 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\23.0
.
- Match the
- Profiles are the special kind of files storing the formatter options. To be shown in the Profiles list, a file must:
- The standard set of profiles includes
Formatter_Compact.config
,Formatter_Default.config
, andFormatter_Wide.config
.
- The standard set of profiles includes
- Sets of formatter options stored in profiles can be treated as formatter-profiles.
- Click Apply. The set of formatter options stored in the selected profile becomes the active formatter options.
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
- Click Tools > Options > Language > Formatter.
- 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.
- 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 saving a file in the RAD Studio working directory.
- By default, the Save as type filter uses the
Profiles (Formatter_*.config)
file mask.
- In File name, specify the name of the file in which to save the current set of formatter options.
- 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
- Click Tools > Options > Language > Formatter.
- In the tree in the left pane of the Options dialog box, expand the Formatter node.
- 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 loading 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)
.
- 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
- In File name, specify the name of the file from which to load the stored set of formatter options.
- Click Open.
The set of formatter options stored in the specified file loads and becomes the current set of formatter options.