Document Formatting (C#)
- 4 minutes to read
Document Formatting extends Visual Studio's Code Formatting features to enforce code formatting standards in aspects Visual Studio does not cover.
You can execute the Format Document action in one of the following ways:
- Enable "Format document" on the Editor | C# | Code Cleanup options page and execute the Code Cleanup.
- Enable "Format block on }" and/or "Format statement on ;" options on the Editor | C# | Formatting | General options page to maintain formatting as you type.
- Create a new shortcut and bind it to the "FormatDocument" command. This shortcut executes the Document Formatting.
CodeRush provides the following Document Formatting features:
Spacing
Code Spacing enables you to add or omit whitespace symbols around the following code elements:
Arithmetic Expression
- Spaces around an operator
Relational Expression
- Spaces around an operator
Bitwise Expression
- Spaces around an operator
Logical Expression
- Spaces around an operator
Unary Expression
- Space before a postfix unary operator
- Space after a prefix unary operator
Ternary Expression
- Space before a question mark
- Space after a question mark
- Space before a colon
- Space after a colon
Assignment Expression
- Spaces around an operator
Expression
- Space before parentheses
- Spaces within parentheses
Statement
- Space before parentheses
- Spaces within parentheses
- Space before a semicolon
- Space before semicolons in 'for' loops
- Space after semicolons in 'for' loops
- Space before a colon in 'case' expressions
Arguments
- Space before parentheses
- Spaces within parentheses
- Space within empty parentheses
- Space before a separator (comma)
- Space after a separator (comma)
- Space before a colon in a named parameter
- Space after a colon in a named parameter
Parameters
- Space before parentheses
- Spaces within parentheses
- Space within empty parentheses
- Space before a separator (comma)
- Space after a separator (comma)
Initializer
- Spaces within braces
- Space before a separator (comma)
- Space after a separator (comma)
Lambda Expression
- Spaces around an arrow
Expression Body
- Spaces around an arrow
Type Parameters
- Space before angle brackets
- Spaces within angle brackets
- Space before a separator (comma)
- Space after a separator (comma)
- Space before a colon
- Space after a colon
Type Arguments
- Space before angle brackets
- Spaces within angle brackets
- Space before a separator (comma)
- Space after a separator (comma)
Attributes
- Space before a colon in target specifier
- Space after a colon in target specifier
- Spaces within brackets
Null Coalescing Expression
- Spaces around an operator
Base List
- Space before a colon
- Space after a colon
- Spaces before commas
- Spaces after commas
Constructor Initializer
- Space before a colon
- Space after a colon
You can see an example of the changes every item makes using the preview box at the bottom of the Editor | C# | Formatting options page.
Wrapping
Code Wrapping enables you to configure the following code elements' wrapping rules:
- LINQ Expression
- Arithmetic Expression
- Relational Expression
- Bitwise Expression
- Logical Expression
- Ternary Expression
- Assignment Expression
- Arguments
- Parameters
- Array Initializer
- Collection Initializer
- Object Initializer
- Chained Method Calls
- Lambda Expression
- Expression Body
- Type Parameters
- Null Coalescing Expression
- Base List
- Constructor Initializer
Each element of the expressions above has separate configuration options. You can set the following wrapping options:
How expression elements should be wrapped:
- No wrap
- Wrap if Long or Multiline (wrap each expression element if the line exceeds the margin by adding the maximum number of line breaks)
- Simple Wrap (wrap only those expression elements that exceeds the margin by adding the minimum number of line breaks)
- Always Wrap
- The line break's position (before or after the element)
- The wrapped line alignment (which element of the first line should be used to align the wrapped lines)
- Extra constant indentation of the wrapped lines
Line Breaks
Line Breaks configuration includes two options pages:
Editor | C# | Formatting | Blank Lines — configures the number of blank lines around and within the following code elements:
- Region
- Namespace
- Type
- Method
- Property \ Event
- Field
Editor | C# | Formatting | Braces — configures the line breaks around and within the following code blocks:
- Namespace
- Type
- Method
- Property \ Event
- Accessor
- Lambda Expression \ Anonymous Method
- Statement
NOTE
Line Breaks feature supports non-destructive mode in which CodeRush does not remove the existing line breaks and only adds them to follow the configured style. You can activate this mode on the Editor | C# | Formatting | General options page.
You can also configure the line break before else, while, catch and finally keywords. This feature is configurable using the Place On New Line item on the Editor | C# | Formatting | Other options page.