Document Formatting (C#)

  • 4 minutes to read

Document Formatting allows you to enforce code formatting aspects unavailable in the Visual Studio’s Code Formatting feature.

Run Format Document Action

You can run the Format Document action in one of the following ways:

  • Check Apply in Action and Apply on Save for the “Format document“ rule on the Editor | C# | Code Cleanup options page and run a Code Cleanup.

    Formatting_Spaces

  • Enable “Format block on }“ and/or “Format statement on ;“ options on the Editor | C# | Formatting | General options page to maintain formatting as you type.

    Formatting_Spaces

  • Press Ctrl+. or Ctrl+~ to invoke the Code Actions Menu, select “Format Document” and press Enter.

    Formatting_Spaces

  • Create a new shortcut and bind it to the “FormatDocument“ command.

    Formatting_Shortcut

    The following screencast shows how the Ctrl+G shortcut initiates a Document Formatting action:

    Formatting_Shortcut

Document Formatting Options

Use the preview box at the bottom of the Editor | C# | Formatting options page to see an example of every item.

Formatting_Spaces

EditorConfig Support

CodeRush supports Visual Studio’s format settings defined in the EditorConfig file. EditorConfig settings have a higher priority than formatting preferences set in the CodeRush options dialog.

The tables below show EditorConfig settings and corresponding CodeRush formatting options.

New Line Options

EditorConfig Setting CodeRush Setting
csharp_new_line_before_open_brace = methods The Braces page: (Method, Simple Method) -> Wrap open brace: Wrap Before
csharp_new_line_before_open_brace = types The Braces page: (Type, Namespace) -> Wrap open brace: Wrap Before
csharp_new_line_before_open_brace = accessors The Braces page: (Accessor, Simple Accessor) -> Wrap open brace: Wrap Before
csharp_new_line_before_open_brace = indexers The Braces page: Property\Indexer\Event -> Wrap open brace: Wrap Before
csharp_new_line_before_open_brace = control_blocks The Braces page: (Statement, Simple Statement) -> Wrap open brace: Wrap Before
csharp_new_line_before_open_brace = lambdas The Braces page: Lambda Expression \ Anonymous Method -> Wrap open brace: Wrap Before
csharp_new_line_before_open_brace = anonymous_methods The Braces page: Lambda Expression \ Anonymous Method -> Wrap open brace: Wrap Before
csharp_new_line_before_open_brace = all For all options above
csharp_new_line_before_open_brace = none For none of the options above
csharp_new_line_before_else = true/false The Other page: Place On New Line -> Place ‘else’ on new line: true/false
csharp_new_line_before_catch = true/false The Other page: Place On New Line -> Place ‘catch’ on new line: true/false
csharp_new_line_before_finally = true/false The Other page: Place On New Line -> Place ‘finally’ on new line: true/false
csharp_new_line_before_members_in_object_initializers = true/false The Wrapping page: Object Initializer -> Wrap separator: Wrap if long or multiline/ the Wrap separator’s current value in your settings
csharp_new_line_between_query_expression_clauses = true/false The Wrapping page: LINQ Expression -> Wrap clauses: Wrap/No Wrap

Indentation Options

EditorConfig Setting CodeRush Setting
csharp_indent_block_contents = true/false The Braces page: (Method, Simple Method, Statement, Simple Statement) -> Align content by -> Indent: 1/0
csharp_indent_braces = true/false The Braces page: (Method, Simple Method, Statement, Simple Statement) -> Wrap open brace-> Align by -> the Indent option: 1/0 and Method -> Wrap close brace -> Align by -> Indent: 1/0

Spacing Options

EditorConfig Setting CodeRush Setting
csharp_space_after_keywords_in_control_flow_statements = true/false The Spacing page: Statement -> Insert space before parentheses: true/false. Note: If this EditorConfig setting is set to true, CodeRush enables the “Insert space before parentheses” option.
csharp_space_between_parentheses = control_flow_statements The Spacing page: Statement -> Insert space within parentheses: true
csharp_space_before_colon_in_inheritance_clause = true/false The Spacing page: Base List -> Insert space before colon: true/false
csharp_space_after_colon_in_inheritance_clause = true/false The Spacing page: Base List -> Insert space after colon: true/false
csharp_space_around_binary_operators = before_and_after/none/ignore The Spacing page: (Arithmetic Expression, Bitwise Expression, Logical Expression, Relational Expression) -> Insert spaces around an operator: true/false/options are disabled for arithmetic, bitwise, logical, and relational expressions
csharp_space_between_method_declaration_parameter_list_parentheses = true/false The Spacing page: Parameters -> Insert spaces within parentheses: true/false
csharp_space_between_method_declaration_empty_parameter_list_parentheses = true/false The Spacing page: Parameters -> Insert space within empty parentheses: true/false
csharp_space_between_method_declaration_name_and_open_parenthesis = true/false The Spacing page: Parameters -> Insert space before parentheses: true/false
csharp_space_between_method_call_parameter_list_parentheses = true/false The Spacing page: Arguments -> Insert spaces within parentheses: true/false
csharp_space_between_method_call_empty_parameter_list_parentheses = true/false The Spacing page: Arguments -> Insert space within empty parentheses: true/false
csharp_space_between_method_call_name_and_opening_parenthesis = true/false The Spacing page: Arguments -> Insert space before parentheses: true/false
csharp_space_after_comma = true/false The Spacing page: (Arguments, Initializer, Parameters, Type Arguments) -> Insert space after a separator (comma): true/false
csharp_space_before_comma = true/false The Spacing page: (Arguments, Initializer, Parameters, Type Arguments) -> Insert space before a separator (comma): true/false
csharp_space_after_semicolon_in_for_statement = true/false The Spacing page: Statement -> Insert space after semicolons in ‘for’ loops: true/false
csharp_space_before_semicolon_in_for_statement = true/false The Spacing page: Statement -> Insert space before semicolons in ‘for’ loops: true/false

Example

The “csharp_new_line_before_open_brace = types, methods” setting overrides the “Wrap open brace” option set to “After” for namespaces, types, methods, and simple methods on the Editor | C# | Formatting | Braces options page.

Formatting_Spaces

The screencast below shows the “csharp_new_line_before_open_brace = types, methods” setting in action:

Formatting_Spaces