This is a code-beautifier feature that reformats source code.
Source Insight’s code formatter works with C/C++, C#, Java and other language files that have similar curly-brace syntax.
There are several different code formatting presets, and you can save additional presets. A preset is a group of formatting options. For example, there is a K&R preset, and a GNU preset.
To reformat your source code:
1.Either select a block of code, or leave an insertion point in your file to format the whole file.
2.Select Tools > Reformat Source Code Options
3.The Reformat Source Code Options dialog will appear. Select your desired preset and click the Format button.
The preset you select will become the "current preset". Once selected, you can use the Tools > Reformat Source Code with xxx Preset command to reformat your code in one step.
This dialog lists the formatting presets that are defined. To use a preset to format your source code, select the preset and click the Format button. You can also add, delete, import, and export presets.
Format
Click this button to format your source code using the selected preset.
Add / Delete
Click these to add or delete your own presets. The presets that are installed by default cannot be deleted.
Edit Preset...
Edits the code formatting options for the selected preset.
Import
Imports a single preset from a preset XML file.
Export
Exports the selected preset to a preset XML file.
Export All
Exports the list of presets to a preset XML file.
Restore Defaults
Restores the default presets that are installed with Source Insight.
Each formatting preset contains a group of formatting options. To view and edit a preset's formatting options, click the Properties button in the Reformat Source Code dialog box. The Preset Properties dialog will open.
This tab contains options that control the general flow of source code around control statements and curly braces.
Control Statements Options
Control statements start a new line
Each control statement (i.e. if, while, for, etc.) will start on its own line.
For example:
x = 0; while (x < limit)
would be converted to:
x = 0;
while (x < limit)
Blank line before control statements
A blank line is inserted just above the control statement.
Blank line after control statements
A blank line is inserted after the control statement's block of code.
Keep else-if on same line
The "if" following an "else" will appear on the same line as the "else", instead of breaking the "if" statement onto its own line.
Force single line bodies onto next line
This forces the statement body onto a new line. For example:
if (abc) x = 0;
would be converted to:
if (abc)
x = 0;
Add braces around single statement bodies
A single statement body would be converted to a brace block. For example:
if (abc)
x = 0;
would be converted to:
if (abc)
{
x = 0;
}
Space after keywords: if, while, for, etc.
Control keywords such as if, while, for, etc. are followed by a single space.
Brace Style Options
Open { brace on new line
The open brace will start on its own line. For example:
if (abc)
{
If this box is not checked, the output looks like the code below. This is used by the K&R style preset:
if (abc) {
Indent the open { brace
Indents the open brace by one indent level (if the open brace is the first character on the line). For example:
if (abc)
{
If this box is not checked, the output will look like this:
if (abc)
{
Indent the close } brace
Indents the closing } brace by one level. For example:
if (abc)
{
x = 0;
} // closing brace is indented
If this box is not checked, then the output will look like this:
if (abc)
{
x = 0;
} // closing brace is not indented
Extra indent of statements inside of braces
Adds one more level of indentation to the block of statements inside of the curly braces. For example:
if (abc)
{
x = 0; // indented 1 more level inside of braces
y = 1;
}
Outermost braces at column 1
The outer-most open and close curly braces are placed at the far left (column 1). This is used typically for the braces around function or class bodies, even when the inside braces follow different indent rules. For example:
void MyFunction()
{ // open brace at column 1
if (abc)
{ // inner braces are still indented according to other options
}
}
Indentation Options
Convert spaces to tabs
Spaces are converted to the equivalent number of tabs, or tabs plus spaces.
Indent width
This is the width in character positions of each indent level. For example, entering 4 would mean that an indent is equivalent to 4 spaces, or 1 tab is the tab width is set to 4.
Label Indentation Options
Indent with statements
Labels are indented at the same level as the surrounding statements. For example:
label1:
if (abc)
{
label2:
x = 0;
}
At far left edge
Labels are indented all the way to left, regardless of the indentation level of the surrounding statements. For example:
label1:
if (abc)
{
label2: // both labels are at column 1
x = 0;
}
Hanging indent
Labels are indented 1 level less than the surrounding code. For example:
if (abc)
{
label1:
if (xyz)
{
label2:
x = 0;
}
}
Blank line above labels
Adds a blank line above each label statement. For example:
x = 0;
L1: // label has blank line above it
y = 1;
Blank line below labels
Adds a blank line below each label statement.
This tab contains options that control how classes, structures and comments are formatted.
Classes and Structs Options
Indent members
The members, or the contents of the body of the class or struct is indented by one level. This applies to class, struct, enum, and unions. For example:
class MyClass
{
void func1(); // member declaration is indented 1 level
}
Blank line above public/private labels
Labels inside the class declaration, such as private, public, protected, etc. will have a blank line above them.
Blank line below public/private labels
Labels inside the class declaration, such as private, public, protected, etc. will have a blank line below them.
Align field members at column #
This aligns the declared names of data members at a particular column. For example:
class MyClass
{
alignment here---> <-----
int m1; // m1 and m2 are aligned
int m2;
}
Align bit-field sizes at column #
This aligns the bit field sizes of data members at a particular column. For example:
class MyClass
{
alignment here---> <----
int bitfield1 : 8 ; // bit field : are aligned
int bitfield2 : 24;
}
Indent labels for public/private
Indent with statements
Labels are indented at the same level as the surrounding statements. For example:
class MyClass
{
public:
int func1();
private:
int func2();
}
At far left edge
Labels are indented all the way to left, regardless of the indentation level of the surrounding statements. For example:
class MyClass
{
public:
int func1();
private: // label is always at column 1 even if members are indented
int special_function();
int another_special_function();
int func2();
}
Hanging indent
Labels are indented 1 level less than the surrounding code. For example:
class MyClass
{
public:
int func1();
private:
int func2();
class InnerClass
{
public:
int func3();
}
}
Comment Options
Preserve blank lines in comments
Blank lines completely inside of multi-line comments are preserved. For example:
/*
Description:
The blank line above this line is preserved.
*/
Blank line above comment blocks
This inserts a blank line above a block of statements following comments. This attempts to identify a block of statements that follows one or more comment lines. This presumes the comments are referring to the block of statements that follow it. For example:
a = 0;
// Initialize block - some comment before a "block" of statements
// (The blank line above this block is inserted by this formatting option.)
s.x = 1;
s.y = 14;
Wrap comment line at right margin
Multiline comments are word-wrapped at the right margin. The right margin value is defined by the file type. See: File Type Options.
For example:
right margin ---------------------------->|
/* some really long long long long long
long wordy wordy wordy comment that is
word wrapped at the right margin*/
Preserve text formatting in lines that contain only comments
This preserves extra spaces and tabs inside of comments if the comment is the only thing on the line. This typically preserves the author's intentional formatting of the comment. For example:
/*
Field1 Description
------- -----------
something something else in table format
*/
Align right-hand comments at column #
Aligns comments at the end of a line to a specific column. For example:
alignment here ---> <---
x = 0; // right hand comment
y = 1; // another right hand comment
Other Options
Indent namespace contents
Indents the contents of a C++ or C# namespace. Everything inside the namespace is nested by one level, so this affects whether that nesting affects the indentation. For example:
namespace N1
{
int n1_x;
void n1_func();
}
If this box is not checked, then the formatting would look like this:
namespace N1
{
int n1_x;
void n1_func();
}
This tab contains options that control how white space and line space is handled, as well as preprocessor macros.
White Space Options
Space between function name and parentheses
Adds a space between function names and the open parentheses that follows. For example:
myfunction (x) // note the space before the parameter list.
Spaces between function parameters
Adds a space between function parameters. For example:
myfunction(x, y, z) // note spaces after commas
Spaces around operators
Adds spaces around binary operators in expressions, such as + and -. Some examples:
a + b * c
a < b
a += b
If this box is not checked, then the above code would be formatted like this:
a+b*c
a<b
a+=b
Spaces inside parentheses like "( this )"
Adds spaces after the open parentheses, and before the closing parentheses in expressions and function parameter lists. For example:
myfunction( a )
if ( x > 0 )
If this box is not checked, then the above code would be formatted like this:
myfunction(a)
if (x > 0)
Remove extra white space at end of lines
All extra spaces and tabs at the end of lines are stripped.
Align variables at column #
This aligns the declared names of variables at a particular column. For example:
alignment here---> <-----
int x1; // x1 and x2 are aligned
int x2;
Align assignments at column #
This aligns assignment operators at a particular column. For example:
alignment here---> <-----
int x1 = 0; // x1 and xyz are aligned
int xyz = 0;
Line Spacing Options
Preserve blank lines in source
This preserves any completely blank line in the original source. If this box is not checked, then unnecessary blank lines are removed. Some blank lines are automatically inserted by some formatting options.
Blank lines between functions #
Inserts the specified number of blank lines just above function declarations.
Preprocessor Macros (#defines) Options
Align macro names at column #
This aligns the declared names of preprocessor macros at a particular column. For example:
alignment here---> <-----
#define SUM(a, b) ((a) + (b))
#define ABCD(x) (x)
Align macro values at column #
This aligns the substitution value of preprocessor macros at a particular column. For example:
alignment here---> <-----
#define SUM(a, b) ((a) + (b))
#define ABCD(x) (x)
Align macro continuation \ at column #
This aligns the line-continuation character of preprocessor macros at a particular column. For example:
alignment here---> <-----
#define SUM(a, b) \
((a) + \
(b))